From 3579af2945dd38d841a23cd340acd474bb63773a Mon Sep 17 00:00:00 2001
From: lihang <lihang@vci-tech.com>
Date: 星期四, 04 五月 2023 18:02:01 +0800
Subject: [PATCH] 业务类型与DDL服务相关的添加

---
 Source/UBCS/ubcs-service/ubcs-ddl/src/main/java/com/vci/ubcs/ddl/service/impl/DdlServiceImpl.java |  596 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 523 insertions(+), 73 deletions(-)

diff --git a/Source/UBCS/ubcs-service/ubcs-ddl/src/main/java/com/vci/ubcs/ddl/service/impl/DdlServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-ddl/src/main/java/com/vci/ubcs/ddl/service/impl/DdlServiceImpl.java
index 561ae46..85e886c 100644
--- a/Source/UBCS/ubcs-service/ubcs-ddl/src/main/java/com/vci/ubcs/ddl/service/impl/DdlServiceImpl.java
+++ b/Source/UBCS/ubcs-service/ubcs-ddl/src/main/java/com/vci/ubcs/ddl/service/impl/DdlServiceImpl.java
@@ -1,14 +1,12 @@
 package com.vci.ubcs.ddl.service.impl;
 
-<<<<<<< HEAD
-import com.alibaba.nacos.client.naming.NacosNamingService;
 
-=======
-import com.vci.starter.web.util.VciBaseUtil;
->>>>>>> dd3a6c681f938fd53d5f6cc3a4fcee40e71b259c
+import com.alibaba.nacos.shaded.com.google.protobuf.ServiceException;
 import com.vci.starter.word.bo.WordMergeStartTableDataBO;
 import com.vci.ubcs.ddl.bo.DdlTableBO;
 import com.vci.ubcs.ddl.bo.DdlTableInDataBaseBO;
+import com.vci.ubcs.ddl.enums.BusinessTypeEnum;
+import com.vci.ubcs.ddl.enums.ModifyTableTaskEnum;
 import com.vci.ubcs.ddl.processor.ddl.DdlMapperProcessStrategy;
 import com.vci.ubcs.ddl.processor.ddl.DdlMapperProcessor;
 import com.vci.ubcs.ddl.processor.dll.DllMapperProcessor;
@@ -17,15 +15,17 @@
 import com.vci.ubcs.ddl.properties.DdlPropertise;
 import com.vci.ubcs.ddl.service.IDdlService;
 import com.vci.ubcs.omd.cache.BtmTypeCache;
-import com.vci.ubcs.omd.dto.BtmTypeDTO;
-import com.vci.ubcs.omd.entity.ModifyAttributeInfoDO;
-import com.vci.ubcs.omd.vo.BtmTypeAttributeVO;
-import com.vci.ubcs.omd.vo.BtmTypeVO;
-import com.vci.ubcs.omd.vo.LinkTypeAttributeVO;
-import com.vci.ubcs.omd.vo.LinkTypeVO;
+import com.vci.ubcs.omd.constant.BtmTypeFieldConstant;
+import com.vci.ubcs.omd.constant.BtmTypeLcStatusConstant;
+import com.vci.ubcs.omd.dto.BtmAndLinkTypeDdlDTO;
+import com.vci.ubcs.omd.entity.ModifyAttributeInfo;
+import com.vci.ubcs.omd.vo.*;
 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 org.apache.commons.lang3.StringUtils;
+import org.springblade.core.secure.utils.AuthUtil;
 import org.springblade.core.tool.api.R;
 import org.springblade.core.tool.utils.BeanUtil;
 import org.springblade.core.tool.utils.Func;
@@ -34,7 +34,9 @@
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
+import javax.validation.constraints.NotNull;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.stream.Collectors;
 
 /**
@@ -63,15 +65,61 @@
 	 */
 	private final DdlMapperProcessor ddlMapper = DdlMapperProcessStrategy.getProcessor();
 
+
 	/**
 	 * dll鏁版嵁鎿嶄綔鏈嶅姟
 	 */
 	private final DllMapperProcessor dllMapper = DllMapperProcessorStrategy.getProcessor();
 
-	/**
-	 * 琛ㄥ悕鍓嶇紑
-	 */
-	private final String TABLE_PREFIX = "PL";
+	private static final String YES = "Y";
+
+	private static final String NO = "N";
+
+	private final Map<String, BtmTypeVO> idBtmMap = new ConcurrentHashMap<>();
+
+	private final Map<String, LinkTypeVO> idLinkMap = new ConcurrentHashMap<>();
+
+	private void putBtm(@NotNull BtmTypeVO... voList) throws ServiceException {
+		String collect = Arrays.stream(voList).map(BtmTypeVO::getId).filter(idBtmMap::containsKey).collect(Collectors.joining(","));
+		if (StringUtils.isBlank(collect)) {
+			Arrays.stream(voList).forEach(vo -> {
+				idBtmMap.put(vo.getId(), vo);
+			});
+		} else {
+			throw new ServiceException("涓氬姟绫诲瀷:[" + collect + "]姝e湪琚叾浠栫敤鎴锋搷浣�");
+		}
+	}
+
+	private void putLink(@NotNull LinkTypeVO... voList) throws ServiceException {
+		String collect = Arrays.stream(voList).map(LinkTypeVO::getId).filter(idLinkMap::containsKey).collect(Collectors.joining(","));
+		if (StringUtils.isBlank(collect)) {
+			Arrays.stream(voList).forEach(vo -> {
+				idLinkMap.put(vo.getId(), vo);
+			});
+		} else {
+			throw new ServiceException("閾炬帴绫诲瀷:[" + collect + "]姝e湪琚叾浠栫敤鎴锋搷浣�");
+		}
+	}
+
+	private void removeBtm(@NotNull BtmTypeVO... btmTypeVO) throws ServiceException {
+		if (Arrays.stream(btmTypeVO).allMatch(s -> idBtmMap.containsKey(s.getId()))) {
+			for (BtmTypeVO vo : btmTypeVO) {
+				idBtmMap.remove(vo.getId());
+			}
+		} else {
+			throw new ServiceException("瑙i攣瀵硅薄涓嶅瓨鍦�");
+		}
+	}
+
+	private void removeLink(@NotNull LinkTypeVO... linkTypeVO) throws ServiceException {
+		if (Arrays.stream(linkTypeVO).allMatch(s -> idLinkMap.containsKey(s.getId()))) {
+			for (LinkTypeVO vo : linkTypeVO) {
+				idLinkMap.remove(vo.getId());
+			}
+		} else {
+			throw new ServiceException("瑙i攣瀵硅薄涓嶅瓨鍦�");
+		}
+	}
 
 	/**
 	 * 鏍¢獙涓氬姟绫诲瀷鐨勬暟鎹簱琛ㄤ腑鏄惁鏈夋暟鎹�
@@ -165,7 +213,7 @@
 	 */
 	@Override
 	public void createDbTablesByOidCollection(Collection<String> oidCollection) throws VciBaseException {
-		VciBaseUtil.alertCollectionNotNull("涓氬姟绫诲瀷/閾炬帴绫诲瀷鐨勪富閿泦鍚�", oidCollection);
+		VciBaseUtil.alertNotNull("涓氬姟绫诲瀷/閾炬帴绫诲瀷鐨勪富閿泦鍚�", oidCollection);
 		List<BtmTypeVO> btmTypeVOList = BtmTypeCache.listBtmTypeByOidCollection(oidCollection);
 //		List<OsLinkTypeVO> linkTypeVOList = linkTypeService.listLinkTypeByOidCollection(oidCollection);
 		if (!CollectionUtils.isEmpty(btmTypeVOList)) {
@@ -194,16 +242,33 @@
 		} else {
 			String tableName = btmTypeVO.getTableName();
 			if (!checkTableExistByTableName(tableName)) {
-				String attributeSql = dllMapper.getCreateSqlByAttributeForBtm(btmTypeVO.getAttributes());
+				String attributeSql = getCreateSqlByAttributeForBtm(btmTypeVO.getAttributes());
 				dllMapper.createTableBySql(tableName, attributeSql);
 				if (StringUtils.isNotBlank(btmTypeVO.getName())) {
 					dllMapper.commentTable(tableName, btmTypeVO.getName());
 				}
-				btmTypeVO.getAttributes().stream().forEach(s -> {
+				btmTypeVO.getAttributes().forEach(s -> {
 					dllMapper.commentColumnTable(tableName, s.getId(), s.getName());
 				});
 			}
 		}
+	}
+
+	/**
+	 * 鑾峰彇鍒涘缓鐨剆ql璇彞涓睘鎬ч儴鍒�
+	 *
+	 * @param attributeVOList 灞炴�х殑绔嬬
+	 * @return sql璇彞
+	 */
+	@Override
+	public String getCreateSqlByAttributeForBtm(List<BtmTypeAttributeVO> attributeVOList) {
+		StringBuilder sb = new StringBuilder();
+		attributeVOList.forEach(a -> {
+			sb.append(a.getId()).append(StringPool.SPACE);
+			VciFieldTypeEnum fieldTypeEnum = VciFieldTypeEnum.forValue(a.getAttrDataType());
+			sb.append(dllMapper.getColumnTypeSql(fieldTypeEnum, a));
+		});
+		return sb.substring(0, sb.lastIndexOf(","));
 	}
 
 	/**
@@ -238,7 +303,7 @@
 	 */
 	@Override
 	public void changeColumnForBtm(List<BtmTypeAttributeVO> modifyLengthAttrVOList) throws VciBaseException {
-		VciBaseUtil.alertCollectionNotNull("瑕佷慨鏀归暱搴︾殑灞炴�ч泦", modifyLengthAttrVOList);
+		VciBaseUtil.alertNotNull("瑕佷慨鏀归暱搴︾殑灞炴�ч泦", modifyLengthAttrVOList);
 		Map<String, List<BtmTypeAttributeVO>> btmTypeHasAttributeVOMap = modifyLengthAttrVOList.stream().collect(Collectors.groupingBy(BtmTypeAttributeVO::getPkBtmType));
 		btmTypeHasAttributeVOMap.forEach((k, v) -> {
 			BtmTypeVO btmTypeVO = BtmTypeCache.getDetail(k);
@@ -265,7 +330,7 @@
 	 * @param attributeVOList 瑕佹坊鍔犵殑灞炴�у瓧娈�
 	 */
 	private void changeColumnsForTable(String tableName, List<BtmTypeAttributeVO> attributeVOList) {
-		String attributeSql = dllMapper.getCreateSqlByAttributeForBtm(attributeVOList);
+		String attributeSql = getCreateSqlByAttributeForBtm(attributeVOList);
 		//鍏堝垽鏂〃鏍兼槸鍚﹀瓨鍦�
 		boolean tableExist = false;
 		try {
@@ -289,7 +354,7 @@
 	 */
 	private void commentColumnsForTable(String tableName, List<BtmTypeAttributeVO> attributeVOList) {
 		if (StringUtils.isNotBlank(tableName) && !CollectionUtils.isEmpty(attributeVOList)) {
-			attributeVOList.stream().forEach(s -> {
+			attributeVOList.forEach(s -> {
 				ddlMapper.commentColumnTable(tableName, s.getId(), s.getName());
 			});
 		}
@@ -314,9 +379,9 @@
 	 */
 	@Override
 	public void addColumn2TableForBtm(List<BtmTypeAttributeVO> addAttrDOList) throws VciBaseException {
-		VciBaseUtil.alertCollectionNotNull("瑕佹坊鍔犲埌鏁版嵁搴撹〃涓殑灞炴�ч泦", addAttrDOList);
-		Map<String, List<BtmTypeAttributeVO>> btmTypeHasAttributeVOMap = addAttrDOList.stream().collect(Collectors.groupingBy(BtmTypeAttributeVO::getPkBtmType));
-		btmTypeHasAttributeVOMap.forEach((k, v) -> {
+		VciBaseUtil.alertNotNull("瑕佹坊鍔犲埌鏁版嵁搴撹〃涓殑灞炴�ч泦", addAttrDOList);
+		Map<String, List<BtmTypeAttributeVO>> btmTypeHasAttributeMap = addAttrDOList.stream().collect(Collectors.groupingBy(BtmTypeAttributeVO::getPkBtmType));
+		btmTypeHasAttributeMap.forEach((k, v) -> {
 			BtmTypeVO btmTypeVO = BtmTypeCache.getDetail(k);
 			if (btmTypeVO == null || StringUtils.isBlank(btmTypeVO.getOid())) {
 				throw new VciBaseException("瑕佷慨鏀瑰睘鎬у垪闀垮害鐨勪笟鍔$被鍨嬩笉瀛樺湪");
@@ -334,12 +399,8 @@
 	 * @param attributeVOList 灞炴�х殑鏄剧ず瀵硅薄
 	 * @throws VciBaseException 鎵ц鎴栬�呰幏鍙杝ql璇彞鐨勬椂鍊欏嚭鐜伴敊璇細鎶涘嚭寮傚父
 	 */
-<<<<<<< HEAD
-	private void addColumnForTable(String tableName, List<OmdBtmTypeAttributeVO> attributeVOList) throws VciBaseException{
-=======
 	private void addColumnForTable(String tableName, List<BtmTypeAttributeVO> attributeVOList) {
->>>>>>> dd3a6c681f938fd53d5f6cc3a4fcee40e71b259c
-		String attributeSql = dllMapper.getCreateSqlByAttributeForBtm(attributeVOList);
+		String attributeSql = getCreateSqlByAttributeForBtm(attributeVOList);
 		//鍏堝垽鏂〃鏍兼槸鍚﹀瓨鍦�
 		if (!checkTableExistByTableName(tableName)) {
 			BtmTypeAttributeVO attributeVO = attributeVOList.get(0);
@@ -358,7 +419,11 @@
 	 */
 	@Override
 	public void addColumn2TableForLink(List<LinkTypeAttributeVO> addAttrDOListForLinkType) throws VciBaseException {
+		VciBaseUtil.alertNotNull("瑕佹坊鍔犲埌鏁版嵁搴撹〃涓睘鎬ч泦",addAttrDOListForLinkType);
+		Map<String, List<LinkTypeAttributeVO>> linkTypeHasAttributeVOMap = addAttrDOListForLinkType.stream().collect(Collectors.groupingBy(LinkTypeAttributeVO::getPkLinkType));
+		linkTypeHasAttributeVOMap.forEach((k,v) -> {
 
+		});
 	}
 
 	/**
@@ -458,8 +523,281 @@
 	 * @return 涓嶅悓鐨勫湴鏂癸紝姣忎竴涓笟鍔$被鍨嬫垨鑰呴摼鎺ョ被鍨嬩竴鏉℃暟鎹�
 	 */
 	@Override
-	public List<ModifyAttributeInfoDO> checkDifferent(List<BtmTypeVO> btmTypeVOList, List<LinkTypeVO> linkTypeVOList) throws VciBaseException {
-		return null;
+	public List<ModifyAttributeInfo> checkDifferent(List<BtmTypeVO> btmTypeVOList, List<LinkTypeVO> linkTypeVOList) throws VciBaseException {
+		if (CollectionUtils.isEmpty(btmTypeVOList) && CollectionUtils.isEmpty(linkTypeVOList)) {
+			throw new VciBaseException("瑕佹鏌ョ殑涓氬姟绫诲瀷鍜岄摼鎺ョ被鍨嬩负绌�");
+		}
+		List<ModifyAttributeInfo> modifyAttributeInfoList = new ArrayList<>();
+		if (!CollectionUtils.isEmpty(btmTypeVOList)) {
+			modifyAttributeInfoList.addAll(checkBtmType(btmTypeVOList));
+		}
+		if (!CollectionUtils.isEmpty(linkTypeVOList)) {
+			modifyAttributeInfoList.addAll(checkLinkType(linkTypeVOList));
+		}
+		if (CollectionUtils.isEmpty(modifyAttributeInfoList)) {
+			reflexDifferent(modifyAttributeInfoList, btmTypeVOList, linkTypeVOList);
+		}
+		return modifyAttributeInfoList;
+	}
+
+	/**
+	 * 妫�鏌ヤ笟鍔$被鍨�
+	 *
+	 * @param btmTypeVOList 涓氬姟绫诲瀷
+	 * @return 妫�鏌ョ粨鏋�
+	 */
+	private List<ModifyAttributeInfo> checkBtmType(List<BtmTypeVO> btmTypeVOList) {
+		List<ModifyAttributeInfo> modifyAttributeInfoDOList = new ArrayList<>();
+		btmTypeVOList.stream().filter(s -> !isCompatibilityTable(s.getId(),null)).forEach(btm -> {
+			ModifyAttributeInfo modifyAttributeInfo = new ModifyAttributeInfo();
+			modifyAttributeInfo.setId(btm.getId());
+			modifyAttributeInfo.setName(btm.getName());
+			modifyAttributeInfo.setTableName(btm.getTableName());
+			modifyAttributeInfo.setBusinessType(BusinessTypeEnum.BTM.getValue());
+			// 1. 瑙嗗浘鍜屾柊寤虹殑锛岀洿鎺ュ垱寤猴紝璁板綍鍒涘缓鍘嗗彶
+			if (btm.isViewFlag() || !checkTableExistByTableName(btm.getTableName())){
+				modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CREATE.getValue());
+				modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue());
+			}else {
+				VciBaseUtil.alertNotNull(btm.getAttributes(),"涓氬姟绫诲瀷" + btm.getId() + "鐨勫睘鎬�");
+				List<DdlTableInDataBaseBO> columnDataInTable = selectTableColumnInfo(btm.getTableName());
+				if (CollectionUtils.isEmpty(columnDataInTable)){
+					modifyAttributeInfo.setAddAttributes(btm.getAttributes().stream().map(s -> s.getId().toLowerCase().trim()).collect(Collectors.joining(",")));
+					modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.EDIT.getValue());
+					modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue());
+				}else {
+					// 2. 瀛樺湪鐨勬墠鍒ゆ柇锛屽垎鏂板灞炴�с�佸睘鎬ф洿鏀广�佸睘鎬у垹闄ゅ垎鍒搷浣�
+					// 3. 灞炴�ф搷浣滀腑鐗瑰埆鍒ゆ柇[鏄惁鎺у埗鐗堟湰][鏄惁鎺у埗瀵嗙骇][鏄惁鎺у埗鐢熷懡鍛ㄦ湡]鏉ユ坊鍔犲搴斿睘鎬�
+					Map<String, DdlTableInDataBaseBO> tableColumMap = columnDataInTable.stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k));
+					Map<String, BtmTypeAttributeVO> attributeMap = btm.getAttributes().stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k));
+					List<String> addList = new ArrayList<>();
+					List<String> editList = new ArrayList<>();
+					List<String> deleteList = new ArrayList<>();
+					List<String> beforeList = new ArrayList<>();
+					attributeMap.forEach((k,v) -> {
+						String attributeLowId = k.toLowerCase();
+						if (!checkIsDefaultColumn(attributeLowId)) {
+							if (tableColumMap.containsKey(k)) {
+								//璇存槑浠ュ墠涔熸湁锛岄偅灏辩湅鏄惁闇�瑕佷慨鏀�
+								DdlTableInDataBaseBO columnInDB = tableColumMap.get(k);
+								if (columnInDB.getAttributeLength() == null) {
+									columnInDB.setAttributeLength(0);
+								}
+								boolean needEdit = false;
+								if (!v.getName().equalsIgnoreCase(columnInDB.getName())) {
+									//璇存槑闇�瑕佷慨鏀�
+									needEdit = true;
+								}
+								if (needEdit || checkNeedEditAttr(v, columnInDB)) {
+									editList.add(v.getId());
+									beforeList.add(columnInDB.getId() + StringPool.SPACE + columnInDB.getAttrDataType() + "(" + columnInDB.getAttributeLength() + ")" + StringPool.SPACE + columnInDB.getNullableFlag()
+										+ StringPool.SPACE + columnInDB.getPrecisionLength() + StringPool.SPACE + columnInDB.getScaleLength() + StringPool.SPACE + columnInDB.getName());
+								}
+							} else {
+								addList.add(v.getId());
+							}
+						} else {
+							//閽堝榛樿灞炴�э紝濡傛灉浠ュ墠娌℃湁锛岀幇鍦ㄦ湁鍙互娣诲姞锛屽鏋滀互鍓�
+							if (!tableColumMap.containsKey(k)) {
+								boolean needAddNewField = false;
+								if (!BtmTypeLcStatusConstant.EMTYPE_LIFE_CYCLE.equalsIgnoreCase(btm.getLifeCycleId()) && BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP.containsKey(attributeLowId)) {
+									needAddNewField = true;
+								}
+								if (StringUtils.isNotBlank(btm.getRevisionRuleId()) && BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP.containsKey(attributeLowId)) {
+									needAddNewField = true;
+								}
+								if (btm.isSecretFlag() && BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP.containsKey(attributeLowId)) {
+									needAddNewField = true;
+								}
+								if (needAddNewField) {
+									addList.add(v.getId());
+								}
+							}
+						}
+					});
+					tableColumMap.forEach((k, v) -> {
+						if (!attributeMap.containsKey(k)) {
+							deleteList.add(k);
+						}
+					});
+					modifyAttributeInfo.setOrderModifyUserName(AuthUtil.getUserAccount());
+					modifyAttributeInfo.setOrderModifyDate(new Date());
+					modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue());
+					modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue());
+					modifyAttributeInfo.setAddAttributes(String.join(",", addList));
+					modifyAttributeInfo.setModifyAttributes(String.join(",", editList));
+					modifyAttributeInfo.setDeleteAttributes(String.join(",", deleteList));
+					modifyAttributeInfo.setBeforeModifyAttributes(String.join(",", beforeList));
+				}
+			}
+		});
+		return modifyAttributeInfoDOList;
+	}
+
+	private boolean checkIsDefaultColumn(String attributeLowId) {
+		return ((BtmTypeFieldConstant.BASIC_FIELD_MAP.containsKey(attributeLowId)
+			&& !VciBaseUtil.inArray(new String[]{"id", "name", "description"}, attributeLowId))
+			|| BtmTypeFieldConstant.LINK_TYPE_FIELD_MAP.containsKey(attributeLowId)
+			|| BtmTypeFieldConstant.LIFECYCLE_MANAGE_FIELD_MAP.containsKey(attributeLowId)
+			|| BtmTypeFieldConstant.REVISION_MANAGE_FIELD_MAP.containsKey(attributeLowId)
+			|| BtmTypeFieldConstant.SECRET_MANAGE_FIELD_MAP.containsKey(attributeLowId));
+	}
+
+	/**
+	 * 妫�鏌ラ摼鎺ョ被鍨�
+	 *
+	 * @param linkTypeVOList 閾炬帴绫诲瀷
+	 * @return 妫�鏌ョ粨鏋�
+	 */
+	private List<ModifyAttributeInfo> checkLinkType(List<LinkTypeVO> linkTypeVOList) {
+		List<ModifyAttributeInfo> modifyAttributeInfoDOList = new ArrayList<>();
+		linkTypeVOList.stream().filter(s -> !isCompatibilityTable(null,s.getId())).forEach(link -> {
+				ModifyAttributeInfo modifyAttributeInfo = new ModifyAttributeInfo();
+				modifyAttributeInfo.setId(link.getId());
+				modifyAttributeInfo.setName(link.getName());
+				modifyAttributeInfo.setTableName(link.getTableName());
+				modifyAttributeInfo.setBusinessType(BusinessTypeEnum.LINK.getValue());
+				// 1. 瑙嗗浘鍜屾柊寤虹殑锛岀洿鎺ュ垱寤猴紝璁板綍鍒涘缓鍘嗗彶
+				if (!checkTableExistByTableName(link.getTableName())){
+					modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CREATE.getValue());
+					modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue());
+				}else {
+					VciBaseUtil.alertNotNull(link.getAttributes(), "閾炬帴绫诲瀷" + link.getId() + "鐨勫睘鎬�");
+					List<DdlTableInDataBaseBO> columnDataInTable = selectTableColumnInfo(link.getTableName());
+					if (CollectionUtils.isEmpty(columnDataInTable)) {
+						modifyAttributeInfo.setAddAttributes(link.getAttributes().stream().map(s -> s.getId().toLowerCase().trim()).collect(Collectors.joining(",")));
+						modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.EDIT.getValue());
+						modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CREATE.getValue());
+					} else {
+						// 2. 瀛樺湪鐨勬墠鍒ゆ柇锛屽垎鏂板灞炴�с�佸睘鎬ф洿鏀广�佸睘鎬у垹闄ゅ垎鍒搷浣�
+						// 3. 灞炴�ф搷浣滀腑鐗瑰埆鍒ゆ柇[鏄惁鎺у埗鐗堟湰][鏄惁鎺у埗瀵嗙骇][鏄惁鎺у埗鐢熷懡鍛ㄦ湡]鏉ユ坊鍔犲搴斿睘鎬�
+						Map<String, DdlTableInDataBaseBO> tableColumMap = columnDataInTable.stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k));
+						Map<String, LinkTypeAttributeVO> attributeMap = link.getAttributes().stream().collect(Collectors.toMap(t -> t.getId().toLowerCase().trim(), k -> k));
+						List<String> addList = new ArrayList<>();
+						List<String> editList = new ArrayList<>();
+						List<String> deleteList = new ArrayList<>();
+						List<String> beforeList = new ArrayList<>();
+						attributeMap.forEach((k, v) -> {
+							String attributeLowId = k.toLowerCase();
+							if (!(BtmTypeFieldConstant.LINK_TYPE_FIELD_MAP.containsKey(attributeLowId))) {
+								if (tableColumMap.containsKey(k)) {
+									//璇存槑浠ュ墠涔熸湁锛岄偅灏辩湅鏄惁闇�瑕佷慨鏀�
+									DdlTableInDataBaseBO columnInDB = tableColumMap.get(k);
+									if (columnInDB.getAttributeLength() == null) {
+										columnInDB.setAttributeLength(0);
+									}
+									boolean needEdit = false;
+									if (!v.getName().equalsIgnoreCase(columnInDB.getName())) {
+										//璇存槑闇�瑕佷慨鏀�
+										needEdit = true;
+									}
+									BtmTypeAttributeVO btmTypeAttributeVO = new BtmTypeAttributeVO();
+									BeanUtil.copy(v, btmTypeAttributeVO);
+									if (needEdit || checkNeedEditAttr(btmTypeAttributeVO, columnInDB)) {
+										editList.add(v.getId());
+										beforeList.add(columnInDB.getId() + StringPool.SPACE + columnInDB.getAttrDataType() + "(" + columnInDB.getAttributeLength() + ")" + StringPool.SPACE + columnInDB.getNullableFlag()
+											+ StringPool.SPACE + columnInDB.getPrecisionLength() + StringPool.SPACE + columnInDB.getScaleLength() + StringPool.SPACE + columnInDB.getName());
+									}
+								} else {
+									addList.add(v.getId());
+								}
+							} else {
+								//閽堝榛樿灞炴�э紝濡傛灉浠ュ墠娌℃湁锛岀幇鍦ㄦ湁鍙互娣诲姞锛屽鏋滀互鍓�
+								if (!tableColumMap.containsKey(k)) {
+									boolean needAddNewField = false;
+									if (BtmTypeFieldConstant.LINK_TYPE_FIELD_MAP.containsKey(attributeLowId)) {
+										//鏄摼鎺ョ被鍨嬬殑灞炴�э紝浠ュ墠娌℃湁灏辨坊鍔犱笂
+										needAddNewField = true;
+									}
+									if (needAddNewField) {
+										addList.add(v.getId());
+									}
+								}
+							}
+						});
+						tableColumMap.forEach((k, v) -> {
+							if (!attributeMap.containsKey(k)) {
+								deleteList.add(k);
+							}
+						});
+						modifyAttributeInfo.setOrderModifyUserName(AuthUtil.getUserAccount());
+						modifyAttributeInfo.setOrderModifyDate(new Date());
+						modifyAttributeInfo.setModifyColumnReason(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue());
+						modifyAttributeInfo.setTaskName(ModifyTableTaskEnum.CONSISTENCY_CHECK.getValue());
+						modifyAttributeInfo.setAddAttributes(String.join(",", addList));
+						modifyAttributeInfo.setModifyAttributes(String.join(",", editList));
+						modifyAttributeInfo.setDeleteAttributes(String.join(",", deleteList));
+						modifyAttributeInfo.setBeforeModifyAttributes(String.join(",", beforeList));
+					}
+
+				}
+		});
+		return modifyAttributeInfoDOList;
+	}
+
+	/**
+	 * 鍒ゆ柇鏄惁闇�瑕佷慨鏀瑰睘鎬�
+	 *
+	 * @param btmTypeAttributeVO 灞炴�х殑鏄剧ず瀵硅薄
+	 * @param tableInDB          鏁版嵁搴撹〃閲岀殑瀵硅薄
+	 * @return true琛ㄧず闇�瑕佷慨鏀癸紝false琛ㄧず涓嶄慨鏀�
+	 */
+	private boolean checkNeedEditAttr(BtmTypeAttributeVO btmTypeAttributeVO, DdlTableInDataBaseBO tableInDB) {
+		if (tableInDB.getAttributeLength() == null) {
+			tableInDB.setAttributeLength(0);
+		}
+		if (tableInDB.getPrecisionLength() == null) {
+			tableInDB.setPrecisionLength(20);
+		}
+		if (tableInDB.getScaleLength() == null) {
+			tableInDB.setScaleLength(0);
+		}
+		String nullInDb = "";
+		if (YES.equalsIgnoreCase(tableInDB.getNullableFlag())) {
+			nullInDb = BooleanEnum.TRUE.getValue();
+		} else if (NO.equalsIgnoreCase(tableInDB.getNullableFlag())) {
+			nullInDb = BooleanEnum.FASLE.getValue();
+		}
+		if (!(String.valueOf(btmTypeAttributeVO.isNullableFlag()).equalsIgnoreCase(nullInDb))) {
+			return true;
+		}
+		if (StringUtils.isNotBlank(btmTypeAttributeVO.getDefaultValue())) {
+			return true;
+		}
+
+
+		List<VciFieldTypeEnum> fieldTypes = dllMapper.listFieldByColumnStr(tableInDB.getAttrDataType());
+		if (CollectionUtils.isEmpty(fieldTypes)) {
+			throw new VciBaseException("娌℃湁鎵惧埌瀵瑰簲鐨勫瓧娈电被鍨�");
+		}
+		Set<String> names = fieldTypes.stream().map(Enum::name).collect(Collectors.toSet());
+		String attributeDataType = btmTypeAttributeVO.getAttrDataType();
+		if (names.size() > 1) {
+			return !names.contains(attributeDataType);
+		} else {
+			VciFieldTypeEnum fieldType = fieldTypes.get(0);
+			if (attributeDataType.equalsIgnoreCase(fieldType.name())) {
+				switch (fieldType) {
+					case VTString:
+						if (btmTypeAttributeVO.getAttributeLength().intValue() != tableInDB.getAttributeLength().intValue()) {
+							return true;
+						}
+						break;
+					case VTDouble:
+						//涓嶈秴杩�128
+						if (btmTypeAttributeVO.getPrecisionLength().intValue() != tableInDB.getPrecisionLength().intValue()) {
+							return true;
+						}
+						if (btmTypeAttributeVO.getScaleLength().intValue() != tableInDB.getScaleLength().intValue()) {
+							return true;
+						}
+						break;
+					default:
+						break;
+				}
+			}
+			return false;
+		}
 	}
 
 	/**
@@ -469,8 +807,138 @@
 	 * @throws VciBaseException 鍙傛暟涓虹┖鎴栬�呮墽琛屽嚭閿欑殑鏃跺�欎細鎶涘嚭寮傚父
 	 */
 	@Override
-	public void reflexDifferent(List<ModifyAttributeInfoDO> differentAttributeList) throws VciBaseException {
+	public void reflexDifferent(List<ModifyAttributeInfo> differentAttributeList, List<BtmTypeVO> btmTypeVOList, List<LinkTypeVO> linkTypeVOList) throws VciBaseException {
+		VciBaseUtil.alertNotNull(differentAttributeList, "瑕佹竻鐞嗙殑鏁版嵁搴撶殑淇℃伅涓虹┖");
+		Map<String, BtmTypeVO> idBtmTypeMap = btmTypeVOList.stream().collect(Collectors.toMap(BtmTypeVO::getId, t -> t, (o1, o2) -> o1));
+		Map<String, LinkTypeVO> idLinkTypeMap = linkTypeVOList.stream().collect(Collectors.toMap(LinkTypeVO::getId, t -> t, (o1, o2) -> o1));
+		differentAttributeList.forEach(s -> {
+//			BtmTypeVO btmType = idBtmTypeMap.get(id);
+			if (ModifyTableTaskEnum.CREATE.getValue().equalsIgnoreCase(s.getTaskName())) {
+				//鐩存帴鍒涘缓琛ㄦ牸
+//				createDbTableForBtm(btmType);
+				createDbTablesById(s.getId());
+			} else {
+				String addAttributes = s.getAddAttributes();
+				if (StringUtils.isNotBlank(s.getModifyAttributes()) || StringUtils.isNotBlank(s.getDeleteAttributes())) {
+					//闇�瑕佸幓鍒ゆ柇鏄惁鍙互鏇存敼
+					if (checkTableHasDataByTableName(s.getTableName())) {
+						//鏈夋暟鎹殑璇濓紝寰楅渶瑕佸垽鏂睘鎬ф槸鍚﹀彲浠ヤ慨鏀�
+						if (StringUtils.isNotBlank(s.getDeleteAttributes())) {
+							s.setHandleResult("琛ㄦ牸瀛樺湪鏁版嵁锛屼笉鍏佽鍒犻櫎瀛楁");
+							return;
+						}
+						List<DdlTableInDataBaseBO> columnInDbList = selectTableColumnInfo(s.getTableName());
+						Map<String, DdlTableInDataBaseBO> columnInDbMap = new HashMap<>();
+						if (!CollectionUtils.isEmpty(columnInDbList)) {
+							columnInDbMap = columnInDbList.stream().collect(Collectors.toMap(t -> t.getId().toLowerCase(), k -> k));
+						}
+						List<BtmTypeAttributeVO> attributeVOList = idBtmTypeMap.getOrDefault(s.getId(), new BtmTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getAddAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId().toLowerCase(), id))).collect(Collectors.toList());
+						//鏈夊彲鑳戒箣鍓嶅垽鏂殑鏃跺�欐暟鎹簱瀛樺湪锛岃�岀幇鍦ㄤ笉瀛樺湪浜�
+						List<String> unExitInDbIdList = new ArrayList<>();
+						Map<String, DdlTableInDataBaseBO> finalColumnInDbMap = columnInDbMap;
+						List<BtmTypeAttributeVO> needEditAttributeVOList = new ArrayList<>();
+						if (!CollectionUtils.isEmpty(attributeVOList)) {
+							attributeVOList.forEach(t -> {
+								if (finalColumnInDbMap.containsKey(t.getId().toLowerCase().trim())) {
+									needEditAttributeVOList.add(t);
+								} else {
+									unExitInDbIdList.add(t.getId());
+								}
+							});
+						}
+						if (!CollectionUtils.isEmpty(needEditAttributeVOList)) {
+							List<BtmTypeAttributeVO> unModifyAttrVOList = needEditAttributeVOList.stream().filter(t -> !checkNotCanEditAttr(t.getAttrDataType(), finalColumnInDbMap.get(t.getId().trim().toLowerCase()))).collect(Collectors.toList());
+							if (!CollectionUtils.isEmpty(unModifyAttrVOList)) {
+								s.setHandleResult("琛ㄦ牸瀛樺湪鏁版嵁锛�" + unModifyAttrVOList.stream().map(BtmTypeAttributeVO::getId).collect(Collectors.joining(",")) + "杩欎簺瀛楁涓嶈兘淇敼");
+								return;
+							}
+							needEditAttributeVOList.stream().forEach(t -> {
+								String attributeLowerId = t.getId().trim().toLowerCase();
+								DdlTableInDataBaseBO columInDb = finalColumnInDbMap.get(attributeLowerId);
+								if (!t.getName().equalsIgnoreCase(columInDb.getName())) {
+									ddlMapper.commentColumnTable(s.getTableName(), t.getId(), t.getName());
+								}
+								if (columInDb.getAttributeLength() != null && t.getAttributeLength() != null
+									&& t.getAttributeLength() < columInDb.getAttributeLength()) {
+									t.setAttributeLength(columInDb.getAttributeLength());
+								}
+								List<BtmTypeAttributeVO> attributeVOS = new ArrayList<>();
 
+								boolean nowNeedNull = false;
+								if (!YES.equalsIgnoreCase(columInDb.getNullableFlag()) && !t.isNullableFlag()) {
+									//璇存槑浠ュ墠鏄笉鑳戒负绌虹殑,浣嗘槸浠ュ墠涔熸槸涓嶄负绌虹殑锛屼慨鏀逛細鎶ラ敊锛屾墍浠ヨ繖涓皟鏁翠竴涓�
+									t.setNullableFlag(true);
+								} else if (!YES.equalsIgnoreCase(columInDb.getNullableFlag()) && t.isNullableFlag()) {
+									//浠ュ墠涓嶄负绌猴紝鐜板湪涓虹┖
+									nowNeedNull = true;
+								}
+								attributeVOS.add(t);
+								String attributeSql = getCreateSqlByAttributeForBtm(attributeVOS);
+								if (nowNeedNull) {
+									attributeSql += StringPool.SPACE + "null" + StringPool.SPACE;
+								}
+								ddlMapper.modifyTableBySql(s.getTableName(), attributeSql);
+							});
+						}
+						if (!CollectionUtils.isEmpty(unExitInDbIdList)) {
+							if (StringUtils.isBlank(addAttributes)) {
+								addAttributes = String.join(",", unExitInDbIdList);
+							} else {
+								addAttributes += "," + String.join(",", unExitInDbIdList);
+								;
+							}
+						}
+					} else {
+						//璇存槑娌℃湁鏁版嵁锛岄偅鍙互鎶婁慨鏀圭殑鍒楀拰鍒犻櫎鐨勫垪閮藉垹闄わ紝鐒跺悗閲嶆柊娣诲姞
+						if (StringUtils.isNotBlank(s.getModifyAttributes())) {
+							List<BtmTypeAttributeVO> attributeVOList = idBtmTypeMap.getOrDefault(s.getId(), new BtmTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getModifyAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId(), id))).collect(Collectors.toList());
+							if (!CollectionUtils.isEmpty(attributeVOList)) {
+								attributeVOList.forEach(t -> {
+									List<BtmTypeAttributeVO> attributeVOS = new ArrayList<>();
+									attributeVOS.add(t);
+									dropColumnByName(s.getTableName(), t.getId());
+									addColumnForTable(s.getTableName(), attributeVOS);
+									ddlMapper.commentColumnTable(s.getTableName(), t.getId(), t.getName());
+								});
+							}
+						}
+						//闇�瑕佸垹闄ょ殑鍒�
+						if (StringUtils.isNotBlank(s.getDeleteAttributes())) {
+							List<String> attributeIdList = VciBaseUtil.str2List(s.getDeleteAttributes());
+							Optional.ofNullable(attributeIdList).orElseGet(ArrayList::new).forEach(t -> {
+								dropColumnByName(s.getTableName(), t);
+							});
+						}
+					}
+				}
+				if (StringUtils.isNotBlank(addAttributes)) {
+					//鍙槸娣诲姞灞炴�у��
+					if (BusinessTypeEnum.LINK.getValue().equalsIgnoreCase(s.getBusinessType())) {
+						List<LinkTypeAttributeVO> attributeVOList = idLinkTypeMap.getOrDefault(s.getId(), new LinkTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getAddAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId(), id))).collect(Collectors.toList());
+						//鏄摼鎺ョ被鍨�
+//						List<LinkTypeAttributeVO> attributeVOList = linkTypeService.listAttributeVOByIdsForLink(s.getId(), VciBaseUtil.str2List(s.getAddAttributes()));
+						addColumn2TableForLink(attributeVOList);
+					} else {
+						List<BtmTypeAttributeVO> attributeVOList = idBtmTypeMap.getOrDefault(s.getId(), new BtmTypeVO()).getAttributes().stream().filter(vo -> Func.toStrList(",", s.getAddAttributes()).stream().anyMatch(id -> StringUtils.equals(vo.getId(), id))).collect(Collectors.toList());
+						addColumn2TableForBtm(attributeVOList);
+					}
+				}
+			}
+		});
+	}
+
+	private boolean checkNotCanEditAttr(String attributeDataType, DdlTableInDataBaseBO tableInDB) {
+		if (tableInDB != null && StringUtils.isNotBlank(attributeDataType)) {
+			List<VciFieldTypeEnum> fieldTypeEnums = dllMapper.listFieldByColumnStr(tableInDB.getAttrDataType());
+			Set<String> names = fieldTypeEnums.stream().map(Enum::name).collect(Collectors.toSet());
+			if (names.contains(attributeDataType)) {
+				return true;
+			}
+			return true;
+		} else {
+			//鍥犱负鏁版嵁搴撻噷娌℃湁杩欎釜琛ㄦ牸鏃�
+			return true;
+		}
 	}
 
 	/**
@@ -492,7 +960,7 @@
 	 */
 	@Override
 	public List<DdlTableBO> listTableInfoInDB(Collection<String> tableNamesCollections) throws VciBaseException {
-		VciBaseUtil.alertCollectionNotNull("鏁版嵁搴撹〃鍚嶇О涓嶈兘涓虹┖", tableNamesCollections);
+		VciBaseUtil.alertNotNull("鏁版嵁搴撹〃鍚嶇О涓嶈兘涓虹┖", tableNamesCollections);
 		List<DdlTableBO> tableBOList = new ArrayList<>();
 		tableNamesCollections.stream().forEach(s -> {
 			tableBOList.add(ddlMapper.selectTableComment(s));
@@ -561,49 +1029,30 @@
 	/**
 	 * 涓氬姟绫诲瀷鏁版嵁搴撴柊澧炴垨淇敼琛�
 	 *
-	 * @param dto 涓氬姟绫诲瀷浼犺緭瀵硅薄
+	 * @param ddlDTO 涓氬姟绫诲瀷閾炬帴绫诲瀷浼犺緭瀵硅薄
 	 * @return 鎵ц缁撴灉
 	 */
 	@Override
-	public R submitBtmType(BtmTypeDTO dto) {
-		VciBaseUtil.alertNotNull(dto, "涓氬姟绫诲瀷", dto.getAttributesDTOList(), "涓氬姟绫诲瀷灞炴��");
-		BtmTypeVO btmTypeVO = Objects.requireNonNull(BeanUtil.copy(dto, BtmTypeVO.class));
-		if (Func.isNotBlank(dto.getTableName())) {
-			checkModifyOrCreateTable(btmTypeVO);
-		} else {
-			if (checkTableExistById(dto.getId())) {
-				String tableName = getTableNameByBtmTypeId(dto.getId());
-				btmTypeVO.setTableName(tableName);
-				checkModifyOrCreateTable(btmTypeVO);
-			} else {
-				String tableName = TABLE_PREFIX + StringPool.UNDERSCORE + dto.getDomain().toUpperCase(Locale.ROOT) + StringPool.UNDERSCORE + dto.getId().toUpperCase(Locale.ROOT);
-				btmTypeVO.setTableName(tableName);
-				createDbTableForBtm(btmTypeVO);
+	public R<List<ModifyAttributeInfo>> submit(BtmAndLinkTypeDdlDTO ddlDTO) {
+		try {
+			List<ModifyAttributeInfo> changedList = new ArrayList<>();
+			if (!CollectionUtils.isEmpty(ddlDTO.getBtmTypeList())) {
+				List<BtmTypeVO> btmTypeList = ddlDTO.getBtmTypeList();
+				putBtm(btmTypeList.toArray(new BtmTypeVO[0]));
+				changedList.addAll(checkDifferent(btmTypeList, null));
+				removeBtm(btmTypeList.toArray(new BtmTypeVO[0]));
 			}
-		}
-		return R.success("000000");
-	}
-
-	/**
-	 * 妫�鏌ヤ笟鍔$被鍨嬬殑琛ㄦ槸鍚﹀瓨鍦紝濡傛灉瀛樺湪鍒欐瘮杈冨樊鍒繘琛屼慨鏀癸紝涓嶅瓨鍦ㄥ垯鍒涘缓
-	 *
-	 * @param btmTypeVO 涓氬姟绫诲瀷
-	 */
-	private void checkModifyOrCreateTable(BtmTypeVO btmTypeVO) {
-		if (checkTableExistByTableName(btmTypeVO.getTableName())) {
-			// 琛ㄥ凡瀛樺湪锛屾牎楠屽睘鎬�
-			List<BtmTypeVO> vos = new ArrayList<>();
-			vos.add(btmTypeVO);
-			List<ModifyAttributeInfoDO> differentAttributeList = checkDifferent(vos, null);
-			if (CollectionUtils.isEmpty(differentAttributeList)) {
-				// 鍙互鐩存帴鍒犻櫎鍐嶅垱寤�
-				dropTableByName(btmTypeVO.getTableName());
-				createDbTableForBtm(btmTypeVO);
-			} else {
-				// TODO: 淇敼鐨勬殏鏃惰繕娌″仛
+			if (!CollectionUtils.isEmpty(ddlDTO.getLinkTypeList())) {
+				List<LinkTypeVO> linkTypeList = ddlDTO.getLinkTypeList();
+				putLink(linkTypeList.toArray(new LinkTypeVO[0]));
+				changedList.addAll(checkDifferent(null,linkTypeList));
+				removeLink(linkTypeList.toArray(new LinkTypeVO[0]));
 			}
-		} else {
-			createDbTableForBtm(btmTypeVO);
+			R<List<ModifyAttributeInfo>> result = R.success("鏁版嵁搴撴搷浣滄垚鍔�");
+			result.setData(changedList);
+			return result;
+		} catch (ServiceException e) {
+			throw new RuntimeException(e.getMessage());
 		}
 	}
 
@@ -615,7 +1064,7 @@
 	 */
 	@Override
 	public DdlTableBO getTableColumnByTableName(String tableName) {
-		VciBaseUtil.alertNotNull(tableName,"琛ㄥ悕");
+		VciBaseUtil.alertNotNull(tableName, "琛ㄥ悕");
 		DdlTableBO tableBO = getTableInfoByTableName(tableName);
 		List<DdlTableInDataBaseBO> columnInfo = selectTableColumnInfo(tableName);
 		tableBO.setColumns(columnInfo);
@@ -635,10 +1084,11 @@
 
 	/**
 	 * 琛ㄥ悕鑾峰彇琛ㄤ俊鎭�
+	 *
 	 * @param tableName 琛ㄥ悕
 	 * @return 琛ㄤ俊鎭�
 	 */
-	private DdlTableBO getTableInfoByTableName(String tableName){
+	private DdlTableBO getTableInfoByTableName(String tableName) {
 		VciBaseUtil.alertNotNull(tableName, "琛ㄦ牸鍚嶇О");
 		return ddlMapper.selectTableComment(tableName);
 	}

--
Gitblit v1.9.3