houzhongjian
2024-07-23 a6de490948278991e47952e90671ddba4555e9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
package com.iailab.module.data.indexdata.common;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iailab.dto.*;
import com.iailab.module.data.common.enums.CommonConstant;
import com.iailab.api.IFeignProddispApi;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.module.data.point.dto.DaPointDTO;
import com.iailab.module.data.point.service.DaPointService;
import com.iailab.module.data.indexdata.entity.IndexDataItemEntity;
import com.iailab.module.data.indexdata.entity.IndexDataValueEntity;
import com.iailab.module.data.indexdata.service.IndexDataItemService;
import com.iailab.module.data.indexdata.service.IndexDataValueService;
import com.iailab.module.data.influxdb.pojo.InfluxPointValuePOJO;
import com.iailab.module.data.influxdb.service.InfluxDBService;
import com.iailab.module.infra.api.config.ConfigApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * @author PanZhibao
 * @Description
 * @createTime 2022年09月11日 14:11:00
 */
@Slf4j
@Component
public class IndexDataUtils {
 
    private BigDecimal hourMins = new BigDecimal("60");
 
    private String DATA_TYPE_BOOLEAN = "boolean";
 
    private final int granularity = 5;
 
    @Resource
    private InfluxDBService influxDBService;
 
    @Resource
    private IndexDataValueService indexDataValueService;
 
    @Resource
    private IFeignProddispApi feignProddispApi;
 
    @Resource
    private ConfigApi configApi;
 
    @Resource
    private DaPointService daPointService;
 
    @Resource
    private IndexDataItemService indexDataItemService;
 
    public void doCal(Date startTime, Date endTime, String CLOCK_CODE, String DICT_DC) throws Exception {
        log.info("startTime=" + DateUtils.format(startTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
        log.info("endTime=" + DateUtils.format(endTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
        // 获取数据项
        List<IndexDataItemEntity> indexDataItemList = indexDataItemService.getListByClockConfig(CLOCK_CODE);
        if (CollectionUtils.isEmpty(indexDataItemList)) {
            log.warn("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
            throw new Exception("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
        }
        indexDataItemList.forEach(dataItem -> {
            switch (dataItem.getItemType()) {
                case IndexDataConstant.RUN_TIME:
                    // 运行时长
                    this.createRunTime(CLOCK_CODE, dataItem, "all" ,startTime, endTime);
                    break;
                case IndexDataConstant.RUN_TIME_ZC:
                    // 运行时长正常
                    this.createRunTime(CLOCK_CODE, dataItem, "zc",startTime, endTime);
                    break;
                case IndexDataConstant.RUN_TIME_KZ:
                    // 运行时长空载
                    this.createRunTime(CLOCK_CODE, dataItem, "kz",startTime, endTime);
                    break;
                case IndexDataConstant.POWER_CONSUME:
                    // 电耗
                    this.createPowerConsume(CLOCK_CODE, dataItem, startTime, endTime);
                    break;
                case IndexDataConstant.WASH_TRANSFER_IN:
                    // 调入量
                    this.createWashTransferIn(CLOCK_CODE, dataItem, startTime, endTime);
                    break;
                case IndexDataConstant.WASH_TO_WASH:
                    // 入洗量
                    this.createWashToWash(CLOCK_CODE, dataItem, startTime, endTime);
                    break;
                case IndexDataConstant.WASH_PROD_VOLUME:
                    // 生产量
                    this.createWashProdVolume(CLOCK_CODE, dataItem, startTime, endTime);
                    break;
                case IndexDataConstant.WAREHOUSE_POSITION:
                    // 仓位
                    this.createWarehousePosition(CLOCK_CODE, dataItem, startTime, endTime);
                    break;
                default:
                    break;
            }
        });
 
        // 统计库存
        this.createWarehouseStock(DateUtils.format(startTime), DICT_DC);
 
        // 统计总电量
        FeignConsumeTargetDTO dto = new FeignConsumeTargetDTO();
        Map param = new HashMap();
        param.put("rq",new LocalDate());
        param.put("bc",DICT_DC);
        BigDecimal hdl = feignProddispApi.sumData(param);
        dto.setRq(DateUtils.format(startTime));
        dto.setBc(DICT_DC);
        dto.setXhzb("hdl");
        dto.setDw("kw.h");
        dto.setNbdrl(hdl);
        dto.setNbtzl(new BigDecimal(0));
        dto.setWbdrl(new BigDecimal(0));
        dto.setWbtzl(new BigDecimal(0));
        dto.setCjr(CommonConstant.CJS_SYS);
        dto.setCjsj(new Date());
        dto.setXgsj(new Date());
        feignProddispApi.createConsumeTarget(dto);
 
    }
 
    public void doHourVolume(Date startTime, Date endTime, String CLOCK_CODE, String DICT_DC) throws Exception {
        log.info("startTime=" + DateUtils.format(startTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
        log.info("endTime=" + DateUtils.format(endTime, DateUtils.FORMAT_YEAR_MONTH_DAY));
        // 获取数据项
        List<IndexDataItemEntity> indexDataItemList = indexDataItemService.getListByClockConfig(CLOCK_CODE);
        if (CollectionUtils.isEmpty(indexDataItemList)) {
            log.warn("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
            throw new Exception("indexDataItemEntityList为空, clockCode=" + CLOCK_CODE);
        }
        indexDataItemList.forEach(dataItem -> {this.createHourVolume(CLOCK_CODE, dataItem, startTime, endTime);});
 
    }
    public void createRunTime(String clockCode, IndexDataItemEntity dataItem, String runTimeType,Date startTime, Date endTime) {
        try{
            log.info("计算运行时长开始,itemCode=" + dataItem.getPointCode());
            InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
            pojo.setPoint(dataItem.getPointCode());
            pojo.setType(DATA_TYPE_BOOLEAN);
            List<Map<String, Object>> pointData = influxDBService.queryPointValues(pojo, startTime, endTime);
            if (CollectionUtils.isEmpty(pointData)) {
                log.warn("历史值为空,PointCode=" + dataItem.getPointCode());
                return;
            }
            int mins = this.getRunMins(pointData);
            int times = this.getStartUpTimes(pointData);
 
            Map<String, BigDecimal> valueMap = new HashMap<>(2);
            valueMap.put("sumValue", new BigDecimal(mins));
            valueMap.put("startUpTimes", new BigDecimal(times));
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
 
            FeignRunTimeDTO dto = new FeignRunTimeDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(dataItem.getBc());
            dto.setScxm(dataItem.getXtCode());
            dto.setRunTimeType(runTimeType);
            dto.setXss(Long.valueOf((mins / 60)));
            dto.setFzs(Long.valueOf((mins % 60)));
            dto.setZsc(Long.valueOf(mins));
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createRunTime(dto);
            log.info("计算运行时长结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算运行时长异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
    public void createConsumeTarget(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            log.info("计算消耗指标开始,itemCode=" + dataItem.getPointCode());
 
            // 插入指标数据表
            Map<String, BigDecimal> valueMap = new HashMap<>(2);
            valueMap.put("sumValue", new BigDecimal(0));
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
 
            FeignConsumeTargetDTO dto = new FeignConsumeTargetDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(dataItem.getBc());
            dto.setXhzb(dataItem.getExt1());
            dto.setDw(dataItem.getUnit());
            dto.setNbdrl(new BigDecimal(0));
            dto.setNbtzl(new BigDecimal(0));
            dto.setWbdrl(new BigDecimal(0));
            dto.setWbtzl(new BigDecimal(0));
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createConsumeTarget(dto);
            log.info("计算消耗指标结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算消耗指标异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
    public void createPowerConsume(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
 
            if (daPoint == null) {
                log.info("daPoint is null");
                return;
            }
            InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
            pojo.setPoint(daPoint.getPointNo());
            pojo.setType(daPoint.getPointType());
            // 取10min内最大值
            Object startPointValue = influxDBService.queryPointMaxValue(pojo, new Date(startTime.getTime() - 1000 * 60 * 10));
            Object endPointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
            BigDecimal startValue = startPointValue == null ? BigDecimal.ZERO : new BigDecimal(startPointValue.toString());
            BigDecimal endValue = endPointValue == null ? BigDecimal.ZERO : new BigDecimal(endPointValue.toString());
            BigDecimal value = new BigDecimal(0);
            if(endValue.compareTo(startValue) == 1){
                value = endValue.subtract(startValue);
            }
            // 插入指标数据表
            Map<String, BigDecimal> valueMap = new HashMap<>(1);
            valueMap.put("sumValue", value);
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
 
            String xt = configApi.queryConfigByCode(dataItem.getExt1());
            // 插入统计表
            FeignPowerConsumeDTO dto = new FeignPowerConsumeDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(dataItem.getBc());
            dto.setXt(xt);
            dto.setNbdrl(value);
            dto.setNbtzl(new BigDecimal(0));
            dto.setWbdrl(value);
            dto.setWbtzl(new BigDecimal(0));
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createPowerConsume(dto);
            log.info("计算电耗结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算电耗异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
    public void createHourVolume(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            /**
             * 861 精煤
             * 862 混煤
             * 903 矸石
             * 603ABC 煤泥板数
             */
            log.info("计算小时量开始,itemCode=" + dataItem.getPointCode());
            List<String> pointNos = new ArrayList<>();
            pointNos.add(dataItem.getPointCode());
            if(StringUtils.isNotBlank(dataItem.getExt1())) {
                pointNos.add(dataItem.getExt1());
            }
            if(StringUtils.isNotBlank(dataItem.getExt2())) {
                pointNos.add(dataItem.getExt2());
            }
            if(StringUtils.isNotBlank(dataItem.getExt3())) {
                pointNos.add(dataItem.getExt3());
            }
            if(StringUtils.isNotBlank(dataItem.getExt4())) {
                pointNos.add(dataItem.getExt4());
            }
            if(StringUtils.isNotBlank(dataItem.getExt5())) {
                pointNos.add(dataItem.getExt5());
            }
            List<DaPointDTO> pointList = daPointService.getByNos(pointNos);
            if (CollectionUtils.isEmpty(pointList)) {
                log.warn("pointNos查询为空!");
                return;
            }
            List<InfluxPointValuePOJO> influxParams = pointList.stream().map(item -> {
                InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
                pojo.setPoint(item.getPointNo());
                pojo.setType(item.getDataType());
                return pojo;
            }).collect(Collectors.toList());
            Map<String, Object> data = influxDBService.queryPointsSpread(influxParams, startTime, endTime);
            log.info("查询完成!");
 
            log.info("插入统计表");
            FeignHourVolumeDTO dto = new FeignHourVolumeDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(this.getBcByTime(endTime));
            dto.setSj(endTime);
            if (StringUtils.isNotBlank(dataItem.getPointCode()) && data.get(dataItem.getPointCode()) != null) {
                dto.setRxl(new BigDecimal(data.get(dataItem.getPointCode()).toString()));
                log.info(" 插入指标数据表");
                Map<String, BigDecimal> valueMap = new HashMap<>(1);
                valueMap.put("sumValue", new BigDecimal(data.get(dataItem.getPointCode()).toString()));
                IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
                indexDataValueService.add(indexDataValue);
            }
            if (StringUtils.isNotBlank(dataItem.getExt1()) && data.get(dataItem.getExt1()) != null) {
                dto.setJml(new BigDecimal(data.get(dataItem.getExt1()).toString()));
            }
            if (StringUtils.isNotBlank(dataItem.getExt2()) && data.get(dataItem.getExt2()) != null) {
                dto.setHml(new BigDecimal(data.get(dataItem.getExt2()).toString()));
            }
            if (StringUtils.isNotBlank(dataItem.getExt3()) && data.get(dataItem.getExt3()) != null) {
                dto.setGsl(new BigDecimal(data.get(dataItem.getExt3()).toString()));
            }
            if (StringUtils.isNotBlank(dataItem.getExt4()) && data.get(dataItem.getExt4()) != null) {
                dto.setFjbs(new BigDecimal(data.get(dataItem.getExt4()).toString()));
            }
            if (StringUtils.isNotBlank(dataItem.getExt5()) && data.get(dataItem.getExt5()) != null) {
                dto.setMnbs(new BigDecimal(data.get(dataItem.getExt5()).toString()));
            }
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createHourVolume(dto);
            log.info("计算小时量结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算小时量异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
 
    private String getBcByTime(Date dateTime) {
        String bc = "bb";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        if (calendar.get(Calendar.HOUR_OF_DAY) >= 21 || calendar.get(Calendar.HOUR_OF_DAY) <= 8) {
            bc = "yb";
        }
        return bc;
    }
 
    public void createWashTransferIn(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            log.info("计算调入量开始,itemCode=" + dataItem.getPointCode());
            // 查询测点里的累计产量
            DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
 
            if (daPoint == null) {
                log.info("daPoint is null");
                return;
            }
            InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
            pojo.setPoint(daPoint.getPointNo());
            pojo.setType(daPoint.getDataType());
            // 取10min内最大值
            Object pointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
            BigDecimal sumValue = pointValue == null ? BigDecimal.ZERO : new BigDecimal(pointValue.toString());
 
            // 插入指标数据表
            Map<String, BigDecimal> valueMap = new HashMap<>(1);
            valueMap.put("sumValue", sumValue);
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
 
            String mz = configApi.queryConfigByCode(dataItem.getExt1());
            // 插入统计表
            FeignWashTransferInDTO dto = new FeignWashTransferInDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(dataItem.getBc());
            dto.setDrly(mz);
            dto.setNbdrl(sumValue);
            dto.setNbtzl(new BigDecimal(0));
            dto.setWbdrl(sumValue);
            dto.setWbtzl(new BigDecimal(0));
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createWashTransferIn(dto);
            log.info("计算调入量结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算调入量异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
    public void createWashToWash(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            log.info("计算入洗量开始,itemCode=" + dataItem.getPointCode());
            // 查询测点里的累计产量
            DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
            if (daPoint == null) {
                log.info("daPoint is null");
                return;
            }
            InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
            pojo.setPoint(daPoint.getPointNo());
            pojo.setType(daPoint.getDataType());
            // 取10min内最大值
            Object pointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
            BigDecimal sumValue = pointValue == null ? BigDecimal.ZERO : new BigDecimal(pointValue.toString());
            log.info("插入指标数据表");
            Map<String, BigDecimal> valueMap = new HashMap<>(1);
            log.info("sumValue"+sumValue);
            valueMap.put("sumValue", sumValue);
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
            List<FeignMcMzDTO> mzList = feignProddispApi.selectMcMzByMc(dataItem.getExt1());
            log.info("dataItem.getExt1()"+dataItem.getExt1());
 
            //获取总入洗量
            pojo.setPoint(dataItem.getExt2());
            pojo.setType("float");
            Object rxlPointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
            BigDecimal sumRXL = rxlPointValue == null ? BigDecimal.ZERO : new BigDecimal(rxlPointValue.toString());
 
            mzList.stream().forEach(
                    mz->{
                        log.info("插入统计表,mc:"+mz.getMc()+";mz:"+mz.getMz());
                        FeignWashToWashDTO dto = new FeignWashToWashDTO();
                        dto.setRq(DateUtils.format(startTime));
                        dto.setBc(dataItem.getBc());
                        dto.setXxjm(mz.getMz());
                        dto.setNbdrl(sumValue.multiply(BigDecimal.valueOf(mz.getProportion())).divide(BigDecimal.valueOf(mz.getDen())) );
                        dto.setRxbl(dto.getNbdrl().divide(sumRXL, 2, RoundingMode.HALF_UP));
                        dto.setRxly(dataItem.getExt1());
                        dto.setNbtzl(new BigDecimal(0));
                        dto.setWbdrl(dto.getNbdrl());
                        dto.setWbtzl(new BigDecimal(0));
                        dto.setCjr(CommonConstant.CJS_SYS);
                        dto.setCjsj(new Date());
                        dto.setXgsj(new Date());
                        feignProddispApi.createWashToWash(dto);
                    }
            );
 
 
            log.info("计算入洗量结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算入洗量异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
    public void createWashProdVolume(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            log.info("计算生产量开始,itemCode=" + dataItem.getPointCode());
            // 查询测点里的累计产量
            DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
            if (daPoint == null) {
                log.info("daPoint is null");
                return;
            }
            InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
            pojo.setPoint(daPoint.getPointNo());
            pojo.setType(daPoint.getDataType());
            // 取10min内最大值
            Object pointValue = influxDBService.queryPointMaxValue(pojo, new Date(endTime.getTime() - 1000 * 60 * 10));
            BigDecimal sumValue = pointValue == null ? BigDecimal.ZERO : new BigDecimal(pointValue.toString());
 
            log.info("插入指标数据表");
            Map<String, BigDecimal> valueMap = new HashMap<>(1);
            valueMap.put("sumValue", sumValue);
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
 
            log.info("插入统计表");
            FeignWashProdVolumeDTO dto = new FeignWashProdVolumeDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(dataItem.getBc());
            dto.setClmz(dataItem.getExt1());
            dto.setNbdrl(sumValue);
            dto.setNbtzl(new BigDecimal(0));
            dto.setWbdrl(sumValue);
            dto.setWbtzl(new BigDecimal(0));
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createWashProdVolume(dto);
            log.info("计算生产量结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算生产量异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
    public void createWarehousePosition(String clockCode, IndexDataItemEntity dataItem, Date startTime, Date endTime) {
        try{
            log.info("计算仓位开始,itemCode=" + dataItem.getPointCode());
            DaPointDTO daPoint = daPointService.getByNo(dataItem.getPointCode());
            if (daPoint == null) {
                log.info("daPoint is null");
                return;
            }
            InfluxPointValuePOJO pojo = new InfluxPointValuePOJO();
            pojo.setPoint(daPoint.getPointNo());
            pojo.setType(daPoint.getDataType());
            List<Map<String, Object>> pointValues = influxDBService.queryPointValues(pojo, new Date(endTime.getTime() - 1000 * 30), endTime);
            String point = JSON.toJSONString(pointValues);
            log.info("pointValues :"+point);
            if (CollectionUtils.isEmpty(pointValues)) {
                log.info("pointValues is null");
                return;
            }
 
            Map<String, Object> pointValue = pointValues.get(pointValues.size() - 1);
            JSONObject json = new JSONObject(pointValue);
            log.info("插入指标数据表:"+json);
            Map<String, BigDecimal> valueMap = new HashMap<>(1);
            BigDecimal sumValue =  new BigDecimal(pointValue.get("value").toString());
            valueMap.put("sumValue",sumValue);
            IndexDataValueEntity indexDataValue = this.buildIndexDataValueEntity(dataItem, clockCode, valueMap);
            indexDataValueService.add(indexDataValue);
            log.info("mc:"+dataItem.getExt1());
            FeignWarehouseItemDTO warehouseItem = feignProddispApi.selectWarehouseItemByMc(dataItem.getExt1());
            log.info("插入仓位统计表开始");
            FeignWarehousePositionDTO dto = new FeignWarehousePositionDTO();
            dto.setRq(DateUtils.format(startTime));
            dto.setBc(dataItem.getBc());
            dto.setMc(dataItem.getExt1());
            dto.setMclx(dataItem.getExt2());
            log.info(Integer.toString(warehouseItem.getCapacity()));
            dto.setCrl(Integer.toString(warehouseItem.getCapacity()));
            BigDecimal dqcc = sumValue.multiply(new BigDecimal(dto.getCrl())).divide(new BigDecimal(100), 1, BigDecimal.ROUND_HALF_UP);
            dto.setNbgl(dqcc);
            dto.setWbgl(dqcc);
            dto.setCjr(CommonConstant.CJS_SYS);
            dto.setCjsj(new Date());
            dto.setXgsj(new Date());
            feignProddispApi.createWarehousePosition(dto);
            log.info("计算仓位结束,itemCode=" + dataItem.getPointCode());
        } catch (Exception ex) {
            log.info("计算仓位异常,itemCode=" + dataItem.getPointCode());
            ex.printStackTrace();
        }
    }
 
    public void createWarehouseStock(String rq, String bc) {
        try{
            log.info("计算库存开始,rq=" + rq);
 
            Map<String, Object> params = new HashMap<>();
            params.put("rq", rq);
            params.put("bc", bc);
            List<FeignWarehousePositionDTO> list = feignProddispApi.selectWarehouseGroupByCp(params);
            list.forEach(item -> {
                log.info("遍历" + item.getMc()+"煤仓煤种");
                List<FeignMcMzDTO> mzList = feignProddispApi.selectMcMzByMc(item.getMc());
                mzList.stream().forEach(
                        mz->{
                            log.info("插入" + mz.getMz()+"库存");
                            FeignWarehouseStockDTO dto = new FeignWarehouseStockDTO();
                            dto.setRq(rq);
                            dto.setBc(bc);
                            dto.setLsh(new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date()));
                            dto.setBs(item.getMclx());
                            dto.setKcmz(mz.getMz());
                            dto.setNbzrkc(new BigDecimal(0));
                            dto.setNbjrrk(new BigDecimal(0));
                            log.info(mz.getProportion().toString());
                            log.info(mz.getDen().toString());
                            BigDecimal math = item.getNbgl().multiply(BigDecimal.valueOf(mz.getProportion()).divide(BigDecimal.valueOf(mz.getDen())));
                            BigDecimal nbjrkc = math.setScale(2, RoundingMode.HALF_UP);
                            dto.setNbjrkc(nbjrkc);
                            dto.setNbtzl(new BigDecimal(0));
                            dto.setWbzrkc(new BigDecimal(0));
                            dto.setWbjrrk(new BigDecimal(0));
                            dto.setWbjrkc(dto.getNbjrkc());
                            dto.setWbtzl(new BigDecimal(0));
                            dto.setCjr(CommonConstant.CJS_SYS);
                            dto.setCjsj(new Date());
                            dto.setXgsj(new Date());
                            feignProddispApi.createWarehouseStock(dto);
                        }
                );
            });
            log.info("计算库存结束,rq=" + rq);
        } catch (Exception ex) {
            log.info("计算库存异常,rq=" + rq);
            ex.printStackTrace();
        }
    }
 
    public int getRunMins(List<Map<String, Object>> pointData) {
        int seconds = 0;
        for (Map<String, Object> item:
                pointData) {
            if (Boolean.parseBoolean(item.get("value").toString())) {
                seconds = seconds + granularity;
            }
        }
        return (seconds / 60);
    }
 
    /**
     * getAvgOutput
     *
     * @param indexDataItemEntity
     * @param output
     * @param hours
     * @return
     */
    public BigDecimal getAvgOutput(IndexDataItemEntity indexDataItemEntity, BigDecimal output, BigDecimal hours) {
        BigDecimal result = BigDecimal.ZERO;
        if (hours.compareTo(BigDecimal.ZERO) > 0) {
            result = output.divide(hours, 0, BigDecimal.ROUND_HALF_UP);
        }
        return result;
    }
 
    /**
     * buildIndexDataValueEntity
     *
     * @param indexDataItemEntity
     * @param clock
     * @param valueMap
     * @return
     */
    public IndexDataValueEntity buildIndexDataValueEntity(IndexDataItemEntity indexDataItemEntity, String clock,
                                                          Map<String, BigDecimal> valueMap) {
        IndexDataValueEntity entity = new IndexDataValueEntity();
        entity.setId(UUID.randomUUID().toString().replace("-",""));
        entity.setConfigId(indexDataItemEntity.getId());
        entity.setUnit2(indexDataItemEntity.getUnit2());
        entity.setSsdw(indexDataItemEntity.getSsdw());
        entity.setItemCode(indexDataItemEntity.getItemCode());
        entity.setItemName(indexDataItemEntity.getItemName());
        entity.setUnit(indexDataItemEntity.getUnit());
        entity.setClock(clock);
        entity.setSumValue(valueMap.get("sumValue"));
        entity.setAvgValue(valueMap.get("avgValue"));
        entity.setReportValue(valueMap.get("reportValue"));
        entity.setReportHourValue(valueMap.get("reportHourValue"));
        entity.setCollectValue(valueMap.get("collectValue"));
        entity.setCollectHourValue(valueMap.get("collectHourValue"));
        entity.setStartUpTimes(valueMap.get("startUpTimes"));
        entity.setCreateTime(new Date());
        return entity;
    }
 
    /**
     * getAvgValue
     *
     * @param dataMap
     * @return
     */
    public BigDecimal getAvgValue (LinkedHashMap<String, BigDecimal> dataMap) {
        BigDecimal avgValue = BigDecimal.ZERO;
        double sumValue = 0d;
        if (!CollectionUtils.isEmpty(dataMap)) {
            int size = dataMap.size();
            for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
                if (item.getValue() != null && item.getValue().compareTo(BigDecimal.ZERO) > 0) {
                    sumValue = sumValue + item.getValue().doubleValue();
                }
            }
            avgValue = new BigDecimal(sumValue).divide(new BigDecimal(size), 2, BigDecimal.ROUND_HALF_UP);
        }
        return avgValue;
    }
 
    /**
     * getRunHours
     *
     * @param dataMap
     * @return
     */
    public BigDecimal getRunHours (LinkedHashMap<String, BigDecimal> dataMap) {
        BigDecimal result = BigDecimal.ZERO;
        if (!CollectionUtils.isEmpty(dataMap)) {
            int mins = 0;
            for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
                if (item.getValue() != null && item.getValue().compareTo(BigDecimal.ZERO) > 0) {
                    mins++;
                }
            }
            result = new BigDecimal(mins).divide(hourMins, 2, BigDecimal.ROUND_HALF_UP);
        }
        return result;
    }
 
    /**
     * getStartUpTimes
     *
     * @param pointData
     * @return
     */
    public int getStartUpTimes (List<Map<String, Object>> pointData) {
        int times = 0;
        Boolean lastValue = Boolean.FALSE;
        for (Map<String, Object> item:
                pointData) {
            Boolean itemValue = Boolean.parseBoolean(item.get("value").toString());
            if (itemValue.compareTo(lastValue) != 0) {
                times++;
            }
            lastValue = itemValue;
        }
        return times;
    }
 
    /**
     * 完善数据
     *
     * @param length
     * @param sourceDataMap
     * @param startTime
     * @param endTime
     * @return
     */
    public LinkedHashMap<String, BigDecimal> completionData(int length, LinkedHashMap<String, BigDecimal> sourceDataMap, Date startTime, Date endTime) {
        if (CollectionUtils.isEmpty(sourceDataMap) || length <= sourceDataMap.size()) {
            return sourceDataMap;
        }
        log.info("补全数据, length =" + length + "; size = " + sourceDataMap.size() + "; startTime = " + startTime.getTime() + "; endTime = " + endTime.getTime());
 
        //找出缺少项
        long oneMin = 1000 * 60;
        long start = startTime.getTime();
        long end = endTime.getTime();
        long mins = ((end - start) / oneMin);
        LinkedHashMap<String, BigDecimal> dataMap = new LinkedHashMap<>();
        for (int i = 0; i < mins; i ++) {
            String key = DateUtils.format(new Date(start + oneMin * i), "");
            dataMap.put(key, sourceDataMap.get(key));
        }
 
        //补充缺少项
        int k = 0;
        Map.Entry<String, BigDecimal> lastItem = null;
        for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
            if (k == 0 && item.getValue() == null) {
                item.setValue(getFirstValue(dataMap));
            } else if (item.getValue() == null) {
                item.setValue(lastItem.getValue());
            }
            k ++;
            lastItem = item;
        }
        log.info("补全完成");
        return dataMap;
    }
 
    /**
     * getFirstValue
     *
     * @param dataMap
     * @return
     */
    private BigDecimal getFirstValue(LinkedHashMap<String, BigDecimal> dataMap) {
        for (Map.Entry<String, BigDecimal> item : dataMap.entrySet()) {
            if (item.getValue() != null) {
                return item.getValue();
            }
        }
        return null;
    }
}