From 33e6270d2d3a40591e58d45fd8435d2f3eaf66b2 Mon Sep 17 00:00:00 2001
From: yangys <y_ys79@sina.com>
Date: 星期四, 14 八月 2025 11:02:56 +0800
Subject: [PATCH] 启动固化流程优化

---
 blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/excution/StartDispatcher.java |  142 ++++++++++++++++++++++++++++++++++------------
 1 files changed, 104 insertions(+), 38 deletions(-)

diff --git a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/excution/StartDispatcher.java b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/excution/StartDispatcher.java
index 675d536..2f20bad 100644
--- a/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/excution/StartDispatcher.java
+++ b/blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/excution/StartDispatcher.java
@@ -1,73 +1,138 @@
 package org.springblade.mdm.flow.excution;
 
 import io.swagger.v3.oas.annotations.media.Schema;
-import org.flowable.common.engine.impl.identity.Authentication;
+import lombok.AllArgsConstructor;
+import org.apache.commons.lang3.StringUtils;
 import org.flowable.engine.IdentityService;
 import org.flowable.engine.runtime.ProcessInstance;
 import org.springblade.core.secure.utils.AuthUtil;
-import org.springblade.core.tool.api.R;
-import org.springblade.flow.core.feign.IFlowClient;
-import org.springblade.flow.core.pojo.entity.BladeFlow;
-import org.springblade.flow.core.utils.TaskUtil;
+import org.springblade.core.tool.utils.Func;
+import org.springblade.mdm.flow.constants.FlowContants;
+import org.springblade.mdm.flow.entity.FlowProgramFile;
+import org.springblade.mdm.flow.service.FlowProgramFileService;
 import org.springblade.mdm.flow.vo.TaskAssignVO;
-import org.springblade.mdm.program.entity.NcProgram;
-import org.springblade.mdm.program.service.NcProgramService;
+import org.springblade.mdm.program.entity.NcNode;
+import org.springblade.mdm.program.service.NcNodeService;
+import org.springblade.mdm.program.service.NodeDeptQueryService;
+import org.springblade.mdm.utils.EntityUtil;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.flowable.engine.RuntimeService;
+import org.springframework.transaction.annotation.Transactional;
+
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+@AllArgsConstructor
 @Service("startDispatcher")
 public class StartDispatcher  {
 
-	@Autowired
-	private RuntimeService runtimeService;
-	@Autowired
-	private AutoAssignUsersService autoAssignUsersService;
-	@Autowired
-	private IdentityService identityService;
+	private final RuntimeService runtimeService;
+	private final AutoAssignUsersService autoAssignUsersService;
 
-	@Autowired
-	private NcProgramService ncProgramService;
-	private static final String PROCESS_KEY = "dispatch";
+	private final IdentityService identityService;
+	private final NcNodeService ncNodeService;
 
-	public void start(TaskAssignVO startVO){
+	private final FlowProgramFileService flowProgramFileService;
+	private final NodeDeptQueryService nodeDeptQueryService;
+	//public static final String PROCESS_KEY = "dispatch";
 
-		Map<String,Object> preAssignee = autoAssignUsersService.autoAssignUsers(startVO.getProducePlanId());
+	/**
+	 * 鍚姩娲惧伐娴佺▼
+	 * @param startVO 琛ㄥ崟鏁版嵁
+	 * @return 娴佺▼瀹炰緥id
+	 */
+	@Transactional
+	public String start(TaskAssignVO startVO){
+
+		Map<String,Object> preAssignee = autoAssignUsersService.autoAssignUsers(startVO);
 		Map<String, Object> vars = new HashMap<>(preAssignee);
+		vars.put(FlowContants.ASSIGNEE,preAssignee.get(FlowContants.TEAM_LEADER));//绗竴涓鎵圭敤鎴凤細缁勯暱
 
-		vars.put("machineCode",startVO.getMachineCode());
-		vars.put("machineMode",startVO.getMachineMode());
+		vars.put(FlowContants.TITLE,startVO.getTitle());
+		//鏈哄簥缂栧彿
+		vars.put(FlowContants.MACHINE_CODE,startVO.getMachineCode());
+		String workshop = nodeDeptQueryService.getWorkshopNameByMachineCode(startVO.getMachineCode());
+		vars.put("workshop",workshop);
+		//鏈哄簥鍨嬪彿
+		vars.put(FlowContants.MACHINE_MODE,startVO.getMachineMode());
 
-		vars.put("processNo",startVO.getProcessNo());
-		vars.put("processName",startVO.getProcessName());
-		vars.put("processEdition",startVO.getProcessEdition());
 
-		vars.put("craftEdition",startVO.getCraftEdition());
+		vars.put(FlowContants.PROCESS_NO,startVO.getProcessNo());
+		vars.put(FlowContants.PROCESS_NAME,startVO.getProcessName());
+		vars.put(FlowContants.PROCESS_EDITION,startVO.getProcessEdition());
+		vars.put(FlowContants.CRAFT_EDITION,startVO.getCraftEdition());
 		vars.put("planStartTime",startVO.getPlanStartTime());
-		vars.put("partNo",startVO.getPartNo());
-		vars.put("partNoEdition",startVO.getPartNoEdition());
+		vars.put(FlowContants.DRAWING_NO,startVO.getDrawingNo());
+		vars.put(FlowContants.DRAWING_NO_EDITION,startVO.getDrawingNoEdition());
 		vars.put("planLockDays",startVO.getPlanLockDays());
-		vars.put("productModel",startVO.getProductModel());
-		//闆剁粍浠朵唬鐮�
-		String partNo = generatePartNo(startVO);
-		vars.put("partNo",partNo);
+		vars.put(FlowContants.PRODUCT_MODEL,startVO.getProductModel());
 
-		List<NcProgram> curedNcProgram = ncProgramService.getCuredNcProgram(partNo,startVO.getMachineCode());
-		//vars.put("curedNcProgram",curedNcProgram);
+		if(StringUtils.isNotBlank(startVO.getDeviation())) {
+			vars.put(FlowContants.DEVIATION, startVO.getDeviation());//鍋忕鍗�
+		}
+		String programPkgName = NcNodeService.genProgramName(startVO.getDrawingNo(),startVO.getProcessNo());
+		vars.put(FlowContants.PROGRAM_PACKAGE_NAME,programPkgName);
 
+		//TODO 鏌ヨ鍥哄寲绋嬪簭鏄紝搴旇鍘绘帀鏈夊亸绂诲崟鐨勶紵
+		NcNode curedProgramPackage = ncNodeService.getLastEditionCuredProgramPackage(programPkgName);
 		//璁剧疆鏄惁鏈夊浐鍖栫▼搴忔爣璁�
-		vars.put("hasCuredProgram",!curedNcProgram.isEmpty() ? "Y":"N");
+		vars.put(FlowContants.HAS_CURED_PROGRAM,curedProgramPackage != null ? FlowContants.Y:FlowContants.N);
+		//宸ュ簭鐗堟鏄惁涓�鑷达紝鏄惁鍦ㄦ湁鏁堟湡鍐�
+		if(curedProgramPackage != null && Func.isBlank(startVO.getDeviation())){//鍋忕鍗曚紭鍏堬紝濡傛灉鏈夊亸绂诲崟锛岄偅涔堟壘鍒板浐鍖栫▼搴忕殑涔熶笉绠楀浐鍖栦簡
+			vars.put(FlowContants.CURED_NODE_ID,curedProgramPackage.getId());
+			vars.put(FlowContants.IS_PROCESS_EDITION_SAME, StringUtils.equals(curedProgramPackage.getProcessEdition(),startVO.getProcessEdition()) ? "Y":"N");
+			vars.put(FlowContants.VALIDITY_PERIOD, curedProgramPackage.withinValidityPeriod() ? FlowContants.Y:FlowContants.N);
+		}else{
+			vars.put(FlowContants.IS_PROCESS_EDITION_SAME, FlowContants.Y);
+			vars.put(FlowContants.VALIDITY_PERIOD, FlowContants.Y);
+		}
 
 		String businessKey = "0";//涓氬姟琛╧ey
-
 		identityService.setAuthenticatedUserId(String.valueOf(AuthUtil.getUserId()));//璁剧疆娴佺▼鍙戣捣浜�
-		ProcessInstance pinst = runtimeService.startProcessInstanceByKey(PROCESS_KEY,businessKey,vars);
+		ProcessInstance inst = runtimeService.startProcessInstanceByKey(FlowContants.TRY_PROCESS_KEY,businessKey,vars);
 
-		int a=1;
-		//log.info()
+		if(curedProgramPackage != null) {//瀛樺湪宸插浐鍖栫▼搴忥紝澶嶅埗鍘熸潵鐨勭▼搴忔枃浠躲�傚垱寤鸿妭鐐逛粛鐒跺湪缂栫▼浜哄憳鎻愪氦浠ュ悗
+			copyFlowProgramFiles(curedProgramPackage.getProcessInstanceId(),inst.getProcessInstanceId());
+		}
+
+		return inst.getProcessInstanceId();
+	}
+
+	/**
+	 * 鏍规嵁鍘熸湁鍥哄寲绋嬪簭鍖呭悕鑺傜偣锛屽鍒朵竴涓紝璧嬩簣鏂扮殑娴佺▼瀹炰緥id
+	 * @param programPkg 鍘熸湁鍥哄寲鐨勭▼搴忓寘鍚嶈妭鐐�
+	 * @param processInstanceId 娴佺▼瀹炰緥id
+	 */
+
+	void cloneNodes(NcNode programPkg,String processInstanceId){
+		NcNode newProgramPkg = new NcNode();
+		BeanUtils.copyProperties(programPkg,newProgramPkg);
+		EntityUtil.clearBaseProperties(newProgramPkg);
+
+		newProgramPkg.setProcessInstanceId(processInstanceId);
+		this.ncNodeService.save(newProgramPkg);
+	}
+
+	/**
+	 * 澶嶅埗娴佺▼绋嬪簭鏂囦欢
+	 * @param processInstanceId 鏂板惎鍔ㄧ殑娴佺▼id
+	 */
+	private void copyFlowProgramFiles(String oldProcessInstanceId,String processInstanceId) {
+		List<FlowProgramFile> fileList = flowProgramFileService.lambdaQuery()
+			.eq(FlowProgramFile::getProcessInstanceId,oldProcessInstanceId).list();
+
+		for (FlowProgramFile file : fileList) {
+			FlowProgramFile newFile = new FlowProgramFile();
+			BeanUtils.copyProperties(file, newFile);
+			EntityUtil.clearBaseProperties(newFile);
+
+			newFile.setProcessInstanceId(processInstanceId);
+
+			this.flowProgramFileService.save(newFile);
+		}
 	}
 
 	/**
@@ -76,6 +141,7 @@
 	 * @param startVO
 	 * @return
 	 */
+	/*
 	String generatePartNo(TaskAssignVO startVO){
 		//TODO 杩欎釜鏍煎紡鏈‘瀹氾紝闇�瑕佺‘璁�
 		return String.format("%s-%s-%s-%s-%s-%s",
@@ -85,5 +151,5 @@
 			startVO.getProcessName(),
 			startVO.getCraftEdition(),
 			startVO.getProcessEdition());
-	}
+	}*/
 }

--
Gitblit v1.9.3