KeyValueConfigServiceImpl.java 12.8 KB
Newer Older
1 2
package pwc.taxtech.atms.service.impl;

3
import com.alibaba.druid.util.StringUtils;
4 5
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
import org.nutz.lang.Strings;
6 7 8 9 10
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import pwc.taxtech.atms.common.ApplyScope;
import pwc.taxtech.atms.common.CommonUtils;
11 12 13
import pwc.taxtech.atms.common.OperateLogType;
import pwc.taxtech.atms.common.OperationModule;
import pwc.taxtech.atms.constant.enums.KeyValueConfigType;
14 15 16
import pwc.taxtech.atms.dpo.AnalyticsModelDetail;
import pwc.taxtech.atms.dpo.FinancialStatementDetail;
import pwc.taxtech.atms.dpo.TaxReturnDetail;
17 18 19 20
import pwc.taxtech.atms.dto.AddKeyValueConfigDto;
import pwc.taxtech.atms.dto.KeyValueConfigDisplayDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.UpdateKeyValueConfigDto;
21
import pwc.taxtech.atms.dto.formula.FormulaConfigDto;
22
import pwc.taxtech.atms.entity.*;
23 24 25 26 27 28 29 30 31

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
32 33

@Service
34
public class KeyValueConfigServiceImpl extends AbstractService {
35

36 37
    private static final Logger logger = LoggerFactory.getLogger(KeyValueConfigServiceImpl.class);

38 39
    private OperateLogType logType = OperateLogType.OperationLogKeyvalue;

40 41 42 43 44
    public List<KeyValueConfigDisplayDto> get() {
        List<KeyValueConfig> list = keyValueConfigMapper.selectKeyValueConfigsByOrderByCreateTime();
        List<KeyValueConfigDisplayDto> result = new ArrayList<>();
        if (list.size() > 0) {
            Map<String, String> industryList = new HashMap<>();
45
            industryMapper.selectByExample(new IndustryExample()).forEach(item -> industryList.put(item.getId(), item.getName()));
46
            Map<String, String> serviceType = new HashMap<>();
47
            serviceTypeMapper.selectByExample(new ServiceTypeExample()).forEach(item -> serviceType.put(item.getId(), item.getName()));
48 49 50 51
            List<KeyValueReference> scopes = keyValueReferenceMapper.selectByExample(new KeyValueReferenceExample());
            for (KeyValueConfig keyValueConfig : list) {
                KeyValueConfigDisplayDto dtoObj = new KeyValueConfigDisplayDto();
                CommonUtils.copyProperties(keyValueConfig, dtoObj);
52 53
                dtoObj.setServiceTypes(getNamesByIds(keyValueConfig.getServiceTypeIds(), serviceType));
                dtoObj.setIndustrys(getNamesByIds(keyValueConfig.getIndustryIds(), industryList));
frank.xa.zhang's avatar
frank.xa.zhang committed
54 55
                dtoObj.setIndustryIDs(Arrays.asList(keyValueConfig.getIndustryIds().split(",")));
                dtoObj.setServiceTypeIDs(Arrays.asList(keyValueConfig.getServiceTypeIds().split(",")));
56
                List<KeyValueReference> selectScopeList = scopes.stream().filter(item -> item.getKeyValueConfigId().equals(keyValueConfig.getId())).collect(Collectors.toList());
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 82
                List<Integer> selectScopes = selectScopeList.stream().map(KeyValueReference::getScope).distinct().collect(Collectors.toList());
                Map<Integer, Integer> scopeCount = new HashMap<>();
                if (selectScopeList.size() > 0) {
                    for (Integer selectScope : selectScopes) {
                        Integer count = (int) (selectScopeList.stream().filter(a -> a.getScope().equals(selectScope)).count());
                        scopeCount.put(selectScope, count);
                    }
                }
                dtoObj.setScopeCount(scopeCount);
                dtoObj.setScopeSummary(jointToString(selectScopes));
                result.add(dtoObj);
            }
        }
        return result;
    }

    public OperationResultDto<List<FormulaConfigDto>> getAllFormulaList() {
        OperationResultDto<List<FormulaConfigDto>> result = new OperationResultDto<>();
        List<FormulaConfig> dbItems = formulaConfigMapper.selectByExample(new FormulaConfigExample());
        List<FormulaConfigDto> items = new ArrayList<>();
        CommonUtils.copyProperties(dbItems, items);
        result.setData(items);
        result.setResult(true);
        return result;
    }

83
    public List<FinancialStatementDetail> getFinacialStatement(String configurationId) {
84
        Map<String, String> jointObjectMap = new HashMap<>();
85 86
        industryMapper.selectByExample(new IndustryExample()).forEach(a -> jointObjectMap.put(a.getId(), a.getName()));
        List<FinancialStatementDetail> referenceFinance = keyValueReferenceMapper.getFinancialStatementDetails(configurationId);
87
        for (FinancialStatementDetail item : referenceFinance) {
88
            item.setIndustry(getNamesByIds(item.getIndustry(), jointObjectMap));
89 90
        }
        return referenceFinance;
91 92
    }

93
    public List<TaxReturnDetail> getTaxReturn(String configurationId) {
94
        Map<String, String> jointObjectMap = new HashMap<>();
95 96
        industryMapper.selectByExample(new IndustryExample()).forEach(a -> jointObjectMap.put(a.getId(), a.getName()));
        List<TaxReturnDetail> referenceTax = keyValueReferenceMapper.getTaxReturnDetails(configurationId);
97
        for (TaxReturnDetail item : referenceTax) {
98
            item.setIndustry(getNamesByIds(item.getIndustry(), jointObjectMap));
99 100
        }
        return referenceTax;
101 102
    }

103
    public List<AnalyticsModelDetail> getAnalyticsModel(String configurationId) {
104
        Map<String, String> industryList = new HashMap<>();
105
        industryMapper.selectByExample(new IndustryExample()).forEach(a -> industryList.put(a.getId(), a.getName()));
106
        Map<String, String> organization = new HashMap<>();
107 108
        organizationMapper.selectByExample(new OrganizationExample()).forEach(a -> organization.put(a.getId(), a.getName()));
        List<AnalyticsModelDetail> referenceModel = keyValueReferenceMapper.getAnalyticsModelDetails(configurationId);
109
        for (AnalyticsModelDetail analyticsModelDetail : referenceModel) {
110 111
            analyticsModelDetail.setIndustry(getNamesByIds(analyticsModelDetail.getIndustry(), industryList));
            analyticsModelDetail.setEntityName(getNamesByIds(analyticsModelDetail.getEntityId(), organization));
112 113
        }
        return referenceModel;
114 115
    }

116
    public int deleteKeyValueConfiguration(String keyId) {
117
        KeyValueReferenceExample example = new KeyValueReferenceExample();
118
        example.createCriteria().andKeyValueConfigIdEqualTo(keyId);
119 120 121
        int referenceCount = (int) keyValueReferenceMapper.countByExample(example);
        if (referenceCount == 0) {
            KeyValueConfigExample kvcExample = new KeyValueConfigExample();
122
            kvcExample.createCriteria().andIdEqualTo(keyId).andKeyValueTypeEqualTo(KeyValueConfigType.Customize.getCode());
123 124
            Optional<KeyValueConfig> deleteKey = keyValueConfigMapper.selectByExample(kvcExample).stream().findFirst();
            if (deleteKey.isPresent()) {
125
                keyValueConfigMapper.deleteByPrimaryKey(deleteKey.get().getId());
126 127 128 129 130 131 132 133
                operationLogService.deleteDataAddLog(deleteKey, OperationModule.KeyValueConfig, ""
                        , "DeleteKeyValueConfiguration", "", deleteKey.get().getName(), logType);
                referenceCount = 1;
            }
        } else {
            referenceCount = -1;
        }
        return referenceCount;
134 135 136
    }

    public void addKeyValueConfiguration(AddKeyValueConfigDto addKeyValueConfigDto) {
137 138 139
        Date now = new Date();
        KeyValueConfig keyValueConfig = new KeyValueConfig();
        CommonUtils.copyProperties(addKeyValueConfigDto, keyValueConfig);
140 141
        keyValueConfig.setIndustryIds(Strings.join(",", addKeyValueConfigDto.getIndustryIds()));
        keyValueConfig.setServiceTypeIds(Strings.join(",", addKeyValueConfigDto.getServiceTypeIds()));
142
        String guid = CommonUtils.getUUID();
143
        keyValueConfig.setId(guid);
144 145 146 147 148 149 150 151
        keyValueConfig.setKeyCode(guid.replaceAll("-", ""));
        keyValueConfig.setIsConstant(0);
        keyValueConfig.setCalculateStatus(99);
        keyValueConfig.setCreateTime(now);
        keyValueConfig.setUpdateTime(now);
        keyValueConfig.setUpdator(addKeyValueConfigDto.getUserName());
        keyValueConfig.setKeyValueType(2);

152
        keyValueConfigMapper.insertSelective(keyValueConfig);
153 154
        operationLogService.addDataAddLog(keyValueConfig, OperationModule.KeyValueConfig, addKeyValueConfigDto.getUserName()
                , Message.Log.AddKeyValueConfiguration, "", keyValueConfig.getName(), logType);
155 156 157
    }

    public void updateKeyValueConfiguration(UpdateKeyValueConfigDto updateKeyValueConfigDto) {
158
        KeyValueConfig keyValueConfig = keyValueConfigMapper.selectByPrimaryKey(updateKeyValueConfigDto.getId());
159 160 161 162 163 164 165 166 167 168 169

        if (keyValueConfig == null) {
            throw new InvalidOperationException("Not found");
        }

        KeyValueConfig original = new KeyValueConfig();
        CommonUtils.copyProperties(keyValueConfig, original);
        keyValueConfig.setFormula(updateKeyValueConfigDto.getFormula());
        keyValueConfig.setDescription(updateKeyValueConfigDto.getDescription());
        keyValueConfig.setName(updateKeyValueConfigDto.getName());
        keyValueConfig.setDataSource(updateKeyValueConfigDto.getDataSource());
170 171
        keyValueConfig.setServiceTypeIds(Strings.join(",", updateKeyValueConfigDto.getServiceTypeIds()));
        keyValueConfig.setIndustryIds(Strings.join(",", updateKeyValueConfigDto.getIndustryIds()));
172 173

        keyValueConfigMapper.updateByPrimaryKey(keyValueConfig);
174

175 176 177
        operationLogService.updateDataAddLog(original, keyValueConfig, OperationModule.KeyValueConfig
                , updateKeyValueConfigDto.getUserName(), Message.Log.UpdateKeyValueConfiguration
                , keyValueConfig.getName(), "Operation content", logType);
178 179 180
    }

    public boolean isKeyValueDuplicated(String name) {
181 182 183 184
        KeyValueConfigExample example = new KeyValueConfigExample();
        example.createCriteria().andNameEqualTo(name);
        Optional<KeyValueConfig> keyValueConfig = keyValueConfigMapper.selectByExample(example).stream().findFirst();
        return keyValueConfig.isPresent();
185 186
    }

187 188
    public List<KeyValueConfigDisplayDto> getByOrgId(String orgId) {
        Organization organization = organizationMapper.selectByPrimaryKey(orgId);
189 190 191 192
        if (organization == null) {
            return new ArrayList<>();
        }
        KeyValueConfigExample example = new KeyValueConfigExample();
193
        example.createCriteria().andIndustryIdsLike(organization.getIndustryId());
194 195 196 197
        List<KeyValueConfig> list = keyValueConfigMapper.selectByExample(example);
        List<KeyValueConfigDisplayDto> result = new ArrayList<>();
        if (list.size() > 0) {
            Map<String, String> industryList = new HashMap<>();
198
            industryMapper.selectByExample(new IndustryExample()).forEach(a -> industryList.put(a.getId(), a.getName()));
199
            Map<String, String> serviceType = new HashMap<>();
200
            serviceTypeMapper.selectByExample(new ServiceTypeExample()).forEach(a -> serviceType.put(a.getId(), a.getName()));
201 202 203 204
            List<KeyValueReference> scopes = keyValueReferenceMapper.selectByExample(new KeyValueReferenceExample());
            for (KeyValueConfig item : list) {
                KeyValueConfigDisplayDto dtoObj = new KeyValueConfigDisplayDto();
                CommonUtils.copyProperties(item, dtoObj);
205 206
                dtoObj.setServiceTypes(getNamesByIds(item.getServiceTypeIds(), serviceType));
                dtoObj.setIndustrys(getNamesByIds(item.getIndustryIds(), industryList));
frank.xa.zhang's avatar
frank.xa.zhang committed
207
                dtoObj.setIndustryIDs(new ArrayList<>(Arrays.asList(item.getIndustryIds().split(","))));
208
                List<Integer> selectScopes = scopes.stream().filter(a -> a.getKeyValueConfigId().equals(item.getId()))
209 210 211 212 213 214
                        .map(KeyValueReference::getScope).collect(Collectors.toList());
                dtoObj.setScopeSummary(jointToString(selectScopes));
                result.add(dtoObj);
            }
        }
        return result;
215 216 217 218 219 220 221 222 223 224
    }

    private String jointToString(List<Integer> selectScopes) {
        StringBuilder scopeBuilder = new StringBuilder();
        for (Integer selectScope : selectScopes) {
            scopeBuilder.append(ApplyScope.getName(selectScope)).append(",");
        }
        return scopeBuilder.length() > 0 ? scopeBuilder.toString().substring(0, scopeBuilder.length() - 2) : "";
    }

225
    private String getNamesByIds(String source, Map<String, String> queryList) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239
        if (StringUtils.isEmpty(source)) {
            return source;
        }
        String[] sArray = source.split(",");
        List<String> sName = new ArrayList<>();
        for (String s : sArray) {
            if (queryList.containsKey(s)) {
                sName.add(queryList.get(s));
            }
        }
        StringBuilder retStr = new StringBuilder();
        sName.forEach(s -> retStr.append(s).append(","));
        return retStr.toString().substring(0, retStr.length() - 2);
    }
240 241

}