From 41c256180e5e7900c88b71a9f22590db21f87a7a Mon Sep 17 00:00:00 2001
From: lihang <lihang@vci-tech.com>
Date: 星期二, 09 五月 2023 16:54:36 +0800
Subject: [PATCH] 业务类型修正

---
 Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/BtmTypeServiceImpl.java |  184 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 183 insertions(+), 1 deletions(-)

diff --git a/Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/BtmTypeServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/BtmTypeServiceImpl.java
index 994199f..8c7215d 100644
--- a/Source/UBCS/ubcs-service/ubcs-omd/src/main/java/com/vci/ubcs/omd/service/impl/BtmTypeServiceImpl.java
+++ b/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;
 	}
+
+	/**
+	 * 鎸塪omain鍒嗙粍锛屾煡璇笟鍔$被鍨嬪睘鎬х粨鏋�
+	 *
+	 * @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);
+	}
 }

--
Gitblit v1.9.3