ludc
2023-07-06 abe9f2de85ff402a4c989dbc1427807b4e3a59f4
Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/LifeCycleServiceImpl.java
@@ -268,7 +268,7 @@
    */
   @Override
   @Transactional
   public List<LifeCycleVO> batchAddSave(List<LifeCycleDTO> lifeCycleDTOs) {
   public List<LifeCycleVO> batchAddSave(Collection<LifeCycleDTO> lifeCycleDTOs) {
      VciBaseUtil.alertNotNull(lifeCycleDTOs,"生命周期的信息");
      //先集体校验一下
      if(lifeCycleDTOs.stream().anyMatch(s->!StringUtils.hasLength(s.getId()) || !StringUtils.hasLength(s.getName())
@@ -276,7 +276,7 @@
         throw new VciBaseException("生命周期的编号,名称,起始状态,包含的节点不能为空");
      }
      //统一校验重复
      Map<String, List<LifeCycleDTO>> dtoMap = lifeCycleDTOs.stream().collect(Collectors.groupingBy(LifeCycleDTO::getId));
      Map<String, List<LifeCycleDTO>> dtoMap = lifeCycleDTOs.stream().collect(Collectors.groupingBy(s->s.getId().toLowerCase(Locale.ROOT)));
      dtoMap.forEach((id,dtos)->{
         if(dtos.size()>1){
            throw new VciBaseException("编号为【" + id + "】的生命周期重复");
@@ -286,7 +286,7 @@
         ids->{
            QueryWrapper wrapper = new QueryWrapper(LifeCycleRule.class);
            ids.stream().forEach(id->{
               wrapper.eq("lower(id)",id.toLowerCase(Locale.ROOT));
               wrapper.eq("lower(id)",id);
               wrapper.or();
            });
            wrapper.eq("1","2");
@@ -304,6 +304,156 @@
   }
   /**
    * 修改保存
    *
    * @param lifeCycleDTO 数据传输对象
    * @return
    */
   @Override
   public LifeCycleVO editSave(LifeCycleDTO lifeCycleDTO) {
      VciBaseUtil.alertNotNull(lifeCycleDTO,"生命周期信息",lifeCycleDTO.getId(),"生命周期的编号",lifeCycleDTO.getName(),"生命周期名称"
         ,lifeCycleDTO.getNodes(),"生命周期的节点",lifeCycleDTO.getStartStatus(),"起始状态",
         lifeCycleDTO.getOid(),"主键");
      LifeCycleRule rule = selectByOid(lifeCycleDTO.getOid());
      //查询重复
      QueryWrapper wrapper = new QueryWrapper(LifeCycleRule.class);
      if(rule.getId().equals(lifeCycleDTO.getId())){
         throw new VciBaseException("生命周期编号不能修改");
      }
      wrapper.eq("lower(id)",lifeCycleDTO.getId().toLowerCase(Locale.ROOT));
      wrapper.ne("oid",lifeCycleDTO.getOid());
      if(baseMapper.selectCount(wrapper)>0){
         throw new VciBaseException("生命周期的编号不能重复");
      }
      editLifeCycle(lifeCycleDTO,rule);
      return LifeCycleRuleWrapper.build().entityVO(getById(rule.getOid()));
   }
   /**
    * 修改生命周期
    * @param lifeCycleDTO
    * @param life
    */
   private void editLifeCycle(LifeCycleDTO lifeCycleDTO,LifeCycleRule life){
      if(!lifeCycleDTO.getId().matches(RegExpConstant.LETTER)){
         throw new VciBaseException("生命周期的编号只能是字母");
      }
      BeanUtil.convert(lifeCycleDTO,life);
      String creator = AuthUtil.getUserAccount();
      Date now = new Date();
      life.setLastModifier(creator);
      life.setLastModifyTime(now);
      life.setTs(now);
      //删除现在全部的数据,然后重新添加=
      List<LifeCycleNode> nodeList = selectNodeByLifeOid(life.getOid());
      List<LifeCycleEdge> edges = selectEdgeByLifeOid(life.getOid());
      List<LifeCycleLineEvent> eventList = selectEventByEdgeOids(Optional.ofNullable(edges).orElseGet(ArrayList::new).stream().map(LifeCycleEdge::getOid).collect(Collectors.toList()));
      if(!CollectionUtils.isEmpty(nodeList)){
         nodeMapper.deleteBatchIds(nodeList.stream().map(LifeCycleNode::getOid).collect(Collectors.toList()));
      }
      if(!CollectionUtils.isEmpty(edges)){
         edgeMapper.deleteBatchIds(edges.stream().map(LifeCycleEdge::getOid).collect(Collectors.toList()));
      }
      if(!CollectionUtils.isEmpty(eventList)){
         lineEventMapper.deleteBatchIds(eventList.stream().map(LifeCycleLineEvent::getOid).collect(Collectors.toList()));
      }
      List<String> statusList = new ArrayList<>();
      //处理节点
      if(!CollectionUtils.isEmpty(lifeCycleDTO.getNodes())){
         lifeCycleDTO.getNodes().stream().forEach(nodeDTO->{
            addLifeCycleNode(nodeDTO,life.getOid(),creator,now);
            statusList.add(nodeDTO.getId());
         });
      }
      if(!statusList.contains(life.getStartStatus())){
         throw new VciBaseException("起始状态不在生命周期的画布中");
      }
      //判断所有的节点在系统里都存在
      List<String> existStatusIdList = statusService.listStatusByIdCollection(statusList).stream().map(StatusVO::getId).collect(Collectors.toList());
      String unExistStatus = statusList.stream().filter(s -> !existStatusIdList.contains(s)).collect(Collectors.joining(","));
      if(StringUtils.hasLength(unExistStatus)){
         throw new VciBaseException(unExistStatus + "这些状态在状态池里不存在,不能添加到生命周期中");
      }
      //处理边界和连接线
      if(!CollectionUtils.isEmpty(lifeCycleDTO.getEdges())){
         lifeCycleDTO.getEdges().stream().forEach(edgeDTO->{
            String edgeOid = addLifeCycleEdge(edgeDTO,statusList,life.getOid(),creator,now);
            if(!CollectionUtils.isEmpty(edgeDTO.getEvents())){
               //有事件
               edgeDTO.getEvents().stream().forEach(eventDTO->{
                  addLifeCycleLineEvent(eventDTO,edgeOid,creator,now);
               });
            }
         });
      }
      baseMapper.updateById(life);
   }
   /**
    * 使用主键查询
    * @param oid
    * @return
    */
   private LifeCycleRule selectByOid(String oid){
      LifeCycleRule rule = null;
      try {
         rule = getById(oid);
      }catch (Throwable e){
         throw new VciBaseException("使用主键获取对象出错,这个数据可能不存在,或者数据重复了");
      }
      return rule;
   }
   /**
    * 批量修改保存
    *
    * @param lifeCycleDTOs
    * @return
    */
   @Override
   public List<LifeCycleVO> batchEditSave(Collection<LifeCycleDTO> lifeCycleDTOs) {
      if(CollectionUtils.isEmpty(lifeCycleDTOs)){
         return new ArrayList<>();
      }
      if(lifeCycleDTOs.stream().anyMatch(s->!StringUtils.hasLength(s.getId()) || !StringUtils.hasLength(s.getName())
         || CollectionUtils.isEmpty(s.getNodes()) || !StringUtils.hasLength(s.getStartStatus()))){
         throw new VciBaseException("生命周期的编号,名称,起始状态,包含的节点不能为空");
      }
      //统一校验重复
      Map<String, List<LifeCycleDTO>> dtoMap = lifeCycleDTOs.stream().collect(Collectors.groupingBy(s->s.getId().toLowerCase(Locale.ROOT)));
      dtoMap.forEach((id,dtos)->{
         if(dtos.size()>1){
            throw new VciBaseException("编号为【" + id + "】的生命周期重复");
         }
      });
      VciBaseUtil.switchCollectionForOracleIn(dtoMap.keySet()).stream().forEach(
         ids->{
            QueryWrapper wrapper = new QueryWrapper(LifeCycleRule.class);
            ids.stream().forEach(id->{
               wrapper.eq("lower(id)",id);
               wrapper.ne("oid",dtoMap.get(id).get(0).getOid());
               wrapper.or();
            });
            wrapper.eq("1","2");
            if(baseMapper.selectCount(wrapper)>0){
               throw new VciBaseException("生命周期的编号不能重复");
            }
         }
      );
      List<String> oidList = lifeCycleDTOs.stream().map(LifeCycleDTO::getOid).collect(Collectors.toList());
      List<LifeCycleRule> rules = listByIds(oidList);
      if(!CollectionUtils.isEmpty(rules)){
         rules.stream().forEach(life->{
            editLifeCycle(dtoMap.get(life.getId().toLowerCase(Locale.ROOT)).get(0),life);
         });
      }
      return LifeCycleRuleWrapper.build().listEntityVO(listByIds(oidList));
   }
   /**
    * 删除生命周期
    *
    * @param lifeCycleDTO 数据传输对象
@@ -312,22 +462,93 @@
   @Transactional
   public void delete(LifeCycleDTO lifeCycleDTO) {
      VciBaseUtil.alertNotNull(lifeCycleDTO,"数据传输对象",lifeCycleDTO.getOid(),"主键");
      LifeCycleRule rule = null;
      try {
         rule = getById(lifeCycleDTO.getOid());
      }catch (Throwable e){
         throw new VciBaseException("使用主键获取对象出错,这个数据可能不存在,或者数据重复了");
      }
      LifeCycleRule rule = selectByOid(lifeCycleDTO.getOid());
      //检查被引用不能删除
      Integer count = btmTypeService.countByLifeId(lifeCycleDTO.getOid());
      Integer count = btmTypeService.countByLifeId(lifeCycleDTO.getId());
      if(count !=null && count>0){
         throw new VciBaseException("生命周期被使用,不能被删除");
      }
      //我们查询全部node和edge,然后一起删除
      List<LifeCycleNode> nodeList = selectNodeByLifeOid(rule.getOid());
      List<LifeCycleEdge> edges = selectEdgeByLifeOid(rule.getOid());
      List<LifeCycleLineEvent> eventList = selectEventByEdgeOids(Optional.ofNullable(edges).orElseGet(ArrayList::new).stream().map(LifeCycleEdge::getOid).collect(Collectors.toList()));
      if(!CollectionUtils.isEmpty(nodeList)){
         nodeMapper.deleteBatchIds(nodeList.stream().map(LifeCycleNode::getOid).collect(Collectors.toList()));
      }
      if(!CollectionUtils.isEmpty(edges)){
         edgeMapper.deleteBatchIds(edges.stream().map(LifeCycleEdge::getOid).collect(Collectors.toList()));
      }
      if(!CollectionUtils.isEmpty(eventList)){
         lineEventMapper.deleteBatchIds(eventList.stream().map(LifeCycleLineEvent::getOid).collect(Collectors.toList()));
      }
      baseMapper.deleteById(rule);
   }
   /**
    * 查询链接线
    * @param lifeOid
    * @return
    */
   private List<LifeCycleEdge> selectEdgeByLifeOid(String lifeOid){
       if(!StringUtils.hasLength(lifeOid)){
          return new ArrayList<>();
       }
      LambdaQueryWrapper<LifeCycleEdge> query = new LambdaQueryWrapper<LifeCycleEdge>();
      query.eq(LifeCycleEdge::getLifeCycleOid,lifeOid);
      return edgeMapper.selectList(query);
   }
   /**
    * 获取节点的信息
    * @param lifeOids 生命周期的主键集合
    * @return
    */
   private List<LifeCycleEdge> selectEdgeByLifeOids(Collection<String> lifeOids){
      if(!CollectionUtils.isEmpty(lifeOids)){
         return new ArrayList<>();
      }
      List<LifeCycleEdge> edgeList = new ArrayList<>();
      VciBaseUtil.switchCollectionForOracleIn(lifeOids).stream().forEach(lOids->{
         LambdaQueryWrapper<LifeCycleEdge> query = new LambdaQueryWrapper<LifeCycleEdge>();
         lOids.stream().forEach(lOid->{
            query.eq(LifeCycleEdge::getLifeCycleOid,lOid);
            query.or();
         });
         query.eq(LifeCycleEdge::getLifeCycleOid,"-1");
         edgeList.addAll(edgeMapper.selectList(query));
      });
      return edgeList;
   }
   /**
    * 查询链接线上的事件
    * @param edgeOids
    * @return
    */
   private List<LifeCycleLineEvent> selectEventByEdgeOids(Collection<String> edgeOids){
      if(CollectionUtils.isEmpty(edgeOids)){
         return new ArrayList<>();
      }
      List<LifeCycleLineEvent> eventList = new ArrayList<>();
      VciBaseUtil.switchCollectionForOracleIn(edgeOids).stream().forEach(edgeOidList->{
         LambdaQueryWrapper<LifeCycleLineEvent> query = new LambdaQueryWrapper<LifeCycleLineEvent>();
         edgeOidList.stream().forEach(edgeOid->{
            query.eq(LifeCycleLineEvent::getPkLifeCycleEdge,edgeOid);
            query.or();
         });
         query.eq(LifeCycleLineEvent::getPkLifeCycleEdge,"-1");
         eventList.addAll(lineEventMapper.selectList(query));
      });
      return eventList;
   }
   /**
    * 获取节点的信息
    * @param lifeOid
    * @return
    */
   private List<LifeCycleNode> selectNodeByLifeOid(String lifeOid){
      if(!StringUtils.hasLength(lifeOid)){
         return new ArrayList<>();
@@ -338,13 +559,60 @@
   }
   /**
    * 获取节点的信息
    * @param lifeOids 生命周期的主键集合
    * @return
    */
   private List<LifeCycleNode> selectNodeByLifeOids(Collection<String> lifeOids){
      if(!CollectionUtils.isEmpty(lifeOids)){
         return new ArrayList<>();
      }
      List<LifeCycleNode> nodeList = new ArrayList<>();
      VciBaseUtil.switchCollectionForOracleIn(lifeOids).stream().forEach(lOids->{
         LambdaQueryWrapper<LifeCycleNode> query = new LambdaQueryWrapper<LifeCycleNode>();
         lOids.stream().forEach(lOid->{
            query.eq(LifeCycleNode::getLifeCycleOid,lOid);
            query.or();
         });
         query.eq(LifeCycleNode::getLifeCycleOid,"-1");
         nodeList.addAll(nodeMapper.selectList(query));
      });
      return nodeList;
   }
   /**
    * 批量删除生命周期
    *
    * @param lifeCycleDTOs 数据传输对象列表
    */
   @Override
   @Transactional
   public void batchDelete(List<LifeCycleDTO> lifeCycleDTOs) {
      VciBaseUtil.alertNotNull(lifeCycleDTOs,"生命周期的信息");
      if(lifeCycleDTOs.stream().anyMatch(s->!StringUtils.hasLength(s.getOid()))){
         throw new VciBaseException("生命周期的主键不能为空");
      }
      List<String> oidList = lifeCycleDTOs.stream().map(LifeCycleDTO::getOid).collect(Collectors.toList());
      List<LifeCycleRule> lifeList = baseMapper.selectBatchIds(oidList);
      //批量查询
      String usedBtmTypeId = Optional.ofNullable(btmTypeService.selectByLifeIds(lifeList.stream().map(LifeCycleRule::getId).collect(Collectors.toList()))).orElseGet(ArrayList::new).stream().map(BtmTypeVO::getId).collect(Collectors.joining(","));
      if(StringUtils.hasLength(usedBtmTypeId)){
         throw new VciBaseException(usedBtmTypeId + "这些业务类型引用了生命周期,不能删除");
      }
      List<LifeCycleNode> nodeList = selectNodeByLifeOids(oidList);
      List<LifeCycleEdge> edgeList = selectEdgeByLifeOids(oidList);
      if(!CollectionUtils.isEmpty(nodeList)){
         nodeMapper.deleteBatchIds(nodeList.stream().map(LifeCycleNode::getOid).collect(Collectors.toList()));
      }
      if(!CollectionUtils.isEmpty(edgeList)){
         List<String> edgeOids = edgeList.stream().map(LifeCycleEdge::getOid).collect(Collectors.toList());
         edgeMapper.deleteBatchIds(edgeOids);
         List<LifeCycleLineEvent> eventList = selectEventByEdgeOids(edgeOids);
         if(!CollectionUtils.isEmpty(eventList)){
            lineEventMapper.deleteBatchIds(eventList.stream().map(LifeCycleLineEvent::getOid).collect(Collectors.toList()));
         }
      }
      baseMapper.deleteBatchIds(oidList);
   }
   /**
@@ -355,6 +623,8 @@
    */
   @Override
   public List<BtmTypeVO> listUses(LifeCycleDTO lifeCycleDTO) {
      return null;
      VciBaseUtil.alertNotNull(lifeCycleDTO,"数据传输对象",lifeCycleDTO.getOid(),"主键");
      LifeCycleRule rule = selectByOid(lifeCycleDTO.getOid());
      return btmTypeService.selectByLifeId(rule.getId());
   }
}