| | |
| | | import com.alibaba.excel.event.AnalysisEventListener; |
| | | import com.alibaba.excel.util.ListUtils; |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.google.common.collect.Lists; |
| | | import com.ruoyi.common.NumberDisplaceChineseUtil; |
| | | import com.ruoyi.common.enums.ReportingStatusEnum; |
| | | import com.ruoyi.common.enums.ShowStatusEnum; |
| | | import com.ruoyi.common.enums.*; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.system.domain.TbBasicData; |
| | | import com.ruoyi.system.domain.TbBasicDataField; |
| | | import com.ruoyi.system.domain.TbDept; |
| | | import com.ruoyi.system.domain.TbField; |
| | | import com.ruoyi.system.service.TbBasicDataFieldService; |
| | | import com.ruoyi.system.service.TbBasicDataService; |
| | | import com.ruoyi.system.service.TbFieldService; |
| | | import com.ruoyi.common.utils.CalculateUtil; |
| | | import com.ruoyi.system.domain.*; |
| | | import com.ruoyi.system.service.*; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Slf4j |
| | |
| | | /** |
| | | * 每隔5条存储数据库,实际使用中可以100条,然后清理list ,方便内存回收 |
| | | */ |
| | | private static final int BATCH_COUNT = 10; |
| | | private static final int BATCH_COUNT = 100; |
| | | private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
| | | public TbBasicDataService tbBasicDataService; |
| | | public List<TbField> fieldList; |
| | | public TbFieldService tbFieldService; |
| | | public TbDept dept; |
| | | public TbBasicDataFieldService tbBasicDataFieldService; |
| | | public TbBasicDataConfigService tbBasicDataConfigService; |
| | | public TbBasicDataConfigDetailService tbBasicDataConfigDetailService; |
| | | public TbScoreService tbScoreService; |
| | | public BasicDataListener() { |
| | | } |
| | | |
| | | public BasicDataListener(TbBasicDataService tbBasicDataService, List<TbField> fieldList, TbFieldService tbFieldService, TbDept dept, TbBasicDataFieldService tbBasicDataFieldService) { |
| | | public BasicDataListener(TbBasicDataService tbBasicDataService, List<TbField> fieldList,TbFieldService tbFieldService, |
| | | TbDept dept,TbBasicDataFieldService tbBasicDataFieldService,TbBasicDataConfigService tbBasicDataConfigService, |
| | | TbBasicDataConfigDetailService tbBasicDataConfigDetailService,TbScoreService tbScoreService ) { |
| | | this.tbBasicDataService = tbBasicDataService; |
| | | this.fieldList = fieldList; |
| | | this.tbFieldService = tbFieldService; |
| | | this.dept = dept; |
| | | this.tbBasicDataFieldService = tbBasicDataFieldService; |
| | | this.tbBasicDataConfigService = tbBasicDataConfigService; |
| | | this.tbBasicDataConfigDetailService = tbBasicDataConfigDetailService; |
| | | this.tbScoreService = tbScoreService; |
| | | } |
| | | |
| | | @Override |
| | |
| | | try { |
| | | saveData(); |
| | | } catch (Exception e) { |
| | | if (e instanceof ServiceException) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | throw new RuntimeException(e); |
| | | } |
| | | cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); |
| | |
| | | try { |
| | | saveData(); |
| | | } catch (Exception e) { |
| | | if (e instanceof ServiceException) { |
| | | throw new ServiceException(e.getMessage()); |
| | | } |
| | | throw new RuntimeException(e); |
| | | } |
| | | log.info("所有数据解析完成!"); |
| | |
| | | /** |
| | | * 加上存储数据库 |
| | | */ |
| | | private void saveData() throws Exception { |
| | | private void saveData() { |
| | | Map<Integer, String> headMap = cachedDataList.get(2); |
| | | Map<Integer, String> quarterMap = cachedDataList.get(4); |
| | | Map<Integer, String> dataMap = cachedDataList.get(5); |
| | | log.info("{}条数据,开始存储数据库!", cachedDataList.size()); |
| | | log.info("表头:{}", JSON.toJSONString(headMap)); |
| | | log.info("填写的数据:{}",JSON.toJSONString(dataMap)); |
| | | int remarkIndex = headMap.size()-1; |
| | | Map<Integer, String> dynamicFieldsMap = headMap.entrySet().stream().filter(entry -> { |
| | | return !(Lists.newArrayList(0, 1, 2, 3).contains(entry.getKey()) || entry.getKey() == remarkIndex); |
| | | }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| | | 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)) |
| | | .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()); |
| | | boolean flag = new ArrayList<>(dynamicFields).containsAll(collect); |
| | | if (dynamicFields.size() != collect.size() || flag){ |
| | | if (dynamicFields.size() != collect.size() || !flag) { |
| | | throw new ServiceException("导入失败,请下载最新的导入模板"); |
| | | } |
| | | Date date = new Date(); |
| | | //当前所在季度 |
| | | int quarterOfYear = DateUtils.getQuarterOfYear(date); |
| | | String quarterOfYearStr = NumberDisplaceChineseUtil.numberToChinese(quarterOfYear); |
| | | LocalDate now = LocalDate.now(); |
| | | TbBasicData tbBasicData = new TbBasicData(); |
| | | tbBasicData.setQuarter(String.format("%s年%s",now.getYear(),quarterMap.get(1))); |
| | | TbBasicData tbBasicData; |
| | | Optional<TbBasicData> tbBasicDataOpt = tbBasicDataService.lambdaQuery(). |
| | | eq(TbBasicData::getQuarter, String.format("%s年%s", now.getYear(), quarterMap.get(1))) |
| | | .eq(TbBasicData::getDeptAreaCode, dept.getAreaCode()).oneOpt(); |
| | | tbBasicData = tbBasicDataOpt.orElseGet(TbBasicData::new); |
| | | tbBasicData.setQuarter(String.format("%s年%s", now.getYear(), quarterMap.get(1))); |
| | | tbBasicData.setTransferPaymentScale(dataMap.get(2)); |
| | | tbBasicData.setCurrentGdp(dataMap.get(3)); |
| | | tbBasicData.setDeptAreaCode(dept.getAreaCode()); |
| | | tbBasicData.setRemark(dataMap.get(remarkIndex)); |
| | | tbBasicData.setStatus(ReportingStatusEnum.MISSING_DATA); |
| | | tbBasicDataService.saveOrUpdate(tbBasicData); |
| | | tbBasicDataFieldService.remove(Wrappers.<TbBasicDataField>lambdaQuery().eq(TbBasicDataField::getBasicDataId, tbBasicData)); |
| | | tbBasicDataFieldService.remove(Wrappers.<TbBasicDataField>lambdaQuery().eq(TbBasicDataField::getBasicDataId, tbBasicData.getId())); |
| | | List<TbBasicDataField> fields = new ArrayList<>(); |
| | | //遍历动态字段map |
| | | for (Map.Entry<Integer, String> integerStringEntry : dynamicFieldsMap.entrySet()) { |
| | | Optional<TbField> tbField = tbFieldService.lambdaQuery().eq(TbField::getFieldName, integerStringEntry.getValue()) |
| | | .eq(TbField::getStatus, ShowStatusEnum.SHOW).oneOpt(); |
| | | if (tbField.isPresent()) { |
| | | TbField field = tbField.get(); |
| | | String value = validateFields(integerStringEntry, dataMap, field); |
| | | TbBasicDataField tbBasicDataField = new TbBasicDataField(); |
| | | tbBasicDataField.setBasicDataId(tbBasicData.getId()); |
| | | tbBasicDataField.setFieldId(tbField.get().getId()); |
| | | tbBasicDataField.setFieldValue(dataMap.get(integerStringEntry.getKey())); |
| | | tbBasicDataFieldService.save(tbBasicDataField); |
| | | tbBasicDataField.setFieldId(field.getId()); |
| | | tbBasicDataField.setFieldValue(value); |
| | | fields.add(tbBasicDataField); |
| | | } |
| | | } |
| | | log.info("存储数据库成功!"); |
| | | //异步执行批量保存 |
| | | CompletableFuture.runAsync(() -> tbBasicDataFieldService.saveBatch(fields)); |
| | | tbBasicData.setStatus(ReportingStatusEnum.FILLED); |
| | | tbBasicDataService.updateById(tbBasicData); |
| | | log.info(String.format("%s导入基础数据成功!", quarterMap.get(0))); |
| | | CompletableFuture.runAsync(() -> calculateScore(tbBasicData)); |
| | | } |
| | | |
| | | private void calculateScore(TbBasicData tbBasicData) { |
| | | //计算得分 |
| | | List<TbBasicDataConfig> list = tbBasicDataConfigService.lambdaQuery().eq(TbBasicDataConfig::getStatus, ShowStatusEnum.SHOW).list(); |
| | | if (CollectionUtils.isEmpty(list)) { |
| | | throw new ServiceException("计算得分失败,平台未配置得分计算规则"); |
| | | } |
| | | List<TbBasicDataConfig> numCalculates = list.stream() |
| | | .filter(item -> CalculateTypeEnum.NUMBER.equals(item.getCalculateType())) |
| | | .collect(Collectors.toList()); |
| | | List<TbBasicDataConfig> textAndPercentages = list.stream() |
| | | .filter(item -> !CalculateTypeEnum.NUMBER.equals(item.getCalculateType())) |
| | | .collect(Collectors.toList()); |
| | | if (CollectionUtils.isNotEmpty(numCalculates)) { |
| | | numCalculates.forEach(item -> { |
| | | Map<String, Object> valueMap = new HashMap<>(); |
| | | String numberCalculateFormula = item.getNumberCalculateFormula(); |
| | | 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(); |
| | | if (tbBasicDataField.isPresent()) { |
| | | valueMap.put(stringIntegerEntry.getKey(), tbBasicDataField.get().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); |
| | | }); |
| | | } |
| | | if (CollectionUtils.isNotEmpty(textAndPercentages)) { |
| | | for (TbBasicDataConfig textAndPercentage : textAndPercentages) { |
| | | TbScore tbScore = new TbScore(); |
| | | List<TbBasicDataConfigDetail> details = tbBasicDataConfigDetailService.lambdaQuery() |
| | | .eq(TbBasicDataConfigDetail::getBasicDataConfigId, textAndPercentage.getId()) |
| | | .list(); |
| | | 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()) |
| | | .eq(TbBasicDataField::getFieldId, textAndPercentage.getFieldIdStr()) |
| | | .oneOpt(); |
| | | if (tbBasicDataFieldOptional.isPresent()) { |
| | | TbBasicDataField tbBasicDataField = tbBasicDataFieldOptional.get(); |
| | | if (CalculateTypeEnum.TEXT.equals(textAndPercentage.getCalculateType())) { |
| | | String score = scoreMap.get(tbBasicDataField.getFieldValue()); |
| | | tbScore.setBasicDataId(tbBasicData.getId()); |
| | | tbScore.setScore(Double.parseDouble(score)); |
| | | tbScore.setBasicDataConfigId(textAndPercentage.getId()); |
| | | tbScoreService.save(tbScore); |
| | | } |
| | | 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.setBasicDataId(tbBasicData.getId()); |
| | | tbScore.setBasicDataConfigId(textAndPercentage.getId()); |
| | | tbScoreService.save(tbScore); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static String validateFields(Map.Entry<Integer, String> integerStringEntry, Map<Integer, String> dataMap, TbField field) { |
| | | String value = dataMap.get(integerStringEntry.getKey()); |
| | | if (FieldTypeEnum.NUMBER.getCode().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.getCode().equals(field.getTextInputType()) && FieldTypeEnum.TEXT.getCode().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.getCode().equals(field.getFieldType())) { |
| | | if (0 > Double.parseDouble(value) || 100 < Double.parseDouble(value)) { |
| | | throw new ServiceException(String.format("字段%s的内容不在0~100范围内", field.getFieldName())); |
| | | } |
| | | } |
| | | return value; |
| | | } |
| | | } |