StdAccountServiceImpl.java 7.69 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8
package pwc.taxtech.atms.service.impl;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.common.CommonUtils;
frank.xa.zhang's avatar
frank.xa.zhang committed
9 10
import pwc.taxtech.atms.constant.ActiveStatus;
import pwc.taxtech.atms.constant.StandAccountConstant;
eddie.woo's avatar
eddie.woo committed
11
import pwc.taxtech.atms.constant.enums.StdAccountEnum;
eddie.woo's avatar
eddie.woo committed
12
import pwc.taxtech.atms.dao.*;
eddie.woo's avatar
eddie.woo committed
13
import pwc.taxtech.atms.dto.stdaccount.StandardAccountDto;
eddie.woo's avatar
eddie.woo committed
14
import pwc.taxtech.atms.dto.stdaccount.StdAccountFancyTreeDto;
eddie.woo's avatar
eddie.woo committed
15
import pwc.taxtech.atms.entity.*;
16
import pwc.taxtech.atms.exception.ServiceException;
eddie.woo's avatar
eddie.woo committed
17

18
import java.util.*;
eddie.woo's avatar
eddie.woo committed
19 20 21
import java.util.stream.Collectors;

@Service
22
public class StdAccountServiceImpl extends BaseService {
eddie.woo's avatar
eddie.woo committed
23 24 25 26 27 28

    @Autowired
    private StandardAccountMapper standardAccountMapper;

    @Autowired
    private OrganizationMapper organizationMapper;
eddie.woo's avatar
eddie.woo committed
29 30
    @Autowired
    private StandardAccountDao standardAccountDao;
eddie.woo's avatar
eddie.woo committed
31 32 33 34
    @Autowired
    private AccountMappingMapper accountMappingMapper;
    @Autowired
    private EnterpriseAccountMapper enterpriseAccountMapper;
eddie.woo's avatar
eddie.woo committed
35 36 37 38 39 40 41 42 43 44 45

    public List<StdAccountFancyTreeDto> getStdAccountHierarchy(String orgId) throws ServiceException {
        List<StdAccountFancyTreeDto> resultList;
        try {
            if (StringUtils.isBlank(orgId)) {
                return Collections.emptyList();
            }
            Organization organization = organizationMapper.selectByPrimaryKey(orgId);
            if (null == organization) {
                return Collections.emptyList();
            }
46
            String industryId = organization.getIndustryId();
eddie.woo's avatar
eddie.woo committed
47 48 49 50 51
            resultList = new ArrayList<>();
            if (StringUtils.isNotBlank(industryId)) {
                List<StdAccountFancyTreeDto> dtoList = Lists.newArrayList();
                StandardAccountExample example = new StandardAccountExample();
                StandardAccountExample.Criteria criteria = example.createCriteria();
52
                criteria.andIndustryIdEqualTo(industryId).andRuleTypeEqualTo(2).andIsActiveEqualTo(true);
eddie.woo's avatar
eddie.woo committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66
                List<StandardAccount> standardAccountList = standardAccountMapper.selectByExample(example);
                List<StandardAccount> topList = standardAccountList.stream().filter(x -> StringUtils.isBlank(x.getParentCode()))
                        .collect(Collectors.toList());
                for (StandardAccount topNode : topList) {
                    StdAccountFancyTreeDto dto = rotateToDto(topNode);
                    getSubStdAccount(dto, standardAccountList);
                    dtoList.add(dto);
                }
                Map<String, List<StdAccountFancyTreeDto>> map = dtoList.stream().collect(Collectors.groupingBy(x -> x.getAcctProp()));
                for (Map.Entry<String, List<StdAccountFancyTreeDto>> entry : map.entrySet()) {
                    StdAccountFancyTreeDto root = new StdAccountFancyTreeDto();
                    root.setExpanded(false);
                    root.setHasChildren(CollectionUtils.isNotEmpty(entry.getValue()));
                    root.setTitle(StdAccountEnum.AcctProp.MAPPING.get(entry.getKey()));
67 68 69
                    List<StdAccountFancyTreeDto> temp = entry.getValue();
                    root.setChildren(temp.stream().sorted(Comparator.comparing(StdAccountFancyTreeDto::getCode)).collect(Collectors.toList()));
//                    root.setChildren(entry.getValue());
eddie.woo's avatar
eddie.woo committed
70 71 72 73 74 75 76 77 78
                    resultList.add(root);
                }
            }
        } catch (Exception e) {
            throw new ServiceException("getStdAccountHierarchy error.", e);
        }
        return resultList;
    }

79 80
    public List<StandardAccountDto> GetStdAccountLinkEtsAccount(String orgId, String accountSetId) {
        if (StringUtils.isBlank(orgId)) {
eddie.woo's avatar
eddie.woo committed
81 82 83
            return Collections.emptyList();
        }
        //todo
84
        Organization organization = organizationMapper.selectByPrimaryKey(orgId);
eddie.woo's avatar
eddie.woo committed
85 86 87
        if (organization == null) {
            return Collections.emptyList();
        }
eddie.woo's avatar
eddie.woo committed
88 89 90 91
        String industryId = StringUtils.defaultString(organization.getIndustryId(), "0");
        AccountMappingExample example = new AccountMappingExample();
        example.createCriteria().andOrganizationIdEqualTo(orgId).andEnterpriseAccountSetIdEqualTo(accountSetId).andIndustryIdEqualTo(industryId);
        List<AccountMapping> mappingList = accountMappingMapper.selectByExample(example);
eddie.woo's avatar
eddie.woo committed
92

eddie.woo's avatar
eddie.woo committed
93 94
        if (CollectionUtils.isEmpty(mappingList)) {
            return Collections.emptyList();
eddie.woo's avatar
eddie.woo committed
95 96
        }

eddie.woo's avatar
eddie.woo committed
97 98 99
        StandardAccountExample stdExample = new StandardAccountExample();
        stdExample.createCriteria().andCodeIn(mappingList.stream().map(AccountMapping::getStandardAccountCode).collect(Collectors.toList()));
        List<StandardAccount> stdList = standardAccountMapper.selectByExample(stdExample);
eddie.woo's avatar
eddie.woo committed
100

eddie.woo's avatar
eddie.woo committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
        EnterpriseAccountExample etExample = new EnterpriseAccountExample();
        etExample.createCriteria().andEnterpriseAccountSetIdEqualTo(accountSetId).andCodeIn(mappingList.stream()
                .map(AccountMapping::getEnterpriseAccountCode).collect(Collectors.toList()));
        List<EnterpriseAccount> etList = enterpriseAccountMapper.selectByExample(etExample);

        Map<String, AccountMapping> mappingMap = mappingList.stream().collect(Collectors.toMap(AccountMapping::getStandardAccountCode, a -> a, (k1, k2) -> k1));
        Map<String, EnterpriseAccount> etMappingMap = etList.stream().collect(Collectors.toMap(EnterpriseAccount::getCode, a -> a, (k1, k2) -> k1));

        return stdList.stream().map(o -> {
            StandardAccountDto dto = new StandardAccountDto();
            beanUtil.copyProperties(o, dto);
            String etsCode = mappingMap.get(o.getCode()).getEnterpriseAccountCode();
            dto.setEtsCode(etsCode);
            dto.setEtsName(etMappingMap.get(etsCode).getName());
            return dto;
        }).collect(Collectors.toList());
eddie.woo's avatar
eddie.woo committed
117 118
    }

119
    public List<StandardAccountDto> getStdAccountByIndustry(String industryId) {
frank.xa.zhang's avatar
frank.xa.zhang committed
120
        StandardAccountExample example = new StandardAccountExample();
121
        example.createCriteria().andRuleTypeEqualTo((int) StandAccountConstant.TWO).andIsActiveEqualTo(ActiveStatus.Active).andIndustryIdEqualTo(industryId);
frank.xa.zhang's avatar
frank.xa.zhang committed
122

eddie.woo's avatar
eddie.woo committed
123
        example.setOrderByClause("Code");
frank.xa.zhang's avatar
frank.xa.zhang committed
124 125 126 127 128 129 130 131 132 133 134 135 136

        List<StandardAccount> items = standardAccountMapper.selectByExample(example);
        List<StandardAccountDto> dtos = new ArrayList<>();

        for (StandardAccount item : items) {
            StandardAccountDto dto = new StandardAccountDto();
            CommonUtils.copyProperties(item, dto);
            dto.setTitle(item.getCode() + "-" + (item.getDirection().equals(1) ? "借" : "贷") + "-" + item.getName());
            dtos.add(dto);
        }
        return dtos;
    }

eddie.woo's avatar
eddie.woo committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    private void getSubStdAccount(StdAccountFancyTreeDto node, List<StandardAccount> allList) {
        List<StdAccountFancyTreeDto> childList = allList.stream().filter(x -> StringUtils.equals(node.getCode(), x.getParentCode()))
                .map(this::rotateToDto).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(childList)) {
            node.setChildren(childList);
            node.setHasChildren(true);
            for (StdAccountFancyTreeDto tmp : childList) {
                getSubStdAccount(tmp, allList);
            }
        }
    }

    private StdAccountFancyTreeDto rotateToDto(StandardAccount account) {
        StdAccountFancyTreeDto dto = new StdAccountFancyTreeDto();
        dto.setExpanded(false);
        dto.setHasChildren(false);
eddie.woo's avatar
eddie.woo committed
153 154 155
        CommonUtils.copyProperties(account, dto);
        dto.setTitle(dto.getCode() + "-" + (StringUtils.equals(dto.getDirection(), "1") ? "借" : "贷") + "-" + dto.getName());
        return dto;
eddie.woo's avatar
eddie.woo committed
156 157
    }
}