yangys
2025-08-12 552944268d5c1fccb2b47c2f6ee41ebac719da31
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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.log.exception.ServiceException;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
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.ReplaceUploadVO;
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.program.vo.NcNodeVO;
import org.springblade.mdm.utils.EntityUtil;
import org.springblade.mdm.utils.ProgramFileNameCheckUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.util.Arrays;
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 RuntimeService runtimeService;
    private final TaskService taskService;
    private final IdentityService identityService;
    private final FlowProgramFileService flowProgramFileService;
    private final ReplaceProgramFileService replaceProgramFileService;
    private final NodeDeptQueryService nodeDeptQueryService;
    private OssTemplate ossTemplate;
    public static final String NODE_ID = "nodeId";
    /**
     * 转派,并记录自己的备注信息
     * @param nodeId 替换的节点id
     */
    /*
    @Transactional
    public void prestart(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) {
            FlowProgramFile newFile = new FlowProgramFile();
            BeanUtils.copyProperties(programFile, newFile);
            EntityUtil.clearBaseProperties(newFile);
            newFile.setProcessInstanceId(tempInstanceId);
            flowProgramFileService.save(newFile);
        }
    }
 
     */
 
    @Transactional
    public NcNodeVO 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();
        if(fileIds.isEmpty()){
            throw new RuntimeException("未找到程序文件");
        }
        List<FlowProgramFile> programFiles = flowProgramFileService.lambdaQuery()
            .in(FlowProgramFile::getId, fileIds).list();
 
 
        //将现有文件复制到独立的表中,后续作为审批的文件,审批界面调用的接口也不同,最好单独开发
        for(FlowProgramFile programFile : programFiles) {
            FlowProgramFile newFile = new FlowProgramFile();
            BeanUtils.copyProperties(programFile, newFile);
            EntityUtil.clearBaseProperties(newFile);
            newFile.setProcessInstanceId(tempInstanceId);
            flowProgramFileService.save(newFile);
        }
 
        NcNode node = nodeService.getById(nodeId);
        NcNodeVO vo = new NcNodeVO();
        BeanUtils.copyProperties(node, vo);
        vo.setProcessInstanceId(tempInstanceId);
 
        vo.setWorkshop(nodeDeptQueryService.getWorkshopNameByMachineCode(node.getMachineCode()));
 
        return vo;
    }
 
    /**
     * 启动替换流程
     */
    @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 programPackage = nodeService.getById(startVO.getNodeId());
        //机床编号
        vars.put(FlowContants.MACHINE_CODE,programPackage.getMachineCode());
        //Machine machine = machineService.getByCode(programPackge.getMachineCode());
        //机床型号
        //if(machine != null) {
            //vars.put(FlowContants.MACHINE_MODE, programPackge.getMachineMode());
        //}
        vars.put(FlowContants.PROCESS_NO,programPackage.getProcessNo());
        vars.put(FlowContants.PROCESS_NAME,programPackage.getProcessName());
        vars.put(FlowContants.PROCESS_EDITION,programPackage.getProcessEdition());
        vars.put(FlowContants.CRAFT_EDITION,programPackage.getCraftEdition());
 
        vars.put(FlowContants.DRAWING_NO,programPackage.getDrawingNo());
        vars.put(FlowContants.DRAWING_NO_EDITION,programPackage.getDrawingNoEdition());
 
        vars.put(FlowContants.PROGRAM_PACKAGE_NAME,programPackage.getName());
 
        vars.put(FlowContants.PRODUCT_MODEL,programPackage.getProductModel());
        vars.put(NODE_ID, startVO.getNodeId());
 
        vars.put("comment", startVO.getComment());
        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());
 
        /*
        Task startTask = taskService.createTaskQuery()
            .processInstanceId(inst.getId())
            .singleResult();
 
        // 添加评论
        taskService.addComment(startTask.getId(), inst.getProcessInstanceId(),startVO.getComment());
        */
        flowProgramFileService.lambdaUpdate()
            .eq(FlowProgramFile::getProcessInstanceId,startVO.getTempInstanceId())
            .set(FlowProgramFile::getProcessInstanceId,inst.getProcessInstanceId()).update();
 
    }
 
 
    /**
     * 上传程序徐文件(编制节点调用)
     * @param uploadVO 上传对象
     */
    public void uploadReplaceProgramFile(ReplaceUploadVO uploadVO) {
 
        //FlowProgramProperties progProps= flowCommonService.getProgramProperties(uploadVO.getProcessInstanceId());
        MultipartFile file = uploadVO.getFile();
        if(file.getSize() == 0){
            throw new ServiceException("程序文件不可为空文件");
        }
        FlowProgramProperties progProps = new FlowProgramProperties();
        progProps.setDrawingNo(uploadVO.getDrawingNo());
        progProps.setProcessNo(uploadVO.getProcessNo());
        progProps.setProcessEdition(uploadVO.getProcessEdition());
 
        ProgramFileNameCheckUtil.checkFilename(file.getOriginalFilename(),progProps);
 
        String programName = NcNodeService.genProgramName(progProps.getDrawingNo(),progProps.getProcessNo());
 
        BladeFile bfile = ossTemplate.putFile(file);
 
        FlowProgramFile progFile = new FlowProgramFile();
        progFile.setName(file.getOriginalFilename());
        progFile.setOssName(bfile.getName());
        progFile.setProcessInstanceId(uploadVO.getProcessInstanceId());
        progFile.setIsCured(0);
        progFile.setProgramName(programName);
 
        flowProgramFileService.save(progFile);
 
    }
}