refactor(bpm): 优化流程实例创建后的抄送人处理逻辑

- 移除了延迟处理抄送人信息的方法 processInstanceCCDeferred
- 删除了冗余的 processCCToUsers 方法
-简化了抄送人信息的处理流程,直接在创建流程实例后进行处理
- 优化了代码结构,提高了代码的可读性和维护性
This commit is contained in:
aikai 2025-07-08 12:25:43 +08:00
parent 1bac18b92e
commit d91365ca0b

View File

@ -361,8 +361,8 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
.setResult(BpmProcessInstanceResultEnum.PROCESS.getResult());
processInstanceExtMapper.insert(instanceExtDO);
// 在记录创建后立即处理抄送人信息双重保险
// 在记录创建后立即处理抄送人信息
handleProcessInstanceCC(instance);
}
@ -373,14 +373,14 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
try {
// 获取流程变量
Map<String, Object> variables = runtimeService.getVariables(instance.getId());
// 根据流程名称流程发起人 查询流程配置的抄送人信息
List<BpmProcessCcDO> processCcList = processCcService.getCCListByName(instance.getName(), Long.valueOf(instance.getStartUserId()));
// 提取抄送信息用中对应的用户组编号
Set<Long> userGroupIds = processCcList.stream()
.flatMap(data -> data.getUserGroupId().stream())
.collect(Collectors.toSet());
if (CollectionUtil.isNotEmpty(userGroupIds)) {
// 获取用户组信息
List<BpmUserGroupDO> userGroups = userGroupService.getUserGroupList(userGroupIds);
@ -388,18 +388,18 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
Set<Long> userIds = userGroups.stream()
.flatMap(data -> data.getMemberUserIds().stream())
.collect(Collectors.toSet());
// 将用户ID列表转换为字符串
String ccIds = userIds.stream()
.map(id -> "[" + id + "]") // 每个值用方括号包裹
.collect(Collectors.joining());
// 直接更新刚创建的记录
BpmProcessInstanceExtDO updateDO = new BpmProcessInstanceExtDO()
.setProcessInstanceId(instance.getId())
.setFormVariables(variables)
.setCcids(ccIds);
processInstanceExtMapper.updateByProcessInstanceId(updateDO);
log.debug("事件监听器中抄送人信息设置成功processInstanceId: {}", instance.getId());
}
@ -565,28 +565,9 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
// 设置流程审批部门缓存
stringRedisTemplate.opsForValue().set("assignee_dept_" + instance.getId(), startUser.getDeptId().toString());
// 延迟处理抄送人信息避免与事件监听器的并发问题
processInstanceCCDeferred(instance, variables);
return instance.getId();
}
/**
* 延迟处理抄送人信息避免与事件监听器的并发问题
*/
private void processInstanceCCDeferred(ProcessInstance instance, Map<String, Object> variables) {
// 使用异步处理确保主流程不被阻塞
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(100); // 短暂延迟让事件监听器先执行
processCCToUsers(instance, variables);
} catch (Exception e) {
log.error("异步处理抄送人信息失败processInstanceId: {}", instance.getProcessInstanceId(), e);
}
});
}
private void approveSelfTask(String processInstanceId) {
List<Task> tasks = engineTaskService.createTaskQuery().processInstanceId(processInstanceId).list();
if (tasks != null && tasks.size() > 0) {
@ -740,112 +721,6 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
}
}
/**
* /创建流程后添加抄送人 Begin add by yj 2024.1.4
* 在设计流程的时候需要添加一个任务块 名字必须叫Activity_cc 分配权限的时候需要选择用户组
*
* @param definition
* @param instance
*/
private void processCCToUsers(ProcessDefinition definition, ProcessInstance instance) {
//获取bpm_task_assign_reule Bpm 任务规则表的流程中有没有配置抄送节点 固定抄送名称为Activity_cc
String processDefinitionId = definition.getId();
List<BpmTaskAssignRuleDO> rules = taskRuleMapper.selectListByProcessDefinitionId(processDefinitionId, null);
String BpmConstantsName = BpmConstants.CC_NAME;
// 获取发起人信息
AdminUserRespDTO userRespDTO = adminUserApi.getUser(Long.valueOf(instance.getStartUserId())).getCheckedData();
DeptRespDTO deptRespDTO = deptApi.getDept(userRespDTO.getDeptId()).getCheckedData();
//发起人是深圳分公司
if (deptRespDTO.getFlag().contains("136")) {
BpmConstantsName = BpmConstants.CCSZ_NAME;
}
for (BpmTaskAssignRuleDO rule : rules) {
String key = rule.getTaskDefinitionKey(); //任务名称
Integer type = rule.getType();
if (!key.isEmpty() && key.equals(BpmConstantsName) && type == 40) {
StringBuffer str = new StringBuffer();
Set<Long> options = rule.getOptions();
List<Long> list = new ArrayList(options);
for (Long groupId : list) {
//需要根据这个groupId查询这个组中的用户id
BpmUserGroupDO userGroup = userGroupService.getUserGroup(groupId);
Set<Long> userIds = userGroup.getMemberUserIds();
List<Long> userIdList = new ArrayList(userIds);
for (Long user_id : userIdList) {
str.append("[").append(user_id).append("]");
}
}
//流程是采购计划时
if (definition.getKey().equals(BpmOAProcureServiceImpl.PROCESS_KEY)) {
//发起人部门为 生产部及以下时
if (deptRespDTO.getFlag().contains("130")) {
//添加 供应部抄送
BpmUserGroupDO userGroup = userGroupService.getUserGroup(121L);
Set<Long> userIds = userGroup.getMemberUserIds();
List<Long> userIdList = new ArrayList(userIds);
for (Long user_id : userIdList) {
str.append("[").append(user_id).append("]");
}
}
}
String ccids = str.toString();
//根据processDefinitionId 将ccids保存到bpm_process_instance_ext中的ccids字段
BpmProcessInstanceExtDO instanceExtDO = new BpmProcessInstanceExtDO().setProcessDefinitionId(processDefinitionId)
.setCcids(ccids).setProcessInstanceId(instance.getProcessInstanceId());
processInstanceExtMapper.updateByProcessInstanceId(instanceExtDO);
break;
}
}
}
/**
* 创建流程后添加抄送人
* @param instance 流程实例
*/
private void processCCToUsers(ProcessInstance instance, Map<String, Object> variables) {
// 根据流程名称流程发起人 查询流程配置的抄送人信息
List<BpmProcessCcDO> processCcList = processCcService.getCCListByName(instance.getName(), Long.valueOf(instance.getStartUserId()));
// 提取抄送信息用中对应的用户组编号
Set<Long> userGroupIds = processCcList.stream()
.flatMap(data -> data.getUserGroupId().stream())
.collect(Collectors.toSet());
//根据processDefinitionId 将ccIds保存到bpm_process_instance_ext中的ccIds字段
BpmProcessInstanceExtDO instanceExtDO = new BpmProcessInstanceExtDO()
.setProcessDefinitionId(instance.getProcessDefinitionId())
.setFormVariables(variables)
.setProcessInstanceId(instance.getProcessInstanceId());
if (CollectionUtil.isNotEmpty(userGroupIds)) {
// 获取用户组信息
List<BpmUserGroupDO> userGroups = userGroupService.getUserGroupList(userGroupIds);
// 提取用户组中对应的用户ID
Set<Long> userIds = userGroups.stream()
.flatMap(data -> data.getMemberUserIds().stream())
.collect(Collectors.toSet());
// 将用户ID列表转换为字符串
String ccIds = userIds.stream()
.map(id -> "[" + id + "]") // 每个值用方括号包裹
.collect(Collectors.joining());
//根据processDefinitionId 将ccIds保存到bpm_process_instance_ext中的ccIds字段
instanceExtDO.setCcids(ccIds);
}
// 增加幂等性处理确保更新操作能正常命中
updateProcessInstanceExtWithRetry(instanceExtDO);
}
/**
* 带重试机制的更新操作确保幂等性
*/
@ -859,7 +734,7 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
log.debug("异步处理抄送人信息更新成功processInstanceId: {}", instanceExtDO.getProcessInstanceId());
return;
}
// 记录不存在等待一下再重试
Thread.sleep(50);
existingRecord = processInstanceExtMapper.selectByProcessInstanceId(instanceExtDO.getProcessInstanceId());