| | |
| | | package org.springblade.mdm.flow.service.execute; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.flowable.engine.RuntimeService; |
| | | import org.flowable.engine.TaskService; |
| | | import org.flowable.engine.runtime.ProcessInstance; |
| | | import org.flowable.task.api.Task; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.support.Kv; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springblade.mdm.flow.constants.FlowContants; |
| | | import org.springblade.mdm.flow.service.ApproveRecordService; |
| | | import org.springblade.mdm.flow.service.FlowProgramFileService; |
| | | import org.springblade.mdm.program.service.NcNodeAutoCreateService; |
| | | 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.service.FlowCommonService; |
| | | import org.springblade.mdm.flow.vo.BatchDispatchVO; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | */ |
| | | @Service |
| | | public class BatchDispatchService { |
| | | |
| | | @Autowired |
| | | private ApproveRecordService approveRecordService; |
| | | @Autowired |
| | | private TaskService taskService; |
| | | @Autowired |
| | | private RuntimeService runtimeService; |
| | | @Autowired |
| | | private FlowCommonService flowCommonService; |
| | | @Autowired |
| | | private UserCommonService userCommonService; |
| | | @Transactional |
| | | public void batchCompleteTask(String[] taskIds, String[] processInstanceIds, String comment,String assignee) { |
| | | if(StringUtils.isBlank(assignee)){ |
| | | public void batchDispatchTask(BatchDispatchVO batchDispatchVO) { |
| | | //String[] taskIds, String[] processInstanceIds, String comment, String assignee |
| | | if(StringUtils.isBlank(batchDispatchVO.getAssignee())){ |
| | | throw new ServiceException("缺少处理人参数"); |
| | | } |
| | | checkBatchParameter(batchDispatchVO); |
| | | |
| | | Map<String, Object> variables = Kv.create(); |
| | | variables.put("assignee", batchDispatchVO.getAssignee()); |
| | | variables.put("approve", FlowConstant.Y);//默认就是通过 |
| | | String taskId; |
| | | String processInstanceId; |
| | | String comment = batchDispatchVO.getComment(); |
| | | String[] taskIds = batchDispatchVO.getTaskIds(); |
| | | for(int i=0;i<taskIds.length;i++){ |
| | | taskId = taskIds[i]; |
| | | processInstanceId = batchDispatchVO.getProcessInstanceIds()[i]; |
| | | |
| | | runtimeService.setVariable(processInstanceId, FlowConstant.PROGRAMMER_NAME,userCommonService.getUserNameById(Func.toLong(batchDispatchVO.getAssignee()))); |
| | | |
| | | Task task = getTask(taskId); |
| | | |
| | | if(!task.getTaskDefinitionKey().equals("teamLeaderTask")){ |
| | | throw new ServiceException("非[任务分派]节点的任务不能批量派工"); |
| | | } |
| | | if (StringUtil.isNoneBlank(processInstanceId, comment)) { |
| | | taskService.addComment(taskId, processInstanceId, comment); |
| | | } |
| | | |
| | | taskService.complete(taskId, variables); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 自动批量派工 |
| | | * @param batchDispatchVO 批量处理参数 |
| | | */ |
| | | @Transactional |
| | | public void batchAutoDispatchTask(BatchDispatchVO batchDispatchVO) { |
| | | //TODO 待实现 |
| | | checkBatchParameter(batchDispatchVO); |
| | | String[] taskIds = batchDispatchVO.getTaskIds(); |
| | | String taskId; |
| | | String processInstanceId; |
| | | |
| | | Map<String, Object> variables = Kv.create(); |
| | | //variables.put("assignee", batchDispatchVO.getAssignee()); |
| | | variables.put("approve", FlowConstant.Y);//自动分派默认就是通过 |
| | | variables.put(FlowVariableConstant.COMMENT,batchDispatchVO.getComment()); |
| | | ProcessInstance processInstance; |
| | | Object programmer; |
| | | for(int i=0;i<taskIds.length;i++) { |
| | | taskId = taskIds[i]; |
| | | processInstanceId = batchDispatchVO.getProcessInstanceIds()[i]; |
| | | |
| | | Task task = getTask(taskId); |
| | | if(!task.getTaskDefinitionKey().equals("teamLeaderTask")){ |
| | | throw new ServiceException("非[任务分派]节点的任务不能批量派工"); |
| | | } |
| | | |
| | | Object programmerId = getAutoProgrammer(processInstanceId); |
| | | variables.put("assignee", programmerId); |
| | | |
| | | runtimeService.setVariable(processInstanceId, FlowConstant.PROGRAMMER_NAME,userCommonService.getUserNameById(Func.toLong(programmerId))); |
| | | |
| | | |
| | | if (StringUtil.isNoneBlank(processInstanceId, batchDispatchVO.getComment())) { |
| | | taskService.addComment(taskId, processInstanceId, batchDispatchVO.getComment()); |
| | | } |
| | | |
| | | //最后一步完成 |
| | | taskService.complete(taskId, variables); |
| | | } |
| | | } |
| | | |
| | | private Task getTask(String taskId) { |
| | | return taskService.createTaskQuery() |
| | | .taskId(taskId) |
| | | .singleResult(); |
| | | } |
| | | |
| | | /** |
| | | * 获取编程的工艺员id |
| | | * @param processInstanceId |
| | | * @return |
| | | */ |
| | | Object getAutoProgrammer(String processInstanceId){ |
| | | ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables() |
| | | .processInstanceId(processInstanceId) |
| | | .singleResult(); |
| | | Object programmer = processInstance.getProcessVariables().get(FlowVariableConstant.PROGRAMMER); |
| | | if(programmer == null){ |
| | | throw new ServiceException(processInstance.getProcessVariables().get(FlowVariableConstant.TITLE)+":未找到编程人员"); |
| | | } |
| | | return programmer; |
| | | } |
| | | |
| | | /** |
| | | * 高师批量审批 |
| | | * @param batchDispatchVO |
| | | */ |
| | | @Transactional |
| | | public void batchApprove(BatchDispatchVO batchDispatchVO) { |
| | | checkBatchParameter(batchDispatchVO); |
| | | String[] taskIds = batchDispatchVO.getTaskIds(); |
| | | String taskId; |
| | | String processInstanceId; |
| | | |
| | | //审批结果和处理人都一样的 |
| | | Map<String, Object> variables = Kv.create(); |
| | | variables.put(FlowVariableConstant.APPROVE, batchDispatchVO.getApprove()); |
| | | variables.put(FlowVariableConstant.COMMENT, batchDispatchVO.getComment()); |
| | | |
| | | for(int i=0;i<taskIds.length;i++) { |
| | | taskId = taskIds[i]; |
| | | processInstanceId = batchDispatchVO.getProcessInstanceIds()[i]; |
| | | |
| | | Task task = getTask(taskId); |
| | | |
| | | if (StringUtil.isNoneBlank(processInstanceId, batchDispatchVO.getComment())) { |
| | | taskService.addComment(taskId, processInstanceId, batchDispatchVO.getComment()); |
| | | } |
| | | if(FlowConstant.N.equals(batchDispatchVO.getApprove())) { |
| | | //驳回,查询实际编程人员 驳回 |
| | | variables.put(FlowVariableConstant.ASSIGNEE, getActualProgrammer(processInstanceId)); |
| | | } |
| | | |
| | | taskService.complete(taskId, variables); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取流程执行中实际的编程人员(执行上传文件的人) |
| | | * @param processInstanceId 流程实例id |
| | | * @return 编程人员id |
| | | */ |
| | | Object getActualProgrammer(String processInstanceId){ |
| | | ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().includeProcessVariables() |
| | | .processInstanceId(processInstanceId) |
| | | .singleResult(); |
| | | Object actProgrammer = processInstance.getProcessVariables().get(FlowVariableConstant.ACT_PROGRAMMER); |
| | | if(actProgrammer == null){ |
| | | throw new ServiceException(processInstance.getProcessVariables().get(FlowVariableConstant.TITLE)+":未找到编程人员"); |
| | | } |
| | | return actProgrammer; |
| | | } |
| | | /** |
| | | * 验证批量处理参数 |
| | | * @param batchDispatchVO 批量处理参数对象 |
| | | */ |
| | | void checkBatchParameter(BatchDispatchVO batchDispatchVO){ |
| | | String[] taskIds = batchDispatchVO.getTaskIds(); |
| | | if(taskIds == null || taskIds.length == 0){ |
| | | throw new ServiceException("任务id组为空"); |
| | | } |
| | | |
| | | String[] processInstanceIds = batchDispatchVO.getProcessInstanceIds(); |
| | | if(processInstanceIds == null || processInstanceIds.length == 0){ |
| | | throw new ServiceException("流程实例id组为空"); |
| | | } |
| | | if(taskIds.length != processInstanceIds.length){ |
| | | throw new ServiceException("任务id与流程实例id数量不一致"); |
| | | } |
| | | |
| | | Map<String, Object> variables = Kv.create(); |
| | | variables.put("assignee", assignee); |
| | | variables.put("approve", FlowContants.Y);//默认就是通过 |
| | | String taskId; |
| | | String processInstanceId; |
| | | for(int i=0;i<taskIds.length;i++){ |
| | | taskId = taskIds[i]; |
| | | processInstanceId = processInstanceIds[i]; |
| | | |
| | | Task task = taskService.createTaskQuery() |
| | | .taskId(taskId) |
| | | .singleResult(); |
| | | taskService.complete(taskId, variables); |
| | | |
| | | if (StringUtil.isNoneBlank(processInstanceId, comment)) { |
| | | taskService.addComment(taskId, processInstanceId, comment); |
| | | } |
| | | |
| | | approveRecordService.saveApproveRecords(task,FlowContants.Y,comment); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 自动批量派工 |
| | | * @param taskIds |
| | | * @param processInstanceIds |
| | | * @param comment |
| | | * 排练通过现场编制 |
| | | * @param batchDispatchVO |
| | | */ |
| | | public void batchAutoCompleteTask(String[] taskIds, String[] processInstanceIds, String comment) { |
| | | //TODO 带实现 |
| | | } |
| | | public void batchProgramOnMachine(BatchDispatchVO batchDispatchVO) { |
| | | checkBatchParameter(batchDispatchVO); |
| | | String[] taskIds = batchDispatchVO.getTaskIds(); |
| | | String taskId; |
| | | String processInstanceId; |
| | | |
| | | //审批结果和处理人都一样的 |
| | | Map<String, Object> variables = Kv.create(); |
| | | variables.put(FlowVariableConstant.APPROVE, FlowConstant.Y); |
| | | variables.put(FlowVariableConstant.COMMENT, batchDispatchVO.getComment()); |
| | | variables.put(FlowVariableConstant.PROGRAM_ON_MACHINE, FlowConstant.Y);//现场编制:是 |
| | | variables.put(FlowVariableConstant.ACT_PROGRAMMER, AuthUtil.getUserId()+"");//实际编制 |
| | | variables.put(FlowVariableConstant.ASSIGNEE,batchDispatchVO.getAssignee()); |
| | | for(int i=0;i<taskIds.length;i++) { |
| | | taskId = taskIds[i]; |
| | | processInstanceId = batchDispatchVO.getProcessInstanceIds()[i]; |
| | | |
| | | Task task = getTask(taskId); |
| | | if(!task.getTaskDefinitionKey().equals("programmingTask")){ |
| | | throw new ServiceException("非编制任务,不能指定现场编制"); |
| | | } |
| | | if (StringUtil.isNoneBlank(processInstanceId, batchDispatchVO.getComment())) { |
| | | taskService.addComment(taskId, processInstanceId, batchDispatchVO.getComment()); |
| | | } |
| | | |
| | | taskService.complete(taskId, variables); |
| | | } |
| | | } |
| | | } |