ludc
2023-08-08 6ceaecf1a3eb4b8801b2200cf692cb093263a493
Source/UBCS/ubcs-ops/ubcs-flow/src/main/java/com/vci/ubcs/flow/business/service/impl/FlowBusinessServiceImpl.java
@@ -24,12 +24,19 @@
import com.vci.ubcs.flow.engine.constant.FlowEngineConstant;
import com.vci.ubcs.flow.engine.entity.FlowProcess;
import com.vci.ubcs.flow.engine.utils.FlowCache;
import com.vci.ubcs.starter.web.util.VciDateUtil;
import com.vci.ubcs.system.user.cache.UserCache;
import com.vci.ubcs.system.user.entity.User;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
@@ -40,6 +47,7 @@
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -55,6 +63,7 @@
   private final TaskService taskService;
   private final HistoryService historyService;
   private final RuntimeService runtimeService;
   @Override
   public IPage<BladeFlow> selectClaimPage(IPage<BladeFlow> page, BladeFlow bladeFlow) {
@@ -73,9 +82,9 @@
         .includeProcessVariables().active().orderByTaskCreateTime().desc();
      // 构建列表数据
      buildFlowTaskList(bladeFlow, flowList, claimUserQuery, FlowEngineConstant.STATUS_CLAIM);
      buildFlowTaskList(bladeFlow, flowList, claimRoleWithTenantIdQuery, FlowEngineConstant.STATUS_CLAIM);
      buildFlowTaskList(bladeFlow, flowList, claimRoleWithoutTenantIdQuery, FlowEngineConstant.STATUS_CLAIM);
      buildFlowTaskList(bladeFlow, flowList, claimUserQuery, FlowEngineConstant.STATUS_CLAIM, page);
      buildFlowTaskList(bladeFlow, flowList, claimRoleWithTenantIdQuery, FlowEngineConstant.STATUS_CLAIM, page);
      buildFlowTaskList(bladeFlow, flowList, claimRoleWithoutTenantIdQuery, FlowEngineConstant.STATUS_CLAIM, page);
      // 计算总数
      long count = claimUserQuery.count() + claimRoleWithTenantIdQuery.count() + claimRoleWithoutTenantIdQuery.count();
@@ -98,7 +107,7 @@
         .includeProcessVariables().orderByTaskCreateTime().desc();
      // 构建列表数据
      buildFlowTaskList(bladeFlow, flowList, todoQuery, FlowEngineConstant.STATUS_TODO);
      buildFlowTaskList(bladeFlow, flowList, todoQuery, FlowEngineConstant.STATUS_TODO,page);
      // 计算总数
      long count = todoQuery.count();
@@ -264,8 +273,22 @@
         variables = Kv.create();
      }
      variables.put(ProcessConstant.PASS_KEY, flow.isPass());
      // 完成任务
      taskService.complete(taskId, variables);
//      //设置下一步审核人
//      Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();//下一步节点
//      String taskKey = task.getTaskDefinitionKey();//就是流程配置的节点id
//      Object to = getVar(processInstanceId,taskKey);
//      if(to==null){
//         throw new ServiceException(taskKey+"没有设置审核人");
//      }
//      String assignee = to.toString();
//
//      if(StringUtils.isNotEmpty(assignee)) {
//         task.setAssignee(assignee);
//      }
      return true;
   }
@@ -276,8 +299,9 @@
    * @param flowList  流程列表
    * @param taskQuery 任务查询类
    * @param status    状态
    * @param page
    */
   private void buildFlowTaskList(BladeFlow bladeFlow, List<BladeFlow> flowList, TaskQuery taskQuery, String status) {
   private void buildFlowTaskList(BladeFlow bladeFlow, List<BladeFlow> flowList, TaskQuery taskQuery, String status, IPage<BladeFlow> page) {
//      if (bladeFlow.getCategory() != null) {
      if (StringUtils.isNotEmpty(bladeFlow.getCategory())) {
         taskQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory()));
@@ -291,7 +315,9 @@
      if (bladeFlow.getEndDate() != null) {
         taskQuery.taskCreatedBefore(bladeFlow.getEndDate());
      }
      taskQuery.list().forEach(task -> {
      int firstResult = (int) ((page.getCurrent()-1)* page.getSize());
      int maxResult = (int) page.getSize();
      taskQuery.listPage(firstResult,maxResult).forEach(task -> {
         BladeFlow flow = new BladeFlow();
         flow.setTaskId(task.getId());
         flow.setTaskDefinitionKey(task.getTaskDefinitionKey());
@@ -303,10 +329,47 @@
         flow.setVariables(task.getProcessVariables());
         HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(task.getProcessInstanceId());
         //获取上一任务信息
         HistoricActivityInstance historicActivityInstance = getPrevHistoricActivityInstance(task.getProcessInstanceId(),task.getId());
         flow.setHistoryActivityId(historicActivityInstance.getTaskId());
         flow.setHistoryActivityName(historicActivityInstance.getActivityName());
         //获取历史任务执行人
         if(historicActivityInstance.getActivityType().equals(FlowEngineConstant.USR_TASK)){
            User user = UserCache.getUser(TaskUtil.getUserId(historicActivityInstance.getAssignee()));
            flow.setHistoryActivityAssigne(historicActivityInstance.getAssignee());
            flow.setHistoryActivityAssigneName(user.getName());
            List<Comment> commentList = taskService.getTaskComments(historicActivityInstance.getTaskId());
            if (commentList.size() > 0) {
               flow.setHistoryActivityIdea(commentList.get(0).getFullMessage());
            }
         }
         List<HistoricProcessInstance> processInstanceList = historyService.createHistoricProcessInstanceQuery()
            .processInstanceId(task.getProcessInstanceId()).orderByProcessInstanceStartTime().asc().list();
         flow.setProcessDefinitionStartTime(VciDateUtil.date2Str(processInstanceList.get(0).getStartTime(),VciDateUtil.DateTimeFormat));
         // 获取流程发起人名称
         if (FlowEngineConstant.START_EVENT.equals(historicActivityInstance.getActivityType())) {
            if (processInstanceList.size() > 0) {
               if (StringUtil.isNotBlank(processInstanceList.get(0).getStartUserId())) {
                  String taskUser = processInstanceList.get(0).getStartUserId();
                  User user = UserCache.getUser(TaskUtil.getUserId(taskUser));
                  if (user != null) {
                     flow.setHistoryActivityAssigne(historicActivityInstance.getAssignee());
                     flow.setHistoryActivityAssigneName(user.getName());
                  }
               }
            }
         }
         if (Func.isNotEmpty(historicProcessInstance)) {
            String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
            String[] businessKey = new String[2];
            if(historicProcessInstance.getBusinessKey().contains(StringPool.COLON)){
               businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
            }else{
               businessKey[0] = historicProcessInstance.getBusinessKey();
            }
            flow.setBusinessTable(businessKey[0]);
            flow.setBusinessId(businessKey.length>1?businessKey[1]:"1");
            flow.setBusinessId(businessKey.length>1 && StringUtils.isNotEmpty(businessKey[1])?businessKey[1]:"1");
         }
         FlowProcess processDefinition = FlowCache.getProcessDefinition(task.getProcessDefinitionId());
@@ -322,6 +385,29 @@
      });
   }
   private HistoricActivityInstance getPrevHistoricActivityInstance(String processInstanceId, String taskDefinitionId) {
      List<HistoricActivityInstance> historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId)
         .orderByHistoricActivityInstanceStartTime().asc()
         .orderByHistoricActivityInstanceEndTime().asc().list();
      List<HistoricActivityInstance> tempList = new ArrayList<>();
      int index = 0;
      for(HistoricActivityInstance historicActivityInstance : historicActivityInstanceList){
         if (StringUtils.equals(FlowEngineConstant.USR_TASK, historicActivityInstance.getActivityType())
            || FlowEngineConstant.START_EVENT.equals(historicActivityInstance.getActivityType())
            || FlowEngineConstant.END_EVENT.equals(historicActivityInstance.getActivityType())) {
            tempList.add(historicActivityInstance);
         }
         if(StringUtils.isNotEmpty(historicActivityInstance.getTaskId()) && historicActivityInstance.getTaskId().equals(taskDefinitionId)){
            index = tempList.indexOf(historicActivityInstance);
            break;
         }
      }
      if(index == 0){
         return tempList.get(0);
      }
      return tempList.get(index-1);
   }
   /**
    * 获取历史流程
    *
@@ -332,4 +418,16 @@
      return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
   }
   public Object getVar(String processInstanceId,String key){
      Object t = null;
      List<Execution> list = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).list();
      for (Execution execution : list) {
         Object va = runtimeService.getVariable(execution.getId(),key);
         if(va!=null){
            t=va;
         }
      }
      return t;
   }
}