dev #2
@ -5,10 +5,13 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.xzzn.common.utils.StringUtils;
|
||||
import com.xzzn.ems.domain.EmsStrategyRunning;
|
||||
import com.xzzn.ems.domain.MqttSyncLog;
|
||||
import com.xzzn.ems.domain.vo.StrategyRunningVo;
|
||||
import com.xzzn.ems.mapper.EmsMqttTopicConfigMapper;
|
||||
import com.xzzn.ems.mapper.EmsStrategyRunningMapper;
|
||||
import com.xzzn.ems.mapper.MqttSyncLogMapper;
|
||||
import com.xzzn.framework.web.service.MqttPublisher;
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
@ -17,22 +20,22 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cglib.beans.BeanMap;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 策略运行切面同步
|
||||
* 云端 - 本地
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class StrategySyncAspect {
|
||||
public class StrategyRunningSyncAspect {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StrategyRunningSyncAspect.class);
|
||||
@Autowired
|
||||
private MqttPublisher mqttPublisher;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final String STRATEGY_TOPIC = "EMS_STRATEGY_UP";
|
||||
private static final String MQTT_TOPIC = "EMS_STRATEGY_UP";
|
||||
private static final String TABLE_NAME = "ems_strategy_running";
|
||||
@Autowired
|
||||
private EmsMqttTopicConfigMapper emsMqttTopicConfigMapper;
|
||||
@ -41,27 +44,32 @@ public class StrategySyncAspect {
|
||||
@Autowired
|
||||
private EmsStrategyRunningMapper emsStrategyRunningMapper;
|
||||
|
||||
// 定义切点:拦截策略相关表的Mapper方法
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyRunningMapper.stopEmsStrategyRunning(..)) && args(id)) ")
|
||||
public void stopPointCut(Long id) {
|
||||
System.out.println("【停止策略切面】StrategyAspect 被实例化");
|
||||
logger.info("【停止策略切面】StrategyAspect 被实例化");
|
||||
}
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyRunningMapper.insertEmsStrategyRunning(..)) && args(insertEntity)) ")
|
||||
public void insertPointCut(EmsStrategyRunning insertEntity) {
|
||||
System.out.println("【新增策略切面】StrategyAspect 被实例化");
|
||||
logger.info("【新增策略切面】StrategyAspect 被实例化");
|
||||
}
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyRunningMapper.updateEmsStrategyRunning(..)) && args(updateEntity)) ")
|
||||
public void updatePointCut(EmsStrategyRunning updateEntity) {
|
||||
System.out.println("【更新策略切面】StrategyAspect 被实例化");
|
||||
logger.info("【更新策略切面】StrategyAspect 被实例化");
|
||||
}
|
||||
|
||||
// 方法执行成功后发布同步消息
|
||||
@AfterReturning(pointcut = "updatePointCut(updateEntity)", returning = "result")
|
||||
public void afterUpdate(JoinPoint joinPoint, EmsStrategyRunning updateEntity, Integer result) {
|
||||
System.out.println("【更新策略切面进入成功】");
|
||||
if (result == 0) {
|
||||
logger.info("【更新策略切面进入成功】");
|
||||
if (result == 0 || updateEntity == null) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
@ -72,14 +80,16 @@ public class StrategySyncAspect {
|
||||
|
||||
try {
|
||||
// 数据转换
|
||||
String content = convertEntityToJson(updateEntity);
|
||||
List<StrategyRunningVo> runningVos = emsStrategyRunningMapper.getRunningList(siteId);
|
||||
if (runningVos == null || runningVos.size() == 0) {
|
||||
return;
|
||||
}
|
||||
StrategyRunningVo runningVo = runningVos.get(0);
|
||||
String content = objectMapper.writeValueAsString(runningVo);
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题 - 判断区分本地还是云上
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(STRATEGY_TOPIC);
|
||||
if (StringUtils.isEmpty(topic)) {
|
||||
mqttPublisher.publish(STRATEGY_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
}
|
||||
// 发布到MQTT主题
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
@ -90,10 +100,16 @@ public class StrategySyncAspect {
|
||||
|
||||
@AfterReturning(pointcut = "insertPointCut(insertEntity)", returning = "result")
|
||||
public void afterInsert(JoinPoint joinPoint, EmsStrategyRunning insertEntity, Integer result) {
|
||||
System.out.println("【新增策略切面进入成功】");
|
||||
if (result == 0) {
|
||||
logger.info("【新增策略切面进入成功】");
|
||||
if (result == 0 || insertEntity == null) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
@ -104,14 +120,16 @@ public class StrategySyncAspect {
|
||||
|
||||
try {
|
||||
// 数据转换
|
||||
String content = convertEntityToJson(insertEntity);
|
||||
List<StrategyRunningVo> runningVos = emsStrategyRunningMapper.getRunningList(siteId);
|
||||
if (runningVos == null || runningVos.size() == 0) {
|
||||
return;
|
||||
}
|
||||
StrategyRunningVo runningVo = runningVos.get(0);
|
||||
String content = objectMapper.writeValueAsString(runningVo);
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(STRATEGY_TOPIC);
|
||||
if (StringUtils.isEmpty(topic)) {
|
||||
mqttPublisher.publish(STRATEGY_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
}
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
@ -122,10 +140,16 @@ public class StrategySyncAspect {
|
||||
|
||||
@AfterReturning(pointcut = "stopPointCut(id)", returning = "result")
|
||||
public void afterStop(JoinPoint joinPoint, Long id, Integer result) {
|
||||
System.out.println("【停止策略切面进入成功】");
|
||||
if (result == 0) {
|
||||
logger.info("【停止策略切面进入成功】");
|
||||
if (result == 0 || id == null) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
@ -143,10 +167,7 @@ public class StrategySyncAspect {
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(STRATEGY_TOPIC);
|
||||
if (StringUtils.isEmpty(topic)) {
|
||||
mqttPublisher.publish(STRATEGY_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
}
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
@ -162,8 +183,9 @@ public class StrategySyncAspect {
|
||||
message.setOperateType(operateType);
|
||||
message.setTableName(TABLE_NAME);
|
||||
message.setCreateTime(new Date());
|
||||
message.setTopic(STRATEGY_TOPIC);
|
||||
message.setTopic(MQTT_TOPIC);
|
||||
message.setStatus("SUCCESS");
|
||||
message.setSyncObject("CLOUD");
|
||||
message.setTarget(siteId);
|
||||
return message;
|
||||
}
|
||||
@ -0,0 +1,250 @@
|
||||
package com.xzzn.framework.aspectj;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.xzzn.common.utils.StringUtils;
|
||||
import com.xzzn.common.utils.bean.BeanUtils;
|
||||
import com.xzzn.ems.domain.*;
|
||||
import com.xzzn.ems.domain.vo.SyncStrategyTempVo;
|
||||
import com.xzzn.ems.mapper.*;
|
||||
import com.xzzn.framework.web.service.MqttPublisher;
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cglib.beans.BeanMap;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 策略模板数据同步
|
||||
* 云端 - 本地
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class StrategyTempSyncAspect {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StrategyTempSyncAspect.class);
|
||||
@Autowired
|
||||
private MqttPublisher mqttPublisher;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final String MQTT_TOPIC = "EMS_STRATEGY_UP";
|
||||
private static final String TABLE_NAME = "ems_strategy_temp";
|
||||
@Autowired
|
||||
private EmsMqttTopicConfigMapper emsMqttTopicConfigMapper;
|
||||
@Autowired
|
||||
private MqttSyncLogMapper mqttSyncLogMapper;
|
||||
@Autowired
|
||||
private EmsStrategyMapper emsStrategyMapper;
|
||||
@Autowired
|
||||
private EmsStrategyTempMapper emsStrategyTempMapper;
|
||||
|
||||
// 用ThreadLocal暂存删除前的对象
|
||||
private ThreadLocal<EmsStrategyTemp> beforeDeleteThreadLocal = new ThreadLocal<>();
|
||||
@Before("execution(* com.xzzn.ems.mapper.EmsStrategyTempMapper.deleteEmsStrategyTempById(..)) && args(templateId)")
|
||||
public void beforeDelete(JoinPoint joinPoint, String templateId) {
|
||||
// 查询删除前的数据-仅存一获取siteId
|
||||
List<EmsStrategyTemp> tempList = emsStrategyTempMapper.selectStrategyTempByTempId(templateId);
|
||||
if (tempList != null && tempList.size() > 0) {
|
||||
beforeDeleteThreadLocal.set(tempList.get(0)); // 暂存
|
||||
}
|
||||
}
|
||||
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyTempMapper.deleteEmsStrategyTempById(..)) && args(templateId)) ")
|
||||
public void deletePointCut(String templateId) {
|
||||
logger.info("【删除策略模版切面】StrategyTempSyncAspect 被实例化");
|
||||
}
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyTempMapper.insertEmsStrategyTemp(..)) && args(insertEntity)) ")
|
||||
public void insertPointCut(EmsStrategyTemp insertEntity) {
|
||||
logger.info("【新增策略模版切面】StrategyTempSyncAspect 被实例化");
|
||||
}
|
||||
|
||||
// 方法执行成功后发布同步消息
|
||||
@AfterReturning(pointcut = "insertPointCut(insertEntity)", returning = "result")
|
||||
public void afterInsert(JoinPoint joinPoint, EmsStrategyTemp insertEntity, Integer result) {
|
||||
logger.info("【新增策略切面进入成功】");
|
||||
if (result == 0 || insertEntity == null) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
String siteId = insertEntity.getSiteId();
|
||||
|
||||
// 构建日志同步消息
|
||||
MqttSyncLog message = createMessageObject(operateType, siteId);
|
||||
|
||||
try {
|
||||
// 校验策略id是否存在
|
||||
Long strategyId = insertEntity.getStrategyId();
|
||||
if (strategyId == null) {
|
||||
return;
|
||||
}
|
||||
// 数据转换
|
||||
SyncStrategyTempVo tempVo = convertEntity(insertEntity);
|
||||
String content = objectMapper.writeValueAsString(tempVo);
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
}
|
||||
// 存储同步信息
|
||||
mqttSyncLogMapper.insertMqttSyncLog(message);
|
||||
}
|
||||
private SyncStrategyTempVo convertEntity(EmsStrategyTemp insertEntity) {
|
||||
SyncStrategyTempVo tempVo = new SyncStrategyTempVo();
|
||||
BeanUtils.copyProperties(insertEntity, tempVo);
|
||||
EmsStrategy strategy = emsStrategyMapper.selectEmsStrategyById(insertEntity.getStrategyId());
|
||||
if (strategy != null) {
|
||||
tempVo.setStrategyName(strategy.getStrategyName());
|
||||
tempVo.setStrategyType(strategy.getStrategyType());
|
||||
}
|
||||
return tempVo;
|
||||
}
|
||||
|
||||
@AfterReturning(pointcut = "deletePointCut(templateId)", returning = "result")
|
||||
public void afterDelete(JoinPoint joinPoint, String templateId, Integer result) {
|
||||
logger.info("【删除策略模版切面进入成功】");
|
||||
if (result == 0 || StringUtils.isEmpty(templateId)) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
// 从ThreadLocal中获取删除前的对象
|
||||
EmsStrategyTemp strategyTemp = beforeDeleteThreadLocal.get();
|
||||
String siteId = "";
|
||||
if (strategyTemp != null) {
|
||||
siteId = strategyTemp.getSiteId();
|
||||
}
|
||||
|
||||
// 构建日志同步消息
|
||||
MqttSyncLog message = createMessageObject(operateType, siteId);
|
||||
|
||||
try {
|
||||
// 数据转换
|
||||
Map<String, Object> idMap = new HashMap<>();
|
||||
idMap.put("templateId", templateId); // 手动将参数值映射到"id"字段
|
||||
String content = JSON.toJSONString(idMap);
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
}
|
||||
// 存储同步信息
|
||||
mqttSyncLogMapper.insertMqttSyncLog(message);
|
||||
}
|
||||
|
||||
// 构建同步信息
|
||||
private MqttSyncLog createMessageObject(String operateType, String siteId) {
|
||||
MqttSyncLog message = new MqttSyncLog();
|
||||
message.setSyncId(UUID.randomUUID().toString());
|
||||
message.setOperateType(operateType);
|
||||
message.setTableName(TABLE_NAME);
|
||||
message.setCreateTime(new Date());
|
||||
message.setTopic(MQTT_TOPIC);
|
||||
message.setStatus("SUCCESS");
|
||||
message.setSyncObject("CLOUD");
|
||||
message.setTarget(siteId);
|
||||
return message;
|
||||
}
|
||||
// 从方法名判断操作类型(示例:insert→INSERT,update→UPDATE,delete→DELETE)
|
||||
private String getOperateType(String methodName) {
|
||||
if (methodName.startsWith("insert")) return "INSERT";
|
||||
if (methodName.startsWith("stop")) return "STOP";
|
||||
if (methodName.startsWith("update") || methodName.startsWith("stop")) return "UPDATE";
|
||||
if (methodName.startsWith("delete")) return "DELETE";
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
// 从方法参数提取数据(示例:若参数是实体类,转成Map)
|
||||
private Map<String, Object> extractDataFromParams(Object[] args) {
|
||||
// 实际需反射获取实体类的字段和值(如id、name等)
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
if (args == null || args.length == 0) {
|
||||
return data;
|
||||
}
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Object arg = args[i];
|
||||
if (arg == null) {
|
||||
continue; // 跳过null参数
|
||||
}
|
||||
|
||||
// 处理基本类型/包装类/字符串(直接作为值存入,key为"param0"、"param1"等)
|
||||
if (isBasicType(arg.getClass())) {
|
||||
String key = "param" + i; // 基本类型参数用"param0"、"param1"作为key
|
||||
data.put(key, arg);
|
||||
} else {
|
||||
Map<String, Object> beanMap = beanToMap(arg);
|
||||
data.putAll(beanMap); // 合并实体类的字段到结果Map
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为基本类型或包装类或字符串
|
||||
*/
|
||||
private boolean isBasicType(Class<?> clazz) {
|
||||
return clazz.isPrimitive() // 基本类型(int、long、boolean等)
|
||||
|| clazz == String.class // 字符串
|
||||
|| Number.class.isAssignableFrom(clazz) // 数字包装类(Integer、Long等)
|
||||
|| clazz == Boolean.class; // 布尔包装类
|
||||
}
|
||||
|
||||
/**
|
||||
* 将实体类转换为Map(字段名为key,字段值为value)
|
||||
*/
|
||||
private Map<String, Object> beanToMap(Object bean) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
if (bean == null) {
|
||||
return map;
|
||||
}
|
||||
|
||||
// 方式1:使用BeanMap(简洁高效)
|
||||
BeanMap beanMap = BeanMap.create(bean);
|
||||
for (Object key : beanMap.keySet()) {
|
||||
map.put(key.toString(), beanMap.get(key));
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// 在方法中转换
|
||||
public String convertEntityToJson(EmsStrategyRunning insertEntity) throws Exception {
|
||||
if (insertEntity == null) {
|
||||
return null; // 空对象返回空JSON
|
||||
}
|
||||
|
||||
// 将实体类转换为JSON字符串
|
||||
return objectMapper.writeValueAsString(insertEntity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,231 @@
|
||||
package com.xzzn.framework.aspectj;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.xzzn.common.utils.StringUtils;
|
||||
import com.xzzn.common.utils.bean.BeanUtils;
|
||||
import com.xzzn.ems.domain.*;
|
||||
import com.xzzn.ems.domain.vo.SyncStrategyTimeConfigVo;
|
||||
import com.xzzn.ems.mapper.*;
|
||||
import com.xzzn.framework.web.service.MqttPublisher;
|
||||
import org.apache.juli.logging.Log;
|
||||
import org.apache.juli.logging.LogFactory;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.AfterReturning;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cglib.beans.BeanMap;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 策略时间配置同步
|
||||
* 云端 - 本地
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class StrategyTimeConfigSyncAspect {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(StrategyTimeConfigSyncAspect.class);
|
||||
@Autowired
|
||||
private MqttPublisher mqttPublisher;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final String MQTT_TOPIC = "EMS_STRATEGY_UP";
|
||||
private static final String TABLE_NAME = "ems_strategy_temp";
|
||||
@Autowired
|
||||
private EmsMqttTopicConfigMapper emsMqttTopicConfigMapper;
|
||||
@Autowired
|
||||
private MqttSyncLogMapper mqttSyncLogMapper;
|
||||
@Autowired
|
||||
private EmsStrategyMapper emsStrategyMapper;
|
||||
@Autowired
|
||||
private EmsStrategyTempMapper emsStrategyTempMapper;
|
||||
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper.insertEmsStrategyTimeConfig(..)) && args(insertEntity)) ")
|
||||
public void insertPointCut(EmsStrategyTimeConfig insertEntity) {
|
||||
logger.info("【新增策略模版时间配置切面】StrategyTimeConfigSyncAspect 被实例化");
|
||||
}
|
||||
@Pointcut("(execution(* com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(..)) && args(updateEntity)) ")
|
||||
public void updatePointCut(EmsStrategyTimeConfig updateEntity) {
|
||||
logger.info("【更新策略模版时间配置切面】StrategyTimeConfigSyncAspect 被实例化");
|
||||
}
|
||||
|
||||
// 方法执行成功后发布同步消息
|
||||
@AfterReturning(pointcut = "insertPointCut(insertEntity)", returning = "result")
|
||||
public void afterInsert(JoinPoint joinPoint, EmsStrategyTimeConfig insertEntity, Integer result) {
|
||||
logger.info("【新增策略模版时间切面进入成功】");
|
||||
if (result == 0 || insertEntity == null) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
String siteId = insertEntity.getSiteId();
|
||||
|
||||
// 构建日志同步消息
|
||||
MqttSyncLog message = createMessageObject(operateType, siteId);
|
||||
|
||||
try {
|
||||
// 校验策略id是否存在
|
||||
Long strategyId = insertEntity.getStrategyId();
|
||||
if (strategyId == null) {
|
||||
return;
|
||||
}
|
||||
// 数据转换
|
||||
SyncStrategyTimeConfigVo timeConfigVo = convertEntity(insertEntity);
|
||||
String content = objectMapper.writeValueAsString(timeConfigVo);
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
}
|
||||
// 存储同步信息
|
||||
mqttSyncLogMapper.insertMqttSyncLog(message);
|
||||
}
|
||||
private SyncStrategyTimeConfigVo convertEntity(EmsStrategyTimeConfig insertEntity) {
|
||||
SyncStrategyTimeConfigVo timeConfigVo = new SyncStrategyTimeConfigVo();
|
||||
BeanUtils.copyProperties(insertEntity, timeConfigVo);
|
||||
EmsStrategy strategy = emsStrategyMapper.selectEmsStrategyById(insertEntity.getStrategyId());
|
||||
if (strategy != null) {
|
||||
timeConfigVo.setStrategyName(strategy.getStrategyName());
|
||||
timeConfigVo.setStrategyType(strategy.getStrategyType());
|
||||
}
|
||||
return timeConfigVo;
|
||||
}
|
||||
|
||||
@AfterReturning(pointcut = "updatePointCut(updateEntity)", returning = "result")
|
||||
public void afterUpdate(JoinPoint joinPoint, EmsStrategyTimeConfig updateEntity, Integer result) {
|
||||
logger.info("【删除策略模版切面进入成功】");
|
||||
if (result == 0 || updateEntity == null) {
|
||||
return;
|
||||
}
|
||||
// 校验是否配置监听topic-监听则不发布
|
||||
String topic = emsMqttTopicConfigMapper.checkTopicIsExist(MQTT_TOPIC);
|
||||
if (!StringUtils.isEmpty(topic)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 解析方法名,获取操作类型(INSERT/UPDATE/DELETE)和表名
|
||||
String methodName = joinPoint.getSignature().getName();
|
||||
String operateType = getOperateType(methodName);
|
||||
String siteId = updateEntity.getSiteId();
|
||||
|
||||
// 构建日志同步消息
|
||||
MqttSyncLog message = createMessageObject(operateType, siteId);
|
||||
|
||||
try {
|
||||
// 数据转换
|
||||
SyncStrategyTimeConfigVo timeConfigVo = convertEntity(updateEntity);
|
||||
String content = objectMapper.writeValueAsString(timeConfigVo);
|
||||
message.setContent(content);
|
||||
|
||||
// 发布到MQTT主题
|
||||
mqttPublisher.publish(MQTT_TOPIC, objectMapper.writeValueAsString(message), 1);
|
||||
} catch (Exception e) {
|
||||
message.setStatus("FAIL");
|
||||
message.setErrorMsg(e.getMessage());
|
||||
}
|
||||
// 存储同步信息
|
||||
mqttSyncLogMapper.insertMqttSyncLog(message);
|
||||
}
|
||||
|
||||
// 构建同步信息
|
||||
private MqttSyncLog createMessageObject(String operateType, String siteId) {
|
||||
MqttSyncLog message = new MqttSyncLog();
|
||||
message.setSyncId(UUID.randomUUID().toString());
|
||||
message.setOperateType(operateType);
|
||||
message.setTableName(TABLE_NAME);
|
||||
message.setCreateTime(new Date());
|
||||
message.setTopic(MQTT_TOPIC);
|
||||
message.setStatus("SUCCESS");
|
||||
message.setSyncObject("CLOUD");
|
||||
message.setTarget(siteId);
|
||||
return message;
|
||||
}
|
||||
// 从方法名判断操作类型(示例:insert→INSERT,update→UPDATE,delete→DELETE)
|
||||
private String getOperateType(String methodName) {
|
||||
if (methodName.startsWith("insert")) return "INSERT";
|
||||
if (methodName.startsWith("stop")) return "STOP";
|
||||
if (methodName.startsWith("update") || methodName.startsWith("stop")) return "UPDATE";
|
||||
if (methodName.startsWith("delete")) return "DELETE";
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
// 从方法参数提取数据(示例:若参数是实体类,转成Map)
|
||||
private Map<String, Object> extractDataFromParams(Object[] args) {
|
||||
// 实际需反射获取实体类的字段和值(如id、name等)
|
||||
Map<String, Object> data = new HashMap<>();
|
||||
if (args == null || args.length == 0) {
|
||||
return data;
|
||||
}
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
Object arg = args[i];
|
||||
if (arg == null) {
|
||||
continue; // 跳过null参数
|
||||
}
|
||||
|
||||
// 处理基本类型/包装类/字符串(直接作为值存入,key为"param0"、"param1"等)
|
||||
if (isBasicType(arg.getClass())) {
|
||||
String key = "param" + i; // 基本类型参数用"param0"、"param1"作为key
|
||||
data.put(key, arg);
|
||||
} else {
|
||||
Map<String, Object> beanMap = beanToMap(arg);
|
||||
data.putAll(beanMap); // 合并实体类的字段到结果Map
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为基本类型或包装类或字符串
|
||||
*/
|
||||
private boolean isBasicType(Class<?> clazz) {
|
||||
return clazz.isPrimitive() // 基本类型(int、long、boolean等)
|
||||
|| clazz == String.class // 字符串
|
||||
|| Number.class.isAssignableFrom(clazz) // 数字包装类(Integer、Long等)
|
||||
|| clazz == Boolean.class; // 布尔包装类
|
||||
}
|
||||
|
||||
/**
|
||||
* 将实体类转换为Map(字段名为key,字段值为value)
|
||||
*/
|
||||
private Map<String, Object> beanToMap(Object bean) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
if (bean == null) {
|
||||
return map;
|
||||
}
|
||||
|
||||
// 方式1:使用BeanMap(简洁高效)
|
||||
BeanMap beanMap = BeanMap.create(bean);
|
||||
for (Object key : beanMap.keySet()) {
|
||||
map.put(key.toString(), beanMap.get(key));
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
// 在方法中转换
|
||||
public String convertEntityToJson(EmsStrategyRunning insertEntity) throws Exception {
|
||||
if (insertEntity == null) {
|
||||
return null; // 空对象返回空JSON
|
||||
}
|
||||
|
||||
// 将实体类转换为JSON字符串
|
||||
return objectMapper.writeValueAsString(insertEntity);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package com.xzzn.ems.domain.vo;
|
||||
|
||||
import com.xzzn.common.annotation.Excel;
|
||||
import com.xzzn.ems.domain.EmsStrategyTemp;
|
||||
|
||||
/**
|
||||
* 同步策略模版对象
|
||||
*
|
||||
* @author xzzn
|
||||
* @date 2025-11-16
|
||||
*/
|
||||
public class SyncStrategyTempVo extends EmsStrategyTemp
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 策略名称,如“削峰填谷” */
|
||||
@Excel(name = "策略名称,如“削峰填谷”")
|
||||
private String strategyName;
|
||||
|
||||
/** 策略类型:1 - 主策略;2 - 辅助策略 */
|
||||
@Excel(name = "策略类型:1 - 主策略;2 - 辅助策略")
|
||||
private Long strategyType;
|
||||
|
||||
public String getStrategyName() {
|
||||
return strategyName;
|
||||
}
|
||||
|
||||
public void setStrategyName(String strategyName) {
|
||||
this.strategyName = strategyName;
|
||||
}
|
||||
|
||||
public Long getStrategyType() {
|
||||
return strategyType;
|
||||
}
|
||||
|
||||
public void setStrategyType(Long strategyType) {
|
||||
this.strategyType = strategyType;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.xzzn.ems.domain.vo;
|
||||
|
||||
import com.xzzn.common.annotation.Excel;
|
||||
import com.xzzn.ems.domain.EmsStrategyTemp;
|
||||
import com.xzzn.ems.domain.EmsStrategyTimeConfig;
|
||||
|
||||
/**
|
||||
* 同步策略时间配置对象
|
||||
*
|
||||
* @author xzzn
|
||||
* @date 2025-11-16
|
||||
*/
|
||||
public class SyncStrategyTimeConfigVo extends EmsStrategyTimeConfig
|
||||
{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 策略名称,如“削峰填谷” */
|
||||
@Excel(name = "策略名称,如“削峰填谷”")
|
||||
private String strategyName;
|
||||
|
||||
/** 策略类型:1 - 主策略;2 - 辅助策略 */
|
||||
@Excel(name = "策略类型:1 - 主策略;2 - 辅助策略")
|
||||
private Long strategyType;
|
||||
|
||||
public String getStrategyName() {
|
||||
return strategyName;
|
||||
}
|
||||
|
||||
public void setStrategyName(String strategyName) {
|
||||
this.strategyName = strategyName;
|
||||
}
|
||||
|
||||
public Long getStrategyType() {
|
||||
return strategyType;
|
||||
}
|
||||
|
||||
public void setStrategyType(Long strategyType) {
|
||||
this.strategyType = strategyType;
|
||||
}
|
||||
}
|
||||
@ -60,4 +60,7 @@ public interface EmsStrategyMapper
|
||||
public int deleteEmsStrategyByIds(Long[] ids);
|
||||
|
||||
public List<EmsStrategy> getStrategyListByType(Long type);
|
||||
|
||||
// 通过名称获取策略
|
||||
public EmsStrategy getStrategyByName(String strategyName);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.xzzn.ems.mapper;
|
||||
import java.util.List;
|
||||
import com.xzzn.ems.domain.EmsStrategyTimeConfig;
|
||||
import com.xzzn.ems.domain.vo.StrategyTimeConfigVo;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 时间配置Mapper接口
|
||||
@ -71,4 +72,9 @@ public interface EmsStrategyTimeConfigMapper
|
||||
|
||||
// 设置该模版的时间配置为待下发
|
||||
public void updateTimeConfigWaitingPost(String templateId);
|
||||
|
||||
// 判断时间配置是否存在
|
||||
public EmsStrategyTimeConfig getExistTimeConfig(@Param("siteId")String siteId,
|
||||
@Param("strategyId")Long strategyId,
|
||||
@Param("month")Long month);
|
||||
}
|
||||
|
||||
@ -29,5 +29,5 @@ public interface IEmsStrategyService
|
||||
public int configStrategy(EmsStrategyRunning emsStrategyRunning);
|
||||
|
||||
// 接收云上运行策略配置
|
||||
public void dealStrategyData(String content, String operateType);
|
||||
public void dealStrategyRunningData(String content, String operateType);
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.xzzn.ems.service;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.xzzn.ems.domain.EmsStrategyTemp;
|
||||
import com.xzzn.ems.domain.vo.StrategyTempConfigRequest;
|
||||
|
||||
@ -32,4 +33,7 @@ public interface IEmsStrategyTempService
|
||||
|
||||
//根据templateId删除模板
|
||||
public int deleteStrategyTempById(String templateId);
|
||||
|
||||
// 处理同步数据
|
||||
public void dealStrategyTempData(String content, String operateType) throws JsonProcessingException;
|
||||
}
|
||||
|
||||
@ -67,4 +67,7 @@ public interface IEmsStrategyTimeConfigService
|
||||
* @return 时间配置集合
|
||||
*/
|
||||
public List<StrategyTimeConfigVo> getStrategyTimeList(EmsStrategyTimeConfig emsStrategyTimeConfig);
|
||||
|
||||
// 处理同步数据
|
||||
public void dealStrategyTimeData(String content, String operateType);
|
||||
}
|
||||
|
||||
@ -3,11 +3,13 @@ package com.xzzn.ems.service.impl;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.xzzn.common.enums.StrategyStatus;
|
||||
import com.xzzn.common.utils.DateUtils;
|
||||
import com.xzzn.common.utils.StringUtils;
|
||||
import com.xzzn.ems.domain.EmsStrategyRunning;
|
||||
import com.xzzn.ems.domain.vo.StrategyRunningVo;
|
||||
import com.xzzn.ems.mapper.EmsStrategyRunningMapper;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.xzzn.ems.mapper.EmsStrategyMapper;
|
||||
@ -67,31 +69,43 @@ public class EmsStrategyServiceImpl implements IEmsStrategyService
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dealStrategyData(String content, String operateType) {
|
||||
public void dealStrategyRunningData(String content, String operateType) {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return;
|
||||
}
|
||||
EmsStrategyRunning emsStrategyRunning = JSON.parseObject(content, EmsStrategyRunning.class);
|
||||
String siteId = "";
|
||||
EmsStrategyRunning emsStrategyRunning = new EmsStrategyRunning();
|
||||
switch (operateType) {
|
||||
case "INSERT":
|
||||
StrategyRunningVo insertVo = JSON.parseObject(content, StrategyRunningVo.class);
|
||||
BeanUtils.copyProperties(insertVo, emsStrategyRunning);
|
||||
// 先校验策略是否存在,不存在则插入
|
||||
dealStrategyData(emsStrategyRunning, insertVo);
|
||||
// 新增策略运行数据
|
||||
emsStrategyRunningMapper.insertEmsStrategyRunning(emsStrategyRunning);
|
||||
break;
|
||||
case "STOP":
|
||||
Long id = emsStrategyRunning.getId();
|
||||
emsStrategyRunningMapper.stopEmsStrategyRunning(id);
|
||||
EmsStrategyRunning vo = JSON.parseObject(content, EmsStrategyRunning.class);
|
||||
siteId = vo.getSiteId();
|
||||
// 停止站点正在运行的策略即可
|
||||
EmsStrategyRunning runningStrategy = emsStrategyRunningMapper.getRunningStrategy(siteId);
|
||||
emsStrategyRunningMapper.stopEmsStrategyRunning(runningStrategy.getId());
|
||||
break;
|
||||
case "UPDATE":
|
||||
String siteId = emsStrategyRunning.getSiteId();
|
||||
Long mainId = emsStrategyRunning.getMainStrategyId();
|
||||
Long auxId = emsStrategyRunning.getAuxiliaryStrategyId();
|
||||
StrategyRunningVo updateVo = JSON.parseObject(content, StrategyRunningVo.class);
|
||||
siteId = updateVo.getSiteId();
|
||||
// 获取该站点是否有正在运行的策略,无则新增,有则更新
|
||||
EmsStrategyRunning existStrategy = emsStrategyRunningMapper.getRunningStrategy(siteId);
|
||||
if (existStrategy != null) { // 存在正在运行的则更新
|
||||
existStrategy.setMainStrategyId(mainId);
|
||||
existStrategy.setAuxiliaryStrategyId(auxId);
|
||||
emsStrategyRunningMapper.updateEmsStrategyRunning(emsStrategyRunning);
|
||||
} else { // 不存在着插入
|
||||
emsStrategyRunning.setCreateTime(DateUtils.getNowDate());
|
||||
if (existStrategy == null) {
|
||||
BeanUtils.copyProperties(updateVo, emsStrategyRunning);
|
||||
// 先校验策略是否存在,不存在则插入
|
||||
dealStrategyData(emsStrategyRunning, updateVo);
|
||||
emsStrategyRunningMapper.insertEmsStrategyRunning(emsStrategyRunning);
|
||||
} else {
|
||||
// 校验更新的主副策略是否存在,不存在则插入,存在则获取id
|
||||
dealStrategyData(existStrategy, updateVo);
|
||||
emsStrategyRunning.setCreateTime(DateUtils.getNowDate());
|
||||
emsStrategyRunningMapper.updateEmsStrategyRunning(existStrategy);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -99,4 +113,36 @@ public class EmsStrategyServiceImpl implements IEmsStrategyService
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void dealStrategyData(EmsStrategyRunning emsStrategyRunning, StrategyRunningVo insertVo) {
|
||||
// 主策略
|
||||
String mainStrategyName = insertVo.getMainStrategyName();
|
||||
EmsStrategy mainStrategy = emsStrategyMapper.getStrategyByName(mainStrategyName);
|
||||
if (mainStrategy != null) {
|
||||
emsStrategyRunning.setMainStrategyId(mainStrategy.getId());
|
||||
} else {
|
||||
mainStrategy = createStrategyEntity(mainStrategyName, 1L);
|
||||
emsStrategyMapper.insertEmsStrategy(mainStrategy);
|
||||
emsStrategyRunning.setMainStrategyId(mainStrategy.getId());
|
||||
}
|
||||
// 副策略
|
||||
String auxStrategyName = insertVo.getAuxStrategyName();
|
||||
EmsStrategy auxStrategy = emsStrategyMapper.getStrategyByName(auxStrategyName);
|
||||
if (auxStrategy != null) {
|
||||
emsStrategyRunning.setAuxiliaryStrategyId(auxStrategy.getId());
|
||||
} else {
|
||||
auxStrategy = createStrategyEntity(mainStrategyName, 2L);
|
||||
emsStrategyMapper.insertEmsStrategy(auxStrategy);
|
||||
emsStrategyRunning.setAuxiliaryStrategyId(auxStrategy.getId());
|
||||
}
|
||||
}
|
||||
|
||||
private EmsStrategy createStrategyEntity(String strategyName, Long type) {
|
||||
EmsStrategy strategy = new EmsStrategy();
|
||||
strategy.setStrategyType(type);
|
||||
strategy.setStrategyName(strategyName);
|
||||
strategy.setStatus(StrategyStatus.RUNNING.getCode());
|
||||
strategy.setCreateTime(DateUtils.getNowDate());
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,12 +3,22 @@ package com.xzzn.ems.service.impl;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.xzzn.common.enums.StrategyStatus;
|
||||
import com.xzzn.common.utils.DateUtils;
|
||||
import com.xzzn.common.utils.StringUtils;
|
||||
import com.xzzn.common.utils.bean.BeanUtils;
|
||||
import com.xzzn.ems.domain.EmsStrategy;
|
||||
import com.xzzn.ems.domain.EmsStrategyRunning;
|
||||
import com.xzzn.ems.domain.EmsStrategyTempTimeConfig;
|
||||
import com.xzzn.ems.domain.vo.StrategyRunningVo;
|
||||
import com.xzzn.ems.domain.vo.StrategyTempConfigRequest;
|
||||
import com.xzzn.ems.domain.vo.SyncStrategyTempVo;
|
||||
import com.xzzn.ems.mapper.EmsStrategyCurveMapper;
|
||||
import com.xzzn.ems.mapper.EmsStrategyMapper;
|
||||
import com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -33,7 +43,10 @@ public class EmsStrategyTempServiceImpl implements IEmsStrategyTempService
|
||||
private EmsStrategyTimeConfigMapper emsStrategyTimeConfigMapper;
|
||||
@Autowired
|
||||
private EmsStrategyCurveMapper emsStrategyCurveMapper;
|
||||
@Autowired
|
||||
private EmsStrategyMapper emsStrategyMapper;
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
/**
|
||||
* 查询模板列表
|
||||
*
|
||||
@ -123,4 +136,54 @@ public class EmsStrategyTempServiceImpl implements IEmsStrategyTempService
|
||||
|
||||
return emsStrategyTempMapper.deleteTempByTempId(templateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dealStrategyTempData(String content, String operateType) throws JsonProcessingException {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return;
|
||||
}
|
||||
String siteId = "";
|
||||
EmsStrategyTemp temp = new EmsStrategyTemp();
|
||||
switch (operateType) {
|
||||
case "INSERT":
|
||||
SyncStrategyTempVo syncVo = JSON.parseObject(content, SyncStrategyTempVo.class);
|
||||
BeanUtils.copyProperties(syncVo, temp);
|
||||
// 先校验策略是否存在,不存在则插入
|
||||
dealStrategyData(temp, syncVo);
|
||||
// 新增策略运行数据
|
||||
emsStrategyTempMapper.insertEmsStrategyTemp(temp);
|
||||
break;
|
||||
case "DELETE":
|
||||
JsonNode jsonNode = objectMapper.readTree(content);
|
||||
String tempId = jsonNode.get("templateId").asText();
|
||||
// 删除模版
|
||||
deleteStrategyTempById(tempId);
|
||||
break;
|
||||
default:
|
||||
// 未知操作类型-不同步
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void dealStrategyData(EmsStrategyTemp temp, SyncStrategyTempVo syncVo) {
|
||||
// 主策略
|
||||
String mainStrategyName = syncVo.getStrategyName();
|
||||
EmsStrategy strategy = emsStrategyMapper.getStrategyByName(mainStrategyName);
|
||||
if (strategy != null) {
|
||||
temp.setStrategyId(strategy.getId());
|
||||
} else {
|
||||
strategy = createStrategyEntity(mainStrategyName, syncVo.getStrategyType());
|
||||
emsStrategyMapper.insertEmsStrategy(strategy);
|
||||
temp.setStrategyId(strategy.getId());
|
||||
}
|
||||
}
|
||||
|
||||
private EmsStrategy createStrategyEntity(String strategyName, Long type) {
|
||||
EmsStrategy strategy = new EmsStrategy();
|
||||
strategy.setStrategyType(type);
|
||||
strategy.setStrategyName(strategyName);
|
||||
strategy.setStatus(StrategyStatus.RUNNING.getCode());
|
||||
strategy.setCreateTime(DateUtils.getNowDate());
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,10 +1,17 @@
|
||||
package com.xzzn.ems.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.xzzn.common.enums.StrategyStatus;
|
||||
import com.xzzn.common.utils.DateUtils;
|
||||
import com.xzzn.common.utils.StringUtils;
|
||||
import com.xzzn.common.utils.bean.BeanUtils;
|
||||
import com.xzzn.ems.domain.EmsStrategy;
|
||||
import com.xzzn.ems.domain.vo.StrategyTimeConfigVo;
|
||||
import com.xzzn.ems.domain.vo.SyncStrategyTimeConfigVo;
|
||||
import com.xzzn.ems.mapper.EmsStrategyCurveMapper;
|
||||
import com.xzzn.ems.mapper.EmsStrategyMapper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper;
|
||||
@ -24,6 +31,8 @@ public class EmsStrategyTimeConfigServiceImpl implements IEmsStrategyTimeConfigS
|
||||
private EmsStrategyTimeConfigMapper emsStrategyTimeConfigMapper;
|
||||
@Autowired
|
||||
private EmsStrategyCurveMapper emsStrategyCurveMapper;
|
||||
@Autowired
|
||||
private EmsStrategyMapper emsStrategyMapper;
|
||||
|
||||
/**
|
||||
* 查询时间配置
|
||||
@ -129,4 +138,69 @@ public class EmsStrategyTimeConfigServiceImpl implements IEmsStrategyTimeConfigS
|
||||
{
|
||||
return emsStrategyTimeConfigMapper.getStrategyTimeList(emsStrategyTimeConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dealStrategyTimeData(String content, String operateType) {
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
return;
|
||||
}
|
||||
String siteId = "";
|
||||
EmsStrategyTimeConfig timeConfig = new EmsStrategyTimeConfig();
|
||||
SyncStrategyTimeConfigVo syncVo = JSON.parseObject(content, SyncStrategyTimeConfigVo.class);
|
||||
BeanUtils.copyProperties(syncVo, timeConfig);
|
||||
// 先校验策略是否存在,不存在则插入
|
||||
dealStrategyData(timeConfig, syncVo);
|
||||
switch (operateType) {
|
||||
case "INSERT":
|
||||
// 新增策略运行数据
|
||||
emsStrategyTimeConfigMapper.insertEmsStrategyTimeConfig(timeConfig);
|
||||
break;
|
||||
case "UPDATE":
|
||||
// 根据站点 + strategyId + 月份判断是否存在数据存在则更新否则插入
|
||||
if (checkExistConfig(timeConfig)) {
|
||||
emsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(timeConfig);
|
||||
} else {
|
||||
timeConfig.setCreateTime(DateUtils.getNowDate());
|
||||
emsStrategyTimeConfigMapper.insertEmsStrategyTimeConfig(timeConfig);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
// 未知操作类型-不同步
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean checkExistConfig(EmsStrategyTimeConfig timeConfig) {
|
||||
boolean result = true;
|
||||
String siteId = timeConfig.getSiteId();
|
||||
Long strategyId = timeConfig.getStrategyId();
|
||||
Long month = timeConfig.getMonth();
|
||||
EmsStrategyTimeConfig emsStrategyTimeConfig = emsStrategyTimeConfigMapper.getExistTimeConfig(siteId,strategyId,month);
|
||||
if (emsStrategyTimeConfig == null) {
|
||||
result = false;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void dealStrategyData(EmsStrategyTimeConfig temp, SyncStrategyTimeConfigVo syncVo) {
|
||||
// 主策略
|
||||
String mainStrategyName = syncVo.getStrategyName();
|
||||
EmsStrategy strategy = emsStrategyMapper.getStrategyByName(mainStrategyName);
|
||||
if (strategy != null) {
|
||||
temp.setStrategyId(strategy.getId());
|
||||
} else {
|
||||
strategy = createStrategyEntity(mainStrategyName, syncVo.getStrategyType());
|
||||
emsStrategyMapper.insertEmsStrategy(strategy);
|
||||
temp.setStrategyId(strategy.getId());
|
||||
}
|
||||
}
|
||||
|
||||
private EmsStrategy createStrategyEntity(String strategyName, Long type) {
|
||||
EmsStrategy strategy = new EmsStrategy();
|
||||
strategy.setStrategyType(type);
|
||||
strategy.setStrategyName(strategyName);
|
||||
strategy.setStatus(StrategyStatus.RUNNING.getCode());
|
||||
strategy.setCreateTime(DateUtils.getNowDate());
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,4 +89,9 @@
|
||||
and strategy_type = #{type}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
<select id="getStrategyByName" parameterType="String" resultMap="EmsStrategyResult">
|
||||
<include refid="selectEmsStrategyVo"/>
|
||||
where strategy_name = #{strategyName}
|
||||
</select>
|
||||
</mapper>
|
||||
@ -127,4 +127,11 @@
|
||||
<update id="updateTimeConfigWaitingPost" parameterType="String">
|
||||
update ems_strategy_time_config set isPost = 1 where template_id = #{templateId}
|
||||
</update>
|
||||
|
||||
<select id="getExistTimeConfig" resultMap="EmsStrategyTimeConfigResult">
|
||||
<include refid="selectEmsStrategyTimeConfigVo"/>
|
||||
where site_id = #{siteId}
|
||||
and strategy_id = #{strategyId}
|
||||
and month = #{month}
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user