| | |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.excel.util.ListUtils; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.google.common.collect.Lists; |
| | | import com.ruoyi.common.enums.*; |
| | | import com.ruoyi.common.enums.CalculateTypeEnum; |
| | | import com.ruoyi.common.enums.FieldInputTypeEnum; |
| | | import com.ruoyi.common.enums.FieldTypeEnum; |
| | | import com.ruoyi.common.enums.ReportingStatusEnum; |
| | | import com.ruoyi.common.enums.ShowStatusEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.CalculateUtil; |
| | | import com.ruoyi.system.domain.*; |
| | | import com.ruoyi.system.service.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import com.ruoyi.common.utils.CollUtils; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.system.domain.TbBasicData; |
| | | import com.ruoyi.system.domain.TbBasicDataConfig; |
| | | import com.ruoyi.system.domain.TbBasicDataConfigDetail; |
| | | import com.ruoyi.system.domain.TbBasicDataField; |
| | | import com.ruoyi.system.domain.TbField; |
| | | import com.ruoyi.system.domain.TbScore; |
| | | import com.ruoyi.system.service.TbBasicDataConfigDetailService; |
| | | import com.ruoyi.system.service.TbBasicDataConfigService; |
| | | import com.ruoyi.system.service.TbBasicDataFieldService; |
| | | import com.ruoyi.system.service.TbBasicDataService; |
| | | import com.ruoyi.system.service.TbFieldService; |
| | | import com.ruoyi.system.service.TbScoreService; |
| | | import java.time.LocalDate; |
| | | import java.util.*; |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | import java.util.Optional; |
| | | import java.util.stream.Collectors; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | @Slf4j |
| | | public class BasicDataListener extends AnalysisEventListener<Map<Integer, String>> { |
| | | |
| | | /** |
| | | * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
| | | * 每隔5条存储数据库,实际使用中可以1000条,然后清理list ,方便内存回收 |
| | | */ |
| | | private static final int BATCH_COUNT = 100; |
| | | private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
| | | private static final int BATCH_COUNT = 1000; |
| | | private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize( |
| | | BATCH_COUNT); |
| | | public TbBasicDataService tbBasicDataService; |
| | | public List<TbField> fieldList; |
| | | public TbFieldService tbFieldService; |
| | | public String areaCode; |
| | | public TbBasicDataFieldService tbBasicDataFieldService; |
| | | public TbBasicDataConfigService tbBasicDataConfigService; |
| | | public TbBasicDataConfigDetailService tbBasicDataConfigDetailService; |
| | | public TbScoreService tbScoreService; |
| | | public BasicDataListener() { |
| | | } |
| | | |
| | | public BasicDataListener(TbBasicDataService tbBasicDataService, List<TbField> fieldList,TbFieldService tbFieldService, |
| | | String areaCode,TbBasicDataFieldService tbBasicDataFieldService,TbBasicDataConfigService tbBasicDataConfigService, |
| | | TbBasicDataConfigDetailService tbBasicDataConfigDetailService,TbScoreService tbScoreService ) { |
| | | public BasicDataListener(TbBasicDataService tbBasicDataService, |
| | | TbFieldService tbFieldService, |
| | | String areaCode, TbBasicDataFieldService tbBasicDataFieldService, |
| | | TbBasicDataConfigService tbBasicDataConfigService, |
| | | TbBasicDataConfigDetailService tbBasicDataConfigDetailService, |
| | | TbScoreService tbScoreService) { |
| | | this.tbBasicDataService = tbBasicDataService; |
| | | this.fieldList = fieldList; |
| | | this.tbFieldService = tbFieldService; |
| | | this.areaCode = areaCode; |
| | | this.tbBasicDataFieldService = tbBasicDataFieldService; |
| | |
| | | log.info("所有数据解析完成!"); |
| | | } |
| | | |
| | | private static String validateFields(Map.Entry<Integer, String> integerStringEntry, |
| | | Map<Integer, String> dataMap, TbField field) { |
| | | String value = dataMap.get(integerStringEntry.getKey()); |
| | | if (FieldTypeEnum.NUMBER.equals(field.getFieldType())) { |
| | | Integer numMin = field.getNumMin(); |
| | | Integer numMax = field.getNumMax(); |
| | | if (Objects.nonNull(numMin) && Objects.nonNull(numMax)) { |
| | | if (numMin > Integer.parseInt(value) || numMax < Integer.parseInt(value)) { |
| | | throw new ServiceException( |
| | | String.format("字段(%s)的内容不在%d~%d范围内", field.getFieldName(), |
| | | numMin, numMax)); |
| | | } |
| | | } |
| | | } |
| | | if (FieldInputTypeEnum.MANUAL_INPUT.equals(field.getTextInputType()) |
| | | && FieldTypeEnum.TEXT.equals(field.getFieldType())) { |
| | | Integer textMinNum = field.getTextMinNum(); |
| | | Integer textMaxNum = field.getTextMaxNum(); |
| | | if (Objects.nonNull(textMinNum) && Objects.nonNull(textMaxNum)) { |
| | | if (textMinNum > value.length() || textMaxNum < value.length()) { |
| | | throw new ServiceException( |
| | | String.format("字段(%s)的内容长度超出%d~%d的范围", field.getFieldName(), |
| | | textMinNum, textMaxNum)); |
| | | } |
| | | } |
| | | } |
| | | if (FieldTypeEnum.PERCENTAGE.equals(field.getFieldType())) { |
| | | if (0 > Double.parseDouble(value) || 100 < Double.parseDouble(value)) { |
| | | throw new ServiceException( |
| | | String.format("字段(%s)的内容不在0~100范围内", field.getFieldName())); |
| | | } |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | /** |
| | | * 加上存储数据库 |
| | | */ |
| | | private TbBasicData saveData() { |
| | | private TbBasicData saveData() throws Exception { |
| | | // 查询需要填写的动态字段 |
| | | List<TbField> fieldList = |
| | | tbFieldService.lambdaQuery().eq(TbField::getStatus, ShowStatusEnum.SHOW).list(); |
| | | Map<Integer, String> headMap = cachedDataList.get(cachedDataList.size()-3); |
| | | Map<Integer, String> dataMap = cachedDataList.get(cachedDataList.size()-1); |
| | | log.info("{}条数据,开始存储数据库!", cachedDataList.size()); |
| | |
| | | log.info("填写的数据:{}", JSON.toJSONString(dataMap)); |
| | | int remarkIndex = headMap.size() - 1; |
| | | Map<Integer, String> dynamicFieldsMap = headMap.entrySet().stream() |
| | | .filter(entry -> !(Lists.newArrayList(0, 1, 2, 3).contains(entry.getKey()) || entry.getKey() == remarkIndex)) |
| | | .filter(entry -> !(Lists.newArrayList(0, 1, 2, 3).contains(entry.getKey()) |
| | | || entry.getKey() == remarkIndex)) |
| | | .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| | | List<String> dynamicFields = new ArrayList<>(dynamicFieldsMap.values()); |
| | | List<String> collect = fieldList.stream().map(TbField::getFieldName).collect(Collectors.toList()); |
| | | List<String> collect = fieldList.stream().map(TbField::getFieldName) |
| | | .collect(Collectors.toList()); |
| | | boolean flag = new ArrayList<>(dynamicFields).containsAll(collect); |
| | | if (dynamicFields.size() != collect.size() || !flag) { |
| | | throw new ServiceException("导入失败,请下载最新的导入模板"); |
| | |
| | | eq(TbBasicData::getQuarter, String.format("%s年%s", now.getYear(), dataMap.get(1))) |
| | | .eq(TbBasicData::getDeptAreaCode, areaCode).oneOpt(); |
| | | tbBasicData = tbBasicDataOpt.orElseGet(TbBasicData::new); |
| | | tbBasicData.setQuarter(String.format("%s年%s", now.getYear(), dataMap.get(1))); |
| | | tbBasicData.setQuarter(DateUtils.getNowQuarter()); |
| | | tbBasicData.setTransferPaymentScale(dataMap.get(2)); |
| | | tbBasicData.setCurrentGdp(dataMap.get(3)); |
| | | tbBasicData.setDeptAreaCode(areaCode); |
| | | tbBasicData.setRemark(dataMap.get(remarkIndex)); |
| | | tbBasicData.setStatus(ReportingStatusEnum.MISSING_DATA); |
| | | tbBasicDataService.saveOrUpdate(tbBasicData); |
| | | tbBasicDataFieldService.remove(Wrappers.<TbBasicDataField>lambdaQuery().eq(TbBasicDataField::getBasicDataId, tbBasicData.getId())); |
| | | tbBasicDataFieldService.remove(Wrappers.<TbBasicDataField>lambdaQuery() |
| | | .eq(TbBasicDataField::getBasicDataId, tbBasicData.getId())); |
| | | List<TbBasicDataField> fields = new ArrayList<>(); |
| | | //添加固定字段 转移支付规模、当期GDP |
| | | TbBasicDataField transferPaymentScale = new TbBasicDataField(); |
| | |
| | | fields.add(currentGdp); |
| | | //遍历动态字段map |
| | | for (Map.Entry<Integer, String> integerStringEntry : dynamicFieldsMap.entrySet()) { |
| | | Optional<TbField> tbField = tbFieldService.lambdaQuery().eq(TbField::getFieldName, integerStringEntry.getValue()) |
| | | Optional<TbField> tbField = tbFieldService.lambdaQuery() |
| | | .eq(TbField::getFieldName, integerStringEntry.getValue()) |
| | | .eq(TbField::getStatus, ShowStatusEnum.SHOW).oneOpt(); |
| | | if (tbField.isPresent()) { |
| | | TbField field = tbField.get(); |
| | |
| | | log.info(String.format("%s导入基础数据成功!", dataMap.get(0))); |
| | | return tbBasicData; |
| | | } |
| | | private static String validateFields(Map.Entry<Integer, String> integerStringEntry, Map<Integer, String> dataMap, TbField field) { |
| | | String value = dataMap.get(integerStringEntry.getKey()); |
| | | if (FieldTypeEnum.NUMBER.equals(field.getFieldType())) { |
| | | Integer numMin = field.getNumMin(); |
| | | Integer numMax = field.getNumMax(); |
| | | if (Objects.nonNull(numMin) && Objects.nonNull(numMax)) { |
| | | if (numMin > Integer.parseInt(value) || numMax < Integer.parseInt(value)) { |
| | | throw new ServiceException(String.format("字段%s的内容不在%d~%d范围内", field.getFieldName(),numMin,numMax)); |
| | | } |
| | | } |
| | | } |
| | | if (FieldInputTypeEnum.MANUAL_INPUT.equals(field.getTextInputType()) && FieldTypeEnum.TEXT.equals(field.getFieldType())) { |
| | | Integer textMinNum = field.getTextMinNum(); |
| | | Integer textMaxNum = field.getTextMaxNum(); |
| | | if (Objects.nonNull(textMinNum) && Objects.nonNull(textMaxNum)) { |
| | | if (textMinNum > value.length() || textMaxNum < value.length()) { |
| | | throw new ServiceException(String.format("字段%s的内容长度超出%d~%d的范围", field.getFieldName(),textMinNum,textMaxNum)); |
| | | } |
| | | } |
| | | } |
| | | if (FieldTypeEnum.PERCENTAGE.equals(field.getFieldType())) { |
| | | if (0 > Double.parseDouble(value) || 100 < Double.parseDouble(value)) { |
| | | throw new ServiceException(String.format("字段%s的内容不在0~100范围内", field.getFieldName())); |
| | | } |
| | | } |
| | | return value; |
| | | } |
| | | |
| | | private void calculateScore(TbBasicData tbBasicData) { |
| | | List<TbScore> scoreList = new ArrayList<>(); |
| | | //计算得分 |
| | | List<TbBasicDataConfig> list = tbBasicDataConfigService.lambdaQuery() |
| | | .eq(TbBasicDataConfig::getStatus, ShowStatusEnum.SHOW).list(); |
| | |
| | | List<TbBasicDataConfig> textAndPercentages = list.stream() |
| | | .filter(item -> !CalculateTypeEnum.NUMBER.equals(item.getCalculateType())) |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isNotEmpty(numCalculates)) { |
| | | numCalculates.forEach(item -> { |
| | | tbScoreService.remove( |
| | | new LambdaQueryWrapper<TbScore>().eq(TbScore::getBasicDataId, tbBasicData.getId())); |
| | | if (CollUtils.isNotEmpty(numCalculates)) { |
| | | for (TbBasicDataConfig item : numCalculates) { |
| | | Map<String, Object> valueMap = new HashMap<>(); |
| | | String numberCalculateFormula = item.getNumberCalculateFormula(); |
| | | Map<String, Integer> fieldsAndValue = CalculateUtil.getFieldsAndValue(numberCalculateFormula); |
| | | Map<String, Integer> fieldsAndValue = CalculateUtil.getFieldsAndValue( |
| | | numberCalculateFormula); |
| | | for (Map.Entry<String, Integer> stringIntegerEntry : fieldsAndValue.entrySet()) { |
| | | Optional<TbBasicDataField> tbBasicDataField = tbBasicDataFieldService.lambdaQuery() |
| | | .eq(TbBasicDataField::getBasicDataId, tbBasicData.getId()) |
| | | .eq(TbBasicDataField::getFieldId, stringIntegerEntry.getValue()) |
| | | .oneOpt(); |
| | | tbBasicDataField.ifPresent(basicDataField -> valueMap.put(stringIntegerEntry.getKey(), basicDataField.getFieldValue())); |
| | | tbBasicDataField.ifPresent( |
| | | basicDataField -> valueMap.put(stringIntegerEntry.getKey(), |
| | | basicDataField.getFieldValue())); |
| | | } |
| | | double score = CalculateUtil.calculate(numberCalculateFormula, valueMap); |
| | | TbScore tbScore = new TbScore(); |
| | | tbScore.setBasicDataId(tbBasicData.getId()); |
| | | tbScore.setScore(score); |
| | | tbScore.setBasicDataConfigId(item.getId()); |
| | | tbScoreService.save(tbScore); |
| | | }); |
| | | scoreList.add(tbScore); |
| | | } |
| | | if (CollectionUtils.isNotEmpty(textAndPercentages)) { |
| | | } |
| | | if (CollUtils.isNotEmpty(textAndPercentages)) { |
| | | for (TbBasicDataConfig textAndPercentage : textAndPercentages) { |
| | | TbScore tbScore = new TbScore(); |
| | | List<TbBasicDataConfigDetail> details = tbBasicDataConfigDetailService.lambdaQuery() |
| | | .eq(TbBasicDataConfigDetail::getBasicDataConfigId, textAndPercentage.getId()) |
| | | .eq(TbBasicDataConfigDetail::getBasicDataConfigId, |
| | | textAndPercentage.getId()) |
| | | .list(); |
| | | Map<String, String> scoreMap = details.stream().collect(Collectors.toMap(TbBasicDataConfigDetail::getKey, TbBasicDataConfigDetail::getValue)); |
| | | Map<String, String> scoreMap = details.stream().collect( |
| | | Collectors.toMap(TbBasicDataConfigDetail::getKey, |
| | | TbBasicDataConfigDetail::getValue)); |
| | | if (CollectionUtils.isNotEmpty(details)) { |
| | | Optional<TbBasicDataField> tbBasicDataFieldOptional = tbBasicDataFieldService.lambdaQuery() |
| | | .eq(TbBasicDataField::getBasicDataId, tbBasicData.getId()) |
| | |
| | | tbScore.setBasicDataId(tbBasicData.getId()); |
| | | tbScore.setScore(Double.parseDouble(score)); |
| | | tbScore.setBasicDataConfigId(textAndPercentage.getId()); |
| | | tbScoreService.save(tbScore); |
| | | scoreList.add(tbScore); |
| | | } |
| | | if (CalculateTypeEnum.PERCENTAGE.equals(textAndPercentage.getCalculateType())) { |
| | | if (CalculateTypeEnum.PERCENTAGE.equals( |
| | | textAndPercentage.getCalculateType())) { |
| | | for (Map.Entry<String, String> stringStringEntry : scoreMap.entrySet()) { |
| | | String[] split = stringStringEntry.getKey().split("-"); |
| | | double v = Double.parseDouble(tbBasicDataField.getFieldValue()); |
| | | double min = Double.parseDouble(split[0]); |
| | | double max = Double.parseDouble(split[1]); |
| | | if (v >= min && v <= max) { |
| | | tbScore.setScore(Double.parseDouble(stringStringEntry.getValue())); |
| | | tbScore.setScore( |
| | | Double.parseDouble(stringStringEntry.getValue())); |
| | | } |
| | | } |
| | | tbScore.setBasicDataId(tbBasicData.getId()); |
| | | tbScore.setBasicDataConfigId(textAndPercentage.getId()); |
| | | tbScoreService.save(tbScore); |
| | | scoreList.add(tbScore); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | tbScoreService.saveBatch(scoreList); |
| | | } |
| | | } |