StdAccountServiceImpl.java 6.29 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;
9
import pwc.taxtech.atms.exception.ServiceException;
frank.xa.zhang's avatar
frank.xa.zhang committed
10 11
import pwc.taxtech.atms.constant.ActiveStatus;
import pwc.taxtech.atms.constant.StandAccountConstant;
eddie.woo's avatar
eddie.woo committed
12 13 14
import pwc.taxtech.atms.constant.enums.StdAccountEnum;
import pwc.taxtech.atms.dao.OrganizationMapper;
import pwc.taxtech.atms.dao.StandardAccountMapper;
15
import pwc.taxtech.atms.dao.StandardAccountDao;
eddie.woo's avatar
eddie.woo committed
16
import pwc.taxtech.atms.dto.stdaccount.StandardAccountDto;
eddie.woo's avatar
eddie.woo committed
17
import pwc.taxtech.atms.dto.stdaccount.StdAccountFancyTreeDto;
18 19 20
import pwc.taxtech.atms.entity.Organization;
import pwc.taxtech.atms.entity.StandardAccount;
import pwc.taxtech.atms.entity.StandardAccountExample;
eddie.woo's avatar
eddie.woo committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
import pwc.taxtech.atms.service.StdAccountService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StdAccountServiceImpl extends BaseService implements StdAccountService {

    @Autowired
    private StandardAccountMapper standardAccountMapper;

    @Autowired
    private OrganizationMapper organizationMapper;
eddie.woo's avatar
eddie.woo committed
37 38
    @Autowired
    private StandardAccountDao standardAccountDao;
eddie.woo's avatar
eddie.woo committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    @Override
    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();
            }
            String industryId = organization.getIndustryID();
            resultList = new ArrayList<>();
            if (StringUtils.isNotBlank(industryId)) {
                List<StdAccountFancyTreeDto> dtoList = Lists.newArrayList();
                StandardAccountExample example = new StandardAccountExample();
                StandardAccountExample.Criteria criteria = example.createCriteria();
                criteria.andIndustryIDEqualTo(industryId).andRuleTypeEqualTo(2).andIsActiveEqualTo(true);
                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()));
                    root.setChildren(entry.getValue());
                    resultList.add(root);
                }
            }
        } catch (Exception e) {
            throw new ServiceException("getStdAccountHierarchy error.", e);
        }
        return resultList;
    }

eddie.woo's avatar
eddie.woo committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    @Override
    public List<StandardAccountDto> GetStdAccountLinkEtsAccount(String orgID, String accountSetID) {
        if (StringUtils.isBlank(orgID)) {
            return Collections.emptyList();
        }
        //todo
        Organization organization = organizationMapper.selectByPrimaryKey(orgID);
        if (organization == null) {
            return Collections.emptyList();
        }
        String industryID = organization.getIndustryID();

        List<StandardAccount> stdAccounts = standardAccountDao.getStdAccount(StdAccountEnum.RuleType.TWO, industryID, true, true);

        if (CollectionUtils.isEmpty(stdAccounts)) {
            stdAccounts = standardAccountDao.getStdAccount(StdAccountEnum.RuleType.TWO, "0", true, true);
        }


        return Collections.emptyList();
    }

frank.xa.zhang's avatar
frank.xa.zhang committed
104
    @Override
105
    public List<StandardAccountDto> getStdAccountByIndustry(String industryID) {
frank.xa.zhang's avatar
frank.xa.zhang committed
106 107 108
        StandardAccountExample example = new StandardAccountExample();
        example.createCriteria().andRuleTypeEqualTo((int)StandAccountConstant.TWO).andIsActiveEqualTo(ActiveStatus.Active).andIndustryIDEqualTo(industryID);

eddie.woo's avatar
eddie.woo committed
109
        example.setOrderByClause("Code");
frank.xa.zhang's avatar
frank.xa.zhang committed
110 111 112 113 114 115 116 117 118 119 120 121 122

        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
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    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
139 140 141
        CommonUtils.copyProperties(account, dto);
        dto.setTitle(dto.getCode() + "-" + (StringUtils.equals(dto.getDirection(), "1") ? "借" : "贷") + "-" + dto.getName());
        return dto;
eddie.woo's avatar
eddie.woo committed
142 143
    }
}