update 优化 工作流代码写法

This commit is contained in:
疯狂的狮子Li 2025-07-03 14:48:53 +08:00
parent 589ec1fdbc
commit 176793e15b
6 changed files with 67 additions and 66 deletions

View File

@ -206,7 +206,7 @@ public class FlwTaskController extends BaseController {
*/ */
@GetMapping("/currentTaskAllUser/{taskId}") @GetMapping("/currentTaskAllUser/{taskId}")
public R<List<UserDTO>> currentTaskAllUser(@PathVariable Long taskId) { public R<List<UserDTO>> 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; package org.dromara.workflow.listener;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
@ -136,33 +137,36 @@ public class WorkflowGlobalListener implements GlobalListener {
return; return;
} }
// 只有办理或者退回的时候才执行消息通知和抄送 // 只有办理或者退回的时候才执行消息通知和抄送
if (TaskStatusEnum.PASS.getStatus().equals(flowParams.getHisStatus()) if (!StringUtils.equalsAny(flowParams.getHisStatus(),
|| TaskStatusEnum.BACK.getStatus().equals(flowParams.getHisStatus())) { TaskStatusEnum.PASS.getStatus(), TaskStatusEnum.BACK.getStatus())) {
if (variable != null) { return;
if (variable.containsKey(FlowConstant.FLOW_COPY_LIST)) { }
List<FlowCopyBo> flowCopyList = (List<FlowCopyBo>) variable.get(FlowConstant.FLOW_COPY_LIST); if (ObjectUtil.isNull(variable)) {
// 添加抄送人 return;
flwTaskService.setCopy(task, flowCopyList); }
}
if (variable.containsKey(FlowConstant.MESSAGE_TYPE)) { if (variable.containsKey(FlowConstant.FLOW_COPY_LIST)) {
List<String> messageType = (List<String>) variable.get(FlowConstant.MESSAGE_TYPE); List<FlowCopyBo> flowCopyList = MapUtil.get(variable, FlowConstant.FLOW_COPY_LIST, new TypeReference<>() {});
String notice = (String) variable.get(FlowConstant.MESSAGE_NOTICE); // 添加抄送人
// 消息通知 flwTaskService.setCopy(task, flowCopyList);
if (CollUtil.isNotEmpty(messageType)) { }
flwCommonService.sendMessage(definition.getFlowName(), instance.getId(), messageType, notice); 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);
FlowInstance ins = new FlowInstance(); // 消息通知
Map<String, Object> variableMap = instance.getVariableMap(); if (CollUtil.isNotEmpty(messageType)) {
variableMap.remove(FlowConstant.FLOW_COPY_LIST); flwCommonService.sendMessage(definition.getFlowName(), instance.getId(), messageType, notice);
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);
} }
} }
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 结果 * @return 结果
*/ */
List<UserDTO> currentTaskAllUser(Long taskId); List<UserDTO> currentTaskAllUser(List<Long> taskIds);
/** /**
* 按照节点编码查询节点 * 按照节点编码查询节点

View File

@ -49,40 +49,36 @@ public class FlwCommonServiceImpl implements IFlwCommonService {
@Override @Override
public void sendMessage(String flowName, Long instId, List<String> messageType, String message) { public void sendMessage(String flowName, Long instId, List<String> messageType, String message) {
IFlwTaskService flwTaskService = SpringUtils.getBean(IFlwTaskService.class); IFlwTaskService flwTaskService = SpringUtils.getBean(IFlwTaskService.class);
List<UserDTO> userList = new ArrayList<>();
List<FlowTask> list = flwTaskService.selectByInstId(instId); List<FlowTask> list = flwTaskService.selectByInstId(instId);
if (StringUtils.isBlank(message)) { if (StringUtils.isBlank(message)) {
message = "有新的【" + flowName + "】单据已经提交至您,请您及时处理。"; message = "有新的【" + flowName + "】单据已经提交至您,请您及时处理。";
} }
for (Task task : list) { List<UserDTO> userList = flwTaskService.currentTaskAllUser(StreamUtils.toList(list, FlowTask::getId));
List<UserDTO> users = flwTaskService.currentTaskAllUser(task.getId()); if (CollUtil.isEmpty(userList)) {
if (CollUtil.isNotEmpty(users)) { return;
userList.addAll(users);
}
} }
if (CollUtil.isNotEmpty(userList)) { for (String code : messageType) {
for (String code : messageType) { MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByCode(code);
MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByCode(code); if (ObjectUtil.isEmpty(messageTypeEnum)) {
if (ObjectUtil.isNotEmpty(messageTypeEnum)) { continue;
switch (messageTypeEnum) { }
case SYSTEM_MESSAGE: switch (messageTypeEnum) {
SseMessageDto dto = new SseMessageDto(); case SYSTEM_MESSAGE -> {
dto.setUserIds(StreamUtils.toList(userList, UserDTO::getUserId).stream().distinct().collect(Collectors.toList())); SseMessageDto dto = new SseMessageDto();
dto.setMessage(message); dto.setUserIds(StreamUtils.toList(userList, UserDTO::getUserId).stream().distinct().collect(Collectors.toList()));
SseMessageUtils.publishMessage(dto); dto.setMessage(message);
break; SseMessageUtils.publishMessage(dto);
case EMAIL_MESSAGE:
MailUtils.sendText(StreamUtils.join(userList, UserDTO::getEmail), "单据审批提醒", message);
break;
case SMS_MESSAGE:
//todo 短信发送
break;
default:
throw new IllegalStateException("Unexpected value: " + messageTypeEnum);
}
} }
case EMAIL_MESSAGE -> {
MailUtils.sendText(StreamUtils.join(userList, UserDTO::getEmail), "单据审批提醒", message);
}
case SMS_MESSAGE -> {
//todo 短信发送
}
default -> throw new IllegalStateException("Unexpected value: " + messageTypeEnum);
} }
} }
} }

View File

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

View File

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