123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399 |
- package com.care.common.service.impl;
- import cn.hutool.core.bean.BeanUtil;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.date.DateUtil;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.care.common.cache.RedisKeyConstant;
- import com.care.common.cache.RedisUtil;
- import com.care.common.entity.*;
- import com.care.common.enums.*;
- import com.care.common.mapper.CareEventOrderMapper;
- import com.care.common.service.*;
- import com.care.common.vo.order.EventStaVO;
- import org.apache.commons.compress.utils.Lists;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 事件定单表(CareEventOrder)表服务实现类
- *
- * @author makejava
- * @since 2021-05-21 00:08:29
- */
- @Service
- public class CareEventOrderServiceImpl extends ServiceImpl<CareEventOrderMapper, CareEventOrder> implements CareEventOrderService {
- @Resource
- private CareEventOrderHandleHisService careEventOrderHandleHisService;
- @Resource
- private CareHouseContactService careHouseContactService;
- @Resource
- private CareOlderService careOlderService;
- @Resource
- private CareEventOrderOlderService careEventOrderOlderService;
- @Resource
- private CareEventOrderChambService careEventOrderChambService;
- @Resource
- private CareEventOrderContactStatusService careEventOrderContactStatusService;
- @Resource
- private CareHouseService careHouseService;
- @Resource
- private CareDeviceService careDeviceService;
- @Resource
- private CareSysUserService careSysUserService;
- @Resource
- private RedisUtil redisUtil;
- @Resource
- private SmsSendService smsSendService;
- @Resource
- private CareHouseOlderRelService careHouseOlderRelService;
- /**
- * 统计事件数量
- *
- * @param orgId
- * @param stationId
- * @return
- */
- @Override
- public EventStaVO statOrder(Long orgId, Long stationId) {
- return this.baseMapper.statOrder(orgId,stationId);
- }
- /**
- * 更新,不再生成新的告警工单,只作为子事件插入到当前工单的历史记录中
- * @param order
- * @param orderDb
- * @return
- */
- @Override
- @Transactional(rollbackFor = Exception.class)
- public boolean saveHisOrder(CareEventOrder order,CareEventOrder orderDb,boolean isCancel){
- // orderDb.setOrderType(order.getOrderType());
- // this.updateById(orderDb);//更新
- order.setId(orderDb.getId());
- CareEventOrderHandleHis his = new CareEventOrderHandleHis();
- his.setOrgId(order.getOrgId());
- his.setStationId(order.getStationId());
- his.setOrderId(order.getId());
- his.setHouseId(order.getHouseId());
- his.setOrderType(order.getOrderType());
- if (OrderTypeEnum.ZHU_DONG_HU_JIAO.getValue().equals(order.getOrderType()) || OrderTypeEnum.HU_WAI_HU_JIAO.getValue().equals(order.getOrderType())){
- his.setLogType(LogTypeEnum.OLDER_EVENT.getValue());
- his.setLogObjectId(order.getOlderId());
- his.setLogObjectName(order.getOlderName());
- his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType()));
- his.setRelationTypeDesc(RelationTypeEnum.OLDER.getName());
- } else {
- his.setLogType(LogTypeEnum.DEV_EVENT.getValue());
- his.setLogObjectId(order.getDevId());
- his.setLogObjectName(order.getDevName());
- if (isCancel){
- his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType())+"取消");
- }else{
- his.setLogResult("发生"+OrderTypeEnum.getCodeToName(order.getOrderType()));
- }
- his.setRelationTypeDesc(RelationTypeEnum.HOUSE.getName());
- }
- his.setCreateTime(order.getCreateTime());
- this.careEventOrderHandleHisService.save(his);
- return true;
- }
- /**
- * 新增,生成新的告警事件工单
- * @param order
- * @return
- */
- @Override
- @Transactional(rollbackFor = Exception.class)
- public boolean saveOrder(CareEventOrder order){
- this.baseMapper.insert(order);
- CareEventOrderHandleHis his = new CareEventOrderHandleHis();
- his.setOrgId(order.getOrgId());
- his.setStationId(order.getStationId());
- his.setOrderId(order.getId());
- his.setHouseId(order.getHouseId());
- his.setOrderType(order.getOrderType());
- if (OrderTypeEnum.ZHU_DONG_HU_JIAO.getValue().equals(order.getOrderType()) || OrderTypeEnum.HU_WAI_HU_JIAO.getValue().equals(order.getOrderType())){
- his.setLogType(LogTypeEnum.OLDER_EVENT.getValue());
- his.setLogObjectId(order.getOlderId());
- his.setLogObjectName(order.getOlderName());
- his.setLogResult(OrderTypeEnum.getCodeToName(order.getOrderType()));
- his.setRelationTypeDesc(RelationTypeEnum.OLDER.getName());
- }else{
- his.setLogType(LogTypeEnum.DEV_EVENT.getValue());
- his.setLogObjectId(order.getDevId());
- his.setLogObjectName(order.getDevName());
- his.setLogResult("发生"+OrderTypeEnum.getCodeToName(order.getOrderType()));
- his.setRelationTypeDesc(RelationTypeEnum.HOUSE.getName());
- }
- his.setCreateTime(order.getCreateTime());
- addOlderToOrderOlder(order);
- this.careEventOrderHandleHisService.save(his);
- List<CareEventOrderChamb> chambList = addChamberlainToOrder(order);
- List<CareEventOrderContactStatus> contacts = addContactToOrderContact(order);
- sendSms(order,chambList,contacts);
- return true;
- }
- private void sendSms(CareEventOrder order,List<CareEventOrderChamb> chambList,List<CareEventOrderContactStatus> contacts){
- try{
- Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
- if (switchSms != null){
- //给联系人发送短信
- CareHouse house = this.careHouseService.getById(order.getHouseId());
- if (CollUtil.isNotEmpty(contacts)){
- contacts.forEach(item ->{
- boolean smsResult = false;
- if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.JIU_ZHI,item.getContactPhone(),house.getName());
- }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.DIE_DAO,item.getContactPhone(),house.getName());
- }
- if (smsResult){
- CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
- his2.setOrgId(item.getOrgId());
- his2.setStationId(item.getStationId());
- his2.setOrderId(item.getOrderId());
- his2.setLogType(LogTypeEnum.SMS.getValue());
- his2.setLogObjectId(item.getContactId());
- his2.setLogObjectName(item.getContactName());
- his2.setLogResult("短信通知");
- his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
- his2.setCreateTime(DateUtil.date());
- his2.setRelationTypeDesc(item.getRelationTypeDesc());
- this.careEventOrderHandleHisService.save(his2);
- }
- });
- }
- //给管家发短信
- if (CollUtil.isNotEmpty(chambList)){
- chambList.forEach(item ->{
- boolean smsResult = false;
- if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.JIU_ZHI,item.getPhone(),house.getName());
- }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.DIE_DAO,item.getPhone(),house.getName());
- }
- if (smsResult){
- CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
- his2.setOrgId(item.getOrgId());
- his2.setStationId(item.getStationId());
- his2.setOrderId(item.getOrderId());
- his2.setLogType(LogTypeEnum.SMS.getValue());
- his2.setLogObjectId(item.getChambId());
- his2.setLogObjectName(item.getChambName());
- his2.setLogResult("短信通知");
- his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
- his2.setCreateTime(DateUtil.date());
- his2.setRelationTypeDesc("管家");
- this.careEventOrderHandleHisService.save(his2);
- }
- });
- }
- }
- }catch (Exception e){
- log.error("发送短信通知失败",e);
- }
- }
- /**
- * 将老人加入到 工单中
- * @param order
- */
- private void addOlderToOrderOlder(CareEventOrder order){
- QueryWrapper<CareHouseOlderRel> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(CareHouseOlderRel::getHouseId,order.getHouseId());
- List<CareHouseOlderRel> olderRels = this.careHouseOlderRelService.list(queryWrapper);
- if (CollUtil.isNotEmpty(olderRels)){
- List<CareEventOrderOlder> ceoos = Lists.newArrayList();
- olderRels.forEach(item -> {
- CareOlder careOlder = this.careOlderService.getById(item.getOlderId());
- CareEventOrderOlder ceoo = new CareEventOrderOlder();
- BeanUtil.copyProperties(careOlder,ceoo);
- ceoo.setOrgId(order.getOrgId());
- ceoo.setStationId(order.getStationId());
- ceoo.setOrderId(order.getId());
- ceoo.setOlderId(item.getId());
- if (order.getOlderId() == item.getId()){
- //主动呼叫
- ceoo.setIsZhudong(1);
- }else{
- ceoo.setIsZhudong(0);
- }
- ceoo.setStatus(OrderOlderStatusEnum.WEI_LIAN_XI.getValue());
- ceoo.setCreateTime(order.getCreateTime());
- ceoos.add(ceoo);
- });
- this.careEventOrderOlderService.saveBatch(ceoos);
- }
- }
- /**
- * 将管家加入到工单当中
- * @param order
- */
- private List<CareEventOrderChamb> addChamberlainToOrder(CareEventOrder order){
- List<CareEventOrderChamb> chambList = Lists.newArrayList();
- CareDevice dev = this.careDeviceService.getById(order.getDevId());
- if (dev != null){
- CareSysUser user = this.careSysUserService.getById(dev.getChambId());
- if (user != null){
- CareEventOrderChamb chamb = new CareEventOrderChamb();
- chamb.setOrgId(user.getOrgId());
- chamb.setStationId(user.getStationId());
- chamb.setOrderId(order.getId());
- chamb.setChambId(user.getId());
- chamb.setChambName(user.getName());
- chamb.setPhone(user.getPhone());
- chamb.setStatus(ChambOrderStatusEnum.TODO.getValue());
- chamb.setHouseId(dev.getHouseId());
- chamb.setCreateTime(order.getCreateTime());
- chamb.setModifyTime(order.getCreateTime());
- this.careEventOrderChambService.save(chamb);
- chambList.add(chamb);
- }
- }
- return chambList;
- }
- /**
- * 将紧急联系人加入到联系人当中
- * @param order
- */
- private List<CareEventOrderContactStatus> addContactToOrderContact(CareEventOrder order){
- List<CareEventOrderContactStatus> orderContactList = Lists.newArrayList();
- List<CareHouseContact> cons = this.careHouseContactService.queryContactByHouseId(order.getHouseId());
- if (CollUtil.isNotEmpty(cons)){
- cons.forEach(item ->{
- CareEventOrderContactStatus orderContactStatus = new CareEventOrderContactStatus();
- orderContactStatus.setOrgId(order.getOrgId());
- orderContactStatus.setStationId(order.getStationId());
- orderContactStatus.setOrderId(order.getId());
- orderContactStatus.setHouseId(order.getHouseId());
- orderContactStatus.setContactRole(UserRoleEnum.CONTACT.getValue());
- orderContactStatus.setContactId(item.getId());
- orderContactStatus.setStatus(ContactorStatusEnum.WEI_LIAN_XI.getValue());
- orderContactStatus.setContactName(item.getContactName());
- orderContactStatus.setContactPhone(item.getContactPhone());
- orderContactStatus.setLongitude(item.getLongitude());
- orderContactStatus.setLatitude(item.getLatitude());
- orderContactStatus.setRelationTypeDesc(RelationTypeEnum.getCodeToName(item.getRelationType()));
- orderContactStatus.setContactLevel(item.getContactLevel());
- orderContactStatus.setCreateTime(order.getCreateTime());
- orderContactStatus.setModifyTime(order.getCreateTime());
- orderContactList.add(orderContactStatus);
- });
- this.careEventOrderContactStatusService.saveBatch(orderContactList);
- }
- return orderContactList;
- }
- /**
- * 自动取消工单
- *
- * @param order
- * @return
- */
- @Override
- public boolean autoCancelOrder(CareEventOrder order) {
- UpdateWrapper<CareEventOrder> updateWrapper = new UpdateWrapper<>();
- updateWrapper.lambda().set(CareEventOrder::getStatus,OrderStatusEnum.CANCEL.getValue()).eq(CareEventOrder::getId,order.getId());
- order.setStatus(OrderStatusEnum.CANCEL.getValue());
- this.update(updateWrapper);
- //管家工单标记为取消
- CareEventOrderChamb careEventOrderChamb = this.careEventOrderChambService.getChambOrderByOrderId(order.getId());
- UpdateWrapper<CareEventOrderChamb> updateWrapperChamb = new UpdateWrapper<>();
- updateWrapperChamb.lambda().set(CareEventOrderChamb::getStatus,ChambOrderStatusEnum.CANCEL.getValue()).eq(CareEventOrderChamb::getId,careEventOrderChamb.getId());
- this.careEventOrderChambService.update(updateWrapperChamb);
- //记录一条取消his
- CareEventOrder orderDb = new CareEventOrder();
- orderDb.setId(order.getId());
- this.saveHisOrder(order, orderDb,true);
- cancelEventSMSNotic(order);
- return true;
- }
- /**
- * 取消事件,短信通知
- * @param order
- */
- private void cancelEventSMSNotic(CareEventOrder order){
- try{
- Object switchSms = redisUtil.get(RedisKeyConstant.SWITCH_SMS);
- if (switchSms != null){
- List<CareEventOrderChamb> chambList = new ArrayList<>();
- CareEventOrderChamb chamb = this.careEventOrderChambService.getChambOrderByOrderId(order.getId());
- chambList.add(chamb);
- QueryWrapper<CareEventOrderContactStatus> queryWrapper = new QueryWrapper<>();
- queryWrapper.lambda().eq(CareEventOrderContactStatus::getOrderId,order.getId());
- List<CareEventOrderContactStatus> contacts = this.careEventOrderContactStatusService.list(queryWrapper);
- //给联系人发送短信
- CareHouse house = this.careHouseService.getById(order.getHouseId());
- if (CollUtil.isNotEmpty(contacts)){
- contacts.forEach(item ->{
- boolean smsResult = false;
- if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_JIUZHI,item.getContactPhone(),house.getName());
- }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_DIEDAO,item.getContactPhone(),house.getName());
- }
- if (smsResult){
- CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
- his2.setOrgId(item.getOrgId());
- his2.setStationId(item.getStationId());
- his2.setOrderId(item.getOrderId());
- his2.setLogType(LogTypeEnum.SMS.getValue());
- his2.setLogObjectId(item.getContactId());
- his2.setLogObjectName(item.getContactName());
- his2.setLogResult("事件取消短信通知");
- his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
- his2.setCreateTime(DateUtil.date());
- his2.setRelationTypeDesc(item.getRelationTypeDesc());
- this.careEventOrderHandleHisService.save(his2);
- }
- });
- }
- //给管家发短信
- if (CollUtil.isNotEmpty(chambList)){
- chambList.forEach(item ->{
- boolean smsResult = false;
- if (OrderTypeEnum.JIU_ZHI.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_JIUZHI,item.getPhone(),house.getName());
- }else if(OrderTypeEnum.DIE_DAO.getValue().equals(order.getOrderType())){
- smsResult = smsSendService.sendSmsToLianxiren(SmsTemplateTypeEnum.CANCEL_DIEDAO,item.getPhone(),house.getName());
- }
- if (smsResult){
- CareEventOrderHandleHis his2 = new CareEventOrderHandleHis();
- his2.setOrgId(item.getOrgId());
- his2.setStationId(item.getStationId());
- his2.setOrderId(item.getOrderId());
- his2.setLogType(LogTypeEnum.SMS.getValue());
- his2.setLogObjectId(item.getChambId());
- his2.setLogObjectName(item.getChambName());
- his2.setLogResult("事件取消短信通知");
- his2.setOpUserRole(UserRoleEnum.SEAT.getValue());
- his2.setCreateTime(DateUtil.date());
- his2.setRelationTypeDesc("管家");
- this.careEventOrderHandleHisService.save(his2);
- }
- });
- }
- }
- }catch (Exception e){
- log.error("发送短信通知失败",e);
- }
- }
- }
|