IncidentServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package com.ruoyi.zhdd.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.convert.Convert;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.json.JSONArray;
  7. import cn.hutool.json.JSONObject;
  8. import cn.hutool.json.JSONUtil;
  9. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  10. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  11. import com.ruoyi.common.constant.Constants;
  12. import com.ruoyi.common.core.domain.entity.SysDept;
  13. import com.ruoyi.common.core.domain.entity.SysUser;
  14. import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
  15. import com.ruoyi.common.core.page.PagePlus;
  16. import com.ruoyi.common.core.page.TableDataInfo;
  17. import com.ruoyi.common.exception.ServiceException;
  18. import com.ruoyi.common.utils.PageUtils;
  19. import com.ruoyi.common.utils.RedisUtils;
  20. import com.ruoyi.common.utils.StringUtils;
  21. import com.ruoyi.system.service.ISysDeptService;
  22. import com.ruoyi.system.service.ISysUserService;
  23. import com.ruoyi.zhdd.domain.Incident;
  24. import com.ruoyi.zhdd.domain.IncidentProcess;
  25. import com.ruoyi.zhdd.domain.IncidentTask;
  26. import com.ruoyi.zhdd.domain.IncidentUser;
  27. import com.ruoyi.zhdd.domain.Plan;
  28. import com.ruoyi.zhdd.domain.bo.BacklogMessageUpdateBo;
  29. import com.ruoyi.zhdd.domain.bo.IncidentBo;
  30. import com.ruoyi.zhdd.domain.bo.MessagePushUser;
  31. import com.ruoyi.zhdd.domain.vo.IncidentVo;
  32. import com.ruoyi.zhdd.domain.vo.PlanTaskVo;
  33. import com.ruoyi.zhdd.domain.vo.PlanVo;
  34. import com.ruoyi.zhdd.feign.FeignBacklogInfoService;
  35. import com.ruoyi.zhdd.mapper.IncidentMapper;
  36. import com.ruoyi.zhdd.service.IIncidentProcessService;
  37. import com.ruoyi.zhdd.service.IIncidentService;
  38. import com.ruoyi.zhdd.service.IIncidentTaskService;
  39. import com.ruoyi.zhdd.service.IPlanService;
  40. import com.ruoyi.zhdd.service.IPlanTaskService;
  41. import lombok.extern.slf4j.Slf4j;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.stereotype.Service;
  44. import org.springframework.transaction.annotation.Transactional;
  45. import java.sql.Timestamp;
  46. import java.util.ArrayList;
  47. import java.util.Arrays;
  48. import java.util.Collection;
  49. import java.util.List;
  50. import java.util.Map;
  51. import java.util.stream.Collectors;
  52. /**
  53. * 事件基础Service业务层处理
  54. *
  55. * @author xitong
  56. * @date 2021-09-28
  57. */
  58. @Service
  59. @Slf4j
  60. public class IncidentServiceImpl extends ServicePlusImpl<IncidentMapper, Incident, IncidentVo> implements IIncidentService {
  61. @Autowired
  62. private IIncidentProcessService incidentProcessService;
  63. @Autowired
  64. private ISysUserService sysUserService;
  65. @Autowired
  66. private ISysDeptService sysDeptService;
  67. @Autowired
  68. private IIncidentTaskService incidentTaskService;
  69. @Autowired
  70. private FeignBacklogInfoService feignBacklogInfoService;
  71. @Autowired
  72. private IPlanService iPlanService;
  73. @Autowired
  74. private IPlanTaskService planTaskService;
  75. @Override
  76. public IncidentVo queryById(String id) {
  77. return getVoById(id);
  78. }
  79. @Override
  80. public TableDataInfo<IncidentVo> queryPageList(IncidentBo bo) {
  81. PagePlus<Incident, IncidentVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo));
  82. return PageUtils.buildDataInfo(result);
  83. }
  84. @Override
  85. public List<IncidentVo> queryList(IncidentBo bo) {
  86. return listVo(buildQueryWrapper(bo));
  87. }
  88. private LambdaQueryWrapper<Incident> buildQueryWrapper(IncidentBo bo) {
  89. Map<String, Object> params = bo.getParams();
  90. LambdaQueryWrapper<Incident> lqw = Wrappers.lambdaQuery();
  91. lqw.eq(bo.getType() != null, Incident::getType, bo.getType());
  92. lqw.eq(bo.getLevel() != null, Incident::getLevel, bo.getLevel());
  93. lqw.eq(StringUtils.isNotBlank(bo.getAddr()), Incident::getAddr, bo.getAddr());
  94. lqw.eq(StringUtils.isNotBlank(bo.getLocations()), Incident::getLocations, bo.getLocations());
  95. lqw.eq(StringUtils.isNotBlank(bo.getCreateDept()), Incident::getCreateDept, bo.getCreateDept());
  96. lqw.eq(StringUtils.isNotBlank(bo.getSource()), Incident::getSource, bo.getSource());
  97. lqw.eq(StringUtils.isNotBlank(bo.getDes()), Incident::getDes, bo.getDes());
  98. lqw.eq(StringUtils.isNotBlank(bo.getPic()), Incident::getPic, bo.getPic());
  99. lqw.eq(StringUtils.isNotBlank(bo.getVideo()), Incident::getVideo, bo.getVideo());
  100. lqw.eq(StringUtils.isNotBlank(bo.getExpr1()), Incident::getExpr1, bo.getExpr1());
  101. lqw.eq(StringUtils.isNotBlank(bo.getExpr2()), Incident::getExpr2, bo.getExpr2());
  102. lqw.eq(StringUtils.isNotBlank(bo.getExprJson()), Incident::getExprJson, bo.getExprJson());
  103. lqw.like(StringUtils.isNotBlank(bo.getName()), Incident::getName, bo.getName());
  104. lqw.eq(bo.getStatus() != null, Incident::getStatus, bo.getStatus());
  105. if (StrUtil.isNotBlank(bo.getStatuss())) {
  106. lqw.in(Incident::getStatus, Arrays.stream(bo.getStatuss().split(",")).map(Integer::parseInt).collect(Collectors.toList()));
  107. }
  108. lqw.eq(bo.getMadinDept() != null, Incident::getMadinDept, bo.getMadinDept());
  109. lqw.eq(StringUtils.isNotBlank(bo.getAssistDept()), Incident::getAssistDept, bo.getAssistDept());
  110. if (params.get("beginTime") != null && params.get("endTime") != null) {
  111. lqw.between(Incident::getCreateTime, Timestamp.valueOf(Convert.toStr(params.get("beginTime")) + " 00:00:00"), Timestamp.valueOf(Convert.toStr(params.get("endTime")) + " 23:59:59"));
  112. }
  113. lqw.in(bo.getLevels() != null && bo.getLevels().size() > 0, Incident::getLevel, bo.getLevels());
  114. if (bo.getIds() != null && bo.getIds().size() > 0) {
  115. lqw.in(Incident::getId, bo.getIds());
  116. }
  117. lqw.orderByDesc(Incident::getCreateTime);
  118. return lqw;
  119. }
  120. @Override
  121. public String insertByBo(IncidentBo bo) {
  122. Incident add = BeanUtil.toBean(bo, Incident.class);
  123. add.setExprJson(JSONUtil.toJsonStr(bo.getMessagePushUsers()));
  124. boolean save = save(add);
  125. JSONObject jsonObject = new JSONObject();
  126. jsonObject.set("des", "事件接报");
  127. jsonObject.set("incidentId", add.getId());
  128. jsonObject.set("incidentStatus", 2);
  129. RedisUtils.publish(Constants.PROCESS_RECORD, jsonObject, msg -> {
  130. log.info("事件处置详情消息 => " + msg);
  131. });
  132. return add.getId();
  133. }
  134. @Override
  135. @Transactional(rollbackFor = Exception.class)
  136. public Boolean updateByBo(IncidentBo bo) {
  137. bo.setCreateBy(null);
  138. bo.setCreateDept(null);
  139. bo.setAssistDept(StrUtil.nullToEmpty(bo.getAssistDept()));
  140. bo.setAssistTaskUser(StrUtil.nullToEmpty(bo.getAssistTaskUser()));
  141. bo.setAssistDeptUser(StrUtil.nullToEmpty(bo.getAssistDeptUser()));
  142. // 是否为状态变更
  143. boolean flag;
  144. // 查询变更前信息
  145. IncidentVo beforeInfo = getVoById(bo.getId());
  146. log.info("事件变更前信息:{}", beforeInfo);
  147. log.info("事件变更后信息:{}", bo);
  148. if (beforeInfo != null) {
  149. flag = !(beforeInfo.getStatus().equals(bo.getStatus()));
  150. } else {
  151. throw new ServiceException("事件不存在!");
  152. }
  153. Incident update = BeanUtil.toBean(bo, Incident.class);
  154. // 待删除事件用户关系的array
  155. JSONArray removeJsonArray = new JSONArray();
  156. // 待新增事件用户关系的array
  157. JSONArray saveJsonArray = new JSONArray();
  158. // 待发送消息的jsonObject
  159. JSONObject sendJsonObject = new JSONObject();
  160. if (flag && bo.getStatus() == 3) {
  161. // 第一次派发(只派发给主办人员)
  162. // 待处置状态
  163. if (StrUtil.isBlank(bo.getMadinDept())) {
  164. throw new ServiceException("请选择主办单位!");
  165. }
  166. if (StrUtil.isBlank(bo.getMadinTaskUser())) {
  167. throw new ServiceException("请选择主办单位处置人员!");
  168. }
  169. // 冗余主办
  170. SysDept sysDept = sysDeptService.selectDeptById(bo.getMadinDept());
  171. if (sysDept != null) {
  172. update.setMadinDeptText(sysDept.getDeptName());
  173. }
  174. // 组装人员数据
  175. // 主办单位处置人员
  176. for (MessagePushUser messagePushUser : JSONUtil.toList(bo.getMadinTaskUser(), MessagePushUser.class)) {
  177. IncidentUser save = new IncidentUser();
  178. save.setIncidentId(bo.getId());
  179. save.setUserId(messagePushUser.getUserId());
  180. save.setSource("2");
  181. saveJsonArray.add(save);
  182. }
  183. // 主办单位通知人员
  184. if (StrUtil.isNotBlank(bo.getMadinDeptUser())) {
  185. for (MessagePushUser messagePushUser : JSONUtil.toList(bo.getMadinDeptUser(), MessagePushUser.class)) {
  186. IncidentUser save = new IncidentUser();
  187. save.setIncidentId(bo.getId());
  188. save.setUserId(messagePushUser.getUserId());
  189. save.setSource("6");
  190. saveJsonArray.add(save);
  191. }
  192. }
  193. } else if (!flag
  194. && bo.getStatus() == 3
  195. && (!StrUtil.equals(bo.getAssistDept(), beforeInfo.getAssistDept())
  196. // || !StrUtil.equals(bo.getCommander(), beforeInfo.getCommander())
  197. || !StrUtil.equals(bo.getAssistTaskUser(), beforeInfo.getAssistTaskUser())
  198. || !StrUtil.equals(bo.getAssistDeptUser(), beforeInfo.getAssistDeptUser()))) {
  199. // 协办部门派发或再次对协办修改
  200. // 重新冗余协办部门、总指挥
  201. /*if (StrUtil.isBlank(bo.getCommander())) {
  202. throw new ServiceException("请选择总指挥!");
  203. }*/
  204. if (StrUtil.isNotBlank(bo.getCommander())) {
  205. SysUser commanderText = sysUserService.selectUserById(bo.getCommander());
  206. if (commanderText != null) {
  207. update.setCommanderText(commanderText.getNickName());
  208. }
  209. }
  210. if (StrUtil.isBlank(bo.getAssistDept())) {
  211. update.setAssistDeptText("");
  212. } else {
  213. // 增加了协办单位后,协办的处置人员非空
  214. if (StrUtil.isBlank(bo.getAssistTaskUser())) {
  215. throw new ServiceException("请选择协办单位处置人员!");
  216. }
  217. if (!StrUtil.equals(beforeInfo.getAssistDept(), bo.getAssistDept())) {
  218. List<SysDept> sysDepts = sysDeptService.listByIds(Arrays.asList(bo.getAssistDept().split(",")));
  219. if (sysDepts != null && sysDepts.size() > 0) {
  220. update.setAssistDeptText(sysDepts.stream().map(SysDept::getDeptName).collect(Collectors.joining(",")));
  221. }
  222. }
  223. }
  224. // 分析变更的派发对象
  225. // 变更的总指挥(目前需求:总指挥不能变更)
  226. String oldCommander = beforeInfo.getCommander();
  227. String newCommander = bo.getCommander();
  228. /*if (StrUtil.isNotBlank(oldCommander) && !StrUtil.equals(oldCommander, newCommander)) {
  229. // 删除关系
  230. IncidentUser remove = new IncidentUser();
  231. remove.setIncidentId(bo.getId());
  232. remove.setUserId(oldCommander);
  233. remove.setSource("5");
  234. removeJsonArray.add(remove);
  235. }*/
  236. // 将总指挥加入到关系
  237. if (StrUtil.isNotBlank(newCommander) && !StrUtil.equals(oldCommander, newCommander)) {
  238. IncidentUser save = new IncidentUser();
  239. save.setIncidentId(bo.getId());
  240. save.setUserId(newCommander);
  241. save.setSource("5");
  242. saveJsonArray.add(save);
  243. }
  244. // 变更的协办处置人员
  245. List<MessagePushUser> oldAssistTaskUser = JSONUtil.toList(StrUtil.blankToDefault(beforeInfo.getAssistTaskUser(), "[]"), MessagePushUser.class);
  246. List<MessagePushUser> newAssistTaskUser = JSONUtil.toList(StrUtil.blankToDefault(bo.getAssistTaskUser(), "[]"), MessagePushUser.class);
  247. List<MessagePushUser> removeAssistTaskUser = CollUtil.subtractToList(oldAssistTaskUser, newAssistTaskUser);
  248. List<MessagePushUser> addAssistTaskUser = CollUtil.subtractToList(newAssistTaskUser, oldAssistTaskUser);
  249. for (MessagePushUser messagePushUser : removeAssistTaskUser) {
  250. // 删除关系
  251. IncidentUser remove = new IncidentUser();
  252. remove.setIncidentId(bo.getId());
  253. remove.setUserId(messagePushUser.getUserId());
  254. remove.setSource("4");
  255. removeJsonArray.add(remove);
  256. }
  257. for (MessagePushUser messagePushUser : addAssistTaskUser) {
  258. IncidentUser save = new IncidentUser();
  259. save.setIncidentId(bo.getId());
  260. save.setUserId(messagePushUser.getUserId());
  261. save.setSource("4");
  262. saveJsonArray.add(save);
  263. }
  264. // 变更的协办通知人员
  265. List<MessagePushUser> oldAssistDeptUser = JSONUtil.toList(StrUtil.blankToDefault(beforeInfo.getAssistDeptUser(), "[]"), MessagePushUser.class);
  266. List<MessagePushUser> newAssistDeptUser = JSONUtil.toList(StrUtil.blankToDefault(bo.getAssistDeptUser(), "[]"), MessagePushUser.class);
  267. List<MessagePushUser> removeAssistDeptUser = CollUtil.subtractToList(oldAssistDeptUser, newAssistDeptUser);
  268. List<MessagePushUser> addAssistDeptUser = CollUtil.subtractToList(newAssistDeptUser, oldAssistDeptUser);
  269. for (MessagePushUser messagePushUser : removeAssistDeptUser) {
  270. // 删除关系
  271. IncidentUser remove = new IncidentUser();
  272. remove.setIncidentId(bo.getId());
  273. remove.setUserId(messagePushUser.getUserId());
  274. remove.setSource("6");
  275. removeJsonArray.add(remove);
  276. }
  277. for (MessagePushUser messagePushUser : addAssistDeptUser) {
  278. IncidentUser save = new IncidentUser();
  279. save.setIncidentId(bo.getId());
  280. save.setUserId(messagePushUser.getUserId());
  281. save.setSource("6");
  282. saveJsonArray.add(save);
  283. }
  284. if (addAssistDeptUser.size() > 0 || addAssistTaskUser.size() > 0) {
  285. sendJsonObject.set("des", "再次派发总指挥及协办部门");
  286. sendJsonObject.set("incidentStatus", bo.getStatus());
  287. sendJsonObject.set("incidentId", bo.getId());
  288. sendJsonObject.set("messagePushUsers", CollUtil.unionDistinct(addAssistDeptUser, addAssistTaskUser));
  289. RedisUtils.publish(Constants.NOTICE_PUSH_SINGLE, sendJsonObject);
  290. }
  291. } else if (flag && bo.getStatus() == 4) {
  292. // 将待办变成已办
  293. // 查询待办的处置方案
  294. List<IncidentTask> taskList = incidentTaskService.list(Wrappers.<IncidentTask>lambdaQuery()
  295. .in(IncidentTask::getSendFlag, "2", "3")
  296. .eq(IncidentTask::getTaskSend, "1")
  297. .eq(IncidentTask::getBackLogFlag, "0"));
  298. if (taskList != null && taskList.size() > 0) {
  299. List<String> TaskIds = taskList.stream().map(IncidentTask::getId).collect(Collectors.toList());
  300. // 更新task中状态
  301. incidentTaskService.update(Wrappers.<IncidentTask>lambdaUpdate()
  302. .set(IncidentTask::getBackLogFlag, "1")
  303. .in(IncidentTask::getSendFlag, "2", "3")
  304. .eq(IncidentTask::getTaskSend, "1")
  305. .eq(IncidentTask::getBackLogFlag, "0"));
  306. // 去日志中找到相应的日志,更改待办变为已办
  307. List<IncidentProcess> processList = incidentProcessService.list(Wrappers.<IncidentProcess>lambdaQuery()
  308. .in(IncidentProcess::getTaskId, TaskIds)
  309. .in(IncidentProcess::getStatus, 2, 3));
  310. // 组装需要调用已办接口的数据
  311. if (processList != null && processList.size() > 0) {
  312. List<BacklogMessageUpdateBo> list = new ArrayList<>();
  313. for (IncidentProcess incidentProcess : processList) {
  314. BacklogMessageUpdateBo messageUpdateBo = new BacklogMessageUpdateBo();
  315. messageUpdateBo.setUuid(incidentProcess.getId());
  316. incidentProcess.setDes(incidentProcess.getDes().replace("【待办】", "【已办】"));
  317. list.add(messageUpdateBo);
  318. }
  319. Object o = feignBacklogInfoService.backlogMessageUpdate(list);
  320. log.info("消息待办办结返回结果:{}", o);
  321. incidentProcessService.updateBatchById(processList);
  322. }
  323. }
  324. }
  325. // 更新事件信息
  326. boolean b = updateById(update);
  327. if (removeJsonArray.size() > 0) {
  328. log.info("需要移除的用户事件关系:{}", removeJsonArray);
  329. RedisUtils.publish(Constants.INCIDENT_USER_DELETE, removeJsonArray);
  330. }
  331. if (saveJsonArray.size() > 0) {
  332. log.info("需要新增的用户事件关系:{}", saveJsonArray);
  333. RedisUtils.publish(Constants.INCIDENT_USER_BATCH, saveJsonArray);
  334. }
  335. if (flag) {
  336. if (bo.getStatus() == 3) {
  337. // 第一次派发给主办部门时
  338. RedisUtils.publish(Constants.NOTICE_PUSH, 1 + "A" + update.getId());
  339. return b;
  340. }
  341. // 事件状态的变更,将发送到处置日志中
  342. sendJsonObject.set("incidentId", update.getId());
  343. sendJsonObject.set("incidentStatus", update.getStatus());
  344. RedisUtils.publish(Constants.PROCESS_RECORD, sendJsonObject);
  345. }
  346. return b;
  347. }
  348. @Override
  349. public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
  350. if (isValid) {
  351. //TODO 做一些业务上的校验,判断是否需要校验
  352. }
  353. return removeByIds(ids);
  354. }
  355. @Override
  356. @Transactional(rollbackFor = Exception.class)
  357. public void savePlanAndCreate(IncidentBo bo) {
  358. Incident update = BeanUtil.toBean(bo, Incident.class);
  359. boolean b = updateById(update);
  360. if (b) {
  361. // 根据预案生成处置方案
  362. List<PlanVo> voOne = iPlanService.listVo(Wrappers.<Plan>lambdaQuery().eq(Plan::getType, bo.getType()).eq(Plan::getCreateDept, bo.getPlanDept()));
  363. if (voOne != null && voOne.size() > 0) {
  364. Map<Integer, List<PlanTaskVo>> integerListMap = planTaskService.queryPlanTaskByPlanId(voOne.get(0).getId());
  365. if (integerListMap != null) {
  366. List<PlanTaskVo> planTaskVos = integerListMap.get(bo.getLevel());
  367. if (planTaskVos != null) {
  368. List<IncidentTask> incidentTaskList = new ArrayList<>();
  369. for (PlanTaskVo planTaskVo : planTaskVos) {
  370. IncidentTask task = new IncidentTask();
  371. task.setIncidentId(bo.getId());
  372. task.setTaskName(planTaskVo.getTaskName() + ":" + planTaskVo.getTaskDes());
  373. task.setSendFlag("1");
  374. incidentTaskList.add(task);
  375. }
  376. incidentTaskService.saveAll(incidentTaskList);
  377. }
  378. }
  379. }
  380. }
  381. }
  382. }