update 优化 优化工作流代码写法

This commit is contained in:
疯狂的狮子Li
2025-07-03 13:44:59 +08:00
parent c690a30221
commit 39079e53aa
6 changed files with 64 additions and 72 deletions

View File

@@ -206,7 +206,7 @@ public class FlwTaskController extends BaseController {
*/
@GetMapping("/currentTaskAllUser/{taskId}")
public R<List<RemoteUserVo>> currentTaskAllUser(@PathVariable Long taskId) {
return R.ok(flwTaskService.currentTaskAllUser(taskId));
return R.ok(flwTaskService.currentTaskAllUser(List.of(taskId)));
}
}

View File

@@ -1,6 +1,7 @@
package org.dromara.workflow.listener;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
@@ -136,33 +137,36 @@ public class WorkflowGlobalListener implements GlobalListener {
return;
}
// 只有办理或者退回的时候才执行消息通知和抄送
if (TaskStatusEnum.PASS.getStatus().equals(flowParams.getHisStatus())
|| TaskStatusEnum.BACK.getStatus().equals(flowParams.getHisStatus())) {
if (variable != null) {
if (variable.containsKey(FlowConstant.FLOW_COPY_LIST)) {
List<FlowCopyBo> flowCopyList = (List<FlowCopyBo>) variable.get(FlowConstant.FLOW_COPY_LIST);
// 添加抄送人
flwTaskService.setCopy(task, flowCopyList);
}
if (variable.containsKey(FlowConstant.MESSAGE_TYPE)) {
List<String> messageType = (List<String>) variable.get(FlowConstant.MESSAGE_TYPE);
String notice = (String) variable.get(FlowConstant.MESSAGE_NOTICE);
// 消息通知
if (CollUtil.isNotEmpty(messageType)) {
flwCommonService.sendMessage(definition.getFlowName(), instance.getId(), messageType, notice);
}
}
FlowInstance ins = new FlowInstance();
Map<String, Object> variableMap = instance.getVariableMap();
variableMap.remove(FlowConstant.FLOW_COPY_LIST);
variableMap.remove(FlowConstant.MESSAGE_TYPE);
variableMap.remove(FlowConstant.MESSAGE_NOTICE);
variableMap.remove(FlowConstant.SUBMIT);
ins.setId(instance.getId());
ins.setVariable(FlowEngine.jsonConvert.objToStr(variableMap));
insService.updateById(ins);
if (!StringUtils.equalsAny(flowParams.getHisStatus(),
TaskStatusEnum.PASS.getStatus(), TaskStatusEnum.BACK.getStatus())) {
return;
}
if (ObjectUtil.isNull(variable)) {
return;
}
if (variable.containsKey(FlowConstant.FLOW_COPY_LIST)) {
List<FlowCopyBo> flowCopyList = MapUtil.get(variable, FlowConstant.FLOW_COPY_LIST, new TypeReference<>() {});
// 添加抄送人
flwTaskService.setCopy(task, flowCopyList);
}
if (variable.containsKey(FlowConstant.MESSAGE_TYPE)) {
List<String> messageType = MapUtil.get(variable, FlowConstant.FLOW_COPY_LIST, new TypeReference<>() {});
String notice = MapUtil.getStr(variable, FlowConstant.MESSAGE_NOTICE);
// 消息通知
if (CollUtil.isNotEmpty(messageType)) {
flwCommonService.sendMessage(definition.getFlowName(), instance.getId(), messageType, notice);
}
}
FlowInstance ins = new FlowInstance();
Map<String, Object> variableMap = instance.getVariableMap();
variableMap.remove(FlowConstant.FLOW_COPY_LIST);
variableMap.remove(FlowConstant.MESSAGE_TYPE);
variableMap.remove(FlowConstant.MESSAGE_NOTICE);
variableMap.remove(FlowConstant.SUBMIT);
ins.setId(instance.getId());
ins.setVariable(FlowEngine.jsonConvert.objToStr(variableMap));
insService.updateById(ins);
}
/**

View File

@@ -177,10 +177,10 @@ public interface IFlwTaskService {
/**
* 获取当前任务的所有办理人
*
* @param taskId 任务id
* @param taskIds 任务id
* @return 结果
*/
List<RemoteUserVo> currentTaskAllUser(Long taskId);
List<RemoteUserVo> currentTaskAllUser(List<Long> taskIds);
/**
* 按照节点编码查询节点

View File

@@ -11,26 +11,17 @@ import org.dromara.common.core.utils.StringUtils;
import org.dromara.resource.api.RemoteMailService;
import org.dromara.resource.api.RemoteMessageService;
import org.dromara.system.api.domain.vo.RemoteUserVo;
import org.dromara.warm.flow.core.FlowEngine;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.entity.Node;
import org.dromara.warm.flow.core.entity.Task;
import org.dromara.warm.flow.core.entity.User;
import org.dromara.warm.flow.core.enums.SkipType;
import org.dromara.warm.flow.core.service.NodeService;
import org.dromara.warm.flow.core.service.UserService;
import org.dromara.warm.flow.core.utils.MapUtil;
import org.dromara.warm.flow.orm.entity.FlowTask;
import org.dromara.warm.flow.orm.entity.FlowUser;
import org.dromara.workflow.common.ConditionalOnEnable;
import org.dromara.workflow.common.enums.MessageTypeEnum;
import org.dromara.workflow.common.enums.TaskAssigneeType;
import org.dromara.workflow.service.IFlwCommonService;
import org.dromara.workflow.service.IFlwTaskAssigneeService;
import org.dromara.workflow.service.IFlwTaskService;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
@@ -62,38 +53,34 @@ public class FlwCommonServiceImpl implements IFlwCommonService {
@Override
public void sendMessage(String flowName, Long instId, List<String> messageType, String message) {
IFlwTaskService flwTaskService = SpringUtils.getBean(IFlwTaskService.class);
List<RemoteUserVo> userList = new ArrayList<>();
List<FlowTask> list = flwTaskService.selectByInstId(instId);
if (StringUtils.isBlank(message)) {
message = "有新的【" + flowName + "】单据已经提交至您,请您及时处理。";
}
for (Task task : list) {
List<RemoteUserVo> users = flwTaskService.currentTaskAllUser(task.getId());
if (CollUtil.isNotEmpty(users)) {
userList.addAll(users);
}
List<RemoteUserVo> userList = flwTaskService.currentTaskAllUser(StreamUtils.toList(list, FlowTask::getId));
if (CollUtil.isEmpty(userList)) {
return;
}
if (CollUtil.isNotEmpty(userList)) {
for (String code : messageType) {
MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByCode(code);
if (ObjectUtil.isNotEmpty(messageTypeEnum)) {
switch (messageTypeEnum) {
case SYSTEM_MESSAGE:
List<Long> userIds = StreamUtils.toList(userList, RemoteUserVo::getUserId).stream().distinct().collect(Collectors.toList());
remoteMessageService.publishMessage(userIds, message);
break;
case EMAIL_MESSAGE:
remoteMailService.send(StreamUtils.join(userList, RemoteUserVo::getEmail), "单据审批提醒", message);
break;
case SMS_MESSAGE:
//todo 短信发送
break;
default:
throw new IllegalStateException("Unexpected value: " + messageTypeEnum);
}
for (String code : messageType) {
MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByCode(code);
if (ObjectUtil.isEmpty(messageTypeEnum)) {
continue;
}
switch (messageTypeEnum) {
case SYSTEM_MESSAGE -> {
List<Long> userIds = StreamUtils.toList(userList, RemoteUserVo::getUserId).stream().distinct().collect(Collectors.toList());
remoteMessageService.publishMessage(userIds, message);
}
case EMAIL_MESSAGE -> {
remoteMailService.send(StreamUtils.join(userList, RemoteUserVo::getEmail), "单据审批提醒", message);
}
case SMS_MESSAGE -> {
//todo 短信发送
}
default -> throw new IllegalStateException("Unexpected value: " + messageTypeEnum);
}
}
}
/**

View File

@@ -123,8 +123,8 @@ public class FlwDefinitionServiceImpl implements IFlwDefinitionService {
List<FlowNode> flowNodes = flowNodeMapper.selectList(new LambdaQueryWrapper<FlowNode>().eq(FlowNode::getDefinitionId, id));
List<String> errorMsg = new ArrayList<>();
if (CollUtil.isNotEmpty(flowNodes)) {
String applyNodeCode = flwCommonService.applyNodeCode(id);
for (FlowNode flowNode : flowNodes) {
String applyNodeCode = flwCommonService.applyNodeCode(id);
if (StringUtils.isBlank(flowNode.getPermissionFlag()) && !applyNodeCode.equals(flowNode.getNodeCode()) && NodeType.BETWEEN.getKey().equals(flowNode.getNodeType())) {
errorMsg.add(flowNode.getNodeName());
}
@@ -215,7 +215,8 @@ public class FlwDefinitionServiceImpl implements IFlwDefinitionService {
return;
}
FlowCategory flowCategory = flwCategoryMapper.selectOne(new LambdaQueryWrapper<FlowCategory>()
.eq(FlowCategory::getTenantId, DEFAULT_TENANT_ID).eq(FlowCategory::getCategoryId, FlowConstant.FLOW_CATEGORY_ID));
.eq(FlowCategory::getTenantId, DEFAULT_TENANT_ID)
.eq(FlowCategory::getCategoryId, FlowConstant.FLOW_CATEGORY_ID));
flowCategory.setCategoryId(null);
flowCategory.setTenantId(tenantId);
flowCategory.setCreateDept(null);

View File

@@ -535,12 +535,12 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
//构建以下节点数据
List<Task> buildNextTaskList = StreamUtils.toList(nextNodeList, node -> taskService.addTask(node, instance, definition, FlowParams.build()));
//办理人变量替换
ExpressionUtil.evalVariable(buildNextTaskList,
FlowParams.build()
.variable(mergeVariable)
);
ExpressionUtil.evalVariable(buildNextTaskList, FlowParams.build().variable(mergeVariable));
for (FlowNode flowNode : nextFlowNodes) {
buildNextTaskList.stream().filter(t -> t.getNodeCode().equals(flowNode.getNodeCode())).findFirst().ifPresent(t -> {
Optional<Task> first = buildNextTaskList.stream()
.filter(t -> t.getNodeCode().equals(flowNode.getNodeCode()))
.findFirst();
first.ifPresent(t -> {
if (CollUtil.isNotEmpty(t.getPermissionList())) {
List<RemoteUserVo> users = flwTaskAssigneeService.fetchUsersByStorageIds(String.join(StringUtils.SEPARATOR, t.getPermissionList()));
if (CollUtil.isNotEmpty(users)) {
@@ -685,12 +685,12 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
/**
* 获取当前任务的所有办理人
*
* @param taskId 任务id
* @param taskIds 任务id
*/
@Override
public List<RemoteUserVo> currentTaskAllUser(Long taskId) {
public List<RemoteUserVo> currentTaskAllUser(List<Long> taskIds) {
// 获取与当前任务关联的用户列表
List<User> userList = FlowEngine.userService().getByAssociateds(Collections.singletonList(taskId));
List<User> userList = FlowEngine.userService().getByAssociateds(taskIds);
if (CollUtil.isEmpty(userList)) {
return Collections.emptyList();
}