org.zodiac.flowable.business.service.impl.DefaultFlowBusinessServiceImpl Maven / Gradle / Ivy
package org.zodiac.flowable.business.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.repository.ProcessDefinition;
import org.flowable.task.api.TaskQuery;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.zodiac.commons.util.Func;
import org.zodiac.flowable.business.service.FlowBusinessService;
import org.zodiac.flowable.core.constants.ProcessConstants;
import org.zodiac.flowable.core.model.PlatformProcessFlow;
import org.zodiac.flowable.core.util.ProcessTaskUtil;
import org.zodiac.flowable.engine.constants.FlowEngineConstants;
import org.zodiac.flowable.engine.util.ProcessFlowCacheUtil;
import org.zodiac.sdk.toolkit.constants.StringPool;
import org.zodiac.sdk.toolkit.support.Kv;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import org.zodiac.flowable.engine.util.FlowCategoryNameSupplier;
import org.zodiac.security.SecurityAuthOperations2;
import java.util.List;
import java.util.Map;
public class DefaultFlowBusinessServiceImpl implements FlowBusinessService {
private final TaskService taskService;
private final HistoryService historyService;
private final SecurityAuthOperations2 securityAuthOperations;
public DefaultFlowBusinessServiceImpl(TaskService taskService, HistoryService historyService,
SecurityAuthOperations2 securityAuthOperations) {
this.taskService = taskService;
this.historyService = historyService;
this.securityAuthOperations = securityAuthOperations;
}
@Override
public IPage selectClaimPage(IPage page, PlatformProcessFlow platformFlow,
FlowCategoryNameSupplier flowCategoryNameSupplier) {
String taskUser = ProcessTaskUtil.getTaskUser();
String taskGroup = ProcessTaskUtil.getCandidateGroup();
List flowList = CollUtil.linkedList();
/*个人等待签收的任务。*/
TaskQuery claimUserQuery = taskService.createTaskQuery().taskCandidateUser(taskUser).includeProcessVariables()
.active().orderByTaskCreateTime().desc();
/*定制流程等待签收的任务。*/
TaskQuery claimRoleWithTenantIdQuery = taskService.createTaskQuery().taskTenantId(securityAuthOperations.getTenantId())
.taskCandidateGroupIn(Func.toStrList(taskGroup)).includeProcessVariables().active().orderByTaskCreateTime()
.desc();
/*通用流程等待签收的任务。*/
TaskQuery claimRoleWithoutTenantIdQuery =
taskService.createTaskQuery().taskWithoutTenantId().taskCandidateGroupIn(Func.toStrList(taskGroup))
.includeProcessVariables().active().orderByTaskCreateTime().desc();
/*构建列表数据。*/
buildFlowTaskList(platformFlow, flowList, claimUserQuery, FlowEngineConstants.STATUS_CLAIM, flowCategoryNameSupplier);
buildFlowTaskList(platformFlow, flowList, claimRoleWithTenantIdQuery, FlowEngineConstants.STATUS_CLAIM, flowCategoryNameSupplier);
buildFlowTaskList(platformFlow, flowList, claimRoleWithoutTenantIdQuery, FlowEngineConstants.STATUS_CLAIM, flowCategoryNameSupplier);
/*计算总数。*/
long count =
claimUserQuery.count() + claimRoleWithTenantIdQuery.count() + claimRoleWithoutTenantIdQuery.count();
/*设置页数。*/
page.setSize(count);
/*设置总数。*/
page.setTotal(count);
/*设置数据。*/
page.setRecords(flowList);
return page;
}
@Override
public IPage selectTodoPage(IPage page, PlatformProcessFlow platformFlow,
FlowCategoryNameSupplier flowCategoryNameSupplier) {
String taskUser = ProcessTaskUtil.getTaskUser();
List flowList = CollUtil.linkedList();
/*已签收的任务*/
TaskQuery todoQuery = taskService.createTaskQuery().taskAssignee(taskUser).active().includeProcessVariables()
.orderByTaskCreateTime().desc();
/*构建列表数据*/
buildFlowTaskList(platformFlow, flowList, todoQuery, FlowEngineConstants.STATUS_TODO, flowCategoryNameSupplier);
// 计算总数
long count = todoQuery.count();
/*设置页数*/
page.setSize(count);
/*设置总数*/
page.setTotal(count);
/*设置数据*/
page.setRecords(flowList);
return page;
}
@Override
public IPage selectSendPage(IPage page, PlatformProcessFlow platformFlow,
FlowCategoryNameSupplier flowCategoryNameSupplier) {
String taskUser = ProcessTaskUtil.getTaskUser();
List flowList = CollUtil.linkedList();
HistoricProcessInstanceQuery historyQuery = historyService.createHistoricProcessInstanceQuery()
.startedBy(taskUser).orderByProcessInstanceStartTime().desc();
if (platformFlow.getCategory() != null) {
historyQuery.processDefinitionCategory(platformFlow.getCategory());
}
if (platformFlow.getBeginDate() != null) {
historyQuery.startedAfter(platformFlow.getBeginDate());
}
if (platformFlow.getEndDate() != null) {
historyQuery.startedBefore(platformFlow.getEndDate());
}
// 查询列表
List historyList =
historyQuery.listPage(Func.toInt((page.getCurrent() - 1) * page.getSize()), Func.toInt(page.getSize()));
historyList.forEach(historicProcessInstance -> {
/*historicProcessInstance*/
PlatformProcessFlow flow = new PlatformProcessFlow().setCreateTime(historicProcessInstance.getStartTime())
.setEndTime(historicProcessInstance.getEndTime()).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*/
ProcessDefinition processDefinition =
ProcessFlowCacheUtil.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(ProcessFlowCacheUtil.getCategoryName(processDefinition.getCategory(), flowCategoryNameSupplier));
flow.setProcessInstanceId(historicProcessInstance.getId());
/*HistoricTaskInstance*/
List 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(FlowEngineConstants.STATUS_FINISHED);
} else {
flow.setProcessIsFinished(FlowEngineConstants.STATUS_UNFINISHED);
}
flow.setStatus(FlowEngineConstants.STATUS_FINISH);
flowList.add(flow);
});
/*计算总数*/
long count = historyQuery.count();
/*设置总数*/
page.setTotal(count);
page.setRecords(flowList);
return page;
}
@Override
public IPage selectDonePage(IPage page, PlatformProcessFlow platformFlow,
FlowCategoryNameSupplier flowCategoryNameSupplier) {
String taskUser = ProcessTaskUtil.getTaskUser();
List flowList = CollUtil.linkedList();
HistoricTaskInstanceQuery doneQuery = historyService.createHistoricTaskInstanceQuery().taskAssignee(taskUser)
.finished().includeProcessVariables().orderByHistoricTaskInstanceEndTime().desc();
if (platformFlow.getCategory() != null) {
doneQuery.processCategoryIn(Func.toStrList(platformFlow.getCategory()));
}
if (platformFlow.getBeginDate() != null) {
doneQuery.taskCompletedAfter(platformFlow.getBeginDate());
}
if (platformFlow.getEndDate() != null) {
doneQuery.taskCompletedBefore(platformFlow.getEndDate());
}
/*查询列表*/
List doneList =
doneQuery.listPage(Func.toInt((page.getCurrent() - 1) * page.getSize()), Func.toInt(page.getSize()));
doneList.forEach(historicTaskInstance -> {
PlatformProcessFlow flow = new PlatformProcessFlow().setTaskId(historicTaskInstance.getId()).setTaskDefinitionKey(historicTaskInstance.getTaskDefinitionKey())
.setTaskName(historicTaskInstance.getName()).setAssignee(historicTaskInstance.getAssignee()).setCreateTime(historicTaskInstance.getCreateTime())
.setExecutionId(historicTaskInstance.getExecutionId()).setHistoryTaskEndTime(historicTaskInstance.getEndTime())
.setVariables(historicTaskInstance.getProcessVariables());
ProcessDefinition processDefinition =
ProcessFlowCacheUtil.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(ProcessFlowCacheUtil.getCategoryName(processDefinition.getCategory(), flowCategoryNameSupplier));
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(FlowEngineConstants.STATUS_FINISHED);
} else {
flow.setProcessIsFinished(FlowEngineConstants.STATUS_UNFINISHED);
}
}
flow.setStatus(FlowEngineConstants.STATUS_FINISH);
flowList.add(flow);
});
/*计算总数*/
long count = doneQuery.count();
/*设置总数*/
page.setTotal(count);
page.setRecords(flowList);
return page;
}
@Override
public boolean completeTask(PlatformProcessFlow flow) {
String taskId = flow.getTaskId();
String processInstanceId = flow.getProcessInstanceId();
String comment = Func.toStr(flow.getComment(), ProcessConstants.PASS_COMMENT);
/*增加评论*/
if (StrUtil.isNoneBlank(processInstanceId, comment)) {
taskService.addComment(taskId, processInstanceId, comment);
}
/*创建变量*/
Map variables = flow.getVariables();
if (variables == null) {
variables = Kv.create();
}
variables.put(ProcessConstants.PASS_KEY, flow.isPass());
/*完成任务*/
taskService.complete(taskId, variables);
return true;
}
/**
* 构建流程。
*
* @param platformFlow 流程通用类
* @param flowList 流程列表
* @param taskQuery 任务查询类
* @param status 状态
* @param flowCategoryNameSupplier 类型名提供者
*/
private void buildFlowTaskList(PlatformProcessFlow platformFlow, List flowList, TaskQuery taskQuery, String status,
FlowCategoryNameSupplier flowCategoryNameSupplier) {
if (platformFlow.getCategory() != null) {
taskQuery.processCategoryIn(Func.toStrList(platformFlow.getCategory()));
}
if (platformFlow.getProcessDefinitionName() != null) {
taskQuery.processDefinitionName(platformFlow.getProcessDefinitionName());
}
if (platformFlow.getBeginDate() != null) {
taskQuery.taskCreatedAfter(platformFlow.getBeginDate());
}
if (platformFlow.getEndDate() != null) {
taskQuery.taskCreatedBefore(platformFlow.getEndDate());
}
taskQuery.list().forEach(task -> {
PlatformProcessFlow flow = new PlatformProcessFlow().setTaskId(task.getId()).setTaskDefinitionKey(task.getTaskDefinitionKey())
.setTaskName(task.getName()).setAssignee(task.getAssignee()).setCreateTime(task.getCreateTime())
.setClaimTime(task.getClaimTime()).setExecutionId(task.getExecutionId()).setVariables(task.getProcessVariables());
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]);
}
ProcessDefinition processDefinition = ProcessFlowCacheUtil.getProcessDefinition(task.getProcessDefinitionId());
flow.setCategory(processDefinition.getCategory());
flow.setCategoryName(ProcessFlowCacheUtil.getCategoryName(processDefinition.getCategory(), flowCategoryNameSupplier));
flow.setProcessDefinitionId(processDefinition.getId());
flow.setProcessDefinitionName(processDefinition.getName());
flow.setProcessDefinitionKey(processDefinition.getKey());
flow.setProcessDefinitionVersion(processDefinition.getVersion());
flow.setProcessInstanceId(task.getProcessInstanceId());
flow.setStatus(status);
flowList.add(flow);
});
}
/**
* 获取历史流程。
*
* @param processInstanceId 流程实例id
* @return HistoricProcessInstance
*/
private HistoricProcessInstance getHistoricProcessInstance(String processInstanceId) {
return historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy