AccountServiceImpl.java 4.4 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6
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;
7 8 9
import pwc.taxtech.atms.entity.AccountMapping;
import pwc.taxtech.atms.entity.AccountMappingExample;
import pwc.taxtech.atms.entity.EnterpriseAccount;
eddie.woo's avatar
eddie.woo committed
10 11

@Service
12 13 14
public class AccountServiceImpl extends AbstractService {

    @Autowired
15
    EnterpriseAccountServiceImpl enterpriseAccountService;
eddie.woo's avatar
eddie.woo committed
16

17 18
    /**
     * 匹配标准科目到企业科目(按科目编码)
19
     *
20
     * @param enterpriseAccountSetId - 账套Id
21 22 23 24 25 26
     * @param stdAccountCode         - 标准科目编码
     * @param epAccountCode          - 企业科目编码
     * @param industryId             - 行业Id
     * @param organizationId         - 机构Id
     * @param bOverwrite             - 是否覆盖已有对应
     * @param acctLevel              - 科目级别
eddie.woo's avatar
eddie.woo committed
27
     */
28
    public void mapStdAccountByCode(String enterpriseAccountSetId, String stdAccountCode, String epAccountCode, String industryId, String organizationId,
29
                                    Boolean bOverwrite, Integer acctLevel) {
eddie.woo's avatar
eddie.woo committed
30 31

        //set default value
32
        if (bOverwrite == null) {
eddie.woo's avatar
eddie.woo committed
33 34
            bOverwrite = true;
        }
35
        if (acctLevel == null) {
eddie.woo's avatar
eddie.woo committed
36 37
            acctLevel = 0;
        }
38

39
        EnterpriseAccount epAccount = enterpriseAccountService.getEnterpriseAccount(epAccountCode, enterpriseAccountSetId);
eddie.woo's avatar
eddie.woo committed
40 41 42
        if (epAccount == null) {
            return;
        }
43

eddie.woo's avatar
eddie.woo committed
44
        //set stdAccountCode
45
        if (stdAccountCode == null || stdAccountCode.isEmpty()) {
eddie.woo's avatar
eddie.woo committed
46 47
            stdAccountCode = CommonConstants.NullStdCode;
        }
48

eddie.woo's avatar
eddie.woo committed
49
        if (!bOverwrite) {
50
            if (!isStdExists(enterpriseAccountSetId, epAccountCode, industryId, organizationId)) {
51 52
                deleteAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetId(), organizationId, industryId);
                updateStdAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetId(), stdAccountCode, industryId, organizationId);
eddie.woo's avatar
eddie.woo committed
53
            }
54
        } else {
55 56
            deleteAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetId(), organizationId, industryId);
            updateStdAccountMapping(epAccountCode, epAccount.getEnterpriseAccountSetId(), stdAccountCode, industryId, organizationId);
eddie.woo's avatar
eddie.woo committed
57 58
        }
    }
59

eddie.woo's avatar
eddie.woo committed
60 61
    /**
     * 删除对应关系
62
     *
eddie.woo's avatar
eddie.woo committed
63 64
     * @param epAccountCode
     * @param enterpriseAccountSetId
65 66
     * @param organizaionId
     * @param industryId
eddie.woo's avatar
eddie.woo committed
67
     */
68
    private void deleteAccountMapping(String epAccountCode, String enterpriseAccountSetId, String organizaionId,
69
                                      String industryId) {
eddie.woo's avatar
eddie.woo committed
70 71 72

        AccountMappingExample accountMappingExample = new AccountMappingExample();
        accountMappingExample.createCriteria()
73 74 75 76 77
                .andEnterpriseAccountCodeEqualTo(epAccountCode)
                .andEnterpriseAccountSetIdEqualTo(enterpriseAccountSetId)
                .andOrganizationIdEqualTo(organizaionId)
                .andIndustryIdEqualTo(industryId);

eddie.woo's avatar
eddie.woo committed
78 79 80 81 82
        accountMappingMapper.deleteByExample(accountMappingExample);
    }

    /**
     * 插入对应关系到对应关系表
83
     *
eddie.woo's avatar
eddie.woo committed
84 85 86
     * @param epAccountCode
     * @param enterpriseAccountSetId
     * @param stdAccountCode
87 88
     * @param industryId
     * @param enterpriseAccountId
eddie.woo's avatar
eddie.woo committed
89 90
     * @param organizationId
     */
91
    private void updateStdAccountMapping(String epAccountCode, String enterpriseAccountSetId,
92 93
                                         String stdAccountCode, String industryId, String organizationId) {

eddie.woo's avatar
eddie.woo committed
94
        AccountMapping accountMapping = new AccountMapping();
95 96
        accountMapping.setId(CommonUtils.getUUID());
        accountMapping.setEnterpriseAccountSetId(enterpriseAccountSetId);
eddie.woo's avatar
eddie.woo committed
97 98
        accountMapping.setEnterpriseAccountCode(epAccountCode);
        accountMapping.setStandardAccountCode(stdAccountCode);
99 100
        accountMapping.setIndustryId(industryId);
        accountMapping.setOrganizationId(organizationId);
eddie.woo's avatar
eddie.woo committed
101 102 103
        accountMappingMapper.insert(accountMapping);
    }

104
    private boolean isStdExists(String enterpriseAccountSetId, String enterpirseAccountCode, String industryId, String organizationId) {
105

106
        Long stdAccountsCount = customAccountMapper.countStandardAccounts(enterpriseAccountSetId, enterpirseAccountCode, industryId, organizationId);
107 108

        return stdAccountsCount >= 1;
eddie.woo's avatar
eddie.woo committed
109
    }
110

eddie.woo's avatar
eddie.woo committed
111
}