yangys
3 天以前 0ecd5acd3b9f320a487c68df2ea6234b4dacb12c
blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowBusinessService.java
@@ -1,28 +1,4 @@
/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.mdm.flow.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -30,28 +6,29 @@
import org.flowable.engine.HistoryService;
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.flowable.task.api.history.HistoricTaskInstanceQuery;
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.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.flow.core.constant.ProcessConstant;
import org.springblade.flow.core.pojo.entity.BladeFlow;
import org.springblade.flow.core.utils.TaskUtil;
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.MdmFlowProcess;
import org.springblade.mdm.flow.util.MdmFlowCache;
import org.springblade.mdm.flow.vo.FlowVO;
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;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 流程业务实现类
@@ -64,45 +41,46 @@
   private final TaskService taskService;
   private final HistoryService historyService;
   private final IUserClient userClient;
   public IPage<BladeFlow> selectTodoPage(IPage<BladeFlow> page, String keyword) {
   /**
    * 查询我的流程(个人待办列表)
    * @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<BladeFlow> flowList = new LinkedList<>();
      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));
      if(Func.isNotEmpty(keyword)){
         todoQuery.or();
         todoQuery.processVariableValueLike("processNo",keyword);
         todoQuery.processVariableValueLike("machineCode",keyword);
         todoQuery.processVariableValueLike("machineMode",keyword);
         todoQuery.processVariableValueLike("processName",keyword);
         todoQuery.processVariableValueLike("processEdition",keyword);
         todoQuery.processVariableValueLike("craftEdition",keyword);
         //TODO 多个字段都要匹配
         todoQuery.endOr();
         ////如果查询实例的开始时间:只能用以下这个,先查出实例id来
         //todoQuery.processInstanceIdIn()
      }
      if (Func.isNotEmpty(createTimeEnd)) {
         todoQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd));
         //todoQuery.taskInProgressStartTimeBefore(DateUtil.toDate(createTimeEnd));
      }
      if (Func.isNotEmpty(keyword)) {
         addKeywordCondition(todoQuery, keyword);
      }
      todoQuery.orderByTaskCreateTime().desc();
      //List<Task> listt = todoQuery.list();
      // 已签收的任务.caseVariableValueLike("processNo",keyword)
      /*
      TaskQuery todoQuery = taskService.createTaskQuery().taskAssignee(userId).active()
         .includeProcessVariables().orderByTaskCreateTime().desc();
       */
      // 构建列表数据
      BladeFlow bladeFlow = new BladeFlow();
      buildFlowTaskList(bladeFlow, flowList, todoQuery, "todo");//FlowEngineConstant.STATUS_TODO
      FlowVO bladeFlow = new FlowVO();
      buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO , "todo"
      flowList.forEach(flowVO -> {
         if(flowVO.getVariables().containsKey("machineCode")){
            //Machine machinemachineService.getByCode((String)flowVO.getVariables().get("machineCode"))
         }
      });
      // 计算总数
      long count = todoQuery.count();
      // 设置页数
@@ -112,6 +90,21 @@
      // 设置数据
      page.setRecords(flowList);
      return page;
   }
   void addKeywordCondition(TaskQuery todoQuery,String keyword) {
      if(Func.isNotEmpty(keyword)) {
         todoQuery.or();
         todoQuery.processVariableValueLike("processNo", "%" + keyword + "%");
         todoQuery.processVariableValueLike("machineCode", "%" + keyword + "%");
         todoQuery.processVariableValueLike("machineMode", "%" + keyword + "%");
         todoQuery.processVariableValueLike("processName", "%" + keyword + "%");
         todoQuery.processVariableValueLike("processEdition", "%" + keyword + "%");
         todoQuery.processVariableValueLike("craftEdition", "%" + keyword + "%");
         todoQuery.endOr();
      }
   }
   /*
   @Override
@@ -252,25 +245,6 @@
      return page;
   }
   @Override
   public boolean completeTask(BladeFlow flow) {
      String taskId = flow.getTaskId();
      String processInstanceId = flow.getProcessInstanceId();
      String comment = Func.toStr(flow.getComment(), ProcessConstant.PASS_COMMENT);
      // 增加评论
      if (StringUtil.isNoneBlank(processInstanceId, comment)) {
         taskService.addComment(taskId, processInstanceId, comment);
      }
      // 创建变量
      Map<String, Object> variables = flow.getVariables();
      if (variables == null) {
         variables = Kv.create();
      }
      variables.put(ProcessConstant.PASS_KEY, flow.isPass());
      // 完成任务
      taskService.complete(taskId, variables);
      return true;
   }
   */
   /**
    * 构建流程
@@ -278,9 +252,8 @@
    * @param bladeFlow 流程通用类
    * @param flowList  流程列表
    * @param taskQuery 任务查询类
    * @param status    状态
    */
   private void buildFlowTaskList(BladeFlow bladeFlow, List<BladeFlow> flowList, TaskQuery taskQuery, String status) {
   private void buildFlowTaskList(FlowVO bladeFlow, List<FlowVO> flowList, TaskQuery taskQuery) {
      if (bladeFlow.getCategory() != null) {
         taskQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory()));
      }
@@ -293,9 +266,10 @@
      if (bladeFlow.getEndDate() != null) {
         taskQuery.taskCreatedBefore(bladeFlow.getEndDate());
      }
      List<Task> tasks = taskQuery.list();
      tasks.forEach(task -> {
         BladeFlow flow = new BladeFlow();
         FlowVO flow = new FlowVO();
         flow.setTaskId(task.getId());
         flow.setTaskDefinitionKey(task.getTaskDefinitionKey());
         flow.setTaskName(task.getName());
@@ -304,20 +278,29 @@
         flow.setClaimTime(task.getClaimTime());
         flow.setExecutionId(task.getExecutionId());
         flow.setVariables(task.getProcessVariables());
         flow.setDueDate(task.getDueDate());
         flow.setProcessDefinitionId(task.getProcessDefinitionId());
         //flow.setProcessDefinitionName(task.getprocessd);
         //flow.setProcessDefinitionKey(processDefinition.getKey());
         //flow.setProcessDefinitionVersion(processDefinition.getVersion());
         flow.setProcessInstanceId(task.getProcessInstanceId());
         /*
         HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(task.getProcessInstanceId());
         if (Func.isNotEmpty(historicProcessInstance)) {
            String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
            flow.setBusinessTable(businessKey[0]);
            flow.setBusinessId(businessKey[1]);
         }*/
         // 查询流程实例创建时间
         HistoricProcessInstance historicProcess = historyService.createHistoricProcessInstanceQuery()
            .processInstanceId(task.getProcessInstanceId())
            .singleResult();
         flow.setProcessCreateTime(historicProcess.getStartTime());
         R<User> ru = userClient.userInfoById(Long.valueOf(historicProcess.getStartUserId()));
         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());
@@ -329,12 +312,37 @@
         flow.setProcessDefinitionVersion(processDefinition.getVersion());
         flow.setProcessInstanceId(task.getProcessInstanceId());
         //flow.setStatus(status);
         /*
          */
         flowList.add(flow);
      });
   }
   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();
      }
   }
   /**
    * 获取历史流程
    *
@@ -345,33 +353,20 @@
      return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
   }
   public IPage<BladeFlow> selectAllTaskPage(IPage<BladeFlow> page, String keyword) {
   public IPage<FlowVO> selectAllTaskPage(IPage<FlowVO> page, String keyword) {
      List<BladeFlow> flowList = new LinkedList<>();
      List<FlowVO> flowList = new LinkedList<>();
      TaskQuery todoQuery = taskService.createTaskQuery().active().includeProcessVariables();
      if(Func.isNotEmpty(keyword)){
         todoQuery.or();
         todoQuery.processVariableValueLike("processNo",keyword);
         todoQuery.processVariableValueLike("machineCode",keyword);
         todoQuery.processVariableValueLike("machineMode",keyword);
         todoQuery.processVariableValueLike("processName",keyword);
         todoQuery.processVariableValueLike("processEdition",keyword);
         todoQuery.processVariableValueLike("craftEdition",keyword);
         //TODO 多个字段都要匹配
         todoQuery.endOr();
      }
      addKeywordCondition(todoQuery, keyword);
      todoQuery.orderByTaskCreateTime().desc();
      // 构建列表数据
      BladeFlow bladeFlow = new BladeFlow();
      buildFlowTaskList(bladeFlow, flowList, todoQuery, "todo");//FlowEngineConstant.STATUS_TODO
      FlowVO bladeFlow = new FlowVO();
      buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO
      // 计算总数
      long count = todoQuery.count();
@@ -386,40 +381,33 @@
   /**
    * 超时任务查询
    * @param page
    * @param keyword
    * @return
    * @param page 分页信息
    * @param keyword 关键字
    * @return 分页数据
    */
   public IPage<BladeFlow> selectOvertimePage(IPage<BladeFlow> page, String assigneeName,String keyword) {
   public IPage<FlowVO> selectOvertimePage(IPage<FlowVO> page, LocalDate createTimeBegin, LocalDate createTimeEnd, String assigneeName, String keyword) {
      List<BladeFlow> flowList = new LinkedList<>();
      List<FlowVO> flowList = new LinkedList<>();
      Date now = new Date();
      TaskQuery todoQuery = taskService.createTaskQuery().taskDueBefore(now).active().includeProcessVariables();
      if(Func.isNotEmpty(createTimeBegin)) {
         todoQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin));
      }
      if(Func.isNotEmpty(createTimeEnd)) {
         todoQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd.plusDays(1)));
      }
      if(Func.isNotEmpty(keyword)) {
         todoQuery.taskVariableValueLike("assigneeName", "%"+assigneeName+"%");
      }
      if(Func.isNotEmpty(keyword)){
         todoQuery.or();
         keyword = "%"+keyword+"%";
         todoQuery.processVariableValueLike("processNo",keyword);
         todoQuery.processVariableValueLike("machineCode",keyword);
         todoQuery.processVariableValueLike("machineMode",keyword);
         todoQuery.processVariableValueLike("processName",keyword);
         todoQuery.processVariableValueLike("processEdition",keyword);
         todoQuery.processVariableValueLike("craftEdition",keyword);
         //TODO 多个字段都要匹配
         todoQuery.endOr();
      }
      addKeywordCondition(todoQuery, keyword);
      todoQuery.orderByTaskCreateTime().desc();
      // 构建列表数据
      BladeFlow bladeFlow = new BladeFlow();
      buildFlowTaskList(bladeFlow, flowList, todoQuery, "todo");//FlowEngineConstant.STATUS_TODO
      FlowVO bladeFlow = new FlowVO();
      buildFlowTaskList(bladeFlow, flowList, todoQuery);//FlowEngineConstant.STATUS_TODO
      // 计算总数
      long count = todoQuery.count();
@@ -431,4 +419,44 @@
      page.setRecords(flowList);
      return page;
   }
   /**
    * 流程查询 功能 的分页
    * @param page
    * @param createTimeBegin
    * @param createTimeEnd
    * @param assigneeName
    * @param keyword
    * @return
    */
   public IPage<FlowVO> selectSearchPage(IPage<FlowVO> page, LocalDate createTimeBegin, LocalDate createTimeEnd, 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)));
      }
      addKeywordCondition(taskQuery, keyword);
      taskQuery.orderByTaskCreateTime().desc();
      // 构建列表数据
      FlowVO bladeFlow = new FlowVO();
      buildFlowTaskList(bladeFlow, flowList, taskQuery);//FlowEngineConstant.STATUS_TODO
      // 计算总数
      long count = taskQuery.count();
      // 设置页数
      page.setSize(count);
      // 设置总数
      page.setTotal(count);
      // 设置数据
      page.setRecords(flowList);
      return page;
   }
}