package com.care.bms.service; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.care.bigscreen.service.BigScreenService; 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.exception.BDException; import com.care.common.service.*; import com.care.common.vo.PageReqVO; import com.care.common.vo.UserLogindConvertVO; import com.care.common.vo.order.*; import com.care.common.vo.outcall.CcCallResultVO; import org.apache.commons.compress.utils.Lists; import org.springframework.beans.BeanUtils; 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 @Transactional(rollbackFor = Exception.class) public class BmsEventOrderService { @Resource RedisUtil redisUtil; @Resource private CareEventOrderService careEventOrderService; @Resource private CareSysUserService careSysUserService; @Resource private CareEventOrderOlderService careEventOrderOlderService; @Resource private CareEventOrderChambService careEventOrderChambService; @Resource private CareEventOrderContactStatusService careEventOrderContactStatusService; @Resource private CareEventOrderHandleHisService careEventOrderHandleHisService; @Resource private CareEventOrderKeyApplyService careEventOrderKeyApplyService; @Resource private CcCallResultService ccCallResultService; @Resource private CareHouseService careHouseService; @Resource private CareHouseContactService careHouseContactService; @Resource private CareOlderService careOlderService; @Resource private SmsSendService smsSendService; @Resource private BigScreenService bigScreenService; /** * 工单查询 * @param orderStatus * @param olderName * @param loginUser * @param pageReqVo * @return */ public IPage listEvent(String orderStatus, String olderName, UserLogindConvertVO loginUser, PageReqVO pageReqVo) { IPage page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize()); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(StrUtil.isNotEmpty(orderStatus),CareEventOrder::getStatus, orderStatus) .like(StrUtil.isNotEmpty(olderName),CareEventOrder::getOlderName,olderName) .eq(CareEventOrder::getOrgId,loginUser.getOrgId()) .eq(loginUser.getStationId()!=null,CareEventOrder::getStationId,loginUser.getStationId()) .orderByAsc(CareEventOrder::getCreateTime); IPage pageRes = this.careEventOrderService.page(page, queryWrapper); IPage results = new Page<>(pageRes.getCurrent(),pageRes.getSize(),pageRes.getTotal()); if(CollUtil.isNotEmpty(pageRes.getRecords())){ List list = new ArrayList<>(); pageRes.getRecords().forEach(item -> { EventOrderVO resVO = new EventOrderVO(); BeanUtils.copyProperties(item,resVO); resVO.setOrderTypeName(OrderTypeEnum.getCodeToName(item.getOrderType())); resVO.setStatusName(OrderStatusEnum.getCodeToName(item.getStatus())); list.add(resVO); }); results.setRecords(list); } return results; } /** * 我的处理事件 - 坐席 * * @param orderStatus * @param olderName * @param loginUser * @param pageReqVo * @return */ public IPage listEvent4MyDo(String orderStatus, String olderName, UserLogindConvertVO loginUser, PageReqVO pageReqVo) { IPage page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize()); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(StrUtil.isNotEmpty(orderStatus),CareEventOrder::getStatus, orderStatus) .like(StrUtil.isNotEmpty(olderName),CareEventOrder::getOlderName,olderName) .eq(CareEventOrder::getOrgId,loginUser.getOrgId()) .eq(CareEventOrder::getSeatId,loginUser.getId()) .orderByAsc(CareEventOrder::getModifyTime); IPage pageRes = this.careEventOrderService.page(page, queryWrapper); IPage results = new Page<>(pageRes.getCurrent(),pageRes.getSize(),pageRes.getTotal()); if(CollUtil.isNotEmpty(pageRes.getRecords())){ List list = new ArrayList<>(); pageRes.getRecords().forEach(item -> { EventOrderVO resVO = new EventOrderVO(); BeanUtils.copyProperties(item,resVO); resVO.setOrderTypeName(OrderTypeEnum.getCodeToName(item.getOrderType())); resVO.setStatusName(OrderStatusEnum.getCodeToName(item.getStatus())); list.add(resVO); }); results.setRecords(list); } return results; } /** * 处理工单 * 使用分布式锁 * * @param orderId * @param loginUser */ public void receiveOrder(Long orderId, UserLogindConvertVO loginUser) { String key = RedisKeyConstant.DO_ORDER+":"+orderId; boolean result = redisUtil.tryLock(key,3000); try{ if (result){ CareEventOrder order = this.careEventOrderService.getById(orderId); //1.判断工单的状态,如果不是未处理状态,则不能处理 if (OrderStatusEnum.TODO.getValue().equals(order.getStatus())){ UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.lambda().eq(CareEventOrder::getId,orderId) .set(CareEventOrder::getStatus,OrderStatusEnum.DOING.getValue()) .set(CareEventOrder::getSeatId,loginUser.getId()) .set(CareEventOrder::getSeatName,loginUser.getName()) .set(CareEventOrder::getModifyTime,DateUtil.date()); this.careEventOrderService.update(updateWrapper); }else{ throw new BDException("该工单状态已变化,请刷新"); } }else { throw new BDException("该工单已经再被处理"); } }finally { redisUtil.releaseLock(key); } } /** * * @param orderId * @return */ public EventOrderVO getOrderInfo(Long orderId){ EventOrderVO eventOrderVO = null; CareEventOrder order = this.careEventOrderService.getById(orderId); if (order!=null){ eventOrderVO = new EventOrderVO(); BeanUtil.copyProperties(order,eventOrderVO); eventOrderVO.setStatusName(OrderStatusEnum.getCodeToName(eventOrderVO.getStatus())); eventOrderVO.setOrderType(OrderTypeEnum.getCodeToName(eventOrderVO.getOrderType())); CareHouse house = this.careHouseService.getById(order.getHouseId()); CareHouseVO houseVO = new CareHouseVO(); BeanUtil.copyProperties(house,houseVO); houseVO.setRelationTypeDesc("屋主"); eventOrderVO.setHouse(houseVO); } return eventOrderVO; } /** * 查询老人列表 * @param orderId * @return */ public List queryOrderOlderList(Long orderId){ List orderOlderVOList = Lists.newArrayList(); CareEventOrder order = this.careEventOrderService.getById(orderId); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareEventOrderOlder::getOrderId,orderId); List ceoos = this.careEventOrderOlderService.list(queryWrapper); if (CollUtil.isNotEmpty(ceoos)){ ChamberlainVO chamberlainVO = new ChamberlainVO(); CareHouse house = this.careHouseService.getById(order.getHouseId()); if (house!=null){ CareSysUser user = this.careSysUserService.getById(house.getChambId()); if (user!= null){ BeanUtil.copyProperties(user,chamberlainVO); } } ceoos.forEach(item->{ OrderOlderVO orderOlderVO = new OrderOlderVO(); BeanUtil.copyProperties(item,orderOlderVO); orderOlderVO.setStatusDesc(OrderOlderStatusEnum.getCodeToName(orderOlderVO.getStatus())); orderOlderVO.setChamberlain(chamberlainVO); orderOlderVOList.add(orderOlderVO); }); } return orderOlderVOList; } /** * 查询事件处理历史 * @param orderId * @return */ public List queryOrderHandleHis(Long orderId){ List result = Lists.newArrayList(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareEventOrderHandleHis::getOrderId,orderId) .orderByDesc(CareEventOrderHandleHis::getCreateTime); List datas = this.careEventOrderHandleHisService.list(queryWrapper); if (CollUtil.isNotEmpty(datas)){ datas.forEach(item ->{ OrderHandleHisVO hisVO = new OrderHandleHisVO(); BeanUtil.copyProperties(item,hisVO); result.add(hisVO); }); } return result; } /** * 查询工单的相关联系人 * @param orderId * @return */ public List queryContactList(Long orderId){ List result = Lists.newArrayList(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareEventOrderContactStatus::getOrderId,orderId).orderByAsc(CareEventOrderContactStatus::getContactLevel); List ceocs = this.careEventOrderContactStatusService.list(queryWrapper); if (CollUtil.isNotEmpty(ceocs)){ ceocs.forEach(item -> { OrderContactorVO contactorVO = new OrderContactorVO(); BeanUtil.copyProperties(item,contactorVO); contactorVO.setStatusDesc(ContactorStatusEnum.getCodeToName(item.getStatus())); result.add(contactorVO); }); } return result; } /** * 更新工单状态 * @param orderId * @param orderStatusEnum */ public void updateOrderStatus(Long orderId,OrderStatusEnum orderStatusEnum){ CareEventOrder order = this.careEventOrderService.getById(orderId); UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.lambda().eq(CareEventOrder::getId,orderId) .set(CareEventOrder::getStatus,orderStatusEnum.getValue()) .set(CareEventOrder::getModifyTime,DateUtil.date()); this.careEventOrderService.update(updateWrapper); this.bigScreenService.pushRtEventFlag(order.getStationId().toString()); } /** * 获取钥匙请求历史 * @param orderId * @return */ public List queryKeyAuthList(Long orderId){ List result = Lists.newArrayList(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareEventOrderKeyApply::getOrderId,orderId).orderByDesc(CareEventOrderKeyApply::getCreateTime); List ceocs = this.careEventOrderKeyApplyService.list(queryWrapper); if (CollUtil.isNotEmpty(ceocs)){ ceocs.forEach(item -> { OrderKeyApplyVO keyApplyVO = new OrderKeyApplyVO(); BeanUtil.copyProperties(item,keyApplyVO); result.add(keyApplyVO); }); } return result; } /** * 钥匙授权 * @param applyId * @return */ public boolean keyAuth(Long applyId ,UserLogindConvertVO loginUser){ CareEventOrderKeyApply apply = this.careEventOrderKeyApplyService.getById(applyId); UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.lambda().eq(CareEventOrderKeyApply::getId,applyId) .set(CareEventOrderKeyApply::getStatus,KeyAuthEnum.AUTHED.getValue()); //TODO 增加日志 CareEventOrderHandleHis his = new CareEventOrderHandleHis(); his.setOrgId(apply.getOrgId()); his.setStationId(apply.getStationId()); his.setOrderId(apply.getOrderId()); his.setLogType(LogTypeEnum.KEY.getValue()); his.setLogObjectId(apply.getApplyUserId()); his.setLogObjectName(apply.getApplyUserName()); his.setLogResult("获得钥匙"); his.setOpUserRole(UserRoleEnum.SEAT.getValue()); his.setOpUserId(loginUser.getId()); his.setOpUserName(loginUser.getName()); his.setCreateTime(DateUtil.date()); his.setRelationTypeDesc(apply.getRelationTypeDesc()); this.careEventOrderHandleHisService.save(his); return this.careEventOrderKeyApplyService.update(updateWrapper); } /** * 修改联系人状态 * @param orderContactId * @param status * @return */ public boolean updateOrderContactStatus(Long orderContactId,Integer status,UserLogindConvertVO loginUser){ CareEventOrderContactStatus contact = this.careEventOrderContactStatusService.getById(orderContactId); UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.lambda().eq(CareEventOrderContactStatus::getId,orderContactId) .set(CareEventOrderContactStatus::getStatus,status); CareEventOrderHandleHis his = new CareEventOrderHandleHis(); his.setOrgId(contact.getOrgId()); his.setStationId(contact.getStationId()); his.setOrderId(contact.getOrderId()); his.setLogType(LogTypeEnum.OUT_CALL.getValue()); his.setLogObjectId(contact.getContactId()); his.setLogObjectName(contact.getContactName()); his.setLogResult(ContactorStatusEnum.getCodeToName(status)); his.setOpUserRole(UserRoleEnum.SEAT.getValue()); his.setOpUserId(loginUser.getId()); his.setOpUserName(loginUser.getName()); his.setCreateTime(DateUtil.date()); his.setRelationTypeDesc(contact.getRelationTypeDesc()); this.careEventOrderHandleHisService.save(his); return this.careEventOrderContactStatusService.update(updateWrapper); } /** * 获取通话结果 * @param hisId */ public CcCallResultVO getCallRadio(Long hisId){ CareEventOrderHandleHis his = this.careEventOrderHandleHisService.getById(hisId); if (his.getCallResultId()!= null){ return this.ccCallResultService.getCallResult(his.getCallResultId()); }else { return null; } } public void insertTestData(){ List houses = Lists.newArrayList(); CareHouse house = new CareHouse(); house.setId(1L); house.setName("201室"); house.setAddr("北太平庄21号院1单元201室"); houses.add(house); CareEventOrder order = new CareEventOrder(); order.setOrgId(1L); order.setOrgName("测试机构"); order.setStationId(1L); order.setStationName("站点1"); order.setHouseId(1L); order.setDevId(1L); order.setDevName("卧室"); order.setHouseName(house.getName()); order.setTitle(house.getAddr()); order.setOrderType(OrderTypeEnum.values()[RandomUtil.randomInt(0,4)].getValue()); order.setStatus("0"); order.setCreateTime(DateUtil.date()); order.setModifyTime(DateUtil.date()); this.careEventOrderService.saveOrder(order); } /** * 发送短信接口 * @param orderContactId * @return */ public boolean sendSmsToLianxiren(Long orderContactId){ CareEventOrderContactStatus contactStatus = this.careEventOrderContactStatusService.getById(orderContactId); if (contactStatus!=null){ CareHouse house = this.careHouseService.getById(contactStatus.getHouseId()); boolean smsResult = smsSendService.sendSmsToLianxiren(contactStatus.getContactPhone(),house.getName()); if (smsResult){ CareEventOrderHandleHis his = new CareEventOrderHandleHis(); his.setOrgId(contactStatus.getOrgId()); his.setStationId(contactStatus.getStationId()); his.setOrderId(contactStatus.getOrderId()); his.setLogType(LogTypeEnum.SMS.getValue()); his.setLogObjectId(contactStatus.getContactId()); his.setLogObjectName(contactStatus.getContactName()); his.setLogResult("短信通知"); his.setOpUserRole(UserRoleEnum.SEAT.getValue()); his.setCreateTime(DateUtil.date()); his.setRelationTypeDesc(contactStatus.getRelationTypeDesc()); this.careEventOrderHandleHisService.save(his); return true; }else { return false; } }else{ return false; } } /** * 统计机构下的事件数量 * @param loginUser * @return */ public EventStaVO statOrder(UserLogindConvertVO loginUser) { return this.careEventOrderService.statOrder(loginUser.getOrgId(),null); } /** * 查询管家 * @param orderId * @return */ public CareEventOrderChamb queryChamberlain(Long orderId){ CareEventOrderChamb data = new CareEventOrderChamb(); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.lambda().eq(CareEventOrderChamb::getOrderId,orderId); List datas = this.careEventOrderChambService.list(queryWrapper); if (CollUtil.isNotEmpty(datas)){ data = datas.get(0); } return data ; } public String getSmsTemplate(Long orderId){ CareEventOrder order = this.careEventOrderService.getById(orderId); if (order!=null){ CareHouse house = this.careHouseService.getById(order.getHouseId()); return house.getName()+"的老人发生跌倒/久滞事情"; } return null; } }