浏览代码

精益模型处理部分逻辑

lingpeng.li 6 月之前
父节点
当前提交
4e2c48e2b2
共有 1 个文件被更改,包括 135 次插入20 次删除
  1. 135 20
      jeecg-module-gather/src/main/java/org/jeecg/modules/fpgJob/LeanModelStatistics.java

+ 135 - 20
jeecg-module-gather/src/main/java/org/jeecg/modules/fpgJob/LeanModelStatistics.java

@@ -2,13 +2,11 @@ package org.jeecg.modules.fpgJob;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.jeecg.common.util.DateUtils;
 import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.device.entity.DeviceInformation;
 import org.jeecg.modules.device.service.IDeviceInformationService;
-import org.jeecg.modules.devicePoint.entity.DevicePoint;
 import org.jeecg.modules.devicePoint.service.IDevicePointService;
 import org.jeecg.modules.fpgJob.entity.FpgLeanModel;
 import org.jeecg.modules.fpgJob.service.IFpgLeanModelService;
@@ -37,7 +35,6 @@ import java.time.*;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicReference;
-import java.util.stream.Collectors;
 
 
 @Slf4j
@@ -95,6 +92,7 @@ public class LeanModelStatistics {
         deviceStatistics();
     }
 
+
     public void changeProportion(){
         log.info("变更占比");
         // 峰平谷精益模型配置获取
@@ -418,7 +416,7 @@ public class LeanModelStatistics {
         }
     }
 
-    // 精益模型统计数据
+    //精益模型统计数据
     public void leanModel(){
         log.info("峰平谷模型");
         // 峰平谷精益模型配置获取
@@ -426,7 +424,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());
+//        Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
         // 尖峰平谷时段配置
         List<PeaksAndValleysTimeConfig>  peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
         // 存储fpgGather数据id集合
@@ -452,6 +450,11 @@ public class LeanModelStatistics {
                 fpgGatherData.setFpgModelUpdatetime(new Date());
                 try {
                     BigDecimal runTime = new BigDecimal("300");
+                    // 设备停止运行不统计
+                    if (fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) < 0) { // 设备停止运行
+                        fpgGatherUpdateList.add(fpgGatherData);
+                        return;
+                    }
                     String finalDatestr = "";
                     // 统计单位
                     if("minute".equals(fpgLeanModeInfo.getTotalUnit())){ // 分钟
@@ -486,21 +489,18 @@ public class LeanModelStatistics {
                             classs = "center";
                         }
                     }
+                    Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
                     // 重组对象
                     FpgStatiscsModelData fpgStatiscsModelData = new FpgStatiscsModelData(fpgGatherData, fpgLeanModeInfo, finalDatestr, dayDate, classs, curentDate, curentDate);
                     // 实时统计直接插入
                     if("real_time". equals(fpgLeanModeInfo.getTotalUnit())){
                         fpgStatiscsModelData.setSelectricCurrent(fpgGatherData.getRunCurrent());
                         fpgStatiscsModelData.setPower(fpgGatherData.getActivePower());
+                        fpgStatiscsModelData.setIngTime(new BigDecimal("300"));
                         mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
                         fpgGatherUpdateList.add(fpgGatherData);
                         return;
                     }
-                    // 设备停止运行不统计
-                    if (fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) < 0) { // 设备停止运行
-                        fpgGatherUpdateList.add(fpgGatherData);
-                        return;
-                    }
                     // 定义尖峰平谷
                     AtomicReference<String> jfpgStr = new AtomicReference<>("");
                     // 判断尖峰平谷时段
@@ -523,7 +523,18 @@ public class LeanModelStatistics {
                     if(!classs.isEmpty()){ // 增加班次条件
                         query.addCriteria(Criteria.where("classs").is(classs));
                     }
-                    FpgStatiscsModelMongodb fpgStatiscsModelMongo = mongoTemplate.findOne(query, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
+
+                    String leanModelCode = fpgLeanModeInfo.getLeanModelCode();
+                    String collectionName;
+
+                    // 判断 leanModelCode 是否包含 "_report"
+                    if (leanModelCode.endsWith("_report")) {
+                        collectionName = leanModelCode;
+                    } else {
+                        collectionName = "leanmodel_" + leanModelCode;
+                    }
+
+                    FpgStatiscsModelMongodb fpgStatiscsModelMongo = mongoTemplate.findOne(query, FpgStatiscsModelMongodb.class, collectionName);
                     // 功率
                     BigDecimal activePower = fpgGatherData.getActivePower().divide(new BigDecimal("12"), 8, RoundingMode.HALF_UP);
                     // 电流
@@ -536,9 +547,10 @@ public class LeanModelStatistics {
                         activePower = fpgGatherData.getActivePower() == null ? new BigDecimal("0.00") : activePower;
                         BigDecimal oldPower = fpgStatiscsModelMongo.getPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPower();
                         BigDecimal oldingTime = fpgStatiscsModelMongo.getIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getIngTime();
+                        BigDecimal oldSelectricCurrent = fpgStatiscsModelMongo.getSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getSelectricCurrent();
                         // 总运行时间处理
                         if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1) { // 运行时长统计
-                            ((Update) update).set("ingTime", oldingTime.add(runTime));
+                            ((  Update) update).set("ingTime", oldingTime.add(runTime));
                         }
                         // 电流处理
                         if(fpgLeanModeInfo.getElectricCurrent() != null && fpgLeanModeInfo.getElectricCurrent() == 1){
@@ -556,12 +568,19 @@ public class LeanModelStatistics {
                                 ((Update) update).set("valleysSelectricCurrent", oldvalleysSelectricCurrent.add(selectricCurrent));
                             }
                         }
+
+                        BigDecimal zb = new BigDecimal("100");
                         // 总功率处理
                         ((Update) update).set("power", oldPower.add(activePower));
+                        //总电流处理
+                        ((Update) update).set("selectricCurrent", oldSelectricCurrent.add(selectricCurrent));
                         // 尖峰平谷判定
                         if("tops".equals(jfpgStr.get())){ // 尖
                             // 功率处理
                             BigDecimal oldtopsPower = fpgStatiscsModelMongo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsPower();
+                            BigDecimal oldPeaksPower = fpgStatiscsModelMongo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksPower();
+                            BigDecimal oldFlatPower = fpgStatiscsModelMongo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatPower();
+                            BigDecimal oldValleysPower = fpgStatiscsModelMongo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysPower();
                             ((Update) update).set("topsPower", oldtopsPower.add(activePower));
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
@@ -575,9 +594,29 @@ public class LeanModelStatistics {
                                 ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getTopsPrice()).add(totalAmount));
                                 ((Update) update).set("topsAmount", oldtopsPower.add(activePower).multiply(fpgLeanModeInfo.getTopsPrice()));
                             }
+                            if(fpgLeanModeInfo.getProportion() !=null && fpgLeanModeInfo.getProportion() == 1){ //占比统计
+
+                                ((Update) update).set("topsProportion", oldtopsPower.add(activePower).divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                if(fpgStatiscsModelMongo.getPeaksPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("peaksProportion", oldPeaksPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+                                }
+                                if(fpgStatiscsModelMongo.getFlatPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("flatProportion", oldFlatPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+                                }
+
+                                if(fpgStatiscsModelMongo.getValleysPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("valleysProportion", oldValleysPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+                                }
+
+
+                            }
                         } else if("peaks".equals(jfpgStr.get())){ // 峰
                             // 功率处理
                             BigDecimal oldpeaksPower = fpgStatiscsModelMongo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksPower();
+                            BigDecimal oldtopsPower = fpgStatiscsModelMongo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsPower();
+                            BigDecimal oldFlatPower = fpgStatiscsModelMongo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatPower();
+                            BigDecimal oldValleysPower = fpgStatiscsModelMongo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysPower();
                             ((Update) update).set("peaksPower", oldpeaksPower.add(activePower));
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
@@ -589,16 +628,34 @@ public class LeanModelStatistics {
                                 // 总金额
                                 BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getAmount();
                                 ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getPeaksPrice()).add(totalAmount));
-                                ((Update) update).set("topsAmount", oldpeaksPower.add(activePower).multiply(fpgLeanModeInfo.getPeaksPrice()));
+                                ((Update) update).set("peaksAmount", oldpeaksPower.add(activePower).multiply(fpgLeanModeInfo.getPeaksPrice()));
+                            }
+                            if(fpgLeanModeInfo.getProportion() !=null && fpgLeanModeInfo.getProportion() == 1){ //占比统计
+
+                                ((Update) update).set("peaksProportion", oldpeaksPower.add(activePower).divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                if(fpgStatiscsModelMongo.getTopsPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("topsProportion", oldtopsPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+                                }
+                                if(fpgStatiscsModelMongo.getFlatPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("flatProportion", oldFlatPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+                                }
+                                if(fpgStatiscsModelMongo.getValleysPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("valleysProportion", oldValleysPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
                             }
                         } else if("flat".equals(jfpgStr.get())){ // 平
                             // 功率处理
                             BigDecimal oldflatPower = fpgStatiscsModelMongo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatPower();
+                            BigDecimal oldpeaksPower = fpgStatiscsModelMongo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksPower();
+                            BigDecimal oldtopsPower = fpgStatiscsModelMongo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsPower();
+                            BigDecimal oldValleysPower = fpgStatiscsModelMongo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysPower();
                             ((Update) update).set("flatPower", oldflatPower.add(activePower));
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
                                 BigDecimal oldflatIngTime = fpgStatiscsModelMongo.getFlatIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatIngTime();
-                                ((Update) update).set("topsIngTime", oldflatIngTime.add(runTime));
+                                ((Update) update).set("flatIngTime", oldflatIngTime.add(runTime));
                             }
                             // 金额处理
                             if(fpgLeanModeInfo.getFlatPrice() != null && fpgLeanModeInfo.getFlatPrice().compareTo(new BigDecimal("0.00")) > 0) {
@@ -607,9 +664,31 @@ public class LeanModelStatistics {
                                 ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getFlatPrice()).add(totalAmount));
                                 ((Update) update).set("flatAmount", oldflatPower.add(activePower).multiply(fpgLeanModeInfo.getFlatPrice()));
                             }
+                            if(fpgLeanModeInfo.getProportion() !=null && fpgLeanModeInfo.getProportion() == 1){ //占比统计
+
+                                ((Update) update).set("flatProportion", oldflatPower.add(activePower).divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                if(fpgStatiscsModelMongo.getTopsPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("topsProportion", oldtopsPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
+                                if(fpgStatiscsModelMongo.getPeaksPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("peaksProportion", oldpeaksPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
+                                if(fpgStatiscsModelMongo.getValleysPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("valleysProportion", oldValleysPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
+
+                            }
                         } else if("valleys".equals(jfpgStr.get())){ // 谷
                             // 功率处理
                             BigDecimal oldvalleysPower = fpgStatiscsModelMongo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysPower();
+                            BigDecimal oldflatPower = fpgStatiscsModelMongo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatPower();
+                            BigDecimal oldpeaksPower = fpgStatiscsModelMongo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksPower();
+                            BigDecimal oldtopsPower = fpgStatiscsModelMongo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsPower();
+
                             ((Update) update).set("valleysPower", oldvalleysPower.add(activePower));
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
@@ -623,12 +702,34 @@ public class LeanModelStatistics {
                                 ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getValleysPrice()).add(totalAmount));
                                 ((Update) update).set("valleysAmount", oldvalleysPower.add(activePower).multiply(fpgLeanModeInfo.getValleysPrice()));
                             }
+
+                            if(fpgLeanModeInfo.getProportion() !=null && fpgLeanModeInfo.getProportion() == 1){ //占比统计
+
+                                ((Update) update).set("valleysProportion", oldvalleysPower.add(activePower).divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                if(fpgStatiscsModelMongo.getTopsPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("topsProportion", oldtopsPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
+                                if(fpgStatiscsModelMongo.getPeaksPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("peaksProportion", oldpeaksPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
+                                if(fpgStatiscsModelMongo.getFlatPower().compareTo(new BigDecimal("0.00")) > 0) {
+                                    ((Update) update).set("flatProportion", oldflatPower.divide(fpgStatiscsModelMongo.getPower().add(activePower), 2, RoundingMode.HALF_UP).multiply(zb));
+
+                                }
+
+                            }
                         }
                         fpgGatherUpdateList.add(fpgGatherData);
-                        mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
+
+                        mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, collectionName);
+
                     } else { // 为空直接插入
                         fpgGatherUpdateList.add(fpgGatherData);
                         fpgStatiscsModelData.setPower(activePower); // 总功率处理
+                        fpgStatiscsModelData.setSelectricCurrent(selectricCurrent); // 总电流处理
                         if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1) { // 运行时长统计
                             fpgStatiscsModelData.setIngTime(runTime);   // 运行时常
                         }
@@ -646,7 +747,8 @@ public class LeanModelStatistics {
                         }
                         // 尖峰平谷判定
                         if("tops".equals(jfpgStr.get())){ // 尖
-                            fpgStatiscsModelData.setTopsPower(activePower); // 功率
+                            fpgStatiscsModelData.setTopsPower(activePower);
+                            fpgStatiscsModelData.setTopsProportion(new BigDecimal("100"));// 功率
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
                                 fpgStatiscsModelData.setTopsPower(runTime);
@@ -660,6 +762,7 @@ public class LeanModelStatistics {
                         } else if("peaks".equals(jfpgStr.get())){ // 峰
                             // 功率处理
                             fpgStatiscsModelData.setPeaksPower(activePower); // 功率
+                            fpgStatiscsModelData.setPeaksProportion(new BigDecimal("100"));
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
                                 fpgStatiscsModelData.setPeaksIngTime(runTime);
@@ -673,6 +776,7 @@ public class LeanModelStatistics {
                         } else if("flat".equals(jfpgStr.get())){ // 平
                             // 功率处理
                             fpgStatiscsModelData.setFlatPower(activePower); // 功率
+                            fpgStatiscsModelData.setFlatProportion(new BigDecimal("100"));
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
                                 fpgStatiscsModelData.setFlatIngTime(runTime);
@@ -686,6 +790,8 @@ public class LeanModelStatistics {
                         } else if("valleys".equals(jfpgStr.get())){ // 谷
                             // 功率处理
                             fpgStatiscsModelData.setValleysPower(activePower); // 功率
+                            fpgStatiscsModelData.setValleysProportion(new BigDecimal("100"));
+
                             // 运行时常处理
                             if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
                                 fpgStatiscsModelData.setValleysIngTime(runTime);
@@ -698,7 +804,7 @@ public class LeanModelStatistics {
                             }
                         }
                         // 将点位数据存入mongo中
-                        mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
+                        mongoTemplate.insert(fpgStatiscsModelData, collectionName);
                     }
                 }catch (Exception e) {
                     log.error(e.getMessage());
@@ -721,9 +827,18 @@ public class LeanModelStatistics {
         LocalDateTime start_time = LocalDateTime.parse(startTime, formatter);
         LocalDateTime end_time = LocalDateTime.parse(endTime, formatter);
 
-        // 判断是否跨天
-        if (end_time.isBefore(start_time)) {
-            end_time = end_time.plusDays(1);  // 跨天时,结束时间加1天
+        // 检查 createTime 的时分秒部分
+        LocalTime createTimeTime = createTime.toLocalTime();
+        LocalTime midnight = LocalTime.MIDNIGHT;
+
+        // createTime 的时分秒大于 00:00:00 时,startTime 减去一天
+        if (createTimeTime.isAfter(midnight) && end_time.isBefore(start_time)) {
+            start_time = start_time.minusDays(1);  // 将 startTime 减去一天
+        }
+
+        // createTime 的时分秒小于 00:00:00 时,endTime 加一天
+        if (createTimeTime.isBefore(midnight) && end_time.isBefore(start_time)) {
+            end_time = end_time.plusDays(1);  // 将 endTime 加一天
         }
 
         // 检查createTime是否在区间内