lihang
2023-07-04 aedc56b3c034ef33811ca873debee68d6f963a88
Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/LinkTypeServiceImpl.java
@@ -4,15 +4,8 @@
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.exception.NacosException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtQueryChainWrapper;
import com.baomidou.mybatisplus.extension.kotlin.KtUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.omd.constant.BtmTypeConstant;
import com.vci.ubcs.omd.dto.BtmAndLinkTypeDdlDTO;
@@ -26,8 +19,7 @@
import com.vci.ubcs.omd.mapper.LinkTypeMapper;
import com.vci.ubcs.omd.repeater.DomainRepeater;
import com.vci.ubcs.omd.service.*;
import com.vci.ubcs.omd.vo.LinkTypeAttributeVO;
import com.vci.ubcs.omd.vo.LinkTypeVO;
import com.vci.ubcs.omd.vo.*;
import com.vci.ubcs.omd.wrapper.LinkTypeAttributeWrapper;
import com.vci.ubcs.omd.wrapper.LinkTypeWrapper;
import com.vci.ubcs.starter.exception.VciBaseException;
@@ -49,12 +41,12 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.util.CollectionUtils;
import java.io.Serializable;
import javax.validation.constraints.NotEmpty;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -95,12 +87,10 @@
   @Autowired
   private IModifyAttributeService modifyAttributeService;
   public final String BTM_NAME = "linkType";
   /**
    * 表名前缀
    */
   public static final String PL = "PL";
   public static final String PL = "PLLT";
   /**
@@ -171,7 +161,8 @@
      if(!CollectionUtils.isEmpty(pkLinkTypeCollection)){
         List<LinkType> linkTypeDOList = listLinkTypeDOByOidCollection(pkLinkTypeCollection);
         if(!CollectionUtils.isEmpty(linkTypeDOList)) {
            return LinkTypeWrapper.build().listEntityVO(linkTypeDOList);
            Map<String,List<LinkTypeAttributeVO>> pkLinkTypeAndAttributeMap = linkTypeAttributeService.batchGetAttributesNoDefault(linkTypeDOList);
            return LinkTypeWrapper.build().listEntityVO(linkTypeDOList,pkLinkTypeAndAttributeMap);
         }
      }
      return null;
@@ -196,6 +187,7 @@
               linkTypeDOList.addAll(linkTypeDOS);
            }
         });
         return linkTypeDOList;
      }
      return null;
   }
@@ -243,7 +235,8 @@
      if(!CollectionUtils.isEmpty(linkTypeIdCollection)){
         List<LinkType> linkTypeDOList = listLinkTypeDOByIdCollection(linkTypeIdCollection);
         if(!CollectionUtils.isEmpty(linkTypeDOList)) {
            return LinkTypeWrapper.build().listEntityVO(linkTypeDOList);
            Map<String,List<LinkTypeAttributeVO>> pkLinkTypeAndAttributeMap = linkTypeAttributeService.batchGetAttributesNoDefault(linkTypeDOList);
            return LinkTypeWrapper.build().listEntityVO(linkTypeDOList, pkLinkTypeAndAttributeMap);
         }
      }
      return null;
@@ -354,6 +347,7 @@
    * @throws VciBaseException 添加出错的时候会抛出异常
    */
   @Override
   @Transactional
   public LinkTypeVO addSave(LinkTypeVO linkTypeVO, boolean autoCreateTable) throws VciBaseException {
      TransactionStatus transaction = null;
      if(autoCreateTable) {
@@ -400,20 +394,29 @@
         }
         LinkType linkTypeDO = new LinkType();
         BeanUtil.copy(linkTypeVO, linkTypeDO);
         if (StringUtils.isBlank(linkTypeDO.getTableName())){
            linkTypeDO.setTableName(getTableName(linkTypeDO.getId(),linkTypeDO.getBizDomain()));
         }
         String creator = AuthUtil.getUserAccount();
         Date now = new Date();
         linkTypeDO.setCreator(creator);
         linkTypeDO.setCreateTime(now);
         linkTypeDO.setOwner(creator);
         linkTypeDO.setBtmName(BtmTypeConstant.LINK_TYPE);
         linkTypeDO.setTs(now);
         LinkTypeWrapper.build().setBeforeInsert(linkTypeDO,creator,now);
         //处理属性
         addAttributeForLink(linkTypeVO.getAttributes(),linkTypeDO.getOid(),creator,now);
         List<LinkTypeAttributeVO> attributeVOList = addAttributeForLink(linkTypeVO.getAttributes(), linkTypeDO.getOid(), creator, now);
         baseMapper.insert(linkTypeDO);
         if(autoCreateTable) {
            dataSourceTransactionManager.commit(transaction);
            LinkTypeVO typeVO = LinkTypeWrapper.build().entityVO(linkTypeDO);
            typeVO.setAttributes(attributeVOList);
            List<LinkTypeAttributeVO> attributes = typeVO.getAttributes();
            attributes.addAll(linkTypeAttributeService.getDefaultAttributes(typeVO));
            typeVO.setAttributes(attributes);
            R r = DomainRepeater.submitLinkType(typeVO.getBizDomain(), typeVO);
            if (!r.isSuccess()){
               dataSourceTransactionManager.rollback(transaction);
            }else {
               dataSourceTransactionManager.commit(transaction);
            }
         }
      }catch (VciBaseException e){
         if(autoCreateTable) {
@@ -426,9 +429,9 @@
         }
         throw e;
      }
      if(autoCreateTable) {
      /*if(autoCreateTable) {
         checkTableSame(linkTypeVO);
      }
      }*/
      return linkTypeVO;
   }
@@ -439,11 +442,11 @@
    * @param creator 创建人
    * @param now 当前时间
    */
   private void addAttributeForLink(List<LinkTypeAttributeVO> attributesVOList,String pkLinkType,String creator,Date now){
   private List<LinkTypeAttributeVO> addAttributeForLink(List<LinkTypeAttributeVO> attributesVOList,String pkLinkType,String creator,Date now){
      List<LinkTypeAttribute> linkTypeAttributeDOList = new ArrayList<>();
      if(!CollectionUtils.isEmpty(attributesVOList)) {
         List<String> attributeIdList = new ArrayList<>();
         attributesVOList.stream().forEachOrdered(s -> {
         attributesVOList.forEach(s -> {
            linkTypeAttributeService.checkAttribute(s);
            linkTypeAttributeDOList.add(LinkTypeAttributeWrapper.build().copyVO2DO(s, pkLinkType, creator, now));
            attributeIdList.add(s.getId().toLowerCase().trim());
@@ -451,7 +454,9 @@
         //检查属性是否都存在
         attributeService.checkAttributeExists(attributeIdList);
         linkTypeAttributeService.saveOrUpdateBatch(linkTypeAttributeDOList,10000);
         return LinkTypeAttributeWrapper.build().listEntityVO(linkTypeAttributeDOList);
      }
      return null;
   }
   /**
@@ -480,8 +485,8 @@
      }
      if (!CollectionUtils.isEmpty(modifyAttributeInfoDOList)) {
         modifyAttributeService.saveOrUpdateBatch(modifyAttributeInfoDOList);
         modifyAttributeService.finishModify(modifyAttributeInfoDOList);
      }
      modifyAttributeService.finishModify(modifyAttributeInfoDOList);
   }
   /**
@@ -544,15 +549,10 @@
         if(StringUtils.isNotBlank(unExistBtmTypeId)){
            throw new VciBaseException("链接类型使用的业务类型{0}不存在,请查证",new Object[]{unExistBtmTypeId});
         }
         String creator = AuthUtil.getUserAccount();
         Date now = new Date();
         linkTypeDO.setLastModifier(creator);
         linkTypeDO.setLastModifyTime(now);
         linkTypeDO.setTs(now);
         linkTypeDO = LinkTypeWrapper.build().setBeforeUpdate(linkTypeDO,creator,now);
         //处理属性
         LinkTypeWrapper.build().copyLinkTypeVO2DO(linkTypeVO,linkTypeDO,creator,new Date());
         //先查询这个链接类型包含多少属性
@@ -566,22 +566,23 @@
         }
         saveOrUpdate(linkTypeDO);
         if(autoCreateTable) {
            List<LinkTypeAttributeVO> defaultAttributes = linkTypeAttributeService.getDefaultAttributes(linkTypeVO);
            List<LinkTypeAttributeVO> attributes = linkTypeVO.getAttributes();
            attributes.addAll(defaultAttributes);
            linkTypeVO.setAttributes(attributes);
            BtmAndLinkTypeDdlDTO ddlDTO = new BtmAndLinkTypeDdlDTO();
            DomainRepeater.checkDifferent(ddlDTO,linkTypeVO.getBizDomain());
            dataSourceTransactionManager.commit(transaction);
         }
      }catch (VciBaseException e){
      } catch (Throwable e){
         if(autoCreateTable) {
            dataSourceTransactionManager.rollback(transaction);
         }
         throw e;
      }catch (Throwable e) {
         if(autoCreateTable) {
            dataSourceTransactionManager.rollback(transaction);
         }
         throw e;
         throw new VciBaseException(e.getMessage());
      }
      if(autoCreateTable) {
      /*if(autoCreateTable) {
         checkTableSame(linkTypeVO);
      }
      }*/
      return linkTypeVO;
   }
@@ -624,10 +625,10 @@
         List<TableCheckDTO> checkList = linkTypeDOList.stream().map(linkType -> {
            TableCheckDTO tableCheckDTO = new TableCheckDTO();
            tableCheckDTO.setBtmName(linkType.getId());
            tableCheckDTO.setTableName(getTableName(linkType.getId(),linkType.getDomain()));
            tableCheckDTO.setTableName(getTableName(linkType.getId(),linkType.getBizDomain()));
            tableCheckDTO.setCheckTableHasData(true);
            tableCheckDTO.setNeedDelete(true);
            tableCheckDTO.setDomain(linkType.getDomain());
            tableCheckDTO.setDomain(linkType.getBizDomain());
            return tableCheckDTO;
         }).collect(Collectors.toList());
         R result = DomainRepeater.checkTableHasDataByTableNameThenDrop(checkList);
@@ -809,19 +810,19 @@
         attributeVO.setLastModifier(creator);
         attributeVO.setLastModifyTime(now);
         attributeVO.setTs(now);
         //查询链接类型是否存在
         linkTypeAttributeService.saveOrUpdate(attributeDO);
         if (autoEdit2Table) {
            BtmAndLinkTypeDdlDTO ddlDTO = new BtmAndLinkTypeDdlDTO();
//            ddlDTO.setLinkTypeList();
//            DomainRepeater.checkDifferent(,domain);
            LinkTypeVO linkTypeVO = LinkTypeWrapper.build().entityVO(linkTypeDO);
            ddlDTO.setLinkTypeList(Collections.singletonList(linkTypeVO));
            DomainRepeater.checkDifferent(ddlDTO,domain);
            dataSourceTransactionManager.commit(transaction);
         }
//      }catch (NacosException e){
//         if(autoEdit2Table) {
//            dataSourceTransactionManager.rollback(transaction);
//         }
//         throw new RuntimeException(e);
      }catch (NacosException e){
         if(autoEdit2Table) {
            dataSourceTransactionManager.rollback(transaction);
         }
         throw new RuntimeException(e);
      }catch (Throwable e){
         if(autoEdit2Table) {
            dataSourceTransactionManager.rollback(transaction);
@@ -845,7 +846,7 @@
      }else {
         throw new VciBaseException("领域名称不符合规则");
      }
      return PL + StringPool.UNDERSCORE + domain + StringPool.UNDERSCORE + id;
      return PL + StringPool.UNDERSCORE + domain.toUpperCase(Locale.ROOT) + StringPool.UNDERSCORE + id.toUpperCase(Locale.ROOT);
   }
   /**
@@ -865,4 +866,92 @@
      }
      return btmTypeSet;
   }
   /**
    * 按domain分组,查询链接类型属性结构
    *
    * @return 查询结果
    */
   @Override
   public List<BtmTreeVO> treeDomain() {
      try {
         Map<String, String> domain = Optional.ofNullable(DomainRepeater.getDomain()).orElseGet(ArrayList::new).stream().collect(Collectors.toMap(DomainVO::getValue, DomainVO::getLabel));
         List<LinkTypeVO> vos = LinkTypeWrapper.build().listEntityVO(baseMapper.selectAll(), null);
         Map<String, List<LinkTypeVO>> domainMap = vos.stream().collect(Collectors.groupingBy(LinkTypeVO::getBizDomain));
         List<BtmTreeVO> treeList = new ArrayList<>();
         domainMap.forEach((k,v)-> {
            if (domain.containsKey(k)){
               String label = domain.get(k);
               BtmTreeVO parent = new BtmTreeVO();
               parent.setName(label);
               parent.setId(k);
               parent.setLabel(label);
               parent.setChildList(v.stream().map(s -> {
                  BtmTreeVO child = new BtmTreeVO();
                  child.setOid(s.getOid());
                  child.setLabel(s.getId() + (s.getName() == null ? "" : "(" + s.getName() + ")"));
                  child.setId(s.getId());
                  child.setName(s.getName());
                  return child;
               }).collect(Collectors.toList()));
               treeList.add(parent);
            }
         });
         return treeList;
      } catch (NacosException e) {
         throw new RuntimeException(e);
      }
   }
   @Override
   public boolean deleteLogic(@NotEmpty List<Long> ids) {
      return false;
   }
   @Override
   public boolean changeStatus(@NotEmpty List<Long> ids, Integer status) {
      return false;
   }
   /**
    * 获取链接类型,只有默认字段
    *
    * @param linkTypeId 链接类型id
    * @return 默认字段属性
    */
   @Override
   public LinkTypeVO getLinkTypeDefaultAttrById(String linkTypeId) {
      LinkTypeVO vo = getLinkTypeById(linkTypeId);
      List<LinkTypeAttributeVO> attributes = linkTypeAttributeService.getDefaultAttributes(vo);
      vo.setAttributes(attributes);
      return vo;
   }
   /**
    * 获取链接类型,有所有的字段
    *
    * @param linkTypeId 链接类型id
    * @return 所有字段
    */
   @Override
   public LinkTypeVO getLinkTypeAllAttrById(String linkTypeId) {
      LinkTypeVO vo = getLinkTypeById(linkTypeId);
      List<LinkTypeAttributeVO> attributes = linkTypeAttributeService.getAllAttributes(vo);
      vo.setAttributes(attributes);
      return vo;
   }
   /**
    * 获取链接类型,有所有的字段
    *
    * @param linkTypeOid 链接类型主键
    * @return 所有字段
    */
   @Override
   public LinkTypeVO getLinkTypeAllAttrByOid(String linkTypeOid) {
      LinkTypeVO vo = getLinkTypeByOid(linkTypeOid);
      List<LinkTypeAttributeVO> attributes = linkTypeAttributeService.getAllAttributes(vo);
      vo.setAttributes(attributes);
      return vo;
   }
}