|
@@ -0,0 +1,111 @@
|
|
|
+package com.iamberry.rst.service.approval;
|
|
|
+
|
|
|
+import com.iamberry.rst.faces.approval.ApprovalTaskService;
|
|
|
+import org.activiti.engine.RuntimeService;
|
|
|
+import org.activiti.engine.TaskService;
|
|
|
+import org.activiti.engine.runtime.ProcessInstance;
|
|
|
+import org.activiti.engine.task.Task;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author 献
|
|
|
+ * @company 深圳爱贝源科技有限公司
|
|
|
+ * @date 2018/9/25 16:03
|
|
|
+ * @tel 18271840547
|
|
|
+ * @website www.iamberry.com
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class ApprovalTaskServiceImpl implements ApprovalTaskService {
|
|
|
+
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(ApprovalTaskServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RuntimeService runtimeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TaskService taskService;
|
|
|
+
|
|
|
+ private static final String GENERATION_APPROVAL_KEY = "GenerationApproval";
|
|
|
+
|
|
|
+ private static final String APPLICATION_FOR_DELIVERY_KEY = "ApplicationForDelivery";
|
|
|
+
|
|
|
+ private static final String SPLIT_ASSIGNEE_CHAR = "&";
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> salesmanSubmitApply(int type) {
|
|
|
+ Map<String, Object> result = new HashMap<>(4);
|
|
|
+ result.put("status", 1);
|
|
|
+ // 使用流程定义的key启动流程实例
|
|
|
+ ProcessInstance pi = runtimeService.startProcessInstanceByKey(type == 1 ? GENERATION_APPROVAL_KEY : APPLICATION_FOR_DELIVERY_KEY);
|
|
|
+ // 因为第一个审批节点为用户自己,所以默认启动并通过第一个审批流程
|
|
|
+ Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
|
|
|
+ if (task == null) {
|
|
|
+ result.put("status", 0);
|
|
|
+ result.put("key", pi.getId());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ taskService.complete(task.getId());
|
|
|
+ // 寻找下一个审批人
|
|
|
+ Map<String, Object> approver = findTaskNextAssignee(pi.getId());
|
|
|
+ if (approver == null) {
|
|
|
+ result.put("status", 0);
|
|
|
+ result.put("key", pi.getId());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result.put("key", pi.getId());
|
|
|
+ result.put("users", approver.get("assignee"));
|
|
|
+ result.put("taskId", approver.get("taskId"));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, Object> completeTask(String pid, String taskId) {
|
|
|
+ Map<String, Object> result = new HashMap<>(3);
|
|
|
+ result.put("status", 0);
|
|
|
+ // 提交任务
|
|
|
+ taskService.complete(taskId);
|
|
|
+ // 是否存在下一个审批人
|
|
|
+ Map<String, Object> approver = findTaskNextAssignee(pid);
|
|
|
+ if (approver == null) {
|
|
|
+ result.put("status", 1);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result.put("users", approver.get("assignee"));
|
|
|
+ result.put("taskId", approver.get("taskId"));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void revokeTask(String pid, String reason) {
|
|
|
+ runtimeService.deleteProcessInstance(pid, reason);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 寻找任务的下一个审批人
|
|
|
+ * @param pid 任务实例id
|
|
|
+ * @return 审批人,系统中用户的登录名
|
|
|
+ */
|
|
|
+ private Map<String, Object> findTaskNextAssignee(String pid) {
|
|
|
+ Map<String, Object> result = new HashMap<>(2);
|
|
|
+ // 搜索当前实例的待处理信息
|
|
|
+ Task nextTask = taskService.createTaskQuery().processInstanceId(pid).singleResult();
|
|
|
+ if (nextTask == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String assignee = nextTask.getAssignee();
|
|
|
+ if (assignee.contains(SPLIT_ASSIGNEE_CHAR)) {
|
|
|
+ result.put("taskId", nextTask.getId());
|
|
|
+ result.put("assignee", assignee.split(SPLIT_ASSIGNEE_CHAR));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result.put("taskId", nextTask.getId());
|
|
|
+ result.put("assignee", assignee);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|