ContractApplyUserInnerServiceSMOImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package com.java110.common.smo.impl;
  2. import com.java110.core.base.smo.BaseServiceSMO;
  3. import com.java110.dto.PageDto;
  4. import com.java110.dto.contract.ContractDto;
  5. import com.java110.dto.workflow.WorkflowDto;
  6. import com.java110.entity.audit.AuditUser;
  7. import com.java110.intf.common.IContractApplyUserInnerServiceSMO;
  8. import com.java110.intf.common.IWorkflowInnerServiceSMO;
  9. import com.java110.intf.store.IContractInnerServiceSMO;
  10. import com.java110.po.contract.ContractPo;
  11. import com.java110.utils.util.Assert;
  12. import com.java110.utils.util.FlowUtil;
  13. import com.java110.utils.util.StringUtil;
  14. import org.activiti.bpmn.model.*;
  15. import org.activiti.engine.*;
  16. import org.activiti.engine.history.HistoricProcessInstance;
  17. import org.activiti.engine.history.HistoricTaskInstance;
  18. import org.activiti.engine.history.HistoricTaskInstanceQuery;
  19. import org.activiti.engine.impl.identity.Authentication;
  20. import org.activiti.engine.query.Query;
  21. import org.activiti.engine.runtime.ProcessInstance;
  22. import org.activiti.engine.task.Task;
  23. import org.activiti.engine.task.TaskQuery;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.web.bind.annotation.RequestBody;
  26. import org.springframework.web.bind.annotation.RestController;
  27. import java.util.ArrayList;
  28. import java.util.HashMap;
  29. import java.util.List;
  30. import java.util.Map;
  31. //@Service("resourceEntryStoreSMOImpl")
  32. @RestController
  33. public class ContractApplyUserInnerServiceSMOImpl extends BaseServiceSMO implements IContractApplyUserInnerServiceSMO {
  34. @Autowired
  35. private ProcessEngine processEngine;
  36. @Autowired
  37. private RuntimeService runtimeService;
  38. @Autowired
  39. private TaskService taskService;
  40. @Autowired
  41. private IContractInnerServiceSMO contractInnerServiceSMOImpl;
  42. @Autowired
  43. private IWorkflowInnerServiceSMO workflowInnerServiceSMOImpl;
  44. @Autowired
  45. private RepositoryService repositoryService;
  46. /**
  47. * 启动流程
  48. *
  49. * @return
  50. */
  51. public ContractDto startProcess(@RequestBody ContractDto contractDto) {
  52. //将信息加入map,以便传入流程中
  53. Map<String, Object> variables = new HashMap<String, Object>();
  54. variables.put("contractDto", contractDto);
  55. variables.put("userId", contractDto.getCurrentUserId());
  56. variables.put("startUserId", contractDto.getCurrentUserId());
  57. variables.put("nextUserId", contractDto.getNextUserId());
  58. //开启流程
  59. ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(getWorkflowDto(contractDto.getStoreId()), contractDto.getContractId(), variables);
  60. //将得到的实例流程id值赋给之前设置的变量
  61. String processInstanceId = processInstance.getId();
  62. // System.out.println("流程开启成功.......实例流程id:" + processInstanceId);
  63. contractDto.setProcessInstanceId(processInstanceId);
  64. return contractDto;
  65. }
  66. /**
  67. * 查询用户任务数
  68. *
  69. * @param user
  70. * @return
  71. */
  72. public long getUserTaskCount(@RequestBody AuditUser user) {
  73. TaskService taskService = processEngine.getTaskService();
  74. TaskQuery query = taskService.createTaskQuery().processDefinitionKey(getWorkflowDto(user.getStoreId()));
  75. query.taskAssignee(user.getUserId());
  76. return query.count();
  77. }
  78. /**
  79. * 获取用户任务
  80. *
  81. * @param user 用户信息
  82. */
  83. public List<ContractDto> getUserTasks(@RequestBody AuditUser user) {
  84. TaskService taskService = processEngine.getTaskService();
  85. TaskQuery query = taskService.createTaskQuery().processDefinitionKey(getWorkflowDto(user.getStoreId()));
  86. query.taskAssignee(user.getUserId());
  87. query.orderByTaskCreateTime().desc();
  88. List<Task> list = null;
  89. if (user.getPage() >= 1) {
  90. user.setPage(user.getPage() - 1);
  91. }
  92. if (user.getPage() != PageDto.DEFAULT_PAGE) {
  93. list = query.listPage(user.getPage(), user.getRow());
  94. } else {
  95. list = query.list();
  96. }
  97. List<String> contractIds = new ArrayList<>();
  98. Map<String, String> taskBusinessKeyMap = new HashMap<>();
  99. for (Task task : list) {
  100. String processInstanceId = task.getProcessInstanceId();
  101. //3.使用流程实例,查询
  102. ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
  103. //4.使用流程实例对象获取BusinessKey
  104. String business_key = pi.getBusinessKey();
  105. contractIds.add(business_key);
  106. taskBusinessKeyMap.put(business_key, task.getId());
  107. //计算是否有 审核按钮
  108. BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());
  109. FlowNode flowNode = (FlowNode) bpmnModel.getFlowElement(task.getTaskDefinitionKey());
  110. //获取当前节点输出连线
  111. List<SequenceFlow> outgoingFlows = flowNode.getOutgoingFlows();
  112. //计算是否有结束按钮
  113. boolean isReturn;
  114. for (SequenceFlow outgoingFlow : outgoingFlows) {
  115. //获取输出节点元素
  116. FlowElement targetFlowElement = outgoingFlow.getTargetFlowElement();
  117. isReturn = false;
  118. //排除非用户任务接点
  119. if (targetFlowElement instanceof UserTask) {
  120. //判断输出节点的el表达式
  121. Map vars = new HashMap();
  122. vars.put("flag", "false"); // 1100
  123. if (FlowUtil.isCondition(outgoingFlow.getConditionExpression(), vars)) {
  124. isReturn = true;
  125. }
  126. if (!isReturn) {
  127. String assignee = ((UserTask) targetFlowElement).getAssignee();
  128. if (!StringUtil.isEmpty(assignee) && assignee.indexOf("${") < 0) {
  129. taskBusinessKeyMap.put(business_key+"_hasAudit", task.getId());
  130. }
  131. if ("${startUserId}".equals(assignee)) {
  132. taskBusinessKeyMap.put(business_key+"_hasAudit", task.getId());
  133. }
  134. if ("${nextUserId}".equals(assignee)) {
  135. taskBusinessKeyMap.put(business_key+"_hasAudit", task.getId());
  136. }
  137. }
  138. }
  139. //如果下一个为 结束节点
  140. if (targetFlowElement instanceof EndEvent) {
  141. //true 获取输出节点名称
  142. taskBusinessKeyMap.put(business_key+"_hasEnd", task.getId());
  143. }
  144. }
  145. }
  146. if (contractIds == null || contractIds.size() == 0) {
  147. return new ArrayList<>();
  148. }
  149. //查询 投诉信息
  150. ContractDto contractDto = new ContractDto();
  151. contractDto.setStoreId(user.getStoreId());
  152. contractDto.setContractIds(contractIds.toArray(new String[contractIds.size()]));
  153. List<ContractDto> tmpContractDtos = contractInnerServiceSMOImpl.queryContracts(contractDto);
  154. for (ContractDto tmpContractDto : tmpContractDtos) {
  155. tmpContractDto.setTaskId(taskBusinessKeyMap.get(tmpContractDto.getContractId()));
  156. tmpContractDto.setHasAudit(taskBusinessKeyMap.get(tmpContractDto.getContractId()+"_hasAudit"));
  157. tmpContractDto.setHasEnd(taskBusinessKeyMap.get(tmpContractDto.getContractId()+"_hasEnd"));
  158. }
  159. return tmpContractDtos;
  160. }
  161. public boolean agreeCompleteTask(@RequestBody ContractDto contractDto) {
  162. TaskService taskService = processEngine.getTaskService();
  163. Map<String, Object> variables = new HashMap<String, Object>();
  164. variables.put("auditCode", contractDto.getAuditCode());
  165. taskService.complete(contractDto.getTaskId(), variables);
  166. return true;
  167. }
  168. public boolean refuteCompleteTask(@RequestBody ContractDto contractDto) {
  169. TaskService taskService = processEngine.getTaskService();
  170. Map<String, Object> variables = new HashMap<String, Object>();
  171. variables.put("auditCode", contractDto.getAuditCode());
  172. taskService.complete(contractDto.getTaskId(), variables);
  173. return true;
  174. }
  175. /**
  176. * 审核 当前任务
  177. *
  178. * @param contractDto 资源订单
  179. * @return
  180. */
  181. public boolean complete(@RequestBody ContractDto contractDto) {
  182. TaskService taskService = processEngine.getTaskService();
  183. taskService.complete(contractDto.getTaskId());
  184. return true;
  185. }
  186. private String getWorkflowDto(String storeId) {
  187. //开启流程
  188. //WorkflowDto.DEFAULT_PROCESS + workflowDto.getFlowId()
  189. WorkflowDto workflowDto = new WorkflowDto();
  190. workflowDto.setFlowType(WorkflowDto.FLOW_TYPE_CONTRACT_APPLY);
  191. workflowDto.setStoreId(storeId);
  192. List<WorkflowDto> workflowDtos = workflowInnerServiceSMOImpl.queryWorkflows(workflowDto);
  193. Assert.listOnlyOne(workflowDtos, "未找到 合同起草续签流程或找到多条,请在物业账号系统管理下流程管理中配置流程");
  194. WorkflowDto tmpWorkflowDto = workflowDtos.get(0);
  195. if (StringUtil.isEmpty(tmpWorkflowDto.getProcessDefinitionKey())) {
  196. throw new IllegalArgumentException("合同起草续签流程还未部署");
  197. }
  198. return WorkflowDto.DEFAULT_PROCESS + tmpWorkflowDto.getFlowId();
  199. }
  200. /**
  201. * 查询用户任务数
  202. *
  203. * @param user
  204. * @return
  205. */
  206. public long getUserHistoryTaskCount(@RequestBody AuditUser user) {
  207. HistoryService historyService = processEngine.getHistoryService();
  208. // Query query = historyService.createHistoricTaskInstanceQuery()
  209. // .processDefinitionKey("complaint")
  210. // .taskAssignee(user.getUserId());
  211. HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery()
  212. .processDefinitionKey(getWorkflowDto(user.getStoreId()))
  213. .taskAssignee(user.getUserId())
  214. .finished();
  215. if (!StringUtil.isEmpty(user.getAuditLink()) && "START".equals(user.getAuditLink())) {
  216. historicTaskInstanceQuery.taskName("complaint");
  217. } else if (!StringUtil.isEmpty(user.getAuditLink()) && "AUDIT".equals(user.getAuditLink())) {
  218. historicTaskInstanceQuery.taskName("complaitDealUser");
  219. }
  220. Query query = historicTaskInstanceQuery;
  221. return query.count();
  222. }
  223. /**
  224. * 获取用户审批的任务
  225. *
  226. * @param user 用户信息
  227. */
  228. public List<ContractDto> getUserHistoryTasks(@RequestBody AuditUser user) {
  229. HistoryService historyService = processEngine.getHistoryService();
  230. HistoricTaskInstanceQuery historicTaskInstanceQuery = historyService.createHistoricTaskInstanceQuery()
  231. .processDefinitionKey(getWorkflowDto(user.getStoreId()))
  232. .taskAssignee(user.getUserId())
  233. .finished();
  234. // if (!StringUtil.isEmpty(user.getAuditLink()) && "START".equals(user.getAuditLink())) {
  235. // historicTaskInstanceQuery.taskName("complaint");
  236. // } else if (!StringUtil.isEmpty(user.getAuditLink()) && "AUDIT".equals(user.getAuditLink())) {
  237. // historicTaskInstanceQuery.taskName("complaitDealUser");
  238. // }
  239. Query query = historicTaskInstanceQuery.orderByHistoricTaskInstanceStartTime().desc();
  240. List<HistoricTaskInstance> list = null;
  241. if (user.getPage() != PageDto.DEFAULT_PAGE) {
  242. list = query.listPage((user.getPage() - 1) * user.getRow(), user.getRow());
  243. } else {
  244. list = query.list();
  245. }
  246. List<String> contractIds = new ArrayList<>();
  247. Map<String, String> taskBusinessKeyMap = new HashMap<>();
  248. for (HistoricTaskInstance task : list) {
  249. String processInstanceId = task.getProcessInstanceId();
  250. //3.使用流程实例,查询
  251. HistoricProcessInstance pi = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
  252. //4.使用流程实例对象获取BusinessKey
  253. String business_key = pi.getBusinessKey();
  254. contractIds.add(business_key);
  255. taskBusinessKeyMap.put(business_key, task.getId());
  256. }
  257. //查询 投诉信息
  258. ContractDto contractDto = new ContractDto();
  259. contractDto.setStoreId(user.getStoreId());
  260. contractDto.setContractIds(contractIds.toArray(new String[contractIds.size()]));
  261. List<ContractDto> tmpContractDtos = contractInnerServiceSMOImpl.queryContracts(contractDto);
  262. for (ContractDto tmpContractDto : tmpContractDtos) {
  263. tmpContractDto.setTaskId(taskBusinessKeyMap.get(tmpContractDto.getContractId()));
  264. }
  265. return tmpContractDtos;
  266. }
  267. public boolean completeTask(@RequestBody ContractDto contractDto) {
  268. TaskService taskService = processEngine.getTaskService();
  269. Task task = taskService.createTaskQuery().taskId(contractDto.getTaskId()).singleResult();
  270. String processInstanceId = task.getProcessInstanceId();
  271. Authentication.setAuthenticatedUserId(contractDto.getCurrentUserId());
  272. taskService.addComment(contractDto.getTaskId(), processInstanceId, contractDto.getAuditMessage());
  273. Map<String, Object> variables = new HashMap<String, Object>();
  274. variables.put("auditCode", contractDto.getAuditCode());
  275. variables.put("currentUserId", contractDto.getCurrentUserId());
  276. variables.put("flag", "1200".equals(contractDto.getAuditCode()) ? "false" : "true");
  277. variables.put("startUserId", contractDto.getStartUserId());
  278. variables.put("nextUserId", contractDto.getNextUserId());
  279. taskService.complete(contractDto.getTaskId(), variables);
  280. ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
  281. if (pi == null) {
  282. return true;
  283. }
  284. return false;
  285. }
  286. //删除任务
  287. public boolean deleteTask(@RequestBody ContractPo contractDto) {
  288. TaskService taskService = processEngine.getTaskService();
  289. TaskQuery query = taskService.createTaskQuery().processInstanceBusinessKey(contractDto.getContractId());
  290. query.orderByTaskCreateTime().desc();
  291. List<Task> list = query.list();
  292. if (list == null || list.size() < 1) {
  293. return true;
  294. }
  295. for (Task task : list) {
  296. String processInstanceId = task.getProcessInstanceId();
  297. //3.使用流程实例,查询
  298. runtimeService.deleteProcessInstance(processInstanceId, "取消合同");
  299. }
  300. return true;
  301. }
  302. }