package com.iailab.module.bpm.framework.flowable.core.candidate.expression; import com.iailab.framework.common.util.number.NumberUtils; import com.iailab.module.bpm.service.task.BpmProcessInstanceService; import com.iailab.module.system.api.dept.DeptApi; import com.iailab.module.system.api.dept.dto.DeptRespDTO; import com.iailab.module.system.api.user.AdminUserApi; import com.iailab.module.system.api.user.dto.AdminUserRespDTO; import org.flowable.engine.delegate.DelegateExecution; import org.flowable.engine.runtime.ProcessInstance; import org.springframework.stereotype.Component; import org.springframework.util.Assert; import javax.annotation.Resource; import java.util.Set; import static com.iailab.framework.common.util.collection.SetUtils.asSet; import static java.util.Collections.emptySet; /** * 分配给发起人的 Leader 审批的 Expression 流程表达式 * 目前 Leader 的定义是,发起人所在部门的 Leader * * @author iailab */ @Component public class BpmTaskAssignLeaderExpression { @Resource private AdminUserApi adminUserApi; @Resource private DeptApi deptApi; @Resource private BpmProcessInstanceService processInstanceService; /** * 计算审批的候选人 * * @param execution 流程执行实体 * @param level 指定级别 * @return 指定级别的领导 */ public Set calculateUsers(DelegateExecution execution, int level) { Assert.isTrue(level > 0, "level 必须大于 0"); // 获得发起人 ProcessInstance processInstance = processInstanceService.getProcessInstance(execution.getProcessInstanceId()); Long startUserId = NumberUtils.parseLong(processInstance.getStartUserId()); // 获得对应 leve 的部门 DeptRespDTO dept = null; for (int i = 0; i < level; i++) { // 获得 level 对应的部门 if (dept == null) { dept = getStartUserDept(startUserId); if (dept == null) { // 找不到发起人的部门,所以无法使用该规则 return emptySet(); } } else { DeptRespDTO parentDept = deptApi.getDept(dept.getParentId()).getCheckedData(); if (parentDept == null) { // 找不到父级部门,所以只好结束寻找。原因是:例如说,级别比较高的人,所在部门层级比较少 break; } dept = parentDept; } } return dept.getLeaderUserId() != null ? asSet(dept.getLeaderUserId()) : emptySet(); } private DeptRespDTO getStartUserDept(Long startUserId) { AdminUserRespDTO startUser = adminUserApi.getUser(startUserId).getCheckedData(); if (startUser.getDeptId() == null) { // 找不到部门,所以无法使用该规则 return null; } return deptApi.getDept(startUser.getDeptId()).getCheckedData(); } }