Ver Fonte

完善设备启停的部分逻辑

lingpeng.li há 6 meses atrás
pai
commit
5bcded34e2

+ 75 - 15
jeecg-module-gather/src/main/java/org/jeecg/modules/fpgJob/LeanModelStatistics.java

@@ -30,11 +30,13 @@ import org.springframework.stereotype.Component;
 
 import java.math.BigDecimal;
 import java.math.RoundingMode;
+import java.text.DecimalFormat;
 import java.text.SimpleDateFormat;
 import java.time.*;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
+import java.util.stream.Collectors;
 
 
 @Slf4j
@@ -136,11 +138,13 @@ public class LeanModelStatistics {
     // 设备启停统计数据
     public void deviceStatistics() {
         log.info("设备日常统计");
+        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         // 获取当前时间
         LocalDateTime newTime = LocalDateTime.now().plusHours(8);
         Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
         // 峰平谷设备列表
-        List<DeviceInformation> deviceInformationllist = deviceInformationService.list();
+        LambdaQueryWrapper<DeviceInformation> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
+        List<DeviceInformation> deviceInformationllist = deviceInformationService.list(objectLambdaQueryWrapper);
         // 尖峰平谷时段配置
         List<PeaksAndValleysTimeConfig>  peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
         BigDecimal zb = new BigDecimal("100");
@@ -201,37 +205,81 @@ public class LeanModelStatistics {
                     // 组合mongodb条件
                     Query queryStartStop = new Query()
                             .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()))
-                            .addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId())).with(Sort.by(Sort.Order.desc("deviceStartTime")));
+                            .addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()))
+                            .with(Sort.by(Sort.Order.desc("deviceStartTime"))).limit(1);
                     // 查找设备运行记录
                     DeviceStatiscsModelMongodb startStopMongo = mongoTemplate.findOne(queryStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
                     if(startStopMongo != null){
                         // 验证两个时间是否相等
                         int timeRes = compareDatesByTimeStamp(DateUtils.str2Date(startStopMongo.getDeviceStartTime(), DateUtils.datetimeFormat.get()), DateUtils.str2Date(startStopMongo.getDeviceStopTimeBank(), DateUtils.datetimeFormat.get()));
-                        Date deviceStopTimeEnd = DateUtils.addHours(fpgGatherData.getCreateTime(), 8);
+//                        Date deviceStopTimeEnd = DateUtils.addHours(fpgGatherData.getCreateTime(), 8);
+                        Date deviceStopTimeEnd = DateUtils.addHours(fpgGatherData.getCreateTime(), 0);
                         if (fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) < 0) { // 设备停止运行 直接更新停止时间结束不用统计任何东西
+                            Date startDate = dateFormat.parse(startStopMongo.getDeviceStartTime());
+
+                            // 将 Date 转换为 Instant
+                            Instant oldInstant = startDate.toInstant();
+                            Instant newInstant = fpgGatherData.getCreateTime().toInstant();
+
+                            // 计算时间差
+                            Duration duration = Duration.between(oldInstant, newInstant);
+
+                            // 获取时间差的秒数
+                            long diffInSeconds = duration.getSeconds();
+
+                            BigDecimal timeDifference = new BigDecimal(diffInSeconds);
+
                             UpdateDefinition updateStartStop = new Update()
-                                    .set("updateTime", curentDate).set("deviceStopTime", DateUtils.date2Str(deviceStopTimeEnd, DateUtils.datetimeFormat.get())).set("deviceStopTimeBank", DateUtils.date2Str(deviceStopTimeEnd, DateUtils.datetimeFormat.get()));
-                            mongoTemplate.findAndModify(queryStartStop, updateStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
+                                    .set("updateTime", curentDate).set("deviceStopTime", DateUtils.date2Str(deviceStopTimeEnd, DateUtils.datetimeFormat.get()))
+                                    .set("deviceStopTimeBank", DateUtils.date2Str(deviceStopTimeEnd, DateUtils.datetimeFormat.get())).set("ingTime", timeDifference);
+
+                            Query modifyStartStop = new Query()
+                                    .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()))
+                                    .addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()))
+                                    .addCriteria(Criteria.where("_id").is(startStopMongo.get_id()));
+                            mongoTemplate.findAndModify(modifyStartStop, updateStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
                         } else if(fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) > 0 && timeRes != 0){ // 设备重新启动
                             startStopStatiscsData.setPower(runPower);
                             startStopStatiscsData.setIngTime(runTime);
                             startStopStatiscsData.setSelectricCurrent(selectricCurrent);
                             mongoTemplate.insert(startStopStatiscsData, startStopMonKey);
-                            return;
+//                            return;
                         } else { // 设备运行累加
                             // 历史数据
                             BigDecimal oldPower = startStopMongo.getPower() == null ? new BigDecimal("0.00") : startStopMongo.getPower();
                             BigDecimal oldRunTime = startStopMongo.getIngTime() == null ? new BigDecimal("0.00") : startStopMongo.getIngTime();
                             BigDecimal oldSelectricCurrent = startStopMongo.getSelectricCurrent() == null ? new BigDecimal("0.00") : startStopMongo.getSelectricCurrent();
                             // 更新启动运行数据
-                            Date deviceStopTime = DateUtils.addHours(fpgGatherData.getCreateTime(), 8);
+                            Date deviceStopTime = DateUtils.addHours(fpgGatherData.getCreateTime(), 0);
+
+
+                            Date startDate = dateFormat.parse(startStopMongo.getDeviceStartTime());
+
+                            // 将 Date 转换为 Instant
+                            Instant oldInstant = startDate.toInstant();
+                            Instant newInstant = fpgGatherData.getCreateTime().toInstant();
+
+                            // 计算时间差
+                            Duration duration = Duration.between(oldInstant, newInstant);
+
+                            // 获取时间差的秒数
+                            long diffInSeconds = duration.getSeconds();
+
+                            BigDecimal timeDifference = new BigDecimal(diffInSeconds);
+
                             UpdateDefinition updateStartStop = new Update()
                                     .set("power", oldPower.add(runPower))
                                     .set("selectricCurrent", oldSelectricCurrent.add(selectricCurrent))
-                                    .set("ingTime", oldRunTime.add(runTime))
+                                    .set("ingTime", timeDifference)
                                     .set("deviceStopTime", DateUtils.date2Str(deviceStopTime, DateUtils.datetimeFormat.get()))
                                     .set("updateTime", curentDate);
-                            mongoTemplate.updateFirst(queryStartStop, updateStartStop, FpgStatiscsModelMongodb.class, startStopMonKey);
+
+                            Query modifyStartStop = new Query()
+                                    .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()))
+                                    .addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()))
+                                    .addCriteria(Criteria.where("_id").is(startStopMongo.get_id()));
+                            mongoTemplate.findAndModify(modifyStartStop, updateStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
+                            mongoTemplate.updateFirst(modifyStartStop, updateStartStop, FpgStatiscsModelMongodb.class, startStopMonKey);
                         }
                     } else { // 直接插入
                         if(fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) > 0){ // 运行中计入启停记录
@@ -248,7 +296,7 @@ public class LeanModelStatistics {
                         fpgGatherData.setId(fpgGatherData.getId());
                         return;
                     }
-                    // 日统计尖峰平谷数据(功率+占比+电流+运行时长) start
+                     //日统计尖峰平谷数据(功率+占比+电流+运行时长) start
                     String dayPowerproportioncurrentRunMonKey = "total_day_powerproportioncurrent";
                     DeviceStatiscsModelData dayPowerproportioncurrentRunData = new DeviceStatiscsModelData(fpgGatherData, dayPowerproportioncurrentRunMonKey, dayDate, classs, curentDate, curentDate);
                     // 组合mongodb条件
@@ -424,7 +472,7 @@ public class LeanModelStatistics {
         List<FpgLeanModel> fpgLeanModellist = fpgLeanModelService.list(leanmodelQuery);
         // 获取当前时间
         LocalDateTime newTime = LocalDateTime.now().plusHours(8);
-//        Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
+
         // 尖峰平谷时段配置
         List<PeaksAndValleysTimeConfig>  peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
         // 存储fpgGather数据id集合
@@ -585,7 +633,10 @@ public class LeanModelStatistics {
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
                                 BigDecimal oldtopsIngTime = fpgStatiscsModelMongo.getTopsIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsIngTime();
-                                ((Update) update).set("topsIngTime", oldtopsIngTime.add(runTime));
+                                BigDecimal newTopsIngTime = oldtopsIngTime.add(runTime).stripTrailingZeros();
+                                DecimalFormat df = new DecimalFormat("0.##################");
+                                String topFormatted = df.format(newTopsIngTime);
+                                ((Update) update).set("topsIngTime", new BigDecimal(topFormatted));
                             }
                             // 金额处理
                             if(fpgLeanModeInfo.getTopsPrice() != null && fpgLeanModeInfo.getTopsPrice().compareTo(new BigDecimal("0.00")) > 0) {
@@ -621,7 +672,10 @@ public class LeanModelStatistics {
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
                                 BigDecimal oldpeaksIngTime = fpgStatiscsModelMongo.getPeaksIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksIngTime();
-                                ((Update) update).set("peaksIngTime", oldpeaksIngTime.add(runTime));
+                                BigDecimal newPeaksIngTime = oldpeaksIngTime.add(runTime).stripTrailingZeros();
+                                DecimalFormat df = new DecimalFormat("0.##################");
+                                String peaksFormatted = df.format(newPeaksIngTime);
+                                ((Update) update).set("peaksIngTime", new BigDecimal(peaksFormatted));
                             }
                             // 金额处理
                             if(fpgLeanModeInfo.getPeaksPrice() != null && fpgLeanModeInfo.getPeaksPrice().compareTo(new BigDecimal("0.00")) > 0) {
@@ -655,7 +709,10 @@ public class LeanModelStatistics {
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
                                 BigDecimal oldflatIngTime = fpgStatiscsModelMongo.getFlatIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatIngTime();
-                                ((Update) update).set("flatIngTime", oldflatIngTime.add(runTime));
+                                BigDecimal newFlatIngTime = oldflatIngTime.add(runTime).stripTrailingZeros();
+                                DecimalFormat df = new DecimalFormat("0.##################");
+                                String formatted = df.format(newFlatIngTime);
+                                ((Update) update).set("flatIngTime", new BigDecimal(formatted));
                             }
                             // 金额处理
                             if(fpgLeanModeInfo.getFlatPrice() != null && fpgLeanModeInfo.getFlatPrice().compareTo(new BigDecimal("0.00")) > 0) {
@@ -693,7 +750,10 @@ public class LeanModelStatistics {
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
                                 BigDecimal oldvalleysIngTime = fpgStatiscsModelMongo.getValleysIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysIngTime();
-                                ((Update) update).set("valleysIngTime", oldvalleysIngTime.add(runTime));
+                                BigDecimal newValleysIngTime = oldvalleysIngTime.add(runTime).stripTrailingZeros();
+                                DecimalFormat df = new DecimalFormat("0.##################");
+                                String valleysFormatted = df.format(newValleysIngTime);
+                                ((Update) update).set("valleysIngTime", new BigDecimal(valleysFormatted));
                             }
                             // 金额处理
                             if(fpgLeanModeInfo.getValleysPrice() != null && fpgLeanModeInfo.getValleysPrice().compareTo(new BigDecimal("0.00")) > 0) {