CareEventOrderServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package com.care.common.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.collection.CollUtil;
  4. import cn.hutool.core.date.DateUtil;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  7. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  8. import com.care.common.cache.RedisKeyConstant;
  9. import com.care.common.cache.RedisUtil;
  10. import com.care.common.entity.*;
  11. import com.care.common.enums.*;
  12. import com.care.common.mapper.CareEventOrderMapper;
  13. import com.care.common.service.*;
  14. import com.care.common.vo.order.EventStaVO;
  15. import org.apache.commons.compress.utils.Lists;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import javax.annotation.Resource;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * 事件定单表(CareEventOrder)表服务实现类
  23. *
  24. * @author makejava
  25. * @since 2021-05-21 00:08:29
  26. */
  27. @Service
  28. public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper, CareEventOrder> implements CareEventOrderService {
  29. @Resource
  30. private CareEventOrderHandleHisService careEventOrderHandleHisService;
  31. @Resource
  32. private CareHouseContactService careHouseContactService;
  33. @Resource
  34. private CareOlderService careOlderService;
  35. @Resource
  36. private CareEventOrderOlderService careEventOrderOlderService;
  37. @Resource
  38. private CareEventOrderChambService careEventOrderChambService;
  39. @Resource
  40. private CareEventOrderContactStatusService careEventOrderContactStatusService;
  41. @Resource
  42. private CareHouseService careHouseService;
  43. @Resource
  44. private CareDeviceService careDeviceService;
  45. @Resource
  46. private CareSysUserService careSysUserService;
  47. @Resource
  48. private RedisUtil redisUtil;
  49. @Resource
  50. private SmsSendService smsSendService;
  51. @Resource
  52. private CareHouseOlderRelService careHouseOlderRelService;
  53. /**
  54. * 统计事件数量
  55. *
  56. * @param orgId
  57. * @param stationId
  58. * @return
  59. */
  60. @Override
  61. public EventStaVO statOrder(Long orgId, Long stationId) {
  62. return this.baseMapper.statOrder(orgId,stationId);
  63. }
  64. /**
  65. * 更新,不再生成新的告警工单,只作为子事件插入到当前工单的历史记录中
  66. * @param order
  67. * @param orderDb
  68. * @return
  69. */
  70. @Override
  71. @Transactional(rollbackFor = Exception.class)
  72. public boolean saveHisOrder(CareEventOrder order,CareEventOrder orderDb,boolean isCancel){
  73. // orderDb.setOrderType(order.getOrderType());
  74. // this.updateById(orderDb);//更新
  75. order.setId(orderDb.getId());
  76. CareEventOrderHandleHis his = new CareEventOrderHandleHis();
  77. his.setOrgId(order.getOrgId());
  78. his.setStationId(order.getStationId());
  79. his.setOrderId(order.getId());
  80. his.setHouseId(order.getHouseId());
  81. his.setOrderType(order.getOrderType());
  82. if (OrderTypeEnum.ZHU_DONG_HU_JIAO.getValue().equals(order.getOrderType()) || OrderTypeEnum.HU_WAI_HU_JIAO.getValue().equals(order.getOrderType())){
  83. his.setLogType(LogTypeEnum.OLDER_EVENT.getValue());
  84. his.setLogObjectId(order.getOlderId());
  85. his.setLogObjectName(order.getOlderName());
  86. his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType()));
  87. his.setRelationTypeDesc(RelationTypeEnum.OLDER.getName());
  88. } else {
  89. his.setLogType(LogTypeEnum.DEV_EVENT.getValue());
  90. his.setLogObjectId(order.getDevId());
  91. his.setLogObjectName(order.getDevName());
  92. if (isCancel){
  93. his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType())+"取消");
  94. }else{
  95. his.setLogResult("发生"+OrderTypeEnum.getCodeToName(order.getOrderType()));
  96. }
  97. his.setRelationTypeDesc(RelationTypeEnum.HOUSE.getName());
  98. }
  99. his.setCreateTime(order.getCreateTime());
  100. this.careEventOrderHandleHisService.save(his);
  101. return true;
  102. }
  103. /**
  104. * 新增,生成新的告警事件工单
  105. * @param order
  106. * @return
  107. */
  108. @Override
  109. @Transactional(rollbackFor = Exception.class)
  110. public boolean saveOrder(CareEventOrder order){
  111. this.baseMapper.insert(order);
  112. CareEventOrderHandleHis his = new CareEventOrderHandleHis();
  113. his.setOrgId(order.getOrgId());
  114. his.setStationId(order.getStationId());
  115. his.setOrderId(order.getId());
  116. his.setHouseId(order.getHouseId());
  117. his.setOrderType(order.getOrderType());
  118. if (OrderTypeEnum.ZHU_DONG_HU_JIAO.getValue().equals(order.getOrderType()) || OrderTypeEnum.HU_WAI_HU_JIAO.getValue().equals(order.getOrderType())){
  119. his.setLogType(LogTypeEnum.OLDER_EVENT.getValue());
  120. his.setLogObjectId(order.getOlderId());
  121. his.setLogObjectName(order.getOlderName());
  122. his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType()));
  123. his.setRelationTypeDesc(RelationTypeEnum.OLDER.getName());
  124. }else{
  125. his.setLogType(LogTypeEnum.DEV_EVENT.getValue());
  126. his.setLogObjectId(order.getDevId());
  127. his.setLogObjectName(order.getDevName());
  128. his.setLogResult("发生"+OrderTypeEnum.getCodeToName(order.getOrderType()));
  129. his.setRelationTypeDesc(RelationTypeEnum.HOUSE.getName());
  130. }
  131. his.setCreateTime(order.getCreateTime());
  132. addOlderToOrderOlder(order);
  133. this.careEventOrderHandleHisService.save(his);
  134. List<CareEventOrderChamb> chambList = addChamberlainToOrder(order);
  135. List<CareEventOrderContactStatus> contacts = addContactToOrderContact(order);
  136. sendSms(order,chambList,contacts);
  137. return true;
  138. }
  139. private void sendSms(CareEventOrder order,List<CareEventOrderChamb> chambList,List<CareEventOrderContactStatus> contacts){
  140. try{
  141. Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
  142. if (switchSms != null){
  143. //给联系人发送短信
  144. CareHouse house = this.careHouseService.getById(order.getHouseId());
  145. if (CollUtil.isNotEmpty(contacts)){
  146. contacts.forEach(item ->{
  147. boolean smsResult = false;
  148. if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
  149. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.JIU_ZHI,item.getContactPhone(),house.getName());
  150. }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
  151. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.DIE_DAO,item.getContactPhone(),house.getName());
  152. }
  153. if (smsResult){
  154. CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
  155. his2.setOrgId(item.getOrgId());
  156. his2.setStationId(item.getStationId());
  157. his2.setOrderId(item.getOrderId());
  158. his2.setLogType(LogTypeEnum.SMS.getValue());
  159. his2.setLogObjectId(item.getContactId());
  160. his2.setLogObjectName(item.getContactName());
  161. his2.setLogResult("短信通知");
  162. his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
  163. his2.setCreateTime(DateUtil.date());
  164. his2.setRelationTypeDesc(item.getRelationTypeDesc());
  165. this.careEventOrderHandleHisService.save(his2);
  166. }
  167. });
  168. }
  169. //给管家发短信
  170. if (CollUtil.isNotEmpty(chambList)){
  171. chambList.forEach(item ->{
  172. boolean smsResult = false;
  173. if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
  174. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.JIU_ZHI,item.getPhone(),house.getName());
  175. }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
  176. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.DIE_DAO,item.getPhone(),house.getName());
  177. }
  178. if (smsResult){
  179. CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
  180. his2.setOrgId(item.getOrgId());
  181. his2.setStationId(item.getStationId());
  182. his2.setOrderId(item.getOrderId());
  183. his2.setLogType(LogTypeEnum.SMS.getValue());
  184. his2.setLogObjectId(item.getChambId());
  185. his2.setLogObjectName(item.getChambName());
  186. his2.setLogResult("短信通知");
  187. his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
  188. his2.setCreateTime(DateUtil.date());
  189. his2.setRelationTypeDesc("管家");
  190. this.careEventOrderHandleHisService.save(his2);
  191. }
  192. });
  193. }
  194. }
  195. }catch (Exception e){
  196. log.error("发送短信通知失败",e);
  197. }
  198. }
  199. /**
  200. * 将老人加入到 工单中
  201. * @param order
  202. */
  203. private void addOlderToOrderOlder(CareEventOrder order){
  204. QueryWrapper<CareHouseOlderRel> queryWrapper = new QueryWrapper<>();
  205. queryWrapper.lambda().eq(CareHouseOlderRel::getHouseId,order.getHouseId());
  206. List<CareHouseOlderRel> olderRels = this.careHouseOlderRelService.list(queryWrapper);
  207. if (CollUtil.isNotEmpty(olderRels)){
  208. List<CareEventOrderOlder> ceoos = Lists.newArrayList();
  209. olderRels.forEach(item -> {
  210. CareOlder careOlder = this.careOlderService.getById(item.getOlderId());
  211. CareEventOrderOlder ceoo = new CareEventOrderOlder();
  212. BeanUtil.copyProperties(careOlder,ceoo);
  213. ceoo.setOrgId(order.getOrgId());
  214. ceoo.setStationId(order.getStationId());
  215. ceoo.setOrderId(order.getId());
  216. ceoo.setOlderId(item.getId());
  217. if (order.getOlderId() == item.getId()){
  218. //主动呼叫
  219. ceoo.setIsZhudong(1);
  220. }else{
  221. ceoo.setIsZhudong(0);
  222. }
  223. ceoo.setStatus(OrderOlderStatusEnum.WEI_LIAN_XI.getValue());
  224. ceoo.setCreateTime(order.getCreateTime());
  225. ceoos.add(ceoo);
  226. });
  227. this.careEventOrderOlderService.saveBatch(ceoos);
  228. }
  229. }
  230. /**
  231. * 将管家加入到工单当中
  232. * @param order
  233. */
  234. private List<CareEventOrderChamb> addChamberlainToOrder(CareEventOrder order){
  235. List<CareEventOrderChamb> chambList = Lists.newArrayList();
  236. CareDevice dev = this.careDeviceService.getById(order.getDevId());
  237. if (dev != null){
  238. CareSysUser user = this.careSysUserService.getById(dev.getChambId());
  239. if (user != null){
  240. CareEventOrderChamb chamb = new CareEventOrderChamb();
  241. chamb.setOrgId(user.getOrgId());
  242. chamb.setStationId(user.getStationId());
  243. chamb.setOrderId(order.getId());
  244. chamb.setChambId(user.getId());
  245. chamb.setChambName(user.getName());
  246. chamb.setPhone(user.getPhone());
  247. chamb.setStatus(ChambOrderStatusEnum.TODO.getValue());
  248. chamb.setHouseId(dev.getHouseId());
  249. chamb.setCreateTime(order.getCreateTime());
  250. chamb.setModifyTime(order.getCreateTime());
  251. this.careEventOrderChambService.save(chamb);
  252. chambList.add(chamb);
  253. }
  254. }
  255. return chambList;
  256. }
  257. /**
  258. * 将紧急联系人加入到联系人当中
  259. * @param order
  260. */
  261. private List<CareEventOrderContactStatus> addContactToOrderContact(CareEventOrder order){
  262. List<CareEventOrderContactStatus> orderContactList = Lists.newArrayList();
  263. List<CareHouseContact> cons = this.careHouseContactService.queryContactByHouseId(order.getHouseId());
  264. if (CollUtil.isNotEmpty(cons)){
  265. cons.forEach(item ->{
  266. CareEventOrderContactStatus orderContactStatus = new CareEventOrderContactStatus();
  267. orderContactStatus.setOrgId(order.getOrgId());
  268. orderContactStatus.setStationId(order.getStationId());
  269. orderContactStatus.setOrderId(order.getId());
  270. orderContactStatus.setHouseId(order.getHouseId());
  271. orderContactStatus.setContactRole(UserRoleEnum.CONTACT.getValue());
  272. orderContactStatus.setContactId(item.getId());
  273. orderContactStatus.setStatus(ContactorStatusEnum.WEI_LIAN_XI.getValue());
  274. orderContactStatus.setContactName(item.getContactName());
  275. orderContactStatus.setContactPhone(item.getContactPhone());
  276. orderContactStatus.setLongitude(item.getLongitude());
  277. orderContactStatus.setLatitude(item.getLatitude());
  278. orderContactStatus.setRelationTypeDesc(RelationTypeEnum.getCodeToName(item.getRelationType()));
  279. orderContactStatus.setContactLevel(item.getContactLevel());
  280. orderContactStatus.setCreateTime(order.getCreateTime());
  281. orderContactStatus.setModifyTime(order.getCreateTime());
  282. orderContactList.add(orderContactStatus);
  283. });
  284. this.careEventOrderContactStatusService.saveBatch(orderContactList);
  285. }
  286. return orderContactList;
  287. }
  288. /**
  289. * 自动取消工单
  290. *
  291. * @param order
  292. * @return
  293. */
  294. @Override
  295. public boolean autoCancelOrder(CareEventOrder order) {
  296. UpdateWrapper<CareEventOrder> updateWrapper = new UpdateWrapper<>();
  297. updateWrapper.lambda().set(CareEventOrder::getStatus,OrderStatusEnum.CANCEL.getValue()).eq(CareEventOrder::getId,order.getId());
  298. order.setStatus(OrderStatusEnum.CANCEL.getValue());
  299. this.update(updateWrapper);
  300. //管家工单标记为取消
  301. CareEventOrderChamb careEventOrderChamb = this.careEventOrderChambService.getChambOrderByOrderId(order.getId());
  302. UpdateWrapper<CareEventOrderChamb> updateWrapperChamb = new UpdateWrapper<>();
  303. updateWrapperChamb.lambda().set(CareEventOrderChamb::getStatus,ChambOrderStatusEnum.CANCEL.getValue()).eq(CareEventOrderChamb::getId,careEventOrderChamb.getId());
  304. this.careEventOrderChambService.update(updateWrapperChamb);
  305. //记录一条取消his
  306. CareEventOrder orderDb = new CareEventOrder();
  307. orderDb.setId(order.getId());
  308. this.saveHisOrder(order, orderDb,true);
  309. cancelEventSMSNotic(order);
  310. return true;
  311. }
  312. /**
  313. * 取消事件,短信通知
  314. * @param order
  315. */
  316. private void cancelEventSMSNotic(CareEventOrder order){
  317. try{
  318. Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
  319. if (switchSms != null){
  320. List<CareEventOrderChamb> chambList = new ArrayList<>();
  321. CareEventOrderChamb chamb = this.careEventOrderChambService.getChambOrderByOrderId(order.getId());
  322. chambList.add(chamb);
  323. QueryWrapper<CareEventOrderContactStatus> queryWrapper = new QueryWrapper<>();
  324. queryWrapper.lambda().eq(CareEventOrderContactStatus::getOrderId,order.getId());
  325. List<CareEventOrderContactStatus> contacts = this.careEventOrderContactStatusService.list(queryWrapper);
  326. //给联系人发送短信
  327. CareHouse house = this.careHouseService.getById(order.getHouseId());
  328. if (CollUtil.isNotEmpty(contacts)){
  329. contacts.forEach(item ->{
  330. boolean smsResult = false;
  331. if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
  332. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_JIUZHI,item.getContactPhone(),house.getName());
  333. }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
  334. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_DIEDAO,item.getContactPhone(),house.getName());
  335. }
  336. if (smsResult){
  337. CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
  338. his2.setOrgId(item.getOrgId());
  339. his2.setStationId(item.getStationId());
  340. his2.setOrderId(item.getOrderId());
  341. his2.setLogType(LogTypeEnum.SMS.getValue());
  342. his2.setLogObjectId(item.getContactId());
  343. his2.setLogObjectName(item.getContactName());
  344. his2.setLogResult("事件取消短信通知");
  345. his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
  346. his2.setCreateTime(DateUtil.date());
  347. his2.setRelationTypeDesc(item.getRelationTypeDesc());
  348. this.careEventOrderHandleHisService.save(his2);
  349. }
  350. });
  351. }
  352. //给管家发短信
  353. if (CollUtil.isNotEmpty(chambList)){
  354. chambList.forEach(item ->{
  355. boolean smsResult = false;
  356. if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
  357. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_JIUZHI,item.getPhone(),house.getName());
  358. }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
  359. smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_DIEDAO,item.getPhone(),house.getName());
  360. }
  361. if (smsResult){
  362. CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
  363. his2.setOrgId(item.getOrgId());
  364. his2.setStationId(item.getStationId());
  365. his2.setOrderId(item.getOrderId());
  366. his2.setLogType(LogTypeEnum.SMS.getValue());
  367. his2.setLogObjectId(item.getChambId());
  368. his2.setLogObjectName(item.getChambName());
  369. his2.setLogResult("事件取消短信通知");
  370. his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
  371. his2.setCreateTime(DateUtil.date());
  372. his2.setRelationTypeDesc("管家");
  373. this.careEventOrderHandleHisService.save(his2);
  374. }
  375. });
  376. }
  377. }
  378. }catch (Exception e){
  379. log.error("发送短信通知失败",e);
  380. }
  381. }
  382. }