0918电表报表-奉贤每日尖峰平谷累加

This commit is contained in:
2025-10-11 17:21:40 +08:00
parent 83f8f4e293
commit 3454359f01
3 changed files with 47 additions and 25 deletions

View File

@ -89,4 +89,8 @@ public interface EmsAmmeterDataMapper
public List<PowerStatisListVo> getPowerDataByDay(DateSearchRequest requestVo); public List<PowerStatisListVo> getPowerDataByDay(DateSearchRequest requestVo);
public List<PowerStatisListVo> getPowerDataByMonth(DateSearchRequest requestVo); public List<PowerStatisListVo> getPowerDataByMonth(DateSearchRequest requestVo);
public List<PowerStatisListVo> getPowerDataByMinutes(DateSearchRequest requestVo); public List<PowerStatisListVo> getPowerDataByMinutes(DateSearchRequest requestVo);
// 获取最新数据
public EmsAmmeterData getLastData(@Param("siteId")String siteId, @Param("deviceId")String deviceId);
} }

View File

@ -18,7 +18,6 @@ import com.xzzn.ems.mapper.*;
import com.xzzn.ems.service.IEmsAlarmRecordsService; import com.xzzn.ems.service.IEmsAlarmRecordsService;
import com.xzzn.ems.service.IFXXDataProcessService; import com.xzzn.ems.service.IFXXDataProcessService;
import com.xzzn.ems.utils.AbstractBatteryDataProcessor; import com.xzzn.ems.utils.AbstractBatteryDataProcessor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -682,6 +681,9 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
Map<String, Object> obj = JSON.parseObject(dataJson, new TypeReference<Map<String, Object>>() { Map<String, Object> obj = JSON.parseObject(dataJson, new TypeReference<Map<String, Object>>() {
}); });
// 获取上次数据便于后面计算差值均无则默认0
EmsAmmeterData lastAmmeterData = getLastAmmeterData(deviceId);
EmsAmmeterData dataLoad = new EmsAmmeterData(); EmsAmmeterData dataLoad = new EmsAmmeterData();
// 更新时间 // 更新时间
dataLoad.setDataUpdateTime(dataUpdateTime); dataLoad.setDataUpdateTime(dataUpdateTime);
@ -770,10 +772,26 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
redisCache.setCacheObject(RedisKeyConstants.AMMETER + SITE_ID + "_" +deviceId, dataLoad); redisCache.setCacheObject(RedisKeyConstants.AMMETER + SITE_ID + "_" +deviceId, dataLoad);
// 处理电表每日充放电数据 // 处理电表每日充放电数据
dealAmmeterDailyDate(obj, deviceId, dataUpdateTime); dealAmmeterDailyDate(obj, deviceId, dataUpdateTime, lastAmmeterData);
} }
private void dealAmmeterDailyDate(Map<String, Object> obj, String deviceId, Date dataUpdateTime) { private EmsAmmeterData getLastAmmeterData(String deviceId) {
// 先从redis取取不到查数据
EmsAmmeterData lastData = redisCache.getCacheObject(RedisKeyConstants.AMMETER + SITE_ID + "_" +deviceId);
if (lastData == null) {
lastData = emsAmmeterDataMapper.getLastData(SITE_ID,deviceId);
if (lastData == null) {
lastData = new EmsAmmeterData();
lastData.setSiteId(SITE_ID);
lastData.setDeviceId(deviceId);
lastData.setCurrentForwardActiveTotal(BigDecimal.ZERO);
lastData.setCurrentReverseActiveTotal(BigDecimal.ZERO);
}
}
return lastData;
}
private void dealAmmeterDailyDate(Map<String, Object> obj, String deviceId, Date dataUpdateTime, EmsAmmeterData lastData) {
// 先获取当月电价配置redis没有这查数据库都没有则返回 // 先获取当月电价配置redis没有这查数据库都没有则返回
String priceKey = RedisKeyConstants.ENERGY_PRICE_TIME + SITE_ID + "_" + LocalDate.now().getYear() + LocalDate.now().getMonthValue(); String priceKey = RedisKeyConstants.ENERGY_PRICE_TIME + SITE_ID + "_" + LocalDate.now().getYear() + LocalDate.now().getMonthValue();
List<EnergyPriceTimeRange> timeRanges = redisCache.getCacheObject(priceKey); List<EnergyPriceTimeRange> timeRanges = redisCache.getCacheObject(priceKey);
@ -799,41 +817,28 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
return; 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(); EmsDailyEnergyData energyData = initEnergyData();
// 根据 costType 计算差值 // 根据 costType计算本次与上次数据差值,累加到对应的数据类型里面
setDiffByCostType(costType,energyData,redisHash,obj); setDiffByCostType(costType,energyData,lastData,obj);
// 插入或更新电表每日差值数据表 // 插入或更新电表每日差值数据表
emsDailyEnergyDataMapper.insertOrUpdateData(energyData); emsDailyEnergyDataMapper.insertOrUpdateData(energyData);
} }
private void setDiffByCostType(String costType, EmsDailyEnergyData energyData, Map<String, BigDecimal> redisHash, Map<String, Object> obj) { private void setDiffByCostType(String costType, EmsDailyEnergyData energyData, EmsAmmeterData lastData, Map<String, Object> obj) {
BigDecimal currentChargeData = StringUtils.getBigDecimal(obj.get("ZXYGDN")); BigDecimal currentChargeData = StringUtils.getBigDecimal(obj.get("ZXYGDN"));
BigDecimal currentDischargeData = StringUtils.getBigDecimal(obj.get("FXYGDN")); BigDecimal currentDischargeData = StringUtils.getBigDecimal(obj.get("FXYGDN"));
currentChargeData = currentChargeData != null ? currentChargeData : BigDecimal.ZERO; currentChargeData = currentChargeData != null ? currentChargeData : BigDecimal.ZERO;
currentDischargeData = currentDischargeData != null ? currentDischargeData : BigDecimal.ZERO; currentDischargeData = currentDischargeData != null ? currentDischargeData : BigDecimal.ZERO;
// 计算差值,据类型累加 // 计算时段差值,按照数据类型累加
BigDecimal chargeDiffData = currentChargeData.subtract(redisHash.getOrDefault("charge", BigDecimal.ZERO)); BigDecimal chargeDiffData = currentChargeData.subtract(
BigDecimal disChargeDiffData = currentDischargeData.subtract(redisHash.getOrDefault("discharge", BigDecimal.ZERO)); lastData.getCurrentForwardActiveTotal() == null ? BigDecimal.ZERO : lastData.getCurrentForwardActiveTotal());
BigDecimal disChargeDiffData = currentDischargeData.subtract(
lastData.getCurrentReverseActiveTotal() == null ? BigDecimal.ZERO : lastData.getCurrentReverseActiveTotal()
);
switch (costType) { switch (costType) {
case "peak": case "peak":
BigDecimal peakCharge = energyData.getPeakChargeDiff(); BigDecimal peakCharge = energyData.getPeakChargeDiff();

View File

@ -1048,4 +1048,17 @@
GROUP BY statisDate GROUP BY statisDate
order by statisDate desc order by statisDate desc
</select> </select>
<select id="getLastData" resultMap="EmsAmmeterDataResult">
SELECT
t.site_id,
t.device_id,
t.data_update_time,
t.current_forward_active_total,
t.current_reverse_active_total
FROM ems_ammeter_data t
WHERE site_id = #{siteId}
and device_id = #{deviceId}
ORDER BY data_update_time DESC LIMIT 1
</select>
</mapper> </mapper>