|
@@ -6,22 +6,28 @@ import org.jeecg.modules.fpgJob.entity.FpgLeanModel;
|
|
|
import org.jeecg.modules.fpgJob.service.IFpgLeanModelService;
|
|
|
import org.jeecg.modules.gatherData.entity.FpgGatherData;
|
|
|
import org.jeecg.modules.gatherData.entity.FpgStatiscsModelData;
|
|
|
+import org.jeecg.modules.gatherData.entity.FpgStatiscsModelMongodb;
|
|
|
import org.jeecg.modules.gatherData.service.IFpgGatherDataService;
|
|
|
+import org.jeecg.modules.peaksAndValleysTimeConfig.entity.PeaksAndValleysTimeConfig;
|
|
|
+import org.jeecg.modules.peaksAndValleysTimeConfig.service.IPeaksAndValleysTimeConfigService;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
+import org.springframework.data.mongodb.core.query.UpdateDefinition;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.time.LocalTime;
|
|
|
-import java.util.Calendar;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
+import java.time.*;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
-import static org.bouncycastle.asn1.x500.style.RFC4519Style.c;
|
|
|
|
|
|
@Slf4j
|
|
|
@EnableAsync
|
|
@@ -35,6 +41,9 @@ public class LeanModelStatistics {
|
|
|
@Autowired
|
|
|
IFpgGatherDataService fpgGatherDataService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IPeaksAndValleysTimeConfigService peaksAndValleysTimeConfigService;
|
|
|
+
|
|
|
@Autowired
|
|
|
MongoTemplate mongoTemplate;
|
|
|
|
|
@@ -54,47 +63,166 @@ public class LeanModelStatistics {
|
|
|
log.info("峰平谷模型");
|
|
|
// 获取当前时间
|
|
|
Date curentDate = new Date();
|
|
|
+ // 尖峰平谷时段配置
|
|
|
+ List<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
|
|
|
//遍历设备集合
|
|
|
fpgLeanModellist.forEach(fpgLeanModeInfo -> {
|
|
|
- String datestr = "";
|
|
|
- String classs = "";
|
|
|
try {
|
|
|
- // 统计单位
|
|
|
- if("hour". equals(fpgLeanModeInfo.getTotalUnit())){ // 小时
|
|
|
- datestr = new SimpleDateFormat("H").format(curentDate);
|
|
|
- } else if("day". equals(fpgLeanModeInfo.getTotalUnit())){ //
|
|
|
- datestr = new SimpleDateFormat("dd").format(curentDate);
|
|
|
- } else if("week". equals(fpgLeanModeInfo.getTotalUnit())){ //
|
|
|
- Calendar calendar = Calendar.getInstance();
|
|
|
- datestr = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
|
|
|
- } else if("month". equals(fpgLeanModeInfo.getTotalUnit())){ //
|
|
|
- datestr = new SimpleDateFormat("MM").format(curentDate);
|
|
|
- }
|
|
|
- // 班次处理
|
|
|
- if(fpgLeanModeInfo.getClassUnit() == 1){ // 开启了班次统计
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
- LocalDateTime nightStartTime = LocalDateTime.of(now.toLocalDate(), LocalTime.of(0, 0));
|
|
|
- LocalDateTime nightEndTime = LocalDateTime.of(now.toLocalDate(), LocalTime.of(8, 0));
|
|
|
- LocalDateTime whiteStartTime = LocalDateTime.of(now.toLocalDate(), LocalTime.of(8, 0));
|
|
|
- LocalDateTime whiteEndTime = LocalDateTime.of(now.toLocalDate(), LocalTime.of(16, 0));
|
|
|
- LocalDateTime centerStartTime = LocalDateTime.of(now.toLocalDate(), LocalTime.of(16, 0));
|
|
|
- LocalDateTime centerEndTime = LocalDateTime.of(now.toLocalDate(), LocalTime.of(0, 0));
|
|
|
- if(now.isBefore(nightStartTime) && now.isBefore(nightEndTime)){ // 夜班
|
|
|
- classs = "night";
|
|
|
- } else if(now.isBefore(whiteStartTime) && now.isBefore(whiteEndTime)){ // 白班
|
|
|
- classs = "white";
|
|
|
- } else if(now.isBefore(centerStartTime) && now.isBefore(centerEndTime)){ // 中班
|
|
|
- classs = "center";
|
|
|
+ // 处理数据业务逻辑
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 重组对象
|
|
|
+ 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));
|
|
|
}
|
|
|
- }
|
|
|
- FpgGatherData FpgGatherData = fpgGatherDataService.getById("1836591252581129108");
|
|
|
- //将点位数据存入mongo中
|
|
|
- mongoTemplate.insert(new FpgStatiscsModelData(FpgGatherData, fpgLeanModeInfo, datestr, new SimpleDateFormat("yyyy-MM-dd").format(curentDate), classs, curentDate, curentDate), "leanmodel_" +fpgLeanModeInfo.getLeanModelCode());
|
|
|
+ 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()));
|
|
|
+ }
|
|
|
+ } 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());
|
|
|
+ }
|
|
|
+ });
|
|
|
} catch (Exception e) {
|
|
|
log.error(e.getMessage());
|
|
|
- } finally {
|
|
|
-
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断创建时间是否在开始时间和结束时间范围内
|
|
|
+ * @param createTime
|
|
|
+ * @param startTime
|
|
|
+ * @param endTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean containsCreateTime(LocalDateTime createTime, String startTime, String endTime) {
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ LocalDateTime start_time = LocalDateTime.parse(startTime, formatter);
|
|
|
+ LocalDateTime end_time = LocalDateTime.parse(endTime, formatter);
|
|
|
+ return createTime!= null && (createTime.isAfter(start_time) || createTime.equals(start_time)) &&
|
|
|
+ (createTime.isBefore(end_time) || createTime.equals(end_time));
|
|
|
+ }
|
|
|
}
|