ludc
2023-04-26 7f0570d20aac189f1b170942bd7100b281a1c824
Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/CodeRuleServiceImpl.java
@@ -17,16 +17,18 @@
package com.vci.ubcs.code.service.impl;
import com.alibaba.cloud.commons.lang.StringUtils;
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.dto.CodeBasicSecDTO;
import com.vci.ubcs.code.dto.CodeRuleDTO;
import com.vci.ubcs.code.entity.CodeAllCode;
import com.vci.ubcs.code.entity.CodeClassify;
import com.vci.ubcs.code.entity.CodeRule;
import com.vci.ubcs.code.entity.*;
import com.vci.ubcs.code.lifecycle.CodeRuleLC;
import com.vci.ubcs.code.mapper.CodeRuleMapper;
import com.vci.ubcs.code.service.ICodeAllcodeService;
import com.vci.ubcs.code.mapper.CodeSerialValueMapper;
import com.vci.ubcs.code.mapper.CommonsMapper;
import com.vci.ubcs.code.service.ICodeAllCodeService;
import com.vci.ubcs.code.service.ICodeBasicSecService;
import com.vci.ubcs.code.service.ICodeClassifyService;
import com.vci.ubcs.code.service.ICodeRuleService;
@@ -34,21 +36,23 @@
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.com.vci.starter.exception.VciBaseException;
import com.vci.ubcs.com.vci.starter.revision.service.RevisionModelUtil;
import com.vci.ubcs.com.vci.starter.web.util.VciBaseUtil;
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.web.util.BeanUtilForVCI;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import com.vci.ubcs.starter.web.util.WebUtil;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import static com.vci.ubcs.code.constant.FrameWorkDefaultValueConstant.FRAMEWORK_RELEASE_EDITING;
import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.DATA_OID_NOT_EXIST;
@@ -68,13 +72,19 @@
    */
   @Resource
   private CodeRuleMapper codeRuleMapper;
   @Resource
   private CommonsMapper commonsMapper;
   @Resource
   private CodeSerialValueMapper codeSerialValueMapper;
   /**
    * 其他服务层
    */
   @Resource
   @Lazy
   private ICodeClassifyService codeClassifyServcie;
   @Resource
   private ICodeAllcodeService codeAllcodeService;
   private ICodeAllCodeService codeAllcodeService;
   @Resource
   private ICodeBasicSecService codeBasicSecService;
@@ -85,7 +95,7 @@
   private RevisionModelUtil revisionModelUtil;
   @Override
   public IPage<CodeRuleVO> selectPlCodeRulePage(IPage<CodeRuleVO> page, CodeRuleVO codeRule) {
   public IPage<CodeRuleVO> gridCodeRule(IPage<CodeRuleVO> page, CodeRuleVO codeRule) {
      //对生命周期的枚举进行转换
      if(!StringUtils.isEmpty(codeRule.getLcStatusText())){
         codeRule.setLcStatus(CodeRuleLC.getValueByText(codeRule.getLcStatusText()));
@@ -103,12 +113,13 @@
    * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
    */
   @Override
   public Boolean addSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
   public boolean addSave(CodeRuleDTO codeRuleDTO) throws VciBaseException {
      VciBaseUtil.alertNotNull(codeRuleDTO, "需要添加的数据对象");
      //将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");
@@ -152,7 +163,7 @@
    * @throws VciBaseException 参数为空,唯一项,必输项不通过时会抛出异常
    */
   @Override
   public Boolean editSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
   public boolean editSave(CodeRuleDTO codeRuleDTO) throws VciBaseException{
      VciBaseUtil.alertNotNull(codeRuleDTO, "数据对象", codeRuleDTO.getOid(), "主数据编码规则主键");
      if (!checkEditDelStatus(codeRuleDTO.getLcStatus())) {
         throw new VciBaseException("编码规则已发布,不允许编辑或删除");
@@ -183,16 +194,62 @@
         }
      }
      List<CodeAllCode> codeDOList = codeAllcodeService.selectByWrapper(Wrappers.<CodeAllCode>query().eq("codeRuleOid", codeRuleDTO.getOid()));
      List<CodeAllCode> codeDOList = codeAllcodeService.selectByWrapper(Wrappers.<CodeAllCode>query().lambda().eq(CodeAllCode::getCodeRuleOid, codeRuleDTO.getOid()));
      if (!CollectionUtils.isEmpty(codeDOList)) {
         return R.fail("编码规则已生成编码,不允许删除");
      }
      //执行删除操作
      //WebUtil.setPersistence(false);
      boolean resBoolean = codeBasicSecService.batchDeleteSecByCodeRuleOid(codeRule.getOid());
      resBoolean = codeRuleMapper.deleteById(codeRule.getOid())>0;
      boolean resBoolean1 = codeRuleMapper.deleteById(codeRule.getOid()) > 0;
      //WebUtil.setPersistence(true);
      return R.status(resBoolean);
      return R.status(resBoolean&&resBoolean1);
   }
   /**
    * 编码规则清空所有已生成的编码
    *
    * @param oid 编码规则主键
    * @return 执行结果
    */
   @Override
   public R clearAllCode(String oid) {
      WebUtil.alertNotNull(oid, "编码规则主键");
      List<CodeAllCode> codeDOList = codeAllcodeService.selectByWrapper(Wrappers.<CodeAllCode>query().lambda().eq(CodeAllCode::getCodeRuleOid, oid));
      if (CollectionUtils.isEmpty(codeDOList)) {
         return R.fail("当前编码规则没有生成编码,无需清空");
      }
      //是否需要进行btm不统一的判断?
      String btmType = codeDOList.get(0).getCreateCodeBtm();
      //多种类型下的查询
      Set<String> btmTypeSet = codeDOList.stream().filter(s -> {
         return !btmType.equalsIgnoreCase(s.getCreateCodeBtm());
      }).map(s -> s.getCreateCodeBtm()).collect(Collectors.toSet());
      List<Map<String,String>> businessDataList = new ArrayList<>();
      if (btmTypeSet.size() > 1) {
         for (String btm : btmTypeSet) {
            Map<String,String> businessData = commonsMapper.selectByCount("PL_CODE_"+btm,oid,btm);
            businessDataList.add(businessData);
         }
      }else {
         Map<String,String> businessData = commonsMapper.selectByCount("PL_CODE_"+btmType,oid,btmType);
         businessDataList.add(businessData);
      }
      if (CollectionUtils.isEmpty(businessDataList) || StringUtils.isBlank(businessDataList.get(0).get("count"))) {
         //无效查询
         return R.fail("查询失败");
      } else if (!"0".equalsIgnoreCase(businessDataList.get(0).get("count"))) {
         return R.fail("编码规则生成的编码已在业务数据中应用,不允许清空");
      }
      //没有数据,可以安全删除
      WebUtil.setPersistence(false);
      //删除码值表中的数据,但不够,如果有流水的码段,需要重新计算流水
      List<CodeSerialValue> serialValueList = codeSerialValueMapper.selectByCodeRoleId(oid);
      /*BatchCBO batchCBO = boService.batchDelete(codeDOList);
      batchCBO.getDeleteCbos().addAll(serialValueList);
      WebUtil.setPersistence(true);
      boService.persistenceBatch(batchCBO);*/
      return R.success("清空码值成功!");
   }
   /**
@@ -239,4 +296,138 @@
      return CodeClassifyWrapper.build().listVO(codeClassifies);
   }
   /**
    * 主键获取主数据编码规则
    *
    * @param oid 主键
    * @return 主数据编码规则显示对象
    * @throws VciBaseException 参数为空,数据不存在时会抛出异常
    */
   @Override
   public CodeRuleVO getObjectByOid(String oid) throws VciBaseException {
      CodeRule codeRule = selectByOid(oid);
      return CodeRuleWrapper.build().entityVO(codeRule);
   }
   /**
    * 主键批量获取主数据编码规则
    *
    * @param oidCollections 主键集合,但是受性能影响,建议一次查询不超过10000个
    * @return 主数据编码规则显示对象
    * @throws VciBaseException 查询出现异常时会抛出
    */
   @Override
   public Collection<CodeRuleVO> listCodeRuleByOids(Collection<String> oidCollections) throws VciBaseException {
      VciBaseUtil.alertNotNull(oidCollections, "数据对象主键集合");
      List<CodeRule> codeRuleDOList = listCodeRuleDOByOidCollections(oidCollections);
      return CodeRuleWrapper.build().listVO(codeRuleDOList);
   }
   /**
    * 使用主键集合查询数据对象
    *
    * @param oidCollections 主键的集合
    * @return 数据对象列表
    */
   private List<CodeRule> listCodeRuleDOByOidCollections(Collection<String> oidCollections) {
      List<CodeRule> codeRuleDOList = new ArrayList<CodeRule>();
      if (!CollectionUtils.isEmpty(oidCollections)) {
         Collection<Collection<String>> oidCollectionsList = VciBaseUtil.switchCollectionForOracleIn(oidCollections);
         for (Collection<String> oids : oidCollectionsList) {
            List<CodeRule> tempDOList = codeRuleMapper.selectBatchIds(oids);
            if (!CollectionUtils.isEmpty(tempDOList)) {
               codeRuleDOList.addAll(tempDOList);
            }
         }
      }
      return codeRuleDOList;
   }
   /**
    * 参照主数据编码规则列表
    *
    * @param codeRule 查询条件
    * @param page   分页和排序
    * @return 主数据编码规则显示对象列表,生效的内容
    * @throws VciBaseException 查询条件和分页出错的时候会抛出异常
    */
   @Override
   public IPage<CodeRuleVO> refDataGridCodeRule(IPage<CodeRuleVO> page, CodeRuleVO codeRule) throws VciBaseException {
      return gridCodeRule(page, codeRule);
   }
   /**
    * 停用与启用编码规则
    *
    * @param oid    编码规则主键
    * @param update enable:启用 disable:停用 release:发布
    * @return 执行结果
    */
   @Override
   public R updateStatus(String oid, String update) {
      int count = 0;
      //先查询对象
      CodeRule codeRuleDO = selectByOid(oid);
      //创建修改对象的的wrapper条件
      LambdaUpdateWrapper<CodeRule> wrapper = Wrappers.<CodeRule>update()
         .lambda()
         .eq(CodeRule::getOid, oid);
      if ("enable".equalsIgnoreCase(update)) {
         if (!CodeRuleLC.DISABLED.getValue().equals(codeRuleDO.getLcStatus())) {
            return R.fail("非停用状态的编码规则无需点击启用");
         }
         count = codeRuleMapper.update(null,wrapper.set(CodeRule::getLcStatus, CodeRuleLC.RELEASED.getValue()));
      } else if ("disable".equalsIgnoreCase(update)) {
         if (!CodeRuleLC.RELEASED.getValue().equals(codeRuleDO.getLcStatus())) {
            return R.fail("非发布状态的编码规则无法停用");
         }
         count = codeRuleMapper.update(null,wrapper.set(CodeRule::getLcStatus, CodeRuleLC.DISABLED.getValue()));
      } else if ("release".equalsIgnoreCase(update)) {
         if (!CodeRuleLC.EDITING.getValue().equals(codeRuleDO.getLcStatus())) {
            return R.fail("非编辑状态的编码规则无法发布");
         }
         count = codeRuleMapper.update(null,wrapper.set(CodeRule::getLcStatus, CodeRuleLC.RELEASED.getValue()));
      } else {
         return R.fail("暂不支持的操作类型");
      }
      if (count > 0) {
         return R.success("更新状态成功");
      } else {
         return R.fail("更新状态失败");
      }
   }
   /**
    * 克隆编码规则
    *
    * @param codeRuleDTO 主数据编码规则数据传输对象,oid和ts需要传输
    * @return 主数据编码规则显示对象
    */
   @Override
   @Transactional(rollbackFor = Exception.class)
   public R cloneCodeRule(CodeRuleDTO codeRuleDTO) {
      VciBaseUtil.alertNotNull(codeRuleDTO, "需要添加的数据对象");
      CodeRule codeRuleDO = new CodeRule();
      BeanUtilForVCI.copyPropertiesIgnoreCase(codeRuleDTO, codeRuleDO);
      boolean exFlag = codeRuleMapper.insert(codeRuleDO)>0;
      List<CodeBasicSecDTO> secList = codeRuleDTO.getElements();
      List<CodeBasicSec> codeBasicSecs = new ArrayList<>();
      secList.forEach(codeBasicSecDTO -> {
         codeBasicSecDTO.setPkCodeRule(codeRuleDO.getOid());
         KeyValue attrKv = codeBasicSecService.checkAttrNullableBySecType(codeBasicSecDTO);
         if (!"success".equals(attrKv.getKey())) {
            throw new VciBaseException(attrKv.getValue() + "不能为空");
         }
         CodeBasicSec codeBasicSecDO = new CodeBasicSec();
         BeanUtilForVCI.copyPropertiesIgnoreCase(codeBasicSecDTO, codeBasicSecDO);
         codeBasicSecDO.setOid("");
         codeBasicSecDO.setRevisionOid("");
         codeBasicSecDO.setNameOid("");
         codeBasicSecs.add(codeBasicSecDO);
      });
      boolean exFlag1 = this.codeBasicSecService.saveBatch(codeBasicSecs);
      return R.data(exFlag&&exFlag1);
   }
}