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

186 lines
7.9 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;
import com.isu.gaswellwatch.entity.*;
2024-11-25 01:04:53 +08:00
import com.isu.gaswellwatch.exception.BusinessException;
import com.isu.gaswellwatch.modbus.data.PersistenceHandler;
2024-11-26 18:46:34 +08:00
import com.isu.gaswellwatch.modbus.data.Redis2DBPersistenceService;
import com.isu.gaswellwatch.service.*;
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;
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;
import java.util.Objects;
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-25 01:04:53 +08:00
@Override
2024-11-25 15:48:00 +08:00
public Page<DeviceVO> page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName,Long deviceTypeId){
Page<DeviceVO> page = deviceDao.page(new Page<>(currentPage, pageSize),gasWellName,gasStationName,deviceTypeId);
List<DeviceVO> deviceVOList = page.getRecords();
// 从Redis获取设备运行数据
if(CollectionUtil.isNotEmpty(deviceVOList)) {
Map<String, Dictionary> runModeMap = dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> plugStatusMap = dictionaryService.getValueMapByType("plugStatus");
for (DeviceVO deviceVO : deviceVOList) {
String deviceKey = PersistenceHandler.DEVICE_DATA_CACHE + deviceVO.getId();
deviceVO.setCasPressure(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "casPressure")).toString());
deviceVO.setOilPressure(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "oilPressure")).toString());
deviceVO.setPrePressure(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "prePressure")).toString());
deviceVO.setOnline(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "online")).toString());
Dictionary runMode = runModeMap.get(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "runMode")).toString());
deviceVO.setRunMode(runMode==null?"":runMode.getName());
Dictionary plugStatus = plugStatusMap.get(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "plugStatus")).toString());
deviceVO.setPlugStatus(plugStatus==null?"":plugStatus.getName());
deviceVO.setWellCtl(Objects.requireNonNull(redisTemplate.opsForHash().get(deviceKey, "gas_status")).toString());
}
}
2024-11-25 15:48:00 +08:00
return ConverterUtil.convertPage(page, DeviceVO.class);
2024-11-25 01:04:53 +08:00
}
@Override
public void add(DeviceCreateDTO deviceCreateDTO){
//查重
List<Device> list = list(new LambdaQueryWrapper<Device>().eq(Device::getId, deviceCreateDTO.getCode()));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("已有相同设备编码,请重新输入");
}
2024-11-25 16:29:26 +08:00
GasWell gasWell = gasWellService.getOne(new LambdaQueryWrapper<GasWell>().eq(GasWell::getId, deviceCreateDTO.getGasWell()));
if(gasWell==null) {
throw new BusinessException("该气井不存在");
}
if(gasWell.getDeviceId()!=null) {
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()));
save(device);
//在气井中绑定设备
gasWellService.bindDevice(deviceCreateDTO.getGasWell(),device.getId());
}
@Override
public void edit(DeviceEditDTO deviceEditDTO){
updateById(ConverterUtil.convert(deviceEditDTO, Device.class));
}
@Override
public void delete(Long id){
//删除设备
removeById(id);
//解绑气井
gasWellService.unbindDevice(id);
}
@Override
2024-11-25 15:48:00 +08:00
public DeviceVO getDevice(Long id) {
return deviceDao.getDeviceById(id);
2024-11-25 01:04:53 +08:00
}
@Override
public Map<String,String> getDeviceControlData(Long deviceId) {
return 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");
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);
2024-11-25 01:04:53 +08:00
}
@Override
2024-11-26 18:46:34 +08:00
public void saveDeviceControlData(Map<String, String> controlData, Long deviceId) {
2024-11-25 01:04:53 +08:00
//TODO 等待封装控制指令
2024-11-26 18:46:34 +08:00
//记录用户保存控制指令日志
deviceOptLogService.saveGasWellOptLog(null,deviceId);
}
@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");
if(!StringUtils.isEmpty(startTime) ) {
start = simpleDateFormat.parse(startTime);
}
if(!StringUtils.isEmpty(endTime)) {
end = simpleDateFormat.parse(endTime);
}
2024-11-26 18:46:34 +08:00
String tableName = Redis2DBPersistenceService.DEFAULT_DATA_TABLE +deviceId;
Page<DeviceHistoryVO> page = deviceDao.historyPage(new Page<>(currentPage, pageSize),start,end,deviceId,tableName);
List<DeviceHistoryVO> deviceHistoryVO = page.getRecords();
if(CollectionUtil.isNotEmpty(deviceHistoryVO)) {
Map<String, Dictionary> runModeMap = dictionaryService.getValueMapByType("runMode");
Map<String, Dictionary> 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;
2024-11-25 01:04:53 +08:00
}
2024-11-26 18:46:34 +08:00
@Override
public void wellCtl(Integer isOpen, Long deviceId) {
//TODO 待封装控制指令
//记录用户操作开关井日志
deviceOptLogService.saveGasWellOptLog(isOpen,deviceId);
}
2024-11-25 01:04:53 +08:00
}