gasWellWatch/src/main/java/com/isu/gaswellwatch/service/impl/DeviceServiceImpl.java

223 lines
9.7 KiB
Java
Raw Normal View History

2024-11-25 01:04:53 +08:00
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;
2024-11-28 23:35:05 +08:00
import com.isu.gaswellwatch.entity.Device;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import com.isu.gaswellwatch.entity.Dictionary;
import com.isu.gaswellwatch.entity.GasWell;
2024-11-25 01:04:53 +08:00
import com.isu.gaswellwatch.exception.BusinessException;
2024-12-09 16:05:45 +08:00
import com.isu.gaswellwatch.modbus.data.CacheService;
import com.isu.gaswellwatch.modbus.data.PersistenceHandler;
2024-11-26 18:46:34 +08:00
import com.isu.gaswellwatch.modbus.data.Redis2DBPersistenceService;
2024-11-28 01:30:00 +08:00
import com.isu.gaswellwatch.modbus.data.listener.DynamicRabbitListener;
2024-11-28 23:35:05 +08:00
import com.isu.gaswellwatch.service.DeviceOptLogService;
import com.isu.gaswellwatch.service.DeviceService;
import com.isu.gaswellwatch.service.DictionaryService;
import com.isu.gaswellwatch.service.GasWellService;
2024-11-25 01:04:53 +08:00
import com.isu.gaswellwatch.utils.ConverterUtil;
import com.isu.gaswellwatch.vo.DeviceHistoryVO;
2024-11-25 15:48:00 +08:00
import com.isu.gaswellwatch.vo.DeviceVO;
2024-11-25 01:04:53 +08:00
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
2024-11-28 01:30:00 +08:00
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.RedisTemplate;
2024-11-25 01:04:53 +08:00
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
2024-11-25 01:04:53 +08:00
import java.util.List;
import java.util.Map;
2024-11-25 01:04:53 +08:00
@Service("deviceService")
@Transactional(rollbackFor = Exception.class)
public class DeviceServiceImpl extends ServiceImpl<DeviceDao, Device> implements DeviceService {
@Resource
private SnowflakeConfig snowflakeConfig;
@Resource
private DeviceDao deviceDao;
@Resource
private GasWellService gasWellService;
@Resource(name = "stringRedisTemplate")
private RedisTemplate redisTemplate;
@Resource
private DictionaryService dictionaryService;
2024-11-26 18:46:34 +08:00
@Resource
private DeviceOptLogService deviceOptLogService;
2024-11-28 01:30:00 +08:00
@Resource
private DynamicRabbitListener dynamicRabbitListener;
2024-12-09 16:05:45 +08:00
@Resource
private CacheService cacheService;
2024-11-25 01:04:53 +08:00
@Override
2024-11-28 23:35:05 +08:00
public Page<DeviceVO> page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName, Long deviceTypeId, Long blockId) {
Page<DeviceVO> page = this.deviceDao.page(new Page<>(currentPage, pageSize), gasWellName, gasStationName, deviceTypeId, blockId);
List<DeviceVO> deviceVOList = page.getRecords();
// 从Redis获取设备运行数据
2024-11-28 23:35:05 +08:00
if (CollectionUtil.isNotEmpty(deviceVOList)) {
Map<String, Dictionary> runModeMap = this.dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> plugStatusMap = this.dictionaryService.getValueMapByType("plugStatus");
2024-11-28 01:30:00 +08:00
try {
for (DeviceVO deviceVO : deviceVOList) {
String deviceKey = PersistenceHandler.DEVICE_DATA_CACHE + deviceVO.getId();
2024-11-28 23:35:05 +08:00
Object casPressure = this.redisTemplate.opsForHash().get(deviceKey, "casPressure");
2024-11-28 01:30:00 +08:00
deviceVO.setCasPressure(casPressure == null ? "" : casPressure.toString());
2024-11-28 23:35:05 +08:00
Object oilPressure = this.redisTemplate.opsForHash().get(deviceKey, "oilPressure");
2024-11-28 01:30:00 +08:00
deviceVO.setOilPressure(oilPressure == null ? "" : oilPressure.toString());
2024-11-28 23:35:05 +08:00
Object prePressure = this.redisTemplate.opsForHash().get(deviceKey, "prePressure");
2024-11-28 01:30:00 +08:00
deviceVO.setPrePressure(prePressure == null ? "" : prePressure.toString());
2024-11-28 23:35:05 +08:00
Object online = this.redisTemplate.opsForHash().get(deviceKey, "online");
2024-11-28 01:30:00 +08:00
deviceVO.setOnline(online == null ? "" : online.toString());
2024-12-06 16:11:15 +08:00
Object gas_status = this.redisTemplate.opsForHash().get(deviceKey, "wellStatus");
2024-12-08 21:18:38 +08:00
deviceVO.setWellStatus(gas_status == null ? "" : gas_status.toString());
2024-11-28 01:30:00 +08:00
2024-11-28 23:35:05 +08:00
Object runMode = this.redisTemplate.opsForHash().get(deviceKey, "runMode");
if (runMode == null) {
2024-11-28 01:30:00 +08:00
deviceVO.setRunMode("");
2024-11-28 23:35:05 +08:00
} else {
2024-11-28 01:30:00 +08:00
Dictionary runMode1 = runModeMap.get(runMode.toString());
2024-11-28 23:35:05 +08:00
deviceVO.setRunMode(runMode1 == null ? "" : runMode1.getName());
2024-11-28 01:30:00 +08:00
}
2024-11-28 23:35:05 +08:00
Object plugStatus = this.redisTemplate.opsForHash().get(deviceKey, "plugStatus");
if (plugStatus == null) {
2024-11-28 01:30:00 +08:00
deviceVO.setPlugStatus("");
2024-11-28 23:35:05 +08:00
} else {
2024-11-28 01:30:00 +08:00
Dictionary plugStatus1 = plugStatusMap.get(plugStatus.toString());
2024-11-28 23:35:05 +08:00
deviceVO.setPlugStatus(plugStatus1 == null ? "" : plugStatus1.getName());
2024-11-28 01:30:00 +08:00
}
2024-11-26 22:51:15 +08:00
}
2024-11-28 23:35:05 +08:00
} catch (RedisConnectionFailureException e) {
this.log.error("redis连接失败请检查redis连接");
}
2024-11-28 01:30:00 +08:00
}
2024-11-25 15:48:00 +08:00
return ConverterUtil.convertPage(page, DeviceVO.class);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 23:35:05 +08:00
public void add(DeviceCreateDTO deviceCreateDTO) throws NumberFormatException {
2024-11-25 01:04:53 +08:00
//查重
2024-11-28 23:35:05 +08:00
List<Device> list = this.list(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceCreateDTO.getCode()));
if (CollectionUtil.isNotEmpty(list)) {
2024-11-25 01:04:53 +08:00
throw new BusinessException("已有相同设备编码,请重新输入");
}
2024-11-25 16:29:26 +08:00
2024-11-28 23:35:05 +08:00
GasWell gasWell = this.gasWellService.getOne(new LambdaQueryWrapper<GasWell>().eq(GasWell::getId, deviceCreateDTO.getGasWell()));
if (gasWell == null) {
2024-11-25 16:29:26 +08:00
throw new BusinessException("该气井不存在");
}
2024-11-28 23:35:05 +08:00
if (gasWell.getDeviceId() != null) {
2024-11-25 16:29:26 +08:00
throw new BusinessException("该气井已存在绑定设备,无法添加新设备");
}
2024-11-25 01:04:53 +08:00
Device device = ConverterUtil.convert(deviceCreateDTO, Device.class);
//code必须为整形用于在缓存中查询设备的上报数据
device.setId(Long.valueOf(deviceCreateDTO.getCode()));
2024-11-28 23:35:05 +08:00
this.save(device);
2024-11-25 01:04:53 +08:00
//在气井中绑定设备
2024-11-28 23:35:05 +08:00
this.gasWellService.bindDevice(deviceCreateDTO.getGasWell(), device.getId());
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 23:35:05 +08:00
public void edit(DeviceEditDTO deviceEditDTO) {
this.updateById(ConverterUtil.convert(deviceEditDTO, Device.class));
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 23:35:05 +08:00
public void delete(Long id) {
Long gasWellId = this.getDevice(id).getGasWell().getId();
2024-11-25 01:04:53 +08:00
//删除设备
2024-11-28 23:35:05 +08:00
this.removeById(id);
2024-11-28 01:30:00 +08:00
//解绑气井
2024-11-28 23:35:05 +08:00
this.gasWellService.unbindDevice(gasWellId);
2024-12-09 16:05:45 +08:00
//删除t_data_设备历史数据表 删除缓存和MQ
cacheService.cleanDeviceCache(id);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-25 15:48:00 +08:00
public DeviceVO getDevice(Long id) {
2024-11-28 23:35:05 +08:00
return this.deviceDao.getDeviceById(id);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 23:35:05 +08:00
public Map<String, String> getDeviceControlData(Long deviceId) {
return this.redisTemplate.opsForHash().entries(PersistenceHandler.DEVICE_DATA_CACHE + deviceId);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-26 18:46:34 +08:00
public Page<DeviceOptLog> 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");
2024-11-28 23:35:05 +08:00
if (!StringUtils.isEmpty(startTime)) {
2024-11-26 18:46:34 +08:00
start = simpleDateFormat.parse(startTime);
}
2024-11-28 23:35:05 +08:00
if (!StringUtils.isEmpty(endTime)) {
2024-11-26 18:46:34 +08:00
end = simpleDateFormat.parse(endTime);
}
2024-11-28 23:35:05 +08:00
return this.deviceOptLogService.page(new Page<>(currentPage, pageSize), start, end, deviceId);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-28 01:30:00 +08:00
public List<DeviceVO> getDeviceVOByIds(List<Long> deviceIdList) {
2024-11-28 23:35:05 +08:00
return this.deviceDao.getDeviceVOByIds(deviceIdList);
}
2024-11-28 01:30:00 +08:00
@Override
public Page<DeviceHistoryVO> getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException {
Date start = null;
Date end = null;
2024-11-26 18:46:34 +08:00
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2024-11-28 23:35:05 +08:00
if (!StringUtils.isEmpty(startTime)) {
start = simpleDateFormat.parse(startTime);
}
2024-11-28 23:35:05 +08:00
if (!StringUtils.isEmpty(endTime)) {
end = simpleDateFormat.parse(endTime);
}
2024-11-28 23:35:05 +08:00
String tableName = Redis2DBPersistenceService.DEFAULT_DATA_TABLE + deviceId;
2024-12-08 21:18:38 +08:00
//判断设备品牌
DeviceVO device = this.getDevice(deviceId);
Page<DeviceHistoryVO> page = this.deviceDao.historyPage(new Page<>(currentPage, pageSize), start, end, deviceId, tableName,device.getProduct().getCode());
List<DeviceHistoryVO> deviceHistoryVO = page.getRecords();
2024-11-28 23:35:05 +08:00
if (CollectionUtil.isNotEmpty(deviceHistoryVO)) {
Map<String, Dictionary> runModeMap = this.dictionaryService.getValueMapByType("runMode");
2024-12-09 21:01:54 +08:00
if(PersistenceHandler.ETC_MODBUS_TYPE.equalsIgnoreCase(device.getProduct().getCode())){
runModeMap = this.dictionaryService.getValueMapByType("controlMode");
}
2024-11-28 23:35:05 +08:00
Map<String, Dictionary> plugStatusMap = this.dictionaryService.getValueMapByType("plugStatus");
for (DeviceHistoryVO deviceVO : deviceHistoryVO) {
2024-11-28 23:35:05 +08:00
deviceVO.setRunMode(StringUtils.isEmpty(deviceVO.getRunMode()) ? "" : runModeMap.get(deviceVO.getRunMode()).getName());
deviceVO.setPlugStatus(StringUtils.isEmpty(deviceVO.getPlugStatus()) ? "" : plugStatusMap.get(deviceVO.getPlugStatus()).getName());
}
}
return page;
2024-11-25 01:04:53 +08:00
}
2024-11-26 18:46:34 +08:00
2024-11-25 01:04:53 +08:00
}