guoqiang 7 months ago
parent
commit
b6c5c6e4e6

+ 133 - 131
jeecg-module-gather/src/main/java/org/jeecg/modules/fpgJob/LeanModelStatistics.java

@@ -67,147 +67,149 @@ public class LeanModelStatistics {
         List<PeaksAndValleysTimeConfig>  peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
         //遍历设备集合
         fpgLeanModellist.forEach(fpgLeanModeInfo -> {
-            try {
+            LambdaQueryWrapper<FpgGatherData> fpgGatherQuery = new LambdaQueryWrapper<FpgGatherData>().eq(FpgGatherData::getFpgModelState, "0");
+            List<FpgGatherData> fpgGatherList = fpgGatherDataService.list(fpgGatherQuery);
                 // 处理数据业务逻辑
-                LambdaQueryWrapper<FpgGatherData> fpgGatherQuery = new LambdaQueryWrapper<FpgGatherData>().eq(FpgGatherData::getFpgModelState, "0");
-                List<FpgGatherData> fpgGatherList = fpgGatherDataService.list(fpgGatherQuery);
                 fpgGatherList.forEach(fpgGatherData -> { // 循环处理采集数据处理
-                    String finalDatestr = "";
-                    // 统计单位
-                    if("minute". equals(fpgLeanModeInfo.getTotalUnit())){ // 分钟
-                        finalDatestr = new SimpleDateFormat("HH:mm").format(fpgGatherData.getCreateTime());
-                    } else if("hour". equals(fpgLeanModeInfo.getTotalUnit())){ // 小时
-                        finalDatestr = new SimpleDateFormat("HH").format(fpgGatherData.getCreateTime());
-                    } else if("day". equals(fpgLeanModeInfo.getTotalUnit())){ //
-                        finalDatestr = new SimpleDateFormat("dd").format(fpgGatherData.getCreateTime());
-                    } else if("week". equals(fpgLeanModeInfo.getTotalUnit())){ //
-                        Calendar calendar = Calendar.getInstance();
-                        finalDatestr = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
-                    } else if("month". equals(fpgLeanModeInfo.getTotalUnit())){ //
-                        finalDatestr = new SimpleDateFormat("MM").format(fpgGatherData.getCreateTime());
-                    }
-                    String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(fpgGatherData.getCreateTime());
-                    String classs = "";
-                    // 采集时间
-                    Instant instant = fpgGatherData.getCreateTime().toInstant();
-                    LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
-                    // 班次处理
-                    if (fpgLeanModeInfo.getClassUnit() != null && fpgLeanModeInfo.getClassUnit() == 1) { // 开启了班次统计
-                        if (containsCreateTime(localDateTime, dayDate + " 00:00:00", dayDate + " 07:59:59")) { // 夜班
-                            classs = "night";
-                        } else if (containsCreateTime(localDateTime, dayDate + " 08:00:00", dayDate + " 15:59:59")) { // 白班
-                            classs = "white";
-                        } else if (containsCreateTime(localDateTime, dayDate + " 16:00:00", dayDate + " 23:59:59")) { // 中班
-                            classs = "center";
-                        }
-                    }
-                    // 定义尖峰平谷
-                    AtomicReference<String> jfpgStr = new AtomicReference<>("");
-                    // 判断尖峰平谷时段
-                    peaksAndValleysTimeConfiglist.forEach(peaksAndValleysTimeConfig -> {
-                        if (containsCreateTime(localDateTime, dayDate + " " + peaksAndValleysTimeConfig.getStartTime(), dayDate + " " + peaksAndValleysTimeConfig.getEndTime())) {
-                            jfpgStr.set(peaksAndValleysTimeConfig.getType());
-                            return;
+                    try {
+                        // 变更记录状态
+                        fpgGatherDataService.updateById(fpgGatherData.getId());
+                        String finalDatestr = "";
+                        // 统计单位
+                        if("minute". equals(fpgLeanModeInfo.getTotalUnit())){ // 分钟
+                            finalDatestr = new SimpleDateFormat("HH:mm").format(fpgGatherData.getCreateTime());
+                        } else if("hour". equals(fpgLeanModeInfo.getTotalUnit())){ // 小时
+                            finalDatestr = new SimpleDateFormat("HH").format(fpgGatherData.getCreateTime());
+                        } else if("day". equals(fpgLeanModeInfo.getTotalUnit())){ //
+                            finalDatestr = new SimpleDateFormat("dd").format(fpgGatherData.getCreateTime());
+                        } else if("week". equals(fpgLeanModeInfo.getTotalUnit())){ //
+                            Calendar calendar = Calendar.getInstance();
+                            finalDatestr = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
+                        } else if("month". equals(fpgLeanModeInfo.getTotalUnit())){ //
+                            finalDatestr = new SimpleDateFormat("MM").format(fpgGatherData.getCreateTime());
                         }
-                    });
-                    // 重组对象
-                    FpgStatiscsModelData fpgStatiscsModelData = new FpgStatiscsModelData(fpgGatherData, fpgLeanModeInfo, finalDatestr, dayDate, classs, curentDate, curentDate);
-                    Query query = new Query();
-                    query.addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId())).addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId())).addCriteria(Criteria.where("devicePointId").is(fpgGatherData.getDevicePointId())).addCriteria(Criteria.where("dates").is(dayDate)).addCriteria(Criteria.where("datestr").is(finalDatestr));
-                    if(!classs.isEmpty()){ // 增加班次条件
-                        query.addCriteria(Criteria.where("classs").is(classs));
-                    }
-                    FpgStatiscsModelMongodb FpgStatiscsModelMongo = mongoTemplate.findOne(query, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
-                    if(FpgStatiscsModelMongo != null){ // 不为空处理
-                        // 基础数据变更不受条件影响
-                        UpdateDefinition update = new Update();
-                        ((Update) update).set("updateTime", curentDate);
-                        // 功率
-                        BigDecimal activePower = new BigDecimal(String.valueOf(fpgGatherData.getActivePower()));
-                        BigDecimal runTime = new BigDecimal("1");
-                        BigDecimal oldPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getPower()));
-                        BigDecimal oldingTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getIngTime()));
-                        // 总运行时间处理
-                        if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1) { // 运行时长统计
-                            ((Update) update).set("ingTime", oldingTime.add(runTime));
-                        }
-                        // 总功率处理
-                        ((Update) update).set("power", oldPower.add(activePower));
-                        // 尖峰平谷判定
-                        if("tops".equals(jfpgStr.get())){ // 尖
-                            // 功率处理
-                            BigDecimal oldtopsPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getTopsPower()));
-                            ((Update) update).set("topsPower", oldtopsPower.add(activePower));
-                            // 运行时常处理
-                            if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
-                                BigDecimal oldtopsIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getTopsIngTime()));
-                                ((Update) update).set("topsIngTime", oldtopsIngTime.add(runTime));
-                            }
-                            // 金额处理
-                            if(fpgLeanModeInfo.getTopsPrice() != null && fpgLeanModeInfo.getTopsPrice().compareTo(new BigDecimal("0.00")) > 0) {
-                                // 总金额
-                                BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
-                                ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getTopsPrice()).add(totalAmount));
-                                ((Update) update).set("topsAmount", oldtopsPower.add(activePower).multiply(fpgLeanModeInfo.getTopsPrice()));
+                        String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(fpgGatherData.getCreateTime());
+                        String classs = "";
+                        // 采集时间
+                        Instant instant = fpgGatherData.getCreateTime().toInstant();
+                        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
+                        // 班次处理
+                        if (fpgLeanModeInfo.getClassUnit() != null && fpgLeanModeInfo.getClassUnit() == 1) { // 开启了班次统计
+                            if (containsCreateTime(localDateTime, dayDate + " 00:00:00", dayDate + " 07:59:59")) { // 夜班
+                                classs = "night";
+                            } else if (containsCreateTime(localDateTime, dayDate + " 08:00:00", dayDate + " 15:59:59")) { // 白班
+                                classs = "white";
+                            } else if (containsCreateTime(localDateTime, dayDate + " 16:00:00", dayDate + " 23:59:59")) { // 中班
+                                classs = "center";
                             }
-                        } else if("peaks".equals(jfpgStr.get())){ // 峰
-                            // 功率处理
-                            BigDecimal oldpeaksPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getPeaksPower()));
-                            ((Update) update).set("peaksPower", oldpeaksPower.add(activePower));
-                            // 运行时常处理
-                            if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
-                                BigDecimal oldpeaksIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getPeaksIngTime()));
-                                ((Update) update).set("peaksIngTime", oldpeaksIngTime.add(runTime));
-                            }
-                            // 金额处理
-                            if(fpgLeanModeInfo.getPeaksPrice() != null && fpgLeanModeInfo.getPeaksPrice().compareTo(new BigDecimal("0.00")) > 0) {
-                                // 总金额
-                                BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
-                                ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getPeaksPrice()).add(totalAmount));
-                                ((Update) update).set("topsAmount", oldpeaksPower.add(activePower).multiply(fpgLeanModeInfo.getPeaksPrice()));
-                            }
-                        } else if("flat".equals(jfpgStr.get())){ // 平
-                            // 功率处理
-                            BigDecimal oldflatPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getFlatPower()));
-                            ((Update) update).set("flatPower", oldflatPower.add(activePower));
-                            // 运行时常处理
-                            if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
-                                BigDecimal oldflatIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getTopsIngTime()));
-                                ((Update) update).set("topsIngTime", oldflatIngTime.add(runTime));
-                            }
-                            // 金额处理
-                            if(fpgLeanModeInfo.getFlatPrice() != null && fpgLeanModeInfo.getFlatPrice().compareTo(new BigDecimal("0.00")) > 0) {
-                                // 总金额
-                                BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
-                                ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getFlatPrice()).add(totalAmount));
-                                ((Update) update).set("flatAmount", oldflatPower.add(activePower).multiply(fpgLeanModeInfo.getFlatPrice()));
+                        }
+                        // 定义尖峰平谷
+                        AtomicReference<String> jfpgStr = new AtomicReference<>("");
+                        // 判断尖峰平谷时段
+                        peaksAndValleysTimeConfiglist.forEach(peaksAndValleysTimeConfig -> {
+                            if (containsCreateTime(localDateTime, dayDate + " " + peaksAndValleysTimeConfig.getStartTime(), dayDate + " " + peaksAndValleysTimeConfig.getEndTime())) {
+                                jfpgStr.set(peaksAndValleysTimeConfig.getType());
+                                return;
                             }
-                        } else if("valleys".equals(jfpgStr.get())){ // 谷
-                            // 功率处理
-                            BigDecimal oldvalleysPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getValleysPower()));
-                            ((Update) update).set("valleysPower", oldvalleysPower.add(activePower));
-                            // 运行时常处理
-                            if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
-                                BigDecimal oldvalleysIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getValleysIngTime()));
-                                ((Update) update).set("valleysIngTime", oldvalleysIngTime.add(runTime));
+                        });
+                        // 重组对象
+                        FpgStatiscsModelData fpgStatiscsModelData = new FpgStatiscsModelData(fpgGatherData, fpgLeanModeInfo, finalDatestr, dayDate, classs, curentDate, curentDate);
+                        Query query = new Query();
+                        query.addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId())).addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId())).addCriteria(Criteria.where("devicePointId").is(fpgGatherData.getDevicePointId())).addCriteria(Criteria.where("dates").is(dayDate)).addCriteria(Criteria.where("datestr").is(finalDatestr));
+                        if(!classs.isEmpty()){ // 增加班次条件
+                            query.addCriteria(Criteria.where("classs").is(classs));
+                        }
+                        FpgStatiscsModelMongodb FpgStatiscsModelMongo = mongoTemplate.findOne(query, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
+                        if(FpgStatiscsModelMongo != null){ // 不为空处理
+                            // 基础数据变更不受条件影响
+                            UpdateDefinition update = new Update();
+                            ((Update) update).set("updateTime", curentDate);
+                            // 功率
+                            BigDecimal activePower = new BigDecimal(String.valueOf(fpgGatherData.getActivePower()));
+                            BigDecimal runTime = new BigDecimal("1");
+                            BigDecimal oldPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getPower()));
+                            BigDecimal oldingTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getIngTime()));
+                            // 总运行时间处理
+                            if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1) { // 运行时长统计
+                                ((Update) update).set("ingTime", oldingTime.add(runTime));
                             }
-                            // 金额处理
-                            if(fpgLeanModeInfo.getValleysPrice() != null && fpgLeanModeInfo.getValleysPrice().compareTo(new BigDecimal("0.00")) > 0) {
-                                // 总金额
-                                BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
-                                ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getValleysPrice()).add(totalAmount));
-                                ((Update) update).set("valleysAmount", oldvalleysPower.add(activePower).multiply(fpgLeanModeInfo.getValleysPrice()));
+                            // 总功率处理
+                            ((Update) update).set("power", oldPower.add(activePower));
+                            // 尖峰平谷判定
+                            if("tops".equals(jfpgStr.get())){ // 尖
+                                // 功率处理
+                                BigDecimal oldtopsPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getTopsPower()));
+                                ((Update) update).set("topsPower", oldtopsPower.add(activePower));
+                                // 运行时常处理
+                                if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
+                                    BigDecimal oldtopsIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getTopsIngTime()));
+                                    ((Update) update).set("topsIngTime", oldtopsIngTime.add(runTime));
+                                }
+                                // 金额处理
+                                if(fpgLeanModeInfo.getTopsPrice() != null && fpgLeanModeInfo.getTopsPrice().compareTo(new BigDecimal("0.00")) > 0) {
+                                    // 总金额
+                                    BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
+                                    ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getTopsPrice()).add(totalAmount));
+                                    ((Update) update).set("topsAmount", oldtopsPower.add(activePower).multiply(fpgLeanModeInfo.getTopsPrice()));
+                                }
+                            } else if("peaks".equals(jfpgStr.get())){ // 峰
+                                // 功率处理
+                                BigDecimal oldpeaksPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getPeaksPower()));
+                                ((Update) update).set("peaksPower", oldpeaksPower.add(activePower));
+                                // 运行时常处理
+                                if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
+                                    BigDecimal oldpeaksIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getPeaksIngTime()));
+                                    ((Update) update).set("peaksIngTime", oldpeaksIngTime.add(runTime));
+                                }
+                                // 金额处理
+                                if(fpgLeanModeInfo.getPeaksPrice() != null && fpgLeanModeInfo.getPeaksPrice().compareTo(new BigDecimal("0.00")) > 0) {
+                                    // 总金额
+                                    BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
+                                    ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getPeaksPrice()).add(totalAmount));
+                                    ((Update) update).set("topsAmount", oldpeaksPower.add(activePower).multiply(fpgLeanModeInfo.getPeaksPrice()));
+                                }
+                            } else if("flat".equals(jfpgStr.get())){ // 平
+                                // 功率处理
+                                BigDecimal oldflatPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getFlatPower()));
+                                ((Update) update).set("flatPower", oldflatPower.add(activePower));
+                                // 运行时常处理
+                                if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
+                                    BigDecimal oldflatIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getTopsIngTime()));
+                                    ((Update) update).set("topsIngTime", oldflatIngTime.add(runTime));
+                                }
+                                // 金额处理
+                                if(fpgLeanModeInfo.getFlatPrice() != null && fpgLeanModeInfo.getFlatPrice().compareTo(new BigDecimal("0.00")) > 0) {
+                                    // 总金额
+                                    BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
+                                    ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getFlatPrice()).add(totalAmount));
+                                    ((Update) update).set("flatAmount", oldflatPower.add(activePower).multiply(fpgLeanModeInfo.getFlatPrice()));
+                                }
+                            } else if("valleys".equals(jfpgStr.get())){ // 谷
+                                // 功率处理
+                                BigDecimal oldvalleysPower = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getValleysPower()));
+                                ((Update) update).set("valleysPower", oldvalleysPower.add(activePower));
+                                // 运行时常处理
+                                if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
+                                    BigDecimal oldvalleysIngTime = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getValleysIngTime()));
+                                    ((Update) update).set("valleysIngTime", oldvalleysIngTime.add(runTime));
+                                }
+                                // 金额处理
+                                if(fpgLeanModeInfo.getValleysPrice() != null && fpgLeanModeInfo.getValleysPrice().compareTo(new BigDecimal("0.00")) > 0) {
+                                    // 总金额
+                                    BigDecimal totalAmount = new BigDecimal(String.valueOf(FpgStatiscsModelMongo.getAmount()));
+                                    ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getValleysPrice()).add(totalAmount));
+                                    ((Update) update).set("valleysAmount", oldvalleysPower.add(activePower).multiply(fpgLeanModeInfo.getValleysPrice()));
+                                }
                             }
+                            mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
+                        } else { // 为空直接插入
+                            // 将点位数据存入mongo中
+                            mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
                         }
-                        mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
-                    } else { // 为空直接插入
-                        // 将点位数据存入mongo中
-                        mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
+                    }catch (Exception e) {
+                        log.error(e.getMessage());
                     }
                 });
-            } catch (Exception e) {
-                log.error(e.getMessage());
-            }
         });
     }