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 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 chambList = addChamberlainToOrder(order); List contacts = addContactToOrderContact(order); sendSms(order,chambList,contacts); return true; } private void sendSms(CareEventOrder order,List chambList,List 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 queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareHouseOlderRel::getHouseId,order.getHouseId()); List olderRels = this.careHouseOlderRelService.list(queryWrapper); if (CollUtil.isNotEmpty(olderRels)){ List 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 addChamberlainToOrder(CareEventOrder order){ List 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 addContactToOrderContact(CareEventOrder order){ List orderContactList = Lists.newArrayList(); List 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 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 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 chambList = new ArrayList<>(); CareEventOrderChamb chamb = this.careEventOrderChambService.getChambOrderByOrderId(order.getId()); chambList.add(chamb); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareEventOrderContactStatus::getOrderId,order.getId()); List 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); } } }