From ac8e8998235f247365280d1a1632115c43b10037 Mon Sep 17 00:00:00 2001
From: ludc
Date: 星期五, 17 十一月 2023 02:24:30 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmProductCodeServiceImpl.java |  166 ++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 128 insertions(+), 38 deletions(-)

diff --git a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmProductCodeServiceImpl.java b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmProductCodeServiceImpl.java
index ea0a767..bc8b436 100644
--- a/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmProductCodeServiceImpl.java
+++ b/Source/UBCS/ubcs-service/ubcs-code/src/main/java/com/vci/ubcs/code/service/impl/MdmProductCodeServiceImpl.java
@@ -1,5 +1,7 @@
 package com.vci.ubcs.code.service.impl;
 
+import com.vci.ubcs.code.algorithm.CustomSerialEnum;
+import com.vci.ubcs.code.dto.CodeCustomSerialDTO;
 import com.alibaba.nacos.common.utils.StringUtils;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.vci.ubcs.code.bo.CodeClassifyFullInfoBO;
@@ -36,15 +38,16 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
-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;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.stream.Collectors;
 
@@ -105,7 +108,7 @@
 			//涓昏鏄负浜嗗綍鍏ユ渶澶ф祦姘村彿鍜宎llcode
 			//SessionInfo sessionInfo = VciBaseUtil.getCurrentUserSessionInfo();
 			List<CodeAllCode> allCodeDOList = new CopyOnWriteArrayList<>();
-			Map<String/**娴佹按渚濇嵁**/, Map<String/**鐮佹鐨勪富閿�**/,Double/**鏈�澶ф祦姘村彿**/>> maxSerialMap = new HashMap<>();
+			Map<String/**娴佹按渚濇嵁**/, ConcurrentHashMap<String/**鐮佹鐨勪富閿�**/,String/**鏈�澶ф祦姘村彿**/>> maxSerialMap = new ConcurrentHashMap<>();
 			// TODO 澶氱嚎绋嬫祦寮曞彂鐨勯棶棰樺凡淇敼
 			dataCBOList.parallelStream().forEach(cbo->{
 				log.info("code:----->"+cbo.getId());
@@ -120,6 +123,7 @@
 				List<CodeBasicSecVO> secVOList = finalRuleVO.getSecVOList().stream().sorted(((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()))).collect(Collectors.toList());
 				Map<String/**鐮佹鐨勪富閿�**/,String/**鐮佹鐨勫��**/> serialValueMap = new HashMap<>();
 				Map<String, CodeBasicSecVO> secVOMap = secVOList.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
+				Map<String,Integer> serialSecOidIndexMap=new HashMap<>();
 				for (int i = 0; i < secLengths.length; i++) {
 					CodeBasicSecVO secVO = secVOList.get(i);
 					String thisSecValue  = "";
@@ -137,38 +141,57 @@
 					//	}
 					if(VciBaseUtil.getBoolean(secVO.getSerialDependFlag())){
 						serialUnitList.add(thisSecValue);
+						serialSecOidIndexMap.put(secVO.getOid(),i);
 					}
 					if(CodeSecTypeEnum.CODE_SERIAL_SEC.getValue().equalsIgnoreCase(secVO.getSecType())){
 						serialValueMap.put(secVO.getOid(),thisSecValue);
 					}
 				}
-				String serialUnitString = serialUnitList.size() == 0 ? EMPTY_SERIAL_UNIT : serialUnitList.stream().collect(Collectors.joining(SERIAL_UNIT_SPACE));
 				List<String> serialUnFileStringList = new ArrayList<>();
+				AtomicReference<String> newSerialUnitString = new AtomicReference<>("");
 				if(!CollectionUtils.isEmpty(serialValueMap)){
+					AtomicInteger index= new AtomicInteger();
 					serialValueMap.forEach((secOid,secValue)->{
 						//瑕佺湅鏄笉鏄ˉ浣嶇殑
 						CodeBasicSecVO secVO = secVOMap.get(secOid);
-						Double serialDb = null;
+						List<String >newSerialUnitList=new ArrayList<>();
+						newSerialUnitList.addAll(serialUnitList);
+						if(index.get() ==0){
+							if(serialSecOidIndexMap.containsKey(secOid)) {
+								int num = serialSecOidIndexMap.get(secOid);
+								newSerialUnitList.set(num, "");
+							}
+						}
+						String serialUnitString = newSerialUnitList.size() == 0 ? EMPTY_SERIAL_UNIT : newSerialUnitList.stream().collect(Collectors.joining(SERIAL_UNIT_SPACE));
+						newSerialUnitString.set(serialUnitString);
+						index.getAndIncrement();
+						String serialDb = "";
 						// 鎴彇鎺夊墠鍚庣紑涔嬪悗鐨勭爜娈�
 						String subSecValue = killPriffixSuffix(secValue, secVO.getPrefixCode(), secVO.getSuffixCode());
 						if(OsCodeFillTypeEnum.NONE.getValue().equalsIgnoreCase(secVO.getCodeFillType())){
 							//涓嶈ˉ鐮�
 							//鎶婃墍鏈変笉鏄暟瀛楃殑鍘绘帀锛屽洜涓哄彲鑳戒細鏄�佹暟鎹紝鏂拌鍒�
-							serialDb = VciBaseUtil.getDouble(killUnNumberChar(subSecValue));
+							//	serialDb = VciBaseUtil.getDouble(killUnNumberChar(subSecValue));
+							//serialDb = killUnNumberChar(subSecValue);
+							serialDb=subSecValue;
 						}else {
 							//宸﹀彸濉厖鐨勶紝鎴戜滑闇�瑕�
-							serialDb = VciBaseUtil.getDouble(killUnNumberChar(killFillChar(subSecValue,secVO.getCodeFillSeparator(),
-								OsCodeFillTypeEnum.LEFT.getValue().equalsIgnoreCase(secVO.getCodeFillType()))));
+							serialDb = killFillChar(subSecValue,secVO.getCodeFillSeparator(),
+								OsCodeFillTypeEnum.LEFT.getValue().equalsIgnoreCase(secVO.getCodeFillType()));
 						}
-						Map<String, Double> thisUnitMaxMap = maxSerialMap.getOrDefault(serialUnitString, new HashMap<>());
-						Double maxValue=serialDb;
+						Double newSerialDb= CustomSerialEnum.getDoubleCustomSerialValue(secValue,secVO.getCustomCodeSerialType());
+						ConcurrentHashMap<String, String> thisUnitMaxMap = maxSerialMap.getOrDefault(serialUnitString, new ConcurrentHashMap<>());
+						Double maxValue=newSerialDb;
 						if(thisUnitMaxMap.containsKey(secOid)){
-							maxValue=  thisUnitMaxMap.getOrDefault(secOid,new Double(-1));
-							if(maxValue<serialDb){
-								maxValue=serialDb;
+							String 	newMaxValue=  thisUnitMaxMap.getOrDefault(secOid,"");
+							maxValue=StringUtils.isBlank(newMaxValue)?-1:VciBaseUtil.getDouble(newMaxValue);
+							if(maxValue<newSerialDb){
+								maxValue=newSerialDb;
 							}
 						}
-						thisUnitMaxMap.put(secOid,maxValue);
+						String newMaxValue=CustomSerialEnum.getStringCustomSerialValue(maxValue,secVO.getCustomCodeSerialType());
+						serialDb=CustomSerialEnum.getStringCustomSerialValue(newSerialDb,secVO.getCustomCodeSerialType());
+						thisUnitMaxMap.put(secOid,newMaxValue);
 						maxSerialMap.put(serialUnitString,thisUnitMaxMap);
 						serialUnFileStringList.add(String.valueOf(serialDb));
 					});
@@ -181,7 +204,7 @@
 				allCodeDO.setCodeClassifyTemplateOid(templateVO.getOid());
 				allCodeDO.setCreateCodeBtm(cbo.getBtmname());
 				allCodeDO.setCreateCodeOid(cbo.getOid());
-				allCodeDO.setSerialUnit(serialUnitString);
+				allCodeDO.setSerialUnit(newSerialUnitString.get());
 				String unFillSerial =serialUnFileStringList.size()==1?serialUnFileStringList.get(0)+ SERIAL_VALUE_SPACE:serialUnFileStringList.stream().collect(Collectors.joining(SERIAL_VALUE_SPACE));
 				allCodeDO.setUnFillSerial(unFillSerial);
 				allCodeDO.setLcStatus(cbo.getLcStatus());
@@ -204,7 +227,7 @@
 					if (!CollectionUtils.isEmpty(serialValueDOS)) {
 						CodeSerialValue serialValueDO = serialValueDOS.get(0);
 						log.info("oldmaxSerial--->"+serialValueDO.getMaxSerial()+"---- newmaxSerial---->"+maxSerial);
-						if(VciBaseUtil.getDouble(serialValueDO.getMaxSerial())<maxSerial){
+						if(VciBaseUtil.getDouble(serialValueDO.getMaxSerial())<VciBaseUtil.getDouble(maxSerial)){
 							serialValueDO.setMaxSerial(String.valueOf(maxSerial));
 							DefaultAttrAssimtUtil.updateDefaultAttrAssimt(serialValueDO);
 							editSerialValueList.add(serialValueDO);
@@ -318,7 +341,16 @@
 
 					codeAllCodeService.saveBatch(addCodeDOs);
 				}
+
+//				try {
 				mdmEngineService.insertBatchByType(dataCBOList.get(0).getBtmname(),dataCBOList);
+					// 鎻愪氦浜嬪姟
+					//transactionTemplate.getTransactionManager().commit(status);
+//				}catch (Exception e){
+//					// 鍑虹幇寮傚父鏃跺洖婊氫簨鍔�
+//					transactionTemplate.getTransactionManager().rollback(status);
+//				}
+
 			}
 			return codeList;
 		}else {
@@ -382,10 +414,12 @@
 				allCode -> {DefaultAttrAssimtUtil.addDefaultAttrAssimt(allCode,"codeallcode");allCode.setLctid("codeAllCodeLC");}
 			);
 
+
 			Map<String, String> statusMap = allCodeDOList.stream().collect(Collectors.toMap(s -> s.getOid(), s -> s.getLcStatus()));
 			allCodeDOList.stream().filter(s -> StringUtils.equalsIgnoreCase("codeallcode",s.getBtmname())).forEach(s -> {
 				s.setLcStatus(statusMap.get(s.getOid()));
 			});
+
 
 			//閫氳繃ID鏉ヨ繘琛屽幓閲�
 			List<CodeAllCode> distinctCodeAllCOdes = allCodeDOList.stream().collect(Collectors
@@ -414,7 +448,7 @@
 					if(codeAllCode.getId().equals(next.getId())){
 						codeAllCode.setCreateCodeOid(next.getCreateCodeOid());
 						codeAllCode.setLcStatus(next.getLcStatus());
-						iterator.remove();
+						//iterator.remove();
 					}
 				}
 			}
@@ -499,8 +533,21 @@
 									  Map<String/**鐮佹鐨勪富閿�**/,Map<String/**娴佹按渚濇嵁**/,CodeSerialValue>> maxSerialValueMap,List<String> thisSecValueList,
 									  Map<String/**鐮佹鐨勪富閿�**/,Map<String/**娴佹按渚濇嵁**/, Double>> lastMaxSerialValueMap,boolean firstData){
 		if (!CollectionUtils.isEmpty(serialSecVOList)) {
+			Map<String,String> secIdserialValueMap=new LinkedHashMap<>();
 			for (int j = 0; j < serialSecVOList.size(); j++) {
 				CodeBasicSecVO secVO = serialSecVOList.get(j);
+				String secVOValue="";
+				AtomicReference<String> newSerialUnitString= new AtomicReference<>(serialUnitString);
+				if(j==0){
+					secVOValue="";
+					newSerialUnitString.set(serialUnitString.replace("${" + secVO.getOid() + "}", secVOValue));
+				}else{
+					CodeBasicSecVO upSecVO =serialSecVOList.get(j-1);
+					secIdserialValueMap.forEach((key,vaule)->{
+						newSerialUnitString.set(serialUnitString.replace("${"+key+"}", vaule));
+					});
+				}
+
 				if (attrSevIsSerialDepend || firstData) {
 					//濡傛灉灞炴�х爜娈典篃鏄緷璧栵紝鍒欐瘡娆¢兘瑕佹煡璇紝濡傛灉灞炴�х爜娈典笉鏄緷璧栵紝鍒欏彧鏌ヨ涓�娆�
 //					Map<String, String> conditionMap = new HashMap<>();
@@ -509,8 +556,9 @@
 //					//杩欎釜瀛楁鏄负浜嗚В鍐冲涓祦姘寸殑闂
 //					conditionMap.put("codeSecOid", secVO.getOid());
 					QueryWrapper<CodeSerialValue> codeSerialWrapper = new QueryWrapper<>();
+
 					codeSerialWrapper.eq("codeRuleOid", ruleVO.getOid());
-					codeSerialWrapper.eq("serialUnit", serialUnitString);
+					codeSerialWrapper.eq("serialUnit", newSerialUnitString.get());
 					codeSerialWrapper.eq("codeSecOid", secVO.getOid());
 					List<CodeSerialValue> serialValueDOS = serialValueMapper.selectList(codeSerialWrapper);
 					if (!CollectionUtils.isEmpty(serialValueDOS)) {
@@ -520,25 +568,41 @@
 						maxSerialValueMap.put(secVO.getOid(), unitSerialMap);
 					}
 				}
-				Double startValue = null;
-				if(maxSerialValueMap.containsKey(secVO.getOid()) && maxSerialValueMap.get(secVO.getOid()).containsKey(serialUnitString)){
-					startValue = VciBaseUtil.getDouble(maxSerialValueMap.get(secVO.getOid()).get(serialUnitString).getMaxSerial());
+				String serialString="";
+				String thisSerialValue = "";
+				String startValue = null;
+				if (maxSerialValueMap.containsKey(secVO.getOid()) && maxSerialValueMap.get(secVO.getOid()).containsKey(newSerialUnitString.get())) {
+					startValue = maxSerialValueMap.get(secVO.getOid()).get(newSerialUnitString.get()).getMaxSerial();
 				}
-				if(lastMaxSerialValueMap.containsKey(secVO.getOid()) && lastMaxSerialValueMap.get(secVO.getOid()).containsKey(serialUnitString)){
+				if (lastMaxSerialValueMap.containsKey(secVO.getOid()) && lastMaxSerialValueMap.get(secVO.getOid()).containsKey(newSerialUnitString.get())) {
 					//璇存槑澶氫釜鐢宠锛屼箣鍓嶅凡缁忓姞浜嗘祦姘村彿浜�
-					startValue = lastMaxSerialValueMap.get(secVO.getOid()).get(serialUnitString);
+					startValue = lastMaxSerialValueMap.get(secVO.getOid()).get(newSerialUnitString.get());
 				}
-				Double thisSerialValue = 0d;
-				if (startValue == null) {
-					//绗竴涓紪鐮�
-					thisSerialValue = VciBaseUtil.getDouble(secVO.getSerialStart());
-				} else {
-					//娴佹按鍙疯偗瀹氭槸鏁板瓧
-					thisSerialValue = startValue + (j + 1) * secVO.getSerialStep();
-				}
-				//瑕佺湅鏄惁瓒呰繃鏈�澶х殑娴佹按鍊�
-				if (thisSerialValue >= secVO.getCodeFillLimit()) {
-					throw new VciBaseException("娴佹按鍙峰凡缁忚秴杩囧厑璁哥殑鏈�澶ф祦姘村�納0}",new String[]{secVO.getCodeFillLimit().toString()});
+				if(StringUtils.isNotBlank(secVO.getCustomCodeSerialClass())){//鑷畾涔夋祦姘村鐞�
+					String currentFlowValue=startValue;
+					CodeCustomSerialDTO codeCustomSerialDTO=new CodeCustomSerialDTO();
+					codeCustomSerialDTO.setSerialUnitString(newSerialUnitString.get());
+					codeCustomSerialDTO.setSerialCodeCodeBasicSec(secVO);
+					codeCustomSerialDTO.setCurrentFlowValue(startValue);
+					codeCustomSerialDTO.setCodeBasicSecVOList(ruleVO.getSecVOList());
+					codeCustomSerialDTO.setCodeRuleOid(ruleVO.getOid());
+					codeCustomSerialDTO.setBaseModel(cbo);
+					thisSerialValue=customCodeSerial(secVO.getCustomCodeSerialClass(),codeCustomSerialDTO);
+					log.info(secVO.getCustomCodeSerialClassText()+"---->"+thisSerialValue);
+				}else {
+					Double newThisSerialValue = 0d;
+					if (startValue == null) {
+						//绗竴涓紪鐮�
+						newThisSerialValue = VciBaseUtil.getDouble(secVO.getSerialStart());
+					} else {
+						//娴佹按鍙疯偗瀹氭槸鏁板瓧
+						newThisSerialValue = Double.parseDouble(startValue) + (j + 1) * secVO.getSerialStep();
+					}
+					//瑕佺湅鏄惁瓒呰繃鏈�澶х殑娴佹按鍊�
+					if (newThisSerialValue >= secVO.getCodeFillLimit()) {
+						throw new VciBaseException("娴佹按鍙峰凡缁忚秴杩囧厑璁哥殑鏈�澶ф祦姘村�納0}", new String[]{secVO.getCodeFillLimit().toString()});
+					}
+					thisSerialValue=String.valueOf(newThisSerialValue.intValue());
 				}
 				//瑕佺湅琛ヤ綅鐨勫唴瀹�
 				Integer fillLength = VciBaseUtil.getInt(secVO.getCodeFillLength());
@@ -548,6 +612,7 @@
 				}
 				String serialString = String.valueOf(thisSerialValue.longValue());
 				serialString = fillString(fillLength, OsCodeFillTypeEnum.forValue(secVO.getCodeFillType()), serialString, secVO.getCodeFillSeparator());
+
 				for (int z = 0; z < thisSecValueList.size(); z++) {
 					String secValue = thisSecValueList.get(z);
 					if (secValue.equalsIgnoreCase("${" + secVO.getOid() + "}")) {
@@ -555,11 +620,36 @@
 						thisSecValueList.set(z, joinPreffixAndSuffix(secVO, serialString));
 					}
 				}
-				Map<String, Double> unitSerialMap = lastMaxSerialValueMap.getOrDefault(secVO.getOid(), new HashMap<>());
-				unitSerialMap.put(serialUnitString, thisSerialValue);
+				secIdserialValueMap.put(secVO.getOid(),thisSerialValue);//璁板綍娴佹按鐮佹褰撳墠鐨勫��
+				Map<String, String> unitSerialMap = lastMaxSerialValueMap.getOrDefault(secVO.getOid(), new HashMap<>());
+				unitSerialMap.put(newSerialUnitString.get(), String.valueOf(thisSerialValue));
 				lastMaxSerialValueMap.put(secVO.getOid(),unitSerialMap);
 			}
 		}
+	}
+
+	private String customCodeSerial(String className, CodeCustomSerialDTO codeCustomSerialDTO){
+		String result = "";
+		try {
+			Class classInstance=Class.forName(className);
+			Object obj =classInstance.newInstance();
+			Method method= classInstance.getMethod("serialGenerate",CodeCustomSerialDTO.class);
+			method.setAccessible(Boolean.TRUE);
+			//method.invoke(obj);
+			Object  o= method.invoke(obj,codeCustomSerialDTO);
+			result= Func.isEmpty(o) ?"":o.toString();
+		} catch (ClassNotFoundException e) {
+			throw new VciBaseException("鏈壘鍒拌嚜瀹氫箟娴佹按绠楁硶绫绘枃浠�");
+		} catch (InvocationTargetException e) {
+			throw new VciBaseException("鎵ц鑷畾涔夋祦姘寸畻娉曞鐞嗘柟娉曞嚭鐜板紓甯�");
+		} catch (NoSuchMethodException e) {
+			throw new VciBaseException("鏈壘鍒拌嚜瀹氫箟娴佹按绠楁硶澶勭悊鏂规硶");
+		} catch (IllegalAccessException e) {
+			throw new VciBaseException("鎵ц鑷畾涔夋祦姘寸畻娉曞鐞嗘柟娉曞嚭鐜板紓甯�");
+		} catch (InstantiationException e) {
+			e.printStackTrace();
+		}
+		return result;
 	}
 
 	/**
@@ -759,7 +849,7 @@
 	 * @param maxSerialValueMap 渚濇嵁瀛樺偍鐨勬渶澶ф祦姘寸殑鍐呭
 	 */
 	private void saveSerialValue(CodeRuleVO ruleVO,
-								 Map<String/**鐮佹鐨勪富閿�**/,Map<String, Double>> lastMaxSerialValueMap,
+								 Map<String/**鐮佹鐨勪富閿�**/,Map<String, String>> lastMaxSerialValueMap,
 								 Map<String/**鐮佹鐨勪富閿�**/,Map<String, CodeSerialValue>> maxSerialValueMap){
 		List<CodeSerialValue> editSerialValueDOList = new ArrayList<>();
 		List<CodeSerialValue> addSerialValueDOList = new ArrayList<>();

--
Gitblit v1.9.3