1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
| @RestController @RequestMapping("/api/leave") public class LeaveProcessController {
@Resource private RuntimeService runtimeService;
@Resource private TaskService taskService;
@Resource private HistoryService historyService;
@Resource private IdentityService identityService;
@PostMapping("/start") public ResponseEntity<Map<String, Object>> startLeaveProcess(@RequestBody LeaveApplicationDto application) { try { identityService.setAuthenticatedUserId(application.getApplicant());
Map<String, Object> variables = new HashMap<>(); variables.put("applicant", application.getApplicant()); variables.put("leaveType", application.getLeaveType()); variables.put("startDate", application.getStartDate()); variables.put("endDate", application.getEndDate()); variables.put("leaveDays", application.getLeaveDays()); variables.put("reason", application.getReason()); variables.put("departmentManager", "manager_" + getDepartment(application.getApplicant())); variables.put("director", "director_company");
var instance = runtimeService.startProcessInstanceByKey("LeaveProcess", variables);
Map<String, Object> result = new HashMap<>(); result.put("processInstanceId", instance.getId()); result.put("message", "请假流程已启动");
return ResponseEntity.ok(result);
} finally { identityService.clearAuthentication(); } }
@GetMapping("/tasks/{userId}") public ResponseEntity<List<Map<String, Object>>> getUserTasks(@PathVariable String userId) { List<Task> tasks = taskService.createTaskQuery() .taskAssignee(userId) .orderByTaskCreateTime() .desc() .list();
List<Map<String, Object>> taskList = tasks.stream().map(task -> { Map<String, Object> taskInfo = new HashMap<>(); taskInfo.put("taskId", task.getId()); taskInfo.put("taskName", task.getName()); taskInfo.put("processInstanceId", task.getProcessInstanceId()); taskInfo.put("createTime", task.getCreateTime()); taskInfo.put("dueDate", task.getDueDate());
Map<String, Object> variables = taskService.getVariables(task.getId()); taskInfo.put("applicant", variables.get("applicant")); taskInfo.put("leaveType", variables.get("leaveType")); taskInfo.put("leaveDays", variables.get("leaveDays")); taskInfo.put("startDate", variables.get("startDate"));
return taskInfo; }).collect(Collectors.toList());
return ResponseEntity.ok(taskList); }
@PostMapping("/approve/{taskId}") public ResponseEntity<Map<String, Object>> approveTask( @PathVariable String taskId, @RequestParam Boolean approved, @RequestParam(required = false) String comment) {
Map<String, Object> variables = new HashMap<>();
Task task = taskService.createTaskQuery().taskId(taskId).singleResult(); if (task == null) { throw new RuntimeException("任务不存在"); }
if ("UserTask_ManagerApprove".equals(task.getTaskDefinitionKey())) { variables.put("managerApproved", approved); variables.put("managerComment", comment); } else if ("UserTask_DirectorApprove".equals(task.getTaskDefinitionKey())) { variables.put("directorApproved", approved); variables.put("directorComment", comment); }
taskService.complete(taskId, variables);
Map<String, Object> result = new HashMap<>(); result.put("message", "审批完成"); result.put("taskId", taskId); result.put("approved", approved);
return ResponseEntity.ok(result); }
@GetMapping("/history/{processInstanceId}") public ResponseEntity<Map<String, Object>> getProcessHistory(@PathVariable String processInstanceId) { HistoricProcessInstance processInstance = historyService .createHistoricProcessInstanceQuery() .processInstanceId(processInstanceId) .singleResult();
List<HistoricActivityInstance> activities = historyService .createHistoricActivityInstanceQuery() .processInstanceId(processInstanceId) .orderByHistoricActivityInstanceStartTime() .asc() .list();
Map<String, Object> history = new HashMap<>(); history.put("processInstance", processInstance); history.put("activities", activities);
return ResponseEntity.ok(history); }
private String getDepartment(String userId) { return "tech"; } }
@Slf4j @Component public class HRRecordService implements JavaDelegate {
@Override public void execute(DelegateExecution execution) throws Exception { String processInstanceId = execution.getProcessInstanceId(); String applicant = (String) execution.getVariable("applicant"); Double leaveDays = (Double) execution.getVariable("leaveDays"); String leaveType = (String) execution.getVariable("leaveType");
log.info("人事备案 - 流程实例: {}, 申请人: {}, 请假类型: {}, 天数: {}", processInstanceId, applicant, leaveType, leaveDays);
execution.setVariable("hrRecorded", true); execution.setVariable("recordTime", LocalDateTime.now()); } }
@Slf4j @Component public class NotificationService implements JavaDelegate {
@Override public void execute(DelegateExecution execution) throws Exception { String applicant = (String) execution.getVariable("applicant"); Boolean approved = false; String comment = "";
if (execution.hasVariable("managerApproved")) { approved = (Boolean) execution.getVariable("managerApproved"); comment = (String) execution.getVariable("managerComment"); } else if (execution.hasVariable("directorApproved")) { approved = (Boolean) execution.getVariable("directorApproved"); comment = (String) execution.getVariable("directorComment"); }
if (!approved) { log.info("发送通知 - 申请人: {}, 审批结果: 拒绝, 原因: {}", applicant, comment); } } }
|