dev #2

Merged
dashixiong merged 349 commits from dev into main 2026-02-11 01:55:46 +00:00
260 changed files with 30609 additions and 1041 deletions
Showing only changes of commit b861ad7593 - Show all commits

View File

@ -133,11 +133,11 @@ public class MqttMessageController implements MqttPublisher, MqttSubscriber {
}
private static final long FORCE_INTERVAL = 60 * 1000; // 1分钟毫秒
/*private static final long FORCE_INTERVAL = 60 * 1000; // 1分钟毫秒
@Scheduled(fixedRate = FORCE_INTERVAL) // 每分钟执行一次
public void scheduledForceSave() {
System.out.println("执行定时强制存储任务:" + new Date());
}
}*/
}

View File

@ -72,4 +72,10 @@ public class RedisKeyConstants
/** 现有的告警数据 */
public static final String LATEST_ALARM_RECORD = "LATEST_ALARM_RECORD";
/** fx-电表电量差值起始数据 */
public static final String DIFF_CHARGE_START = "diff_charge_start_";
/** 预存电价时间配置 */
public static final String ENERGY_PRICE_TIME = "energy_price_time_";
}

View File

@ -218,7 +218,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
}
/**
* 获取昨天的日期格式为yyyy-MM-dd
* 获取昨天的日期格式为yyyy-MM-dd 23:59:59
* @return 昨天的日期字符串
*/
public static String getYesterdayDate() {
@ -226,8 +226,10 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
LocalDate today = LocalDate.now();
// 减去一天得到昨天
LocalDate yesterday = today.minusDays(1);
// 将日期转换为LocalDateTime并设置时间为23:59:59
LocalDateTime yesterdayEndTime = yesterday.atTime(23, 59, 59);
// 定义日期格式化器
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDD);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
// 格式化并返回
return yesterday.format(formatter);
}
@ -372,4 +374,23 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
.toLocalDateTime();
return dateTime;
}
// 时间戳转时间
public static Date convertUpdateTime(Long updateTime) {
if (updateTime == null) {
return null;
}
// 兼容10位秒级和13位毫秒级
int length = String.valueOf(updateTime).length();
if (length == 10) { // 10位秒级 -> 转换为毫秒级
updateTime *= 1000;
} else if (length != 13) { // 既不是10位也不是13位视为非法
System.err.println("时间戳格式错误必须是10位(秒)或13位(毫秒)");
return null;
}
// 3. 转换为Date
return new Date(updateTime);
}
}

View File

@ -4,6 +4,7 @@ import com.ghgande.j2mod.modbus.net.TCPMasterConnection;
import com.xzzn.ems.domain.EmsDevicesSetting;
import com.xzzn.ems.mapper.EmsDevicesSettingMapper;
import com.xzzn.ems.service.IEmsAlarmRecordsService;
import com.xzzn.ems.service.IEmsEnergyPriceConfigService;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -38,6 +39,8 @@ public class ModbusConnectionManager implements ApplicationRunner {
private EmsDevicesSettingMapper deviceRepo;
@Autowired
private IEmsAlarmRecordsService iEmsAlarmRecordsService;
@Autowired
private IEmsEnergyPriceConfigService iEmsEnergyPriceConfigService;
@Override
public void run(ApplicationArguments args) throws Exception {
@ -57,6 +60,8 @@ public class ModbusConnectionManager implements ApplicationRunner {
private void initData() {
// 初始化-告警数据
iEmsAlarmRecordsService.initAlarmMatchInfo();
// 初始化当月电价
iEmsEnergyPriceConfigService.initCurrentMonthPrice();
}
/**
* 获取连接(带自动创建和缓存)

View File

@ -0,0 +1,211 @@
package com.xzzn.ems.domain;
import java.math.BigDecimal;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.xzzn.common.core.domain.BaseEntity;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.xzzn.common.annotation.Excel;
/**
* 站点电每日数据对象 ems_daily_energy_data
*
* @author xzzn
* @date 2025-10-09
*/
public class EmsDailyEnergyData extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** */
private Long id;
/** 站点id */
@Excel(name = "站点id")
private String siteId;
/** 数据日期:yyyy-MM-dd */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "数据日期:yyyy-MM-dd", width = 30, dateFormat = "yyyy-MM-dd")
private Date dataDate;
/** 尖峰时段充电差值 */
@Excel(name = "尖峰时段充电差值")
private BigDecimal peakChargeDiff;
/** 尖峰时段放电差值 */
@Excel(name = "尖峰时段放电差值")
private BigDecimal peakDischargeDiff;
/** 峰时时段充电差值 */
@Excel(name = "峰时时段充电差值")
private BigDecimal highChargeDiff;
/** 峰时时段放电差值 */
@Excel(name = "峰时时段放电差值")
private BigDecimal highDischargeDiff;
/** 平时时段充电差值 */
@Excel(name = "平时时段充电差值")
private BigDecimal flatChargeDiff;
/** 平时时段放电差值 */
@Excel(name = "平时时段放电差值")
private BigDecimal flatDischargeDiff;
/** 谷时时段充电差值 */
@Excel(name = "谷时时段充电差值")
private BigDecimal valleyChargeDiff;
/** 谷时时段放电差值 */
@Excel(name = "谷时时段放电差值")
private BigDecimal valleyDischargeDiff;
/** 差值计算时间如2025-10-10 23:59:00 */
@Excel(name = "差值计算时间", readConverterExp = "如=2025-10-10,2=3:59:00")
private Date calcTime;
public void setId(Long id)
{
this.id = id;
}
public Long getId()
{
return id;
}
public void setSiteId(String siteId)
{
this.siteId = siteId;
}
public String getSiteId()
{
return siteId;
}
public void setDataDate(Date dataDate)
{
this.dataDate = dataDate;
}
public Date getDataDate()
{
return dataDate;
}
public void setPeakChargeDiff(BigDecimal peakChargeDiff)
{
this.peakChargeDiff = peakChargeDiff;
}
public BigDecimal getPeakChargeDiff()
{
return peakChargeDiff;
}
public void setPeakDischargeDiff(BigDecimal peakDischargeDiff)
{
this.peakDischargeDiff = peakDischargeDiff;
}
public BigDecimal getPeakDischargeDiff()
{
return peakDischargeDiff;
}
public void setHighChargeDiff(BigDecimal highChargeDiff)
{
this.highChargeDiff = highChargeDiff;
}
public BigDecimal getHighChargeDiff()
{
return highChargeDiff;
}
public void setHighDischargeDiff(BigDecimal highDischargeDiff)
{
this.highDischargeDiff = highDischargeDiff;
}
public BigDecimal getHighDischargeDiff()
{
return highDischargeDiff;
}
public void setFlatChargeDiff(BigDecimal flatChargeDiff)
{
this.flatChargeDiff = flatChargeDiff;
}
public BigDecimal getFlatChargeDiff()
{
return flatChargeDiff;
}
public void setFlatDischargeDiff(BigDecimal flatDischargeDiff)
{
this.flatDischargeDiff = flatDischargeDiff;
}
public BigDecimal getFlatDischargeDiff()
{
return flatDischargeDiff;
}
public void setValleyChargeDiff(BigDecimal valleyChargeDiff)
{
this.valleyChargeDiff = valleyChargeDiff;
}
public BigDecimal getValleyChargeDiff()
{
return valleyChargeDiff;
}
public void setValleyDischargeDiff(BigDecimal valleyDischargeDiff)
{
this.valleyDischargeDiff = valleyDischargeDiff;
}
public BigDecimal getValleyDischargeDiff()
{
return valleyDischargeDiff;
}
public void setCalcTime(Date calcTime)
{
this.calcTime = calcTime;
}
public Date getCalcTime()
{
return calcTime;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("id", getId())
.append("siteId", getSiteId())
.append("dataDate", getDataDate())
.append("peakChargeDiff", getPeakChargeDiff())
.append("peakDischargeDiff", getPeakDischargeDiff())
.append("highChargeDiff", getHighChargeDiff())
.append("highDischargeDiff", getHighDischargeDiff())
.append("flatChargeDiff", getFlatChargeDiff())
.append("flatDischargeDiff", getFlatDischargeDiff())
.append("valleyChargeDiff", getValleyChargeDiff())
.append("valleyDischargeDiff", getValleyDischargeDiff())
.append("calcTime", getCalcTime())
.append("createBy", getCreateBy())
.append("createTime", getCreateTime())
.append("updateBy", getUpdateBy())
.append("updateTime", getUpdateTime())
.append("remark", getRemark())
.toString();
}
}

View File

@ -0,0 +1,67 @@
package com.xzzn.ems.mapper;
import java.util.List;
import com.xzzn.ems.domain.EmsDailyEnergyData;
import org.apache.ibatis.annotations.Param;
/**
* 站点电每日数据Mapper接口
*
* @author xzzn
* @date 2025-10-09
*/
public interface EmsDailyEnergyDataMapper
{
/**
* 查询站点电每日数据
*
* @param id 站点电每日数据主键
* @return 站点电每日数据
*/
public EmsDailyEnergyData selectEmsDailyEnergyDataById(Long id);
/**
* 查询站点电每日数据列表
*
* @param emsDailyEnergyData 站点电每日数据
* @return 站点电每日数据集合
*/
public List<EmsDailyEnergyData> selectEmsDailyEnergyDataList(EmsDailyEnergyData emsDailyEnergyData);
/**
* 新增站点电每日数据
*
* @param emsDailyEnergyData 站点电每日数据
* @return 结果
*/
public int insertEmsDailyEnergyData(EmsDailyEnergyData emsDailyEnergyData);
/**
* 修改站点电每日数据
*
* @param emsDailyEnergyData 站点电每日数据
* @return 结果
*/
public int updateEmsDailyEnergyData(EmsDailyEnergyData emsDailyEnergyData);
/**
* 删除站点电每日数据
*
* @param id 站点电每日数据主键
* @return 结果
*/
public int deleteEmsDailyEnergyDataById(Long id);
/**
* 批量删除站点电每日数据
*
* @param ids 需要删除的数据主键集合
* @return 结果
*/
public int deleteEmsDailyEnergyDataByIds(Long[] ids);
// 获取站点某日电表数据
public EmsDailyEnergyData getDataByDate(@Param("siteId")String siteId,@Param("today") String today);
// 插入或更新每日尖峰平谷差值
public void insertOrUpdateData(EmsDailyEnergyData energyData);
}

View File

@ -3,6 +3,7 @@ package com.xzzn.ems.mapper;
import java.util.List;
import com.xzzn.ems.domain.EmsEnergyPriceConfig;
import com.xzzn.ems.domain.vo.EnergyPriceTimeRange;
import org.apache.ibatis.annotations.Param;
/**
@ -66,4 +67,6 @@ public interface EmsEnergyPriceConfigMapper
public EmsEnergyPriceConfig getConfigListByYearAndMonth(@Param("currentYear") String currentYear, @Param("month")String month);
// 插入或更新
public void insertOrUpdateEmsEnergyPriceConfig(EmsEnergyPriceConfig priceConfig);
// 获取指定年月的电价时间配置
public List<EnergyPriceTimeRange> getTimeRangeByDate(@Param("currentYear")int currentYear, @Param("currentMonth")int currentMonth);
}

View File

@ -58,4 +58,7 @@ public interface IEmsEnergyPriceConfigService
* @return 结果
*/
public int deleteEmsEnergyPriceConfigById(Long id);
// 初始化当月电价
public void initCurrentMonthPrice();
}

View File

@ -77,6 +77,8 @@ public class DDSDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
private EmsDailyChargeDataMapper emsDailyChargeDataMapper;
@Autowired
private IEmsAlarmRecordsService iEmsAlarmRecordsService;
@Autowired
private EmsDailyEnergyDataMapper emsDailyEnergyDataMapper;
public DDSDataProcessServiceImpl(ObjectMapper objectMapper) {
super(objectMapper);
@ -926,31 +928,32 @@ public class DDSDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
dealDDSDailyChargeDate(obj,deviceId);
}
private EmsDailyEnergyData initEnergyData() {
EmsDailyEnergyData energyData = new EmsDailyEnergyData();
energyData.setSiteId(SITE_ID);
energyData.setDataDate(DateUtils.getNowDate());
energyData.setCreateBy("system");
energyData.setCreateTime(DateUtils.getNowDate());
energyData.setUpdateBy("system");
energyData.setUpdateTime(DateUtils.getNowDate());
return energyData;
}
private void dealDDSDailyChargeDate(Map<String, Object> obj, String deviceId) {
log.info("start dealDDSDailyChargeDate");
// 初始化今日充放电
BigDecimal dailyDisChargeDate = new BigDecimal(0);
BigDecimal dailyChargeDate = new BigDecimal(0);
BigDecimal nowTotalDisChargeDate = StringUtils.getBigDecimal(obj.get("DQFXZYGDN"));
BigDecimal nowTotalChargeDate = StringUtils.getBigDecimal(obj.get("DQZXZYGDN"));
// 初始化当日数据
EmsDailyChargeData emsDailyChargeData = new EmsDailyChargeData();
emsDailyChargeData.setSiteId(SITE_ID);
emsDailyChargeData.setDeviceId(deviceId);
emsDailyChargeData.setDateTime(DateUtils.getNowDate());
emsDailyChargeData.setTotalChargeData(nowTotalChargeDate);
emsDailyChargeData.setTotalDischargeData(nowTotalDisChargeDate);
emsDailyChargeData.setCreateBy("system");
emsDailyChargeData.setCreateTime(DateUtils.getNowDate());
emsDailyChargeData.setUpdateBy("system");
emsDailyChargeData.setUpdateTime(DateUtils.getNowDate());
// 初始化当日数据-总的
EmsDailyChargeData emsDailyChargeData = initDailyChargeData(deviceId,nowTotalChargeDate,nowTotalDisChargeDate);
// 获取redis存放昨日最晚数据
String yestData = DateUtils.getYesterdayDate();
String yestDateRedisKey = RedisKeyConstants.AMMETER + SITE_ID + "_" + deviceId + "_" + yestData;
EmsAmmeterData yestDate = redisCache.getCacheObject(yestDateRedisKey);
if (yestDate == null) {
// redis没有这查电表总数据表
// redis没有这查电表总数据表取截止到昨日最新第一条数据
yestDate = emsAmmeterDataMapper.getYestLatestDate(SITE_ID,deviceId,yestData);
// 数据存redis-有效期1天
redisCache.setCacheObject(yestDateRedisKey, yestDate , Constants.DATE_VALID_TIME, TimeUnit.DAYS);
@ -968,7 +971,44 @@ public class DDSDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
// 插入或更新每日充放电数据表
emsDailyChargeDataMapper.insertOrUpdateData(emsDailyChargeData);
log.info("end dealDDSDailyChargeDate");
// 初始化数据-尖峰平谷
EmsDailyEnergyData energyData = initEnergyData();
// 计算尖峰平谷差值
energyData.setPeakChargeDiff(StringUtils.getBigDecimal(obj.get("DQZXYGJDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentForwardActivePeak()));// 正向-尖
energyData.setHighChargeDiff(StringUtils.getBigDecimal(obj.get("DQZXYGFDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentForwardActiveHigh()));// 正向-峰
energyData.setFlatChargeDiff(StringUtils.getBigDecimal(obj.get("DQZXYGPDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentForwardActiveFlat()));// 正向-平
energyData.setValleyChargeDiff(StringUtils.getBigDecimal(obj.get("DQZXYGGDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentForwardActiveValley()));// 正向-谷
energyData.setPeakDischargeDiff(StringUtils.getBigDecimal(obj.get("DQFXYGJDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentReverseActivePeak()));// 反向-尖
energyData.setHighDischargeDiff(StringUtils.getBigDecimal(obj.get("DQFXYGFDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentReverseActiveHigh()));// 反向-峰
energyData.setFlatDischargeDiff(StringUtils.getBigDecimal(obj.get("DQFXYGPDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentReverseActiveFlat()));// 反向-平
energyData.setValleyDischargeDiff(StringUtils.getBigDecimal(obj.get("DQFXYGGDN"))
.subtract(yestDate == null ? new BigDecimal(0) : yestDate.getCurrentReverseActiveValley()));// 反向-谷
energyData.setCalcTime(DateUtils.getNowDate());
// 插入或更新电表每日差值数据表
emsDailyEnergyDataMapper.insertOrUpdateData(energyData);
}
private EmsDailyChargeData initDailyChargeData(String deviceId, BigDecimal nowTotalChargeDate,
BigDecimal nowTotalDisChargeDate) {
EmsDailyChargeData emsDailyChargeData = new EmsDailyChargeData();
emsDailyChargeData.setSiteId(SITE_ID);
emsDailyChargeData.setDeviceId(deviceId);
emsDailyChargeData.setDateTime(DateUtils.getNowDate());
emsDailyChargeData.setTotalChargeData(nowTotalChargeDate);
emsDailyChargeData.setTotalDischargeData(nowTotalDisChargeDate);
emsDailyChargeData.setCreateBy("system");
emsDailyChargeData.setCreateTime(DateUtils.getNowDate());
emsDailyChargeData.setUpdateBy("system");
emsDailyChargeData.setUpdateTime(DateUtils.getNowDate());
return emsDailyChargeData;
}
// 数据分组处理

View File

@ -3,7 +3,10 @@ package com.xzzn.ems.service.impl;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import com.xzzn.common.constant.RedisKeyConstants;
import com.xzzn.common.core.redis.RedisCache;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.EmsPriceTimeConfig;
@ -30,6 +33,8 @@ public class EmsEnergyPriceConfigServiceImpl implements IEmsEnergyPriceConfigSer
private EmsEnergyPriceConfigMapper emsEnergyPriceConfigMapper;
@Autowired
private EmsPriceTimeConfigMapper emsPriceTimeConfigMapper;
@Autowired
private RedisCache redisCache;
/**
* 查询电价配置
@ -195,4 +200,19 @@ public class EmsEnergyPriceConfigServiceImpl implements IEmsEnergyPriceConfigSer
{
return emsEnergyPriceConfigMapper.deleteEmsEnergyPriceConfigById(id);
}
/**
* 初始化当月的电价
*/
@Override
public void initCurrentMonthPrice() {
// 获取当年和当月的电价
int currentMonth = LocalDate.now().getMonthValue();
int currentYear = LocalDate.now().getYear();
List<EnergyPriceTimeRange> timeRanges = emsEnergyPriceConfigMapper.getTimeRangeByDate(currentYear,currentMonth);
String key = RedisKeyConstants.ENERGY_PRICE_TIME + currentYear + currentMonth;
redisCache.setCacheObject(key,timeRanges, 31, TimeUnit.DAYS);
}
}

View File

@ -13,20 +13,26 @@ import com.xzzn.common.enums.*;
import com.xzzn.common.utils.DateUtils;
import com.xzzn.common.utils.StringUtils;
import com.xzzn.ems.domain.*;
import com.xzzn.ems.domain.vo.EnergyPriceTimeRange;
import com.xzzn.ems.mapper.*;
import com.xzzn.ems.service.IEmsAlarmRecordsService;
import com.xzzn.ems.service.IFXXDataProcessService;
import com.xzzn.ems.utils.AbstractBatteryDataProcessor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service
@ -69,6 +75,10 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
private IEmsAlarmRecordsService iEmsAlarmRecordsService;
@Autowired
private EmsCoolingDataMapper emsCoolingDataMapper;
@Autowired
private EmsDailyEnergyDataMapper emsDailyEnergyDataMapper;
@Autowired
private EmsEnergyPriceConfigMapper emsEnergyPriceConfigMapper;
// 构造方法(调用父类构造)
public FXXDataProcessServiceImpl(ObjectMapper objectMapper) {
@ -84,6 +94,8 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
String deviceId = obj.get("Device").toString();
String jsonData = obj.get("Data").toString();
Long timestamp = Long.valueOf(obj.get("timestamp").toString());
Date dataUpdateTime = DateUtils.convertUpdateTime(timestamp);
log.info("deviceId:" + deviceId);
boolean isEmpty = checkJsonDataEmpty(jsonData);
@ -102,16 +114,16 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
} else if (deviceId.contains("BMSC")) {
log.info("BMSC data:"+ jsonData);
batteryClusterDataProcess(deviceId, jsonData);
batteryDataProcess(deviceId, jsonData);
batteryDataProcess(deviceId, jsonData,dataUpdateTime);
} else if (deviceId.contains("PCS")) {
pcsDataProcess(deviceId, jsonData);
pcsDataProcess(deviceId, jsonData,dataUpdateTime);
pcsBranchDataProcess(deviceId, jsonData);
dealFXXDailyChargeDate(deviceId, jsonData);
} else if (deviceId.contains("LOAD")) {
loadDataProcess(deviceId, jsonData);
loadDataProcess(deviceId, jsonData, dataUpdateTime);
} else if (deviceId.contains("METE")) {
meteDataProcess(deviceId, jsonData);
meteDataProcess(deviceId, jsonData, dataUpdateTime);
} else if (deviceId.contains("donghuan")) {
dhDataProcess(deviceId, jsonData);
} else if (deviceId.contains("ZSLQ")) {
@ -330,7 +342,7 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
}
private void batteryDataProcess(String deviceId, String dataJson) {
private void batteryDataProcess(String deviceId, String dataJson, Date dataUpdateTime) {
EmsDevicesSetting joken = new EmsDevicesSetting();
joken.setDeviceId(deviceId);
List<EmsDevicesSetting> up = emsDevicesSettingMapper.selectEmsDevicesSettingList(joken);
@ -367,7 +379,7 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
batteryData.setBatteryPack(stackDeviceId);
// 时间戳
batteryData.setDataTimestamp(new Date());
batteryData.setDataTimestamp(dataUpdateTime);
// 系统管理字段
batteryData.setCreateBy("system");
batteryData.setCreateTime(DateUtils.getNowDate());
@ -413,13 +425,13 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
}
}
private void pcsDataProcess(String deviceId, String dataJson) {
private void pcsDataProcess(String deviceId, String dataJson, Date dataUpdateTime) {
Map<String, Object> obj = JSON.parseObject(dataJson, new TypeReference<Map<String, Object>>() {
});
//pcs
EmsPcsData pcsData = new EmsPcsData();
// 时间与状态类字段
pcsData.setDataUpdateTime(new Date());
pcsData.setDataUpdateTime(dataUpdateTime);
pcsData.setWorkStatus(WorkStatus.NORMAL.getCode());
pcsData.setGridStatus(GridStatus.GRID.getCode());
pcsData.setDeviceStatus(DeviceStatus.ONLINE.getCode());
@ -541,7 +553,7 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
}
private void loadDataProcess(String deviceId, String dataJson) {
private void loadDataProcess(String deviceId, String dataJson, Date dataUpdateTime) {
//总表
Map<String, Object> obj = JSON.parseObject(dataJson, new TypeReference<Map<String, Object>>() {
@ -549,7 +561,7 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
EmsAmmeterData dataLoad = new EmsAmmeterData();
// 更新时间
dataLoad.setDataUpdateTime(new Date());
dataLoad.setDataUpdateTime(dataUpdateTime);
// 电能设置-组合有功
dataLoad.setCurrentCombActiveTotal(StringUtils.getBigDecimal(obj.get("DQZHYGZDN")));
@ -664,7 +676,7 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
log.info("end dealFXXDailyChargeDate");
}
private void meteDataProcess(String deviceId, String dataJson) {
private void meteDataProcess(String deviceId, String dataJson, Date dataUpdateTime) {
//总表
Map<String, Object> obj = JSON.parseObject(dataJson, new TypeReference<Map<String, Object>>() {
@ -672,7 +684,7 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
EmsAmmeterData dataLoad = new EmsAmmeterData();
// 更新时间
dataLoad.setDataUpdateTime(new Date());
dataLoad.setDataUpdateTime(dataUpdateTime);
// 电压+电流
dataLoad.setPhaseAVoltage(StringUtils.getBigDecimal(obj.get("AXDY")));
@ -756,7 +768,148 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
emsAmmeterDataMapper.insertEmsAmmeterData(dataLoad);
redisCache.setCacheObject(RedisKeyConstants.AMMETER + SITE_ID + "_" +deviceId, dataLoad);
// 处理电表每日充放电数据
dealAmmeterDailyDate(obj, deviceId, dataUpdateTime);
}
private void dealAmmeterDailyDate(Map<String, Object> obj, String deviceId, Date dataUpdateTime) {
// 先获取当月电价配置redis没有这查数据库都没有则返回
String priceKey = RedisKeyConstants.ENERGY_PRICE_TIME + LocalDate.now().getYear() + LocalDate.now().getMonthValue();
List<EnergyPriceTimeRange> timeRanges = redisCache.getCacheObject(priceKey);
if (timeRanges == null) {
timeRanges = emsEnergyPriceConfigMapper.getTimeRangeByDate(LocalDate.now().getYear(),LocalDate.now().getMonthValue());
if (timeRanges == null) {
return;
}
redisCache.setCacheObject(priceKey,timeRanges, 31, TimeUnit.DAYS);
}
// 根据时间范围判断数据类型(尖峰平谷),无法确定数据类型则不处理
String costType = "";
String startTime = "";
for (EnergyPriceTimeRange timeRange : timeRanges) {
startTime = timeRange.getStartTime();
if (isInPriceTimeRange(startTime,timeRange.getEndTime(),dataUpdateTime)) {
costType = timeRange.getCostType();
break;
}
}
if (StringUtils.isEmpty(costType)) {
return;
}
// 确定数据类型,获取小于等于开始时刻的最新数据。
startTime = concateCurrentDayTime(startTime);
String startTimeDataKey = RedisKeyConstants.DIFF_CHARGE_START + SITE_ID +"_" + startTime;
Map<String, BigDecimal> redisHash = redisCache.getCacheObject(startTimeDataKey);
// redis无就取数据库
if (redisHash == null) {
// 查询数据库-电表表数据取截止到昨日最新一条数据均无默认初始值0
EmsAmmeterData ammeterData = emsAmmeterDataMapper.getYestLatestDate(SITE_ID,deviceId,startTime);
redisHash = new HashMap<>();
redisHash.put("charge", ammeterData == null ?
new BigDecimal(0) : ammeterData.getCurrentForwardActiveTotal());
redisHash.put("discharge", ammeterData == null ?
new BigDecimal(0) : ammeterData.getCurrentReverseActiveTotal());
redisCache.setCacheObject(startTimeDataKey, redisHash, 24, TimeUnit.HOURS);
}
//初始化电表每日差值对象
EmsDailyEnergyData energyData = initEnergyData();
// 根据 costType 计算差值
setDiffByCostType(costType,energyData,redisHash,obj);
// 插入或更新电表每日差值数据表
emsDailyEnergyDataMapper.insertOrUpdateData(energyData);
}
private void setDiffByCostType(String costType, EmsDailyEnergyData energyData, Map<String, BigDecimal> redisHash, Map<String, Object> obj) {
BigDecimal currentChargeData = StringUtils.getBigDecimal(obj.get("ZXYGDN"));
BigDecimal currentDischargeData = StringUtils.getBigDecimal(obj.get("FXYGDN"));
currentChargeData = currentChargeData != null ? currentChargeData : BigDecimal.ZERO;
currentDischargeData = currentDischargeData != null ? currentDischargeData : BigDecimal.ZERO;
// 计算差值,根据类型累加
BigDecimal chargeDiffData = currentChargeData.subtract(redisHash.getOrDefault("charge", BigDecimal.ZERO));
BigDecimal disChargeDiffData = currentDischargeData.subtract(redisHash.getOrDefault("discharge", BigDecimal.ZERO));
switch (costType) {
case "peak":
BigDecimal peakCharge = energyData.getPeakChargeDiff();
energyData.setPeakChargeDiff((peakCharge == null ? BigDecimal.ZERO : peakCharge)
.add(chargeDiffData));
BigDecimal peakDischarge = energyData.getPeakDischargeDiff();
energyData.setPeakDischargeDiff((peakDischarge == null ? BigDecimal.ZERO : peakDischarge)
.add(disChargeDiffData));
break;
case "high":
BigDecimal highCharge = energyData.getHighChargeDiff();
energyData.setHighChargeDiff((highCharge == null ? BigDecimal.ZERO : highCharge)
.add(chargeDiffData));
BigDecimal highDischarge = energyData.getHighDischargeDiff();
energyData.setHighDischargeDiff((highDischarge == null ? BigDecimal.ZERO : highDischarge)
.add(disChargeDiffData));
break;
case "flat":
BigDecimal flatCharge = energyData.getFlatChargeDiff();
energyData.setFlatChargeDiff((flatCharge == null ? BigDecimal.ZERO : flatCharge)
.add(chargeDiffData));
BigDecimal flatDischarge = energyData.getFlatDischargeDiff();
energyData.setFlatDischargeDiff((flatDischarge == null ? BigDecimal.ZERO : flatDischarge)
.add(disChargeDiffData));
break;
case "valley":
BigDecimal valleyCharge = energyData.getValleyChargeDiff();
energyData.setValleyChargeDiff((valleyCharge == null ? BigDecimal.ZERO : valleyCharge)
.add(chargeDiffData));
BigDecimal valleyDischarge = energyData.getValleyDischargeDiff();
energyData.setValleyDischargeDiff((valleyDischarge == null ? BigDecimal.ZERO : valleyDischarge)
.add(disChargeDiffData));
break;
default:
return;
}
}
private boolean isInPriceTimeRange(String startTime, String endTime, Date dataUpdateTime) {
if (startTime == null || endTime == null || dataUpdateTime == null) {
return false;
}
LocalDateTime time = DateUtils.toLocalDateTime(dataUpdateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
String dataTimeStr = time.format(formatter);
// 比较时间范围
return dataTimeStr.compareTo(startTime) >= 0
&& dataTimeStr.compareTo(endTime) < 0;
}
private String concateCurrentDayTime(String startTime) {
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime time = LocalTime.parse(startTime, timeFormatter);
LocalDate today = LocalDate.now();
LocalDateTime targetDateTime = LocalDateTime.of(today, time);
DateTimeFormatter resultFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
return targetDateTime.format(resultFormatter);
}
private EmsDailyEnergyData initEnergyData() {
// 先获取数据库当天数据,存在则更新时间,不存在则初始化
EmsDailyEnergyData energyData = emsDailyEnergyDataMapper.getDataByDate(SITE_ID,DateUtils.getDate());
if (energyData == null) {
energyData = new EmsDailyEnergyData();
energyData.setSiteId(SITE_ID);
energyData.setDataDate(DateUtils.getNowDate());
energyData.setCreateBy("system");
energyData.setCreateTime(DateUtils.getNowDate());
}
energyData.setUpdateBy("system");
energyData.setCalcTime(DateUtils.getNowDate());
return energyData;
}
// 数据分组处理
private static Map<String, Map<String, Object>> processData(Map<String, Object> rawData) {
Map<String, Map<String, Object>> records = new HashMap<>();

View File

@ -885,11 +885,23 @@
</select>
<select id="getYestLatestDate" resultMap="EmsAmmeterDataResult">
<include refid="selectEmsAmmeterDataVo"/>
where site_id = #{siteId}
SELECT
t.current_forward_active_total,
t.current_reverse_active_total,
t.current_forward_active_peak,
t.current_forward_active_high,
t.current_forward_active_flat,
t.current_forward_active_valley,
t.current_reverse_active_peak,
t.current_reverse_active_high,
t.current_reverse_active_flat,
t.current_reverse_active_valley
FROM ems_ammeter_data t
WHERE site_id = #{siteId}
and device_id = #{deviceId}
and DATE(data_update_time) = #{yestData}
order by data_update_time desc limit 1
AND data_update_time &lt;= #{yestData}
ORDER BY data_update_time DESC
LIMIT 1
</select>
<select id="getMonthlyTimeRanges" resultType="com.xzzn.ems.domain.vo.MonthlyTimeRange">

View File

@ -0,0 +1,181 @@
<?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.xzzn.ems.mapper.EmsDailyEnergyDataMapper">
<resultMap type="EmsDailyEnergyData" id="EmsDailyEnergyDataResult">
<result property="id" column="id" />
<result property="siteId" column="site_id" />
<result property="dataDate" column="data_date" />
<result property="peakChargeDiff" column="peak_charge_diff" />
<result property="peakDischargeDiff" column="peak_discharge_diff" />
<result property="highChargeDiff" column="high_charge_diff" />
<result property="highDischargeDiff" column="high_discharge_diff" />
<result property="flatChargeDiff" column="flat_charge_diff" />
<result property="flatDischargeDiff" column="flat_discharge_diff" />
<result property="valleyChargeDiff" column="valley_charge_diff" />
<result property="valleyDischargeDiff" column="valley_discharge_diff" />
<result property="calcTime" column="calc_time" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
</resultMap>
<sql id="selectEmsDailyEnergyDataVo">
select id, site_id, data_date, peak_charge_diff, peak_discharge_diff, high_charge_diff, high_discharge_diff, flat_charge_diff, flat_discharge_diff, valley_charge_diff, valley_discharge_diff, calc_time, create_by, create_time, update_by, update_time, remark from ems_daily_energy_data
</sql>
<select id="selectEmsDailyEnergyDataList" parameterType="EmsDailyEnergyData" resultMap="EmsDailyEnergyDataResult">
<include refid="selectEmsDailyEnergyDataVo"/>
<where>
<if test="siteId != null and siteId != ''"> and site_id = #{siteId}</if>
<if test="dataDate != null "> and data_date = #{dataDate}</if>
<if test="peakChargeDiff != null "> and peak_charge_diff = #{peakChargeDiff}</if>
<if test="peakDischargeDiff != null "> and peak_discharge_diff = #{peakDischargeDiff}</if>
<if test="highChargeDiff != null "> and high_charge_diff = #{highChargeDiff}</if>
<if test="highDischargeDiff != null "> and high_discharge_diff = #{highDischargeDiff}</if>
<if test="flatChargeDiff != null "> and flat_charge_diff = #{flatChargeDiff}</if>
<if test="flatDischargeDiff != null "> and flat_discharge_diff = #{flatDischargeDiff}</if>
<if test="valleyChargeDiff != null "> and valley_charge_diff = #{valleyChargeDiff}</if>
<if test="valleyDischargeDiff != null "> and valley_discharge_diff = #{valleyDischargeDiff}</if>
<if test="calcTime != null "> and calc_time = #{calcTime}</if>
</where>
</select>
<select id="selectEmsDailyEnergyDataById" parameterType="Long" resultMap="EmsDailyEnergyDataResult">
<include refid="selectEmsDailyEnergyDataVo"/>
where id = #{id}
</select>
<insert id="insertEmsDailyEnergyData" parameterType="EmsDailyEnergyData" useGeneratedKeys="true" keyProperty="id">
insert into ems_daily_energy_data
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="siteId != null">site_id,</if>
<if test="dataDate != null">data_date,</if>
<if test="peakChargeDiff != null">peak_charge_diff,</if>
<if test="peakDischargeDiff != null">peak_discharge_diff,</if>
<if test="highChargeDiff != null">high_charge_diff,</if>
<if test="highDischargeDiff != null">high_discharge_diff,</if>
<if test="flatChargeDiff != null">flat_charge_diff,</if>
<if test="flatDischargeDiff != null">flat_discharge_diff,</if>
<if test="valleyChargeDiff != null">valley_charge_diff,</if>
<if test="valleyDischargeDiff != null">valley_discharge_diff,</if>
<if test="calcTime != null">calc_time,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="siteId != null">#{siteId},</if>
<if test="dataDate != null">#{dataDate},</if>
<if test="peakChargeDiff != null">#{peakChargeDiff},</if>
<if test="peakDischargeDiff != null">#{peakDischargeDiff},</if>
<if test="highChargeDiff != null">#{highChargeDiff},</if>
<if test="highDischargeDiff != null">#{highDischargeDiff},</if>
<if test="flatChargeDiff != null">#{flatChargeDiff},</if>
<if test="flatDischargeDiff != null">#{flatDischargeDiff},</if>
<if test="valleyChargeDiff != null">#{valleyChargeDiff},</if>
<if test="valleyDischargeDiff != null">#{valleyDischargeDiff},</if>
<if test="calcTime != null">#{calcTime},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<update id="updateEmsDailyEnergyData" parameterType="EmsDailyEnergyData">
update ems_daily_energy_data
<trim prefix="SET" suffixOverrides=",">
<if test="siteId != null">site_id = #{siteId},</if>
<if test="dataDate != null">data_date = #{dataDate},</if>
<if test="peakChargeDiff != null">peak_charge_diff = #{peakChargeDiff},</if>
<if test="peakDischargeDiff != null">peak_discharge_diff = #{peakDischargeDiff},</if>
<if test="highChargeDiff != null">high_charge_diff = #{highChargeDiff},</if>
<if test="highDischargeDiff != null">high_discharge_diff = #{highDischargeDiff},</if>
<if test="flatChargeDiff != null">flat_charge_diff = #{flatChargeDiff},</if>
<if test="flatDischargeDiff != null">flat_discharge_diff = #{flatDischargeDiff},</if>
<if test="valleyChargeDiff != null">valley_charge_diff = #{valleyChargeDiff},</if>
<if test="valleyDischargeDiff != null">valley_discharge_diff = #{valleyDischargeDiff},</if>
<if test="calcTime != null">calc_time = #{calcTime},</if>
<if test="createBy != null">create_by = #{createBy},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where id = #{id}
</update>
<delete id="deleteEmsDailyEnergyDataById" parameterType="Long">
delete from ems_daily_energy_data where id = #{id}
</delete>
<delete id="deleteEmsDailyEnergyDataByIds" parameterType="String">
delete from ems_daily_energy_data where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<select id="getDataByDate" parameterType="String" resultMap="EmsDailyEnergyDataResult">
<include refid="selectEmsDailyEnergyDataVo"/>
where data_date = #{today}
AND site_id = #{siteId}
</select>
<insert id="insertOrUpdateData" parameterType="com.xzzn.ems.domain.EmsDailyEnergyData"
useGeneratedKeys="true" keyProperty="id">
INSERT INTO ems_daily_energy_data (
id, site_id, data_date,
<if test="peakChargeDiff != null">peak_charge_diff,</if>
<if test="peakDischargeDiff != null">peak_discharge_diff,</if>
<if test="highChargeDiff != null">high_charge_diff,</if>
<if test="highDischargeDiff != null">high_discharge_diff,</if>
<if test="flatChargeDiff != null">flat_charge_diff,</if>
<if test="flatDischargeDiff != null">flat_discharge_diff,</if>
<if test="valleyChargeDiff != null">valley_charge_diff,</if>
<if test="valleyDischargeDiff != null">valley_discharge_diff,</if>
calc_time,
create_by,
create_time,
update_by,
update_time,
remark
) VALUES (
#{id},
#{siteId},
#{dataDate},
<if test="peakChargeDiff != null">#{peakChargeDiff},</if>
<if test="peakDischargeDiff != null">#{peakDischargeDiff},</if>
<if test="highChargeDiff != null">#{highChargeDiff},</if>
<if test="highDischargeDiff != null">#{highDischargeDiff},</if>
<if test="flatChargeDiff != null">#{flatChargeDiff},</if>
<if test="flatDischargeDiff != null">#{flatDischargeDiff},</if>
<if test="valleyChargeDiff != null">#{valleyChargeDiff},</if>
<if test="valleyDischargeDiff != null">#{valleyDischargeDiff},</if>
#{calcTime},
'system',
NOW(),
'system',
NOW(),
#{remark}
) ON DUPLICATE KEY UPDATE
<if test="peakChargeDiff != null">peak_charge_diff = #{peakChargeDiff},</if>
<if test="peakDischargeDiff != null">peak_discharge_diff = #{peakDischargeDiff},</if>
<if test="highChargeDiff != null">high_charge_diff = #{highChargeDiff},</if>
<if test="highDischargeDiff != null">high_discharge_diff = #{highDischargeDiff},</if>
<if test="flatChargeDiff != null">flat_charge_diff = #{flatChargeDiff},</if>
<if test="flatDischargeDiff != null">flat_discharge_diff = #{flatDischargeDiff},</if>
<if test="valleyChargeDiff != null">valley_charge_diff = #{valleyChargeDiff},</if>
<if test="valleyDischargeDiff != null">valley_discharge_diff = #{valleyDischargeDiff},</if>
calc_time = #{calcTime},
update_time = NOW();
</insert>
</mapper>

View File

@ -148,4 +148,14 @@
where year = #{currentYear}
and month = #{month}
</select>
<select id="getTimeRangeByDate" resultType="com.xzzn.ems.domain.vo.EnergyPriceTimeRange">
select t2.cost_type as costType,
t2.start_time as startTime,
t2.end_time as endTime
from ems_energy_price_config t
left join ems_price_time_config t2 on t.id = t2.price_id
where t.`year` = #{currentYear}
and t.`month` = #{currentMonth}
</select>
</mapper>