xiejun
2023-11-17 566c542d3f1eb0ad9e918bd1fa9586ab1ed7ec86
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmIOServiceImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.protobuf.ServiceException;
import com.vci.ubcs.code.applyjtcodeservice.feign.IMdmInterJtClient;
import com.vci.ubcs.code.bo.AttributeValue;
import com.vci.ubcs.code.bo.CodeClassifyFullInfoBO;
@@ -65,6 +66,7 @@
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -97,6 +99,10 @@
   @Value("${batchadd.redis.time:6000000}")
   public int BATCHADD_REDIS_TIME;
   @Value("${batchadd.import_data_limit:5001}")
   private Integer IMPORT_DATA_LIMIT;
   /**
    * 主题库分类的服务
    */
@@ -389,17 +395,17 @@
               && (isHistory || VciBaseUtil.getBoolean(s.getFormDisplayFlag()))
         ).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(templateAttrVOS)) {
               throw new VciBaseException("模板没有配置任何【表单显示】为【是】的属性");
            }
         if (CollectionUtils.isEmpty(templateAttrVOS)) {
            throw new VciBaseException("模板没有配置任何【表单显示】为【是】的属性");
         }
         List<CodeClassifyTemplateAttrVO> idAttrVOList = codeClassifyTemplateVO.getAttributes().stream().filter(s -> s.getId().equalsIgnoreCase(CODE_FIELD)).collect(Collectors.toList());
         LinkedList<WriteExcelData> excelDataList = new LinkedList<>();
         Workbook workbook = new HSSFWorkbook();
         if(isHistory){
            excelDataList.add(new WriteExcelData(0,0,"分类路径"));
            excelDataList.add(new WriteExcelData(0,1,"码段宽度"));
            excelDataList.add(new WriteExcelData(0,2,!CollectionUtils.isEmpty(idAttrVOList)?idAttrVOList.get(0).getName():"企业编码"));
            excelDataList.add(new WriteExcelData(0,0,"分类路径",""));
            excelDataList.add(new WriteExcelData(0,1,"码段宽度",""));
            excelDataList.add(new WriteExcelData(0,2,!CollectionUtils.isEmpty(idAttrVOList)?idAttrVOList.get(0).getName():"企业编码",idAttrVOList.get(0).getId()));
         }
         for (int i = 0; i < templateAttrVOS.size(); i++) {
            CodeClassifyTemplateAttrVO attrVO = templateAttrVOS.get(i);
@@ -407,7 +413,7 @@
            Object text = attrVO.getName();
            text = exportKeyAndRequired(workbook,attrVO,text);
            int colIndex = (isHistory?3:0) + i;
            WriteExcelData excelData = new WriteExcelData(0, colIndex, text);
            WriteExcelData excelData = new WriteExcelData(0, colIndex, text,attrVO.getId());
            if(StringUtils.isNotBlank(attrVO.getCodeDateFormat())
               || VciFieldTypeEnum.VTDateTime.name().equalsIgnoreCase(attrVO.getAttributeDataType())
               || VciFieldTypeEnum.VTDate.name().equalsIgnoreCase(attrVO.getAttributeDataType())
@@ -643,7 +649,6 @@
         });
         //都转换完了。需要批量检查
         //如果出错了,我们依然执行有效的数据,无效的数据写回到excel中
         //2.判断必输项。。需要全部的属性,如果是必输,但是表单里面不显示的,只能是分类注入或者组合规则
@@ -788,11 +793,15 @@
      String uuid=VciBaseUtil.getPk();
      boolean isCreateUUid=false;
      boolean isExport=false;
      long start = System.currentTimeMillis();
      //long start = System.currentTimeMillis();
      for(int i=0;i<sheetDataSetList.size()-1;i++) {
         if (CollectionUtils.isEmpty(sheetDataSetList) || CollectionUtils.isEmpty(sheetDataSetList.get(i).getRowData())
            || sheetDataSetList.get(i).getRowData().size() < 1) {
            continue;
         }
         // 单次导入数量限制
         if(sheetDataSetList.get(i).getRowData().size() > IMPORT_DATA_LIMIT){
            throw new ServiceException("为了保证系统的稳定性,请一次不要导入超过"+IMPORT_DATA_LIMIT+"条的数据");
         }
         //历史导入的时候不处理编码
         //----逻辑内容----
@@ -816,7 +825,7 @@
         //找第一行,为了找标题
         CodeClassifyTemplateVO templateVO = new CodeClassifyTemplateVO();
         /**  if (!templateService.checkChildHasSameTemplate(classifyFullInfo.getCurrentClassifyVO().getOid())) {
          throw new VciBaseException("当前的分类以及下级分类的模板不相同");
             throw new VciBaseException("当前的分类以及下级分类的模板不相同");
          }***/
         //都转换完了。需要批量检查
         //找所有的分类路径,需要校验路径是否正确,是否都在当前的分类的下级
@@ -933,7 +942,8 @@
         if (CollectionUtils.isEmpty(ruleOidMap.values())) {
            throw new VciBaseException("导入的数据所选择的分类都没有设置编码规则");
         }
         Map<String, CodeRuleVO> ruleVOMap = ruleService.listCodeRuleByIds(ruleOidMap.values(), true).stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
         // TODO: 改用oid查询规则的,别用id
         Map<String, CodeRuleVO> ruleVOMap = ruleService.listCodeRuleByOids(ruleOidMap.values()).stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
         //校验编码规则和码段是否正确
         Map<String, List<String>> ruleRowIndexMap = new ConcurrentHashMap<>();
         checkSecLengthInHistory(cboList, classifyVOMap, ruleVOMap, ruleOidMap, errorMap, ruleRowIndexMap);
@@ -1028,7 +1038,7 @@
               //VciBaseUtil.setCurrentUserSessionInfo(sessionInfo);
               List<String> rowIndexList = ruleRowIndexMap.get(ruleOid);
               List<ClientBusinessObject> thisCbos = finalNeedSaveCboList.stream().filter(cbo -> rowIndexList.contains(cbo.getAttributeValue(IMPORT_ROW_INDEX)) && !errorMap.containsKey(cbo.getAttributeValue(IMPORT_ROW_INDEX))).collect(Collectors.toList());
               List<BaseModel> dataCBOList=new ArrayList<>();
               List<BaseModel> dataCBOList=new CopyOnWriteArrayList<>();
               thisCbos.stream().forEach(clientBusinessObject -> {
                  BaseModel baseModel=new BaseModel();
                  BeanUtil.convert(clientBusinessObject,baseModel);
@@ -1045,7 +1055,7 @@
                     log.error("批量产生编码的时候出错了", e);
                     thisCbos.stream().forEach(cbo -> {
                        String rowIndex = cbo.getAttributeValue(IMPORT_ROW_INDEX);
                        errorMap.put(rowIndex, errorMap.getOrDefault(rowIndex, "") + ";系统错误,存储数据的时候出错了");
                        errorMap.put(rowIndex, errorMap.getOrDefault(rowIndex, "") + ";系统错误,存储数据的时候出错了:"+e.getMessage());
                     });
                  }
@@ -1058,8 +1068,8 @@
            engineService.batchSaveSelectChar(templateVO, dataCBOIdList);
         }
      }
      long end = System.currentTimeMillis();
      log.debug("=============for执行时间================="+String.valueOf(start-end));
      //long end = System.currentTimeMillis();
      //log.info("=============for执行时间================="+String.valueOf((end-start)/1000));
      String excelFileName="";
      if(isExport&&!CollectionUtils.isEmpty(shetNameMap)) {
         excelFileName = LocalFileUtil.getDefaultTempFolder() + File.separator + "错误信息.xls";
@@ -1139,7 +1149,6 @@
    * @param currentTemplateVO
    * @param templateColumnVOMap
    */
   private void createTemplate(CodeClassifyTemplateVO currentTemplateVO,Map<String,List<ColumnVO>>templateColumnVOMap){
      List<CodeClassifyTemplateAttrVO> templateAttrVOS = currentTemplateVO.getAttributes().stream().filter(s ->
@@ -1750,7 +1759,8 @@
            Map<String/**分类主键**/, String/**规则主键**/> ruleOidMap = new ConcurrentHashMap<String, String>();
            List<String> unExistRuleClassifyOidList = new CopyOnWriteArrayList<>();
            checkRuleOidInHistory(classifyVOMap, ruleOidMap, unExistRuleClassifyOidList);
            ruleVOMap = ruleService.listCodeRuleByIds(ruleOidMap.values(), true).stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
            // TODO   改用oid查询的,这儿不该用id
            ruleVOMap = ruleService.listCodeRuleByOids(ruleOidMap.values()).stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
            checkSecLengthInHistory(cboList, classifyVOMap, ruleVOMap, ruleOidMap, errorMap, ruleRowIndexMap);
         }
@@ -1939,7 +1949,8 @@
         && com.alibaba.cloud.commons.lang.StringUtils.isBlank(s.getComponentRule()) && com.alibaba.cloud.commons.lang.StringUtils.isBlank(s.getClassifyInvokeAttr())//组合规则和分类注入确实没给用户导出去
      ).collect(Collectors.toList());
      if(!CollectionUtils.isEmpty(unExistAttrVOs)){
         throw new VciBaseException("【" + unExistAttrVOs.stream().map(CodeClassifyTemplateAttrVO::getName) + "】这些属性在excel中没有找到");
         String message=unExistAttrVOs.stream().map(CodeClassifyTemplateAttrVO::getName).collect(Collectors.joining(SERIAL_UNIT_SPACE));
         throw new VciBaseException("【" + message + "】这些属性在excel中没有找到");
      }
      List<ClientBusinessObject> cboList = new ArrayList<>();
      String fullPath = getFullPath(classifyFullInfo);
@@ -1955,16 +1966,29 @@
      //都转换完了。需要批量检查
      //如果出错了,我们依然执行有效的数据,无效的数据写回到excel中
      Map<String,String> errorKeyMap=new HashMap<>();
      //1.分类注入
      batchSwitchClassifyAttrOnOrder(attrVOS,cboList,classifyFullInfo,false);
      //boolean
      reSwitchBooleanAttrOnOrder(attrVOS,cboList);
      // cboList.stream().forEach(cbo->{
      //2.校验规则
      batchCheckVerifyOnOrder(attrVOS, cboList,errorMap);
      //3.校验枚举是否正确
      batchSwitchEnumAttrOnOrder(attrVOS,cboList,errorMap);
      //4.时间格式的验证
      //4.时间的,必须统一为yyyy-MM-dd HH:mm:ss
      batchSwitchDateAttrOnOrder(attrVOS,cboList,errorMap);
      //5.处理参照的情况
      batchSwitchReferAttrOnOrder(attrVOS,cboList,errorMap);
      //6设置默认值
      batchSwitchAttrDefault(attrVOS, cboList);
      //2.判断必输项。。需要全部的属性,如果是必输,但是表单里面不显示的,只能是分类注入或者组合规则
      batchCheckRequiredAttrOnOrder(templateVO,cboList,errorMap);
      /**
       * 关键熟悉错误提示
       */
      Map<String,String> errorKeyMap=new HashMap<>();
      //最后弄组合规则
      batchSwitchComponentAttrOnOrder(attrVOS,cboList);
      //3.判断关键属性
      CodeImportResultVO keyResultVO = batchCheckKeyAttrOnOrder(classifyFullInfo, templateVO, cboList);
      Set<String> selfRepeatRowIndexList = keyResultVO.getSelfRepeatRowIndexList();
@@ -2001,20 +2025,6 @@
            errorKeyMap.put(rowIndex,errorMap.getOrDefault(rowIndex,"") + ";关键属性与系统中的重复" );
         });
      }
      //分类注入
      batchSwitchClassifyAttrOnOrder(attrVOS,cboList,classifyFullInfo,false);
      //boolean
      reSwitchBooleanAttrOnOrder(attrVOS,cboList);
      // cboList.stream().forEach(cbo->{
      //4.校验规则
      batchCheckVerifyOnOrder(attrVOS, cboList,errorMap);
      //5.校验枚举是否正确
      batchSwitchEnumAttrOnOrder(attrVOS,cboList,errorMap);
      //6.时间格式的验证
      //6.时间的,必须统一为yyyy-MM-dd HH:mm:ss
      batchSwitchDateAttrOnOrder(attrVOS,cboList,errorMap);
      //7.处理参照的情况
      batchSwitchReferAttrOnOrder(attrVOS,cboList,errorMap);
      //校验属性是否正确错误信息
      if(errorMap.size()>0){
         String[] newMsg = {""};
@@ -2115,10 +2125,7 @@
      }
      //  });
      //设置默认值
      batchSwitchAttrDefault(attrVOS, cboList);
      //最后弄组合规则
      batchSwitchComponentAttrOnOrder(attrVOS,cboList);
      //要把以上的错误的都抛出后,再继续处理时间和组合规则
      List<ClientBusinessObject> needSaveCboList = cboList.stream().filter(cbo -> {
         String rowIndex =cbo.getAttributeValue(IMPORT_ROW_INDEX);
@@ -2134,7 +2141,7 @@
         needSaveCboList.stream().forEach(clientBusinessObject -> {
            BaseModel baseModel=new BaseModel();
            BeanUtil.convert(clientBusinessObject,baseModel);
         //(VciBaseUtil.objectToMapString(clientBusinessObject));
            //(VciBaseUtil.objectToMapString(clientBusinessObject));
            dataCBOList.add(baseModel);
            allNeedSaveCboList.add(baseModel.getOid());
         });
@@ -2368,6 +2375,7 @@
         String dataStatus=cbo.getLcStatus();
         RowDatas rowData=codeDataMap.get(code);
         String status=rowData.getStatus();
         String lastModifier= rowData.getEditor();
         String operation=rowData.getOperation();
         if (cbo.getTs().compareTo(orderDTO.getTs())==0?false:true) {
            // throw new VciBaseException("数据不是最新的,可能他人已经修改,请刷新后再试");
@@ -2377,20 +2385,20 @@
                throw new VciBaseException("数据不是{0}的状态,不允许修改", new String[]{CodeDefaultLC.EDITING.getText()});
            }*/
         if(operation.equals("update")) {
            //1. 判断必输项
            checkRequiredAttrOnOrder(templateVO, orderDTO, errorMap);
            //2.先注入,再组合,最后校验
            //1.先注入,再组合,最后校验
            switchClassifyLevelOnOrder(templateVO, classifyFullInfo, orderDTO, errorMap);
            //3.处理组合规则。组合规则不能使用编码的属性,因为编码的生成可能是需要属性的
            //2.处理组合规则。组合规则不能使用编码的属性,因为编码的生成可能是需要属性的
            switchComponentAttrOnOrder(templateVO, orderDTO);
            //4.校验规则
            //3.校验规则
            checkVerifyOnOrder(templateVO, orderDTO, errorMap);
            //5.判断关键属性
            checkKeyAttrOnOrder(classifyFullInfo, templateVO, orderDTO, errorMap);
            //6.校验枚举的内容是否正确
            //4.校验枚举的内容是否正确
            checkEnumOnOrder(templateVO, orderDTO, errorMap);
            //7.处理时间格式,在数据库里面不论是字符串还是日期格式,都使用相同的格式存储
            //5.处理时间格式,在数据库里面不论是字符串还是日期格式,都使用相同的格式存储
            switchDateAttrOnOrder(templateVO, orderDTO);
            //6. 判断必输项
            checkRequiredAttrOnOrder(templateVO, orderDTO, errorMap);
            //7.判断关键属性
            checkKeyAttrOnOrder(classifyFullInfo, templateVO, orderDTO, errorMap);
            //默认的内容不能变,所以只需要拷贝自定义的相关属性即可
            copyValueToCBO(classifyFullInfo, cbo, orderDTO, templateVO, true, errorMap);
            //企业码和集团码的不修改
@@ -2398,12 +2406,20 @@
            cbo.setName(orderDTO.getName());
            try {
               cbo.setAttributeValueWithNoCheck("description", orderDTO.getDescription());
               cbo.setAttributeValue("name", orderDTO.getName());
            //   cbo.setAttributeValue("name", orderDTO.getName());
               //  if(finalIsProcess){//在流程中不允许更改
               //     errorMap.put(code,errorMap.getOrDefault(code, errorMap.getOrDefault(code,"")+";数据"+code+"在流程中,不允许更改!"));
               //  }else{
               Date date=new Date();
               cbo.setLcStatus(status);
               cbo.setAttributeValue("lcstatus",status);
               cbo.setLastModifyTime(date);
               cbo.setLastModifier(lastModifier);
               cbo.setLastModifyTime(date);
               cbo.setAttributeValue("lastmodifier",lastModifier);
               cbo.setAttributeValue("lastmodifytime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date));
               cbo.setTs(date);
               cbo.setAttributeValue("ts",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date));
               //  }
            } catch (VciBaseException e) {
               e.printStackTrace();
@@ -2749,10 +2765,10 @@
//               if (lifeCycleVO != null) {
//                  cbo.setLcStatus(lifeCycleVO.getStartStatus());
//               } else {
                  cbo.setLcStatus(CodeDefaultLC.EDITING.getValue());
               cbo.setLcStatus(CodeDefaultLC.RELEASED.getValue());
//               }
            } else {
               cbo.setLcStatus(CodeDefaultLC.EDITING.getValue());
               cbo.setLcStatus(CodeDefaultLC.RELEASED.getValue());
            }
         }
@@ -2940,7 +2956,7 @@
                  for (int j = 0; j < ruleVO.getSecVOList().size(); j++) {
                     CodeBasicSecVO secVO = ruleVO.getSecVOList().get(j);
                     String length= secValues[j];
                     if(StringUtils.isNotBlank(secVO.getCodeSecLength())&&VciBaseUtil.getInt(length)>VciBaseUtil.getInt(secVO.getCodeSecLength())){
                     if(StringUtils.isNotBlank(secVO.getCodeSecLength())&&VciBaseUtil.getInt(length)>(VciBaseUtil.getInt(secVO.getCodeSecLength())+((secVO.getPrefixCode()+secVO.getSuffixCode()).length()))){
                        errorMap.put(rowIndex,errorMap.getOrDefault(rowIndex,"") + ";码段宽度与系统中的编码规则不同" );
                        fined = true;
                        break;
@@ -3118,7 +3134,7 @@
                  Integer userSecret = VciBaseUtil.getCurrentUserSecret();
                  cbo.setAttributeValue(SECRET_FIELD,String.valueOf((userSecret==null || userSecret ==0)? UserSecretEnum.NONE.getValue():userSecret));
               }
               cbo.setLcStatus(CodeDefaultLC.EDITING.getValue());
               cbo.setLcStatus(CodeDefaultLC.RELEASED.getValue());
            }else{
               //此时还没有转换路径
               //cbo.setAttributeValue(CODE_FULL_PATH_FILED, childOidPathMap.getOrDefault(rowData.getData().getOrDefault(CODE_CLASSIFY_OID_FIELD,""),fullPath));
@@ -3148,7 +3164,7 @@
                     String fullPath,boolean newCode){
      rowDataList.stream().forEach(rowData -> {
         ClientBusinessObject cbo=new ClientBusinessObject();
          DefaultAttrAssimtUtil.addDefaultAttrAssimt(cbo, classifyFullInfo.getTopClassifyVO().getBtmTypeId());
         DefaultAttrAssimtUtil.addDefaultAttrAssimt(cbo, classifyFullInfo.getTopClassifyVO().getBtmTypeId());
         rowData.getData().forEach((index,value)->{
            String field = fieldIndexMap.get(index);
            if (StringUtils.isBlank(field)) {
@@ -3175,7 +3191,7 @@
                  Integer userSecret = VciBaseUtil.getCurrentUserSecret();
                  cbo.setAttributeValue(SECRET_FIELD,String.valueOf((userSecret==null || userSecret ==0)? UserSecretEnum.NONE.getValue():userSecret));
               }
               cbo.setLcStatus(CodeDefaultLC.EDITING.getValue());
               cbo.setLcStatus(CodeDefaultLC.RELEASED.getValue());
            }else{
               //此时还没有转换路径
               //cbo.setAttributeValue(CODE_FULL_PATH_FILED, childOidPathMap.getOrDefault(rowData.getData().getOrDefault(CODE_CLASSIFY_OID_FIELD,""),fullPath));
@@ -3699,7 +3715,7 @@
    */
   private void batchCheckRequiredAttrOnOrder(CodeClassifyTemplateVO templateVO,List<ClientBusinessObject> cboList,Map<String,String> errorMap){
      Map<String, CodeClassifyTemplateAttrVO> requiredAttrMap = templateVO.getAttributes().stream().filter(s ->
         VciBaseUtil.getBoolean(s.getRequireFlag()) && StringUtils.isBlank(s.getComponentRule()) && StringUtils.isBlank(s.getClassifyInvokeLevel())//不能是组合的和分类注入的
         VciBaseUtil.getBoolean(s.getRequireFlag()) && StringUtils.isBlank(s.getComponentRule()) && (StringUtils.isBlank(s.getClassifyInvokeLevel())||s.getClassifyInvokeLevel().equals("none"))//不能是组合的和分类注入的
      ).collect(Collectors.toMap(s -> s.getId().toLowerCase(Locale.ROOT), t -> t));
      //与MdmEngineServiceImpl里面的checkRequiredAttrOnOrder 逻辑应该相似
      if(!CollectionUtils.isEmpty(requiredAttrMap)) {
@@ -3741,7 +3757,8 @@
                     }
                  }
                  CodeClassifyFullInfoBO newClassifyFullInfo= classifyFullInfoMap.get(cbo.getAttributeValue(CODE_CLASSIFY_OID_FIELD));
                  List<CodeClassifyVO> classifyVOS = newClassifyFullInfo.getParentClassifyVOs().stream().sorted(((o1, o2) -> o2.getDataLevel().compareTo(o1.getDataLevel()))).collect(Collectors.toList());
                  List<CodeClassifyVO> classifyVOS = newClassifyFullInfo.getParentClassifyVOs().stream().sorted(((o1, o2) -> -o2.getDataLevel().compareTo(o1.getDataLevel()))).collect(Collectors.toList());
                  int level = VciBaseUtil.getInt(attrVO.getClassifyInvokeLevel());
                  if (classifyVOS.size()>=level && level > 0 ) {
                     classifyVO = classifyVOS.get(level-1);
@@ -3800,7 +3817,7 @@
      //2.判断关键属性在系统里是否重复
      //因为数据量很大,所以得想办法并行
      //SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
      Map<String,List<BaseModel>> indexTODataMap=new HashMap<>();
      Map<String,List<BaseModel>> indexTODataMap=new ConcurrentHashMap<>();
      List<ClientBusinessObject> repeatDataMap = cboList.parallelStream().filter(cbo -> {
         //每行都得查询.如果其中出现了错误,我们就直接抛出异常,其余的显示
         //VciBaseUtil.setCurrentUserSessionInfo(sessionInfo);
@@ -3952,7 +3969,7 @@
    * @return 重复的行号
    */
   private void getSelfRepeatRowIndex(Map<String/**属性的编号**/, CodeClassifyTemplateAttrVO> ketAttrMap,
                                   List<ClientBusinessObject> dataList,CodeKeyAttrRepeatVO keyRuleVO,CodeImportResultVO resultVO){
                              List<ClientBusinessObject> dataList,CodeKeyAttrRepeatVO keyRuleVO,CodeImportResultVO resultVO){
      Set<String> selfRepeatRowIndexList = new CopyOnWriteArraySet<>();
      Map<String,List<String>> keyAttrOkOidTORepeatOidMap=new HashMap<>();
      boolean trimAll =keyRuleVO ==null?false: VciBaseUtil.getBoolean(keyRuleVO.getIgnoreallspaceflag());