| | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import lombok.AllArgsConstructor; |
| | | import org.flowable.engine.HistoryService; |
| | | import org.flowable.engine.RuntimeService; |
| | | import org.flowable.engine.TaskService; |
| | | import org.flowable.engine.history.HistoricProcessInstance; |
| | | import org.flowable.engine.history.HistoricProcessInstanceQuery; |
| | | import org.flowable.engine.task.Comment; |
| | | import org.flowable.task.api.Task; |
| | | import org.flowable.task.api.TaskQuery; |
| | | import org.flowable.task.api.history.HistoricTaskInstance; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springblade.core.tool.utils.*; |
| | | |
| | | import org.springblade.mdm.basesetting.machine.MachineService; |
| | | import org.springblade.mdm.flow.entity.FlowProgramFile; |
| | | import org.springblade.mdm.flow.entity.MdmFlowProcess; |
| | | import org.springblade.mdm.flow.util.MdmFlowCache; |
| | | import org.springblade.mdm.flow.vo.FlowVO; |
| | | import org.springblade.mdm.program.service.NcProgramService; |
| | | import org.springblade.mdm.program.service.ProcessProgRefService; |
| | | import org.springblade.system.feign.IUserClient; |
| | | import org.springblade.system.pojo.entity.User; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Collections; |
| | | import java.util.Date; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class FlowBusinessService { |
| | | |
| | | private final RuntimeService runtimeService; |
| | | private final TaskService taskService; |
| | | private final HistoryService historyService; |
| | | private final IUserClient userClient; |
| | | |
| | | public IPage<FlowVO> selectTodoPage(IPage<FlowVO> page, LocalDate createTimeBegin, LocalDate createTimeEnd, String keyword) { |
| | | private final ProcessProgRefService processProgRefService; |
| | | private final ApproveRecordService approveRecordService; |
| | | private final NcProgramService ncProgramService; |
| | | private final FlowProgramFileService flowProgramFileService; |
| | | /** |
| | | * 查询我的流程(个人待办列表) |
| | | * @param page 分页信息 |
| | | * @param createTimeBegin 创建时间开始 |
| | | * @param createTimeEnd 创建时间截止 |
| | | * @param keyword 查询关键字 |
| | | * @return |
| | | */ |
| | | public IPage<FlowVO> selectTodoPage(IPage<FlowVO> page, LocalDateTime createTimeBegin, LocalDateTime createTimeEnd, String keyword) { |
| | | //String taskUser = TaskUtil.getTaskUser(); |
| | | String userId = "" + AuthUtil.getUserId(); |
| | | List<FlowVO> flowList = new LinkedList<>(); |
| | |
| | | TaskQuery todoQuery = taskService.createTaskQuery().taskAssignee(userId).active().includeProcessVariables(); |
| | | if (Func.isNotEmpty(createTimeBegin)) { |
| | | todoQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin)); |
| | | //如果查询实例的开始时间:todoQuery.taskInProgressStartDueAfter() |
| | | |
| | | ////如果查询实例的开始时间:只能用以下这个,先查出实例id来 |
| | | //todoQuery.processInstanceIdIn() |
| | | } |
| | | if (Func.isNotEmpty(createTimeEnd)) { |
| | | todoQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd)); |
| | | |
| | | //todoQuery.taskInProgressStartTimeBefore(DateUtil.toDate(createTimeEnd)); |
| | | } |
| | | if (Func.isNotEmpty(keyword)) { |
| | | addKeywordCondition(todoQuery, keyword); |
| | |
| | | |
| | | // 构建列表数据 |
| | | FlowVO bladeFlow = new FlowVO(); |
| | | buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO , "todo" |
| | | buildFlowTaskList(bladeFlow, flowList, todoQuery,page.getCurrent(),page.getSize()); |
| | | flowList.forEach(flowVO -> { |
| | | if(flowVO.getVariables().containsKey("machineCode")){ |
| | | //Machine machinemachineService.getByCode((String)flowVO.getVariables().get("machineCode")) |
| | |
| | | }); |
| | | // 计算总数 |
| | | long count = todoQuery.count(); |
| | | // 设置页数 |
| | | page.setSize(count); |
| | | |
| | | // 设置总数 |
| | | page.setTotal(count); |
| | | // 设置数据 |
| | |
| | | * @param flowList 流程列表 |
| | | * @param taskQuery 任务查询类 |
| | | */ |
| | | private void buildFlowTaskList(FlowVO bladeFlow, List<FlowVO> flowList, TaskQuery taskQuery) { |
| | | private void buildFlowTaskList(FlowVO bladeFlow, List<FlowVO> flowList, TaskQuery taskQuery,Long currentPage,Long pageSize) { |
| | | if (bladeFlow.getCategory() != null) { |
| | | taskQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory())); |
| | | } |
| | |
| | | if (bladeFlow.getEndDate() != null) { |
| | | taskQuery.taskCreatedBefore(bladeFlow.getEndDate()); |
| | | } |
| | | List<Task> tasks = taskQuery.list(); |
| | | |
| | | //List<Task> tasks = taskQuery.list(); |
| | | long total = taskQuery.count(); |
| | | Long firstResult = (currentPage-1)*pageSize; |
| | | List<Task> tasks = taskQuery.listPage(Func.toInt(firstResult), Func.toInt(pageSize)); |
| | | tasks.forEach(task -> { |
| | | FlowVO flow = new FlowVO(); |
| | | flow.setTaskId(task.getId()); |
| | |
| | | |
| | | flow.setProcessDefinitionId(task.getProcessDefinitionId()); |
| | | |
| | | flow.setFile(getFileString(task.getProcessInstanceId())); |
| | | //flow.setProcessDefinitionKey(processDefinition.getKey()); |
| | | //flow.setProcessDefinitionVersion(processDefinition.getVersion()); |
| | | flow.setProcessInstanceId(task.getProcessInstanceId()); |
| | |
| | | if(ru.isSuccess()) { |
| | | flow.setStartUserName(ru.getData().getName()); |
| | | } |
| | | ; |
| | | List<Comment> comments = lastStepComments(task);//taskService.getTaskComments(task.getId()); |
| | | if(!comments.isEmpty()){ |
| | | flow.setComment(comments.get(0).getFullMessage()); |
| | | } |
| | | |
| | | MdmFlowProcess processDefinition = MdmFlowCache.getProcessDefinition(task.getProcessDefinitionId()); |
| | | |
| | | flow.setCategory(processDefinition.getCategory()); |
| | |
| | | }); |
| | | } |
| | | |
| | | List<Comment> lastStepComments(Task currentTask){ |
| | | List<HistoricTaskInstance> previousTasks = historyService.createHistoricActivityInstanceQuery() |
| | | .processInstanceId(currentTask.getProcessInstanceId()) |
| | | .activityType("userTask") // 直接查询用户任务类型 |
| | | .orderByHistoricActivityInstanceEndTime() |
| | | .desc() |
| | | .list() |
| | | .stream() |
| | | .filter(activity -> !activity.getActivityId().equals(currentTask.getTaskDefinitionKey())) |
| | | .map(activity -> historyService.createHistoricTaskInstanceQuery() |
| | | .taskDefinitionKey(activity.getActivityId()) |
| | | .processInstanceId(activity.getProcessInstanceId()) |
| | | .orderByHistoricTaskInstanceEndTime() |
| | | .desc() |
| | | .list() |
| | | ) |
| | | .flatMap(List::stream) |
| | | .toList(); |
| | | List<Comment> comments; |
| | | if (!previousTasks.isEmpty()) { |
| | | return taskService.getTaskComments(previousTasks.get(0).getId()); |
| | | |
| | | }else{ |
| | | return Collections.emptyList(); |
| | | } |
| | | } |
| | | /** |
| | | * 获取历史流程 |
| | | * |
| | |
| | | |
| | | // 构建列表数据 |
| | | FlowVO bladeFlow = new FlowVO(); |
| | | buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO |
| | | buildFlowTaskList(bladeFlow, flowList, todoQuery,page.getCurrent(),page.getSize());//FlowEngineConstant.STATUS_TODO |
| | | |
| | | // 计算总数 |
| | | long count = todoQuery.count(); |
| | | // 设置页数 |
| | | page.setSize(count); |
| | | //page.setSize(count); |
| | | // 设置总数 |
| | | page.setTotal(count); |
| | | // 设置数据 |
| | |
| | | |
| | | // 构建列表数据 |
| | | FlowVO bladeFlow = new FlowVO(); |
| | | buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO |
| | | buildFlowTaskList(bladeFlow, flowList, todoQuery,page.getCurrent(),page.getSize());//FlowEngineConstant.STATUS_TODO |
| | | |
| | | // 计算总数 |
| | | long count = todoQuery.count(); |
| | | // 设置页数 |
| | | page.setSize(count); |
| | | |
| | | // 设置总数 |
| | | page.setTotal(count); |
| | | // 设置数据 |
| | | page.setRecords(flowList); |
| | | return page; |
| | | } |
| | | |
| | | /** |
| | | * 流程查询 功能 的分页 |
| | | * @param page |
| | | * @param createTimeBegin |
| | | * @param createTimeEnd |
| | | * @param keyword |
| | | * @return |
| | | */ |
| | | public IPage<FlowVO> selectSearchPage(IPage<FlowVO> page, LocalDate createTimeBegin, LocalDate createTimeEnd,String processDefinitionKey,String keyword) { |
| | | |
| | | List<FlowVO> flowList = new LinkedList<>(); |
| | | Date now = new Date(); |
| | | TaskQuery taskQuery = taskService.createTaskQuery().active().includeProcessVariables(); |
| | | if(Func.isNotEmpty(createTimeBegin)) { |
| | | taskQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin)); |
| | | } |
| | | if(Func.isNotEmpty(createTimeEnd)) { |
| | | taskQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd.plusDays(1))); |
| | | } |
| | | if(Func.isNotEmpty(processDefinitionKey)) { |
| | | taskQuery.processDefinitionKey(processDefinitionKey); |
| | | } |
| | | |
| | | addKeywordCondition(taskQuery, keyword); |
| | | |
| | | taskQuery.orderByTaskCreateTime().desc(); |
| | | |
| | | // 构建列表数据 |
| | | FlowVO bladeFlow = new FlowVO(); |
| | | buildFlowTaskList(bladeFlow, flowList, taskQuery,page.getCurrent(),page.getSize());//FlowEngineConstant.STATUS_TODO |
| | | |
| | | // 计算总数 |
| | | long count = taskQuery.count(); |
| | | // 设置总数 |
| | | page.setTotal(count); |
| | | // 设置数据 |
| | | page.setRecords(flowList); |
| | | return page; |
| | | } |
| | | |
| | | public void deleteProcessInstance(String processInstancesId) { |
| | | runtimeService.deleteProcessInstance(processInstancesId, "流程查询-删除"); |
| | | //TODO 删除出关联字段 |
| | | } |
| | | |
| | | /** |
| | | * 已完结的流程分页列表 |
| | | * @param page 分页信息 |
| | | * @param createTimeBegin |
| | | * @param createTimeEnd |
| | | * @param keyword 关键字 |
| | | * @return |
| | | */ |
| | | public IPage<FlowVO> selectFinishedPage(IPage<FlowVO> page, String processDefinitionKey,LocalDate createTimeBegin, LocalDate createTimeEnd, String keyword) { |
| | | HistoricProcessInstanceQuery query = this.historyService.createHistoricProcessInstanceQuery().includeProcessVariables(); |
| | | //.finished() // 只查询已完成的流程.unfinished() // 查询未完成的流程 |
| | | if(Func.isNotEmpty(processDefinitionKey)) { |
| | | query.processDefinitionKey(processDefinitionKey); |
| | | } |
| | | if(createTimeBegin!=null) { |
| | | query.startedAfter(DateUtil.toDate(createTimeBegin)); |
| | | } |
| | | if(createTimeEnd!=null) { |
| | | query.startedBefore(DateUtil.toDate(createTimeEnd)); |
| | | } |
| | | query.orderByProcessInstanceEndTime().desc(); // 按结束时间降序排列 |
| | | page.setTotal(query.count()); |
| | | |
| | | long firstResult = (page.getCurrent()-1) * page.getSize(); |
| | | List<HistoricProcessInstance> processes = query |
| | | .listPage((int)firstResult, (int)page.getSize()); |
| | | |
| | | List<FlowVO> records = new LinkedList<>(); |
| | | |
| | | for(HistoricProcessInstance processInstance : processes) { |
| | | FlowVO vo = new FlowVO(); |
| | | vo.setProcessDefinitionKey(processInstance.getProcessDefinitionKey()); |
| | | vo.setProcessInstanceId(processInstance.getId()); |
| | | vo.setProcessDefinitionName(processInstance.getProcessDefinitionName()); |
| | | vo.setVariables(processInstance.getProcessVariables()); |
| | | |
| | | vo.setProcessCreateTime(processInstance.getStartTime()); |
| | | vo.setHistoryTaskEndTime(processInstance.getEndTime()); |
| | | |
| | | vo.setFile(getFileString(processInstance.getId())); |
| | | records.add(vo); |
| | | } |
| | | |
| | | page.setRecords(records); |
| | | |
| | | return page; |
| | | } |
| | | /* |
| | | 获取列表中的文件名 |
| | | */ |
| | | String getFileString(String processInstanceId){ |
| | | String result = ""; |
| | | List<FlowProgramFile> fileList = flowProgramFileService.lambdaQuery().eq(FlowProgramFile::getProcessInstanceId, processInstanceId).list(); |
| | | |
| | | result = String.join(",",fileList.stream().map(FlowProgramFile::getName).toList()); |
| | | |
| | | return result; |
| | | } |
| | | } |