增加提醒功能

This commit is contained in:
qinjie 2024-12-10 12:35:37 +08:00
parent d1b78b3277
commit 47fd97f6a9
12 changed files with 396 additions and 11 deletions

View File

@ -0,0 +1,38 @@
package com.isu.gaswellwatch.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isu.gaswellwatch.entity.Response;
import com.isu.gaswellwatch.service.RemindService;
import com.isu.gaswellwatch.service.SummaryService;
import com.isu.gaswellwatch.vo.RemindRecordVO;
import com.isu.gaswellwatch.vo.summary.PieSummaryVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("remind")
public class RemindController {
@Resource
private RemindService remindService;
@GetMapping("/page")
public Response<Page<RemindRecordVO>> page(@RequestParam(defaultValue = "1") Integer currentPage,
@RequestParam(defaultValue = "10") Integer pageSize,
@RequestParam(required = false) Long deviceId,
@RequestParam(required = false) String startTime,
@RequestParam(required = false) String endTime,
@RequestParam(required = false) String gasWellName) {
return Response.succeed(this.remindService.page(currentPage, pageSize, deviceId, startTime, endTime,gasWellName));
}
}

View File

@ -0,0 +1,27 @@
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.RemindRecord;
import com.isu.gaswellwatch.entity.RemindSetting;
import com.isu.gaswellwatch.entity.RoleMenu;
import com.isu.gaswellwatch.vo.RemindRecordVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface RemindDao extends BaseMapper<RemindRecord> {
List<RemindSetting> getAllRemindSetting();
Page<RemindRecordVO> page(Page<Object> page,
@Param("deviceId")Long deviceId,
@Param("startTime")String startTime,
@Param("endTime")String endTime,
@Param("gasWellName")String gasWellName);
}

View File

@ -0,0 +1,38 @@
package com.isu.gaswellwatch.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class RemindRecord extends Model<RemindRecord> {
/** id */
private Long id;
/** 设备ID */
private Long deviceId;
/** 所属气井 */
private Long gasWell;
/** 提醒内容 */
private String content;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
}

View File

@ -0,0 +1,44 @@
package com.isu.gaswellwatch.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
@Data
@Builder
public class RemindSetting extends Model<RemindSetting> {
/* id */
private Long id;
/* 设备ID */
private Long deviceId;
/* 字段 */
private String field;
/* 字段名称 */
private String fieldName;
/* 字段类型 */
private String fieldType;
/* 字段最小值 */
private String minValue;
/* 字段最大值 */
private String maxValue;
/* 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
/* 更新时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date updateTime;
}

View File

@ -2,16 +2,43 @@ package com.isu.gaswellwatch.modbus.data.listener;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.isu.gaswellwatch.config.SnowflakeConfig;
import com.isu.gaswellwatch.entity.RemindRecord;
import com.isu.gaswellwatch.entity.RemindSetting;
import com.isu.gaswellwatch.modbus.data.PersistenceHandler;
import com.isu.gaswellwatch.service.DeviceOptLogService;
import com.isu.gaswellwatch.service.DeviceService;
import com.isu.gaswellwatch.service.RemindService;
import com.isu.gaswellwatch.vo.DeviceVO;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.BatchMessageListener;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
public class BusinessMessageHandlerListener implements BatchMessageListener {
@Resource
private DeviceService deviceService;
@Resource
private DeviceOptLogService deviceOptLogService;
@Resource
private RemindService remindService;
@Resource
private SnowflakeConfig snowflakeConfig;
@Override
public void onMessage(Message message) {
this.onMessageBatch(List.of(message));
@ -21,33 +48,105 @@ public class BusinessMessageHandlerListener implements BatchMessageListener {
public void onMessageBatch(List<Message> messages) {
//读取全部提醒字段的配置
List<RemindSetting> settingList = remindService.getAllRemindSetting();
List<RemindRecord> recordList = new ArrayList<>();
//将settingList转换为mapkey为deviceIdvalue为List<RemindSetting>
Map<Long,List<RemindSetting>> deviceRemindSettingMap = new HashMap<>();
for (RemindSetting setting : settingList) {
if(deviceRemindSettingMap.containsKey(setting.getDeviceId())){
deviceRemindSettingMap.get(setting.getDeviceId()).add(setting);
}else{
deviceRemindSettingMap.put(setting.getDeviceId(),List.of(setting));
}
}
Map<Long,Integer> deviceWellStatusMap = new HashMap<>();
for (Message message : messages) {
String messageString = new String(message.getBody());
log.info("收到业务消息:{}", messageString);
JSONObject messageObject= JSON.parseObject(messageString);
if(messageObject.containsKey("old")){
JSONObject oldObject = messageObject.getJSONObject("old");
JSONObject newObject = messageObject.getJSONObject("new");
String deviceId = oldObject.getString("deviceId");
//TODO
if(StringUtils.isEmpty(deviceId)){
continue;
}
//根据设备ID获取设备的产品类型
DeviceVO device = deviceService.getDevice(Long.valueOf(deviceId));
//根据对应的产品类型用对应的气井开关字段获取开关状态(wellStatus,solenoidValveStatus,firstSolenoidStatus)
String wellStatusKey = getWellStatusKey(device);
//比对新旧数据看开关状态是否一致
if(!oldObject.getString(wellStatusKey).equals(newObject.getString(wellStatusKey))){
deviceWellStatusMap.put(Long.valueOf(deviceId),newObject.getInteger(wellStatusKey));
}
//根据ID找到对应的提醒字段配置
if(deviceRemindSettingMap.containsKey(Long.valueOf(deviceId))){
List<RemindSetting> remindSettings = deviceRemindSettingMap.get(Long.valueOf(deviceId));
for (RemindSetting remindSetting : remindSettings) {
//遍历该id下的所有字段的配置检查新消息中字段的值是否符合提醒条件
//暂时只处理number类型的字段
if("number".equals(remindSetting.getFieldType())){
BigDecimal newValue = newObject.getBigDecimal(remindSetting.getField());
if(newValue != null){
if(!StringUtils.isEmpty(remindSetting.getMaxValue())){
if(newValue.compareTo(new BigDecimal(remindSetting.getMaxValue())) > 0){
//如果大于最大值则保存提醒记录
addRemindRecordToList(remindSetting, deviceId, device, newValue, recordList);
}else if(!StringUtils.isEmpty(remindSetting.getMinValue())){
if(newValue.compareTo(new BigDecimal(remindSetting.getMinValue())) < 0){
//如果小于最小值则保存提醒记录
addRemindRecordToList(remindSetting, deviceId, device, newValue, recordList);
}
}
}
}
}
}
}
//遍历该id下的所有字段的配置检查新消息中字段的值是否符合提醒条件
}
}
//若不一致则批量记录设备操作日志
//若不一致则记录设备操作日志
if(!deviceWellStatusMap.isEmpty()){
for (Map.Entry<Long, Integer> entry : deviceWellStatusMap.entrySet()) {
this.deviceOptLogService.saveGasWellOptLog(entry.getValue(), entry.getKey(),"device");
}
}
//如果提醒数据不为空批量保存提醒记录
if(!recordList.isEmpty()){
this.remindService.saveBatch(recordList);
}
}
private void addRemindRecordToList(RemindSetting remindSetting, String deviceId, DeviceVO device, BigDecimal newValue, List<RemindRecord> recordList) {
RemindRecord remindRecord = new RemindRecord();
remindRecord.setId(snowflakeConfig.snowflakeId());
remindRecord.setDeviceId(Long.valueOf(deviceId));
remindRecord.setGasWell(device.getGasWell().getId());
remindRecord.setContent(""+ remindSetting.getFieldName()+"】的值达到"+ newValue +",请及时处理");
recordList.add(remindRecord);
}
private static String getWellStatusKey(DeviceVO device) {
String wellStatusKey = "";
if(PersistenceHandler.KNPCV1_MODBUS_TYPE.equals(device.getDeviceType().getCode())){
wellStatusKey = "wellStatus";
} else if(PersistenceHandler.ETC_MODBUS_TYPE.equals(device.getDeviceType().getCode())){
wellStatusKey = "solenoidValveStatus";
} else if(PersistenceHandler.SCSS_MODBUS_TYPE.equals(device.getDeviceType().getCode())){
wellStatusKey = "firstSolenoidStatus";
}
return wellStatusKey;
}
}

View File

@ -58,8 +58,7 @@ public class CommandServiceImpl implements CommandService {
Command.SCSS_TURN_OFF_THE_WELL.equals(command.getCode())) {
flag = 0;
}
;
this.deviceOptLogService.saveGasWellOptLog(flag, command.getDeviceId());
this.deviceOptLogService.saveGasWellOptLog(flag, command.getDeviceId(),"");
return result;
}

View File

@ -8,7 +8,7 @@ import java.util.Date;
public interface DeviceOptLogService extends IService<DeviceOptLog> {
void saveGasWellOptLog(Integer isOpen, Long deviceId);
void saveGasWellOptLog(Integer isOpen, Long deviceId,String source);
Page<DeviceOptLog> page(Page<DeviceOptLog> page, Date start, Date end, Long deviceId);
}

View File

@ -0,0 +1,18 @@
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.RemindRecord;
import com.isu.gaswellwatch.entity.RemindSetting;
import com.isu.gaswellwatch.vo.RemindRecordVO;
import java.util.List;
public interface RemindService extends IService<RemindRecord> {
List<RemindSetting> getAllRemindSetting();
Page<RemindRecordVO> page(Integer currentPage, Integer pageSize, Long deviceId, String startTime, String endTime,String gasWellName);
}

View File

@ -36,7 +36,7 @@ public class DeviceOptLogServiceImpl extends ServiceImpl<DeviceOptLogDao, Device
private SnowflakeConfig snowflakeConfig;
@Override
public void saveGasWellOptLog(Integer isOpen, Long deviceId) {
public void saveGasWellOptLog(Integer isOpen, Long deviceId,String source) {
UserLoginInfoVO userLoginInfoVO = (UserLoginInfoVO) StpUtil.getTokenSession().get(UserConstant.TOKEN_SESSION);
DeviceVO deviceVO = this.deviceDao.getDeviceById(deviceId);
DeviceOptLog deviceOptLog = new DeviceOptLog();
@ -49,7 +49,12 @@ public class DeviceOptLogServiceImpl extends ServiceImpl<DeviceOptLogDao, Device
deviceOptLog.setGasWell(deviceVO.getGasWell().getName());
deviceOptLog.setDeviceDetail(deviceVO.getDeviceType().getName() + "-" + deviceVO.getProduct().getName());
deviceOptLog.setGasStation(deviceVO.getGasStation());
String content = isOpen == null ? "设置成功" : (isOpen == 1 ? "开井成功" : "关井成功");
String content = "";
if("device".equalsIgnoreCase(source)){
content = isOpen == 1 ? "检测到气井开启" : "检测到气井关闭";
}else{
content = isOpen == null ? "设置成功" : (isOpen == 1 ? "开井成功" : "关井成功");
}
deviceOptLog.setContent(content);
this.deviceOptLogDao.insert(deviceOptLog);
}

View File

@ -0,0 +1,36 @@
package com.isu.gaswellwatch.service.impl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isu.gaswellwatch.dao.RemindDao;
import com.isu.gaswellwatch.dao.RoleMenuDao;
import com.isu.gaswellwatch.entity.RemindRecord;
import com.isu.gaswellwatch.entity.RemindSetting;
import com.isu.gaswellwatch.entity.RoleMenu;
import com.isu.gaswellwatch.service.RemindService;
import com.isu.gaswellwatch.service.RoleMenuService;
import com.isu.gaswellwatch.vo.RemindRecordVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("remindService")
@Transactional(rollbackFor = Exception.class)
public class RemindServiceImpl extends ServiceImpl<RemindDao, RemindRecord> implements RemindService {
@Resource
private RemindDao remindDao;
@Override
public List<RemindSetting> getAllRemindSetting() {
return remindDao.getAllRemindSetting();
}
@Override
public Page<RemindRecordVO> page(Integer currentPage, Integer pageSize, Long deviceId, String startTime, String endTime,String gasWellName) {
return remindDao.page(new Page<>(currentPage, pageSize), deviceId, startTime, endTime,gasWellName);
}
}

View File

@ -0,0 +1,39 @@
package com.isu.gaswellwatch.vo;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.isu.gaswellwatch.entity.GasWell;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
public class RemindRecordVO extends Model<RemindRecordVO> {
/** id */
private Long id;
/** 设备ID */
private Long deviceId;
/** 所属气井 */
private GasWell gasWell;
/** 提醒内容 */
private String content;
/** 创建时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private Date createTime;
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.isu.gaswellwatch.dao.RemindDao">
<resultMap id="remindRecordMap" type="com.isu.gaswellwatch.vo.RemindRecordVO">
<id column="id" property="id"/>
<result column="device_id" property="deviceId"/>
<result column="content" property="content"/>
<result column="create_time" property="createTime"/>
<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>
<select id="getAllRemindSetting" resultType="com.isu.gaswellwatch.entity.RemindSetting">
select t.id,t.device_id,t.field,t.field_name,t.field_type,t.min_value,t.max_value,t.create_time,t.update_time from remind_setting t
</select>
<select id="page" resultMap="remindRecordMap">
select t.id,t.device_id,t.content,t.create_time,g.name as gasWellName,g.id as gasWellId,g.block_id as gasWellBlock from remind_record t left join gas_well g on t.gas_well = g.id
<where>
<if test="deviceId!=null">
t.device_id = #{deviceId}
</if>
<if test="gasWellName!=null and gasWellName!='' ">
and g.name LIKE CONCAT('%',#{gasWellName},'%')
</if>
<if test="startTime!=null">
and t.create_time &gt;= #{startTime}
</if>
<if test="endTime!=null">
and t.create_time &lt;= #{endTime}
</if>
</where>
order by t.create_time desc
</select>
</mapper>