田源
2023-05-09 549ecbf13b14a4deb74e42828abcd46ccb68a7c0
Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/BtmTypeServiceImpl.java
@@ -1,15 +1,19 @@
package com.vci.ubcs.omd.service.impl;
import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.exception.NacosException;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vci.ubcs.omd.constant.BtmTypeFieldConstant;
import com.vci.ubcs.omd.dto.BtmAndLinkTypeDdlDTO;
import com.vci.ubcs.omd.entity.Attribute;
import com.vci.ubcs.omd.repeater.DomainRepeater;
import com.vci.ubcs.omd.service.IAttributeService;
import com.vci.ubcs.omd.service.IBtmTypeAttributeService;
import com.vci.ubcs.omd.service.IModifyAttributeService;
import com.vci.ubcs.omd.wrapper.BtmTypeAttributeWrapper;
import com.vci.ubcs.omd.vo.BtmTypeTreeVO;
import com.vci.ubcs.omd.wrapper.BtmTypeWrapper;
import com.vci.ubcs.omd.wrapper.ModifyAttributeWrapper;
import com.vci.ubcs.starter.web.constant.OmdRegExpConstant;
@@ -24,6 +28,7 @@
import com.vci.ubcs.omd.vo.BtmTypeAttributeVO;
import com.vci.ubcs.omd.vo.LinkTypeVO;
import com.vci.ubcs.starter.exception.VciBaseException;
import com.vci.ubcs.starter.web.enumpck.BooleanEnum;
import com.vci.ubcs.starter.web.enumpck.VciFieldTypeEnum;
import com.vci.ubcs.starter.web.util.VciBaseUtil;
import com.vci.ubcs.starter.web.util.VciDateUtil;
@@ -38,6 +43,7 @@
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;
@@ -71,6 +77,12 @@
    */
   @Autowired
   private IModifyAttributeService modifyAttributeService;
   /**
    * 属性的服务
    */
   @Autowired
   private IAttributeService attributeService;
   /**
    * 表名前缀
@@ -621,4 +633,174 @@
   public boolean changeStatus(@NotEmpty List<Long> ids, Integer status) {
      return false;
   }
   /**
    * 按domain分组,查询业务类型属性结构
    *
    * @return 查询结果
    */
   @Override
   public List<BtmTypeTreeVO> treeDomain() {
      try {
         List<String> domain = DomainRepeater.getDomain();
         List<BtmTypeVO> vos = BtmTypeWrapper.build().listEntityVO(baseMapper.selectAll());
         Map<String, List<BtmTypeVO>> domainMap = vos.stream().collect(Collectors.groupingBy(BtmTypeVO::getDomain));
         List<BtmTypeTreeVO> treeList = new ArrayList<>();
         domainMap.forEach((k,v)-> {
            if (domain.contains(k)){
               BtmTypeTreeVO parent = new BtmTypeTreeVO();
               parent.setOid(k);
               parent.setName(k);
               parent.setChildList(v.stream().map(s -> {
                  BtmTypeTreeVO child = new BtmTypeTreeVO();
                  child.setOid(s.getOid());
                  child.setName(s.getId() + " " + (s.getName() == null ? "" : s.getName()));
                  return child;
               }).collect(Collectors.toList()));
               treeList.add(parent);
            }
         });
         return treeList;
      } catch (NacosException e) {
         throw new RuntimeException(e);
      }
   }
   /**
    * 从数据库表中获取
    *
    * @param domain 领域值
    * @return 读取结果
    */
   @Override
   public List<BtmTypeVO> getFromTable(String domain) {
      VciBaseUtil.alertNotNull(domain,"领域值");
      try {
         if (!DomainRepeater.getDomain().contains(domain)){
            return null;
         }
         R result = DomainRepeater.getFromTable(domain);
         if (result.isSuccess()){
            List<BtmTypeVO> list = new ArrayList<>();
            Object dataList = result.getData();
            if (dataList instanceof  List){
               ((List<?>) dataList).forEach(data -> {
                  JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(data));
                  list.add(BeanUtil.copy(jsonObject,BtmTypeVO.class));
               });
            }
            return list;
         }else {
            return new ArrayList<>();
         }
      } catch (NacosException e) {
         throw new RuntimeException(e);
      }
   }
   /**
    * 选择数据库表保存为业务类型
    *
    * @param btmTypeDTOList 页面传输对象
    * @param domain 领域
    * @return 执行结果
    */
   @Override
   @Transactional
   public List<BtmTypeVO> saveFromTable(List<BtmTypeDTO> btmTypeDTOList,String domain) {
      // 之前已经做过重复校验了,这边直接执行保存逻辑即可
      List<BtmType> btmList = new ArrayList<>();
      List<BtmTypeAttribute> btmTypeAttributes = new ArrayList<>();
      Map<String,Attribute> idAttrMap = new HashMap<>();
      // 校验属性是否存在
      Set<String> attributeIds = new HashSet<>();
      Date now = new Date();
      String user = AuthUtil.getUserAccount();
      btmTypeDTOList.stream().forEach(dto -> {
         BtmType btmType = Objects.requireNonNull(BeanUtil.copy(dto, BtmType.class));
         btmType.setOid(VciBaseUtil.getPk());
         btmType.setTs(now);
         btmType.setCreator(user);
         btmType.setBtmName("btmType");
         btmType.setCreateTime(now);
         btmType.setDomain(domain);
         btmType.setLastModifyTime(now);
         btmType.setLastModifier(user);
         btmType.setName(dto.getDescription());
         Set<String> ids = dto.getAttributes().stream().filter(s -> {
            return ! (BtmTypeFieldConstant.BASIC_FIELD_MAP.containsKey(s.getId())
            || BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP.containsKey(s.getId())
            || BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP.containsKey(s.getId())
            || BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP.containsKey(s.getId())
            );
         }).map(attr -> {
            // 添加属性,1是校验,2是添加
            Attribute attribute = new Attribute();
            attribute.setKey(attr.getId());
            attribute.setLabel(attr.getName());
            attribute.setDictKey(attr.getAttrDataType());
            attribute.setMaxLength(attr.getAttributeLength());
            attribute.setPrecision(attr.getPrecisionLength());
            attribute.setDescription(attr.getDescription());
            attribute.setDefaultValue(attr.getDefaultValue());
            attribute.setNullable(String.valueOf(attr.isNullableFlag()));
            attribute.setHashtag(attr.getDescription());
            attribute.setReferToId(attr.getReferBtmTypeId());
            attribute.setReferToName(attr.getReferBtmTypeName());
            attribute.setTs(now);
            attribute.setCreateTime(now);
            attribute.setCreateUser(AuthUtil.getUserId());
            idAttrMap.put(attribute.getKey(),attribute);
            // 在这里给业务类型设置版本控制、生命周期控制、密级控制相关的信息
            // 添加业务类型和属性的关系
            BtmTypeAttribute btmTypeAttribute = Objects.requireNonNull(BeanUtil.copy(attr, BtmTypeAttribute.class));
            btmTypeAttribute.setPkBtmType(btmType.getOid());
            btmTypeAttribute.setCreator(user);
            btmTypeAttribute.setCreateTime(now);
            btmTypeAttribute.setLastModifier(user);
            btmTypeAttribute.setLastModifyTime(now);
//            btmTypeAttribute.setBtmName("btmTypeAttribute");
            btmTypeAttribute.setOid(VciBaseUtil.getPk());
            btmTypeAttribute.setBtmName(btmType.getId());
            btmTypeAttribute.setOwner(user);
            btmTypeAttributes.add(btmTypeAttribute);
            return attr;
         }).map(BtmTypeLinkAttributesDTO::getId).collect(Collectors.toSet());
         attributeIds.addAll(ids);
         btmType.setLifeCycleFlag(String.valueOf(ids.stream().anyMatch(BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP::containsKey)));
         btmType.setRevisionFlag(String.valueOf(ids.stream().anyMatch(BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP::containsKey)));
         btmType.setSecretFlag(String.valueOf(ids.stream().anyMatch(BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP::containsKey)));
         btmType.setConsistence(BooleanEnum.TRUE.getValue());
         btmList.add(btmType);
      });
      List<Attribute> existAttr = attributeService.list(Wrappers.<Attribute>query().lambda().in(Attribute::getKey, attributeIds));
      if (!CollectionUtils.isEmpty(btmList)){
         baseMapper.batchInsert(btmList);
      }
      if (!CollectionUtils.isEmpty(btmTypeAttributes)){
         btmTypeAttributeService.saveBatch(btmTypeAttributes);
      }
      BtmTypeFieldConstant.BASIC_FIELD_MAP.forEach((k,v) -> {
         idAttrMap.remove(k);
      });
      BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP.forEach((k,v) -> {
         idAttrMap.remove(k);
      });
      BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP.forEach((k,v) -> {
         idAttrMap.remove(k);
      });
      BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP.forEach((k,v) -> {
         idAttrMap.remove(k);
      });
      if (CollectionUtils.isEmpty(existAttr)){
         attributeService.saveBatch(idAttrMap.values());
      }else {
         List<Attribute> addList = idAttrMap.values().stream().filter(a -> existAttr.stream().noneMatch(b -> StringUtils.equals(a.getKey(), b.getKey()))).collect(Collectors.toList());
         if (CollectionUtils.isEmpty(addList)) {
            attributeService.saveBatch(addList);
         }
      }
      return BtmTypeWrapper.build().listEntityVO(btmList);
   }
}