| | |
| | | package org.springblade.mdm.flow.excution; |
| | | |
| | | import io.swagger.v3.oas.annotations.media.Schema; |
| | | import org.flowable.common.engine.impl.identity.Authentication; |
| | | import lombok.AllArgsConstructor; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.flowable.engine.IdentityService; |
| | | import org.flowable.engine.runtime.ProcessInstance; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.flow.core.feign.IFlowClient; |
| | | import org.springblade.flow.core.pojo.entity.BladeFlow; |
| | | import org.springblade.flow.core.utils.TaskUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.basesetting.machine.service.MachineService; |
| | | 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.FlowConstant; |
| | | import org.springblade.mdm.flow.constants.FlowVariableConstant; |
| | | 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.FlowStartResult; |
| | | import org.springblade.mdm.flow.vo.TaskAssignVO; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.service.ProgramSeqService; |
| | | import org.springblade.mdm.utils.EntityUtil; |
| | | import org.springframework.beans.BeanUtils; |
| | | 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 ProgramSeqService programSeqService; |
| | | private final IdentityService identityService; |
| | | private final NcNodeService ncNodeService; |
| | | private final MachineService machineService; |
| | | private final FlowProgramFileService flowProgramFileService; |
| | | private final NodeDeptQueryService nodeDeptQueryService; |
| | | private final FlowCommonService flowCommonService; |
| | | private final ProduceDivisionService produceDivisionService; |
| | | private final UserCommonService userCommonService; |
| | | /** |
| | | * 启动派工流程 |
| | | * @param startVO 表单数据 |
| | | * @return 流程实例id |
| | | */ |
| | | @Transactional |
| | | public FlowStartResult start(TaskAssignVO startVO){ |
| | | if(!FlowConstant.Y.equals(startVO.getIsTempFlow())){ |
| | | startVO.setIsTempFlow(FlowConstant.N); |
| | | } |
| | | Map<String, Object> vars = new HashMap<>(); |
| | | |
| | | @Autowired |
| | | private RuntimeService runtimeService; |
| | | @Autowired |
| | | private AutoAssignUsersService autoAssignUsersService; |
| | | @Autowired |
| | | private IdentityService identityService; |
| | | private static final String PROCESS_KEY = "dispatch"; |
| | | ProduceDivision div; |
| | | if(startVO.getProducePlanId() != null && startVO.getProducePlanId()!=0){ |
| | | //界面上选择了分工表的情况,直接根据id获取 |
| | | div = produceDivisionService.getById(startVO.getProducePlanId()); |
| | | vars.put(FlowVariableConstant.TEAM_LEADER,div.getTeamLeaderId()); |
| | | vars.put(FlowVariableConstant.PROGRAMMER,div.getProgrammerId()); |
| | | vars.put(FlowVariableConstant.CHECKER,div.getCheckerId()); |
| | | vars.put(FlowVariableConstant.SENIOR,div.getSeniorId()); |
| | | |
| | | public void start(TaskAssignVO startVO){ |
| | | vars.put(FlowConstant.PROGRAMMER_NAME, userCommonService.getUserNameById(div.getProgrammerId())); |
| | | |
| | | Map<String,Object> preAssignee = autoAssignUsersService.autoAssignUsers(startVO.getProducePlanId()); |
| | | Map<String, Object> vars = new HashMap<>(preAssignee); |
| | | }else { |
| | | div = flowCommonService.putDefaultAssignees(vars, startVO.getDrawingNo(), null); |
| | | } |
| | | vars.put(FlowVariableConstant.TITLE,startVO.getTitle()); |
| | | |
| | | vars.put("machineCode",startVO.getMachineCode()); |
| | | vars.put("machineMode",startVO.getMachineMode()); |
| | | addLastStepUserName(startVO,vars); |
| | | |
| | | vars.put("processNo",startVO.getProcessNo()); |
| | | vars.put("processName",startVO.getProcessName()); |
| | | vars.put("processEdition",startVO.getProcessEdition()); |
| | | //机床编号 |
| | | vars.put(FlowVariableConstant.MACHINE_CODE,startVO.getMachineCode()); |
| | | String workshop = nodeDeptQueryService.getWorkshopNameByMachineCode(startVO.getMachineCode()); |
| | | vars.put(FlowVariableConstant.WORKSHOP,workshop); |
| | | //机床型号 |
| | | |
| | | vars.put("craftEdition",startVO.getCraftEdition()); |
| | | Machine machine = machineService.getByCode(startVO.getMachineCode()); |
| | | if(machine == null){ |
| | | throw new ServiceException("错误的设备编码:"+startVO.getMachineCode()); |
| | | } |
| | | vars.put(FlowVariableConstant.MACHINE_MODE,machine.getName()); |
| | | vars.put(FlowVariableConstant.MACHINE_GROUP_CODE,machine.getMachineGroupCode()); |
| | | |
| | | vars.put(FlowVariableConstant.PROCESS_NO,startVO.getProcessNo()); |
| | | vars.put(FlowVariableConstant.PROCESS_NAME,startVO.getProcessName()); |
| | | vars.put(FlowVariableConstant.PROCESS_EDITION,startVO.getProcessEdition()); |
| | | vars.put(FlowVariableConstant.CRAFT_EDITION,startVO.getCraftEdition()); |
| | | vars.put("planStartTime",startVO.getPlanStartTime()); |
| | | vars.put("drawingNo",startVO.getDrawingNo()); |
| | | vars.put("drawingNoEdition",startVO.getDrawingNoEdition()); |
| | | vars.put(FlowVariableConstant.DRAWING_NO,startVO.getDrawingNo()); |
| | | vars.put(FlowVariableConstant.DRAWING_NO_EDITION,startVO.getDrawingNoEdition()); |
| | | vars.put("planLockDays",startVO.getPlanLockDays()); |
| | | vars.put(FlowVariableConstant.PRODUCT_MODEL,startVO.getProductModel()); |
| | | |
| | | //vars.put("producePlanId",startVO.getCraftEdition()); |
| | | if(StringUtils.isNotBlank(startVO.getDeviation())) { |
| | | vars.put(FlowVariableConstant.DEVIATION, startVO.getDeviation());//偏离单 |
| | | } |
| | | String programPkgName = NcNodeService.genProgramName(startVO.getDrawingNo(),startVO.getProcessNo(),startVO.getProcessEdition()); |
| | | vars.put(FlowVariableConstant.PROGRAM_PACKAGE_NAME,programPkgName); |
| | | |
| | | vars.put(FlowVariableConstant.IS_TEMP_FLOW, FlowConstant.Y.equals(startVO.getIsTempFlow())? FlowConstant.Y: FlowConstant.N); |
| | | NcNode curedProgramPackage = null; |
| | | if(!startVO.isTemporaryFlow() && !startVO.isDeviationFlow()) { |
| | | //正常流程(非临时流程) 且不是 偏离单,才匹配固化程序 |
| | | curedProgramPackage = ncNodeService.getLastCuredProgramWithoutProcessEdition(startVO.getDrawingNo(),startVO.getProcessNo(),startVO.getMachineCode()); |
| | | //设置是否有固化程序标记 |
| | | vars.put(FlowVariableConstant.HAS_CURED_PROGRAM, curedProgramPackage != null ? FlowConstant.Y : FlowConstant.N); |
| | | //工序版次是否一致,是否在有效期内 |
| | | if (curedProgramPackage != null) { |
| | | vars.put(FlowVariableConstant.CURED_NODE_ID, curedProgramPackage.getId()); |
| | | vars.put(FlowVariableConstant.CURED_LOCKED, curedProgramPackage.hasLocked()? FlowConstant.Y: FlowConstant.N); |
| | | vars.put(FlowVariableConstant.IS_PROCESS_EDITION_SAME, StringUtils.equals(curedProgramPackage.getProcessEdition(), startVO.getProcessEdition()) ? FlowConstant.Y : FlowConstant.N); |
| | | vars.put("curedProcessEdition", curedProgramPackage.getProcessEdition()); |
| | | vars.put(FlowVariableConstant.VALIDITY_PERIOD, curedProgramPackage.withinValidityPeriod() ? FlowConstant.Y : FlowConstant.N); |
| | | } else { |
| | | vars.put(FlowVariableConstant.IS_PROCESS_EDITION_SAME, FlowConstant.Y); |
| | | vars.put(FlowVariableConstant.VALIDITY_PERIOD, FlowConstant.Y); |
| | | } |
| | | |
| | | String businessKey = "0";//业务表key |
| | | }else{ |
| | | //临时流程/偏离单,不匹配固化程序 |
| | | vars.put(FlowVariableConstant.HAS_CURED_PROGRAM, FlowConstant.N); |
| | | } |
| | | String myProcessName = "试切下发流程"; |
| | | if(vars.getOrDefault(FlowVariableConstant.IS_TEMP_FLOW, FlowConstant.N).equals(FlowConstant.Y)){ |
| | | myProcessName = "试切补充流程"; |
| | | }else if(StringUtils.isNotBlank(startVO.getDeviation()) ){ |
| | | myProcessName = "临时更改单"; |
| | | }else if(vars.getOrDefault(FlowVariableConstant.HAS_CURED_PROGRAM, FlowConstant.N).equals(FlowConstant.Y)){ |
| | | myProcessName = "固化下发流程"; |
| | | } |
| | | |
| | | vars.put(FlowVariableConstant.MY_PROCESS_NAME, myProcessName); |
| | | |
| | | String businessKey = Func.toStr(startVO.getTaskDispatchId());//业务表key |
| | | identityService.setAuthenticatedUserId(String.valueOf(AuthUtil.getUserId()));//设置流程发起人 |
| | | ProcessInstance pinst = runtimeService.startProcessInstanceByKey(PROCESS_KEY,businessKey,vars); |
| | | ProcessInstance inst; |
| | | if(startVO.isTemporaryFlow()){ |
| | | vars.put(FlowVariableConstant.ASSIGNEE,div.getProgrammerId());//第一个用户编制上传文件 |
| | | vars.put(FlowConstant.PROGRAMMER_NAME,userCommonService.getUserNameById(div.getProgrammerId()));//编程员姓名 |
| | | |
| | | int a=1; |
| | | a =2; |
| | | //log.info() |
| | | inst = runtimeService.startProcessInstanceByKey(FlowConstant.APPEND_PROCESS_KEY,businessKey,vars); |
| | | }else{ |
| | | vars.put(FlowVariableConstant.ASSIGNEE,div.getTeamLeaderId());//第一个用户组长 |
| | | inst = runtimeService.startProcessInstanceByKey(FlowConstant.TRY_PROCESS_KEY,businessKey,vars); |
| | | } |
| | | FlowStartResult result = new FlowStartResult(); |
| | | result.setProcessInstanceId(inst.getProcessInstanceId()); |
| | | //生成程序编号 |
| | | String programNo = programSeqService.generageProgramNo(); |
| | | runtimeService.setVariable(inst.getProcessInstanceId(), FlowVariableConstant.PROGRAM_NO,programNo); |
| | | result.setProgramNo(programNo); |
| | | |
| | | if(curedProgramPackage != null) {//存在已固化程序,复制原来的程序文件。 |
| | | copyFlowProgramFiles(curedProgramPackage.getId(),inst.getProcessInstanceId()); |
| | | curedProgramPackage.setProcessInstanceId(inst.getProcessInstanceId()); |
| | | ncNodeService.updateById(curedProgramPackage); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 流程变量中加入上一步处理了人(发起人) |
| | | * @param startVO 启动vo |
| | | * @param vars 流程变量map |
| | | */ |
| | | private void addLastStepUserName(TaskAssignVO startVO, Map<String, Object> vars) { |
| | | if(startVO.getDispatchBy() != null) { |
| | | vars.put(FlowVariableConstant.LAST_STEP_USER_NICKNAME, startVO.getDispatchBy()); |
| | | }else{ |
| | | if(startVO.getCreateUser() != null) { |
| | | vars.put(FlowVariableConstant.LAST_STEP_USER_NICKNAME, this.userCommonService.getUserNameById(startVO.getCreateUser())); |
| | | }else{ |
| | | vars.put(FlowVariableConstant.LAST_STEP_USER_NICKNAME,"MES系统"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 复制流程程序文件 |
| | | * @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); |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | } |