HsFlowEngineServiceImpl.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. package com.willalp.flow.service.impl;
  2. import cn.hutool.json.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.willalp.common.exception.base.BaseException;
  6. import com.willalp.common.utils.DateUtils;
  7. import com.willalp.common.utils.SecurityUtils;
  8. import com.willalp.common.utils.StringUtils;
  9. import com.willalp.flow.domain.*;
  10. import com.willalp.flow.domain.enums.FlowEnum;
  11. import com.willalp.flow.domain.req.FLowReq;
  12. import com.willalp.flow.mapper.*;
  13. import com.willalp.flow.service.HsFlowEngineService;
  14. import com.willalp.flow.service.IBizLeaveImgService;
  15. import com.willalp.flow.service.IHsFlowConditionModelService;
  16. import com.willalp.flow.service.IHsFlowRunningRecordService;
  17. import com.willalp.msg.domain.HsAppUserMsg;
  18. import com.willalp.msg.service.IHsAppUserMsgService;
  19. import com.willalp.wx.domain.WxMsgTemplateApproval;
  20. import com.willalp.wx.domain.WxMsgTemplateApprovalResult;
  21. import com.willalp.wx.enums.WxTemplateKeyEnum;
  22. import com.willalp.wx.service.WxMsgService;
  23. import com.willalp.wx.utils.WxUtils;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import javax.annotation.Resource;
  27. import java.text.ParseException;
  28. import java.text.SimpleDateFormat;
  29. import java.time.LocalDateTime;
  30. import java.time.format.DateTimeFormatter;
  31. import java.util.*;
  32. import java.util.stream.Collectors;
  33. @Service
  34. public class HsFlowEngineServiceImpl implements HsFlowEngineService {
  35. @Resource
  36. private HsFlowBaseMapper hsFlowBaseMapper;
  37. @Resource
  38. private HsFlowExamplesMapper hsFlowExamplesMapper;
  39. @Resource
  40. private HsFlowModelMapper hsFlowModelMapper;
  41. @Resource
  42. private IHsFlowRunningRecordService flowRunningRecordService;
  43. @Resource
  44. private HsFlowTaskMapper hsFlowTaskMapper;
  45. @Resource
  46. private HsFlowConditionModelMapper flowConditionModelMapper;
  47. @Resource
  48. private IHsFlowConditionModelService conditionModelService;
  49. @Resource
  50. private IBizLeaveImgService leaveImgService;
  51. @Resource
  52. private WxMsgService wxMsgService;
  53. @Resource
  54. private WxUtils wxUtils;
  55. @Resource
  56. private IHsAppUserMsgService userMsgService;
  57. @Override
  58. public void submitATask(HsFlowTask task, Map<String, String> params) {
  59. try {
  60. String examplesId = task.getExamplesId();
  61. if (StringUtils.isBlank(examplesId)) {
  62. throw new BaseException("EXAMPLES_ID - IS_NULL");
  63. }
  64. HsFlowBase hsFlowBase = hsFlowBaseMapper.firstFlowBase(examplesId);
  65. if (null == hsFlowBase) {
  66. throw new BaseException("请创建实例");
  67. }
  68. task.setInitiator(SecurityUtils.getUsername());
  69. task.setBaseId(hsFlowBase.getId());
  70. task.setTaskName("请假");
  71. task.setCreateTime(new Date());
  72. if (ObjectUtils.isNotNull(params)) {
  73. String userName = params.get("candidate");
  74. if (StringUtils.isNotBlank(userName)) {
  75. hsFlowBase.setOperator(userName);
  76. task.setOperator(userName);
  77. }
  78. }
  79. hsFlowBaseMapper.updateById(hsFlowBase);
  80. hsFlowTaskMapper.insert(task);
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. throw new BaseException("提交失败");
  84. }
  85. }
  86. @Transactional(rollbackFor = Exception.class)
  87. @Override
  88. public int nextStep(FLowReq flow) {
  89. if (StringUtils.isBlank(flow.getBaseId())) {
  90. throw new BaseException("FLOW_BASE_ID - IS_NULL");
  91. }
  92. if (StringUtils.isBlank(flow.getTaskId())) {
  93. throw new BaseException("FLOW_TASK_ID - IS_NULL");
  94. }
  95. if (StringUtils.isBlank(flow.getResult())) {
  96. throw new BaseException("FLOW_TASK_ID - IS_NULL");
  97. }
  98. //获取当前基础组件
  99. HsFlowBase hsFlowBase = hsFlowBaseMapper.selectById(flow.getBaseId());
  100. if (null == hsFlowBase) {
  101. throw new BaseException("没有找到组件");
  102. }
  103. //获取任务实例
  104. HsFlowTask hsFlowTask = hsFlowTaskMapper.selectById(flow.getTaskId());
  105. if (null == hsFlowTask) {
  106. throw new BaseException("没有找到任务实例");
  107. }
  108. if (!SecurityUtils.getUsername().equals(hsFlowTask.getOperator())) {
  109. throw new BaseException("无操作权限");
  110. }
  111. if (FlowEnum.IS_ONE.getValue().equals(hsFlowTask.getIsEnd())) {
  112. throw new BaseException("任务已经结束");
  113. }
  114. String stepId = null;
  115. //获取是否 是同意结果
  116. boolean resultTrue = FlowEnum.RESULT_TRUE.getValue().equals(flow.getResult());
  117. boolean resultCaseOf = FlowEnum.RESULT_CASE_OF.getValue().equals(flow.getResult());
  118. JSONObject jsonObject = flow.getJsonObject();
  119. List<HsFlowRunningRecord> list = new ArrayList<>();
  120. //生成记录
  121. HsFlowRunningRecord frr = new HsFlowRunningRecord();
  122. frr.setBaseId(flow.getBaseId());
  123. frr.setTaskId(flow.getTaskId());
  124. frr.setCreateTime(new Date());
  125. if (resultCaseOf) {
  126. //转交
  127. hsFlowTask.setOperator(flow.getNextApproval());
  128. frr.setComment("转交至 " + flow.getNextUserName());
  129. frr.setOperator(SecurityUtils.getUsername());
  130. frr.setYesOrNo(flow.getResult());
  131. } else {
  132. //查看有无条件组件
  133. if (StringUtils.isNotBlank(hsFlowBase.getTermModuleId()) &&
  134. null != jsonObject && resultTrue) {
  135. //遍历绑定的条件组件 查找下一步 (待添加条件级别优化)
  136. List<HsFlowConditionModel> hsFlowConditionModels = flowConditionModelMapper.queryConditionGroup(hsFlowBase.getTermModuleId());
  137. for (HsFlowConditionModel item : hsFlowConditionModels) {
  138. String paramName = item.getParamName();
  139. if (null != jsonObject.get(paramName)) {
  140. if (conditionModelService.executeConditionJudgment(item, jsonObject.get(paramName)) && item.getConditionType().equals("2")) {
  141. stepId = item.getDownStepId();
  142. break;
  143. }
  144. if (conditionModelService.executeConditionJudgment(item, jsonObject.get(paramName))) {
  145. stepId = item.getDownStepId();
  146. }
  147. }
  148. }
  149. } else {
  150. if (resultTrue) {
  151. stepId = hsFlowBase.getDownStepId();
  152. } else {
  153. stepId = hsFlowBase.getUpStepId();
  154. }
  155. }
  156. frr.setComment(flow.getComment());
  157. frr.setOperator(SecurityUtils.getUsername());
  158. frr.setYesOrNo(flow.getResult());
  159. }
  160. list.add(frr);
  161. if (StringUtils.isNotBlank(stepId)) {
  162. hsFlowTask.setBaseId(stepId);
  163. if (StringUtils.isNotBlank(flow.getNextApproval())) {
  164. frr.setOperator(SecurityUtils.getUsername());
  165. hsFlowTask.setOperator(flow.getNextApproval());
  166. } else if (!resultTrue) {
  167. //拒绝时 通过记录 反向比对 查找上一步 同意的数据并 更新任务
  168. List<HsFlowRunningRecord> flowRunningRecords = flowRunningRecordService.selectHsFlowRunningRecordTrue(flow.getTaskId());
  169. int count = 0;
  170. for (int i = 0; i < flowRunningRecords.size(); i++) {
  171. String baseId = flowRunningRecords.get(i).getBaseId();
  172. if (StringUtils.isNotBlank(baseId) && StringUtils.equals(baseId, flow.getBaseId())) {
  173. hsFlowTask.setBaseId(flowRunningRecords.get(i - 1).getBaseId());
  174. hsFlowTask.setOperator(flowRunningRecords.get(i - 1).getOperator());
  175. count++;
  176. break;
  177. }
  178. }
  179. //没有找到 默认当前
  180. if (count == 0) {
  181. int i = flowRunningRecords.size() - 1;
  182. hsFlowTask.setBaseId(flowRunningRecords.get(i).getBaseId());
  183. hsFlowTask.setOperator(flowRunningRecords.get(i).getOperator());
  184. }
  185. } else {
  186. //避免异常找不到任务默认admin
  187. hsFlowTask.setOperator("admin");
  188. throw new BaseException("未选择下一步审批人员");
  189. }
  190. //判断新组件下一步是否需要选择人员
  191. HsFlowBase hsFlowBase1 = hsFlowBaseMapper.selectById(stepId);
  192. if (null != hsFlowBase1 && StringUtils.isBlank(hsFlowBase1.getDownStepId()) &&
  193. StringUtils.isBlank(hsFlowBase1.getTermModuleId())) {
  194. hsFlowTask.setIsCandidate("1");
  195. } else {
  196. hsFlowTask.setIsCandidate("0");
  197. }
  198. }
  199. //任务结束
  200. if (StringUtils.isBlank(stepId) &&
  201. StringUtils.isBlank(hsFlowBase.getDownStepId()) &&
  202. !resultCaseOf) {
  203. hsFlowTask.setBaseId(null);
  204. hsFlowTask.setOperator(hsFlowTask.getOperator());
  205. hsFlowTask.setIsEnd("1");
  206. String businessId = hsFlowTask.getBusinessId();
  207. if (StringUtils.isNotBlank(businessId)) {
  208. BizLeaveImg byId = leaveImgService.getById(businessId);
  209. byId.setRemark("新进度");
  210. leaveImgService.updateById(byId);
  211. }
  212. HsFlowRunningRecord frr1 = new HsFlowRunningRecord();
  213. frr1.setComment("自动结束");
  214. frr1.setOperator(SecurityUtils.getUsername());
  215. frr1.setTaskId(flow.getTaskId());
  216. frr1.setCreateTime(new Date(new Date().getTime() + 1000));
  217. list.add(frr1);
  218. //生成审批结果消息列表
  219. HsAppUserMsg appUserMsg = new HsAppUserMsg();
  220. appUserMsg.setMsgContent("您的 [" + jsonObject.get("title") + "] 申请" + (resultTrue ? "已通过" : "已拒绝"));
  221. appUserMsg.setMsgIsRead("0");
  222. appUserMsg.setUserCode(String.valueOf(jsonObject.get("apply_user_id")));
  223. appUserMsg.setMsgType("4");
  224. appUserMsg.setRemark(hsFlowTask.getId());
  225. appUserMsg.setCreateTime(new Date());
  226. userMsgService.save(appUserMsg);
  227. //推送微信审批结果通知
  228. WxMsgTemplateApprovalResult wxMsgTemplateApproval = new WxMsgTemplateApprovalResult();
  229. wxMsgTemplateApproval.setPhrase4(wxUtils.getFormat(resultTrue ? "已通过" : "已拒绝"));
  230. wxMsgTemplateApproval.setDate3(wxUtils.getFormat(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date())));
  231. wxMsgTemplateApproval.setThing6(wxUtils.getFormat(String.valueOf(jsonObject.get("title"))));
  232. wxMsgService.sendQRCodeMsg(String.valueOf(jsonObject.get("apply_user_id")), wxMsgTemplateApproval, WxTemplateKeyEnum.APPROVAL_RESULT, true);
  233. } else {
  234. //任务继续
  235. hsFlowTask.setMsgIsRead("0");
  236. //生成审批结果消息列表
  237. HsAppUserMsg appUserMsg = new HsAppUserMsg();
  238. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  239. String content = null;
  240. try {
  241. content = "<p>标题:" + jsonObject.get("title") + "</p>" +
  242. "<p>申请人:" + jsonObject.get("apply_user_name") + "</p>" +
  243. "<p>开始时间:" + DateUtils.dateTime(sdf.parse(jsonObject.get("leave_start_time").toString())) + "</p>" +
  244. "<p>结束时间:" + DateUtils.dateTime(sdf.parse(jsonObject.get("leave_end_time").toString())) + "</p>";
  245. } catch (ParseException e) {
  246. e.printStackTrace();
  247. }
  248. appUserMsg.setMsgContent(content);
  249. appUserMsg.setMsgIsRead("0");
  250. appUserMsg.setUserCode(hsFlowTask.getOperator());
  251. appUserMsg.setMsgType("1");
  252. appUserMsg.setRemark(hsFlowTask.getId());
  253. appUserMsg.setCreateTime(new Date());
  254. userMsgService.save(appUserMsg);
  255. //推送微信待审批通知
  256. WxMsgTemplateApproval wxMsgTemplateApproval = new WxMsgTemplateApproval();
  257. wxMsgTemplateApproval.setThing5(wxUtils.getFormat(String.valueOf(jsonObject.get("title"))));
  258. wxMsgTemplateApproval.setThing11(wxUtils.getFormat(String.valueOf(jsonObject.get("apply_user_name"))));
  259. wxMsgTemplateApproval.setTime12(wxUtils.getFormat(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date())));
  260. wxMsgService.sendQRCodeMsg(hsFlowTask.getOperator(), wxMsgTemplateApproval, WxTemplateKeyEnum.APPROVAL_WAIT, true);
  261. }
  262. hsFlowTaskMapper.updateById(hsFlowTask);
  263. flowRunningRecordService.saveBatch(list);
  264. return 1;
  265. }
  266. @Override
  267. public List<HsFlowTask> getToDoByTaskId(String taskId) {
  268. if (StringUtils.isBlank(taskId)) {
  269. return null;
  270. }
  271. List<HsFlowTask> hsFlowTasks = hsFlowTaskMapper.selectList(new QueryWrapper<HsFlowTask>()
  272. .eq("id", taskId));
  273. return getHsFlowTasks(hsFlowTasks);
  274. }
  275. @Override
  276. public List<HsFlowTask> getToDo(String userCode) {
  277. if (StringUtils.isBlank(userCode)) {
  278. return null;
  279. }
  280. List<HsFlowTask> hsFlowTasks = hsFlowTaskMapper.selectAllByUserCode(userCode);
  281. return getHsFlowTasks(hsFlowTasks);
  282. }
  283. @Override
  284. public List<HsFlowTask> getDone(String userCode) {
  285. if (StringUtils.isBlank(userCode)) {
  286. return null;
  287. }
  288. List<HsFlowTask> hsFlowTasks = hsFlowTaskMapper.getDone(userCode);
  289. return getHsFlowTasks(hsFlowTasks);
  290. }
  291. private List<HsFlowTask> getHsFlowTasks(List<HsFlowTask> hsFlowTasks) {
  292. if (0 != hsFlowTasks.size()) {
  293. List<HsFlowExamples> hsFlowExamples = hsFlowExamplesMapper.selectHsFlowExamplesList(null);
  294. hsFlowTasks.forEach(item -> {
  295. List<HsFlowExamples> collect = hsFlowExamples.stream().filter(examples -> {
  296. if (StringUtils.isNotBlank(examples.getId()) && examples.getId().equals(item.getExamplesId())) {
  297. return true;
  298. }
  299. return false;
  300. }).collect(Collectors.toList());
  301. String businessId = item.getBusinessId();
  302. if (0 != collect.size()) {
  303. if (StringUtils.isNotBlank(businessId)) {
  304. Map<String, Object> stringObjectMap = hsFlowTaskMapper
  305. .selectBusinessCase(collect.get(0).getBusinessTableName(), businessId);
  306. if (ObjectUtils.isNotNull(stringObjectMap)) {
  307. List<Map<String, Object>> leaveImgs = leaveImgService.getBaseMapper().selectMaps(new QueryWrapper<BizLeaveImg>().eq("biz_leave_id", businessId));
  308. stringObjectMap.put("imgUrls", leaveImgs);
  309. Set<String> strings = stringObjectMap.keySet();
  310. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  311. strings.forEach(str -> {
  312. if (StringUtils.isNotBlank(str) && str.contains("time")) {
  313. if (stringObjectMap.get(str) instanceof LocalDateTime) {
  314. stringObjectMap.put(str, formatter.format((LocalDateTime) stringObjectMap.get(str)));
  315. }
  316. }
  317. });
  318. item.formData = new JSONObject(stringObjectMap);
  319. }
  320. }
  321. }
  322. });
  323. }
  324. return hsFlowTasks;
  325. }
  326. @Override
  327. public List<HsFlowRunningRecord> currentTaskRecords(String taskId) {
  328. if (StringUtils.isNotBlank(taskId)) {
  329. HsFlowRunningRecord hsFlowRunningRecord = new HsFlowRunningRecord();
  330. hsFlowRunningRecord.setTaskId(taskId);
  331. return flowRunningRecordService.selectHsFlowRunningRecordList(hsFlowRunningRecord);
  332. }
  333. return null;
  334. }
  335. @Override
  336. public int revoke(String taskId) {
  337. if (StringUtils.isNotBlank(taskId)) {
  338. long selectCount = flowRunningRecordService.getBaseMapper().selectCount(new QueryWrapper<HsFlowRunningRecord>()
  339. .eq("task_id", taskId));
  340. if (selectCount > 0) {
  341. throw new BaseException("已有审批结果无法撤销");
  342. }
  343. return hsFlowTaskMapper.deleteById(taskId);
  344. }
  345. return 0;
  346. }
  347. }