yangys
2025-08-14 33e6270d2d3a40591e58d45fd8435d2f3eaf66b2
blade-service/blade-mdm/src/main/java/org/springblade/mdm/flow/service/FlowBusinessService.java
@@ -10,19 +10,19 @@
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.task.Comment;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfoQuery;
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.api.R;
import org.springblade.core.tool.utils.*;
import org.springblade.mdm.basesetting.machine.MachineService;
import org.springblade.mdm.flow.constants.FlowContants;
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.entity.NcProgram;
import org.springblade.mdm.program.entity.ProcessProgRef;
import org.springblade.mdm.program.service.NcProgramService;
import org.springblade.mdm.program.service.ProcessProgRefService;
import org.springblade.system.feign.IUserClient;
@@ -31,11 +31,7 @@
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.stream.Collectors;
import java.util.*;
/**
 * 流程业务实现类
@@ -49,9 +45,7 @@
   private final TaskService taskService;
   private final HistoryService historyService;
   private final IUserClient userClient;
   private final ProcessProgRefService processProgRefService;
   private final ApproveRecordService approveRecordService;
   private final NcProgramService ncProgramService;
   private final FlowProgramFileService flowProgramFileService;
   /**
    * 查询我的流程(个人待办列表)
    * @param page 分页信息
@@ -68,9 +62,6 @@
      TaskQuery todoQuery = taskService.createTaskQuery().taskAssignee(userId).active().includeProcessVariables();
      if (Func.isNotEmpty(createTimeBegin)) {
         todoQuery.taskCreatedAfter(DateUtil.toDate(createTimeBegin));
         ////如果查询实例的开始时间:只能用以下这个,先查出实例id来
         //todoQuery.processInstanceIdIn()
      }
      if (Func.isNotEmpty(createTimeEnd)) {
         todoQuery.taskCreatedBefore(DateUtil.toDate(createTimeEnd));
@@ -85,11 +76,7 @@
      // 构建列表数据
      FlowVO bladeFlow = new FlowVO();
      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();
@@ -99,161 +86,24 @@
      page.setRecords(flowList);
      return page;
   }
   void addKeywordCondition(TaskQuery todoQuery,String keyword) {
   //TaskInfoQuery
   //void addKeywordCondition(TaskQuery todoQuery,String keyword) {
   void addKeywordCondition(TaskInfoQuery todoQuery, String keyword) {
      if(Func.isNotEmpty(keyword)) {
         String likeVal = "%" + keyword + "%";
         todoQuery.or();
         todoQuery.processVariableValueLike(FlowContants.TITLE,likeVal);
         todoQuery.processVariableValueLike(FlowContants.PROCESS_NO, likeVal);
         todoQuery.processVariableValueLike(FlowContants.MACHINE_CODE, likeVal);
         todoQuery.processVariableValueLike(FlowContants.MACHINE_MODE, likeVal);
         todoQuery.processVariableValueLike(FlowContants.PROCESS_NAME, likeVal);
         todoQuery.processVariableValueLike(FlowContants.PROCESS_EDITION, likeVal);
         todoQuery.processVariableValueLike(FlowContants.CRAFT_EDITION, likeVal);
         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.processDefinitionNameLike(likeVal);
         todoQuery.endOr();
      }
   }
   /*
   @Override
   public IPage<BladeFlow> selectSendPage(IPage<BladeFlow> page, BladeFlow bladeFlow) {
      String taskUser = TaskUtil.getTaskUser();
      List<BladeFlow> flowList = new LinkedList<>();
      HistoricProcessInstanceQuery historyQuery = historyService.createHistoricProcessInstanceQuery().startedBy(taskUser).orderByProcessInstanceStartTime().desc();
      if (bladeFlow.getCategory() != null) {
         historyQuery.processDefinitionCategory(bladeFlow.getCategory());
      }
      if (bladeFlow.getProcessDefinitionName() != null) {
         historyQuery.processDefinitionName(bladeFlow.getProcessDefinitionName());
      }
      if (bladeFlow.getBeginDate() != null) {
         historyQuery.startedAfter(bladeFlow.getBeginDate());
      }
      if (bladeFlow.getEndDate() != null) {
         historyQuery.startedBefore(bladeFlow.getEndDate());
      }
      // 查询列表
      List<HistoricProcessInstance> historyList = historyQuery.listPage(Func.toInt((page.getCurrent() - 1) * page.getSize()), Func.toInt(page.getSize()));
      historyList.forEach(historicProcessInstance -> {
         BladeFlow flow = new BladeFlow();
         // historicProcessInstance
         flow.setCreateTime(historicProcessInstance.getStartTime());
         flow.setEndTime(historicProcessInstance.getEndTime());
         flow.setVariables(historicProcessInstance.getProcessVariables());
         String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
         if (businessKey.length > 1) {
            flow.setBusinessTable(businessKey[0]);
            flow.setBusinessId(businessKey[1]);
         }
         flow.setHistoryActivityName(historicProcessInstance.getName());
         flow.setProcessInstanceId(historicProcessInstance.getId());
         flow.setHistoryProcessInstanceId(historicProcessInstance.getId());
         // ProcessDefinition
         FlowProcess processDefinition = FlowCache.getProcessDefinition(historicProcessInstance.getProcessDefinitionId());
         flow.setProcessDefinitionId(processDefinition.getId());
         flow.setProcessDefinitionName(processDefinition.getName());
         flow.setProcessDefinitionVersion(processDefinition.getVersion());
         flow.setProcessDefinitionKey(processDefinition.getKey());
         flow.setCategory(processDefinition.getCategory());
         flow.setCategoryName(FlowCache.getCategoryName(processDefinition.getCategory()));
         flow.setProcessInstanceId(historicProcessInstance.getId());
         // HistoricTaskInstance
         List<HistoricTaskInstance> historyTasks = historyService.createHistoricTaskInstanceQuery().processInstanceId(historicProcessInstance.getId()).orderByHistoricTaskInstanceEndTime().desc().list();
         if (Func.isNotEmpty(historyTasks)) {
            HistoricTaskInstance historyTask = historyTasks.iterator().next();
            flow.setTaskId(historyTask.getId());
            flow.setTaskName(historyTask.getName());
            flow.setTaskDefinitionKey(historyTask.getTaskDefinitionKey());
         }
         // Status
         if (historicProcessInstance.getEndActivityId() != null) {
            flow.setProcessIsFinished(FlowEngineConstant.STATUS_FINISHED);
         } else {
            flow.setProcessIsFinished(FlowEngineConstant.STATUS_UNFINISHED);
         }
         flow.setStatus(FlowEngineConstant.STATUS_FINISH);
         flowList.add(flow);
      });
      // 计算总数
      long count = historyQuery.count();
      // 设置总数
      page.setTotal(count);
      page.setRecords(flowList);
      return page;
   }
   @Override
   public IPage<BladeFlow> selectDonePage(IPage<BladeFlow> page, BladeFlow bladeFlow) {
      String taskUser = TaskUtil.getTaskUser();
      List<BladeFlow> flowList = new LinkedList<>();
      HistoricTaskInstanceQuery doneQuery = historyService.createHistoricTaskInstanceQuery().taskAssignee(taskUser).finished()
         .includeProcessVariables().orderByHistoricTaskInstanceEndTime().desc();
      if (bladeFlow.getCategory() != null) {
         doneQuery.processCategoryIn(Func.toStrList(bladeFlow.getCategory()));
      }
      if (bladeFlow.getProcessDefinitionName() != null) {
         doneQuery.processDefinitionName(bladeFlow.getProcessDefinitionName());
      }
      if (bladeFlow.getBeginDate() != null) {
         doneQuery.taskCompletedAfter(bladeFlow.getBeginDate());
      }
      if (bladeFlow.getEndDate() != null) {
         doneQuery.taskCompletedBefore(bladeFlow.getEndDate());
      }
      // 查询列表
      List<HistoricTaskInstance> doneList = doneQuery.listPage(Func.toInt((page.getCurrent() - 1) * page.getSize()), Func.toInt(page.getSize()));
      doneList.forEach(historicTaskInstance -> {
         BladeFlow flow = new BladeFlow();
         flow.setTaskId(historicTaskInstance.getId());
         flow.setTaskDefinitionKey(historicTaskInstance.getTaskDefinitionKey());
         flow.setTaskName(historicTaskInstance.getName());
         flow.setAssignee(historicTaskInstance.getAssignee());
         flow.setCreateTime(historicTaskInstance.getCreateTime());
         flow.setExecutionId(historicTaskInstance.getExecutionId());
         flow.setHistoryTaskEndTime(historicTaskInstance.getEndTime());
         flow.setVariables(historicTaskInstance.getProcessVariables());
         FlowProcess processDefinition = FlowCache.getProcessDefinition(historicTaskInstance.getProcessDefinitionId());
         flow.setProcessDefinitionId(processDefinition.getId());
         flow.setProcessDefinitionName(processDefinition.getName());
         flow.setProcessDefinitionKey(processDefinition.getKey());
         flow.setProcessDefinitionVersion(processDefinition.getVersion());
         flow.setCategory(processDefinition.getCategory());
         flow.setCategoryName(FlowCache.getCategoryName(processDefinition.getCategory()));
         flow.setProcessInstanceId(historicTaskInstance.getProcessInstanceId());
         flow.setHistoryProcessInstanceId(historicTaskInstance.getProcessInstanceId());
         HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance((historicTaskInstance.getProcessInstanceId()));
         if (Func.isNotEmpty(historicProcessInstance)) {
            String[] businessKey = Func.toStrArray(StringPool.COLON, historicProcessInstance.getBusinessKey());
            flow.setBusinessTable(businessKey[0]);
            flow.setBusinessId(businessKey[1]);
            if (historicProcessInstance.getEndActivityId() != null) {
               flow.setProcessIsFinished(FlowEngineConstant.STATUS_FINISHED);
            } else {
               flow.setProcessIsFinished(FlowEngineConstant.STATUS_UNFINISHED);
            }
         }
         flow.setStatus(FlowEngineConstant.STATUS_FINISH);
         flowList.add(flow);
      });
      // 计算总数
      long count = doneQuery.count();
      // 设置总数
      page.setTotal(count);
      page.setRecords(flowList);
      return page;
   }
   */
   /**
    * 构建流程
    *
@@ -277,7 +127,7 @@
      //List<Task> tasks = taskQuery.list();
      long total = taskQuery.count();
      Long firstResult = (currentPage-1)*pageSize+1;
      Long firstResult = (currentPage-1)*pageSize;
      List<Task> tasks = taskQuery.listPage(Func.toInt(firstResult), Func.toInt(pageSize));
      tasks.forEach(task -> {
         FlowVO flow = new FlowVO();
@@ -293,9 +143,7 @@
         flow.setProcessDefinitionId(task.getProcessDefinitionId());
         flow.setFile(getNcFileString(String.valueOf(task.getProcessVariables().get("programIds"))));
         //flow.setProcessDefinitionKey(processDefinition.getKey());
         //flow.setProcessDefinitionVersion(processDefinition.getVersion());
         flow.setFile(getFileString(task.getProcessInstanceId()));
         flow.setProcessInstanceId(task.getProcessInstanceId());
         // 查询流程实例创建时间
@@ -484,8 +332,8 @@
    * @param keyword 关键字
    * @return
    */
   public IPage<FlowVO> selectFinishedPage(IPage<FlowVO> page, String processDefinitionKey,LocalDate createTimeBegin, LocalDate createTimeEnd, String keyword) {
      HistoricProcessInstanceQuery query = this.historyService.createHistoricProcessInstanceQuery().includeProcessVariables();
   public IPage<FlowVO> selectFinishedPage(IPage<FlowVO> page, String processDefinitionKey,LocalDateTime createTimeBegin, LocalDateTime createTimeEnd, String keyword) {
      HistoricProcessInstanceQuery query = this.historyService.createHistoricProcessInstanceQuery().finished().includeProcessVariables();
         //.finished() // 只查询已完成的流程.unfinished() // 查询未完成的流程
      if(Func.isNotEmpty(processDefinitionKey)) {
         query.processDefinitionKey(processDefinitionKey);
@@ -499,7 +347,7 @@
      query.orderByProcessInstanceEndTime().desc(); // 按结束时间降序排列
      page.setTotal(query.count());
      long firstResult = (page.getCurrent()-1) * page.getSize() + 1;
      long firstResult = (page.getCurrent()-1) * page.getSize();
      List<HistoricProcessInstance> processes = query
         .listPage((int)firstResult, (int)page.getSize());
@@ -515,7 +363,7 @@
         vo.setProcessCreateTime(processInstance.getStartTime());
         vo.setHistoryTaskEndTime(processInstance.getEndTime());
         vo.setFile(getNcFileString(String.valueOf(processInstance.getProcessVariables().get(FlowContants.PROGRAM_IDS_KEY))));
         vo.setFile(getFileString(processInstance.getId()));
         records.add(vo);
      }
@@ -523,13 +371,103 @@
      return page;
   }
   String getNcFileString(String programIds){
   /*
   获取列表中的文件名
    */
   String getFileString(String processInstanceId){
      String result = "";
      if(Func.isNotEmpty(programIds)) {
         List<NcProgram> progList= ncProgramService.listByIds(Func.toLongList(programIds));
         result = String.join(",",progList.stream().map(NcProgram::getName).toList());
      }
      List<FlowProgramFile> fileList = flowProgramFileService.lambdaQuery().eq(FlowProgramFile::getProcessInstanceId, processInstanceId).list();
      result = String.join(",",fileList.stream().map(FlowProgramFile::getName).toList());
      return result;
   }
   /**
    * 个人已办(已完成任务)
    * @param page
    * @param processDefinitionKey
    * @param createTimeBegin
    * @param createTimeEnd
    * @param keyword
    * @return
    */
   public IPage<FlowVO> selectDonePage(IPage<FlowVO> page, String processDefinitionKey,LocalDateTime createTimeBegin, LocalDateTime createTimeEnd, String keyword) {
      String userId = "" + AuthUtil.getUserId();
      HistoricTaskInstanceQuery doneTaskQuery = historyService.createHistoricTaskInstanceQuery().taskAssignee(userId).finished().includeProcessVariables();
      //.finished() // 只查询已完成的流程.unfinished() // 查询未完成的流程
      if(Func.isNotEmpty(processDefinitionKey)) {
         doneTaskQuery.processDefinitionKey(processDefinitionKey);
      }
      if(createTimeBegin!=null) {
         Date after = DateUtil.toDate(createTimeBegin);
         doneTaskQuery.taskCreatedAfter(after);
         //doneTaskQuery.taskCreatedOn(after);
         //doneTaskQuery.taskInProgressStartTimeAfter(after);
      }
      if(createTimeEnd!=null) {
         Date before = DateUtil.toDate(createTimeEnd);
         doneTaskQuery.taskCreatedBefore(before);
         //doneTaskQuery.taskInProgressStartTimeBefore(before);
      }
      if (Func.isNotEmpty(keyword)) {
         addKeywordCondition(doneTaskQuery, keyword);
      }
      doneTaskQuery.orderByTaskCreateTime().desc(); // 按结束时间降序排列
      page.setTotal(doneTaskQuery.count());
      long firstResult = (page.getCurrent()-1) * page.getSize();
      List<HistoricTaskInstance> processes = doneTaskQuery
         .listPage((int)firstResult, (int)page.getSize());
      List<FlowVO> records = new LinkedList<>();
      Map<String,String> startUserMap = new HashMap<>();//使用userId缓存其姓名
      for(HistoricTaskInstance hisTask : processes) {
         FlowVO vo = new FlowVO();
         HistoricProcessInstance processInstance = historyService
            .createHistoricProcessInstanceQuery()
            .processInstanceId(hisTask.getProcessInstanceId())
            .singleResult();
         if(startUserMap.containsKey(processInstance.getStartUserId())) {
            vo.setStartUserName(startUserMap.get(processInstance.getStartUserId()));
         }else {
            R<User> ru = userClient.userInfoById(Long.valueOf(processInstance.getStartUserId()));
            if (ru.isSuccess()) {
               vo.setStartUserName(ru.getData().getName());
               startUserMap.put(processInstance.getStartUserId(),ru.getData().getName());
            }
         }
         vo.setProcessCreateTime(processInstance.getStartTime());
         vo.setProcessInstanceId(hisTask.getProcessInstanceId());
         vo.setProcessDefinitionName(processInstance.getProcessDefinitionName());
         vo.setProcessDefinitionKey(processInstance.getProcessDefinitionKey());
         vo.setTaskName(hisTask.getName());
         vo.setTaskDefinitionKey(hisTask.getTaskDefinitionKey());
         vo.setCreateTime(hisTask.getCreateTime());
         vo.setEndTime(hisTask.getEndTime());
         vo.setVariables(hisTask.getProcessVariables());
         vo.setProcessCreateTime(processInstance.getStartTime());
         vo.setHistoryTaskEndTime(hisTask.getEndTime());
         vo.setFile(getFileString(hisTask.getProcessInstanceId()));
         records.add(vo);
      }
      page.setRecords(records);
      return page;
   }
}