package pwc.taxtech.atms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pwc.taxtech.atms.common.CommonConstants;
import pwc.taxtech.atms.common.CommonUtils;
import pwc.taxtech.atms.entitiy.AccountMapping;
import pwc.taxtech.atms.entitiy.AccountMappingExample;
import pwc.taxtech.atms.entitiy.EnterpriseAccount;
import pwc.taxtech.atms.service.AccountService;
import pwc.taxtech.atms.service.EnterpriseAccountService;

@Service
public class AccountServiceImpl extends AbstractService implements AccountService {

    @Autowired EnterpriseAccountService enterpriseAccountService;
    
    /* (non-Javadoc)
     * @see pwc.taxtech.atms.service.AccountService#mapStdAccountByCode(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Integer)
     */
    @Override
    public void mapStdAccountByCode(String enterpriseAccountSetID, String stdAccountCode, String epAccountCode, String industryID, String organizationID, 
            Boolean bOverwrite, Integer acctLevel) {

        //set default value
        if(bOverwrite == null) {
            bOverwrite = true;
        }
        if(acctLevel == null) {
            acctLevel = 0;
        }
        
        EnterpriseAccount epAccount = enterpriseAccountService.getEnterpriseAccount(epAccountCode, enterpriseAccountSetID);
        if (epAccount == null) {
            return;
        }
        
        //set stdAccountCode
        if(stdAccountCode==null || stdAccountCode.isEmpty()) {
            stdAccountCode = CommonConstants.NullStdCode;
        }
        
        if (!bOverwrite) {
            if(!isStdExists(enterpriseAccountSetID, epAccountCode, industryID, organizationID)) {
                deleteAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetID(), organizationID, industryID);
                updateStdAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetID(), stdAccountCode, industryID, organizationID);
            }
        }
        else {
            deleteAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetID(), organizationID, industryID);
            updateStdAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetID(), stdAccountCode, industryID, organizationID);
        }
    }
    
    /**
     * 删除对应关系
     * @param epAccountCode
     * @param enterpriseAccountSetId
     * @param organizaionID
     * @param industryID
     */
    private void deleteAccountMapping(String epAccountCode, String enterpriseAccountSetId, String organizaionID,
            String industryID) {

        AccountMappingExample accountMappingExample = new AccountMappingExample();
        accountMappingExample.createCriteria()
            .andEnterpriseAccountCodeEqualTo(epAccountCode)
            .andEnterpriseAccountSetIDEqualTo(enterpriseAccountSetId)
            .andOrganizationIDEqualTo(organizaionID)
            .andIndustryIDEqualTo(industryID);
        
        accountMappingMapper.deleteByExample(accountMappingExample);
    }

    /**
     * 插入对应关系到对应关系表
     * @param epAccountCode
     * @param enterpriseAccountSetId
     * @param stdAccountCode
     * @param industryID
     * @param enterpriseAccountID
     * @param organizationId
     */
    private void updateStdAccountMapping(String epAccountCode, String enterpriseAccountSetID,
            String stdAccountCode, String industryID, String organizationID) {
        
        AccountMapping accountMapping = new AccountMapping();
        accountMapping.setID(CommonUtils.getUUID());
        accountMapping.setEnterpriseAccountSetID(enterpriseAccountSetID);
        accountMapping.setEnterpriseAccountCode(epAccountCode);
        accountMapping.setStandardAccountCode(stdAccountCode);
        accountMapping.setIndustryID(industryID);
        accountMapping.setOrganizationID(organizationID);
        accountMappingMapper.insert(accountMapping);
    }

    private boolean isStdExists(String enterpriseAccountSetID, String enterpirseAccountCode, String industryID, String organizationID) {
        
        Long stdAccountsCount = customAccountMapper.countStandardAccounts(enterpriseAccountSetID, enterpirseAccountCode, industryID, organizationID);
        
        return stdAccountsCount>=1;
    }
    
}