From 336eb77c1a07ca3e3ead4482b51f848fa16ab3b7 Mon Sep 17 00:00:00 2001
From: xiejun <xj@2023>
Date: 星期日, 12 十一月 2023 20:15:38 +0800
Subject: [PATCH] 自定义流水相关开发

---
 Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java |  138 ++++++++++++++++++++++++++++++++++------------
 1 files changed, 102 insertions(+), 36 deletions(-)

diff --git a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java
index 3385b48..886df4a 100644
--- a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java
+++ b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmEngineServiceImpl.java
@@ -66,9 +66,14 @@
 import org.springblade.core.tool.utils.Func;
 import org.springblade.core.tool.utils.StringPool;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cache.Cache;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.TransactionStatus;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.transaction.support.DefaultTransactionDefinition;
+import org.springframework.transaction.support.TransactionTemplate;
 import org.springframework.util.CollectionUtils;
 
 import javax.annotation.Resource;
@@ -81,7 +86,7 @@
 import java.time.ZoneId;
 import java.time.ZonedDateTime;
 import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.*;
 import java.util.stream.Collectors;
 
 import static com.vci.ubcs.code.constant.FrameWorkLangCodeConstant.DATA_OID_NOT_EXIST;
@@ -97,12 +102,17 @@
 @Service
 public class MdmEngineServiceImpl implements MdmEngineService {
 
+	/**
+	 * 鍗曟sql鐨勬渶澶氬鍏ユ暟閲�
+	 */
+	@Value("${batchadd.single_maxnum:500}")
+	private Integer MAX_IMPORT_NUM;
 
     /**
      * 妯℃澘鐨勬湇鍔�
      */
     @Resource
-    private CodeClstemplateServiceImpl templateService;
+    private ICodeClstemplateService templateService;
 
     /**
      * 鐢熸垚缂栫爜鐨勬湇鍔�
@@ -135,6 +145,11 @@
      */
     @Resource
     IEnumClient enumClient;
+	/**
+	 * 鐗堟湰瑙勫垯鏈嶅姟
+	 */
+	@Resource
+	private IRevisionRuleClient revisionRuleClient;
 
     /**
      * 鍏紡鐨勬湇鍔�
@@ -142,13 +157,11 @@
     @Autowired
     private FormulaServiceImpl formulaService;
 
-
     /**
      * 瀵筼md涓彁渚涚殑feign鎺ュ彛杩涜璋冪敤锛屼互鍙婂鐞嗙浉鍏抽�昏緫
      */
     @Autowired
     ICodeReferBtmTypeService codeReferBtmTypeService;
-
 
     /**
      * 鐩镐技椤规煡璇㈣鍒�
@@ -172,17 +185,12 @@
      */
     @Autowired
     private IBtmTypeClient btmTypeClient;
-    /**
-     * 鐗堟湰瑙勫垯鐨勬湇鍔�
-     */
-    @Resource
-    private IRevisionRuleClient revisionRuleClient;
-//
+
     /**
      * 閫氱敤鏌ヨ
      */
     @Resource
-    CommonsMapper commonsMapper;
+    private CommonsMapper commonsMapper;
 
     /**
      * 缂栫爜瑙勫垯鐨勬湇鍔�
@@ -382,6 +390,7 @@
         }
         codeAllCodeService.updateBatchById(codeCbos);
     }
+
 	/**
 	 * 鐢宠鍗曚竴缂栫爜
 	 *
@@ -390,7 +399,7 @@
 	 */
 	@Override
 	public String addSaveCode(CodeOrderDTO orderDTO) throws Exception {
-		return	addSaveCode(orderDTO,true);
+		return addSaveCode(orderDTO,true);
 	}
 
 	/***
@@ -403,6 +412,7 @@
 	public String addSaveCodeNotauthUser(CodeOrderDTO orderDTO, boolean authUser) throws Exception {
 		return	addSaveCode(orderDTO,authUser);
 	}
+
     /**
      * 鐢宠鍗曚竴缂栫爜
      *
@@ -431,11 +441,11 @@
         checkEnumOnOrder(templateVO, orderDTO);
         //8.澶勭悊鏃堕棿鏍煎紡锛屽湪鏁版嵁搴撻噷闈笉璁烘槸瀛楃涓茶繕鏄棩鏈熸牸寮忥紝閮戒娇鐢ㄧ浉鍚岀殑鏍煎紡瀛樺偍
         switchDateAttrOnOrder(templateVO, orderDTO);
-        //9.鐢熸垚缂栫爜鐨勪俊鎭�
-//		ClientBusinessObject cbo = boService.createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmtypeid());
-        BaseModel cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmTypeId());
-//		//榛樿鐨勫睘鎬ч兘涓嶇敤浠庡墠绔嫹璐�
-//		//璁剧疆缂栫爜闇�瑕佺殑榛樿灞炴�х殑鍐呭
+        //9.鐢熸垚缂栫爜鐨勪俊鎭� 锛屽垵濮嬪寲涓氬姟绫诲瀷锛氱紦瀛樺厛鍙栨秷锛屽洜涓虹増鏈鍒欎細鍑虹幇鍙樺姩鐨勬儏鍐垫墍浠ユ棤娉曚娇鐢ㄧ紦瀛�
+        // BaseModel cbo = createCBOByBtmName(classifyFullInfo.getTopClassifyVO().getBtmTypeId());
+		BaseModel cbo = createBaseModel(classifyFullInfo.getTopClassifyVO().getBtmTypeId().trim().toLowerCase());
+		//榛樿鐨勫睘鎬ч兘涓嶇敤浠庡墠绔嫹璐�
+		//璁剧疆缂栫爜闇�瑕佺殑榛樿灞炴�х殑鍐呭
         copyValueToCBO(classifyFullInfo, cbo, orderDTO, templateVO, false);
 		cbo.setOid(VciBaseUtil.getPk());
 		cbo.setRevisionOid(VciBaseUtil.getPk());
@@ -455,9 +465,9 @@
 		if(StringUtils.isNotBlank(orderDTO.getLcStatus())||StringUtils.isNotBlank(orderDTO.getData().get("lcStatus"))){
 			cbo.setLcStatus(StringUtils.isNotBlank(orderDTO.getLcStatus())?orderDTO.getLcStatus():orderDTO.getData().get("lcStatus"));
 		}
-//		//TODO:鍥犱负榛樿鐨勫睘鎬ч兘涓嶆嫹璐濓紝鐩墠闆嗗洟鐮佸彨name锛屽苟娌℃湁浠嶥TO鎷疯礉鍒癱bo閲屻�傚鍔犱竴涓崟鐙鐞嗭紝浠ュ悗鍐嶇湅瑕佷笉瑕佽皟鏁�
+		//TODO:鍥犱负榛樿鐨勫睘鎬ч兘涓嶆嫹璐濓紝鐩墠闆嗗洟鐮佸彨name锛屽苟娌℃湁浠嶥TO鎷疯礉鍒癱bo閲屻�傚鍔犱竴涓崟鐙鐞嗭紝浠ュ悗鍐嶇湅瑕佷笉瑕佽皟鏁�
         cbo.setName(orderDTO.getName() == null ? "" : orderDTO.getName());
-//		//end -- modify by lihang @20220407
+		//end -- modify by lihang @20220407
         List<BaseModel> cboList = new ArrayList<>();
 
         //澶囨敞
@@ -513,7 +523,6 @@
             });
         }
     }
-
 
     /**
      * 鍒ゆ柇缂栫爜鐨勭爜娈垫槸鍚﹁緭鍏ユ垨鑰呴�夋嫨浜嗙爜鍊�
@@ -1018,22 +1027,22 @@
             List<CodeClassifyTemplateAttrVO> selectAttrVOs = templateVO.getAttributes().stream().filter(s -> StringUtils.isNotBlank(s.getLibraryIdentification())).collect(Collectors.toList());
 
             if (!CollectionUtils.isEmpty(selectAttrVOs)) {
-//				SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
+				//SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
                 selectAttrVOs.parallelStream().forEach(attrVO -> {
                     List<String> valuesList = new ArrayList<>();
                     cboList.parallelStream().forEach(cbo -> {
-//						String value = cbo.get.getAttributeValue(attrVO.getId());
+						//String value = cbo.get.getAttributeValue(attrVO.getId());
 
                         //灏哹ean杞负map,mybatis缁熶竴澶勭悊
                         Map<String, String> map = null;
 
-//						baseModels.stream().forEach(model-> {
+						//baseModels.stream().forEach(model-> {
                         try {
                             map = VciBaseUtil.convertBean2Map(cbo,null);
                         } catch (Exception e) {
                             throw new VciBaseException("绫诲瀷杞崲閿欒锛�" + e.getMessage());
                         }
-//						});
+						//});
 
                         String value = map.get(attrVO.getId());
                         if (StringUtils.isNotBlank(value)) {
@@ -1562,7 +1571,6 @@
             conditionMap.put(queryKey, QueryOptionConstant.OR + queryValue);
         }
     }
-
 
     /**
      * 鏌ヨ缂栫爜鏁版嵁鐨勫垪琛�
@@ -2963,7 +2971,7 @@
         wrapperData(dataList, templateVO, sqlBO.getSelectFieldList(), true);
         R<List<Map<String, String>>> result = R.data(Collections.singletonList(cbo));
         //鎴戜滑瑕佺湅鏄惁涓嶆槸鍗囩増鐨勶紝鍗囩増鐨勮瘽锛岄渶瑕佸姣斾笉鐩哥瓑鐨勫睘鎬�
-        String copy = cbo.get("copyfromversion");
+/*        String copy = cbo.get("copyfromversion");
 //		if (StringUtils.isBlank(copy)) {
 //			copy = cbo.getAttributeValue("copyfromversion");
 //		}
@@ -2982,11 +2990,12 @@
                 Map<String, String> difFieldMap = new HashMap<>();
                 Map<String, String> finalOldData = oldData;
                 cbo.forEach((key, value) -> {
-                    String oldValue = finalOldData.getOrDefault(key, "");
-                    if (value == null) {
+					// 杩欏効oldmap涓殑鍏ㄦ槸澶у啓锛岃�宑bo涓殑鍏ㄦ槸灏忓啓鎵�浠ヤ細鎷夸笉鍒板彧锛岃繖鍎跨洿鎺ュ鐞嗘垚灏忓啓鎷夸笉鍒板氨鐢ㄥぇ鍐欐嬁
+					String oldValue = String.valueOf(finalOldData.getOrDefault(key.toUpperCase(Locale.ROOT), finalOldData.getOrDefault(key, "")));
+					if (value == null || value == "null") {
                         value = "";
                     }
-                    if (oldValue == null) {
+                    if (oldValue == null || oldValue == "null") {
                         oldValue = "";
                     }
                     if (!value.equalsIgnoreCase(oldValue)) {
@@ -2997,7 +3006,7 @@
                 difFieldList.add(difFieldMap);
                 result.setData(difFieldList);
             }
-        }
+        }*/
         return result;
     }
 
@@ -3355,7 +3364,6 @@
         return getTableDefineByTemplateVO(templateService.getObjectHasAttrByOid(templateOid));
     }
 
-
     /**
      * 浣跨敤妯℃澘鏄剧ず瀵硅薄杞崲涓鸿〃鏍肩殑淇℃伅锛堝寘鍚墿灞曠殑鎸夐挳锛�
      *
@@ -3452,6 +3460,7 @@
         }
         return uiInfoVO;
     }
+
     @Override
 	public MdmUIInfoVO getFlowUIInfoByClassifyOid(String codeClassifyOid, String functionId,String templateId,String taskId,String modelKey){
 //		MdmUIInfoVO uiInfoVO = getTableDefineByClassifyOid_v2(codeClassifyOid,templateId,taskId,modelKey);
@@ -3523,6 +3532,7 @@
      * @return 澶勭悊鎴愬姛鏁版嵁鏉℃暟
      */
     @Override
+	@Transactional(rollbackFor = Exception.class)
     public Integer insertBatchByType(String btmType, List<BaseModel> baseModels) {
         //浣跨敤浼犲叆鐨勪笟鍔$被鍨嬫煡璇㈣〃
         R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(btmType));
@@ -3539,6 +3549,7 @@
 		}).collect(Collectors.toSet());
 		//灏哹ean杞负map,mybatis缁熶竴澶勭悊
         List<Map<String, String>> maps = new ArrayList<>();
+
         baseModels.stream().forEach(model -> {
             try {
                 maps.add(VciBaseUtil.convertBean2Map(model,existFild));
@@ -3546,8 +3557,47 @@
                 throw new VciBaseException("绫诲瀷杞崲閿欒锛�" + e.toString());
             }
         });
-        return commonsMapper.insertByBaseModel(listR.getData().get(0).getTableName(), maps.get(0), maps);
+		try {
+			bactchExecuteInsert(listR.getData().get(0).getTableName(),maps);
+		}catch (Exception e){
+			throw new ServiceException("鍒嗘壒鎵цinsert璇彞鎶ラ敊:"+e.getMessage());
+		}
+        return maps.size();
     }
+
+	/**
+	 * 鍒嗘壒鎵цinsert璇彞
+	 * @param tableName
+	 * @param maps
+	 * @throws ServiceException
+	 */
+	@Transactional(rollbackFor = Exception.class)
+	void bactchExecuteInsert(String tableName, List<Map<String, String>> maps) throws ServiceException{
+		ExecutorService executor = Executors.newFixedThreadPool(10); // 鍒涘缓涓�涓浐瀹氬ぇ灏忕殑绾跨▼姹�
+		List<Map<String, String>> threadSafeMaps = new CopyOnWriteArrayList<>(maps);
+
+		for (int i = 0; i < threadSafeMaps.size(); i += MAX_IMPORT_NUM) {
+			final int startIndex = i;
+			final int endIndex = Math.min(i + MAX_IMPORT_NUM, maps.size());
+
+			executor.execute(() -> {
+				List<Map<String, String>> subList = threadSafeMaps.subList(startIndex, endIndex);
+				// 璋冪敤鎻掑叆鏁版嵁搴撶殑鏂规硶
+				commonsMapper.insertByBaseModel(tableName, threadSafeMaps.get(0), subList);
+			});
+		}
+
+		// 鍏抽棴绾跨▼姹�
+		executor.shutdown();
+		try {
+			// 绛夊緟鎵�鏈変换鍔℃墽琛屽畬鎴�
+			executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
+		} catch (InterruptedException e) {
+			// 澶勭悊寮傚父
+			throw new ServiceException("澶氱嚎绋嬫柟寮忔墽琛屾壒閲忔彃鍏ユ椂浜х敓閿欒:"+e.getMessage());
+		}
+	}
+
 	/**
 	 * 浼犲叆涓氬姟绫诲瀷浠ュ強ID鏌ヨ涓氬姟琛ㄦ暟鎹槸鍚﹂噸澶�
 	 *
@@ -4101,6 +4151,7 @@
         toBo.setNameOid(fromBo.getNameOid());
         toBo.setBtmname(fromBo.getBtmname());
         toBo.setLastR(String.valueOf(1));
+		// 鍗囩増灏咶irstR淇敼涓�0
         toBo.setFirstR(String.valueOf(0));
         toBo.setFirstV(String.valueOf(1));
         toBo.setLastV(String.valueOf(1));
@@ -4111,13 +4162,28 @@
 		toBo.setLastModifier(String.valueOf(AuthUtil.getUser().getAccount()));
         toBo.setLastModifyTime(new Date());
         toBo.setRevisionRule(fromBo.getRevisionRule());
-        toBo.setVersionRule(fromBo.getVersionRule());R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(fromBo.getBtmname()));
+        toBo.setVersionRule(fromBo.getVersionRule());
+		//鏌ヨ涓氬姟绫诲瀷淇℃伅锛屽彲浠ヨ幏鍙栧埌鐗堟湰瑙勫垯淇℃伅锛坮evisionRuleId锛�
+		R<List<BtmTypeVO>> listR = btmTypeClient.selectByIdCollection(Collections.singletonList(fromBo.getBtmname()));
         if (!listR.isSuccess() || listR.getData().size() == 0) {
             throw new VciBaseException("浼犲叆涓氬姟绫诲瀷鏈煡璇㈠埌鐩稿簲琛ㄥ崟锛岃妫�鏌ワ紒");
         }
-        Map<String, Object> nextRevision = commonsMapper.getNextRevision(listR.getData().get(0).getTableName(), fromBo.getNameOid());
-        toBo.setRevisionSeq(Integer.parseInt(nextRevision.get("REVISIONSEQ").toString()));
-        toBo.setRevisionValue(nextRevision.get("REVISIONVAL").toString());
+        //Map<String, Object> nextRevision = commonsMapper.getNextRevision(listR.getData().get(0).getTableName(), fromBo.getNameOid());
+		/*
+			TODO:杩欐槸涓�涓緟鍚庢湡瀹屽杽鐨勫姛鑳斤紝鐩墠瀹炵幇鏂瑰紡鏄紝鍏堟煡璇㈠嚭褰撳墠鏁版嵁鐨勫ぇ鐗堟湰瑙勫垯锛�
+				鐒跺悗鍐嶆煡璇笟鍔¤〃浣跨敤鐨勮鍒欑殑姝ラ暱鏄灏戠劧鍚庤绠楀嚭璇ュ崌鐗堜负澶氬皯
+		*/
+		Map<String, Object> nextRevision = commonsMapper.getCurrentRevision(listR.getData().get(0).getTableName(), fromBo.getNameOid());
+		R<RevisionRuleVO> revisionRuleVOR = revisionRuleClient.selectById(listR.getData().get(0).getRevisionRuleId());
+		String revisionval = nextRevision.get("REVISIONVAL").toString();
+		// 鏈煡璇㈠埌鐗堟湰瑙勫垯锛岄粯璁ょ洿鎺ョ粰澶х増鏈姞涓�
+		if(!revisionRuleVOR.isSuccess() || Func.isEmpty(revisionRuleVOR.getData())){
+			revisionval = String.valueOf((Integer.parseInt(revisionval)+1));
+		}else {
+			revisionval = String.valueOf(Integer.parseInt(revisionval)+revisionRuleVOR.getData().getSerialStep());
+		}
+		toBo.setRevisionValue(revisionval);
+		toBo.setRevisionSeq(Integer.parseInt(nextRevision.get("REVISIONSEQ").toString()));
         toBo.setVersionSeq(Integer.valueOf(nextRevision.get("VERSIONSEQ").toString()));
         toBo.setVersionValue(nextRevision.get("VERSIONVAL").toString());
         toBo.setLctid(fromBo.getLctid());

--
Gitblit v1.9.3