LeanModelStatistics.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package org.jeecg.modules.fpgJob;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import lombok.Data;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.jeecg.common.util.DateUtils;
  7. import org.jeecg.common.util.oConvertUtils;
  8. import org.jeecg.modules.device.entity.DeviceInformation;
  9. import org.jeecg.modules.device.service.IDeviceInformationService;
  10. import org.jeecg.modules.devicePoint.entity.DevicePoint;
  11. import org.jeecg.modules.devicePoint.service.IDevicePointService;
  12. import org.jeecg.modules.fpgJob.entity.FpgLeanModel;
  13. import org.jeecg.modules.fpgJob.service.IFpgLeanModelService;
  14. import org.jeecg.modules.gatherData.entity.*;
  15. import org.jeecg.modules.gatherData.service.IFpgGatherDataService;
  16. import org.jeecg.modules.peaksAndValleysTimeConfig.entity.PeaksAndValleysTimeConfig;
  17. import org.jeecg.modules.peaksAndValleysTimeConfig.entity.SystemVariable;
  18. import org.jeecg.modules.peaksAndValleysTimeConfig.service.IPeaksAndValleysTimeConfigService;
  19. import org.jeecg.modules.peaksAndValleysTimeConfig.service.ISystemVariableService;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.data.mongodb.core.MongoTemplate;
  22. import org.springframework.data.mongodb.core.query.Criteria;
  23. import org.springframework.data.mongodb.core.query.Query;
  24. import org.springframework.data.mongodb.core.query.Update;
  25. import org.springframework.data.mongodb.core.query.UpdateDefinition;
  26. import org.springframework.scheduling.annotation.EnableAsync;
  27. import org.springframework.scheduling.annotation.EnableScheduling;
  28. import org.springframework.scheduling.annotation.Scheduled;
  29. import org.springframework.stereotype.Component;
  30. import java.math.BigDecimal;
  31. import java.math.RoundingMode;
  32. import java.text.SimpleDateFormat;
  33. import java.time.*;
  34. import java.time.format.DateTimeFormatter;
  35. import java.util.*;
  36. import java.util.concurrent.atomic.AtomicReference;
  37. import java.util.stream.Collectors;
  38. @Slf4j
  39. @EnableAsync
  40. @Component
  41. @EnableScheduling
  42. public class LeanModelStatistics {
  43. @Autowired
  44. IFpgLeanModelService fpgLeanModelService;
  45. @Autowired
  46. IFpgGatherDataService fpgGatherDataService;
  47. @Autowired
  48. IDevicePointService devicePointService;
  49. @Autowired
  50. IDeviceInformationService deviceInformationService;
  51. @Autowired
  52. private IPeaksAndValleysTimeConfigService peaksAndValleysTimeConfigService;
  53. @Autowired
  54. private ISystemVariableService systemVariableService;
  55. @Autowired
  56. MongoTemplate mongoTemplate;
  57. /**
  58. * 精益模型统计数据
  59. * 执行完后,间隔1秒继续执行
  60. */
  61. @Scheduled(fixedDelay = 10000)
  62. public void t1(){
  63. leanModel();
  64. }
  65. /**
  66. * 变更占比
  67. * 0 0 03 * * * 每日3点执行 测试期间1ms一次
  68. */
  69. @Scheduled(cron = "0 0 03 * * *")
  70. // @Scheduled(fixedDelay = 1000)
  71. public void t2(){
  72. changeProportion();
  73. }
  74. /**
  75. * 设备启停统计数据
  76. */
  77. @Scheduled(fixedDelay = 10000)
  78. public void t3(){
  79. deviceStatistics();
  80. }
  81. public void changeProportion(){
  82. log.info("变更占比");
  83. // 峰平谷精益模型配置获取
  84. LambdaQueryWrapper<FpgLeanModel> leanmodelQuery = new LambdaQueryWrapper<FpgLeanModel>().eq(FpgLeanModel::getStatus, "1");
  85. List<FpgLeanModel> fpgLeanModellist = fpgLeanModelService.list(leanmodelQuery);
  86. LocalDate yesterday = LocalDate.now().minusDays(1); // 获取今天的日期 减去一天,得到昨天的日期
  87. // 处理数据业务逻辑
  88. fpgLeanModellist.forEach(fpgLeanModeInfo -> {
  89. Query query = new Query();
  90. query.addCriteria(Criteria.where("proportion").is(1))
  91. .addCriteria(Criteria.where("dates").is(yesterday.toString()));
  92. List<FpgStatiscsModelMongodb> fpgStatiscsModelMongoList = mongoTemplate.find(query, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
  93. BigDecimal zb = new BigDecimal("100");
  94. fpgStatiscsModelMongoList.forEach(fpgStatiscsModelMongoInfo -> { // 循环处理采集数据处理
  95. BigDecimal proportion = fpgStatiscsModelMongoInfo.getPower();
  96. BigDecimal topsProportion = fpgStatiscsModelMongoInfo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getTopsPower();
  97. BigDecimal peaksProportion = fpgStatiscsModelMongoInfo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getPeaksPower();
  98. BigDecimal flatProportion = fpgStatiscsModelMongoInfo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getPeaksPower();
  99. BigDecimal valleysProportion = fpgStatiscsModelMongoInfo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongoInfo.getPeaksPower();
  100. // 核算占比
  101. UpdateDefinition update = new Update();
  102. ((Update) update).set("proportion", 2);
  103. if(topsProportion.compareTo(new BigDecimal("0.00")) > 0) { // 尖
  104. ((Update) update).set("topsProportion", topsProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
  105. }
  106. if(peaksProportion.compareTo(new BigDecimal("0.00")) > 0) { // 峰
  107. ((Update) update).set("peaksProportion", peaksProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
  108. }
  109. if(flatProportion.compareTo(new BigDecimal("0.00")) > 0) { // 平
  110. ((Update) update).set("flatProportion", flatProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
  111. }
  112. if(valleysProportion.compareTo(new BigDecimal("0.00")) > 0) { // 谷
  113. ((Update) update).set("valleysProportion", valleysProportion.divide(proportion, 2, RoundingMode.HALF_UP).multiply(zb));
  114. }
  115. // 保存
  116. mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
  117. });
  118. });
  119. }
  120. // 设备启停统计数据
  121. public void deviceStatistics() {
  122. log.info("设备日常统计");
  123. // 获取当前时间
  124. LocalDateTime newTime = LocalDateTime.now().plusHours(8);
  125. Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
  126. // 峰平谷设备列表
  127. List<DeviceInformation> deviceInformationllist = deviceInformationService.list();
  128. // 尖峰平谷时段配置
  129. List<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
  130. BigDecimal zb = new BigDecimal("100");
  131. //遍历设备集合
  132. deviceInformationllist.forEach(deviceInformationlinfo -> {
  133. // 查询采集点
  134. // LambdaQueryWrapper<DevicePoint> devicePointQuery = new LambdaQueryWrapper<DevicePoint>().eq(DevicePoint::getDeviceId, deviceInformationlinfo.getId());
  135. // List<DevicePoint> devicePointList = devicePointService.list(devicePointQuery);
  136. // if(oConvertUtils.listIsEmpty(devicePointList)){
  137. // return;
  138. // }
  139. // 采集点组合
  140. // List<String> idsPoint = devicePointList.stream().map(DevicePoint::getId).collect(Collectors.toList());
  141. // 查询设备对应数据
  142. LambdaQueryWrapper<FpgGatherData> fpgGatherQuery = new LambdaQueryWrapper<FpgGatherData>().isNull(FpgGatherData::getFpgTotalUpdatetime).eq(FpgGatherData::getDeviceInformationId, deviceInformationlinfo.getId());
  143. List<FpgGatherData> fpgGatherList = fpgGatherDataService.list(fpgGatherQuery);
  144. // 系统变量查询
  145. QueryWrapper<SystemVariable> queryWrapper = new QueryWrapper<>();
  146. queryWrapper.eq("variable_address", "sys_run_current_limit");
  147. queryWrapper.eq("status", 0);
  148. SystemVariable systemVariable = systemVariableService.getOne(queryWrapper);
  149. // 处理数据业务逻辑
  150. fpgGatherList.forEach(fpgGatherData -> { // 循环处理采集数据处理
  151. try {
  152. // 采集时间
  153. Instant instant = fpgGatherData.getCreateTime().toInstant();
  154. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
  155. // 上报日期
  156. String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(fpgGatherData.getCreateTime());
  157. String classs = "";
  158. // 班次处理
  159. if (containsCreateTime(localDateTime, dayDate + " 00:00:00", dayDate + " 07:59:59")) { // 夜班
  160. classs = "night";
  161. } else if (containsCreateTime(localDateTime, dayDate + " 08:00:00", dayDate + " 15:59:59")) { // 白班
  162. classs = "white";
  163. } else if (containsCreateTime(localDateTime, dayDate + " 16:00:00", dayDate + " 23:59:59")) { // 中班
  164. classs = "center";
  165. }
  166. // 定义尖峰平谷
  167. AtomicReference<String> jfpgStr = new AtomicReference<>("");
  168. // 判断尖峰平谷时段
  169. peaksAndValleysTimeConfiglist.forEach(peaksAndValleysTimeConfig -> {
  170. if (containsCreateTime(localDateTime, dayDate + " " + peaksAndValleysTimeConfig.getStartTime(), dayDate + " " + peaksAndValleysTimeConfig.getEndTime())) {
  171. jfpgStr.set(peaksAndValleysTimeConfig.getType());
  172. }
  173. });
  174. // 功率
  175. BigDecimal runPower = fpgGatherData.getActivePower().compareTo(new BigDecimal("0.00")) > 0 ? fpgGatherData.getActivePower().divide(new BigDecimal(12), 6, RoundingMode.HALF_UP) : fpgGatherData.getActivePower();
  176. // 运行时常
  177. BigDecimal runTime = new BigDecimal("300");
  178. // 电流
  179. BigDecimal selectricCurrent = fpgGatherData.getRunCurrent();
  180. // 设备启停统计 start
  181. String startStopMonKey = "total_startstop";
  182. DeviceStatiscsModelData startStopStatiscsData = new DeviceStatiscsModelData(fpgGatherData, startStopMonKey, dayDate, classs, curentDate, curentDate);
  183. // 组合mongodb条件
  184. Query queryStartStop = new Query()
  185. .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()))
  186. .addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()));
  187. // 设备停止 start
  188. if(fpgGatherData.getRunCurrent().compareTo(new BigDecimal(systemVariable.getDefaultValue())) < 0){ // 设备停止运行 直接更新停止时间结束不用统计任何东西
  189. // queryStartStop.addCriteria(Criteria.where("deviceStopTime").is(null));
  190. UpdateDefinition updateStartStop = new Update()
  191. .set("updateTime", curentDate).set("deviceStopTime", DateUtils.date2Str(curentDate, DateUtils.datetimeFormat.get()));
  192. mongoTemplate.updateFirst(queryStartStop, updateStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
  193. return;
  194. } else { // 设备运行状态
  195. // 查找上一次运行记录
  196. DeviceStatiscsModelMongodb startStopMongo = mongoTemplate.findOne(queryStartStop, DeviceStatiscsModelMongodb.class, startStopMonKey);
  197. if(startStopMongo != null){
  198. // 历史数据
  199. BigDecimal oldPower = startStopMongo.getPower() == null ? new BigDecimal("0.00") : startStopMongo.getPower();
  200. BigDecimal oldRunTime = startStopMongo.getIngTime() == null ? new BigDecimal("0.00") : startStopMongo.getIngTime();
  201. BigDecimal oldSelectricCurrent = startStopMongo.getSelectricCurrent() == null ? new BigDecimal("0.00") : startStopMongo.getSelectricCurrent();
  202. // 更新启动运行数据
  203. UpdateDefinition updateStartStop = new Update()
  204. .set("power", runPower.compareTo(new BigDecimal("0.00")) > 0 ? oldPower.add(runPower) : 0.00)
  205. .set("selectricCurrent", oldSelectricCurrent.add(selectricCurrent))
  206. .set("ingTime", oldRunTime.add(runTime))
  207. .set("updateTime", curentDate);
  208. mongoTemplate.updateFirst(queryStartStop, updateStartStop, FpgStatiscsModelMongodb.class, startStopMonKey);
  209. } else { // 直接插入
  210. mongoTemplate.insert(startStopStatiscsData, startStopMonKey);
  211. }
  212. }
  213. // 设备启停统计 end
  214. // 日统计尖峰平谷数据(功率+占比+电流+运行时长) start
  215. String dayPowerproportioncurrentRunMonKey = "total_day_powerproportioncurrent";
  216. DeviceStatiscsModelData dayPowerproportioncurrentRunData = new DeviceStatiscsModelData(fpgGatherData, dayPowerproportioncurrentRunMonKey, dayDate, classs, curentDate, curentDate);
  217. // 组合mongodb条件
  218. Query queryDayPowerproportioncurrentRun = new Query()
  219. .addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId())).addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId())).addCriteria(Criteria.where("dates").is(dayDate));
  220. DeviceStatiscsModelMongodb dayPowerproportioncurrentMongo = mongoTemplate.findOne(queryDayPowerproportioncurrentRun, DeviceStatiscsModelMongodb.class, dayPowerproportioncurrentRunMonKey);
  221. if(dayPowerproportioncurrentMongo != null){ // 不为空更新
  222. // 历史数据
  223. BigDecimal runNewPower = dayPowerproportioncurrentMongo.getPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getPower().add(runPower);
  224. BigDecimal oldRunTime = dayPowerproportioncurrentMongo.getIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getIngTime();
  225. BigDecimal oldSelectricCurrent = dayPowerproportioncurrentMongo.getSelectricCurrent() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getSelectricCurrent();
  226. // 更新运行数据
  227. UpdateDefinition update = new Update();
  228. ((Update) update).set("power", runNewPower);
  229. ((Update) update).set("selectricCurrent", oldSelectricCurrent.add(selectricCurrent));
  230. ((Update) update).set("ingTime", oldRunTime.add(runTime));
  231. ((Update) update).set("updateTime", curentDate);
  232. // 尖峰平谷判定
  233. if("tops".equals(jfpgStr.get())){ // 尖
  234. // 功率处理
  235. BigDecimal oldtopsPower = dayPowerproportioncurrentMongo.getTopsPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getTopsPower();
  236. BigDecimal topsPower = oldtopsPower.add(runPower);
  237. ((Update) update).set("topsPower", topsPower);
  238. // 运行时常处理
  239. BigDecimal oldtopsIngTime = dayPowerproportioncurrentMongo.getTopsIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getTopsIngTime();
  240. ((Update) update).set("topsIngTime", oldtopsIngTime.add(runTime));
  241. // 占比
  242. if(topsPower.compareTo(new BigDecimal("0.00")) > 0) {
  243. ((Update) update).set("topsProportion", topsPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
  244. }
  245. } else if("peaks".equals(jfpgStr.get())){ // 峰
  246. // 功率处理
  247. BigDecimal oldpeaksPower = dayPowerproportioncurrentMongo.getPeaksPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getPeaksPower();
  248. BigDecimal peaksPower = oldpeaksPower.add(runPower);
  249. ((Update) update).set("peaksPower", peaksPower);
  250. // 运行时常处理
  251. BigDecimal oldpeaksIngTime = dayPowerproportioncurrentMongo.getPeaksIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getPeaksIngTime();
  252. ((Update) update).set("peaksIngTime", oldpeaksIngTime.add(runTime));
  253. // 占比
  254. if(peaksPower.compareTo(new BigDecimal("0.00")) > 0) {
  255. ((Update) update).set("peaksProportion", peaksPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
  256. }
  257. } else if("flat".equals(jfpgStr.get())){ // 平
  258. // 功率处理
  259. BigDecimal oldflatPower = dayPowerproportioncurrentMongo.getFlatPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getFlatPower();
  260. BigDecimal flatPower = oldflatPower.add(runPower);
  261. ((Update) update).set("flatPower", flatPower);
  262. // 运行时常处理
  263. BigDecimal oldflatIngTime = dayPowerproportioncurrentMongo.getFlatIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getFlatIngTime();
  264. ((Update) update).set("topsIngTime", oldflatIngTime.add(runTime));
  265. // 占比
  266. if(flatPower.compareTo(new BigDecimal("0.00")) > 0) {
  267. ((Update) update).set("flatProportion", flatPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
  268. }
  269. } else if("valleys".equals(jfpgStr.get())){ // 谷
  270. // 功率处理
  271. BigDecimal oldvalleysPower = dayPowerproportioncurrentMongo.getValleysPower() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getValleysPower();
  272. BigDecimal valleysPower = oldvalleysPower.add(runPower);
  273. ((Update) update).set("valleysPower", oldvalleysPower.add(runPower));
  274. // 运行时常处理
  275. BigDecimal oldvalleysIngTime = dayPowerproportioncurrentMongo.getValleysIngTime() == null ? new BigDecimal("0.00") : dayPowerproportioncurrentMongo.getValleysIngTime();
  276. ((Update) update).set("valleysIngTime", oldvalleysIngTime.add(runTime));
  277. // 占比
  278. if(valleysPower.compareTo(new BigDecimal("0.00")) > 0) {
  279. ((Update) update).set("valleysProportion", valleysPower.divide(runNewPower, 2, RoundingMode.HALF_UP).multiply(zb));
  280. }
  281. }
  282. mongoTemplate.updateFirst(queryDayPowerproportioncurrentRun, update, DeviceStatiscsModelMongodb.class, dayPowerproportioncurrentRunMonKey);
  283. } else { // 为空直接插入
  284. mongoTemplate.insert(dayPowerproportioncurrentRunData, dayPowerproportioncurrentRunMonKey);
  285. }
  286. // 日统计尖峰平谷数据(功率+占比+电流+运行时长) end
  287. // 执行完成数据 标记已处理
  288. fpgGatherData.setFpgTotalUpdatetime(new Date());
  289. fpgGatherData.setId(fpgGatherData.getId());
  290. fpgGatherDataService.updateById(fpgGatherData);
  291. } catch (Exception e) {
  292. log.error(e.getMessage());
  293. }
  294. });
  295. });
  296. }
  297. // 精益模型统计数据
  298. public void leanModel(){
  299. log.info("峰平谷模型");
  300. // 峰平谷精益模型配置获取
  301. LambdaQueryWrapper<FpgLeanModel> leanmodelQuery = new LambdaQueryWrapper<FpgLeanModel>().eq(FpgLeanModel::getStatus, "1");
  302. List<FpgLeanModel> fpgLeanModellist = fpgLeanModelService.list(leanmodelQuery);
  303. // 获取当前时间
  304. LocalDateTime newTime = LocalDateTime.now().plusHours(8);
  305. Date curentDate = Date.from(newTime.atZone(ZoneId.systemDefault()).toInstant());
  306. // 尖峰平谷时段配置
  307. List<PeaksAndValleysTimeConfig> peaksAndValleysTimeConfiglist = peaksAndValleysTimeConfigService.list();
  308. // 存储fpgGather数据id集合
  309. List<FpgGatherData> fpgGatherUpdateList= new ArrayList<>();
  310. // 遍历设备集合
  311. fpgLeanModellist.forEach(fpgLeanModeInfo -> {
  312. LambdaQueryWrapper<FpgGatherData> fpgGatherQuery = new LambdaQueryWrapper<FpgGatherData>().isNull(FpgGatherData::getFpgModelUpdatetime);
  313. // 检测是否配置设备特殊条件
  314. if(oConvertUtils.isNotEmpty(fpgLeanModeInfo.getDeviceInformationIds()) && oConvertUtils.isNotEmpty(fpgLeanModeInfo.getDeviceInformationIds())){
  315. fpgGatherQuery.in(FpgGatherData::getDeviceInformationId, Arrays.asList(fpgLeanModeInfo.getDeviceInformationIds().split(",")));
  316. }
  317. List<FpgGatherData> fpgGatherList = fpgGatherDataService.list(fpgGatherQuery);
  318. // 处理数据业务逻辑
  319. fpgGatherList.forEach(fpgGatherData -> { // 循环处理采集数据处理
  320. fpgGatherData.setFpgModelUpdatetime(new Date());
  321. try {
  322. String finalDatestr = "";
  323. // 统计单位
  324. if("minute".equals(fpgLeanModeInfo.getTotalUnit())){ // 分钟
  325. finalDatestr = new SimpleDateFormat("HH:mm").format(fpgGatherData.getCreateTime());
  326. } else if("hour".equals(fpgLeanModeInfo.getTotalUnit())){ // 小时
  327. finalDatestr = new SimpleDateFormat("HH:00").format(fpgGatherData.getCreateTime());
  328. } else if("day".equals(fpgLeanModeInfo.getTotalUnit())){ //
  329. finalDatestr = new SimpleDateFormat("yyyy-MM-dd").format(fpgGatherData.getCreateTime());
  330. }else if("week".equals(fpgLeanModeInfo.getTotalUnit())){ //
  331. Calendar calendar = Calendar.getInstance();
  332. finalDatestr = String.valueOf(calendar.get(Calendar.WEEK_OF_MONTH));
  333. } else if("month".equals(fpgLeanModeInfo.getTotalUnit())){ //
  334. finalDatestr = new SimpleDateFormat("yyyy-MM").format(fpgGatherData.getCreateTime());
  335. } else if("real_time". equals(fpgLeanModeInfo.getTotalUnit())){ // 实时
  336. fpgLeanModeInfo.setProportion(2);
  337. fpgLeanModeInfo.setElectricCurrent(2);
  338. fpgLeanModeInfo.setRunTime(2);
  339. finalDatestr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(fpgGatherData.getCreateTime());
  340. }
  341. String dayDate = new SimpleDateFormat("yyyy-MM-dd").format(fpgGatherData.getCreateTime());
  342. String classs = "";
  343. // 采集时间
  344. Instant instant = fpgGatherData.getCreateTime().toInstant();
  345. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
  346. // 班次处理
  347. if (fpgLeanModeInfo.getClassUnit() != null && fpgLeanModeInfo.getClassUnit() == 1) { // 开启了班次统计
  348. if (containsCreateTime(localDateTime, dayDate + " 00:00:00", dayDate + " 07:59:59")) { // 夜班
  349. classs = "night";
  350. } else if (containsCreateTime(localDateTime, dayDate + " 08:00:00", dayDate + " 15:59:59")) { // 白班
  351. classs = "white";
  352. } else if (containsCreateTime(localDateTime, dayDate + " 16:00:00", dayDate + " 23:59:59")) { // 中班
  353. classs = "center";
  354. }
  355. }
  356. // 重组对象
  357. FpgStatiscsModelData fpgStatiscsModelData = new FpgStatiscsModelData(fpgGatherData, fpgLeanModeInfo, finalDatestr, dayDate, classs, curentDate, curentDate);
  358. // 实时统计直接插入
  359. if("real_time". equals(fpgLeanModeInfo.getTotalUnit())){
  360. fpgStatiscsModelData.setSelectricCurrent(fpgGatherData.getRunCurrent());
  361. fpgStatiscsModelData.setPower(fpgGatherData.getActivePower());
  362. mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
  363. fpgGatherUpdateList.add(fpgGatherData);
  364. return;
  365. }
  366. // 定义尖峰平谷
  367. AtomicReference<String> jfpgStr = new AtomicReference<>("");
  368. // 判断尖峰平谷时段
  369. peaksAndValleysTimeConfiglist.forEach(peaksAndValleysTimeConfig -> {
  370. if (containsCreateTime(localDateTime, dayDate + " " + peaksAndValleysTimeConfig.getStartTime(), dayDate + " " + peaksAndValleysTimeConfig.getEndTime())) {
  371. jfpgStr.set(peaksAndValleysTimeConfig.getType());
  372. }
  373. });
  374. // 组合mongodb条件
  375. Query query = new Query();
  376. query.addCriteria(Criteria.where("devicePointId").is(fpgGatherData.getDevicePointId()))
  377. .addCriteria(Criteria.where("dates").is(dayDate))
  378. .addCriteria(Criteria.where("datestr").is(finalDatestr));
  379. if(fpgGatherData.getDeviceRegionId() != null && !fpgGatherData.getDeviceRegionId().isEmpty()){ // 区域特殊处理
  380. query.addCriteria(Criteria.where("deviceRegionId").is(fpgGatherData.getDeviceRegionId()));
  381. }
  382. if(fpgGatherData.getDeviceInformationId() != null && !fpgGatherData.getDeviceInformationId().isEmpty()){ // 设备特殊处理
  383. query.addCriteria(Criteria.where("deviceInformationId").is(fpgGatherData.getDeviceInformationId()));
  384. }
  385. if(!classs.isEmpty()){ // 增加班次条件
  386. query.addCriteria(Criteria.where("classs").is(classs));
  387. }
  388. FpgStatiscsModelMongodb fpgStatiscsModelMongo = mongoTemplate.findOne(query, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
  389. if(fpgStatiscsModelMongo != null){ // 不为空处理
  390. // 基础数据变更不受条件影响
  391. UpdateDefinition update = new Update();
  392. ((Update) update).set("updateTime", curentDate);
  393. // 功率 5 分钟上报一次 除以 12
  394. BigDecimal activePower = fpgGatherData.getActivePower() == null ? new BigDecimal("0.00") : fpgGatherData.getActivePower().divide(new BigDecimal("12"), 6, RoundingMode.HALF_UP);
  395. // 电流
  396. BigDecimal selectricCurrent = fpgGatherData.getRunCurrent() == null ? new BigDecimal("0.00") : fpgGatherData.getRunCurrent();
  397. BigDecimal runTime = new BigDecimal("300");
  398. BigDecimal oldPower = fpgStatiscsModelMongo.getPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPower();
  399. BigDecimal oldingTime = fpgStatiscsModelMongo.getIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getIngTime();
  400. // 总运行时间处理
  401. if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1) { // 运行时长统计
  402. ((Update) update).set("ingTime", oldingTime.add(runTime));
  403. }
  404. // 电流处理
  405. if(fpgLeanModeInfo.getElectricCurrent() != null && fpgLeanModeInfo.getElectricCurrent() == 1){
  406. if("tops".equals(jfpgStr.get())) { // 尖
  407. BigDecimal oldtopsSelectricCurrent = fpgStatiscsModelMongo.getTopsSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsSelectricCurrent();
  408. ((Update) update).set("topSelectricCurrent", oldtopsSelectricCurrent.add(selectricCurrent));
  409. } else if("peaks".equals(jfpgStr.get())){ // 峰
  410. BigDecimal oldpeaksSelectricCurrent = fpgStatiscsModelMongo.getPeaksSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksSelectricCurrent();
  411. ((Update) update).set("peaksSelectricCurrent", oldpeaksSelectricCurrent.add(selectricCurrent));
  412. } else if("flat".equals(jfpgStr.get())) { // 平
  413. BigDecimal oldtflatSelectricCurrent = fpgStatiscsModelMongo.getFlatSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatSelectricCurrent();
  414. ((Update) update).set("flatSelectricCurrent", oldtflatSelectricCurrent.add(selectricCurrent));
  415. } else if("valleys".equals(jfpgStr.get())) { // 谷
  416. BigDecimal oldvalleysSelectricCurrent = fpgStatiscsModelMongo.getValleysSelectricCurrent() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysSelectricCurrent();
  417. ((Update) update).set("valleysSelectricCurrent", oldvalleysSelectricCurrent.add(selectricCurrent));
  418. }
  419. }
  420. // 总功率处理
  421. ((Update) update).set("power", oldPower.add(activePower));
  422. // 尖峰平谷判定
  423. if("tops".equals(jfpgStr.get())){ // 尖
  424. // 功率处理
  425. BigDecimal oldtopsPower = fpgStatiscsModelMongo.getTopsPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsPower();
  426. ((Update) update).set("topsPower", oldtopsPower.add(activePower));
  427. // 运行时常处理
  428. if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
  429. BigDecimal oldtopsIngTime = fpgStatiscsModelMongo.getTopsIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getTopsIngTime();
  430. ((Update) update).set("topsIngTime", oldtopsIngTime.add(runTime));
  431. }
  432. // 金额处理
  433. if(fpgLeanModeInfo.getTopsPrice() != null && fpgLeanModeInfo.getTopsPrice().compareTo(new BigDecimal("0.00")) > 0) {
  434. // 总金额
  435. BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getAmount();
  436. ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getTopsPrice()).add(totalAmount));
  437. ((Update) update).set("topsAmount", oldtopsPower.add(activePower).multiply(fpgLeanModeInfo.getTopsPrice()));
  438. }
  439. } else if("peaks".equals(jfpgStr.get())){ // 峰
  440. // 功率处理
  441. BigDecimal oldpeaksPower = fpgStatiscsModelMongo.getPeaksPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksPower();
  442. ((Update) update).set("peaksPower", oldpeaksPower.add(activePower));
  443. // 运行时常处理
  444. if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){ // 运行时长统计
  445. BigDecimal oldpeaksIngTime = fpgStatiscsModelMongo.getPeaksIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getPeaksIngTime();
  446. ((Update) update).set("peaksIngTime", oldpeaksIngTime.add(runTime));
  447. }
  448. // 金额处理
  449. if(fpgLeanModeInfo.getPeaksPrice() != null && fpgLeanModeInfo.getPeaksPrice().compareTo(new BigDecimal("0.00")) > 0) {
  450. // 总金额
  451. BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getAmount();
  452. ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getPeaksPrice()).add(totalAmount));
  453. ((Update) update).set("topsAmount", oldpeaksPower.add(activePower).multiply(fpgLeanModeInfo.getPeaksPrice()));
  454. }
  455. } else if("flat".equals(jfpgStr.get())){ // 平
  456. // 功率处理
  457. BigDecimal oldflatPower = fpgStatiscsModelMongo.getFlatPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatPower();
  458. ((Update) update).set("flatPower", oldflatPower.add(activePower));
  459. // 运行时常处理
  460. if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
  461. BigDecimal oldflatIngTime = fpgStatiscsModelMongo.getFlatIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getFlatIngTime();
  462. ((Update) update).set("topsIngTime", oldflatIngTime.add(runTime));
  463. }
  464. // 金额处理
  465. if(fpgLeanModeInfo.getFlatPrice() != null && fpgLeanModeInfo.getFlatPrice().compareTo(new BigDecimal("0.00")) > 0) {
  466. // 总金额
  467. BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getAmount();
  468. ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getFlatPrice()).add(totalAmount));
  469. ((Update) update).set("flatAmount", oldflatPower.add(activePower).multiply(fpgLeanModeInfo.getFlatPrice()));
  470. }
  471. } else if("valleys".equals(jfpgStr.get())){ // 谷
  472. // 功率处理
  473. BigDecimal oldvalleysPower = fpgStatiscsModelMongo.getValleysPower() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysPower();
  474. ((Update) update).set("valleysPower", oldvalleysPower.add(activePower));
  475. // 运行时常处理
  476. if(fpgLeanModeInfo.getRunTime() != null && fpgLeanModeInfo.getRunTime() == 1){
  477. BigDecimal oldvalleysIngTime = fpgStatiscsModelMongo.getValleysIngTime() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getValleysIngTime();
  478. ((Update) update).set("valleysIngTime", oldvalleysIngTime.add(runTime));
  479. }
  480. // 金额处理
  481. if(fpgLeanModeInfo.getValleysPrice() != null && fpgLeanModeInfo.getValleysPrice().compareTo(new BigDecimal("0.00")) > 0) {
  482. // 总金额
  483. BigDecimal totalAmount = fpgStatiscsModelMongo.getAmount() == null ? new BigDecimal("0.00") : fpgStatiscsModelMongo.getAmount();
  484. ((Update) update).set("amount", activePower.multiply(fpgLeanModeInfo.getValleysPrice()).add(totalAmount));
  485. ((Update) update).set("valleysAmount", oldvalleysPower.add(activePower).multiply(fpgLeanModeInfo.getValleysPrice()));
  486. }
  487. }
  488. fpgGatherUpdateList.add(fpgGatherData);
  489. mongoTemplate.updateFirst(query, update, FpgStatiscsModelMongodb.class, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
  490. } else { // 为空直接插入
  491. fpgGatherUpdateList.add(fpgGatherData);
  492. // 将点位数据存入mongo中
  493. mongoTemplate.insert(fpgStatiscsModelData, "leanmodel_" + fpgLeanModeInfo.getLeanModelCode());
  494. }
  495. }catch (Exception e) {
  496. log.error(e.getMessage());
  497. }
  498. });
  499. });
  500. // 数据处理完成标记已完成信息,以免重复处理
  501. fpgGatherDataService.updateBatchById(fpgGatherUpdateList);
  502. }
  503. /**
  504. * 判断创建时间是否在开始时间和结束时间范围内
  505. * @param createTime
  506. * @param startTime
  507. * @param endTime
  508. * @return
  509. */
  510. public static boolean containsCreateTime(LocalDateTime createTime, String startTime, String endTime) {
  511. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  512. LocalDateTime start_time = LocalDateTime.parse(startTime, formatter);
  513. LocalDateTime end_time = LocalDateTime.parse(endTime, formatter);
  514. return createTime!= null && (createTime.isAfter(start_time) || createTime.equals(start_time)) &&
  515. (createTime.isBefore(end_time) || createTime.equals(end_time));
  516. }
  517. }