package org.springblade.mdm.flow.service; import com.alibaba.excel.util.StringUtils; import lombok.AllArgsConstructor; import org.flowable.engine.HistoryService; import org.flowable.engine.RuntimeService; import org.flowable.engine.history.HistoricProcessInstance; import org.flowable.engine.runtime.ProcessInstance; import org.flowable.task.api.history.HistoricTaskInstance; import org.springblade.core.tool.api.R; import org.springblade.core.tool.utils.Func; import org.springblade.mdm.basesetting.machine.service.MachineService; import org.springblade.mdm.basesetting.machine.entity.Machine; import org.springblade.mdm.basesetting.producedivision.entity.ProduceDivision; import org.springblade.mdm.basesetting.producedivision.service.ProduceDivisionService; import org.springblade.mdm.commons.service.UserCommonService; import org.springblade.mdm.flow.constants.FlowContants; import org.springblade.mdm.flow.constants.FlowVariableContants; import org.springblade.mdm.program.entity.NcNode; import org.springblade.mdm.program.service.NodeDeptQueryService; import org.springblade.system.feign.IUserClient; import org.springblade.system.pojo.entity.User; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @AllArgsConstructor @Service public class FlowCommonService { private final RuntimeService runtimeService; private final HistoryService historyService; private final ProduceDivisionService produceDivisionService; private final NodeDeptQueryService nodeDeptQueryService; private final MachineService machineService; private final UserCommonService userCommonService; /** * 根据流程实例id获取definitionKey * @param processInstanceId * @return */ public String getDefinitionKey(String processInstanceId){ ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); return processInstance.getProcessDefinitionKey(); } /** * 从流程变量获取发起流程的树形 * @param processInstanceId * @return */ public FlowProgramProperties getProgramProperties(String processInstanceId){ FlowProgramProperties programProperties = new FlowProgramProperties(); programProperties.setProcessInstanceId(processInstanceId); Map vars = runtimeService.getVariables(processInstanceId); this.copyFlowPropertiesFromMap(programProperties, vars); ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); String processDefinitionKey = processInstance.getProcessDefinitionKey(); programProperties.setProcessDefinitionKey(processDefinitionKey); return programProperties; } public FlowProgramProperties getProgramPropertiesFromHis(String processInstanceId) { HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery().includeProcessVariables() .processInstanceId(processInstanceId) .singleResult(); FlowProgramProperties programProperties = new FlowProgramProperties(); programProperties.setProcessInstanceId(processInstanceId); Map vars =historicInstance.getProcessVariables(); this.copyFlowPropertiesFromMap(programProperties,vars); programProperties.setProcessDefinitionKey(historicInstance.getProcessDefinitionKey()); return programProperties; } /** * 从map中赋值流程便利到vo * @param programProperties vo * @param vars 变量map */ void copyFlowPropertiesFromMap(FlowProgramProperties programProperties,Map vars){ programProperties.setTitle(String.valueOf(vars.get(FlowVariableContants.TITLE))); programProperties.setIsTempFlow(Func.toStr(vars.get(FlowVariableContants.IS_TEMP_FLOW))); programProperties.setDrawingNo(String.valueOf(vars.get(FlowVariableContants.DRAWING_NO))); programProperties.setDrawingNoEdition(String.valueOf(vars.get(FlowVariableContants.DRAWING_NO_EDITION))); programProperties.setProductModel(String.valueOf(vars.get(FlowVariableContants.PRODUCT_MODEL))); programProperties.setProcessNo(String.valueOf(vars.get(FlowVariableContants.PROCESS_NO))); programProperties.setProcessEdition(String.valueOf(vars.get(FlowVariableContants.PROCESS_EDITION)));//工序版次 programProperties.setProcessName(String.valueOf(vars.get(FlowVariableContants.PROCESS_NAME))); programProperties.setMachineCode(String.valueOf(vars.get(FlowVariableContants.MACHINE_CODE))); programProperties.setMachineMode(String.valueOf(vars.get(FlowVariableContants.MACHINE_MODE))); //数据处理名称为版次以前,零件号加工序号:YZL4-1100-01-50 零组件号:YZL4-1100-01 工序号 50 programProperties.setCraftEdition(String.valueOf(vars.get(FlowVariableContants.CRAFT_EDITION))); programProperties.setHasCuredProgram(String.valueOf(vars.get(FlowVariableContants.HAS_CURED_PROGRAM))); programProperties.setIsProcessEditionSame(String.valueOf(vars.get(FlowVariableContants.IS_PROCESS_EDITION_SAME))); if(vars.containsKey(FlowVariableContants.DEVIATION)) { programProperties.setDeviation(String.valueOf(vars.get(FlowVariableContants.DEVIATION))); } if(vars.containsKey(FlowVariableContants.CURED_NODE_ID)){ programProperties.setCuredNodeId(Func.toLong(vars.get(FlowVariableContants.CURED_NODE_ID))); } if(vars.containsKey(FlowContants.NODE_ID)){ programProperties.setNodeId(Func.toLong(vars.get(FlowContants.NODE_ID))); } programProperties.setCureProgramUseable(String.valueOf(vars.get(FlowContants.CURE_PROGRAM_USEABLE))); programProperties.setTeamLeaderId(Func.toLong(vars.get(FlowVariableContants.TEAM_LEADER))); programProperties.setProgrammerId(Func.toLong(vars.get(FlowVariableContants.PROGRAMMER))); programProperties.setCheckerId(Func.toLong(vars.get(FlowVariableContants.CHECKER))); programProperties.setSeniorId(Func.toLong(vars.get(FlowVariableContants.SENIOR))); programProperties.setProgramNo(programProperties.getDrawingNo()+"-"+programProperties.getProcessNo()+"-"+programProperties.getProcessEdition()); programProperties.setActProgrammerId(Func.toLong(vars.get(FlowVariableContants.ACT_PROGRAMMER))); programProperties.setActCheckerId(Func.toLong(vars.get(FlowVariableContants.ACT_CHECKER))); programProperties.setActSeniorId(Func.toLong(vars.get(FlowVariableContants.ACT_SENIOR))); programProperties.setProgrammerName(Func.toStr(vars.get("programmerName"))); } /** * 流程是否在进行 * @param processInstanceId 实例id */ public boolean isProcessInstanceActive(String processInstanceId) { if(processInstanceId == null){ return false; } // 先查运行时表 ProcessInstance instance = runtimeService.createProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); if (instance != null) { return true; } // 再查历史表确认是否曾经存在 HistoricProcessInstance historicInstance = historyService.createHistoricProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult(); return historicInstance != null && historicInstance.getEndTime() == null; } /** * 上一步处理人 * @param processInstanceId 流程实例id * @return assignee */ /* public String previusAssignee(String processInstanceId) { // 获取历史任务实例 List historicTasks = historyService.createHistoricTaskInstanceQuery() .processInstanceId(processInstanceId) .orderByHistoricTaskInstanceEndTime().asc() .list(); // 获取上一个已完成的任务 String assignee = ""; if(!historicTasks.isEmpty()) { assignee = historicTasks.get(historicTasks.size() - 1).getAssignee(); } return assignee; }*/ /** * 将默认审批用户放入map中 * @param vars 启动流程的变量map * @param drawingNo 零组件号 */ public ProduceDivision putDefaultAssignees(Map vars,String drawingNo,String defaultAssignee){ ProduceDivision div = produceDivisionService.getByDrawingNoWithQinzhe(drawingNo); vars.put(FlowVariableContants.TEAM_LEADER,div.getTeamLeaderId()); vars.put(FlowVariableContants.PROGRAMMER,div.getProgrammerId()); vars.put(FlowVariableContants.CHECKER,div.getCheckerId()); vars.put(FlowVariableContants.SENIOR,div.getSeniorId()); if(StringUtils.isNotBlank(defaultAssignee)) { vars.put(FlowVariableContants.ASSIGNEE, defaultAssignee);//第一个审批用户:组长 } vars.put(FlowContants.PROGRAMMER_NAME, userCommonService.getUserNameById(div.getProgrammerId())); return div; } /** * 根据程序包名节点,填充变量map * @param vars * @param programPackageNode */ public void putFlowVariablesByNode(Map vars, NcNode programPackageNode){ //机床编号 vars.put(FlowVariableContants.MACHINE_CODE,programPackageNode.getMachineCode()); Machine machine = machineService.getByCode(programPackageNode.getMachineCode()); //机床型号 if(machine != null) { vars.put(FlowVariableContants.MACHINE_MODE, machine.getName()); } vars.put(FlowVariableContants.PROCESS_NO,programPackageNode.getProcessNo()); vars.put(FlowVariableContants.PROCESS_NAME,programPackageNode.getProcessName()); vars.put(FlowVariableContants.PROCESS_EDITION,programPackageNode.getProcessEdition()); vars.put(FlowVariableContants.CRAFT_EDITION,programPackageNode.getCraftEdition()); vars.put(FlowVariableContants.DRAWING_NO,programPackageNode.getDrawingNo()); vars.put(FlowVariableContants.DRAWING_NO_EDITION,programPackageNode.getDrawingNoEdition()); vars.put(FlowVariableContants.PROGRAM_PACKAGE_NAME,programPackageNode.getName()); vars.put(FlowVariableContants.PRODUCT_MODEL,programPackageNode.getProductModel()); vars.put(FlowContants.NODE_ID, programPackageNode.getId()); vars.put(FlowVariableContants.WORKSHOP,nodeDeptQueryService.getWorkshopNameByMachineCode(programPackageNode.getMachineCode())); } }