@ -18,7 +18,6 @@ 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 ;
@ -682,6 +681,9 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
Map < String , Object > obj = JSON . parseObject ( dataJson , new TypeReference < Map < String , Object > > ( ) {
} ) ;
// 获取上次数据, 便于后面计算差值均无则默认0
EmsAmmeterData lastAmmeterData = getLastAmmeterData ( deviceId ) ;
EmsAmmeterData dataLoad = new EmsAmmeterData ( ) ;
// 更新时间
dataLoad . setDataUpdateTime ( dataUpdateTime ) ;
@ -770,10 +772,26 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
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没有这查数据库, 都没有则返回
String priceKey = RedisKeyConstants . ENERGY_PRICE_TIME + SITE_ID + " _ " + LocalDate . now ( ) . getYear ( ) + LocalDate . now ( ) . getMonthValue ( ) ;
List < EnergyPriceTimeRange > timeRanges = redisCache . getCacheObject ( priceKey ) ;
@ -799,41 +817,28 @@ public class FXXDataProcessServiceImpl extends AbstractBatteryDataProcessor impl
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 ) ;
// 根据 costType, 计算本次与上次数据差值,累加到对应的数据类型里面
setDiffByCostType ( costType , energyData , lastData , obj ) ;
// 插入或更新电表每日差值数据表
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 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 ) ) ;
// 计算时段 差值,按照数 据类型累加
BigDecimal chargeDiffData = currentChargeData . subtract (
lastData . getCurrentForwardActiveTotal ( ) = = null ? BigDecimal . ZERO : lastData . getCurrentForwardActiveTotal ( ) ) ;
BigDecimal disChargeDiffData = currentDischargeData . subtract (
lastData . getCurrentReverseActiveTotal ( ) = = null ? BigDecimal . ZERO : lastData . getCurrentReverseActiveTotal ( )
) ;
switch ( costType ) {
case " peak " :
BigDecimal peakCharge = energyData . getPeakChargeDiff ( ) ;