package com.isu.gaswellwatch.service.impl; import cn.hutool.core.collection.CollectionUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.isu.gaswellwatch.config.SnowflakeConfig; import com.isu.gaswellwatch.dao.DeviceDao; import com.isu.gaswellwatch.dto.DeviceCreateDTO; import com.isu.gaswellwatch.dto.DeviceEditDTO; import com.isu.gaswellwatch.entity.*; import com.isu.gaswellwatch.exception.BusinessException; import com.isu.gaswellwatch.modbus.data.PersistenceHandler; import com.isu.gaswellwatch.modbus.data.Redis2DBPersistenceService; import com.isu.gaswellwatch.modbus.data.listener.DynamicRabbitListener; import com.isu.gaswellwatch.service.*; import com.isu.gaswellwatch.utils.ConverterUtil; import com.isu.gaswellwatch.vo.DeviceHistoryVO; import com.isu.gaswellwatch.vo.DeviceVO; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.data.redis.RedisConnectionFailureException; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Objects; @Service("deviceService") @Transactional(rollbackFor = Exception.class) public class DeviceServiceImpl extends ServiceImpl implements DeviceService { @Resource private SnowflakeConfig snowflakeConfig; @Resource private DeviceDao deviceDao; @Resource private GasWellService gasWellService; @Resource(name = "stringRedisTemplate") private RedisTemplate redisTemplate; @Resource private DictionaryService dictionaryService; @Resource private DeviceOptLogService deviceOptLogService; @Resource private DynamicRabbitListener dynamicRabbitListener; @Override public Page page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName,Long deviceTypeId){ Page page = deviceDao.page(new Page<>(currentPage, pageSize),gasWellName,gasStationName,deviceTypeId); List deviceVOList = page.getRecords(); // 从Redis获取设备运行数据 if(CollectionUtil.isNotEmpty(deviceVOList)) { Map runModeMap = dictionaryService.getValueMapByType("runMode"); Map plugStatusMap = dictionaryService.getValueMapByType("plugStatus"); try { for (DeviceVO deviceVO : deviceVOList) { String deviceKey = PersistenceHandler.DEVICE_DATA_CACHE + deviceVO.getId(); Object casPressure = redisTemplate.opsForHash().get(deviceKey, "casPressure"); deviceVO.setCasPressure(casPressure == null ? "" : casPressure.toString()); Object oilPressure = redisTemplate.opsForHash().get(deviceKey, "oilPressure"); deviceVO.setOilPressure(oilPressure == null ? "" : oilPressure.toString()); Object prePressure = redisTemplate.opsForHash().get(deviceKey, "prePressure"); deviceVO.setPrePressure(prePressure == null ? "" : prePressure.toString()); Object online = redisTemplate.opsForHash().get(deviceKey, "online"); deviceVO.setOnline(online == null ? "" : online.toString()); Object gas_status = redisTemplate.opsForHash().get(deviceKey, "gas_status"); deviceVO.setWellCtl(gas_status == null ? "" : gas_status.toString()); Object runMode = redisTemplate.opsForHash().get(deviceKey, "runMode"); if(runMode==null) { deviceVO.setRunMode(""); }else { Dictionary runMode1 = runModeMap.get(runMode.toString()); deviceVO.setRunMode(runMode1==null?"":runMode1.getName()); } Object plugStatus = redisTemplate.opsForHash().get(deviceKey, "plugStatus"); if(plugStatus==null) { deviceVO.setPlugStatus(""); }else { Dictionary plugStatus1 = plugStatusMap.get(plugStatus.toString()); deviceVO.setPlugStatus(plugStatus1==null?"":plugStatus1.getName()); } } }catch (RedisConnectionFailureException e){ log.error("redis连接失败,请检查redis连接"); } } return ConverterUtil.convertPage(page, DeviceVO.class); } @Override public void add(DeviceCreateDTO deviceCreateDTO) throws NumberFormatException{ //查重 List list = list(new LambdaQueryWrapper().eq(Device::getId, deviceCreateDTO.getCode())); if(CollectionUtil.isNotEmpty(list)) { throw new BusinessException("已有相同设备编码,请重新输入"); } GasWell gasWell = gasWellService.getOne(new LambdaQueryWrapper().eq(GasWell::getId, deviceCreateDTO.getGasWell())); if(gasWell==null) { throw new BusinessException("该气井不存在"); } if(gasWell.getDeviceId()!=null) { throw new BusinessException("该气井已存在绑定设备,无法添加新设备"); } Device device = ConverterUtil.convert(deviceCreateDTO, Device.class); //code必须为整形,用于在缓存中查询设备的上报数据 device.setId(Long.valueOf(deviceCreateDTO.getCode())); save(device); //在气井中绑定设备 gasWellService.bindDevice(deviceCreateDTO.getGasWell(),device.getId()); //创建该设备在mq中的事件队列 dynamicRabbitListener.registerDeviceAndListener(device.getId()); } @Override public void edit(DeviceEditDTO deviceEditDTO){ updateById(ConverterUtil.convert(deviceEditDTO, Device.class)); } @Override public void delete(Long id){ Long gasWellId = getDevice(id).getGasWell().getId(); //删除设备 removeById(id); //解绑气井 gasWellService.unbindDevice(gasWellId); } @Override public DeviceVO getDevice(Long id) { return deviceDao.getDeviceById(id); } @Override public Map getDeviceControlData(Long deviceId) { return redisTemplate.opsForHash().entries(PersistenceHandler.DEVICE_DATA_CACHE + deviceId); } @Override public Page getDeviceLogData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException { Date start = null; Date end = null; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if(!StringUtils.isEmpty(startTime) ) { start = simpleDateFormat.parse(startTime); } if(!StringUtils.isEmpty(endTime)) { end = simpleDateFormat.parse(endTime); } return deviceOptLogService.page(new Page<>(currentPage, pageSize),start,end,deviceId); } @Override public List getDeviceVOByIds(List deviceIdList) { return deviceDao.getDeviceVOByIds(deviceIdList); } @Override public Page getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException { Date start = null; Date end = null; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if(!StringUtils.isEmpty(startTime) ) { start = simpleDateFormat.parse(startTime); } if(!StringUtils.isEmpty(endTime)) { end = simpleDateFormat.parse(endTime); } String tableName = Redis2DBPersistenceService.DEFAULT_DATA_TABLE +deviceId; Page page = deviceDao.historyPage(new Page<>(currentPage, pageSize),start,end,deviceId,tableName); List deviceHistoryVO = page.getRecords(); if(CollectionUtil.isNotEmpty(deviceHistoryVO)) { Map runModeMap = dictionaryService.getValueMapByType("runMode"); Map plugStatusMap = dictionaryService.getValueMapByType("plugStatus"); for (DeviceHistoryVO deviceVO : deviceHistoryVO) { deviceVO.setRunMode(StringUtils.isEmpty(deviceVO.getRunMode()) ?"":runModeMap.get(deviceVO.getRunMode()).getName()); deviceVO.setPlugStatus(StringUtils.isEmpty(deviceVO.getPlugStatus()) ?"":plugStatusMap.get(deviceVO.getPlugStatus()).getName()); } } return page; } }