package com.jeesharp.business.modules.food.service.impl;

import com.jeesharp.business.modules.food.entity.FoodCompanyAccount;
import com.jeesharp.business.modules.food.mapper.FoodCompanyAccountMapper;
import com.jeesharp.business.modules.food.service.FoodCompanyAccountService;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import com.jeesharp.framework.pojo.query.PaginationVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class FoodCompanyAccountServiceImpl extends CrudService<FoodCompanyAccountMapper, FoodCompanyAccount, PaginationVO> implements FoodCompanyAccountService {

    private static final int STATUS_ENABLED = 1;

    @Override
    public FoodCompanyAccount getByCompanyId(String companyId, String tenantCode) {
        return getBaseMapper().selectByCompanyId(companyId, tenantCode);
    }

    @Override
    public FoodCompanyAccount getEnabledByCompanyId(String companyId, String tenantCode) {
        FoodCompanyAccount account = getByCompanyId(companyId, tenantCode);
        if (account == null || !Integer.valueOf(STATUS_ENABLED).equals(account.getStatus())) {
            return null;
        }
        return account;
    }

    @Override
    public FoodCompanyAccount lockById(String id, String tenantCode) {
        return getBaseMapper().selectByIdForUpdate(id, tenantCode);
    }

    @Override
    public FoodCompanyAccount lockByCompanyId(String companyId, String tenantCode) {
        return getBaseMapper().selectByCompanyIdForUpdate(companyId, tenantCode);
    }

    @Override
    @Transactional(readOnly = false)
    public void insertAccount(FoodCompanyAccount account) {
        account.setIsNewRecord(true);
        account.preInsert();
        getBaseMapper().insert(account);
    }

    @Override
    @Transactional(readOnly = false)
    public void updateAccount(FoodCompanyAccount account) {
        account.preUpdate();
        getBaseMapper().updateById(account);
    }
}
