fuliqi
2024-09-23 134ef8858210a09d34567b5ef0b8d29b6c8bf38d
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
package com.ycl.platform.service.impl;
 
import annotation.DataScope;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.platform.domain.entity.TMonitor;
import com.ycl.platform.domain.excel.VideoDailyExp;
import com.ycl.platform.domain.excel.VideoTotalExp;
import com.ycl.platform.domain.form.VideoExportForm;
import com.ycl.platform.domain.query.DashboardQuery;
import com.ycl.platform.domain.query.DataCenterQuery;
import com.ycl.platform.domain.query.HomeQuery;
import com.ycl.platform.domain.result.BaseResult;
import com.ycl.platform.domain.result.HK.FaceDeviceInspectionResult;
import com.ycl.platform.domain.result.HK.FaceDeviceSamplingResult;
import com.ycl.platform.domain.result.HK.VehicleDeviceInspectionResult;
import com.ycl.platform.domain.result.HK.VehicleDeviceSamplingResult;
import com.ycl.platform.domain.result.SYS.TMonitorResult;
import com.ycl.platform.domain.result.UY.MonitorQualifyResult;
import com.ycl.platform.domain.result.UY.OsdCheckResult;
import com.ycl.platform.domain.result.UY.RecordMetaDSumResult;
import com.ycl.platform.domain.vo.TMonitorVO;
import com.ycl.platform.domain.vo.WorkOrderVO;
import com.ycl.platform.domain.vo.screen.MonitorRateVO;
import com.ycl.platform.domain.vo.screen.MonitorTotalVO;
import com.ycl.platform.mapper.TMonitorMapper;
import com.ycl.platform.mapper.WorkOrderMapper;
import com.ycl.platform.service.ITMonitorService;
import com.ycl.system.Result;
import com.ycl.system.page.PageUtil;
import com.ycl.system.service.ISysConfigService;
import com.ycl.utils.DateUtils;
import com.ycl.utils.StringUtils;
import constant.ApiConstants;
import constant.CheckConstants;
import enumeration.general.AreaDeptEnum;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ObjectUtils;
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.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pojo.ExcelExp;
import utils.poi.ExcelUtilManySheet;
 
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 设备资产Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-04
 */
@Service
public class TMonitorServiceImpl extends ServiceImpl<TMonitorMapper, TMonitor> implements ITMonitorService {
    @Autowired
    private TMonitorMapper tMonitorMapper;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private WorkOrderMapper workOrderMapper;
 
    /**
     * 查询设备资产
     *
     * @param id 设备资产主键
     * @return 设备资产
     */
    @Override
    public TMonitor selectTMonitorById(Long id) {
        return tMonitorMapper.selectTMonitorById(id);
    }
 
    /**
     * 查询设备资产列表
     * 异常恢复监控、车辆、人脸、视频通用接口
     *
     * @param tMonitor 设备资产
     * @return 设备资产
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<TMonitorVO> selectTMonitorList(TMonitorVO tMonitor) {
        // 异常恢复监控
        if (Objects.equals(tMonitor.getRecovery(), 1L)) {
            String time = configService.selectConfigByKey("abnormal.equipment.continuous.attention.time");
            if (StringUtils.isBlank(time)) {
                throw new RuntimeException("请配置异常设备连续关注时间");
            }
            tMonitor.setTime(time);
        }
        List<TMonitorVO> monitors = tMonitorMapper.selectTMonitorList(tMonitor);
        // 异常恢复监控
        if (Objects.equals(tMonitor.getRecovery(), 1L)) {
            //工单号
            List<String> orders = monitors.stream().map(TMonitorVO::getWorkOrderNo).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(orders)) {
                return monitors;
            }
            List<WorkOrderVO> voList = workOrderMapper.getRecoveryInfo(orders);
            for (TMonitorVO monitor : monitors) {
                if (!CollectionUtils.isEmpty(voList)) {
                    for (WorkOrderVO workOrderVO : voList) {
                        if (monitor.getWorkOrderNo().equals(workOrderVO.getWorkOrderNo())) {
                            monitor.setUnitContact(workOrderVO.getUnitContact());
                            monitor.setUnitContactPhone(workOrderVO.getUnitContactPhone());
                            monitor.setYwPeopleName(workOrderVO.getYwPeopleName());
                            monitor.setErrorType(workOrderVO.getErrorType());
                        }
                    }
                }
            }
            return monitors;
        }
        //设备编号
        List<String> numbers = monitors.stream().map(TMonitorVO::getSerialNumber).collect(Collectors.toList());
        Query query = new Query();
        Date now = new Date();
        query.addCriteria(Criteria.where("no").in(numbers)
                .and("mongoCreateTime").gte(DateUtils.getDayStart(now)).lt(DateUtils.getDayEnd(now))
        );
        //一机一档信息
        List<MonitorQualifyResult> monitorQualifyResults = mongoTemplate.find(query, MonitorQualifyResult.class);
        // 视频监控设备
        if (Objects.equals(tMonitor.getCameraFunType(), "1")) {
            //OSD信息
            List<OsdCheckResult> osdCheckResults = mongoTemplate.find(query, OsdCheckResult.class);
            //录像可用信息
            List<RecordMetaDSumResult> videoResults = mongoTemplate.find(query, RecordMetaDSumResult.class);
            for (TMonitorVO monitor : monitors) {
                monitor.setMongoCreateTime(now);
                //一机一档
                setOneFile(monitorQualifyResults, monitor);
                //录像
                if (!CollectionUtils.isEmpty(videoResults)) {
                    for (RecordMetaDSumResult videoResult : videoResults) {
                        if (monitor.getSerialNumber().equals(videoResult.getNo())) {
                            monitor.setVideoComplete(videoResult.getRecordStatus());
                            monitor.setVideoLoseTime(videoResult.getMissDuration());
                        }
                    }
                }
                //OSD
                if (!CollectionUtils.isEmpty(osdCheckResults)) {
                    for (OsdCheckResult osdCheckResult : osdCheckResults) {
                        if (monitor.getSerialNumber().equals(osdCheckResult.getNo())) {
                            monitor.setOSD(OsdCheckResult.checkOsd(osdCheckResult));
                            monitor.setOSDTime(OsdCheckResult.checkTime(osdCheckResult));
                        }
                    }
                }
            }
        }
        // 车辆监控设备
        if (Objects.equals(tMonitor.getCameraFunType(), "2")) {
            //属性一致率、大图、url
            List<VehicleDeviceSamplingResult> sampleResults = mongoTemplate.find(query, VehicleDeviceSamplingResult.class);
            //抓拍量、时钟、上传
            List<VehicleDeviceInspectionResult> inspectResults = mongoTemplate.find(query, VehicleDeviceInspectionResult.class);
            for (TMonitorVO monitor : monitors) {
                monitor.setMongoCreateTime(now);
                //一机一档
                setOneFile(monitorQualifyResults, monitor);
                //属性一致率、大图、url
                if (!CollectionUtils.isEmpty(sampleResults)) {
                    for (VehicleDeviceSamplingResult sampleResult : sampleResults) {
                        if (monitor.getSerialNumber().equals(sampleResult.getNo())) {
                            if (sampleResult.getBigUseful() != null) {
                                monitor.setBigUsefulPercent(sampleResult.getBigUseful().getBigUsefulPercent());
                                monitor.setUrlPercent(VehicleDeviceSamplingResult.BigUsefulness.calUrl(sampleResult.getBigUseful()));
                            }
                            if (sampleResult.getVehDiff() != null) {
                                monitor.setImportantConPercent(sampleResult.getVehDiff().getImportantConPercent());
                                monitor.setMajorConPercent(sampleResult.getVehDiff().getMajorConPercent());
                            }
                        }
                    }
                }
                //抓拍量、时钟、上传
                if (!CollectionUtils.isEmpty(inspectResults)) {
                    for (VehicleDeviceInspectionResult inspectResult : inspectResults) {
                        if (monitor.getSerialNumber().equals(inspectResult.getNo())) {
                            monitor.setSnapResult(inspectResult.getSnapResult());
                            monitor.setSnapCount(inspectResult.getDataCount());
                            if (inspectResult.getSnapClock() != null)
                                monitor.setClockPercent(inspectResult.getSnapClock().getClockPercent());
                            if (inspectResult.getSnapTimely() != null)
                                monitor.setUploadPercent(inspectResult.getSnapTimely().getTimelyPercent());
                        }
                    }
                }
            }
        }
        // 人脸监控设备
        if (Objects.equals(tMonitor.getCameraFunType(), "3")) {
            //人脸合格、大图、url访问异常
            List<FaceDeviceSamplingResult> sampleResults = mongoTemplate.find(query, FaceDeviceSamplingResult.class);
            //抓拍量、时钟、上传
            List<FaceDeviceInspectionResult> inspectResults = mongoTemplate.find(query, FaceDeviceInspectionResult.class);
            for (TMonitorVO monitor : monitors) {
                monitor.setMongoCreateTime(now);
                //一机一档
                setOneFile(monitorQualifyResults, monitor);
                //人脸合格、大图、url访问异常
                if (!CollectionUtils.isEmpty(sampleResults)) {
                    for (FaceDeviceSamplingResult sampleResult : sampleResults) {
                        if (monitor.getSerialNumber().equals(sampleResult.getNo())) {
                            if (sampleResult.getBigUseful() != null) {
                                monitor.setBigUsefulPercent(sampleResult.getBigUseful().getBigUsefulPercent());
                                monitor.setUrlPercent(FaceDeviceSamplingResult.BigUsefulness.calUrl(sampleResult.getBigUseful()));
                            }
                            if (sampleResult.getFaceEligibility() != null)
                                monitor.setFacePercent(sampleResult.getFaceEligibility().getFaceEligPercent());
                        }
                    }
                }
                //抓拍量、时钟、上传、建模失败率
                if (!CollectionUtils.isEmpty(inspectResults)) {
                    for (FaceDeviceInspectionResult inspectResult : inspectResults) {
                        if (monitor.getSerialNumber().equals(inspectResult.getNo())) {
                            monitor.setSnapResult(inspectResult.getSnapResult());
                            monitor.setSnapCount(inspectResult.getDataCount());
                            if (inspectResult.getSnapClock() != null)
                                monitor.setClockPercent(inspectResult.getSnapClock().getClockPercent());
                            if (inspectResult.getSnapTimely() != null)
                                monitor.setUploadPercent(inspectResult.getSnapTimely().getTimelyPercent());
                            if (inspectResult.getSnapValidity() != null)
                                monitor.setFailPercent(inspectResult.getSnapValidity().getFailPercent());
                        }
                    }
                }
            }
        }
        return monitors;
    }
 
    /**
     * 补充一机一档信息
     *
     * @param monitorQualifyResults
     * @param monitor
     */
    private void setOneFile(List<MonitorQualifyResult> monitorQualifyResults, TMonitorVO monitor) {
        if (!CollectionUtils.isEmpty(monitorQualifyResults)) {
            for (MonitorQualifyResult oneFile : monitorQualifyResults) {
                if (monitor.getSerialNumber().equals(oneFile.getNo())) {
                    monitor.setMonitorQualify(MonitorQualifyResult.correct(oneFile));
                    monitor.setNewMonitor(oneFile.getNewDevice());
                }
            }
        }
    }
 
    /**
     * 新增设备资产
     *
     * @param tMonitor 设备资产
     * @return 结果
     */
    @Override
    public int insertTMonitor(TMonitor tMonitor) {
        return tMonitorMapper.insertTMonitor(tMonitor);
    }
 
    /**
     * 修改设备资产
     *
     * @param tMonitor 设备资产
     * @return 结果
     */
    @Override
    public int updateTMonitor(TMonitor tMonitor) {
        return tMonitorMapper.updateTMonitor(tMonitor);
    }
 
    /**
     * 批量删除设备资产
     *
     * @param ids 需要删除的设备资产主键
     * @return 结果
     */
    @Override
    public int deleteTMonitorByIds(Long[] ids) {
        return tMonitorMapper.deleteTMonitorByIds(ids);
    }
 
    /**
     * 删除设备资产信息
     *
     * @param id 设备资产主键
     * @return 结果
     */
    @Override
    public int deleteTMonitorById(Long id) {
        return tMonitorMapper.deleteTMonitorById(id);
    }
 
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public Map<String, String> getVideoCount(TMonitorVO tMonitor) {
        return tMonitorMapper.getVideoCount(tMonitor);
    }
 
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public Map<String, String> recoveryException(TMonitorVO monitor) {
//        String time = configService.selectConfigByKey("abnormal.equipment.continuous.attention.time");
//        monitor.setTime(time);
        return tMonitorMapper.recoveryException(monitor);
    }
 
    @Override
    public Map<String, Object> home(HomeQuery monitorQuery) {
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> monthMap1 = new HashMap<>();
        Map<String, Object> monthMap2 = new HashMap<>();
        List<Map<String, Object>> home = baseMapper.home(monitorQuery);
        if (ObjectUtils.isNotEmpty(home)) {
            for (Map<String, Object> map : home) {
                monthMap1.put(map.get("months").toString(), map.get("num1"));
                monthMap2.put(map.get("months").toString(), map.get("num2"));
            }
            dataMap.put("name", home.get(0).get("name"));
            dataMap.put("state", monthMap1);
            dataMap.put("state2", monthMap2);
        }
        return dataMap;
    }
 
    @Override
    public Map<String, List<Map<String, Object>>> monitorTotal(DashboardQuery dashboardQuery) {
        List<MonitorTotalVO> monitorTotalVOS = baseMapper.monitorTotal(dashboardQuery);
        /** facilityData: {
         * video:[
         * {value: 4589,title: '设备总数'},
         * {value: 4294,title: '设备正常数'},
         * {value: 295,title: '设备异常数'}
         * ]} */
        Map<String, List<Map<String, Object>>> resultMap = new HashMap<>();
        for (MonitorTotalVO vo : monitorTotalVOS) {
            List<Map<String, Object>> list = new ArrayList();
            Map<String, Object> total = new HashMap<>();
            total.put("value", vo.getTotalNum());
            total.put("title", "设备总数");
            Map<String, Object> normal = new HashMap<>();
            normal.put("value", vo.getNormalNum());
            normal.put("title", "正常数");
            Map<String, Object> error = new HashMap<>();
            error.put("value", vo.getErrorNum());
            error.put("title", "异常数");
            list.add(total);
            list.add(normal);
            list.add(error);
            resultMap.put(vo.getType(), list);
        }
        return resultMap;
    }
 
    @Override
    public List<MonitorRateVO> monitorRate(DashboardQuery dashboardQuery) {
        return baseMapper.monitorRate(dashboardQuery);
    }
 
 
    @Override
    public Result assetManagement(DataCenterQuery query) {
        IPage<TMonitorVO> page = PageUtil.getPage(query, TMonitorVO.class);
        baseMapper.assetManagement(page, query);
        return Result.ok().data(page.getRecords()).total(page.getTotal());
    }
 
    /**
     * 导出总量数据
     */
    @Override
    public void exportVideoTotal(HttpServletResponse response, VideoExportForm exportForm) throws IOException {
        List<ExcelExp> mysheet = new ArrayList<>();
        exportForm.setCameraFunType(Integer.valueOf(CheckConstants.Rule_Category_Video+""));
        VideoExportForm.convertTags(exportForm);
        List<TMonitorResult> tMonitorResults = tMonitorMapper.selectMonitorResult(exportForm);
        List<String> deviceIds = tMonitorResults.stream().map(BaseResult::getNo).collect(Collectors.toList());
        Query query = getQuery(deviceIds, exportForm.getMonth());
        //月份每日在线数据
        List<TMonitorResult> onlineResult = mongoTemplate.find(query, TMonitorResult.class);
        //月份每日录像数据
        List<RecordMetaDSumResult> recordResult = mongoTemplate.find(query, RecordMetaDSumResult.class);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String[] weeks = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        // 创建一个Map来存储每天的累加数据
        Map<String, VideoTotalExp> totalMap = new HashMap<>();
        //一个部门一个sheet
        for (Integer deptId : exportForm.getDeptIds()) {
            //从数据库集合筛选部门数据
            List<String> ids = tMonitorResults.stream().filter(tMonitorResult -> deptId.equals(tMonitorResult.getDeptId())).map(BaseResult::getNo).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(ids)) continue;
            //筛选部门数据
            List<TMonitorResult> onlineList = onlineResult.stream().filter(tMonitorResult -> ids.contains(tMonitorResult.getNo())).collect(Collectors.toList());
            List<RecordMetaDSumResult> recordList = recordResult.stream().filter(result -> ids.contains(result.getNo())).collect(Collectors.toList());
            List<VideoTotalExp> videoTotalExps = new ArrayList<>();
            for (int i = 0; i < 31; i++) {
                String date = exportForm.getMonth();
                date += "-" + (i < 9 ? "0" + (i + 1) : (i + 1));
                //总量
                VideoTotalExp totalExp = totalMap.computeIfAbsent(date, k -> new VideoTotalExp());
                LocalDate parseTime = LocalDate.parse(date, formatter);
                //获取星期几
                String week = weeks[parseTime.getDayOfWeek().getValue() - 1];
                VideoTotalExp videoExp = new VideoTotalExp();
                videoExp.setDate(date);
                videoExp.setWeek(week);
                //设置点位在线总量
                List<TMonitorResult> onlines = onlineList.stream().filter(tMonitorResult -> tMonitorResult.getMongoCreateTime().minusDays(1).equals(parseTime)).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(onlines)) {
                    videoExp.setTotal(onlines.size());
                    videoExp.setOnline(Integer.valueOf(onlines.stream()
                            .filter(TMonitorResult::getOnline)
                            .count() + ""));
                    videoExp.setOffline(videoExp.getTotal() - videoExp.getOnline());
                }
                //设置存储情况
                List<RecordMetaDSumResult> records = recordList.stream().filter(record -> record.getMongoCreateTime().minusDays(1).equals(parseTime)).collect(Collectors.toList());
                if (!CollectionUtils.isEmpty(records)) {
                    videoExp.setNoStore(Integer.valueOf(records.stream()
                            .filter(record -> ApiConstants.UY_RecordStatus_Abnormal.equals(record.getRecordStatus()))
                            .count() + ""));
                    videoExp.setPartStore(Integer.valueOf(records.stream()
                            .filter(record -> ApiConstants.UY_RecordStatus_Interval.equals(record.getRecordStatus()))
                            .count() + ""));
                }
                videoTotalExps.add(videoExp);
                //累加作为全量表
                totalExp.setDate(date);
                totalExp.setWeek(week);
                totalExp.setTotal((totalExp.getTotal() == null ? 0 : totalExp.getTotal()) + (videoExp.getTotal() == null ? 0 : videoExp.getTotal()));
                totalExp.setOnline((totalExp.getOnline() == null ? 0 : totalExp.getOnline()) + (videoExp.getOnline() == null ? 0 : videoExp.getOnline()));
                totalExp.setOffline((totalExp.getOffline() == null ? 0 : totalExp.getOffline()) + (videoExp.getOffline() == null ? 0 : videoExp.getOffline()));
                totalExp.setNoStore((totalExp.getNoStore() == null ? 0 : totalExp.getNoStore()) + (videoExp.getNoStore() == null ? 0 : videoExp.getNoStore()));
                totalExp.setPartStore((totalExp.getPartStore() == null ? 0 : totalExp.getPartStore()) + (videoExp.getPartStore() == null ? 0 : videoExp.getPartStore()));
                totalMap.put(date, totalExp);
            }
            AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromDept(deptId);
            ExcelExp excelExp = new ExcelExp(areaDeptEnum == null ? "未知" : areaDeptEnum.getName(), videoTotalExps, VideoTotalExp.class);
            mysheet.add(excelExp);
        }
        //添加全量表
        List<VideoTotalExp> totalExps = new ArrayList<>(totalMap.values());
        totalExps = totalExps.stream().sorted(Comparator.comparing(VideoTotalExp::getDate)).collect(Collectors.toList());
        ExcelExp excelExp = new ExcelExp("全量", totalExps, VideoTotalExp.class);
        mysheet.add(excelExp);
        //导出
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mysheet);
        util.exportExcelManySheet(response, mysheet);
    }
 
 
    /**
     * 导出每日在线数据
     */
    @Override
    public void exportVideoOnline(HttpServletResponse response, VideoExportForm exportForm) throws IOException, NoSuchFieldException, IllegalAccessException {
        List<ExcelExp> mysheet = new ArrayList<>();
        exportForm.setCameraFunType(Integer.valueOf(CheckConstants.Rule_Category_Video+""));
        VideoExportForm.convertTags(exportForm);
        List<TMonitorResult> tMonitorResults = tMonitorMapper.selectMonitorResult(exportForm);
        List<String> deviceIds = tMonitorResults.stream().map(BaseResult::getNo).collect(Collectors.toList());
        Query query = getQuery(deviceIds, exportForm.getMonth());
        //月份每日在线数据
        List<TMonitorResult> onlineResult = mongoTemplate.find(query, TMonitorResult.class);
        //全量表
        List<VideoDailyExp> totalExps = new ArrayList<>();
        for (Integer deptId : exportForm.getDeptIds()) {
            List<VideoDailyExp> videoDailyExps = new ArrayList<>();
            //从数据库集合筛选部门数据
            List<TMonitorResult> monitors = tMonitorResults.stream().filter(tMonitorResult -> deptId.equals(tMonitorResult.getDeptId())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(monitors)) continue;
            List<String> ids = monitors.stream().map(BaseResult::getNo).collect(Collectors.toList());
            //筛选mongo区县数据
            List<TMonitorResult> onlines = onlineResult.stream().filter(result -> ids.contains(result.getNo())).collect(Collectors.toList());
            AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromDept(deptId);
            for (TMonitorResult result : monitors) {
                VideoDailyExp videoDailyExp = new VideoDailyExp();
                videoDailyExp.setSerialNumber(result.getNo());
                videoDailyExp.setDeviceName(result.getName());
                videoDailyExp.setArea(areaDeptEnum == null ? "未知" : areaDeptEnum.getName());
                String tag ="" + (result.getProvinceTag() ? "省厅、" : "") + (result.getImportantTag() ? "重点点位、" : "") + (result.getImportantCommandImageTag() ? "重点指挥图像、" : "") + (result.getDeptTag() ? "部级、" : "");
                // 删除字符串末尾的“、”
                if (tag.endsWith("、")) {
                    tag = tag.substring(0, tag.length() - 1);
                }
                videoDailyExp.setTag(tag);
                setOnlineDaily(videoDailyExp, result, onlines);
                videoDailyExps.add(videoDailyExp);
                //全量表
                totalExps.add(videoDailyExp);
            }
            ExcelExp excelExp = new ExcelExp(areaDeptEnum == null ? "未知" : areaDeptEnum.getName(), videoDailyExps, VideoDailyExp.class);
            mysheet.add(excelExp);
        }
        ExcelExp excelExp = new ExcelExp("全量", totalExps, VideoDailyExp.class);
        mysheet.add(excelExp);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mysheet);
        util.exportExcelManySheet(response, mysheet);
    }
 
 
    /**
     * 导出每日录像情况数据
     */
    @Override
    public void exportVideoRecord(HttpServletResponse response, VideoExportForm exportForm) throws IOException, NoSuchFieldException, IllegalAccessException {
        List<ExcelExp> mysheet = new ArrayList<>();
        exportForm.setCameraFunType(Integer.valueOf(CheckConstants.Rule_Category_Video+""));
        VideoExportForm.convertTags(exportForm);
        List<TMonitorResult> tMonitorResults = tMonitorMapper.selectMonitorResult(exportForm);
        List<String> deviceIds = tMonitorResults.stream().map(BaseResult::getNo).collect(Collectors.toList());
        Query query = getQuery(deviceIds, exportForm.getMonth());
        //月份每日录像线数据
        List<RecordMetaDSumResult> recordResult = mongoTemplate.find(query, RecordMetaDSumResult.class);
        //全量表
        List<VideoDailyExp> totalExps = new ArrayList<>();
        for (Integer deptId : exportForm.getDeptIds()) {
            List<VideoDailyExp> videoDailyExps = new ArrayList<>();
            //从数据库集合筛选部门数据
            List<TMonitorResult> monitors = tMonitorResults.stream().filter(tMonitorResult -> deptId.equals(tMonitorResult.getDeptId())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(monitors)) continue;
            List<String> ids = monitors.stream().map(BaseResult::getNo).collect(Collectors.toList());
            //筛选mongo区县数据
            List<RecordMetaDSumResult> records = recordResult.stream().filter(result -> ids.contains(result.getNo())).collect(Collectors.toList());
            AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromDept(deptId);
            for (TMonitorResult result : monitors) {
                VideoDailyExp videoDailyExp = new VideoDailyExp();
                videoDailyExp.setSerialNumber(result.getNo());
                videoDailyExp.setDeviceName(result.getName());
                videoDailyExp.setArea(areaDeptEnum == null ? "未知" : areaDeptEnum.getName());
                String tag ="" + (result.getProvinceTag() ? "省厅、" : "") + (result.getImportantTag() ? "重点点位、" : "") + (result.getImportantCommandImageTag() ? "重点指挥图像、" : "") + (result.getDeptTag() ? "部级、" : "");
                // 删除字符串末尾的“、”
                if (tag.endsWith("、")) {
                    tag = tag.substring(0, tag.length() - 1);
                }
                videoDailyExp.setTag(tag);
                setRecordDaily(videoDailyExp, result, records);
                //区县表
                videoDailyExps.add(videoDailyExp);
                //全量表
                totalExps.add(videoDailyExp);
            }
            ExcelExp excelExp = new ExcelExp(areaDeptEnum == null ? "未知" : areaDeptEnum.getName(), videoDailyExps, VideoDailyExp.class);
            mysheet.add(excelExp);
        }
        ExcelExp excelExp = new ExcelExp("全量", totalExps, VideoDailyExp.class);
        mysheet.add(excelExp);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mysheet);
        util.exportExcelManySheet(response, mysheet);
    }
 
    /**
     * 导出每日录像缺失时长
     */
    @Override
    public void exportVideoLoseTime(HttpServletResponse response, VideoExportForm exportForm) throws NoSuchFieldException, IllegalAccessException, IOException {
        List<ExcelExp> mysheet = new ArrayList<>();
        exportForm.setCameraFunType(Integer.valueOf(CheckConstants.Rule_Category_Video+""));
        VideoExportForm.convertTags(exportForm);
        List<TMonitorResult> tMonitorResults = tMonitorMapper.selectMonitorResult(exportForm);
        List<String> deviceIds = tMonitorResults.stream().map(BaseResult::getNo).collect(Collectors.toList());
        Query query = getQuery(deviceIds, exportForm.getMonth());
        //月份每日录像线数据
        List<RecordMetaDSumResult> recordResult = mongoTemplate.find(query, RecordMetaDSumResult.class);
        //全量表
        List<VideoDailyExp> totalExps = new ArrayList<>();
        for (Integer deptId : exportForm.getDeptIds()) {
            List<VideoDailyExp> videoDailyExps = new ArrayList<>();
            //从数据库集合筛选部门数据
            List<TMonitorResult> monitors = tMonitorResults.stream().filter(tMonitorResult -> deptId.equals(tMonitorResult.getDeptId())).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(monitors)) continue;
            List<String> ids = monitors.stream().map(BaseResult::getNo).collect(Collectors.toList());
            //筛选mongo区县数据
            List<RecordMetaDSumResult> records = recordResult.stream().filter(result -> ids.contains(result.getNo())).collect(Collectors.toList());
            AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromDept(deptId);
            for (TMonitorResult result : monitors) {
                VideoDailyExp videoDailyExp = new VideoDailyExp();
                videoDailyExp.setSerialNumber(result.getNo());
                videoDailyExp.setDeviceName(result.getName());
                videoDailyExp.setArea(areaDeptEnum == null ? "未知" : areaDeptEnum.getName());
                String tag ="" + (result.getProvinceTag() ? "省厅、" : "") + (result.getImportantTag() ? "重点点位、" : "") + (result.getImportantCommandImageTag() ? "重点指挥图像、" : "") + (result.getDeptTag() ? "部级、" : "");
                // 删除字符串末尾的“、”
                if (tag.endsWith("、")) {
                    tag = tag.substring(0, tag.length() - 1);
                }
                videoDailyExp.setTag(tag);
                setLoseDaily(videoDailyExp, result, records);
                videoDailyExps.add(videoDailyExp);
                //全量表
                totalExps.add(videoDailyExp);
            }
            ExcelExp excelExp = new ExcelExp(areaDeptEnum == null ? "未知" : areaDeptEnum.getName(), videoDailyExps, VideoDailyExp.class);
            mysheet.add(excelExp);
        }
        ExcelExp excelExp = new ExcelExp("全量", totalExps, VideoDailyExp.class);
        mysheet.add(excelExp);
        ExcelUtilManySheet<List<ExcelExp>> util = new ExcelUtilManySheet<>(mysheet);
        util.exportExcelManySheet(response, mysheet);
    }
 
 
    private Query getQuery(List<String> deviceIds, String month) {
        // 将年月字符串解析为YearMonth对象
        YearMonth yearMonth = YearMonth.parse(month);
        // 获取当月的第一天
        LocalDate start = yearMonth.atDay(1);
        // 获取下个月的第一天(通过加上1个月并设置日为1)
        YearMonth nextMonth = yearMonth.plusMonths(1);
        LocalDate end = nextMonth.atDay(1);
        //获取这个月份的部门数据
        Query query = new Query(Criteria.where("mongoCreateTime").gte(start).lt(end));
        query.addCriteria(Criteria.where("no").in(deviceIds));
        return query;
    }
 
    //设置每日在线数据
    private void setOnlineDaily(VideoDailyExp videoDailyExp, TMonitorResult result, List<TMonitorResult> onlines) throws NoSuchFieldException, IllegalAccessException {
        //一个设备当月在线情况
        List<TMonitorResult> onlineResult = onlines.stream().filter(online -> online.getNo().equals(result.getNo())).collect(Collectors.toList());
        for (TMonitorResult monitorResult : onlineResult) {
            int dayOfMonth = monitorResult.getMongoCreateTime().getDayOfMonth();
            String online = "";
            online += monitorResult.getOnline() ? "在线" : "离线";
            //反射赋值,字段统一定义为day+1,2,3...
            Field field = videoDailyExp.getClass().getDeclaredField("day" + dayOfMonth);
            field.setAccessible(true);
            field.set(videoDailyExp, online);
        }
    }
 
    //设置每日录像数据
    private void setRecordDaily(VideoDailyExp videoDailyExp, TMonitorResult result, List<RecordMetaDSumResult> records) throws NoSuchFieldException, IllegalAccessException {
        //一个设备当月在线情况
        List<RecordMetaDSumResult> recordResults = records.stream().filter(online -> online.getNo().equals(result.getNo())).collect(Collectors.toList());
        for (RecordMetaDSumResult recordResult : recordResults) {
            int dayOfMonth = recordResult.getMongoCreateTime().getDayOfMonth();
            Integer status = recordResult.getRecordStatus();
            String text = "";
            if (ApiConstants.UY_RecordStatus_Interval.equals(status)) {
                text += "间歇";
            } else if (ApiConstants.UY_RecordStatus_Abnormal.equals(status)) {
                text += "缺失";
            } else if (ApiConstants.UY_RecordStatus_Integrity.equals(status)) {
                text += "完整";
            }
            //反射赋值,字段统一定义为day+1,2,3...
            Field field = videoDailyExp.getClass().getDeclaredField("day" + dayOfMonth);
            field.setAccessible(true);
            field.set(videoDailyExp, text);
        }
    }
 
    //设置每日录像缺失时长数据
    private void setLoseDaily(VideoDailyExp videoDailyExp, TMonitorResult result, List<RecordMetaDSumResult> records) throws NoSuchFieldException, IllegalAccessException {
        //一个设备当月在线情况
        List<RecordMetaDSumResult> recordResults = records.stream().filter(online -> online.getNo().equals(result.getNo())).collect(Collectors.toList());
        for (RecordMetaDSumResult recordResult : recordResults) {
            int dayOfMonth = recordResult.getMongoCreateTime().getDayOfMonth();
            //反射赋值,字段统一定义为day+1,2,3...
            Field field = videoDailyExp.getClass().getDeclaredField("day" + dayOfMonth);
            field.setAccessible(true);
            //防止转换为科学计数法
            BigDecimal bigDecimal = BigDecimal.valueOf(recordResult.getMissDuration() == null ? 0 : recordResult.getMissDuration());
            field.set(videoDailyExp,bigDecimal.toString());
        }
    }
}