|
@@ -1,26 +1,31 @@
|
|
|
package com.care.bms.service;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.HexUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.care.bms.mqtt.MqttTool;
|
|
|
-import com.care.common.entity.CareDevice;
|
|
|
-import com.care.common.entity.CareHouse;
|
|
|
-import com.care.common.entity.CareStation;
|
|
|
-import com.care.common.enums.DeviceStatusEnum;
|
|
|
+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.util.MyBeanUtils;
|
|
|
import com.care.common.vo.PageReqVO;
|
|
|
import com.care.common.vo.UserLogindConvertVO;
|
|
|
import com.care.common.vo.device.DeviceVO;
|
|
|
+import com.care.common.vo.order.OrderContactorVO;
|
|
|
+import com.care.common.vo.order.OrderOlderVO;
|
|
|
import lombok.Synchronized;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* @Author: lilt
|
|
@@ -42,6 +47,20 @@ public class DeviceService {
|
|
|
private CareEventOrderService careEventOrderService;
|
|
|
@Resource
|
|
|
private CareOrganizationService careOrganizationService;
|
|
|
+ @Resource
|
|
|
+ private CareOlderService careOlderService;
|
|
|
+ @Resource
|
|
|
+ private CareHouseContactService careHouseContactService;
|
|
|
+ @Resource
|
|
|
+ private CareHouseOlderRelService careHouseOlderRelService;
|
|
|
+ @Resource
|
|
|
+ private CareHouseContactRelService careHouseContactRelService;
|
|
|
+ @Resource
|
|
|
+ private CareMemberInfoService careMemberInfoService;
|
|
|
+ @Resource
|
|
|
+ private CareSysUserService careSysUserService;
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 删除设备
|
|
|
* @param id
|
|
@@ -49,6 +68,7 @@ public class DeviceService {
|
|
|
*/
|
|
|
public boolean deleteById(Long id){
|
|
|
MqttTool.removeOne(this.careDeviceService.getById(id));
|
|
|
+
|
|
|
return this.careDeviceService.removeById(id);
|
|
|
}
|
|
|
|
|
@@ -58,6 +78,15 @@ public class DeviceService {
|
|
|
DeviceVO vo = new DeviceVO();
|
|
|
BeanUtil.copyProperties(dev,vo);
|
|
|
vo.setStatusName(DeviceStatusEnum.getCodeToName(vo.getStatus()));
|
|
|
+ vo.setActiveStatusName(DeviceActiveStatusEnum.getCodeToName(vo.getActiveStatus()));
|
|
|
+ vo.setDevSceneName(DeviceSceneEnum.getCodeToName(vo.getDevScene()));
|
|
|
+
|
|
|
+ CareSysUser careSysUser = this.careSysUserService.getById(vo.getChambId());
|
|
|
+ if (careSysUser != null){
|
|
|
+ vo.setChambPhone(careSysUser.getPhone());
|
|
|
+ vo.setChambName(careSysUser.getName());
|
|
|
+ }
|
|
|
+
|
|
|
CareStation station = this.careStationService.getById(vo.getStationId());
|
|
|
if (station!=null){
|
|
|
vo.setStationName(station.getShortName());
|
|
@@ -66,6 +95,36 @@ public class DeviceService {
|
|
|
if (house!=null){
|
|
|
vo.setHouseAddr(house.getAddr());
|
|
|
}
|
|
|
+ QueryWrapper<CareHouseOlderRel> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(CareHouseOlderRel::getHouseId,dev.getHouseId());
|
|
|
+ List<CareHouseOlderRel> olderRels = careHouseOlderRelService.list(queryWrapper);
|
|
|
+ if(CollUtil.isNotEmpty(olderRels)) {
|
|
|
+ List<OrderOlderVO> orderOlderVOList = new ArrayList<>();
|
|
|
+ olderRels.forEach(item -> {
|
|
|
+ CareOlder older = this.careOlderService.getById(item.getOlderId());
|
|
|
+ OrderOlderVO orderOlderVO = new OrderOlderVO();
|
|
|
+ BeanUtil.copyProperties(older,orderOlderVO);
|
|
|
+ orderOlderVO.setLiveTypeDesc(OlderLiveTypeEnum.getCodeToName(orderOlderVO.getLiveType()));
|
|
|
+ orderOlderVOList.add(orderOlderVO);
|
|
|
+ });
|
|
|
+ vo.setOrderOlderVOList(orderOlderVOList);
|
|
|
+ }
|
|
|
+
|
|
|
+ QueryWrapper<CareHouseContactRel> queryWrapper1 = new QueryWrapper<>();
|
|
|
+ queryWrapper1.lambda().eq(CareHouseContactRel::getHouseId,dev.getHouseId());
|
|
|
+ List<CareHouseContactRel> contactRels = careHouseContactRelService.list(queryWrapper1);
|
|
|
+ if(CollUtil.isNotEmpty(contactRels)) {
|
|
|
+ List<OrderContactorVO> orderContactorVOList = new ArrayList<>();
|
|
|
+ contactRels.forEach(item -> {
|
|
|
+ CareHouseContact contact = this.careHouseContactService.getById(item.getContactId());
|
|
|
+ OrderContactorVO orderContactorVO = new OrderContactorVO();
|
|
|
+ BeanUtil.copyProperties(contact,orderContactorVO);
|
|
|
+ orderContactorVO.setRelationTypeDesc(RelationTypeEnum.getCodeToName(orderContactorVO.getRelationType()));
|
|
|
+ orderContactorVOList.add(orderContactorVO);
|
|
|
+ });
|
|
|
+ vo.setOrderContactorVOList(orderContactorVOList);
|
|
|
+ }
|
|
|
+
|
|
|
return vo;
|
|
|
}
|
|
|
return null;
|
|
@@ -91,9 +150,11 @@ public class DeviceService {
|
|
|
//保存设备
|
|
|
CareDevice device = new CareDevice();
|
|
|
BeanUtil.copyProperties(vo,device);
|
|
|
+
|
|
|
device.setStatus(DeviceStatusEnum.ONLINE.getValue());
|
|
|
device.setCreateTime(new Date());
|
|
|
device.setOrgId(loginUser.getOrgId());
|
|
|
+
|
|
|
device.setTopic("5JPD/monitor/"+device.getDevCode()+"/event");
|
|
|
Long time = System.currentTimeMillis();
|
|
|
device.setClientId(HexUtil.toHex(time));
|
|
@@ -104,20 +165,151 @@ public class DeviceService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 保存设备
|
|
|
+ * 修改设备
|
|
|
* @param vo
|
|
|
*/
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void updateDevice(DeviceVO vo){
|
|
|
+ public void updateDevice(DeviceVO vo) {
|
|
|
+
|
|
|
+ List<OrderOlderVO> orderOlderVOList = vo.getOrderOlderVOList();
|
|
|
+ List<OrderContactorVO> orderContactorVOList = vo.getOrderContactorVOList();
|
|
|
+
|
|
|
+ if(CollUtil.isEmpty(orderOlderVOList) || CollUtil.isEmpty(orderContactorVOList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
//保存设备
|
|
|
CareDevice device = this.careDeviceService.getById(vo.getId());
|
|
|
- MqttTool.removeOne(device);
|
|
|
- BeanUtil.copyProperties(vo,device);
|
|
|
- device.setTopic("5JPD/monitor/"+device.getDevCode()+"/event");
|
|
|
+
|
|
|
+ if (DeviceActiveStatusEnum.ACTIVE.getValue().equals(device.getActiveStatus())) {
|
|
|
+ MqttTool.removeOne(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ MyBeanUtils.copyProperties(vo, device);
|
|
|
+
|
|
|
+ //处理house
|
|
|
+ if (device.getHouseId() == null) { //新增
|
|
|
+ CareHouse careHouse = new CareHouse();
|
|
|
+ careHouse.setAddr(vo.getHouseAddr());
|
|
|
+ careHouse.setName(vo.getHouseAddr());
|
|
|
+ careHouse.setLatitude(vo.getLatitude());
|
|
|
+ careHouse.setLongitude(vo.getLongitude());
|
|
|
+ careHouse.setCreateTime(new Date());
|
|
|
+ careHouse.setOrgId(device.getOrgId());
|
|
|
+ careHouse.setStationId(device.getStationId());
|
|
|
+ this.careHouseService.save(careHouse);
|
|
|
+ } else { //修改
|
|
|
+ CareHouse careHouse = this.careHouseService.getById(device.getHouseId());
|
|
|
+ careHouse.setAddr(vo.getHouseAddr());
|
|
|
+ careHouse.setName(vo.getHouseAddr());
|
|
|
+ careHouse.setLatitude(vo.getLatitude());
|
|
|
+ careHouse.setLongitude(vo.getLongitude());
|
|
|
+ careHouse.setModifyTime(new Date());
|
|
|
+ this.careHouseService.updateById(careHouse);
|
|
|
+ }
|
|
|
+
|
|
|
+ //关联用户
|
|
|
+ //用手机号查找care_member_info表,有的话就关联上,没有的话只保留手机号在设备表
|
|
|
+ QueryWrapper<CareMemberInfo> queryWrapper0 = new QueryWrapper<>();
|
|
|
+ queryWrapper0.lambda().eq(CareMemberInfo::getPhone,vo.getMemberPhone());
|
|
|
+ CareMemberInfo careMemberInfo = this.careMemberInfoService.getOne(queryWrapper0);
|
|
|
+
|
|
|
+ if (careMemberInfo != null) { //关联,
|
|
|
+ device.setMemberId(careMemberInfo.getId());
|
|
|
+ } else { //置空
|
|
|
+ device.setMemberId(null);//TODO,是否生效?
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //mqtt
|
|
|
+ if(StringUtils.isEmpty(device.getTopic())){
|
|
|
+ device.setTopic("5JPD/monitor/"+device.getDevCode()+"/event");
|
|
|
+ }
|
|
|
+ if(StringUtils.isEmpty(device.getClientId())){
|
|
|
+ Long time = System.currentTimeMillis();
|
|
|
+ device.setClientId(HexUtil.toHex(time));
|
|
|
+ }
|
|
|
+
|
|
|
device.setModifyTime(new Date());
|
|
|
this.careDeviceService.updateById(device);
|
|
|
- //保存完后,启动通道
|
|
|
- MqttTool.addOne(device, careDeviceService, bmsEventOrderService, careEventOrderService, careHouseService, careOrganizationService, careStationService);
|
|
|
+
|
|
|
+ List<Long> olderIdUpdates = new ArrayList<>();
|
|
|
+ orderOlderVOList.forEach(item -> {
|
|
|
+ CareOlder careOlder = new CareOlder();
|
|
|
+ BeanUtil.copyProperties(item, careOlder);
|
|
|
+ if(careOlder.getId() != null) {//修改
|
|
|
+ careOlder.setModifyTime(new Date());
|
|
|
+ olderIdUpdates.add(careOlder.getId());
|
|
|
+ careOlderService.updateById(careOlder);
|
|
|
+ } else { //新增
|
|
|
+ careOlder.setOrgId(device.getOrgId());
|
|
|
+ careOlder.setStationId(device.getStationId());
|
|
|
+ careOlder.setCreateTime(new Date());
|
|
|
+ careOlderService.save(careOlder);
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ List<Long> contactIdUpdates = new ArrayList<>();
|
|
|
+ orderContactorVOList.forEach(item -> {
|
|
|
+ CareHouseContact careHouseContact = new CareHouseContact();
|
|
|
+ BeanUtil.copyProperties(item, careHouseContact);
|
|
|
+ if(careHouseContact.getId() != null) {//修改
|
|
|
+ careHouseContact.setModifyTime(new Date());
|
|
|
+ contactIdUpdates.add(careHouseContact.getId());
|
|
|
+ careHouseContactService.updateById(careHouseContact);
|
|
|
+ } else { //新增
|
|
|
+ careHouseContact.setOrgId(device.getOrgId());
|
|
|
+ careHouseContact.setStationId(device.getStationId());
|
|
|
+ careHouseContact.setCreateTime(new Date());
|
|
|
+ careHouseContactService.save(careHouseContact);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ //处理删除部分
|
|
|
+ //查询出来数据库里已经有的
|
|
|
+ QueryWrapper<CareHouseOlderRel> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(CareHouseOlderRel::getHouseId,device.getHouseId());
|
|
|
+ List<CareHouseOlderRel> olderRels = careHouseOlderRelService.list(queryWrapper);
|
|
|
+ if(CollUtil.isNotEmpty(olderRels)){
|
|
|
+ List<Long> olderIdDels = new ArrayList<>();
|
|
|
+ List<Long> olderReIdDels = new ArrayList<>();
|
|
|
+ olderRels.forEach(item -> {
|
|
|
+ if(!olderIdUpdates.contains(item.getOlderId())){
|
|
|
+ olderReIdDels.add(item.getId());
|
|
|
+ olderIdDels.add(item.getOlderId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(CollUtil.isNotEmpty(olderIdDels)){
|
|
|
+ this.careHouseOlderRelService.removeByIds(olderReIdDels);
|
|
|
+ this.careOlderService.removeByIds(olderIdDels);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //处理删除部分
|
|
|
+ //查询出来数据库里已经有的
|
|
|
+ QueryWrapper<CareHouseContactRel> queryWrapper2 = new QueryWrapper<>();
|
|
|
+ queryWrapper2.lambda().eq(CareHouseContactRel::getHouseId,device.getHouseId());
|
|
|
+ List<CareHouseContactRel> contactRels = careHouseContactRelService.list(queryWrapper2);
|
|
|
+ if(CollUtil.isNotEmpty(contactRels)){
|
|
|
+ List<Long> contactIdDels = new ArrayList<>();
|
|
|
+ List<Long> contactReIdDels = new ArrayList<>();
|
|
|
+ contactRels.forEach(item -> {
|
|
|
+ if(!contactIdUpdates.contains(item.getContactId())){
|
|
|
+ contactReIdDels.add(item.getId());
|
|
|
+ contactIdDels.add(item.getContactId());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(CollUtil.isNotEmpty(contactIdDels)){
|
|
|
+ this.careHouseContactRelService.removeByIds(contactReIdDels);
|
|
|
+ this.careHouseContactService.removeByIds(contactIdDels);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(DeviceActiveStatusEnum.ACTIVE.getValue().equals(device.getActiveStatus())) {
|
|
|
+ //保存完后,启动通道
|
|
|
+ MqttTool.addOne(device, careDeviceService, bmsEventOrderService, careEventOrderService, careHouseService, careOrganizationService, careStationService);
|
|
|
+ }
|
|
|
}
|
|
|
/**
|
|
|
* 列表查询
|
|
@@ -125,7 +317,8 @@ public class DeviceService {
|
|
|
* @param pageReqVo
|
|
|
* @return
|
|
|
*/
|
|
|
- public IPage<DeviceVO> listDevice(String addr, Long stationId, String status, String devCode,UserLogindConvertVO loginUser, PageReqVO pageReqVo) {
|
|
|
- return this.careDeviceService.queryList(pageReqVo,stationId,status,addr,devCode,loginUser);
|
|
|
+ public IPage<DeviceVO> listDevice(Long stationId, String devCode, String activeStatus, String chambName,String status,String houseAddr,Long chambId,Long memberId,UserLogindConvertVO loginUser, PageReqVO pageReqVo) {
|
|
|
+ return this.careDeviceService.queryList(pageReqVo,stationId,devCode,activeStatus,chambName,status,houseAddr,chambId,memberId,loginUser);
|
|
|
}
|
|
|
+
|
|
|
}
|