yangys
2025-08-28 3c1c93e559d2dc060cdfc2ab7f0c36378230abab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.springblade.mdm.flow.excution;
 
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.utils.Func;
import org.springblade.mdm.basesetting.producedivision.entity.ProduceDivision;
import org.springblade.mdm.flow.constants.FlowContants;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.service.FlowCommonService;
import org.springblade.mdm.flow.service.FlowProgramFileService;
import org.springblade.mdm.flow.vo.TaskAssignVO;
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  {
    private final RuntimeService runtimeService;
 
    private final IdentityService identityService;
    private final NcNodeService ncNodeService;
 
    private final FlowProgramFileService flowProgramFileService;
    private final NodeDeptQueryService nodeDeptQueryService;
    private final FlowCommonService flowCommonService;
 
    /**
     * 启动派工流程
     * @param startVO 表单数据
     * @return 流程实例id
     */
    @Transactional
    public String start(TaskAssignVO startVO){
        if(!FlowContants.Y.equals(startVO.getIsTempFlow())){
            startVO.setIsTempFlow(FlowContants.N);
        }
        Map<String, Object> vars = new HashMap<>();
 
        ProduceDivision div = flowCommonService.putDefaultAssignees(vars,startVO.getDrawingNo(),null);
 
        vars.put(FlowContants.TITLE,startVO.getTitle());
 
        //机床编号
        vars.put(FlowContants.MACHINE_CODE,startVO.getMachineCode());
        String workshop = nodeDeptQueryService.getWorkshopNameByMachineCode(startVO.getMachineCode());
        vars.put(FlowContants.WORKSHOP,workshop);
        //机床型号
        vars.put(FlowContants.MACHINE_MODE,startVO.getMachineMode());
 
        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(FlowContants.DRAWING_NO,startVO.getDrawingNo());
        vars.put(FlowContants.DRAWING_NO_EDITION,startVO.getDrawingNoEdition());
        vars.put("planLockDays",startVO.getPlanLockDays());
        vars.put(FlowContants.PRODUCT_MODEL,startVO.getProductModel());
 
        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);
 
        vars.put(FlowContants.IS_TEMP_FLOW,FlowContants.Y.equals(startVO.getIsTempFlow())?FlowContants.Y:FlowContants.N);
        NcNode curedProgramPackage = null;
        if(!startVO.isTemporaryFlow() && !startVO.isDeviationFlow()) {
            //正常流程(非临时流程) 且不是 偏离单,才匹配固化程序
            curedProgramPackage = ncNodeService.getLastCuredProgramPackageWithoutProcessEdition(programPkgName,startVO.getMachineCode());
            //设置是否有固化程序标记
            vars.put(FlowContants.HAS_CURED_PROGRAM, curedProgramPackage != null ? FlowContants.Y : FlowContants.N);
            //工序版次是否一致,是否在有效期内
            if (curedProgramPackage != null) {
                vars.put(FlowContants.CURED_NODE_ID, curedProgramPackage.getId());
                vars.put(FlowContants.CURED_LOCKED, curedProgramPackage.hasLocked()?FlowContants.Y:FlowContants.N);
                vars.put(FlowContants.IS_PROCESS_EDITION_SAME, StringUtils.equals(curedProgramPackage.getProcessEdition(), startVO.getProcessEdition()) ? FlowContants.Y : FlowContants.N);
                vars.put("curedProcessEdition", curedProgramPackage.getProcessEdition());
                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);
            }
 
        }else{
            //临时流程/偏离单,不匹配固化程序
            vars.put(FlowContants.HAS_CURED_PROGRAM,FlowContants.N);
        }
        String myProcessName = "试切下发流程";
        if(vars.getOrDefault(FlowContants.IS_TEMP_FLOW,FlowContants.N).equals(FlowContants.Y)){
            myProcessName = "试切补充流程";
        }else if(StringUtils.isNotBlank(startVO.getDeviation()) ){
            myProcessName = "偏离单";
        }else if(vars.getOrDefault(FlowContants.HAS_CURED_PROGRAM,FlowContants.N).equals(FlowContants.Y)){
            myProcessName = "固化下发流程";
        }
 
        vars.put(FlowContants.MY_PROCESS_NAME, myProcessName);
 
        String businessKey = "0";//业务表key
        identityService.setAuthenticatedUserId(String.valueOf(AuthUtil.getUserId()));//设置流程发起人
        ProcessInstance inst;
        if(startVO.isTemporaryFlow()){
            vars.put(FlowContants.ASSIGNEE,div.getProgrammerId());//第一个用户编制上传文件
            inst = runtimeService.startProcessInstanceByKey(FlowContants.APPEND_PROCESS_KEY,businessKey,vars);
        }else{
            vars.put(FlowContants.ASSIGNEE,div.getTeamLeaderId());//第一个用户组长
            inst = runtimeService.startProcessInstanceByKey(FlowContants.TRY_PROCESS_KEY,businessKey,vars);
        }
        if(curedProgramPackage != null) {//存在已固化程序,复制原来的程序文件。
            copyFlowProgramFiles(curedProgramPackage.getId(),inst.getProcessInstanceId());
            curedProgramPackage.setProcessInstanceId(inst.getProcessInstanceId());
            ncNodeService.updateById(curedProgramPackage);
        }
 
 
        return inst.getProcessInstanceId();
    }
 
 
    /**
     * 复制流程程序文件
     * @param processInstanceId 新启动的流程id
     */
    private void copyFlowProgramFiles(long packageNameNodeId,String processInstanceId) {
        List<Long> fileIds = ncNodeService.getProgramFilesByPackageId(packageNameNodeId).stream().map(NcNode::getFlowProgramFileId).toList();
        if(!fileIds.isEmpty()) {
            List<FlowProgramFile> fileList = flowProgramFileService.lambdaQuery().in(FlowProgramFile::getId, fileIds).list();
 
            for (FlowProgramFile file : fileList) {
                FlowProgramFile newFile = new FlowProgramFile();
                BeanUtils.copyProperties(file, newFile);
                EntityUtil.clearBaseProperties(newFile);
 
                newFile.setProcessInstanceId(processInstanceId);
 
                this.flowProgramFileService.save(newFile);
            }
        }
    }
 
 
}