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

209 lines
9.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.Device;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import com.isu.gaswellwatch.entity.Dictionary;
import com.isu.gaswellwatch.entity.GasWell;
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.DeviceOptLogService;
import com.isu.gaswellwatch.service.DeviceService;
import com.isu.gaswellwatch.service.DictionaryService;
import com.isu.gaswellwatch.service.GasWellService;
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;
@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;
@Resource
private DeviceOptLogService deviceOptLogService;
@Resource
private DynamicRabbitListener dynamicRabbitListener;
@Override
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获取设备运行数据
if (CollectionUtil.isNotEmpty(deviceVOList)) {
Map<String, Dictionary> runModeMap = this.dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> plugStatusMap = this.dictionaryService.getValueMapByType("plugStatus");
try {
for (DeviceVO deviceVO : deviceVOList) {
String deviceKey = PersistenceHandler.DEVICE_DATA_CACHE + deviceVO.getId();
Object casPressure = this.redisTemplate.opsForHash().get(deviceKey, "casPressure");
deviceVO.setCasPressure(casPressure == null ? "" : casPressure.toString());
Object oilPressure = this.redisTemplate.opsForHash().get(deviceKey, "oilPressure");
deviceVO.setOilPressure(oilPressure == null ? "" : oilPressure.toString());
Object prePressure = this.redisTemplate.opsForHash().get(deviceKey, "prePressure");
deviceVO.setPrePressure(prePressure == null ? "" : prePressure.toString());
Object online = this.redisTemplate.opsForHash().get(deviceKey, "online");
deviceVO.setOnline(online == null ? "" : online.toString());
Object gas_status = this.redisTemplate.opsForHash().get(deviceKey, "gas_status");
deviceVO.setWellCtl(gas_status == null ? "" : gas_status.toString());
Object runMode = this.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 = this.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) {
this.log.error("redis连接失败请检查redis连接");
}
}
return ConverterUtil.convertPage(page, DeviceVO.class);
}
@Override
public void add(DeviceCreateDTO deviceCreateDTO) throws NumberFormatException {
//查重
List<Device> list = this.list(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceCreateDTO.getCode()));
if (CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("已有相同设备编码,请重新输入");
}
GasWell gasWell = this.gasWellService.getOne(new LambdaQueryWrapper<GasWell>().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()));
this.save(device);
//在气井中绑定设备
this.gasWellService.bindDevice(deviceCreateDTO.getGasWell(), device.getId());
}
@Override
public void edit(DeviceEditDTO deviceEditDTO) {
this.updateById(ConverterUtil.convert(deviceEditDTO, Device.class));
}
@Override
public void delete(Long id) {
Long gasWellId = this.getDevice(id).getGasWell().getId();
//删除设备
this.removeById(id);
//解绑气井
this.gasWellService.unbindDevice(gasWellId);
}
@Override
public DeviceVO getDevice(Long id) {
return this.deviceDao.getDeviceById(id);
}
@Override
public Map<String, String> getDeviceControlData(Long deviceId) {
return this.redisTemplate.opsForHash().entries(PersistenceHandler.DEVICE_DATA_CACHE + deviceId);
}
@Override
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");
if (!StringUtils.isEmpty(startTime)) {
start = simpleDateFormat.parse(startTime);
}
if (!StringUtils.isEmpty(endTime)) {
end = simpleDateFormat.parse(endTime);
}
return this.deviceOptLogService.page(new Page<>(currentPage, pageSize), start, end, deviceId);
}
@Override
public List<DeviceVO> getDeviceVOByIds(List<Long> deviceIdList) {
return this.deviceDao.getDeviceVOByIds(deviceIdList);
}
@Override
public Page<DeviceHistoryVO> 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<DeviceHistoryVO> page = this.deviceDao.historyPage(new Page<>(currentPage, pageSize), start, end, deviceId, tableName);
List<DeviceHistoryVO> deviceHistoryVO = page.getRecords();
if (CollectionUtil.isNotEmpty(deviceHistoryVO)) {
Map<String, Dictionary> runModeMap = this.dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> plugStatusMap = this.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;
}
}