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.program.vo.NcNodeVO; 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.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 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 prestart(long nodeId,String tempInstanceId) { List fileNodes = nodeService.lambdaQuery() .eq(NcNode::getParentId, nodeId) .eq(NcNode::getIsLastEdition,1) .eq(NcNode::getNodeType,NcNode.TYPE_PROGRAM_FILE).list(); List fileIds = fileNodes.stream().map(NcNode::getFlowProgramFileId).toList(); List 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 fileNodes = nodeService.lambdaQuery() .eq(NcNode::getParentId, nodeId) .eq(NcNode::getIsLastEdition,1) .eq(NcNode::getNodeType,NcNode.TYPE_PROGRAM_FILE).list(); List fileIds = fileNodes.stream().map(NcNode::getFlowProgramFileId).toList(); if(fileIds.isEmpty()){ throw new RuntimeException("未找到程序文件"); } List 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); return vo; } /** * 启动替换流程 */ @Transactional public void start(ReplaceFlowStartVO startVO){ Map 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()); 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 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(); } }