设备历史数据接口

This commit is contained in:
qinjie 2024-11-26 18:46:34 +08:00
parent 8a8b9c206b
commit e8b61641de
10 changed files with 219 additions and 27 deletions

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isu.gaswellwatch.annotation.OperationLog;
import com.isu.gaswellwatch.dto.DeviceCreateDTO;
import com.isu.gaswellwatch.dto.DeviceEditDTO;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import com.isu.gaswellwatch.entity.Response;
import com.isu.gaswellwatch.enums.LogType;
import com.isu.gaswellwatch.exception.BusinessException;
@ -15,8 +16,6 @@ import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
@ -95,7 +94,7 @@ public class DeviceController {
*/
@PostMapping("/saveDeviceControlData")
public Response<String> saveDeviceControlData(@RequestBody Map<String,String> controlData,@RequestParam Long deviceId) {
deviceService.saveDeviceControlData(controlData);
deviceService.saveDeviceControlData(controlData,deviceId);
return Response.succeed();
}
@ -119,8 +118,27 @@ public class DeviceController {
* 获取设备日志数据
*/
@GetMapping("/getDeviceLogData")
public Response<String> getDeviceLogData(@RequestParam Long id) {
deviceService.getDeviceLogData(id);
public Response<Page<DeviceOptLog>> getDeviceLogData(@RequestParam(defaultValue = "1") Integer currentPage,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false)String startTime,
@RequestParam (required = false)String endTime,
@RequestParam Long deviceId) {
try {
return Response.succeed(deviceService.getDeviceLogData(currentPage,pageSize,startTime,endTime,deviceId));
} catch (ParseException e) {
throw new BusinessException("日期格式错误");
}
}
/**
* 控制开/关井
*
* @Param isOpen 1 开井 0 关井
* @Param deviceId 设备id
*/
@GetMapping("/wellCtl")
public Response<String> wellCtl(@RequestParam Integer isOpen, @RequestParam Long deviceId) {
deviceService.wellCtl(isOpen,deviceId);
return Response.succeed();
}

View File

@ -3,6 +3,7 @@ package com.isu.gaswellwatch.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isu.gaswellwatch.entity.Device;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import com.isu.gaswellwatch.vo.DeviceHistoryVO;
import com.isu.gaswellwatch.vo.DeviceVO;
import org.apache.ibatis.annotations.Mapper;
@ -28,5 +29,10 @@ public interface DeviceDao extends BaseMapper<Device> {
@Param("endTime")Date endTime,
@Param("deviceId") Long deviceId,
@Param("tableName") String tableName);
Page<DeviceOptLog> deviceOptLogPage(Page<DeviceOptLog> objectPage,
@Param("startTime")Date startTime,
@Param("endTime")Date endTime,
@Param("deviceId") Long deviceId);
}

View File

@ -0,0 +1,14 @@
package com.isu.gaswellwatch.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import com.isu.gaswellwatch.entity.UserDepartment;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface DeviceOptLogDao extends BaseMapper<DeviceOptLog> {
}

View File

@ -0,0 +1,42 @@
package com.isu.gaswellwatch.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class DeviceOptLog extends Model<DeviceOptLog> {
private Long id;
//设备id
private Long deviceId;
//账号
private String username;
//姓名
private String name;
//所属区块
private String block;
//所属区块
private String gasWell;
//设备详情
private String deviceDetail;
//集气站
private String gasStation;
//日志内容
private String content;
//创建时间
private String createTime;
//更新时间
private String updateTime;
}

View File

@ -38,7 +38,7 @@ public class Redis2DBPersistenceService {
public static final String DEFAULT_DATA_TABLE = "t_device_data_";
private static final String DEVICE_INFO_SQL = "SELECT d.*, dp.code as modbus_device_product_code from device d "
+ "join dictionary dp on dp.id = d.product where id = ";
+ "join dictionary dp on dp.id = d.product where d.id = ";
private static final String EXISTS_TABLE_SQL = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA"
+ " IN ('gaswellwatch', 'gas_well_watch') AND TABLE_NAME='$TableName$'";
@Resource

View File

@ -0,0 +1,15 @@
package com.isu.gaswellwatch.service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import java.util.Date;
public interface DeviceOptLogService extends IService<DeviceOptLog> {
void saveGasWellOptLog(Integer isOpen, Long deviceId);
Page<DeviceOptLog> page(Page<DeviceOptLog> page, Date start, Date end, Long deviceId);
}

View File

@ -5,11 +5,11 @@ import com.baomidou.mybatisplus.extension.service.IService;
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.vo.DeviceHistoryVO;
import com.isu.gaswellwatch.vo.DeviceVO;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
public interface DeviceService extends IService<Device> {
@ -26,11 +26,12 @@ public interface DeviceService extends IService<Device> {
Map<String,String> getDeviceControlData(Long deviceId);
void getDeviceLogData(Long id);
void saveDeviceControlData(Map<String, String> controlData);
void saveDeviceControlData(Map<String, String> controlData, Long deviceId);
Page<DeviceHistoryVO> getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException;
void wellCtl(Integer isOpen, Long deviceId);
Page<DeviceOptLog> getDeviceLogData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException;
}

View File

@ -0,0 +1,60 @@
package com.isu.gaswellwatch.service.impl;
import cn.dev33.satoken.stp.StpUtil;
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.constants.UserConstant;
import com.isu.gaswellwatch.dao.DeviceDao;
import com.isu.gaswellwatch.dao.DeviceOptLogDao;
import com.isu.gaswellwatch.entity.DeviceOptLog;
import com.isu.gaswellwatch.service.*;
import com.isu.gaswellwatch.vo.BlockVO;
import com.isu.gaswellwatch.vo.DeviceVO;
import com.isu.gaswellwatch.vo.UserLoginInfoVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
@Service("deviceOptLogService")
@Transactional(rollbackFor = Exception.class)
public class DeviceOptLogServiceImpl extends ServiceImpl<DeviceOptLogDao, DeviceOptLog> implements DeviceOptLogService {
@Resource
private DeviceOptLogDao deviceOptLogDao;
@Resource
private UserService userService;
@Resource
private DeviceDao deviceDao;
@Resource
private BlockService blockService;
@Resource
private SnowflakeConfig snowflakeConfig;
@Override
public void saveGasWellOptLog(Integer isOpen, Long deviceId) {
UserLoginInfoVO userLoginInfoVO = (UserLoginInfoVO) StpUtil.getTokenSession().get(UserConstant.TOKEN_SESSION);
DeviceVO deviceVO = deviceDao.getDeviceById(deviceId);
DeviceOptLog deviceOptLog = new DeviceOptLog();
deviceOptLog.setId(snowflakeConfig.snowflakeId());
deviceOptLog.setDeviceId(deviceId);
deviceOptLog.setUsername(userLoginInfoVO.getUserVO().getUsername());
deviceOptLog.setName(userLoginInfoVO.getUserVO().getNickname());
BlockVO blockVO = blockService.selectBlockById(deviceVO.getGasWell().getBlockId());
deviceOptLog.setBlock("苏里格气田-"+blockVO.getName());
deviceOptLog.setGasWell(deviceVO.getGasWell().getName());
deviceOptLog.setDeviceDetail(deviceVO.getDeviceType().getName()+"-"+deviceVO.getProduct().getName());
deviceOptLog.setGasStation(deviceVO.getGasStation());
String content = isOpen==null?"设置成功":(isOpen==1?"开井成功":"关井成功");
deviceOptLog.setContent(content);
deviceOptLogDao.insert(deviceOptLog);
}
@Override
public Page<DeviceOptLog> page(Page<DeviceOptLog> page, Date start, Date end, Long deviceId) {
return deviceDao.deviceOptLogPage(page, start, end, deviceId);
}
}

View File

@ -11,6 +11,7 @@ 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.service.*;
import com.isu.gaswellwatch.utils.ConverterUtil;
import com.isu.gaswellwatch.vo.DeviceHistoryVO;
@ -44,6 +45,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceDao, Device> implements
private RedisTemplate redisTemplate;
@Resource
private DictionaryService dictionaryService;
@Resource
private DeviceOptLogService deviceOptLogService;
@Override
public Page<DeviceVO> page(Integer currentPage, Integer pageSize, String gasWellName, String gasStationName,Long deviceTypeId){
@ -119,29 +122,41 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceDao, Device> implements
}
@Override
public void getDeviceLogData(Long id) {
}
@Override
public void saveDeviceControlData(Map<String, String> controlData) {
//TODO 等待封装控制指令
}
@Override
public Page<DeviceHistoryVO> getDeviceHistoryData(Integer currentPage, Integer pageSize, String startTime, String endTime, Long deviceId) throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
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);
}
String tableName = "t_device_data_"+deviceId;
return deviceOptLogService.page(new Page<>(currentPage, pageSize),start,end,deviceId);
}
@Override
public void saveDeviceControlData(Map<String, String> controlData, Long deviceId) {
//TODO 等待封装控制指令
//记录用户保存控制指令日志
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;
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 = deviceDao.historyPage(new Page<>(currentPage, pageSize),start,end,deviceId,tableName);
List<DeviceHistoryVO> deviceHistoryVO = page.getRecords();
if(CollectionUtil.isNotEmpty(deviceHistoryVO)) {
@ -156,6 +171,15 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceDao, Device> implements
return page;
}
@Override
public void wellCtl(Integer isOpen, Long deviceId) {
//TODO 待封装控制指令
//记录用户操作开关井日志
deviceOptLogService.saveGasWellOptLog(isOpen,deviceId);
}
}

View File

@ -17,6 +17,7 @@
<association property="gasWell" javaType="com.isu.gaswellwatch.entity.GasWell" >
<id column="gasWellId" property="id"/>
<result column="gasWellName" property="name"/>
<result column="gasWellBlock" property="blockId"/>
</association>
</resultMap>
@ -59,7 +60,7 @@
<select id="getDeviceById" resultMap="DeviceVOMap">
select u.id, u.device_type, u.code, u.gas_station, u.product, u.gateway_sn,
u.gas_well, u.details, u.create_time, u.update_time,g.name as gasWellName,g.id as gasWellId
u.gas_well, u.details, u.create_time, u.update_time,g.name as gasWellName,g.id as gasWellId,g.block_id as gasWellBlock
from device u left join gas_well g on u.gas_well = g.id
where u.id = #{id}
</select>
@ -77,5 +78,16 @@
order by t.collection_time desc
</select>
<select id="deviceOptLogPage" resultType="com.isu.gaswellwatch.entity.DeviceOptLog">
select t.* from device_opt_log t where t.device_id = #{deviceId}
<if test="startTime!=null">
and t.create_time &gt;= #{startTime}
</if>
<if test="endTime!=null">
and t.create_time &lt;= #{endTime}
</if>
order by t.create_time desc
</select>
</mapper>