IncidentServiceImpl.java 17 KB

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