From e3e3dc1d96165b37db08e7e13a7cb8303b1ee6f3 Mon Sep 17 00:00:00 2001 From: mashili Date: Tue, 22 Jul 2025 22:27:12 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=96=E7=95=A5=E6=9B=B2=E7=BA=BF=E5=9B=BE?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/scheduler/StrategyPoller.java | 109 ++++++++++-------- .../com/xzzn/ems/domain/EmsStrategyCurve.java | 26 +++++ .../ems/domain/EmsStrategyTimeConfig.java | 13 +++ .../xzzn/ems/domain/vo/StrategyCurveVo.java | 53 +++++++-- .../ems/mapper/EmsStrategyCurveMapper.java | 5 + .../impl/EmsStrategyCurveServiceImpl.java | 22 +++- .../impl/EmsStrategyTempServiceImpl.java | 9 ++ .../EmsStrategyTimeConfigServiceImpl.java | 11 ++ .../mapper/ems/EmsStrategyCurveMapper.xml | 24 +++- .../ems/EmsStrategyTimeConfigMapper.xml | 12 +- 10 files changed, 217 insertions(+), 67 deletions(-) diff --git a/ems-framework/src/main/java/com/xzzn/framework/scheduler/StrategyPoller.java b/ems-framework/src/main/java/com/xzzn/framework/scheduler/StrategyPoller.java index a8b19d1..235af73 100644 --- a/ems-framework/src/main/java/com/xzzn/framework/scheduler/StrategyPoller.java +++ b/ems-framework/src/main/java/com/xzzn/framework/scheduler/StrategyPoller.java @@ -1,9 +1,12 @@ package com.xzzn.framework.scheduler; +import com.alibaba.fastjson2.JSON; import com.xzzn.common.utils.DateUtils; import com.xzzn.common.utils.StringUtils; import com.xzzn.ems.domain.EmsStrategyCurve; +import com.xzzn.ems.domain.EmsStrategyTemp; import com.xzzn.ems.domain.EmsStrategyTimeConfig; +import com.xzzn.ems.domain.vo.StrategyPowerDataVo; import com.xzzn.ems.domain.vo.StrategyRunningVo; import com.xzzn.ems.mapper.*; import com.xzzn.framework.manager.ModbusConnectionManager; @@ -20,6 +23,7 @@ import org.springframework.stereotype.Component; import java.time.LocalDate; import java.time.YearMonth; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; @@ -53,8 +57,8 @@ public class StrategyPoller { this.mqttLifecycleManager = mqttLifecycleManager; } // 每1分钟触发(支持cron表达式动态配置) - //@Scheduled(cron = "0 */5 * * * *") - //@Async("strategyTaskExecutor") + @Scheduled(cron = "0 */1 * * * *") + @Async("strategyTaskExecutor") public void pollAllDevices() { logger.info("开始执行策略数据轮询..."); List strategyRunningVoList = emsStrategyRunningMapper.getRunningList(null); @@ -71,69 +75,74 @@ public class StrategyPoller { logger.error("策略下方{}任务失败", strategyVo.getId(), e); } }); - /* - try { - pollSingleDevice(device); - } catch (Exception e) { - logger.error("调度设备{}任务失败", device.getId(), e); - }*/ - /*activeDevices.forEach(device -> { - try { - CompletableFuture.runAsync(() -> processData(device)) - .exceptionally(e -> { - logger.error("设备{}轮询异常", device.getId(), e); - return null; - }); - } catch (Exception e) { - logger.error("调度设备{}任务失败", device.getId(), e); - } - });*/ } // 处理获取到的数据,发到mqtt服务上 private void processData(StrategyRunningVo strategyVo) { logger.info("策略下发数据处理开始"); // 根据运行策略获取主副策略的模板数据 - Long minStrategyId = strategyVo.getMainStrategyId(); + Long mainStrategyId = strategyVo.getMainStrategyId(); Long auxStrategyId = strategyVo.getAuxStrategyId(); String siteId = strategyVo.getSiteId(); // 处理主策略数据 - if (minStrategyId != null && StringUtils.isNotBlank(siteId)) { - // 获取当前策略的所有模板 - List> temps = emsStrategyTempMapper.getTempNameList(minStrategyId,siteId); - if (temps != null && temps.size() > 0) { - for (Map temp : temps) { - String tempId = temp.get("templateId"); - List timeConfigs = emsStrategyTimeConfigMapper.getAllTimeConfigByTempId(tempId); - if (timeConfigs != null && timeConfigs.size() > 0) { - for (EmsStrategyTimeConfig timeConfig : timeConfigs) { - EmsStrategyCurve curve = new EmsStrategyCurve(); - curve.setStrategyId(minStrategyId); - curve.setSiteId(strategyVo.getSiteId()); - curve.setTemplateId(tempId); - curve.setCreateBy("system"); - curve.setCreateTime(DateUtils.getNowDate()); - curve.setUpdateBy("system"); - curve.setUpdateTime(DateUtils.getNowDate()); - // 时间设置 - String[] dateList= dealWithMonth(Integer.parseInt(timeConfig.getMonth().toString())); - curve.setStartDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,dateList[0])); - curve.setEndDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,dateList[1])); - // powerData暂时不处理 + if (mainStrategyId != null && StringUtils.isNotBlank(siteId)) { + dealStrategyCurveData(mainStrategyId, siteId); + } + // 处理副策略数据 + if (auxStrategyId != null && StringUtils.isNotBlank(siteId)) { + dealStrategyCurveData(auxStrategyId, siteId); + } - // 策略数据下发-下方格式暂无 + // 策略数据下发-下方格式暂无 - // 记录推送记录 - emsStrategyCurveMapper.insertEmsStrategyCurve(curve); + logger.info("策略下发结束"); + } + + private void dealStrategyCurveData(Long mainStrategyId, String siteId) { + // 获取当前策略的所有模板 + List> temps = emsStrategyTempMapper.getTempNameList(mainStrategyId,siteId); + if (temps != null && temps.size() > 0) { + for (Map temp : temps) { + String tempId = temp.get("templateId"); + List timeConfigs = emsStrategyTimeConfigMapper.getAllTimeConfigByTempId(tempId); + if (timeConfigs != null && timeConfigs.size() > 0) { + for (EmsStrategyTimeConfig timeConfig : timeConfigs) { + EmsStrategyCurve curve = new EmsStrategyCurve(); + curve.setStrategyId(mainStrategyId); + curve.setSiteId(siteId); + curve.setTemplateId(tempId); + curve.setCreateBy("system"); + curve.setCreateTime(DateUtils.getNowDate()); + curve.setUpdateBy("system"); + curve.setUpdateTime(DateUtils.getNowDate()); + // 时间设置 + int month = Integer.parseInt(timeConfig.getMonth().toString()); + String[] dateList= dealWithMonth(month); + curve.setMonth(Long.valueOf(month)); + curve.setStartDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,dateList[0])); + curve.setEndDate(DateUtils.dateTime(DateUtils.YYYY_MM_DD,dateList[1])); + // powerData-存json格式 + List powerConfig = emsStrategyTempMapper.selectStrategyTempByTempId(tempId); + List powerDataVoList = new ArrayList<>(); + for (int i = 0; i < powerConfig.size(); i++) { + EmsStrategyTemp powerTemp = powerConfig.get(i); + StrategyPowerDataVo powerDataVo = new StrategyPowerDataVo(); + powerDataVo.setPowerData(powerTemp.getChargeDischargePower()); + powerDataVo.setEndTime(DateUtils.parseDateToStr("HH:mm:ss",powerTemp.getEndTime())); + powerDataVo.setStartTime(DateUtils.parseDateToStr("HH:mm:ss",powerTemp.getStartTime())); + powerDataVoList.add(powerDataVo); } + curve.setPowerData(powerDataVoList !=null ? JSON.toJSON(powerDataVoList).toString() : ""); + + // 记录推送记录 + emsStrategyCurveMapper.insertEmsStrategyCurve(curve); + + // 设置已下发 + timeConfig.setIsPost(0); + emsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(timeConfig); } } } } - // 处理副策略数据 - if (auxStrategyId != null && StringUtils.isNotBlank(siteId)) { - - } - logger.info("策略下发结束"); } private String[] dealWithMonth(int month) { diff --git a/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyCurve.java b/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyCurve.java index 8c3bc3a..fbd89f5 100644 --- a/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyCurve.java +++ b/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyCurve.java @@ -50,6 +50,14 @@ public class EmsStrategyCurve extends BaseEntity @Excel(name = "模板id") private String templateId; + /** 月份,1-12 */ + @Excel(name = "月份,1-12") + private Long month; + + /** 是否删除 0-已删除 1-未删除 */ + @Excel(name = "是否删除 0-已删除 1-未删除") + private Long isDelete; + public void setId(Long id) { this.id = id; @@ -128,6 +136,22 @@ public class EmsStrategyCurve extends BaseEntity this.templateId = templateId; } + public Long getMonth() { + return month; + } + + public void setMonth(Long month) { + this.month = month; + } + + public Long getIsDelete() { + return isDelete; + } + + public void setIsDelete(Long isDelete) { + this.isDelete = isDelete; + } + @Override public String toString() { return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) @@ -144,6 +168,8 @@ public class EmsStrategyCurve extends BaseEntity .append("remark", getRemark()) .append("siteId", getSiteId()) .append("templateId", getTemplateId()) + .append("month", getMonth()) + .append("isDelete", getIsDelete()) .toString(); } } diff --git a/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyTimeConfig.java b/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyTimeConfig.java index de02b59..e6a9ff8 100644 --- a/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyTimeConfig.java +++ b/ems-system/src/main/java/com/xzzn/ems/domain/EmsStrategyTimeConfig.java @@ -38,6 +38,10 @@ public class EmsStrategyTimeConfig extends BaseEntity @Excel(name = "模版id") private String templateId; + /** 是否下发 0-已下发 1-未下发 */ + @Excel(name = "是否下发 0-已下发 1-未下发") + private int isPost; + public void setId(Long id) { this.id = id; @@ -98,6 +102,14 @@ public class EmsStrategyTimeConfig extends BaseEntity return templateId; } + public int getIsPost() { + return isPost; + } + + public void setIsPost(int isPost) { + this.isPost = isPost; + } + @Override public String toString() { return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) @@ -112,6 +124,7 @@ public class EmsStrategyTimeConfig extends BaseEntity .append("remark", getRemark()) .append("siteId", getSiteId()) .append("templateId", getTemplateId()) + .append("isPost", getIsPost()) .toString(); } } diff --git a/ems-system/src/main/java/com/xzzn/ems/domain/vo/StrategyCurveVo.java b/ems-system/src/main/java/com/xzzn/ems/domain/vo/StrategyCurveVo.java index 4108c50..2d7959f 100644 --- a/ems-system/src/main/java/com/xzzn/ems/domain/vo/StrategyCurveVo.java +++ b/ems-system/src/main/java/com/xzzn/ems/domain/vo/StrategyCurveVo.java @@ -1,5 +1,6 @@ package com.xzzn.ems.domain.vo; +import com.xzzn.common.annotation.Excel; import com.xzzn.ems.domain.EmsStrategyCurve; import java.util.List; @@ -13,13 +14,39 @@ import java.util.List; public class StrategyCurveVo { + /** 站点id */ + private String siteId; + + /** 关联的策略ID */ + private Long strategyId; + /** 模板名称 */ private String templateName; /** 模板id */ private String templateId; - List cureList; + /** 月份 */ + private Long month; + + /** 模板时间功率 */ + List powerList; + + public String getSiteId() { + return siteId; + } + + public void setSiteId(String siteId) { + this.siteId = siteId; + } + + public Long getStrategyId() { + return strategyId; + } + + public void setStrategyId(Long strategyId) { + this.strategyId = strategyId; + } public String getTemplateName() { return templateName; @@ -29,14 +56,6 @@ public class StrategyCurveVo this.templateName = templateName; } - public List getCureList() { - return cureList; - } - - public void setCureList(List cureList) { - this.cureList = cureList; - } - public String getTemplateId() { return templateId; } @@ -44,4 +63,20 @@ public class StrategyCurveVo public void setTemplateId(String templateId) { this.templateId = templateId; } + + public Long getMonth() { + return month; + } + + public void setMonth(Long month) { + this.month = month; + } + + public List getPowerList() { + return powerList; + } + + public void setPowerList(List powerList) { + this.powerList = powerList; + } } diff --git a/ems-system/src/main/java/com/xzzn/ems/mapper/EmsStrategyCurveMapper.java b/ems-system/src/main/java/com/xzzn/ems/mapper/EmsStrategyCurveMapper.java index 9157524..e7a759e 100644 --- a/ems-system/src/main/java/com/xzzn/ems/mapper/EmsStrategyCurveMapper.java +++ b/ems-system/src/main/java/com/xzzn/ems/mapper/EmsStrategyCurveMapper.java @@ -2,6 +2,7 @@ package com.xzzn.ems.mapper; import java.util.List; import com.xzzn.ems.domain.EmsStrategyCurve; +import org.apache.ibatis.annotations.Param; /** * 策曲线Mapper接口 @@ -65,4 +66,8 @@ public interface EmsStrategyCurveMapper * @return */ public List getTemplateCurve(String templateId); + + public void physicalDeleteCurve(@Param("strategyId")Long strategyId,@Param("siteId")String siteId); + // 根据模版id 删除推送的曲线图数据 + public void physicalDeleteByTemplateId(String templateId); } diff --git a/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyCurveServiceImpl.java b/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyCurveServiceImpl.java index bb5088e..c7e2d43 100644 --- a/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyCurveServiceImpl.java +++ b/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyCurveServiceImpl.java @@ -4,10 +4,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import com.alibaba.fastjson2.JSON; import com.xzzn.common.utils.DateUtils; import com.xzzn.common.utils.StringUtils; import com.xzzn.ems.domain.EmsStrategyTemp; import com.xzzn.ems.domain.vo.StrategyCurveVo; +import com.xzzn.ems.domain.vo.StrategyPowerDataVo; import com.xzzn.ems.mapper.EmsStrategyTempMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -113,17 +115,27 @@ public class EmsStrategyCurveServiceImpl implements IEmsStrategyCurveService if (tempList != null && !tempList.isEmpty()) { for (Map map : tempList) { - StrategyCurveVo vo = new StrategyCurveVo(); String tempName = map.get("templateName"); String tempId = map.get("templateId"); - vo.setTemplateName(tempName); - vo.setTemplateId(tempId); if (StringUtils.isNotEmpty(tempId)) { List taskList = emsStrategyCurveMapper.getTemplateCurve(tempId); - vo.setCureList(taskList); + + for (int i = 0; i < taskList.size(); i++) { + StrategyCurveVo vo = new StrategyCurveVo(); + vo.setTemplateName(tempName); + vo.setTemplateId(tempId); + vo.setStrategyId(taskList.get(i).getStrategyId()); + vo.setSiteId(taskList.get(i).getSiteId()); + vo.setMonth(taskList.get(i).getMonth()); + // 时间功率 + String powerJson = taskList.get(i).getPowerData(); + List powerList = JSON.parseArray(powerJson, StrategyPowerDataVo.class); + vo.setPowerList(powerList); + + dataList.add(vo); + } } - dataList.add(vo); } } return dataList; diff --git a/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTempServiceImpl.java b/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTempServiceImpl.java index 6352059..1c5a3f2 100644 --- a/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTempServiceImpl.java +++ b/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTempServiceImpl.java @@ -8,6 +8,7 @@ import com.xzzn.common.utils.StringUtils; import com.xzzn.common.utils.bean.BeanUtils; import com.xzzn.ems.domain.EmsStrategyTempTimeConfig; import com.xzzn.ems.domain.vo.StrategyTempConfigRequest; +import com.xzzn.ems.mapper.EmsStrategyCurveMapper; import com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -30,6 +31,8 @@ public class EmsStrategyTempServiceImpl implements IEmsStrategyTempService private EmsStrategyTempMapper emsStrategyTempMapper; @Autowired private EmsStrategyTimeConfigMapper emsStrategyTimeConfigMapper; + @Autowired + private EmsStrategyCurveMapper emsStrategyCurveMapper; /** * 查询模板列表 @@ -95,6 +98,9 @@ public class EmsStrategyTempServiceImpl implements IEmsStrategyTempService // 全删 emsStrategyTempMapper.deleteTempByTempId(templateId); + + // 根据模版id 删除推送的曲线图数据 + emsStrategyCurveMapper.physicalDeleteByTemplateId(templateId); // 重新新增 addNewTempAndTimeConfig(requestVo); @@ -111,6 +117,9 @@ public class EmsStrategyTempServiceImpl implements IEmsStrategyTempService public int deleteStrategyTempById(String templateId) { // 先更新配置该模板的月份数据 emsStrategyTimeConfigMapper.cleanTemplateId(templateId); + // 根据模版id 删除推送的曲线图数据 + emsStrategyCurveMapper.physicalDeleteByTemplateId(templateId); + return emsStrategyTempMapper.deleteTempByTempId(templateId); } } diff --git a/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTimeConfigServiceImpl.java b/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTimeConfigServiceImpl.java index 42f9d2b..9a6c42e 100644 --- a/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTimeConfigServiceImpl.java +++ b/ems-system/src/main/java/com/xzzn/ems/service/impl/EmsStrategyTimeConfigServiceImpl.java @@ -2,7 +2,9 @@ package com.xzzn.ems.service.impl; import java.util.List; import com.xzzn.common.utils.DateUtils; +import com.xzzn.common.utils.StringUtils; import com.xzzn.ems.domain.vo.StrategyTimeConfigVo; +import com.xzzn.ems.mapper.EmsStrategyCurveMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.xzzn.ems.mapper.EmsStrategyTimeConfigMapper; @@ -20,6 +22,8 @@ public class EmsStrategyTimeConfigServiceImpl implements IEmsStrategyTimeConfigS { @Autowired private EmsStrategyTimeConfigMapper emsStrategyTimeConfigMapper; + @Autowired + private EmsStrategyCurveMapper emsStrategyCurveMapper; /** * 查询时间配置 @@ -57,6 +61,9 @@ public class EmsStrategyTimeConfigServiceImpl implements IEmsStrategyTimeConfigS if (timeConfigList != null) { for (EmsStrategyTimeConfig strategyTimeConfig : timeConfigList) { Long id = strategyTimeConfig.getId(); + Long strategyId = strategyTimeConfig.getStrategyId(); + String siteId = strategyTimeConfig.getSiteId(); + strategyTimeConfig.setIsPost(1);//默认未下发 // 新增 if (id == null) { strategyTimeConfig.setCreateTime(DateUtils.getNowDate()); @@ -65,6 +72,10 @@ public class EmsStrategyTimeConfigServiceImpl implements IEmsStrategyTimeConfigS strategyTimeConfig.setUpdateTime(DateUtils.getNowDate()); emsStrategyTimeConfigMapper.updateEmsStrategyTimeConfig(strategyTimeConfig); } + // 处理曲线图:物理删除该策略的曲线图 + if (StringUtils.isNotEmpty(siteId) && strategyId != null) { + emsStrategyCurveMapper.physicalDeleteCurve(strategyId,siteId); + } } } else { return false; diff --git a/ems-system/src/main/resources/mapper/ems/EmsStrategyCurveMapper.xml b/ems-system/src/main/resources/mapper/ems/EmsStrategyCurveMapper.xml index 1939ca5..c651646 100644 --- a/ems-system/src/main/resources/mapper/ems/EmsStrategyCurveMapper.xml +++ b/ems-system/src/main/resources/mapper/ems/EmsStrategyCurveMapper.xml @@ -18,10 +18,12 @@ + + - select id, strategy_id, task_number, start_date, end_date, power_data, create_by, create_time, update_by, update_time, remark, site_id, template_id from ems_strategy_curve + select id, strategy_id, task_number, start_date, end_date, power_data, create_by, create_time, update_by, update_time, remark, site_id, template_id, month, isDelete from ems_strategy_curve @@ -57,6 +61,8 @@ remark, site_id, template_id, + month, + isDelete, #{strategyId}, @@ -71,6 +77,8 @@ #{remark}, #{siteId}, #{templateId}, + #{month}, + #{isDelete}, @@ -89,6 +97,8 @@ remark = #{remark}, site_id = #{siteId}, template_id = #{templateId}, + month = #{month}, + isDelete = #{isDelete}, where id = #{id} @@ -107,6 +117,18 @@ + + + update ems_strategy_curve set isDelete = 0 + where site_id = #{siteId} + and strategy_id = #{strategyId} + + + + update ems_strategy_curve set isDelete = 0 + where template_id = #{templateId} + \ No newline at end of file diff --git a/ems-system/src/main/resources/mapper/ems/EmsStrategyTimeConfigMapper.xml b/ems-system/src/main/resources/mapper/ems/EmsStrategyTimeConfigMapper.xml index 701e00f..4ee7edb 100644 --- a/ems-system/src/main/resources/mapper/ems/EmsStrategyTimeConfigMapper.xml +++ b/ems-system/src/main/resources/mapper/ems/EmsStrategyTimeConfigMapper.xml @@ -16,10 +16,11 @@ + - select id, strategy_id, month, charge_discharge_mode, create_by, create_time, update_by, update_time, remark, site_id, template_id from ems_strategy_time_config + select id, strategy_id, month, charge_discharge_mode, create_by, create_time, update_by, update_time, remark, site_id, template_id, isPost from ems_strategy_time_config @@ -51,6 +53,7 @@ remark, site_id, template_id, + isPost, #{strategyId}, @@ -63,6 +66,7 @@ #{remark}, #{siteId}, #{templateId}, + #{isPost}, @@ -79,6 +83,7 @@ remark = #{remark}, site_id = #{siteId}, template_id = #{templateId}, + isPost = #{isPost}, where id = #{id} @@ -99,7 +104,9 @@ t.month, t.charge_discharge_mode as chargeDischargeMode, t.template_id as templateId, - temp.template_name as templateName + temp.template_name as templateName, + t.site_id as siteId, + t.strategy_id as strategyId from ems_strategy_time_config t LEFT JOIN ems_strategy_temp temp on t.template_id = temp.template_id where t.site_id = #{siteId} @@ -114,5 +121,6 @@ \ No newline at end of file