Commit 8649b285cfa7b85cc492f454aad89b7e32841b86
Committed by
Gitee
!631 【工作流】委派
Merge pull request !631 from Youkehai/feature/bpm-delegate
Showing
6 changed files
with
127 additions
and
7 deletions
yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/ErrorCodeConstants.java
| ... | ... | @@ -50,6 +50,8 @@ public interface ErrorCodeConstants { |
| 50 | 50 | ErrorCode TASK_TARGET_NODE_NOT_EXISTS = new ErrorCode(1_009_005_004, " 目标节点不存在"); |
| 51 | 51 | ErrorCode TASK_RETURN_FAIL_NO_RETURN_TASK = new ErrorCode(1_009_005_005, "回退任务失败,选择回退的节点没有需要回滚的任务!请重新选择其他任务节点"); |
| 52 | 52 | ErrorCode TASK_RETURN_FAIL_SOURCE_TARGET_ERROR = new ErrorCode(1_009_005_006, "回退任务失败,目标节点是在并行网关上或非同一路线上,不可跳转"); |
| 53 | + ErrorCode TASK_DELEGATE_USER_REPEAT = new ErrorCode(1_009_005_007, "任务委派失败,委派人和当前审批人为同一人"); | |
| 54 | + ErrorCode TASK_DELEGATE_USER_NULL = new ErrorCode(1_009_005_008, "任务委派失败,被委派人不存在"); | |
| 53 | 55 | |
| 54 | 56 | // ========== 流程任务分配规则 1-009-006-000 ========== |
| 55 | 57 | ErrorCode TASK_ASSIGN_RULE_EXISTS = new ErrorCode(1_009_006_000, "流程({}) 的任务({}) 已经存在分配规则"); | ... | ... |
yudao-module-bpm/yudao-module-bpm-api/src/main/java/cn/iocoder/yudao/module/bpm/enums/task/BpmProcessInstanceResultEnum.java
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/task/BpmTaskController.java
| ... | ... | @@ -91,4 +91,12 @@ public class BpmTaskController { |
| 91 | 91 | return success(true); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | + @PutMapping("/delegate") | |
| 95 | + @Operation(summary = "委派任务", description = "用于【流程详情】的【委派】按钮,和向前加签有点像,和向前加签的唯一的区别是没有单独创立任务") | |
| 96 | + @PreAuthorize("@ss.hasPermission('bpm:task:delegate')") | |
| 97 | + public CommonResult<Boolean> delegateTask(@Valid @RequestBody BpmTaskDelegateReqVO reqVO) { | |
| 98 | + taskService.delegateTask(reqVO,getLoginUserId()); | |
| 99 | + return success(true); | |
| 100 | + } | |
| 101 | + | |
| 94 | 102 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/controller/admin/task/vo/task/BpmTaskDelegateReqVO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task; | |
| 2 | + | |
| 3 | +import io.swagger.v3.oas.annotations.media.Schema; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +import javax.validation.constraints.NotEmpty; | |
| 7 | +import javax.validation.constraints.NotNull; | |
| 8 | + | |
| 9 | +@Schema(description = "管理后台 - 委派流程任务的 Request VO") | |
| 10 | +@Data | |
| 11 | +public class BpmTaskDelegateReqVO { | |
| 12 | + | |
| 13 | + @Schema(description = "任务编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") | |
| 14 | + @NotEmpty(message = "任务编号不能为空") | |
| 15 | + private String id; | |
| 16 | + | |
| 17 | + @Schema(description = "被委派人ID", requiredMode = Schema.RequiredMode.REQUIRED,example = "1") | |
| 18 | + @NotNull(message = "被委派人ID不能为空") | |
| 19 | + private Long delegateUserId; | |
| 20 | + | |
| 21 | + @Schema(description = "委派原因", requiredMode = Schema.RequiredMode.REQUIRED,example = "做不了决定,需要你先帮忙瞅瞅") | |
| 22 | + @NotEmpty(message = "委派原因不能为空") | |
| 23 | + private String reason; | |
| 24 | +} | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskService.java
| ... | ... | @@ -137,4 +137,11 @@ public interface BpmTaskService { |
| 137 | 137 | */ |
| 138 | 138 | void returnTask(BpmTaskReturnReqVO reqVO); |
| 139 | 139 | |
| 140 | + /** | |
| 141 | + * 将指定任务委派给其他人处理,等接收人处理后再回到原审批人手中审批 | |
| 142 | + * | |
| 143 | + * @param reqVO 被委派人和被委派的任务编号理由参数 | |
| 144 | + * @param userId 委派人ID | |
| 145 | + */ | |
| 146 | + void delegateTask(BpmTaskDelegateReqVO reqVO, Long userId); | |
| 140 | 147 | } | ... | ... |
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java
| ... | ... | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.framework.common.util.date.DateUtils; |
| 8 | 8 | import cn.iocoder.yudao.framework.common.util.number.NumberUtils; |
| 9 | 9 | import cn.iocoder.yudao.framework.common.util.object.PageUtils; |
| 10 | 10 | import cn.iocoder.yudao.framework.flowable.core.util.BpmnModelUtils; |
| 11 | +import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | |
| 11 | 12 | import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.*; |
| 12 | 13 | import cn.iocoder.yudao.module.bpm.convert.task.BpmTaskConvert; |
| 13 | 14 | import cn.iocoder.yudao.module.bpm.dal.dataobject.task.BpmTaskExtDO; |
| ... | ... | @@ -29,6 +30,7 @@ import org.flowable.engine.RuntimeService; |
| 29 | 30 | import org.flowable.engine.TaskService; |
| 30 | 31 | import org.flowable.engine.history.HistoricProcessInstance; |
| 31 | 32 | import org.flowable.engine.runtime.ProcessInstance; |
| 33 | +import org.flowable.task.api.DelegationState; | |
| 32 | 34 | import org.flowable.task.api.Task; |
| 33 | 35 | import org.flowable.task.api.TaskQuery; |
| 34 | 36 | import org.flowable.task.api.history.HistoricTaskInstance; |
| ... | ... | @@ -37,6 +39,7 @@ import org.springframework.stereotype.Service; |
| 37 | 39 | import org.springframework.transaction.annotation.Transactional; |
| 38 | 40 | import org.springframework.transaction.support.TransactionSynchronization; |
| 39 | 41 | import org.springframework.transaction.support.TransactionSynchronizationManager; |
| 42 | +import org.springframework.util.Assert; | |
| 40 | 43 | |
| 41 | 44 | import javax.annotation.Resource; |
| 42 | 45 | import javax.validation.Valid; |
| ... | ... | @@ -193,13 +196,42 @@ public class BpmTaskServiceImpl implements BpmTaskService { |
| 193 | 196 | if (instance == null) { |
| 194 | 197 | throw exception(PROCESS_INSTANCE_NOT_EXISTS); |
| 195 | 198 | } |
| 199 | + //被委派的任务,不调用 complete 去完成任务 | |
| 200 | + if (DelegationState.PENDING.equals(task.getDelegationState())) { | |
| 201 | + this.approveDelegateTask(reqVO, task); | |
| 202 | + } else { | |
| 203 | + // 完成任务,审批通过 | |
| 204 | + taskService.complete(task.getId(), instance.getProcessVariables()); | |
| 205 | + | |
| 206 | + // 更新任务拓展表为通过 | |
| 207 | + taskExtMapper.updateByTaskId( | |
| 208 | + new BpmTaskExtDO().setTaskId(task.getId()).setResult(BpmProcessInstanceResultEnum.APPROVE.getResult()) | |
| 209 | + .setReason(reqVO.getReason())); | |
| 210 | + } | |
| 211 | + } | |
| 196 | 212 | |
| 197 | - // 完成任务,审批通过 | |
| 198 | - taskService.complete(task.getId(), instance.getProcessVariables()); | |
| 199 | - | |
| 200 | - // 更新任务拓展表为通过 | |
| 213 | + /** | |
| 214 | + * 审批被委派的任务 | |
| 215 | + * | |
| 216 | + * @param reqVO 前端请求参数,包含当前任务ID,审批意见等 | |
| 217 | + * @param task 当前被审批的任务 | |
| 218 | + */ | |
| 219 | + private void approveDelegateTask(BpmTaskApproveReqVO reqVO, Task task) { | |
| 220 | + // 添加审批意见 | |
| 221 | + AdminUserRespDTO currentUser = adminUserApi.getUser(WebFrameworkUtils.getLoginUserId()); | |
| 222 | + //原审批人 | |
| 223 | + AdminUserRespDTO sourceApproveUser = adminUserApi.getUser(NumberUtils.parseLong(task.getOwner())); | |
| 224 | + Assert.notNull(sourceApproveUser, "委派任务找不到原审批人,需要检查数据"); | |
| 225 | + //添加审批意见 | |
| 226 | + String comment = StrUtil.format("[{}]完成委派任务,任务重新回到[{}]手中,审批意见为:{}", currentUser.getNickname(), | |
| 227 | + sourceApproveUser.getNickname(), reqVO.getReason()); | |
| 228 | + taskService.addComment(reqVO.getId(), task.getProcessInstanceId(), BpmProcessInstanceResultEnum.DELEGATE.getResult().toString(), comment); | |
| 229 | + //调用 resolveTask 完成任务,底层调用 TaskHelper.changeTaskAssignee(task, task.getOwner()); | |
| 230 | + //将 owner 设置为 assignee | |
| 231 | + taskService.resolveTask(task.getId()); | |
| 232 | + // 更新任务拓展表为【处理中】 | |
| 201 | 233 | taskExtMapper.updateByTaskId( |
| 202 | - new BpmTaskExtDO().setTaskId(task.getId()).setResult(BpmProcessInstanceResultEnum.APPROVE.getResult()) | |
| 234 | + new BpmTaskExtDO().setTaskId(task.getId()).setResult(BpmProcessInstanceResultEnum.PROCESS.getResult()) | |
| 203 | 235 | .setReason(reqVO.getReason())); |
| 204 | 236 | } |
| 205 | 237 | |
| ... | ... | @@ -319,7 +351,11 @@ public class BpmTaskServiceImpl implements BpmTaskService { |
| 319 | 351 | } |
| 320 | 352 | |
| 321 | 353 | private Task getTask(String id) { |
| 322 | - return taskService.createTaskQuery().taskId(id).singleResult(); | |
| 354 | + Task task = taskService.createTaskQuery().taskId(id).singleResult(); | |
| 355 | + if (null == task) { | |
| 356 | + throw exception(TASK_NOT_EXISTS); | |
| 357 | + } | |
| 358 | + return task; | |
| 323 | 359 | } |
| 324 | 360 | |
| 325 | 361 | /** |
| ... | ... | @@ -351,6 +387,7 @@ public class BpmTaskServiceImpl implements BpmTaskService { |
| 351 | 387 | if (task == null) { |
| 352 | 388 | throw exception(TASK_NOT_EXISTS); |
| 353 | 389 | } |
| 390 | + // 根据流程定义获取流程模型信息 | |
| 354 | 391 | BpmnModel bpmnModel = bpmModelService.getBpmnModelByDefinitionId(task.getProcessDefinitionId()); |
| 355 | 392 | FlowElement source = BpmnModelUtils.getFlowElementById(bpmnModel, task.getTaskDefinitionKey()); |
| 356 | 393 | if (source == null) { |
| ... | ... | @@ -445,4 +482,44 @@ public class BpmTaskServiceImpl implements BpmTaskService { |
| 445 | 482 | .changeState(); |
| 446 | 483 | } |
| 447 | 484 | |
| 485 | + @Override | |
| 486 | + @Transactional(rollbackFor = Exception.class) | |
| 487 | + public void delegateTask(BpmTaskDelegateReqVO reqVO,Long userId) { | |
| 488 | + // 校验任务 | |
| 489 | + Task task = this.validateTaskDelegate(reqVO); | |
| 490 | + // 添加审批意见 | |
| 491 | + AdminUserRespDTO currentUser = adminUserApi.getUser(userId); | |
| 492 | + AdminUserRespDTO delegateUser = adminUserApi.getUser(reqVO.getDelegateUserId()); | |
| 493 | + if (delegateUser == null) { | |
| 494 | + throw exception(TASK_DELEGATE_USER_NULL); | |
| 495 | + } | |
| 496 | + String comment = StrUtil.format("[{}]将任务委派给[{}],委派理由为:{}", currentUser.getNickname(), | |
| 497 | + delegateUser.getNickname(), reqVO.getReason()); | |
| 498 | + String taskId = reqVO.getId(); | |
| 499 | + taskService.addComment(taskId, task.getProcessInstanceId(), BpmProcessInstanceResultEnum.DELEGATE.getResult().toString(), comment); | |
| 500 | + // 设置任务所有人 (owner) 为原任务的处理人 (assignee) | |
| 501 | + taskService.setOwner(taskId, task.getAssignee()); | |
| 502 | + // 执行委派,将任务委派给 receiveId | |
| 503 | + taskService.delegateTask(taskId, reqVO.getDelegateUserId().toString()); | |
| 504 | + // 更新任务拓展表为【委派】 | |
| 505 | + taskExtMapper.updateByTaskId( | |
| 506 | + new BpmTaskExtDO().setTaskId(task.getId()).setResult(BpmProcessInstanceResultEnum.DELEGATE.getResult()) | |
| 507 | + .setReason(reqVO.getReason())); | |
| 508 | + } | |
| 509 | + | |
| 510 | + /** | |
| 511 | + * 校验任务委派参数 | |
| 512 | + * | |
| 513 | + * @param reqVO 任务编号,接收人ID | |
| 514 | + * @return 当前任务信息 | |
| 515 | + */ | |
| 516 | + private Task validateTaskDelegate(BpmTaskDelegateReqVO reqVO) { | |
| 517 | + // 校验任务 | |
| 518 | + Task task = checkTask(WebFrameworkUtils.getLoginUserId(), reqVO.getId()); | |
| 519 | + //校验当前审批人和被委派人不是同一人 | |
| 520 | + if (task.getAssignee().equals(reqVO.getDelegateUserId().toString())) { | |
| 521 | + throw exception(TASK_DELEGATE_USER_REPEAT); | |
| 522 | + } | |
| 523 | + return task; | |
| 524 | + } | |
| 448 | 525 | } | ... | ... |