yangys
2025-08-09 264222fc237374a5544b8afe5ab49fbe0184f473
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
package org.springblade.mdm.flow.service;
 
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.IdentityService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.simpleframework.xml.core.Replace;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.mdm.basesetting.machine.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.flow.constants.FlowContants;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.entity.ReplaceProgramFile;
import org.springblade.mdm.flow.vo.ReplaceFlowStartVO;
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.utils.EntityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@AllArgsConstructor
@Service
public class ReplaceFlowService {
    private final NcNodeService nodeService;
    private final TaskService taskService;
    private final RuntimeService runtimeService;
    private final IdentityService identityService;
    private final FlowProgramFileService flowProgramFileService;
    private final ReplaceProgramFileService replaceProgramFileService;
    private final MachineService machineService;
 
    public static final String NODE_ID = "nodeId";
    /**
     * 转派,并记录自己的备注信息
     * @param nodeId 替换的节点id
     */
    @Transactional
    public void pre(long nodeId,String tempInstanceId) {
        List<NcNode> fileNodes = nodeService.lambdaQuery()
            .eq(NcNode::getParentId, nodeId)
            .eq(NcNode::getIsLastEdition,1)
            .eq(NcNode::getNodeType,NcNode.TYPE_PROGRAM_FILE).list();
        List<Long> fileIds = fileNodes.stream().map(NcNode::getFlowProgramFileId).toList();
 
        List<FlowProgramFile> programFiles = flowProgramFileService.lambdaQuery()
            .in(FlowProgramFile::getId, fileIds).list();
 
 
        //将现有文件复制到独立的表中,后续作为审批的文件,审批界面调用的接口也不同,最好单独开发
        for(FlowProgramFile programFile : programFiles) {
            /*
            ReplaceProgramFile replaceProgramFile = new ReplaceProgramFile();
            BeanUtils.copyProperties(programFile, replaceProgramFile);
            EntityUtil.clearBaseProperties(replaceProgramFile);
            replaceProgramFile.setTempId(tempId);
 
            replaceProgramFileService.save(replaceProgramFile);
 
             */
            FlowProgramFile newFile = new FlowProgramFile();
            BeanUtils.copyProperties(programFile, newFile);
            EntityUtil.clearBaseProperties(newFile);
            newFile.setProcessInstanceId(tempInstanceId);
            flowProgramFileService.save(newFile);
        }
    }
 
    /**
     * 启动替换流程
     */
    @Transactional
    public void start(ReplaceFlowStartVO startVO){
 
        Map<String, Object> vars = new HashMap<>();
        vars.put(FlowContants.ASSIGNEE,startVO.getAssignee());//第一个审批用户
 
        vars.put(FlowContants.TITLE,startVO.getTitle());
        NcNode programPackge = nodeService.getById(startVO.getNodeId());
        //机床编号
        vars.put(FlowContants.MACHINE_CODE,programPackge.getMachineCode());
        //Machine machine = machineService.getByCode(programPackge.getMachineCode());
        //机床型号
        //if(machine != null) {
            //vars.put(FlowContants.MACHINE_MODE, programPackge.getMachineMode());
        //}
        vars.put(FlowContants.PROCESS_NO,programPackge.getProcessNo());
        vars.put(FlowContants.PROCESS_NAME,programPackge.getProcessName());
        vars.put(FlowContants.PROCESS_EDITION,programPackge.getProcessEdition());
        vars.put(FlowContants.CRAFT_EDITION,programPackge.getCraftEdition());
 
        vars.put(FlowContants.DRAWING_NO,programPackge.getDrawingNo());
        vars.put(FlowContants.DRAWING_NO_EDITION,programPackge.getDrawingNoEdition());
 
        vars.put(FlowContants.PRODUCT_MODEL,programPackge.getProductModel());
        vars.put(NODE_ID, startVO.getNodeId());
        identityService.setAuthenticatedUserId(String.valueOf(AuthUtil.getUserId()));//设置流程发起人
        ProcessInstance inst = runtimeService.startProcessInstanceByKey(FlowContants.REPLACE_PROCESS_KEY,startVO.getNodeId()+"",vars);
        //replaceProgramFileService.lambdaUpdate().eq(ReplaceProgramFile::getTempId,startVO.getTempId()).set(ReplaceProgramFile::getProcessInstanceId,inst.getProcessInstanceId());
 
        flowProgramFileService.lambdaUpdate()
            .eq(FlowProgramFile::getProcessInstanceId,startVO.getTempInstanceId())
            .set(FlowProgramFile::getProcessInstanceId,inst.getProcessInstanceId()).update();
 
        /*
        List<ReplaceProgramFile> repFiles = replaceProgramFileService.lambdaQuery().eq(ReplaceProgramFile::getTempId,startVO.getTempId()).list();
 
        for(ReplaceProgramFile repFile : repFiles) {
            FlowProgramFile flowProgramFile = new FlowProgramFile();
            BeanUtils.copyProperties(repFile, flowProgramFile);
            flowProgramFile.setProgramName(programPackge.getName());
 
            EntityUtil.clearBaseProperties(flowProgramFile);
            flowProgramFile.setProcessInstanceId(inst.getId());
            flowProgramFile.setIsCured(0);
 
            flowProgramFileService.save(flowProgramFile);
        }
 
        //暂时注释,测试方便
        replaceProgramFileService.deleteByTempId(startVO.getTempId());
 
         */
        //return inst.getProcessInstanceId();
    }
 
}