CareEventOrderServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.extension.service.impl.ServiceImpl;
  7. import com.care.common.cache.RedisKeyConstant;
  8. import com.care.common.cache.RedisUtil;
  9. import com.care.common.entity.*;
  10. import com.care.common.enums.*;
  11. import com.care.common.mapper.CareEventOrderMapper;
  12. import com.care.common.service.*;
  13. import com.care.common.vo.order.EventStaVO;
  14. import org.apache.commons.compress.utils.Lists;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import javax.annotation.Resource;
  18. import java.util.List;
  19. /**
  20. * 事件定单表(CareEventOrder)表服务实现类
  21. *
  22. * @author makejava
  23. * @since 2021-05-21 00:08:29
  24. */
  25. @Service
  26. public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper, CareEventOrder> implements CareEventOrderService {
  27. @Resource
  28. private CareEventOrderHandleHisService careEventOrderHandleHisService;
  29. @Resource
  30. private CareHouseContactService careHouseContactService;
  31. @Resource
  32. private CareOlderService careOlderService;
  33. @Resource
  34. private CareEventOrderOlderService careEventOrderOlderService;
  35. @Resource
  36. private CareEventOrderChambService careEventOrderChambService;
  37. @Resource
  38. private CareEventOrderContactStatusService careEventOrderContactStatusService;
  39. @Resource
  40. private CareHouseService careHouseService;
  41. @Resource
  42. private CareDeviceService careDeviceService;
  43. @Resource
  44. private CareSysUserService careSysUserService;
  45. @Resource
  46. private RedisUtil redisUtil;
  47. @Resource
  48. private SmsSendService smsSendService;
  49. @Resource
  50. private CareHouseOlderRelService careHouseOlderRelService;
  51. /**
  52. * 统计事件数量
  53. *
  54. * @param orgId
  55. * @param stationId
  56. * @return
  57. */
  58. @Override
  59. public EventStaVO statOrder(Long orgId, Long stationId) {
  60. return this.baseMapper.statOrder(orgId,stationId);
  61. }
  62. /**
  63. * 更新,不再生成新的告警工单,只作为子事件插入到当前工单的历史记录中
  64. * @param order
  65. * @param orderDb
  66. * @return
  67. */
  68. @Override
  69. @Transactional(rollbackFor = Exception.class)
  70. public boolean saveHisOrder(CareEventOrder order,CareEventOrder orderDb){
  71. // orderDb.setOrderType(order.getOrderType());
  72. // this.updateById(orderDb);//更新
  73. order.setId(orderDb.getId());
  74. CareEventOrderHandleHis his = new CareEventOrderHandleHis();
  75. his.setOrgId(order.getOrgId());
  76. his.setStationId(order.getStationId());
  77. his.setOrderId(order.getId());
  78. his.setHouseId(order.getHouseId());
  79. his.setOrderType(order.getOrderType());
  80. if (OrderTypeEnum.ZHU_DONG_HU_JIAO.getValue().equals(order.getOrderType()) || OrderTypeEnum.HU_WAI_HU_JIAO.getValue().equals(order.getOrderType())){
  81. his.setLogType(LogTypeEnum.OLDER_EVENT.getValue());
  82. his.setLogObjectId(order.getOlderId());
  83. his.setLogObjectName(order.getOlderName());
  84. his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType()));
  85. his.setRelationTypeDesc(RelationTypeEnum.OLDER.getName());
  86. } else {
  87. his.setLogType(LogTypeEnum.DEV_EVENT.getValue());
  88. his.setLogObjectId(order.getDevId());
  89. his.setLogObjectName(order.getDevName());
  90. his.setLogResult("发生"+OrderTypeEnum.getCodeToName(order.getOrderType()));
  91. his.setRelationTypeDesc(RelationTypeEnum.HOUSE.getName());
  92. }
  93. his.setCreateTime(order.getCreateTime());
  94. this.careEventOrderHandleHisService.save(his);
  95. return true;
  96. }
  97. /**
  98. * 新增,生成新的告警事件工单
  99. * @param order
  100. * @return
  101. */
  102. @Override
  103. @Transactional(rollbackFor = Exception.class)
  104. public boolean saveOrder(CareEventOrder order){
  105. this.baseMapper.insert(order);
  106. CareEventOrderHandleHis his = new CareEventOrderHandleHis();
  107. his.setOrgId(order.getOrgId());
  108. his.setStationId(order.getStationId());
  109. his.setOrderId(order.getId());
  110. his.setHouseId(order.getHouseId());
  111. his.setOrderType(order.getOrderType());
  112. if (OrderTypeEnum.ZHU_DONG_HU_JIAO.getValue().equals(order.getOrderType()) || OrderTypeEnum.HU_WAI_HU_JIAO.getValue().equals(order.getOrderType())){
  113. his.setLogType(LogTypeEnum.OLDER_EVENT.getValue());
  114. his.setLogObjectId(order.getOlderId());
  115. his.setLogObjectName(order.getOlderName());
  116. his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType()));
  117. his.setRelationTypeDesc(RelationTypeEnum.OLDER.getName());
  118. }else{
  119. his.setLogType(LogTypeEnum.DEV_EVENT.getValue());
  120. his.setLogObjectId(order.getDevId());
  121. his.setLogObjectName(order.getDevName());
  122. his.setLogResult("发生"+OrderTypeEnum.getCodeToName(order.getOrderType()));
  123. his.setRelationTypeDesc(RelationTypeEnum.HOUSE.getName());
  124. }
  125. his.setCreateTime(order.getCreateTime());
  126. addOlderToOrderOlder(order);
  127. List<CareEventOrderChamb> chambList = addChamberlainToOrder(order);
  128. List<CareEventOrderContactStatus> contacts = addContactToOrderContact(order);
  129. Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
  130. if (switchSms != null){
  131. //给联系人发送短信
  132. CareHouse house = this.careHouseService.getById(order.getHouseId());
  133. if (CollUtil.isNotEmpty(contacts)){
  134. contacts.forEach(item ->{
  135. boolean smsResult = smsSendService.sendSmsToLianxiren(order.getOrderType(),item.getContactPhone(),house.getName());
  136. if (smsResult){
  137. CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
  138. his2.setOrgId(item.getOrgId());
  139. his2.setStationId(item.getStationId());
  140. his2.setOrderId(item.getOrderId());
  141. his2.setLogType(LogTypeEnum.SMS.getValue());
  142. his2.setLogObjectId(item.getContactId());
  143. his2.setLogObjectName(item.getContactName());
  144. his2.setLogResult("短信通知");
  145. his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
  146. his2.setCreateTime(DateUtil.date());
  147. his2.setRelationTypeDesc(item.getRelationTypeDesc());
  148. this.careEventOrderHandleHisService.save(his2);
  149. }
  150. });
  151. }
  152. //给管家发短信
  153. if (CollUtil.isNotEmpty(chambList)){
  154. chambList.forEach(item ->{
  155. boolean smsResult = smsSendService.sendSmsToLianxiren(order.getOrderType(),item.getPhone(),house.getName());
  156. if (smsResult){
  157. CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
  158. his2.setOrgId(item.getOrgId());
  159. his2.setStationId(item.getStationId());
  160. his2.setOrderId(item.getOrderId());
  161. his2.setLogType(LogTypeEnum.SMS.getValue());
  162. his2.setLogObjectId(item.getChambId());
  163. his2.setLogObjectName(item.getChambName());
  164. his2.setLogResult("短信通知");
  165. his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
  166. his2.setCreateTime(DateUtil.date());
  167. his2.setRelationTypeDesc("管家");
  168. this.careEventOrderHandleHisService.save(his2);
  169. }
  170. });
  171. }
  172. }
  173. this.careEventOrderHandleHisService.save(his);
  174. return true;
  175. }
  176. /**
  177. * 将老人加入到 工单中
  178. * @param order
  179. */
  180. private void addOlderToOrderOlder(CareEventOrder order){
  181. QueryWrapper<CareHouseOlderRel> queryWrapper = new QueryWrapper<>();
  182. queryWrapper.lambda().eq(CareHouseOlderRel::getHouseId,order.getHouseId());
  183. List<CareHouseOlderRel> olderRels = this.careHouseOlderRelService.list(queryWrapper);
  184. if (CollUtil.isNotEmpty(olderRels)){
  185. List<CareEventOrderOlder> ceoos = Lists.newArrayList();
  186. olderRels.forEach(item -> {
  187. CareOlder careOlder = this.careOlderService.getById(item.getOlderId());
  188. CareEventOrderOlder ceoo = new CareEventOrderOlder();
  189. BeanUtil.copyProperties(careOlder,ceoo);
  190. ceoo.setOrgId(order.getOrgId());
  191. ceoo.setStationId(order.getStationId());
  192. ceoo.setOrderId(order.getId());
  193. ceoo.setOlderId(item.getId());
  194. if (order.getOlderId() == item.getId()){
  195. //主动呼叫
  196. ceoo.setIsZhudong(1);
  197. }else{
  198. ceoo.setIsZhudong(0);
  199. }
  200. ceoo.setStatus(OrderOlderStatusEnum.WEI_LIAN_XI.getValue());
  201. ceoo.setCreateTime(order.getCreateTime());
  202. ceoos.add(ceoo);
  203. });
  204. this.careEventOrderOlderService.saveBatch(ceoos);
  205. }
  206. }
  207. /**
  208. * 将管家加入到工单当中
  209. * @param order
  210. */
  211. private List<CareEventOrderChamb> addChamberlainToOrder(CareEventOrder order){
  212. List<CareEventOrderChamb> chambList = Lists.newArrayList();
  213. CareDevice dev = this.careDeviceService.getById(order.getDevId());
  214. if (dev != null){
  215. CareSysUser user = this.careSysUserService.getById(dev.getChambId());
  216. if (user != null){
  217. CareEventOrderChamb chamb = new CareEventOrderChamb();
  218. chamb.setOrgId(user.getOrgId());
  219. chamb.setStationId(user.getStationId());
  220. chamb.setOrderId(order.getId());
  221. chamb.setChambId(user.getId());
  222. chamb.setChambName(user.getName());
  223. chamb.setPhone(user.getPhone());
  224. chamb.setStatus(ChambOrderStatusEnum.TODO.getValue());
  225. chamb.setHouseId(dev.getHouseId());
  226. chamb.setCreateTime(order.getCreateTime());
  227. chamb.setModifyTime(order.getCreateTime());
  228. this.careEventOrderChambService.save(chamb);
  229. chambList.add(chamb);
  230. }
  231. }
  232. return chambList;
  233. }
  234. /**
  235. * 将紧急联系人加入到联系人当中
  236. * @param order
  237. */
  238. private List<CareEventOrderContactStatus> addContactToOrderContact(CareEventOrder order){
  239. List<CareEventOrderContactStatus> orderContactList = Lists.newArrayList();
  240. List<CareHouseContact> cons = this.careHouseContactService.queryContactByHouseId(order.getHouseId());
  241. if (CollUtil.isNotEmpty(cons)){
  242. cons.forEach(item ->{
  243. CareEventOrderContactStatus orderContactStatus = new CareEventOrderContactStatus();
  244. orderContactStatus.setOrgId(order.getOrgId());
  245. orderContactStatus.setStationId(order.getStationId());
  246. orderContactStatus.setOrderId(order.getId());
  247. orderContactStatus.setHouseId(order.getHouseId());
  248. orderContactStatus.setContactRole(UserRoleEnum.CONTACT.getValue());
  249. orderContactStatus.setContactId(item.getId());
  250. orderContactStatus.setStatus(ContactorStatusEnum.WEI_LIAN_XI.getValue());
  251. orderContactStatus.setContactName(item.getContactName());
  252. orderContactStatus.setContactPhone(item.getContactPhone());
  253. orderContactStatus.setLongitude(item.getLongitude());
  254. orderContactStatus.setLatitude(item.getLatitude());
  255. orderContactStatus.setRelationTypeDesc(RelationTypeEnum.getCodeToName(item.getRelationType()));
  256. orderContactStatus.setContactLevel(item.getContactLevel());
  257. orderContactStatus.setCreateTime(order.getCreateTime());
  258. orderContactStatus.setModifyTime(order.getCreateTime());
  259. orderContactList.add(orderContactStatus);
  260. });
  261. this.careEventOrderContactStatusService.saveBatch(orderContactList);
  262. }
  263. return orderContactList;
  264. }
  265. }