郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
package com.gkhy.safePlatform.riskCtrl.service.impl;
 
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.idService.IdGenerateUtil;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.riskCtrl.entity.EmergencyPracticeReportItem;
import com.gkhy.safePlatform.riskCtrl.entity.IncidentMonthReportItem;
import com.gkhy.safePlatform.riskCtrl.entity.PreventMonthReportDetail;
import com.gkhy.safePlatform.riskCtrl.entity.RiskMonthReport;
import com.gkhy.safePlatform.riskCtrl.enums.ReportBizEnum;
import com.gkhy.safePlatform.riskCtrl.model.bo.EmergencyDataBO;
import com.gkhy.safePlatform.riskCtrl.model.bo.IncidentDataBO;
import com.gkhy.safePlatform.riskCtrl.model.bo.RiskDataBO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.EmergencyPracticeReportItemRespDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.IncidentMonthReportItemRespDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.PreventMonthReportDetailRespDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.RiskMonthReportRespDTO;
import com.gkhy.safePlatform.riskCtrl.service.RiskReportService;
import com.gkhy.safePlatform.riskCtrl.service.baseService.EmergencyPracticeReportItemService;
import com.gkhy.safePlatform.riskCtrl.service.baseService.IncidentMonthReportItemService;
import com.gkhy.safePlatform.riskCtrl.service.baseService.PreventMonthReportDetailService;
import com.gkhy.safePlatform.riskCtrl.service.baseService.RiskMonthReportService;
import com.gkhy.safePlatform.riskCtrl.utils.RiskReportAnalysisHandler;
import org.apache.dubbo.config.annotation.DubboReference;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
@Service
public class RiskReportServiceImpl implements RiskReportService {
 
    @DubboReference(check = false)
    private AccountDepartmentService departmentService;
 
    @Autowired
    private CollectReportDataService collectReportDataService;
 
    @Autowired
    private RiskMonthReportService riskMonthReportService;
 
    @Autowired
    private PreventMonthReportDetailService preventMonthReportDetailService;
 
    @Autowired
    private EmergencyPracticeReportItemService emergencyPracticeReportItemService;
 
    @Autowired
    private IncidentMonthReportItemService incidentMonthReportItemService;
 
    @Autowired
    private RedissonClient redissonClient;
 
    /**
     * 生成企业月度风险报表
     * @param depId
     * @param year
     * @param month
     * @return
     */
    @Transactional
    @Override
    public ResultVO generateDepartmentMonthRiskReport(Long depId, Integer year, Integer month) {
 
        ResultVO resultVO = new ResultVO<>();
        //step1、检查该月报表是否已经生成
        RiskMonthReport prevReport = riskMonthReportService.findByMonth(ReportBizEnum.PREVENT_RISK.getType(),depId,year, month);
        RiskMonthReport incidentReport = riskMonthReportService.findByMonth(ReportBizEnum.INCIDENT_COUNT.getType(),depId, year, month);
        RiskMonthReport emgcReport = riskMonthReportService.findByMonth(ReportBizEnum.EMERGENCY_PRACTICE.getType(),depId, year, month);
        if(prevReport != null || incidentReport != null || emgcReport != null){
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("月度报表已存在");
            return resultVO;
        }
        //step2、获取各系统数据
        if(year == null || year < 0 || year > 9999){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc());
            return resultVO;
        }
        if(month != null && (month < 0 || month > 12)){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc());
            return resultVO;
        }
        ResultVO<DepInfoRPCRespDTO> depInfoRpcResult = departmentService.getDepInfoByDepId(depId);
        if(!depInfoRpcResult.getCode().equals(ResultCodes.OK.getCode())){
            throw new RuntimeException("RPC调用出错");
        }
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depInfoRpcResult.getData();
        if(depInfo == null){
            throw new RuntimeException("部门不存在");
        }
 
        RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,year,month);
 
        IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,year,month);
 
        EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo,year,month);
 
        //step3、处理数据
        //3.1、报表POJO
        RiskMonthReport riskMonthReport = new RiskMonthReport();
        IdGenerateUtil idGenerateUtil = new IdGenerateUtil();
        riskMonthReport.setId(idGenerateUtil.nextId());
        riskMonthReport.setDepId(depId);
        riskMonthReport.setYear(year);
        riskMonthReport.setMonth(month);
        riskMonthReport.setDepLevel(depInfo.getDepLevel());
        LocalDateTime beginTime = LocalDateTime.of(year,month,1,0,0,0);
        DateTime dt = DateUtil.beginOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant()));
        riskMonthReport.setBeginDate(dt.toLocalDateTime().toLocalDate());
        dt = DateUtil.endOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant()));
        riskMonthReport.setEndDate(dt.toLocalDateTime().toLocalDate());
        riskMonthReport.setGmtCreate(LocalDateTime.now());
 
        RiskReportAnalysisHandler riskReportAnalysisHandler = new RiskReportAnalysisHandler();
 
        //3.2、双重预防报表POJO
        PreventMonthReportDetail preventMonthReportDetail =
                riskReportAnalysisHandler.analysisPrevReportData(riskDataBO);
        preventMonthReportDetail.setReportId(riskMonthReport.getId());
 
        //3.3、应急管理报表POJO
        List<EmergencyPracticeReportItem> emergencyReportItemList =
                riskReportAnalysisHandler.analysisEmgcReportData(emergencyDataBO);
 
        //3.4、事故管理报表POJO
        List<IncidentMonthReportItem> incidentMonthReportItemList =
                riskReportAnalysisHandler.analysisIncidentReportDate(incidentDataBO);
 
        //step4、保存入库
        if(riskMonthReportService.save(riskMonthReport) == false){
            throw new BusinessException(ResultCodes.BUSINESS_ERROR);
        }
        if(preventMonthReportDetail != null){
            if(preventMonthReportDetailService.save(preventMonthReportDetail) == false){
                throw new BusinessException(ResultCodes.BUSINESS_ERROR);
            }
        }
        if(emergencyReportItemList != null && !emergencyReportItemList.isEmpty()){
            for(EmergencyPracticeReportItem item : emergencyReportItemList){
                item.setId(idGenerateUtil.nextId());
                item.setReportId(riskMonthReport.getId());
            }
            if(emergencyPracticeReportItemService.saveBatch(emergencyReportItemList) == false){
                throw new BusinessException(ResultCodes.BUSINESS_ERROR);
            }
        }
        if(incidentMonthReportItemList != null && !incidentMonthReportItemList.isEmpty()){
            for(IncidentMonthReportItem i : incidentMonthReportItemList){
                i.setId(idGenerateUtil.nextId());
                i.setReportId(riskMonthReport.getId());
            }
            if(incidentMonthReportItemService.saveBatch(incidentMonthReportItemList) == false){
                throw new BusinessException(ResultCodes.BUSINESS_ERROR);
            }
        }
 
        resultVO.setCode(ResultCodes.OK.getCode());
        return resultVO;
    }
 
    @Transactional
    @Override
    public ResultVO generateDepartmentMonthRiskReport(Byte biz, Long depId, Integer year, Integer month) {
        ResultVO resultVO = new ResultVO<>();
        //1、参数校验
        if(biz == null || ReportBizEnum.parse(biz) == null){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务类型不支持");
            return resultVO;
        }
        if(depId == null || depId < 1 || year == null || year < 2000 || year > 9999 || month == null || month < 1 || month > 12){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("参数错误");
            return resultVO;
        }
 
        //加分布式锁
        String lockName = "LOCK_RISK_MONTH_REPORT_GENERATE_"+depId+"_"+biz;
        RLock lock = redissonClient.getLock(lockName);
        lock.lock(30L, TimeUnit.SECONDS);
 
        //2、检查报表是否已经存在
        RiskMonthReport report = riskMonthReportService.findByMonth(biz,depId,year,month);
        if(report != null && report.getId() > 0){
            lock.unlock();
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("报表已经生成");
            return resultVO;
        }
        //3、校验部门是否存在
        ResultVO<DepInfoRPCRespDTO> depInfoRpcResult = departmentService.getDepInfoByDepId(depId);
        if(!depInfoRpcResult.getCode().equals(ResultCodes.OK.getCode())){
            lock.unlock();
            throw new RuntimeException("RPC调用出错");
        }
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depInfoRpcResult.getData();
        if(depInfo == null){
            lock.unlock();
            throw new RuntimeException("部门不存在");
        }
        //4、生成报表项
        RiskMonthReport riskMonthReport = new RiskMonthReport();
        IdGenerateUtil idGenerateUtil = new IdGenerateUtil();
        riskMonthReport.setId(idGenerateUtil.nextId());
        riskMonthReport.setDepId(depId);
        riskMonthReport.setBiz(biz);
        riskMonthReport.setYear(year);
        riskMonthReport.setMonth(month);
        riskMonthReport.setDepLevel(depInfo.getDepLevel());
        LocalDateTime beginTime = LocalDateTime.of(year,month,1,0,0,0);
        DateTime dt = DateUtil.beginOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant()));
        riskMonthReport.setBeginDate(dt.toLocalDateTime().toLocalDate());
        dt = DateUtil.endOfMonth(Date.from(beginTime.atZone(ZoneId.systemDefault()).toInstant()));
        riskMonthReport.setEndDate(dt.toLocalDateTime().toLocalDate());
        riskMonthReport.setGmtCreate(LocalDateTime.now());
 
        if(riskMonthReportService.save(riskMonthReport) == false){
            lock.unlock();
            throw new BusinessException(ResultCodes.BUSINESS_ERROR);
        }
 
        RiskReportAnalysisHandler riskReportAnalysisHandler = new RiskReportAnalysisHandler();
 
        //5、获取对应业务系统的数据,生成业务报表
        if(biz.equals(ReportBizEnum.PREVENT_RISK.getType())){
            //双重预防
            RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,year,month);
            PreventMonthReportDetail preventMonthReportDetail =
                    riskReportAnalysisHandler.analysisPrevReportData(riskDataBO);
            preventMonthReportDetail.setReportId(riskMonthReport.getId());
            if(preventMonthReportDetail != null){
                if(preventMonthReportDetailService.save(preventMonthReportDetail) == false){
                    lock.unlock();
                    throw new BusinessException(ResultCodes.BUSINESS_ERROR);
                }
            }
        }else if(biz.equals(ReportBizEnum.EMERGENCY_PRACTICE.getType())){
            //应急管理
            EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo,year,month);
            List<EmergencyPracticeReportItem> emergencyReportItemList =
                    riskReportAnalysisHandler.analysisEmgcReportData(emergencyDataBO);
            if(emergencyReportItemList != null && !emergencyReportItemList.isEmpty()){
                for(EmergencyPracticeReportItem item : emergencyReportItemList){
                    item.setId(idGenerateUtil.nextId());
                    item.setReportId(riskMonthReport.getId());
                }
                if(emergencyPracticeReportItemService.saveBatch(emergencyReportItemList) == false){
                    lock.unlock();
                    throw new BusinessException(ResultCodes.BUSINESS_ERROR);
                }
            }
        }else if(biz.equals(ReportBizEnum.INCIDENT_COUNT.getType())){
            //事故管理
            IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,year,month);
            List<IncidentMonthReportItem> incidentMonthReportItemList =
                    riskReportAnalysisHandler.analysisIncidentReportDate(incidentDataBO);
            if(incidentMonthReportItemList != null && !incidentMonthReportItemList.isEmpty()){
                for(IncidentMonthReportItem i : incidentMonthReportItemList){
                    i.setId(idGenerateUtil.nextId());
                    i.setReportId(riskMonthReport.getId());
                }
                if(incidentMonthReportItemService.saveBatch(incidentMonthReportItemList) == false){
                    lock.unlock();
                    throw new BusinessException(ResultCodes.BUSINESS_ERROR);
                }
            }
        }
        lock.unlock();
        resultVO.setCode(ResultCodes.OK.getCode());
        return resultVO;
    }
 
    /**
     * 获取指定部门指定月份指定业务的月报
     * @param depId
     * @param biz
     * @param year
     * @param month
     * @return
     */
    @Override
    public SearchResultVO<RiskMonthReportRespDTO> getReportByMonth(Long depId, Byte biz, Integer year, Integer month) {
        SearchResultVO<RiskMonthReportRespDTO> resultVO = new SearchResultVO<>();
        resultVO.setUsePage(false);
        //1、参数校验
        if(depId == null || depId < 1 || biz == null || year == null || month == null || year < 2000 || year > 9999 || month < 1|| month > 12){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("参数错误");
            return resultVO;
        }
        if(ReportBizEnum.parse(biz) == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务不支持");
            return resultVO;
        }
        //2、部门校验
        ResultVO<DepInfoRPCRespDTO> depSearchResult = departmentService.getDepInfoByDepId(depId);
        if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("系统调用错误");
            return resultVO;
        }
        if(depSearchResult.getData() == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("部门不存在");
            return resultVO;
        }
        //3、判断查询实时当月数据,还是往期数据
        LocalDate checkDate = LocalDate.of(year,month,1);
        LocalDate nowDate = LocalDate.now();
        LocalDate offsetDate = LocalDate.of(nowDate.getYear(),nowDate.getMonthValue(),1);
        if(checkDate.isAfter(offsetDate)){
            //查询将来的数据
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("超出查询范围");
            return resultVO;
        }else if(checkDate.isBefore(offsetDate)){
            //查询固化报表
            RiskMonthReport riskMonthReport = riskMonthReportService.findByMonth(biz,depId,year,month);
            if(riskMonthReport == null){
                ResultVO genResult = generateDepartmentMonthRiskReport(biz,depId,year,month);
                if(genResult == null || !genResult.getCode().equals(ResultCodes.OK.getCode())){
                    resultVO.setCount(0);
                    resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
                    resultVO.setMsg(genResult.getMsg());
                    return resultVO;
                }else {
                    //重新获取固化报表
                    riskMonthReport = riskMonthReportService.findByMonth(biz,depId,year,month);
                }
            }
            if(riskMonthReport == null){
                resultVO.setCount(0);
                resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
                resultVO.setMsg("查询失败");
                return resultVO;
            }
            //封装后返回
            RiskMonthReportRespDTO dto = packageRiskMonthReportData(riskMonthReport);
            resultVO.setCode(ResultCodes.OK.getCode());
            resultVO.setCount(1);
            resultVO.setData(dto);
            return resultVO;
        }else {
            //查询当月数据
            RiskMonthReportRespDTO reportRespDTO = getNowMonthRiskReport((DepInfoRPCRespDTO) depSearchResult.getData(),biz);
            if(reportRespDTO == null || reportRespDTO.getDetail() == null){
                resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
                resultVO.setMsg("未获取到当前月份数据");
                return resultVO;
            }
            resultVO.setCode(ResultCodes.OK.getCode());
            resultVO.setCount(1);
            resultVO.setData(reportRespDTO);
            return resultVO;
        }
    }
 
    /**
     * 获取指定部门的指定年度报表
     * @param depId
     * @param biz
     * @param year
     * @return
     */
    @Override
    public SearchResultVO<List<RiskMonthReportRespDTO>> getDepartmentReportByYear(Long depId, Byte biz, Integer year) {
        SearchResultVO resultVO = new SearchResultVO<>();
        if(depId == null || biz == null || year == null){
            resultVO.setCode(ResultCodes.SERVER_PARAM_NULL.getCode());
            resultVO.setMsg(ResultCodes.SERVER_PARAM_NULL.getDesc());
        }
        if(ReportBizEnum.parse(biz) == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务不支持");
            return resultVO;
        }
        //部门校验
        ResultVO<DepInfoRPCRespDTO> depSearchResult = departmentService.getDepInfoByDepId(depId);
        if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("系统调用错误");
            return resultVO;
        }
        if(depSearchResult.getData() == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("部门不存在");
            return resultVO;
        }
        //判断是否为当前年度
        Integer maxMonth = 12;
        if(year == LocalDate.now().getYear()){
            maxMonth = LocalDate.now().getMonthValue();
        }
        List<RiskMonthReportRespDTO> reportRespDTOS = new ArrayList<>();
        for(int month = 1;month<= maxMonth;month++){
            SearchResultVO<RiskMonthReportRespDTO> searchResultVO = getReportByMonth(depId,biz,year,month);
            if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){
                reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData());
            }
        }
 
        resultVO.setCode(ResultCodes.OK.getCode());
        if(!reportRespDTOS.isEmpty()){
            resultVO.setData(reportRespDTOS);
            resultVO.setCount(reportRespDTOS.size());
        }
        return resultVO;
    }
 
    /**
     * 获取指定部门连续多个月的报表
     * @param depId
     * @param biz
     * @param beginYear
     * @param beginMonth
     * @param endYear
     * @param endMonth
     * @return
     */
    @Override
    public SearchResultVO<List<RiskMonthReportRespDTO>> getDepartmentReportByMultipleMonth(Long depId, Byte biz, Integer beginYear, Integer beginMonth, Integer endYear, Integer endMonth) {
        SearchResultVO resultVO = new SearchResultVO<>();
        if(depId == null || biz == null || beginYear == null || beginMonth == null || endYear == null || endMonth == null){
            resultVO.setCode(ResultCodes.SERVER_PARAM_NULL.getCode());
            resultVO.setMsg(ResultCodes.SERVER_PARAM_NULL.getDesc());
        }
        if(ReportBizEnum.parse(biz) == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务不支持");
            return resultVO;
        }
        LocalDate beginDate = LocalDate.of(beginYear,beginMonth,1);
        LocalDate endDate = LocalDate.of(endYear,endMonth,1);
        if(beginDate.isAfter(endDate)){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("查询日期错误");
            return resultVO;
        }
        //部门校验
        ResultVO<DepInfoRPCRespDTO> depSearchResult = departmentService.getDepInfoByDepId(depId);
        if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("系统调用错误");
            return resultVO;
        }
        if(depSearchResult.getData() == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("部门不存在");
            return resultVO;
        }
        List<RiskMonthReportRespDTO> reportRespDTOS = new ArrayList<>();
        while (!beginDate.isAfter(endDate)){
            SearchResultVO<RiskMonthReportRespDTO> searchResultVO = getReportByMonth(depId,biz,beginDate.getYear(),
                    beginDate.getMonthValue());
            if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){
                reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData());
            }
            beginDate = beginDate.plusMonths(1);
        }
        resultVO.setCode(ResultCodes.OK.getCode());
        if(!reportRespDTOS.isEmpty()){
            resultVO.setData(reportRespDTOS);
            resultVO.setCount(reportRespDTOS.size());
        }
 
        return resultVO;
    }
 
 
    /**
     * 获取指定部门的子部门连续多个月的报表
     * @param depId
     * @param biz
     * @param beginYear
     * @param beginMonth
     * @param endYear
     * @param endMonth
     * @return
     */
    @Override
    public SearchResultVO<List<RiskMonthReportRespDTO>> getSubDepartmentReportByMultipleMonth(Long depId, Byte biz, Integer beginYear, Integer beginMonth, Integer endYear, Integer endMonth) {
        SearchResultVO resultVO = new SearchResultVO<>();
        if(depId == null || biz == null || beginYear == null || beginMonth == null || endYear == null || endMonth == null){
            resultVO.setCode(ResultCodes.SERVER_PARAM_NULL.getCode());
            resultVO.setMsg(ResultCodes.SERVER_PARAM_NULL.getDesc());
        }
        if(ReportBizEnum.parse(biz) == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务不支持");
            return resultVO;
        }
        LocalDate beginDate = LocalDate.of(beginYear,beginMonth,1);
        LocalDate endDate = LocalDate.of(endYear,endMonth,1);
        if(beginDate.isAfter(endDate)){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("查询日期错误");
            return resultVO;
        }
        //部门校验
        ResultVO<DepInfoRPCRespDTO> depSearchResult = departmentService.getDepInfoByDepId(depId);
        if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("系统调用错误");
            return resultVO;
        }
        if(depSearchResult.getData() == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("部门不存在");
            return resultVO;
        }
 
        //获取子部门列表
        ResultVO<List<DepInfoRPCRespDTO>> subDepListResult = departmentService.listSubDepsByDepId(depId);
        if(subDepListResult == null || !subDepListResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("获取子部门出错");
            return resultVO;
        }
 
        List<DepInfoRPCRespDTO> subDepList = (List<DepInfoRPCRespDTO>) subDepListResult.getData();
        if(subDepList == null || subDepList.isEmpty()){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.OK.getCode());
            resultVO.setMsg("子部门数据未找到");
            return resultVO;
        }
 
        //获取子部门报表
        List<RiskMonthReportRespDTO> reportRespDTOS = new ArrayList<>();
        while (!beginDate.isAfter(endDate)){
            for(DepInfoRPCRespDTO subDep : subDepList){
                if(subDep == null)
                    continue;
                SearchResultVO<RiskMonthReportRespDTO> searchResultVO = getReportByMonth(subDep.getDepId(),biz, beginDate.getYear(), beginDate.getMonthValue());
                if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){
                    reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData());
                }
            }
            beginDate = beginDate.plusMonths(1);
        }
        resultVO.setCode(ResultCodes.OK.getCode());
        if(!reportRespDTOS.isEmpty()){
            resultVO.setData(reportRespDTOS);
            resultVO.setCount(reportRespDTOS.size());
        }
 
        return resultVO;
 
    }
 
    /**
     * 获取公司指定年份的报表
     * @param year
     * @return
     */
    @Override
    public SearchResultVO<List<RiskMonthReportRespDTO>> getCompanyReportByYear(Byte biz,Integer year) {
        SearchResultVO resultVO = new SearchResultVO<>();
 
        //公司所在部门ID
        //todo:需从数据库读取
        Long companyDepId = 1L;
 
        if(ReportBizEnum.parse(biz) == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务不支持");
            return resultVO;
        }
        //部门校验
        ResultVO<DepInfoRPCRespDTO> depSearchResult = departmentService.getDepInfoByDepId(companyDepId);
        if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("系统调用错误");
            return resultVO;
        }
        if(depSearchResult.getData() == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("未获取到公司信息");
            return resultVO;
        }
        //判断是否为当前年度
        Integer maxMonth = 12;
        if(year == LocalDate.now().getYear()){
            maxMonth = LocalDate.now().getMonthValue();
        }
        List<RiskMonthReportRespDTO> reportRespDTOS = new ArrayList<>();
        for(int month = 1;month<= maxMonth;month++){
            SearchResultVO<RiskMonthReportRespDTO> searchResultVO = getReportByMonth(companyDepId,biz,year,month);
            if(searchResultVO != null && searchResultVO.getCode().equals(ResultCodes.OK.getCode()) && searchResultVO.getData() != null){
                reportRespDTOS.add((RiskMonthReportRespDTO) searchResultVO.getData());
            }
        }
 
        resultVO.setCode(ResultCodes.OK.getCode());
        if(!reportRespDTOS.isEmpty()){
            resultVO.setData(reportRespDTOS);
            resultVO.setCount(reportRespDTOS.size());
        }
        return resultVO;
    }
 
    /**
     * 获取公司在指定年月的报表
     * @param year
     * @return
     */
    @Override
    public SearchResultVO<RiskMonthReportRespDTO> getCompanyReportByMonth(Byte biz,Integer year,Integer month) {
        SearchResultVO<RiskMonthReportRespDTO> resultVO = new SearchResultVO<>();
        resultVO.setUsePage(false);
 
        //公司所在部门ID
        //todo:需从数据库读取
        Long companyDepId = 1L;
 
        //1、参数校验
        if(biz == null || year == null || month == null || year < 2000 || year > 9999 || month < 1|| month > 12){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("参数错误");
            return resultVO;
        }
        if(ReportBizEnum.parse(biz) == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("业务不支持");
            return resultVO;
        }
        //2、部门校验
        ResultVO<DepInfoRPCRespDTO> depSearchResult = departmentService.getDepInfoByDepId(companyDepId);
        if(depSearchResult == null || !depSearchResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("系统调用错误");
            return resultVO;
        }
        if(depSearchResult.getData() == null){
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("未获取到公司信息");
            return resultVO;
        }
        //3、判断查询实时当月数据,还是往期数据
        LocalDate checkDate = LocalDate.of(year,month,1);
        LocalDate nowDate = LocalDate.now();
        LocalDate offsetDate = LocalDate.of(nowDate.getYear(),nowDate.getMonthValue(),1);
        if(checkDate.isAfter(offsetDate)){
            //查询将来的数据
            resultVO.setCount(0);
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("超出查询范围");
            return resultVO;
        }else if(checkDate.isBefore(offsetDate)){
            //查询固化报表
            RiskMonthReport riskMonthReport = riskMonthReportService.findByMonth(biz,companyDepId,year,month);
            if(riskMonthReport == null){
                ResultVO genResult = generateDepartmentMonthRiskReport(biz,companyDepId,year,month);
                if(genResult == null || !genResult.getCode().equals(ResultCodes.OK.getCode())){
                    resultVO.setCount(0);
                    resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
                    resultVO.setMsg(genResult.getMsg());
                    return resultVO;
                }else {
                    //重新获取固化报表
                    riskMonthReport = riskMonthReportService.findByMonth(biz,companyDepId,year,month);
                }
            }
            if(riskMonthReport == null){
                resultVO.setCount(0);
                resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
                resultVO.setMsg("查询失败");
                return resultVO;
            }
            //封装后返回
            RiskMonthReportRespDTO dto = packageRiskMonthReportData(riskMonthReport);
            resultVO.setCode(ResultCodes.OK.getCode());
            resultVO.setCount(1);
            resultVO.setData(dto);
            return resultVO;
        }else {
            //查询当月数据
            RiskMonthReportRespDTO reportRespDTO = getNowMonthRiskReport((DepInfoRPCRespDTO) depSearchResult.getData(),biz);
            if(reportRespDTO == null || reportRespDTO.getDetail() == null){
                resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
                resultVO.setMsg("未获取到当前月份数据");
                return resultVO;
            }
            resultVO.setCode(ResultCodes.OK.getCode());
            resultVO.setCount(1);
            resultVO.setData(reportRespDTO);
            return resultVO;
        }
    }
 
    /**
     * 包装报表数据,获取报表详情信息
     * @param report
     * @return
     */
    private RiskMonthReportRespDTO packageRiskMonthReportData(RiskMonthReport report){
        if(report == null || report.getId() == null)
            return null;
        RiskMonthReportRespDTO respDTO = new RiskMonthReportRespDTO();
        BeanUtils.copyProperties(report,respDTO);
        if(report.getBiz().equals(ReportBizEnum.PREVENT_RISK.getType())){
            PreventMonthReportDetailRespDTO detail = getPreventReportDetail(report.getId());
            respDTO.setDetail(detail);
        }else if(report.getBiz().equals(ReportBizEnum.INCIDENT_COUNT.getType())){
            List<IncidentMonthReportItemRespDTO> details = getIncidentReportDetail(report.getId());
            respDTO.setDetail(details);
        }else if(report.getBiz().equals(ReportBizEnum.EMERGENCY_PRACTICE.getType())){
            List<EmergencyPracticeReportItemRespDTO> details = getEmergencyReportDeatil(report.getId());
            respDTO.setDetail(details);
        }
        return respDTO;
    }
 
    /**
     * 获取隐患报表详情数据
     * @param reportId
     * @return
     */
    private PreventMonthReportDetailRespDTO getPreventReportDetail(Long reportId){
        if(reportId == null || reportId < 0)
            return null;
        PreventMonthReportDetail detail = preventMonthReportDetailService.getById(reportId);
        if(detail == null)
            return null;
        PreventMonthReportDetailRespDTO dto = new PreventMonthReportDetailRespDTO();
        BeanUtils.copyProperties(detail,dto);
        return dto;
    }
 
    /**
     * 获取应急演练报表详情数据
     * @param reportId
     * @return
     */
    private List<EmergencyPracticeReportItemRespDTO> getEmergencyReportDeatil(Long reportId){
        if(reportId == null || reportId < 0)
            return null;
        List<EmergencyPracticeReportItem> itemList = emergencyPracticeReportItemService.listByReportId(reportId);
        if(itemList == null || itemList.isEmpty())
            return null;
 
        List<EmergencyPracticeReportItemRespDTO> dtoList = new ArrayList<>();
        for(EmergencyPracticeReportItem item : itemList){
            if(item == null)
                continue;
            EmergencyPracticeReportItemRespDTO dto = new EmergencyPracticeReportItemRespDTO();
            BeanUtils.copyProperties(item,dto);
            dtoList.add(dto);
        }
 
        return dtoList;
    }
 
    /**
     * 获取事故管理报表详情数据
     * @param reportId
     * @return
     */
    private List<IncidentMonthReportItemRespDTO> getIncidentReportDetail(Long reportId){
        if(reportId == null || reportId < 0)
            return null;
        List<IncidentMonthReportItem> itemList = incidentMonthReportItemService.listByReportId(reportId);
        if(itemList == null || itemList.isEmpty())
            return null;
 
        List<IncidentMonthReportItemRespDTO> dtoList = new ArrayList<>();
        for(IncidentMonthReportItem item : itemList){
            if(item == null)
                continue;
            IncidentMonthReportItemRespDTO dto = new IncidentMonthReportItemRespDTO();
            BeanUtils.copyProperties(item,dto);
            dtoList.add(dto);
        }
 
        return dtoList;
    }
 
    /**
     * 获取当月的风险报告数据
     * @param depInfo
     * @param biz
     * @return
     */
    private RiskMonthReportRespDTO getNowMonthRiskReport(DepInfoRPCRespDTO depInfo,Byte biz){
        if(depInfo == null || biz == null || depInfo.getDepId() == null || ReportBizEnum.parse(biz) == null){
            return null;
        }
        LocalDate nowDate = LocalDate.now();
        //生成报表对象
        RiskMonthReportRespDTO reportRespDTO = new RiskMonthReportRespDTO();
        reportRespDTO.setBiz(biz);
        reportRespDTO.setDepId(depInfo.getDepId());
        reportRespDTO.setDepLevel(depInfo.getDepLevel());
        reportRespDTO.setYear(nowDate.getYear());
        reportRespDTO.setMonth(nowDate.getMonthValue());
        reportRespDTO.setBeginDate(DateUtil.beginOfMonth(Date.from(nowDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())).toLocalDateTime().toLocalDate());
        reportRespDTO.setEndDate(DateUtil.endOfMonth(Date.from(nowDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())).toLocalDateTime().toLocalDate());
        RiskReportAnalysisHandler riskReportAnalysisHandler = new RiskReportAnalysisHandler();
        //获取报表详细数据,并打包数据
        if(biz.equals(ReportBizEnum.PREVENT_RISK.getType())){
            //双重预防隐患统计
            RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,nowDate.getYear(), nowDate.getMonthValue());
            PreventMonthReportDetail reportDetail = riskReportAnalysisHandler.analysisPrevReportData(riskDataBO);
            PreventMonthReportDetailRespDTO detailRespDTO = packagePreventReportDetailDto(reportDetail);
            reportRespDTO.setDetail(detailRespDTO);
        }else if(biz.equals(ReportBizEnum.EMERGENCY_PRACTICE.getType())){
            //应急管理统计
            EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo, nowDate.getYear(), nowDate.getMonthValue());
            List<EmergencyPracticeReportItem> reportItemList =
                    riskReportAnalysisHandler.analysisEmgcReportData(emergencyDataBO);
            List<EmergencyPracticeReportItemRespDTO> itemRespDTOList = packageEmergencyReportDetailDto(reportItemList);
            reportRespDTO.setDetail(itemRespDTOList);
        }else if(biz.equals(ReportBizEnum.INCIDENT_COUNT.getType())){
            //事故管理统计
            IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,nowDate.getYear(), nowDate.getMonthValue());
            List<IncidentMonthReportItem> reportItemList =
                    riskReportAnalysisHandler.analysisIncidentReportDate(incidentDataBO);
            List<IncidentMonthReportItemRespDTO> reportItemRespDTOS = packageIncidentReportDetailDto(reportItemList);
            reportRespDTO.setDetail(reportItemRespDTOS);
        }
        return reportRespDTO;
    }
 
    /**
     * 打包应急管理统计数据
     * @param reportItemList
     * @return
     */
    private List<EmergencyPracticeReportItemRespDTO> packageEmergencyReportDetailDto(List<EmergencyPracticeReportItem> reportItemList){
        if(reportItemList == null || reportItemList.isEmpty())
            return null;
        List<EmergencyPracticeReportItemRespDTO> itemRespDTOList = new ArrayList<>();
        for(EmergencyPracticeReportItem item : reportItemList){
            if(item == null)
                continue;
            EmergencyPracticeReportItemRespDTO itemRespDTO = new EmergencyPracticeReportItemRespDTO();
            BeanUtils.copyProperties(item,itemRespDTO);
            itemRespDTOList.add(itemRespDTO);
        }
        return itemRespDTOList;
    }
 
    /**
     * 打包事故管理统计数据
     * @param reportItemList
     * @return
     */
    private List<IncidentMonthReportItemRespDTO> packageIncidentReportDetailDto(List<IncidentMonthReportItem> reportItemList){
        if(reportItemList == null || reportItemList.isEmpty())
            return null;
        List<IncidentMonthReportItemRespDTO> itemRespDTOList = new ArrayList<>();
        for(IncidentMonthReportItem item : reportItemList){
            if(item == null)
                continue;
            IncidentMonthReportItemRespDTO dto = new IncidentMonthReportItemRespDTO();
            BeanUtils.copyProperties(item,dto);
            itemRespDTOList.add(dto);
        }
        return itemRespDTOList;
    }
 
    /**
     * 打包双重预防隐患统计数据
     * @param reportDetail
     * @return
     */
    private PreventMonthReportDetailRespDTO packagePreventReportDetailDto(PreventMonthReportDetail reportDetail){
        if(reportDetail == null)
            return null;
        PreventMonthReportDetailRespDTO dto = new PreventMonthReportDetailRespDTO();
        BeanUtils.copyProperties(reportDetail,dto);
        return dto;
    }
}