ludc
2023-07-17 58e84408a3cdcd57a349fcd7faadf832361965ff
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/CodeRuleServiceImpl.java
@@ -17,13 +17,17 @@
package com.vci.ubcs.code.service.impl;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.code.constant.FrameWorkDefaultValueConstant;
import com.vci.ubcs.code.constant.MdmBtmTypeConstant;
import com.vci.ubcs.code.dto.CodeBasicSecDTO;
import com.vci.ubcs.code.dto.CodeRuleDTO;
import com.vci.ubcs.code.entity.*;
import com.vci.ubcs.code.enumpack.CodeSecTypeEnum;
import com.vci.ubcs.code.lifecycle.CodeRuleLC;
import com.vci.ubcs.code.mapper.CodeRuleMapper;
import com.vci.ubcs.code.mapper.CodeSerialValueMapper;
@@ -32,19 +36,29 @@
import com.vci.ubcs.code.service.ICodeBasicSecService;
import com.vci.ubcs.code.service.ICodeClassifyService;
import com.vci.ubcs.code.service.ICodeRuleService;
import com.vci.ubcs.code.vo.pagemodel.CodeBasicSecVO;
import com.vci.ubcs.code.vo.pagemodel.CodeClassifyVO;
import com.vci.ubcs.code.vo.pagemodel.CodeRuleVO;
import com.vci.ubcs.code.wrapper.CodeClassifyWrapper;
import com.vci.ubcs.code.wrapper.CodeRuleWrapper;
import com.vci.ubcs.omd.cache.EnumCache;
import com.vci.ubcs.omd.enums.EnumEnum;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.revision.service.RevisionModelUtil;
import com.vci.ubcs.starter.web.pagemodel.KeyValue;
import com.vci.ubcs.starter.util.DefaultAttrAssimtUtil;
import com.vci.ubcs.starter.util.UBCSCondition;
import com.vci.ubcs.starter.web.pagemodel.*;
import com.vci.ubcs.starter.web.util.BeanUtilForVCI;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import com.vci.ubcs.starter.web.util.WebUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -53,6 +67,7 @@
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.vci.ubcs.code.constant.FrameWorkDefaultValueConstant.FRAMEWORK_RELEASE_EDITING;
import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.DATA_OID_NOT_EXIST;
@@ -94,15 +109,27 @@
   @Resource
   private RevisionModelUtil revisionModelUtil;
   @Value("${user-info.tenant-id}")
   private String tenantId;
   @Value("${user-info.id}")
   private String userId;
   /**
    * 分页查询
    * @param query
    * @param conidtionMap
    * @return
    */
   @Override
   public IPage<CodeRuleVO> gridCodeRule(IPage<CodeRuleVO> page, CodeRuleVO codeRule) {
      //对生命周期的枚举进行转换
      if(!StringUtils.isEmpty(codeRule.getLcStatusText())){
         codeRule.setLcStatus(CodeRuleLC.getValueByText(codeRule.getLcStatusText()));
   public IPage<CodeRuleVO> gridCodeRule(Query query, Map<String,Object> conidtionMap) {
      //如果等于自己配置的管理组租户id和管理组超管账号,就不需要按照规则所有者来进行查询
      if(!(AuthUtil.getTenantId().equals(this.tenantId) && AuthUtil.getUserId().toString().equals(this.userId))){
         // 按照规则所有者来查询
         conidtionMap.put("owner",AuthUtil.getUserId());
      }
      List<CodeRule> codeRulePage = codeRuleMapper.selectCodeRulePage(page, codeRule);
      IPage<CodeRule> codeRuleIPage = this.codeRuleMapper.selectPage(Condition.getPage(query), UBCSCondition.getQueryWrapper(conidtionMap, CodeRule.class));
      //do转vo同时setLcStatusText生命周期值,并包装成分页对象返回
      return page.setRecords(CodeRuleWrapper.build().listVO(codeRulePage));
      return CodeRuleWrapper.build().pageVO(codeRuleIPage);
   }
   /**
@@ -113,32 +140,39 @@
    * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
    */
   @Override
   public boolean addSave(CodeRuleDTO codeRuleDTO) throws VciBaseException {
   public R addSave(CodeRuleDTO codeRuleDTO) throws VciBaseException {
      VciBaseUtil.alertNotNull(codeRuleDTO, "需要添加的数据对象");
      //将DTO转换为DO
      if(checkCodeRuleRepeat(codeRuleDTO)){
         return R.fail("规则编号已存在!");
      }
      // 将DTO转换为DO
      CodeRule codeRule = Objects.requireNonNull(BeanUtil.copy(codeRuleDTO, CodeRule.class));
      String userId = AuthUtil.getUserId().toString();
      codeRule.setOid(VciBaseUtil.getPk());
      codeRule.setRevisionOid(VciBaseUtil.getPk());
      codeRule.setNameOid(VciBaseUtil.getPk());
      codeRule.setBtmname("coderule");
      codeRule.setLastR("1");
      codeRule.setLastV("1");
      codeRule.setFirstR("1");
      codeRule.setFirstV("1");
      codeRule.setCreator(userId);
      codeRule.setCreateTime(new Date());
      codeRule.setLastModifier("1");
      codeRule.setLastModifyTime(new Date());
      codeRule.setVersionRule("0");
      codeRule.setVersionSeq(1);
      // 填充默认值
      DefaultAttrAssimtUtil.addDefaultAttrAssimt(codeRule, MdmBtmTypeConstant.CODE_RULE);
      codeRule.setLctid(CODE_RULE_LC);
      codeRule.setLcStatus(FRAMEWORK_RELEASE_EDITING);
      codeRule.setOwner("1");
      codeRule.setCreator(userId);
      codeRule.setLastModifier(userId);
      return codeRuleMapper.insert(codeRule)>0;
      return R.status(codeRuleMapper.insert(codeRule)>0);
   }
   /**
    * 检查id编号是否重复
    * @param codeRuleDTO 当前判断是否重复的对象
    * @return 返回false表示未重复
    */
   @Override
   public boolean checkCodeRuleRepeat(CodeRuleDTO codeRuleDTO){
      List<CodeRule> codeRulesList = this.codeRuleMapper.selectList(Wrappers.<CodeRule>query().lambda().eq(CodeRule::getId, codeRuleDTO.getId()));
      if(!codeRulesList.isEmpty()){
         return codeRulesList.parallelStream().anyMatch(codeRule -> {
            if(StringUtils.isNotBlank(codeRuleDTO.getOid())){
               // 代表是修改
               return !codeRule.getOid().equals(codeRuleDTO.getOid());
            }else {
               return true;
            }
         });
      }
      return false;
   }
   /**
@@ -148,7 +182,7 @@
    * @return true表示可以编辑或删除,false表示不可以
    */
   @Override
   public boolean checkEditDelStatus(String lcStatus) {
   public boolean checkEditDelStatus(String lcStatus) throws VciBaseException {
      if (CodeRuleLC.RELEASED.getValue().equals(lcStatus) || CodeRuleLC.DISABLED.getValue().equals(lcStatus)) {
         return false;
      }
@@ -163,15 +197,22 @@
    * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
    */
   @Override
   public boolean editSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
   public R editSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
      VciBaseUtil.alertNotNull(codeRuleDTO, "数据对象", codeRuleDTO.getOid(), "主数据编码规则主键");
      if(checkCodeRuleRepeat(codeRuleDTO)){
         return R.fail("规则编号已存在!");
      }
      if (!checkEditDelStatus(codeRuleDTO.getLcStatus())) {
         throw new VciBaseException("编码规则已发布,不允许编辑或删除");
      }
      //将DTO转换为DO
      CodeRule codeRule = selectByOid(codeRuleDTO.getOid());
      revisionModelUtil.copyFromDTOIgnore(codeRuleDTO, codeRule);
      return codeRuleMapper.updateById(codeRule)>0;
      if(!codeRule.getOwner().equals(codeRuleDTO.getOwner())){
         codeRule.setOwner(codeRuleDTO.getOwner());
      }
      DefaultAttrAssimtUtil.updateDefaultAttrAssimt(codeRule);
      return R.status(codeRuleMapper.updateById(codeRule)>0);
   }
   /**
@@ -292,7 +333,7 @@
    */
   @Override
   public Collection<CodeClassifyVO> listUseRangeInCodeClassify(String oid) {
      List<CodeClassify> codeClassifies = codeClassifyServcie.selectByWrapper(Wrappers.<CodeClassify>query().eq("codeRuleOid", oid));
      List<CodeClassify> codeClassifies = codeClassifyServcie.selectByWrapper(Wrappers.<CodeClassify>query().lambda().eq(CodeClassify::getCodeRuleOid, oid));
      return CodeClassifyWrapper.build().listVO(codeClassifies);
   }
@@ -324,6 +365,21 @@
   }
   /**
    * 主键批量获取主数据编码规则
    *
    * @param oidCollections 主键集合,但是受性能影响,建议一次查询不超过10000个
    * @param hasSec         是否包含码段
    * @return 主数据编码规则显示对象
    * @throws VciBaseException 查询出现异常时会抛出
    */
   @Override
   public Collection<CodeRuleVO> listCodeRuleByIds(Collection<String> oidCollections, boolean hasSec) throws VciBaseException {
      VciBaseUtil.alertNotNull(oidCollections, "数据对象主键集合");
      List<CodeRule> codeRuleDOList = listCodeRuleDOByOidCollections(oidCollections);
      return codeRuleDO2VOs(codeRuleDOList, true);
   }
   /**
    * 使用主键集合查询数据对象
    *
    * @param oidCollections 主键的集合
@@ -346,14 +402,13 @@
   /**
    * 参照主数据编码规则列表
    *
    * @param codeRule 查询条件
    * @param page   分页和排序
    * @param bladeQueryObject 查询条件
    * @return 主数据编码规则显示对象列表,生效的内容
    * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
    */
   @Override
   public IPage<CodeRuleVO> refDataGridCodeRule(IPage<CodeRuleVO> page, CodeRuleVO codeRule) throws VciBaseException {
      return gridCodeRule(page, codeRule);
   public IPage<CodeRuleVO> refDataGridCodeRule(BladeQueryObject bladeQueryObject) throws VciBaseException {
      return gridCodeRule(bladeQueryObject.getQuery(), bladeQueryObject.getConditionMap());
   }
   /**
@@ -386,6 +441,9 @@
         if (!CodeRuleLC.EDITING.getValue().equals(codeRuleDO.getLcStatus())) {
            return R.fail("非编辑状态的编码规则无法发布");
         }
         // 发布时将码段类型按照字符串分隔的形式拼接并存储
         String secTypeStr = codeBasicSecService.listCodeBasicSecByRuleOid(codeRuleDO.getOid()).stream().map(CodeBasicSecVO::getSecType).collect(Collectors.joining(","));
         codeRuleDO.setBasicSecTypes(secTypeStr);
         count = codeRuleMapper.update(null,wrapper.set(CodeRule::getLcStatus, CodeRuleLC.RELEASED.getValue()));
      } else {
         return R.fail("暂不支持的操作类型");
@@ -429,7 +487,6 @@
      return R.data(exFlag&&exFlag1);
   }
   /**
    * 使用主键获取编码规则的内容
    *
@@ -440,6 +497,107 @@
   public CodeRuleVO getObjectHasSecByOid(String oid) {
      CodeRule ruleDO = selectByOid(oid);
      return codeRuleDO2VO(ruleDO, true);
   }
   /**
    * 检查相似编码规则,并返回对应的结果
    * @param oid 主键
    * @return 执行结果
    */
   @Override
   public R checkLikeCodeRule(String oid) throws VciBaseException {
      // 1、查询出当前要发布的规则
      CodeRuleVO codeRuleVO = getObjectHasSecByOid(oid);
      // 当前发布的规则不存在码段信息
      if(Func.isEmpty(codeRuleVO.getSecVOList())){
         return R.data(null);
      }
      // 2、找出与当前发布的规则码段顺序一致的规则
      String secTypes = codeRuleVO.getSecVOList().stream().map(CodeBasicSecVO::getSecType).collect(Collectors.joining(","));
      //先查询出所有已发布的编码规则
      List<CodeRule> codeRuleList = codeRuleMapper.selectList(Wrappers.<CodeRule>query().lambda().eq(CodeRule::getLcStatus, FrameWorkDefaultValueConstant.FRAMEWORK_RELEASE_RELEASED));
      List<CodeRuleVO> codeRuleVOS = codeRuleDO2VOs(codeRuleList, true);
      // 满足码段类型顺序一致的要求的编码规则,可以进入下一步比较规则
      List<CodeRuleVO> codeRuleVOStream = codeRuleVOS.parallelStream().filter(item -> {
         // 对BasicSec使用orderNum排序(查询的时候其实已经排过一次序了),并将secType用逗号分隔拼接成一个字符串
         String itemSecTypes = item.getSecVOList().parallelStream().sorted(Comparator.comparing(CodeBasicSecVO::getOrderNum)).map(CodeBasicSecVO::getSecType).collect(Collectors.joining(","));
         if (secTypes.equals(itemSecTypes)) {
            return true;
         }
         return false;
      }).collect(Collectors.toList());
      // 3、各类码段分别进行比对
      codeRuleVOStream.parallelStream().filter(item->{
         item.getSecVOList().stream().filter(basicSec->{
            switch (basicSec.getSecType()){
               // 属性码段,比对属性和属性所在分类(referCodeClassifyOid)
               case "codeattrsec":
                  List<CodeBasicSecVO> codeattrsec = this.getBasicSecBySecType("codeattrsec", codeRuleVO);
                  break;
               // 可变码段,比对码段长度,编码补位方式和补位时的字符
               case "codevariablesec":
                  break;
               // 固定码段比对按升序排序的码值,
               case "codefixedsec":
                  break;
               // 分类码段比对按升序排序码值
               case "codeclassifysec":
                  break;
               // 日期码段比对日期格式
               case "codedatesec":
                  break;
               // 引用码段,比对参照应用的业务类型
               case "coderefersec":
                  break;
               // 层级码段,比对层级类型、层级的值、字符截取类型、取值类型
               case "codelevelsec":
                  break;
               case "codeserialsec":
               // 流水码段比对码段的长度
                  break;
               default:
                  return false;
            }
            return false;
         });
         return false;
      });
      // 最后将结果进行返回
      return null;
   }
   private List<CodeBasicSecVO> getBasicSecBySecType(String secType,CodeRuleVO codeRuleVO){
      List<CodeBasicSecVO> codeBasicSecVOList = codeRuleVO.getSecVOList().parallelStream().filter(item -> {
         if (item.getSecType().equals(secType)) {
            return true;
         }
         return false;
      }).collect(Collectors.toList());
      return codeBasicSecVOList;
   }
   /**
    * 批量数据对象转换为显示对象
    *
    * @param codeRules 数据对象列表
    * @param hasSec      是否包含码段
    * @return 显示对象
    * @throws VciBaseException 参数为空或者不存在的时候会抛出异常
    */
   public List<CodeRuleVO> codeRuleDO2VOs(Collection<CodeRule> codeRules, boolean hasSec) throws VciBaseException {
      List<CodeRuleVO> voList = new ArrayList<CodeRuleVO>();
      if (!CollectionUtils.isEmpty(codeRules)) {
         for (CodeRule s : codeRules) {
            CodeRuleVO vo = codeRuleDO2VO(s,true);
            if (vo != null) {
               voList.add(vo);
            }
         }
      }
      return voList;
   }
   /**
@@ -457,8 +615,9 @@
         //如果有lcstatus的类的话
         vo.setLcStatusText(CodeRuleLC.getTextByValue(vo.getLcStatus()));
         if (hasSec) {
            List<CodeBasicSecVO> codeBasicSecVOS = codeBasicSecService.listCodeBasicSecByRuleOid(vo.getOid());
            //查询码段
            vo.setSecVOList(codeBasicSecService.listCodeBasicSecByRuleOid(vo.getOid()));
            vo.setSecVOList(codeBasicSecVOS);
         }
      }
      return vo;