|
| 1 | +package top.lrshuai.camunda.controller; |
| 2 | + |
| 3 | +import jakarta.annotation.Resource; |
| 4 | +import org.camunda.bpm.engine.HistoryService; |
| 5 | +import org.camunda.bpm.engine.IdentityService; |
| 6 | +import org.camunda.bpm.engine.RuntimeService; |
| 7 | +import org.camunda.bpm.engine.TaskService; |
| 8 | +import org.camunda.bpm.engine.history.HistoricActivityInstance; |
| 9 | +import org.camunda.bpm.engine.history.HistoricProcessInstance; |
| 10 | +import org.camunda.bpm.engine.task.Task; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.*; |
| 13 | +import top.lrshuai.camunda.dto.LeaveApplicationDto; |
| 14 | + |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +@RestController |
| 21 | +@RequestMapping("/api/leave") |
| 22 | +public class LeaveProcessController { |
| 23 | + |
| 24 | + @Resource |
| 25 | + private RuntimeService runtimeService; |
| 26 | + |
| 27 | + @Resource |
| 28 | + private TaskService taskService; |
| 29 | + |
| 30 | + @Resource |
| 31 | + private HistoryService historyService; |
| 32 | + |
| 33 | + @Resource |
| 34 | + private IdentityService identityService; |
| 35 | + |
| 36 | + /** |
| 37 | + * 启动请假流程 |
| 38 | + */ |
| 39 | + @PostMapping("/start") |
| 40 | + public ResponseEntity<Map<String, Object>> startLeaveProcess(@RequestBody LeaveApplicationDto application) { |
| 41 | + try { |
| 42 | + // 设置流程启动者 |
| 43 | + identityService.setAuthenticatedUserId(application.getApplicant()); |
| 44 | + |
| 45 | + Map<String, Object> variables = new HashMap<>(); |
| 46 | + variables.put("applicant", application.getApplicant()); |
| 47 | + variables.put("leaveType", application.getLeaveType()); |
| 48 | + variables.put("startDate", application.getStartDate()); |
| 49 | + variables.put("endDate", application.getEndDate()); |
| 50 | + variables.put("leaveDays", application.getLeaveDays()); |
| 51 | + variables.put("reason", application.getReason()); |
| 52 | + // 设置审批人(实际项目中可以从用户服务获取) |
| 53 | + variables.put("departmentManager", "manager_" + getDepartment(application.getApplicant())); |
| 54 | + variables.put("director", "director_company"); |
| 55 | + |
| 56 | + var instance = runtimeService.startProcessInstanceByKey("LeaveProcess", variables); |
| 57 | + |
| 58 | + Map<String, Object> result = new HashMap<>(); |
| 59 | + result.put("processInstanceId", instance.getId()); |
| 60 | + result.put("message", "请假流程已启动"); |
| 61 | + |
| 62 | + return ResponseEntity.ok(result); |
| 63 | + |
| 64 | + } finally { |
| 65 | + identityService.clearAuthentication(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * 获取用户待办任务 |
| 71 | + * @param userId 用户 |
| 72 | + */ |
| 73 | + @GetMapping("/tasks/{userId}") |
| 74 | + public ResponseEntity<List<Map<String, Object>>> getUserTasks(@PathVariable String userId) { |
| 75 | + List<Task> tasks = taskService.createTaskQuery() |
| 76 | + .taskAssignee(userId) |
| 77 | + .orderByTaskCreateTime() |
| 78 | + .desc() |
| 79 | + .list(); |
| 80 | + |
| 81 | + List<Map<String, Object>> taskList = tasks.stream().map(task -> { |
| 82 | + Map<String, Object> taskInfo = new HashMap<>(); |
| 83 | + taskInfo.put("taskId", task.getId()); |
| 84 | + taskInfo.put("taskName", task.getName()); |
| 85 | + taskInfo.put("processInstanceId", task.getProcessInstanceId()); |
| 86 | + taskInfo.put("createTime", task.getCreateTime()); |
| 87 | + taskInfo.put("dueDate", task.getDueDate()); |
| 88 | + |
| 89 | + // 获取流程变量 |
| 90 | + Map<String, Object> variables = taskService.getVariables(task.getId()); |
| 91 | + taskInfo.put("applicant", variables.get("applicant")); |
| 92 | + taskInfo.put("leaveType", variables.get("leaveType")); |
| 93 | + taskInfo.put("leaveDays", variables.get("leaveDays")); |
| 94 | + taskInfo.put("startDate", variables.get("startDate")); |
| 95 | + |
| 96 | + return taskInfo; |
| 97 | + }).collect(Collectors.toList()); |
| 98 | + |
| 99 | + return ResponseEntity.ok(taskList); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * 审批任务 |
| 104 | + * @param taskId 任务id |
| 105 | + * @param approved 审批变量 |
| 106 | + * @param comment 审批意见 |
| 107 | + */ |
| 108 | + @PostMapping("/approve/{taskId}") |
| 109 | + public ResponseEntity<Map<String, Object>> approveTask( |
| 110 | + @PathVariable String taskId, |
| 111 | + @RequestParam Boolean approved, |
| 112 | + @RequestParam(required = false) String comment) { |
| 113 | + |
| 114 | + Map<String, Object> variables = new HashMap<>(); |
| 115 | + |
| 116 | + Task task = taskService.createTaskQuery().taskId(taskId).singleResult(); |
| 117 | + if (task == null) { |
| 118 | + throw new RuntimeException("任务不存在"); |
| 119 | + } |
| 120 | + |
| 121 | + // 根据任务ID设置对应的审批变量 |
| 122 | + if ("UserTask_ManagerApprove".equals(task.getTaskDefinitionKey())) { |
| 123 | + variables.put("managerApproved", approved); |
| 124 | + variables.put("managerComment", comment); |
| 125 | + } else if ("UserTask_DirectorApprove".equals(task.getTaskDefinitionKey())) { |
| 126 | + variables.put("directorApproved", approved); |
| 127 | + variables.put("directorComment", comment); |
| 128 | + } |
| 129 | + |
| 130 | + taskService.complete(taskId, variables); |
| 131 | + |
| 132 | + Map<String, Object> result = new HashMap<>(); |
| 133 | + result.put("message", "审批完成"); |
| 134 | + result.put("taskId", taskId); |
| 135 | + result.put("approved", approved); |
| 136 | + |
| 137 | + return ResponseEntity.ok(result); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * 获取流程历史 |
| 142 | + * @param processInstanceId 请假实例id |
| 143 | + */ |
| 144 | + @GetMapping("/history/{processInstanceId}") |
| 145 | + public ResponseEntity<Map<String, Object>> getProcessHistory(@PathVariable String processInstanceId) { |
| 146 | + HistoricProcessInstance processInstance = historyService |
| 147 | + .createHistoricProcessInstanceQuery() |
| 148 | + .processInstanceId(processInstanceId) |
| 149 | + .singleResult(); |
| 150 | + |
| 151 | + List<HistoricActivityInstance> activities = historyService |
| 152 | + .createHistoricActivityInstanceQuery() |
| 153 | + .processInstanceId(processInstanceId) |
| 154 | + .orderByHistoricActivityInstanceStartTime() |
| 155 | + .asc() |
| 156 | + .list(); |
| 157 | + |
| 158 | + Map<String, Object> history = new HashMap<>(); |
| 159 | + history.put("processInstance", processInstance); |
| 160 | + history.put("activities", activities); |
| 161 | + |
| 162 | + return ResponseEntity.ok(history); |
| 163 | + } |
| 164 | + |
| 165 | + private String getDepartment(String userId) { |
| 166 | + // 模拟根据用户ID获取部门信息 |
| 167 | + // 实际项目中应该调用用户服务 |
| 168 | + return "tech"; // 返回部门代码 |
| 169 | + } |
| 170 | +} |
0 commit comments