refactor: 重构查找候选人的逻辑
parent
5ae1d0ab3e
commit
e21c262bd7
@ -0,0 +1,105 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
||||||
|
import org.flowable.engine.delegate.DelegateExecution;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class BpmCandidateSourceInfoProcessorChain {
|
||||||
|
|
||||||
|
// 保存处理节点
|
||||||
|
|
||||||
|
private List<BpmCandidateSourceInfoProcessor> processorList;
|
||||||
|
@Resource
|
||||||
|
private AdminUserApi adminUserApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 可添加其他处理器
|
||||||
|
*
|
||||||
|
* @param processorOp
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Resource
|
||||||
|
// 动态扩展处理节点
|
||||||
|
public BpmCandidateSourceInfoProcessorChain addProcessor(ObjectProvider<BpmCandidateSourceInfoProcessor> processorOp) {
|
||||||
|
List<BpmCandidateSourceInfoProcessor> processor = processorOp.orderedStream().collect(Collectors.toList());
|
||||||
|
if (null == processorList) {
|
||||||
|
processorList = new ArrayList<>(processor.size());
|
||||||
|
}
|
||||||
|
processorList.addAll(processor);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取处理器处理
|
||||||
|
public Set<Long> process(BpmCandidateSourceInfo sourceInfo) throws Exception {
|
||||||
|
// Verify our parameters
|
||||||
|
if (sourceInfo == null) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
}
|
||||||
|
for (BpmCandidateSourceInfoProcessor processor : processorList) {
|
||||||
|
try {
|
||||||
|
for (BpmTaskCandidateVO vo : sourceInfo.getRules()) {
|
||||||
|
processor.validRuleOptions(vo.getType(), vo.getOptions());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Set<Long> saveResult = Collections.emptySet();
|
||||||
|
Exception saveException = null;
|
||||||
|
for (BpmCandidateSourceInfoProcessor processor : processorList) {
|
||||||
|
try {
|
||||||
|
saveResult = processor.process(sourceInfo, this);
|
||||||
|
if (CollUtil.isNotEmpty(saveResult)) {
|
||||||
|
removeDisableUsers(saveResult);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
saveException = e;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Return the exception or result state from the last execute()
|
||||||
|
if ((saveException != null)) {
|
||||||
|
throw saveException;
|
||||||
|
} else {
|
||||||
|
return (saveResult);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Long> calculateTaskCandidateUsers(DelegateExecution execution, BpmCandidateSourceInfo sourceInfo) {
|
||||||
|
sourceInfo.setExecution(execution);
|
||||||
|
Set<Long> results = Collections.emptySet();
|
||||||
|
try {
|
||||||
|
results = process(sourceInfo);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移除禁用用户
|
||||||
|
* @param assigneeUserIds
|
||||||
|
*/
|
||||||
|
public void removeDisableUsers(Set<Long> assigneeUserIds) {
|
||||||
|
if (CollUtil.isEmpty(assigneeUserIds)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(assigneeUserIds);
|
||||||
|
assigneeUserIds.removeIf(id -> {
|
||||||
|
AdminUserRespDTO user = userMap.get(id);
|
||||||
|
return user == null || !CommonStatusEnum.ENABLE.getStatus().equals(user.getStatus());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate.sourceInfoProcessor;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfo;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class BpmCandidateAdminUserApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
|
||||||
|
@Resource
|
||||||
|
private AdminUserApi api;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> getSupportedTypes() {
|
||||||
|
return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.USER.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validRuleOptions(Integer type, Set<Long> options) {
|
||||||
|
api.validateUserList(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateVO rule) {
|
||||||
|
return rule.getOptions();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,49 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate.sourceInfoProcessor;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfo;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
|
||||||
|
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
|
||||||
|
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||||
|
|
||||||
|
public class BpmCandidateDeptApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
|
||||||
|
@Resource
|
||||||
|
private DeptApi api;
|
||||||
|
@Resource
|
||||||
|
private AdminUserApi adminUserApi;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> getSupportedTypes() {
|
||||||
|
return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(),
|
||||||
|
BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validRuleOptions(Integer type, Set<Long> options) {
|
||||||
|
api.validateDeptList(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateVO rule) {
|
||||||
|
if (Objects.equals(BpmTaskAssignRuleTypeEnum.DEPT_MEMBER.getType(), rule.getType())) {
|
||||||
|
List<AdminUserRespDTO> users = adminUserApi.getUserListByDeptIds(rule.getOptions());
|
||||||
|
return convertSet(users, AdminUserRespDTO::getId);
|
||||||
|
} else if (Objects.equals(BpmTaskAssignRuleTypeEnum.DEPT_LEADER.getType(), rule.getType())) {
|
||||||
|
List<DeptRespDTO> depts = api.getDeptList(rule.getOptions());
|
||||||
|
return convertSet(depts, DeptRespDTO::getLeaderUserId);
|
||||||
|
}
|
||||||
|
return Collections.emptySet();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate.sourceInfoProcessor;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfo;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
|
||||||
|
import cn.iocoder.yudao.module.system.api.dept.PostApi;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
||||||
|
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||||
|
|
||||||
|
public class BpmCandidatePostApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
|
||||||
|
@Resource
|
||||||
|
private PostApi api;
|
||||||
|
@Resource
|
||||||
|
private AdminUserApi adminUserApi;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> getSupportedTypes() {
|
||||||
|
return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.POST.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validRuleOptions(Integer type, Set<Long> options) {
|
||||||
|
api.validPostList(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateVO rule) {
|
||||||
|
List<AdminUserRespDTO> users = adminUserApi.getUserListByPostIds(rule.getOptions());
|
||||||
|
return convertSet(users, AdminUserRespDTO::getId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate.sourceInfoProcessor;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfo;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
|
||||||
|
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
|
||||||
|
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class BpmCandidateRoleApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
|
||||||
|
@Resource
|
||||||
|
private RoleApi api;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private PermissionApi permissionApi;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> getSupportedTypes() {
|
||||||
|
return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.ROLE.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validRuleOptions(Integer type, Set<Long> options) {
|
||||||
|
api.validRoleList(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateVO rule) {
|
||||||
|
return permissionApi.getUserRoleIdListByRoleIds(rule.getOptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,73 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate.sourceInfoProcessor;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.DictTypeConstants;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.framework.flowable.core.behavior.script.BpmTaskAssignScript;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfo;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
|
||||||
|
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
|
||||||
|
import org.flowable.engine.delegate.DelegateExecution;
|
||||||
|
import org.springframework.beans.factory.ObjectProvider;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
|
||||||
|
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.TASK_ASSIGN_SCRIPT_NOT_EXISTS;
|
||||||
|
|
||||||
|
public class BpmCandidateScriptApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
|
||||||
|
@Resource
|
||||||
|
private DictDataApi dictDataApi;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 任务分配脚本
|
||||||
|
*/
|
||||||
|
private Map<Long, BpmTaskAssignScript> scriptMap = Collections.emptyMap();
|
||||||
|
|
||||||
|
public void setScripts(ObjectProvider<BpmTaskAssignScript> scriptsOp) {
|
||||||
|
List<BpmTaskAssignScript> scripts = scriptsOp.orderedStream().collect(Collectors.toList());
|
||||||
|
setScripts(scripts);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setScripts(List<BpmTaskAssignScript> scripts) {
|
||||||
|
this.scriptMap = convertMap(scripts, script -> script.getEnum().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> getSupportedTypes() {
|
||||||
|
return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.SCRIPT.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validRuleOptions(Integer type, Set<Long> options) {
|
||||||
|
dictDataApi.validateDictDataList(DictTypeConstants.TASK_ASSIGN_SCRIPT,
|
||||||
|
CollectionUtils.convertSet(options, String::valueOf));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateVO rule) {
|
||||||
|
return calculateTaskCandidateUsersByScript(request.getExecution(), rule.getOptions());
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<Long> calculateTaskCandidateUsersByScript(DelegateExecution execution, Set<Long> options) {
|
||||||
|
// 获得对应的脚本
|
||||||
|
List<BpmTaskAssignScript> scripts = new ArrayList<>(options.size());
|
||||||
|
options.forEach(id -> {
|
||||||
|
BpmTaskAssignScript script = scriptMap.get(id);
|
||||||
|
if (script == null) {
|
||||||
|
throw exception(TASK_ASSIGN_SCRIPT_NOT_EXISTS, id);
|
||||||
|
}
|
||||||
|
scripts.add(script);
|
||||||
|
});
|
||||||
|
// 逐个计算任务
|
||||||
|
Set<Long> userIds = new HashSet<>();
|
||||||
|
scripts.forEach(script -> CollUtil.addAll(userIds, script.calculateTaskCandidateUsers(execution)));
|
||||||
|
return userIds;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,40 @@
|
|||||||
|
package cn.iocoder.yudao.module.bpm.service.candidate.sourceInfoProcessor;
|
||||||
|
|
||||||
|
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
|
||||||
|
import cn.iocoder.yudao.module.bpm.controller.admin.candidate.vo.BpmTaskCandidateVO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmUserGroupDO;
|
||||||
|
import cn.iocoder.yudao.module.bpm.enums.definition.BpmTaskAssignRuleTypeEnum;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfo;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.candidate.BpmCandidateSourceInfoProcessor;
|
||||||
|
import cn.iocoder.yudao.module.bpm.service.definition.BpmUserGroupService;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class BpmCandidateUserGroupApiSourceInfoProcessor implements BpmCandidateSourceInfoProcessor {
|
||||||
|
@Resource
|
||||||
|
private BpmUserGroupService api;
|
||||||
|
@Resource
|
||||||
|
private BpmUserGroupService userGroupService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Integer> getSupportedTypes() {
|
||||||
|
return SetUtils.asSet(BpmTaskAssignRuleTypeEnum.USER_GROUP.getType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void validRuleOptions(Integer type, Set<Long> options) {
|
||||||
|
api.validUserGroups(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<Long> doProcess(BpmCandidateSourceInfo request, BpmTaskCandidateVO rule) {
|
||||||
|
List<BpmUserGroupDO> userGroups = userGroupService.getUserGroupList(rule.getOptions());
|
||||||
|
Set<Long> userIds = new HashSet<>();
|
||||||
|
userGroups.forEach(group -> userIds.addAll(group.getMemberUserIds()));
|
||||||
|
return userIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue