ludc
2023-11-10 d2d00b57e1a8ab328e2e94049cd9149ababe7e0c
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java
@@ -66,6 +66,7 @@
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -81,7 +82,7 @@
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.*;
import java.util.stream.Collectors;
import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.DATA_OID_NOT_EXIST;
@@ -97,12 +98,17 @@
@Service
public class MdmEngineServiceImpl implements MdmEngineService {
   /**
    * 单次sql的最多导入数量
    */
   @Value("${import.maxNum}")
   private Integer MAX_IMPORT_NUM;
    /**
     * 模板的服务
     */
    @Resource
    private CodeClstemplateServiceImpl templateService;
    private ICodeClstemplateService templateService;
    /**
     * 生成编码的服务
@@ -135,6 +141,11 @@
     */
    @Resource
    IEnumClient enumClient;
   /**
    * 版本规则服务
    */
   @Resource
   private IRevisionRuleClient revisionRuleClient;
    /**
     * 公式的服务
@@ -172,17 +183,12 @@
     */
    @Autowired
    private IBtmTypeClient btmTypeClient;
    /**
     * 版本规则的服务
     */
    @Resource
    private IRevisionRuleClient revisionRuleClient;
//
    /**
     * 通用查询
     */
    @Resource
    CommonsMapper commonsMapper;
    private CommonsMapper commonsMapper;
    /**
     * 编码规则的服务
@@ -433,9 +439,9 @@
        checkEnumOnOrder(templateVO, orderDTO);
        //8.处理时间格式,在数据库里面不论是字符串还是日期格式,都使用相同的格式存储
        switchDateAttrOnOrder(templateVO, orderDTO);
        //9.生成编码的信息
//      ClientBusinessObject cbo = boService.createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
        BaseModel cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmTypeId());
        //9.生成编码的信息 ,初始化业务类型:缓存先取消,因为版本规则会出现变动的情况所以无法使用缓存
        // BaseModel cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmTypeId());
      BaseModel cbo = createBaseModel(classifyFullInfo.getTopClassifyVO().getBtmTypeId().trim().toLowerCase());
      //默认的属性都不用从前端拷贝
      //设置编码需要的默认属性的内容
        copyValueToCBO(classifyFullInfo, cbo, orderDTO, templateVO, false);
@@ -1563,7 +1569,6 @@
            conditionMap.put(queryKey, QueryOptionConstant.OR + queryValue);
        }
    }
    /**
     * 查询编码数据的列表
@@ -2964,7 +2969,7 @@
        wrapperData(dataList, templateVO, sqlBO.getSelectFieldList(), true);
        R<List<Map<String, String>>> result = R.data(Collections.singletonList(cbo));
        //我们要看是否不是升版的,升版的话,需要对比不相等的属性
        String copy = cbo.get("copyfromversion");
/*        String copy = cbo.get("copyfromversion");
//      if (StringUtils.isBlank(copy)) {
//         copy = cbo.getAttributeValue("copyfromversion");
//      }
@@ -2983,11 +2988,12 @@
                Map<String, String> difFieldMap = new HashMap<>();
                Map<String, String> finalOldData = oldData;
                cbo.forEach((key, value) -> {
                    String oldValue = finalOldData.getOrDefault(key, "");
                    if (value == null) {
               // 这儿oldmap中的全是大写,而cbo中的全是小写所以会拿不到只,这儿直接处理成小写拿不到就用大写拿
               String oldValue = String.valueOf(finalOldData.getOrDefault(key.toUpperCase(Locale.ROOT), finalOldData.getOrDefault(key, "")));
               if (value == null || value == "null") {
                        value = "";
                    }
                    if (oldValue == null) {
                    if (oldValue == null || oldValue == "null") {
                        oldValue = "";
                    }
                    if (!value.equalsIgnoreCase(oldValue)) {
@@ -2998,7 +3004,7 @@
                difFieldList.add(difFieldMap);
                result.setData(difFieldList);
            }
        }
        }*/
        return result;
    }
@@ -3356,7 +3362,6 @@
        return getTableDefineByTemplateVO(templateService.getObjectHasAttrByOid(templateOid));
    }
    /**
     * 使用模板显示对象转换为表格的信息(包含扩展的按钮)
     *
@@ -3453,6 +3458,7 @@
        }
        return uiInfoVO;
    }
    @Override
   public MdmUIInfoVO getFlowUIInfoByClassifyOid(String codeClassifyOid, String functionId,String templateId,String taskId,String modelKey){
//      MdmUIInfoVO uiInfoVO = getTableDefineByClassifyOid_v2(codeClassifyOid,templateId,taskId,modelKey);
@@ -3524,6 +3530,7 @@
     * @return 处理成功数据条数
     */
    @Override
   @Transactional(rollbackFor = Exception.class)
    public Integer insertBatchByType(String btmType, List<BaseModel> baseModels) {
        //使用传入的业务类型查询表
        R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(btmType));
@@ -3540,6 +3547,7 @@
      }).collect(Collectors.toSet());
      //将bean转为map,mybatis统一处理
        List<Map<String, String>> maps = new ArrayList<>();
        baseModels.stream().forEach(model -> {
            try {
                maps.add(VciBaseUtil.convertBean2Map(model,existFild));
@@ -3547,8 +3555,45 @@
                throw new VciBaseException("类型转换错误:" + e.toString());
            }
        });
        return commonsMapper.insertByBaseModel(listR.getData().get(0).getTableName(), maps.get(0), maps);
      try {
         bactchExecuteInsert(listR.getData().get(0).getTableName(),maps);
      }catch (Exception e){
         throw new ServiceException("分批执行insert语句报错:"+e.getMessage());
      }
        return maps.size();
    }
   /**
    * 分批执行insert语句
    * @param tableName
    * @param maps
    * @throws ServiceException
    */
   @Transactional(rollbackFor = Exception.class)
   void bactchExecuteInsert(String tableName, List<Map<String, String>> maps) throws ServiceException{
      ExecutorService executor = Executors.newFixedThreadPool(10); // 创建一个固定大小的线程池
      List<Map<String, String>> threadSafeMaps = new CopyOnWriteArrayList<>(maps);
      for (int i = 0; i < threadSafeMaps.size(); i += MAX_IMPORT_NUM) {
         final int startIndex = i;
         final int endIndex = Math.min(i + MAX_IMPORT_NUM, maps.size());
         executor.execute(() -> {
            List<Map<String, String>> subList = threadSafeMaps.subList(startIndex, endIndex);
            // 调用插入数据库的方法
            commonsMapper.insertByBaseModel(tableName, threadSafeMaps.get(0), subList);
         });
      }
      // 关闭线程池
      executor.shutdown();
      try {
         // 等待所有任务执行完成
         executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
      } catch (InterruptedException e) {
         // 处理异常
         throw new ServiceException("多线程方式执行批量插入时产生错误:"+e.getMessage());
      }
   }
   /**
    * 传入业务类型以及ID查询业务表数据是否重复
    *
@@ -4102,6 +4147,7 @@
        toBo.setNameOid(fromBo.getNameOid());
        toBo.setBtmname(fromBo.getBtmname());
        toBo.setLastR(String.valueOf(1));
      // 升版将FirstR修改为0
        toBo.setFirstR(String.valueOf(0));
        toBo.setFirstV(String.valueOf(1));
        toBo.setLastV(String.valueOf(1));
@@ -4112,13 +4158,28 @@
      toBo.setLastModifier(String.valueOf(AuthUtil.getUser().getAccount()));
        toBo.setLastModifyTime(new Date());
        toBo.setRevisionRule(fromBo.getRevisionRule());
        toBo.setVersionRule(fromBo.getVersionRule());R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(fromBo.getBtmname()));
        toBo.setVersionRule(fromBo.getVersionRule());
      //查询业务类型信息,可以获取到版本规则信息(revisionRuleId)
      R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(fromBo.getBtmname()));
        if (!listR.isSuccess() || listR.getData().size() == 0) {
            throw new VciBaseException("传入业务类型未查询到相应表单,请检查!");
        }
        Map<String, Object> nextRevision = commonsMapper.getNextRevision(listR.getData().get(0).getTableName(), fromBo.getNameOid());
        toBo.setRevisionSeq(Integer.parseInt(nextRevision.get("REVISIONSEQ").toString()));
        toBo.setRevisionValue(nextRevision.get("REVISIONVAL").toString());
        //Map<String, Object> nextRevision = commonsMapper.getNextRevision(listR.getData().get(0).getTableName(), fromBo.getNameOid());
      /*
         TODO:这是一个待后期完善的功能,目前实现方式是,先查询出当前数据的大版本规则,
            然后再查询业务表使用的规则的步长是多少然后计算出该升版为多少
      */
      Map<String, Object> nextRevision = commonsMapper.getCurrentRevision(listR.getData().get(0).getTableName(), fromBo.getNameOid());
      R<RevisionRuleVO> revisionRuleVOR = revisionRuleClient.selectById(listR.getData().get(0).getRevisionRuleId());
      String revisionval = nextRevision.get("REVISIONVAL").toString();
      // 未查询到版本规则,默认直接给大版本加一
      if(!revisionRuleVOR.isSuccess() || Func.isEmpty(revisionRuleVOR.getData())){
         revisionval = String.valueOf((Integer.parseInt(revisionval)+1));
      }else {
         revisionval = String.valueOf(Integer.parseInt(revisionval)+revisionRuleVOR.getData().getSerialStep());
      }
      toBo.setRevisionValue(revisionval);
      toBo.setRevisionSeq(Integer.parseInt(nextRevision.get("REVISIONSEQ").toString()));
        toBo.setVersionSeq(Integer.valueOf(nextRevision.get("VERSIONSEQ").toString()));
        toBo.setVersionValue(nextRevision.get("VERSIONVAL").toString());
        toBo.setLctid(fromBo.getLctid());