123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- 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;
- import org.jeecg.modules.gatherData.entity.*;
- import org.jeecg.modules.gatherData.service.IFpgGatherDataService;
- import org.jeecg.modules.peaksAndValleysTimeConfig.entity.PeaksAndValleysTimeConfig;
- import org.jeecg.modules.peaksAndValleysTimeConfig.entity.SystemVariable;
- import org.jeecg.modules.peaksAndValleysTimeConfig.service.IPeaksAndValleysTimeConfigService;
- import org.jeecg.modules.peaksAndValleysTimeConfig.service.ISystemVariableService;
- 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.scheduling.annotation.EnableAsync;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- 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
- @EnableAsync
- @Component
- @EnableScheduling
- public class LeanModelStatistics {
- @Autowired
- IFpgLeanModelService fpgLeanModelService;
- @Autowired
- IFpgGatherDataService fpgGatherDataService;
- @Autowired
- IDevicePointService devicePointService;
- @Autowired
- IDeviceInformationService deviceInformationService;
- @Autowired
- private IPeaksAndValleysTimeConfigService peaksAndValleysTimeConfigService;
- @Autowired
- private ISystemVariableService systemVariableService;
- @Autowired
- MongoTemplate mongoTemplate;
- /**
- * 精益模型统计数据
- * 执行完后,间隔1秒继续执行
- */
- @Scheduled(fixedDelay = 10000)
- public void t1(){
- leanModel();
- }
- /**
- * 变更占比
- * 0 0 03 * * * 每日3点执行 测试期间1ms一次
- */
- @Scheduled(cron = "0 0 03 * * *")
- // @Scheduled(fixedDelay = 1000)
- public void t2(){
- changeProportion();
- }
- /**
- * 设备启停统计数据
- */
- @Scheduled(fixedDelay = 10000)
- public void t3(){
- deviceStatistics();
- }
- public void changeProportion(){
- log.info("变更占比");
- // 峰平谷精益模型配置获取
- LambdaQueryWrapper<FpgLeanModel> leanmodelQuery = new LambdaQueryWrapper<FpgLeanModel>().eq(FpgLeanModel::getStatus, "1");
- List<FpgLeanModel> fpgLeanModellist = fpgLeanModelService.list(leanmodelQuery);
- LocalDate yesterday = LocalDate.now().minusDays(1); // 获取今天的日期 减去一天,得到昨天的日期
- // 处理数据业务逻辑
- fpgLeanModellist.forEach(fpgLeanModeInfo -> {
- Query query = new Query();
- query.addCriteria(Criteria.where("proportion").is(1))
- .addCriteria(Criteria.where("dates").is(yesterday.toString()));
- List<FpgStatiscsModelMongodb> fpgStatiscsModelMongoList = mongoTemplate.find(query, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
- BigDecimal zb = new BigDecimal("100");
- fpgStatiscsModelMongoList.forEach(fpgStatiscsModelMongoInfo -> { // 循环处理采集数据处理
- BigDecimal proportion = fpgStatiscsModelMongoInfo.getPower();
- BigDecimal topsProportion = fpgStatiscsModelMongoInfo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getTopsPower();
- BigDecimal peaksProportion = fpgStatiscsModelMongoInfo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getPeaksPower();
- BigDecimal flatProportion = fpgStatiscsModelMongoInfo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getPeaksPower();
- BigDecimal valleysProportion = fpgStatiscsModelMongoInfo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getPeaksPower();
- // 核算占比
- UpdateDefinition update = new Update();
- ((Update) update).set("proportion", 2);
- if(topsProportion.compareTo(new BigDecimal("0.00")) > 0) { // 尖
- ((Update) update).set("topsProportion", topsProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- if(peaksProportion.compareTo(new BigDecimal("0.00")) > 0) { // 峰
- ((Update) update).set("peaksProportion", peaksProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- if(flatProportion.compareTo(new BigDecimal("0.00")) > 0) { // 平
- ((Update) update).set("flatProportion", flatProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- if(valleysProportion.compareTo(new BigDecimal("0.00")) > 0) { // 谷
- ((Update) update).set("valleysProportion", valleysProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- // 保存
- mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
- });
- });
- }
- // 设备启停统计数据
- public void deviceStatistics() {
- log.info("设备日常统计");
- // 获取当前时间
- LocalDateTime newTime = LocalDateTime.now().plusHours(8);
- Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
- // 峰平谷设备列表
- List<DeviceInformation> deviceInformationllist = deviceInformationService.list();
- // 尖峰平谷时段配置
- List<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
- BigDecimal zb = new BigDecimal("100");
- //遍历设备集合
- deviceInformationllist.forEach(deviceInformationlinfo -> {
- // 查询采集点
- // LambdaQueryWrapper<DevicePoint> devicePointQuery = new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId, deviceInformationlinfo.getId());
- // List<DevicePoint> devicePointList = devicePointService.list(devicePointQuery);
- // if(oConvertUtils.listIsEmpty(devicePointList)){
- // return;
- // }
- // 采集点组合
- // List<String> idsPoint = devicePointList.stream().map(DevicePoint::getId).collect(Collectors.toList());
- // 查询设备对应数据
- LambdaQueryWrapper<FpgGatherData> fpgGatherQuery = new LambdaQueryWrapper<FpgGatherData>().isNull(FpgGatherData::getFpgTotalUpdatetime).eq(FpgGatherData::getDeviceInformationId, deviceInformationlinfo.getId());
- List<FpgGatherData> fpgGatherList = fpgGatherDataService.list(fpgGatherQuery);
- // 系统变量查询
- QueryWrapper<SystemVariable> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq("variable_address", "sys_run_current_limit");
- queryWrapper.eq("status", 0);
- SystemVariable systemVariable = systemVariableService.getOne(queryWrapper);
- // 处理数据业务逻辑
- fpgGatherList.forEach(fpgGatherData -> { // 循环处理采集数据处理
- try {
- // 采集时间
- Instant instant = fpgGatherData.getCreateTime().toInstant();
- LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
- // 上报日期
- String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(fpgGatherData.getCreateTime());
- String classs = "";
- // 班次处理
- 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());
- }
- });
- // 功率
- BigDecimal runPower = fpgGatherData.getActivePower().compareTo(new BigDecimal("0.00")) > 0 ? fpgGatherData.getActivePower().divide(new BigDecimal(12), 6, RoundingMode.HALF_UP) : fpgGatherData.getActivePower();
- // 运行时常
- BigDecimal runTime = new BigDecimal("300");
- // 电流
- BigDecimal selectricCurrent = fpgGatherData.getRunCurrent();
- // 设备启停统计 start
- String startStopMonKey = "total_startstop";
- DeviceStatiscsModelData startStopStatiscsData = new DeviceStatiscsModelData(fpgGatherData, startStopMonKey, dayDate, classs, curentDate, curentDate);
- // 组合mongodb条件
- Query queryStartStop = new Query()
- .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()))
- .addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()));
- // 设备停止 start
- if(fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) < 0){ // 设备停止运行 直接更新停止时间结束不用统计任何东西
- // queryStartStop.addCriteria(Criteria.where("deviceStopTime").is(null));
- UpdateDefinition updateStartStop = new Update()
- .set("updateTime", curentDate).set("deviceStopTime", DateUtils.date2Str(curentDate, DateUtils.datetimeFormat.get()));
- mongoTemplate.updateFirst(queryStartStop, updateStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
- return;
- } else { // 设备运行状态
- // 查找上一次运行记录
- DeviceStatiscsModelMongodb startStopMongo = mongoTemplate.findOne(queryStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
- if(startStopMongo != null){
- // 历史数据
- 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();
- // 更新启动运行数据
- UpdateDefinition updateStartStop = new Update()
- .set("power", runPower.compareTo(new BigDecimal("0.00")) > 0 ? oldPower.add(runPower) : 0.00)
- .set("selectricCurrent", oldSelectricCurrent.add(selectricCurrent))
- .set("ingTime", oldRunTime.add(runTime))
- .set("updateTime", curentDate);
- mongoTemplate.updateFirst(queryStartStop, updateStartStop, FpgStatiscsModelMongodb.class, startStopMonKey);
- } else { // 直接插入
- mongoTemplate.insert(startStopStatiscsData, startStopMonKey);
- }
- }
- // 设备启停统计 end
- // 日统计尖峰平谷数据(功率+占比+电流+运行时长) start
- String dayPowerproportioncurrentRunMonKey = "total_day_powerproportioncurrent";
- DeviceStatiscsModelData dayPowerproportioncurrentRunData = new DeviceStatiscsModelData(fpgGatherData, dayPowerproportioncurrentRunMonKey, dayDate, classs, curentDate, curentDate);
- // 组合mongodb条件
- Query queryDayPowerproportioncurrentRun = new Query()
- .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId())).addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId())).addCriteria(Criteria.where("dates").is(dayDate));
- DeviceStatiscsModelMongodb dayPowerproportioncurrentMongo = mongoTemplate.findOne(queryDayPowerproportioncurrentRun, DeviceStatiscsModelMongodb.class, dayPowerproportioncurrentRunMonKey);
- if(dayPowerproportioncurrentMongo != null){ // 不为空更新
- // 历史数据
- BigDecimal runNewPower = dayPowerproportioncurrentMongo.getPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getPower().add(runPower);
- BigDecimal oldRunTime = dayPowerproportioncurrentMongo.getIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getIngTime();
- BigDecimal oldSelectricCurrent = dayPowerproportioncurrentMongo.getSelectricCurrent() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getSelectricCurrent();
- // 更新运行数据
- UpdateDefinition update = new Update();
- ((Update) update).set("power", runNewPower);
- ((Update) update).set("selectricCurrent", oldSelectricCurrent.add(selectricCurrent));
- ((Update) update).set("ingTime", oldRunTime.add(runTime));
- ((Update) update).set("updateTime", curentDate);
- // 尖峰平谷判定
- if("tops".equals(jfpgStr.get())){ // 尖
- // 功率处理
- BigDecimal oldtopsPower = dayPowerproportioncurrentMongo.getTopsPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getTopsPower();
- BigDecimal topsPower = oldtopsPower.add(runPower);
- ((Update) update).set("topsPower", topsPower);
- // 运行时常处理
- BigDecimal oldtopsIngTime = dayPowerproportioncurrentMongo.getTopsIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getTopsIngTime();
- ((Update) update).set("topsIngTime", oldtopsIngTime.add(runTime));
- // 占比
- if(topsPower.compareTo(new BigDecimal("0.00")) > 0) {
- ((Update) update).set("topsProportion", topsPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- } else if("peaks".equals(jfpgStr.get())){ // 峰
- // 功率处理
- BigDecimal oldpeaksPower = dayPowerproportioncurrentMongo.getPeaksPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getPeaksPower();
- BigDecimal peaksPower = oldpeaksPower.add(runPower);
- ((Update) update).set("peaksPower", peaksPower);
- // 运行时常处理
- BigDecimal oldpeaksIngTime = dayPowerproportioncurrentMongo.getPeaksIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getPeaksIngTime();
- ((Update) update).set("peaksIngTime", oldpeaksIngTime.add(runTime));
- // 占比
- if(peaksPower.compareTo(new BigDecimal("0.00")) > 0) {
- ((Update) update).set("peaksProportion", peaksPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- } else if("flat".equals(jfpgStr.get())){ // 平
- // 功率处理
- BigDecimal oldflatPower = dayPowerproportioncurrentMongo.getFlatPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getFlatPower();
- BigDecimal flatPower = oldflatPower.add(runPower);
- ((Update) update).set("flatPower", flatPower);
- // 运行时常处理
- BigDecimal oldflatIngTime = dayPowerproportioncurrentMongo.getFlatIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getFlatIngTime();
- ((Update) update).set("topsIngTime", oldflatIngTime.add(runTime));
- // 占比
- if(flatPower.compareTo(new BigDecimal("0.00")) > 0) {
- ((Update) update).set("flatProportion", flatPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- } else if("valleys".equals(jfpgStr.get())){ // 谷
- // 功率处理
- BigDecimal oldvalleysPower = dayPowerproportioncurrentMongo.getValleysPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getValleysPower();
- BigDecimal valleysPower = oldvalleysPower.add(runPower);
- ((Update) update).set("valleysPower", oldvalleysPower.add(runPower));
- // 运行时常处理
- BigDecimal oldvalleysIngTime = dayPowerproportioncurrentMongo.getValleysIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getValleysIngTime();
- ((Update) update).set("valleysIngTime", oldvalleysIngTime.add(runTime));
- // 占比
- if(valleysPower.compareTo(new BigDecimal("0.00")) > 0) {
- ((Update) update).set("valleysProportion", valleysPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
- }
- }
- mongoTemplate.updateFirst(queryDayPowerproportioncurrentRun, update, DeviceStatiscsModelMongodb.class, dayPowerproportioncurrentRunMonKey);
- } else { // 为空直接插入
- mongoTemplate.insert(dayPowerproportioncurrentRunData, dayPowerproportioncurrentRunMonKey);
- }
- // 日统计尖峰平谷数据(功率+占比+电流+运行时长) end
- // 执行完成数据 标记已处理
- fpgGatherData.setFpgTotalUpdatetime(new Date());
- fpgGatherData.setId(fpgGatherData.getId());
- fpgGatherDataService.updateById(fpgGatherData);
- } catch (Exception e) {
- log.error(e.getMessage());
- }
- });
- });
- }
- // 精益模型统计数据
- public void leanModel(){
- log.info("峰平谷模型");
- // 峰平谷精益模型配置获取
- LambdaQueryWrapper<FpgLeanModel> leanmodelQuery = new LambdaQueryWrapper<FpgLeanModel>().eq(FpgLeanModel::getStatus, "1");
- 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集合
- List<FpgGatherData> fpgGatherUpdateList= new ArrayList<>();
- // 遍历设备集合
- fpgLeanModellist.forEach(fpgLeanModeInfo -> {
- LambdaQueryWrapper<FpgGatherData> fpgGatherQuery = new LambdaQueryWrapper<FpgGatherData>().isNull(FpgGatherData::getFpgModelUpdatetime);
- // 检测是否配置设备特殊条件
- if(oConvertUtils.isNotEmpty(fpgLeanModeInfo.getDeviceInformationIds()) && oConvertUtils.isNotEmpty(fpgLeanModeInfo.getDeviceInformationIds())){
- fpgGatherQuery.in(FpgGatherData::getDeviceInformationId, Arrays.asList(fpgLeanModeInfo.getDeviceInformationIds().split(",")));
- }
- List<FpgGatherData> fpgGatherList = fpgGatherDataService.list(fpgGatherQuery);
- // 处理数据业务逻辑
- fpgGatherList.forEach(fpgGatherData -> { // 循环处理采集数据处理
- fpgGatherData.setFpgModelUpdatetime(new Date());
- try {
- 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:00").format(fpgGatherData.getCreateTime());
- } else if("day".equals(fpgLeanModeInfo.getTotalUnit())){ //
- finalDatestr = new SimpleDateFormat("yyyy-MM-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("yyyy-MM").format(fpgGatherData.getCreateTime());
- } else if("real_time". equals(fpgLeanModeInfo.getTotalUnit())){ // 实时
- fpgLeanModeInfo.setProportion(2);
- fpgLeanModeInfo.setElectricCurrent(2);
- fpgLeanModeInfo.setRunTime(2);
- finalDatestr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").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";
- }
- }
- // 重组对象
- 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());
- mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
- fpgGatherUpdateList.add(fpgGatherData);
- return;
- }
- // 定义尖峰平谷
- AtomicReference<String> jfpgStr = new AtomicReference<>("");
- // 判断尖峰平谷时段
- peaksAndValleysTimeConfiglist.forEach(peaksAndValleysTimeConfig -> {
- if (containsCreateTime(localDateTime, dayDate + " " + peaksAndValleysTimeConfig.getStartTime(), dayDate + " " + peaksAndValleysTimeConfig.getEndTime())) {
- jfpgStr.set(peaksAndValleysTimeConfig.getType());
- }
- });
- // 组合mongodb条件
- Query query = new Query();
- query.addCriteria(Criteria.where("devicePointId").is(fpgGatherData.getDevicePointId()))
- .addCriteria(Criteria.where("dates").is(dayDate))
- .addCriteria(Criteria.where("datestr").is(finalDatestr));
- if(fpgGatherData.getDeviceRegionId() != null && !fpgGatherData.getDeviceRegionId().isEmpty()){ // 区域特殊处理
- query.addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()));
- }
- if(fpgGatherData.getDeviceInformationId() != null && !fpgGatherData.getDeviceInformationId().isEmpty()){ // 设备特殊处理
- query.addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()));
- }
- 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);
- // 功率 5 分钟上报一次 除以 12
- BigDecimal activePower = fpgGatherData.getActivePower() == null ? new BigDecimal("0.00") : fpgGatherData.getActivePower().divide(new BigDecimal("12"), 6, RoundingMode.HALF_UP);
- // 电流
- BigDecimal selectricCurrent = fpgGatherData.getRunCurrent() == null ? new BigDecimal("0.00") : fpgGatherData.getRunCurrent();
- BigDecimal runTime = new BigDecimal("300");
- BigDecimal oldPower = fpgStatiscsModelMongo.getPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPower();
- BigDecimal oldingTime = fpgStatiscsModelMongo.getIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getIngTime();
- // 总运行时间处理
- if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1) { // 运行时长统计
- ((Update) update).set("ingTime", oldingTime.add(runTime));
- }
- // 电流处理
- if(fpgLeanModeInfo.getElectricCurrent() != null && fpgLeanModeInfo.getElectricCurrent() == 1){
- if("tops".equals(jfpgStr.get())) { // 尖
- BigDecimal oldtopsSelectricCurrent = fpgStatiscsModelMongo.getTopsSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsSelectricCurrent();
- ((Update) update).set("topSelectricCurrent", oldtopsSelectricCurrent.add(selectricCurrent));
- } else if("peaks".equals(jfpgStr.get())){ // 峰
- BigDecimal oldpeaksSelectricCurrent = fpgStatiscsModelMongo.getPeaksSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksSelectricCurrent();
- ((Update) update).set("peaksSelectricCurrent", oldpeaksSelectricCurrent.add(selectricCurrent));
- } else if("flat".equals(jfpgStr.get())) { // 平
- BigDecimal oldtflatSelectricCurrent = fpgStatiscsModelMongo.getFlatSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatSelectricCurrent();
- ((Update) update).set("flatSelectricCurrent", oldtflatSelectricCurrent.add(selectricCurrent));
- } else if("valleys".equals(jfpgStr.get())) { // 谷
- BigDecimal oldvalleysSelectricCurrent = fpgStatiscsModelMongo.getValleysSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysSelectricCurrent();
- ((Update) update).set("valleysSelectricCurrent", oldvalleysSelectricCurrent.add(selectricCurrent));
- }
- }
- // 总功率处理
- ((Update) update).set("power", oldPower.add(activePower));
- // 尖峰平谷判定
- if("tops".equals(jfpgStr.get())){ // 尖
- // 功率处理
- BigDecimal oldtopsPower = fpgStatiscsModelMongo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsPower();
- ((Update) update).set("topsPower", oldtopsPower.add(activePower));
- // 运行时常处理
- 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));
- }
- // 金额处理
- if(fpgLeanModeInfo.getTopsPrice() != null && fpgLeanModeInfo.getTopsPrice().compareTo(new BigDecimal("0.00")) > 0) {
- // 总金额
- BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : 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 = fpgStatiscsModelMongo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksPower();
- ((Update) update).set("peaksPower", oldpeaksPower.add(activePower));
- // 运行时常处理
- 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));
- }
- // 金额处理
- if(fpgLeanModeInfo.getPeaksPrice() != null && fpgLeanModeInfo.getPeaksPrice().compareTo(new BigDecimal("0.00")) > 0) {
- // 总金额
- 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()));
- }
- } else if("flat".equals(jfpgStr.get())){ // 平
- // 功率处理
- BigDecimal oldflatPower = fpgStatiscsModelMongo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatPower();
- ((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));
- }
- // 金额处理
- if(fpgLeanModeInfo.getFlatPrice() != null && fpgLeanModeInfo.getFlatPrice().compareTo(new BigDecimal("0.00")) > 0) {
- // 总金额
- BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : 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 = fpgStatiscsModelMongo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysPower();
- ((Update) update).set("valleysPower", oldvalleysPower.add(activePower));
- // 运行时常处理
- 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));
- }
- // 金额处理
- if(fpgLeanModeInfo.getValleysPrice() != null && fpgLeanModeInfo.getValleysPrice().compareTo(new BigDecimal("0.00")) > 0) {
- // 总金额
- BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getAmount();
- ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getValleysPrice()).add(totalAmount));
- ((Update) update).set("valleysAmount", oldvalleysPower.add(activePower).multiply(fpgLeanModeInfo.getValleysPrice()));
- }
- }
- fpgGatherUpdateList.add(fpgGatherData);
- mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
- } else { // 为空直接插入
- fpgGatherUpdateList.add(fpgGatherData);
- // 将点位数据存入mongo中
- mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
- }
- }catch (Exception e) {
- log.error(e.getMessage());
- }
- });
- });
- // 数据处理完成标记已完成信息,以免重复处理
- fpgGatherDataService.updateBatchById(fpgGatherUpdateList);
- }
- /**
- * 判断创建时间是否在开始时间和结束时间范围内
- * @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));
- }
- }
|