郑永安
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
package com.gkhy.safePlatform.riskCtrl.service.impl;
 
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.enums.DepartmentLevelEnum;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepRPCRespDTO;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.doublePrevent.rpc.apimodel.PreventRPCDataCountService;
import com.gkhy.safePlatform.emergency.rpc.api.EmergencyRpcAPi;
import com.gkhy.safePlatform.incidentManage.rpc.api.IncidentManageRpcAPi;
import com.gkhy.safePlatform.riskCtrl.constant.SpiWeighValue;
import com.gkhy.safePlatform.riskCtrl.entity.RiskSpiReport;
import com.gkhy.safePlatform.riskCtrl.entity.SpiReportDetail;
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.utils.SpiItemAnalysisHandler;
import com.gkhy.safePlatform.riskCtrl.model.dto.req.CreateNewRespSpiReportReqDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.req.SearchRiskSpiReportConditionDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.RiskSpiRepostRespDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.SpiReportDetailDTO;
import com.gkhy.safePlatform.riskCtrl.model.dto.resp.SpiValueRespDTO;
import com.gkhy.safePlatform.riskCtrl.service.RiskSpiService;
import com.gkhy.safePlatform.riskCtrl.service.baseService.*;
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 javax.transaction.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
@Service
public class RiskSpiServiceImpl implements RiskSpiService {
 
    @Autowired
    private RiskSpiReportService riskSpiReportService;
 
    @Autowired
    private SpiReportDetailService spiReportDetailService;
 
    @Autowired
    private RiskMonthReportService riskMonthReportService;
 
    @Autowired
    private PreventMonthReportDetailService preventMonthReportDetailService;
 
    @Autowired
    private IncidentMonthReportItemService incidentMonthReportItemService;
 
    @Autowired
    private EmergencyPracticeReportItemService emergencyPracticeReportItemService;
 
    @Autowired
    private CollectReportDataService collectReportDataService;
 
    @DubboReference(check = false)
    private AccountDepartmentService departmentService;
 
    @DubboReference(check = false)
    private IncidentManageRpcAPi incidentManageRpcAPi;
 
    @DubboReference(check = false)
    private PreventRPCDataCountService preventRPCDataCountService;
 
    @DubboReference(check = false)
    private EmergencyRpcAPi emergencyRpcAPi;
 
    @Autowired
    private RedissonClient redissonClient;
 
 
    @Transactional
    @Override
    public ResultVO saveOneRiskSpiReport(CreateNewRespSpiReportReqDTO dto) {
        ResultVO resultVO = new ResultVO<>();
        if(dto == null){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_NULL.getCode());
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc());
            return resultVO;
        }
        RiskSpiReport report = new RiskSpiReport();
        BeanUtils.copyProperties(dto,report);
        report.setGmtCreate(LocalDateTime.now());
        if(riskSpiReportService.getBaseMapper().insert(report) > 0){
            resultVO.setCode(ResultCodes.OK.getCode());
        }else {
            resultVO.setCode(ResultCodes.SERVER_ADD_ERROR.getCode());
            resultVO.setMsg(ResultCodes.SERVER_ADD_ERROR.getDesc());
        }
        return resultVO;
    }
 
    @Override
    public SearchResultVO<RiskSpiRepostRespDTO> getOneRiskSpiReportById(Long reportId) {
        return null;
    }
 
    @Override
    public SearchResultVO<List<RiskSpiRepostRespDTO>> listRiskSpiReportListByConditions(SearchRiskSpiReportConditionDTO conditionDTO) {
        SearchResultVO<List<RiskSpiRepostRespDTO>> resultVO = new SearchResultVO<>();
        resultVO.setCode(ResultCodes.OK.getCode());
        resultVO.setUsePage(false);
        resultVO.setCount(0);
        if(conditionDTO == null){
            return resultVO;
        }
        if(conditionDTO.getYear() != null && conditionDTO.getBeginMonth() != null){
            if(conditionDTO.getEndMonth() != null && conditionDTO.getBeginMonth() < conditionDTO.getEndMonth()){
                return resultVO;
            }
        }
        List<RiskSpiReport> reportList = riskSpiReportService.listByConditions(conditionDTO);
        if(reportList != null && !reportList.isEmpty()){
            List<RiskSpiRepostRespDTO> dtoList = new ArrayList<>();
            reportList.forEach(r -> {
                RiskSpiRepostRespDTO dto = new RiskSpiRepostRespDTO();
                BeanUtils.copyProperties(r,dto);
                dtoList.add(dto);
            });
        }
        return resultVO;
    }
 
    /**
     * 查找部门月度SPI报表
     * @param depId
     * @param year
     * @param month
     * @return
     */
    @Override
    public SearchResultVO<RiskSpiRepostRespDTO> getMonthlySpiReport(Long depId, Integer year, Integer month) {
        SearchResultVO<RiskSpiRepostRespDTO> searchResultVO = new SearchResultVO<>();
        searchResultVO.setCode(ResultCodes.OK.getCode());
        searchResultVO.setCount(0);
        searchResultVO.setUsePage(false);
        RiskSpiReport spiReport = riskSpiReportService.getMonthlySpiReport(depId,year,month);
        if(spiReport != null && spiReport.getDepId().equals(depId)){
            searchResultVO.setCount(1);
            RiskSpiRepostRespDTO dto = new RiskSpiRepostRespDTO();
            BeanUtils.copyProperties(spiReport,dto);
            searchResultVO.setData(dto);
        }
        return searchResultVO;
    }
 
    /**
     * 生成部门级SPI月报
     * @param depId
     * @param year
     * @param month
     * @return
     */
    @Override
    public ResultVO generateDepartmentSpiMonthReport(Long depId, Integer year, Integer month) {
        ResultVO resultVO = new ResultVO<>();
        if(depId == null || depId < 0 || year == null || month == null){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("参数错误");
            return resultVO;
        }
        //1、判断年份月份是否合理
        LocalDateTime nowTime = LocalDateTime.now();
        if(year > nowTime.getYear() || year < 2022){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("年份错误");
            return resultVO;
        }else if(year.equals(nowTime.getYear()) && month >= nowTime.getMonthValue()){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("月份错误");
            return resultVO;
        }
        //必须至少提前21天
        if(nowTime.getDayOfMonth() < 21){
            LocalDate td = LocalDate.of(year,month,21);
            if(td.plusMonths(1L).isAfter(nowTime.toLocalDate())){
                resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
                resultVO.setMsg("未到生成月报时间");
                return resultVO;
            }
        }
        //2、判断部门是否存在
        ResultVO<DepInfoRPCRespDTO> depInfoRpcResult = departmentService.getDepInfoByDepId(depId);
        if(!depInfoRpcResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCode(ResultCodes.SERVER_ERROR);
            return resultVO;
        }
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depInfoRpcResult.getData();
        if(depInfo == null){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("部门不存在");
            return resultVO;
        }
        //加分布式锁
        String lockName = "LOCK_RISKCTRL_SPI_REPORT_"+depId;
        RLock lock = redissonClient.getLock(lockName);
        //最长加锁时间30秒
        lock.lock(30, TimeUnit.SECONDS);
        //看是否已经生成该部门指定年月的SPI报表
        SearchResultVO<RiskSpiRepostRespDTO> riskSpiSearchResult = getMonthlySpiReport(depId,year, month);
        if(riskSpiSearchResult.getCode().equals(ResultCodes.OK.getCode()) && riskSpiSearchResult.getCount() > 0){
            //该部门已经生成了指定年月的SPI报表
            lock.unlock(); //释放锁
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setMsg("SPI月报已经生成");
            return resultVO;
        }
        //计算SPI
        ResultVO<SpiValueRespDTO> calSpiResultVO = calSpiValue(depId,year, month);
        if(calSpiResultVO.getCode().equals(ResultCodes.OK.getCode())){
            SpiValueRespDTO spiValueRespDTO = (SpiValueRespDTO) calSpiResultVO.getData();
            if(spiValueRespDTO != null && depId.longValue() == spiValueRespDTO.getDepId().longValue()){
                //生成月报入库
                RiskSpiReport riskSpiReport = new RiskSpiReport();
                riskSpiReport.setDepId(depId);
                riskSpiReport.setYear(year);
                riskSpiReport.setMonth(month);
                riskSpiReport.setVal(spiValueRespDTO.getSpiVal());
                riskSpiReport.setGmtCreate(LocalDateTime.now());
                if(riskSpiReportService.save(riskSpiReport) == true){
                    resultVO.setCode(ResultCodes.OK.getCode());
                    lock.unlock();
                }else {
                    lock.unlock();
                    throw new RuntimeException("数据库保存SPI月度报表出错");
                }
            }
        }else {
            lock.unlock();
        }
        return resultVO;
    }
 
    /**
     * 定时调度生成部门SPI月度报表
     * @param depId
     * @return
     */
    @Transactional
    @Override
    public ResultVO generateDepartmentSpiReportMonthlyScheduled(Long depId) {
        ResultVO resultVO = new ResultVO<>();
        if(depId == null || depId < 0){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("参数错误");
        }
        LocalDate nowDate = LocalDate.now();
        //当前日期已经过了21号,可以生成上个月的固化报表
        if(nowDate.getDayOfMonth() > 15){
            LocalDate targetTime = nowDate.minusMonths(1L);
            resultVO = generateDepartmentSpiMonthReport(depId, targetTime.getYear(), targetTime.getMonthValue());
        }else {
            resultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            resultVO.setCode("不在生成日报的时间");
        }
        return resultVO;
    }
 
    /**
     * 定时调度生成企业SPI月度报表
     * @param eid
     * @return
     */
    @Transactional
    @Override
    public ResultVO generateCompanySpiReportMonthlyScheduled(Long eid) {
        ResultVO generateResultVO = new ResultVO<>();
        ResultVO<List<DepRPCRespDTO>> resultVO = departmentService.depList();
        if(!resultVO.getCode().equals(ResultCodes.OK.getCode())){
            generateResultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            generateResultVO.setMsg("RPC获取部门数据出错");
            return generateResultVO;
        }
        List<DepRPCRespDTO> depList = (List<DepRPCRespDTO>) resultVO.getData();
        if(depList == null || depList.isEmpty()){
            generateResultVO.setCode(ResultCodes.BUSINESS_ERROR.getCode());
            generateResultVO.setMsg("未获取到部门数据");
        }
        depList.forEach(dep -> {
            if(dep.getDepLevel().equals(DepartmentLevelEnum.COMPANY.code)){
                //公司
            }else if(dep.getDepLevel().equals(DepartmentLevelEnum.BUSINESS.code)){
                //事业部
            } else if(dep.getDepLevel().equals(DepartmentLevelEnum.WORKSHOP.code)){
                //车间
            }
        });
        //todo : 完善公司级月报
        return generateResultVO;
    }
 
    @Transactional
    @Override
    public ResultVO<SpiValueRespDTO> calSpiValue(Long depId, Integer year, Integer month) {
        ResultVO<SpiValueRespDTO> resultVO = new ResultVO<>();
        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("部门不存在");
        }
        //1、获取双重预防隐患统计数据
        RiskDataBO riskDataBO = collectReportDataService.getPreventRiskData(depInfo,year,month);
        //2、获取事故管理统计数据
        IncidentDataBO incidentDataBO = collectReportDataService.getIncidentCountData(depInfo,year,month);
        //3、获取应急演练次数统计数据
        EmergencyDataBO emergencyDataBO = collectReportDataService.getEmergencyCountData(depInfo,year,month);
 
        SpiItemAnalysisHandler spiItemAnalysisHandler = new SpiItemAnalysisHandler();
 
        //4.1计算I1
        BigDecimal i1 = BigDecimal.valueOf(0);
        if(riskDataBO != null){
            i1 = spiItemAnalysisHandler.calI1(riskDataBO);
        }
 
        //4.2计算I2
        BigDecimal i2 = BigDecimal.valueOf(0);
        if(riskDataBO != null){
            i2 = spiItemAnalysisHandler.calI2(riskDataBO);
        }
 
        //4.3计算I3
        BigDecimal i3 = BigDecimal.valueOf(0);
        if(riskDataBO != null){
            i3 = spiItemAnalysisHandler.calI3(riskDataBO);
        }
 
        //4.4计算I6
        BigDecimal i6 = BigDecimal.valueOf(0);
        if(emergencyDataBO != null){
            i6 = spiItemAnalysisHandler.calI6(emergencyDataBO);
        }
 
        //4.5计算I7
        BigDecimal i7 = BigDecimal.valueOf(0);
        if(emergencyDataBO != null){
            i7 = spiItemAnalysisHandler.calI7(emergencyDataBO,true,year,
                    month);
        }
 
        //4.6计算I8
        BigDecimal i8 = BigDecimal.valueOf(0);
        if(incidentDataBO != null){
            i8 = spiItemAnalysisHandler.calI8Val(incidentDataBO);
        }
 
        //4.7计算I9
        BigDecimal i9 = BigDecimal.valueOf(0);
        if(incidentDataBO != null){
            i9 = spiItemAnalysisHandler.calI9Val(incidentDataBO);
        }
 
        //5、计算SPI
        BigDecimal spiVal =
                i1.multiply(SpiWeighValue.W1)
                        .add(i2.multiply(SpiWeighValue.W2))
                        .add(i3.multiply(SpiWeighValue.W3))
                        .add(i6.multiply(SpiWeighValue.W6))
                        .add(i7.multiply(SpiWeighValue.W7))
                        .add(i8.multiply(SpiWeighValue.W8))
                        .add(i9.multiply(SpiWeighValue.W9));
 
        SpiValueRespDTO spiValueRespDTO = new SpiValueRespDTO();
        spiValueRespDTO.setDepId(depId);
        spiValueRespDTO.setDepLevel(depInfo.getDepLevel());
        spiValueRespDTO.setDepName(depInfo.getDepName());
        spiValueRespDTO.setYear(year);
        spiValueRespDTO.setMonth(month);
        spiValueRespDTO.setSpiVal(spiVal.setScale(2, RoundingMode.HALF_UP));
 
        resultVO.setCode(ResultCodes.OK.getCode());
        resultVO.setData(spiValueRespDTO);
 
        return resultVO;
    }
 
    /**
     * 计算最近12个月的SPI值
     * @param depId
     * @return
     */
    @Override
    public ResultVO<List<SpiValueRespDTO>> calSpiValueForLast12Months(Long depId) {
        ResultVO<List<SpiValueRespDTO>> resultVO = new ResultVO<>();
        resultVO.setCode(ResultCodes.OK.getCode());
        List<SpiValueRespDTO> list = new ArrayList<>();
        //获取最近的12个月
        LocalDateTime nowTime = LocalDateTime.now();
        for(long i=11;i>=0;i--){
            LocalDateTime targetTime = nowTime.minusMonths(i);
//            ResultVO<SpiValueRespDTO> r = calSpiValue(depId, targetTime.getYear(), targetTime.getMonthValue());
            ResultVO<SpiValueRespDTO> r = getSpiReportByMonth(depId, targetTime.getYear(),targetTime.getMonthValue());
            if(r.getCode().equals(ResultCodes.OK.getCode()) && r.getData() != null){
                list.add((SpiValueRespDTO) r.getData());
            }
        }
        resultVO.setData(list);
        return resultVO;
    }
 
    /**
     * 获取任意部门指定年月份的SPI报表
     * @param depId
     * @param year
     * @param month
     * @return
     */
    @Override
    public ResultVO<SpiValueRespDTO> getSpiReportByMonth(Long depId, Integer year, Integer month) {
        ResultVO<SpiValueRespDTO> resultVO = new ResultVO<>();
        if(depId == null || depId < 0 || year == null || month == null){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("参数错误");
            return resultVO;
        }
        //1、判断年份月份是否合理
        LocalDateTime nowTime = LocalDateTime.now();
        if(year > nowTime.getYear() || year < 2022){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("年份错误");
            return resultVO;
        }else if(year.equals(nowTime.getYear()) && month > nowTime.getMonthValue()){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("月份错误");
            return resultVO;
        }
        //2、判断部门是否存在
        ResultVO<DepInfoRPCRespDTO> depInfoRpcResult = departmentService.getDepInfoByDepId(depId);
        if(!depInfoRpcResult.getCode().equals(ResultCodes.OK.getCode())){
            resultVO.setCode(ResultCodes.SERVER_ERROR);
            return resultVO;
        }
        DepInfoRPCRespDTO depInfo = (DepInfoRPCRespDTO) depInfoRpcResult.getData();
        if(depInfo == null){
            resultVO.setCode(ResultCodes.CLIENT_PARAM_ERROR.getCode());
            resultVO.setMsg("部门不存在");
            return resultVO;
        }
        //3、查找该月份是否有固化报表,有就返回报表信息,没有就生成该月的固化报表,并返回报表信息
        RiskSpiReport riskSpiReport = riskSpiReportService.getMonthlySpiReport(depId,year,month);
        if(riskSpiReport == null || riskSpiReport.getDepId() == null){
            //不存在固化报表,要先生成
            ResultVO genReportResult = generateDepartmentSpiMonthReport(depId,year,month);
            if(genReportResult.getCode().equals(ResultCodes.OK.getCode())){
                //生成月报成功
                //重新从数据库获取数据
                riskSpiReport = riskSpiReportService.getMonthlySpiReport(depId,year,month);
                if(riskSpiReport != null){
                    //打包数据
                    SpiValueRespDTO spiValueRespDTO = packageSpiDto(riskSpiReport);
                    spiValueRespDTO.setDepName(depInfo.getDepName());
                    spiValueRespDTO.setDepLevel(depInfo.getDepLevel());
                    resultVO.setData(spiValueRespDTO);
                }
            }else {
                //没有成功生成月报
                //实时计算SPI
                ResultVO<SpiValueRespDTO> calRes = calSpiValue(depId,year,month);
                if(calRes.getCode().equals(ResultCodes.OK.getCode()) && calRes.getData() != null){
                    SpiValueRespDTO spiValueRespDTO = (SpiValueRespDTO) calRes.getData();
                    resultVO.setData(spiValueRespDTO);
                }
            }
        }else {
            //存在该月的固化报表,可直接用
            //打包数据
            SpiValueRespDTO spiValueRespDTO = packageSpiDto(riskSpiReport);
            spiValueRespDTO.setDepName(depInfo.getDepName());
            spiValueRespDTO.setDepLevel(depInfo.getDepLevel());
            resultVO.setData(spiValueRespDTO);
        }
        resultVO.setCode(ResultCodes.OK.getCode());
        return resultVO;
    }
 
    /**
     * 新增一个部门的SPI详细月报记录
     * @param depId
     * @param year
     * @param month
     * @return
     */
    @Override
    public ResultVO addOneSpiReportDetail(Long depId, Integer year, Integer month) {
        return null;
    }
 
    /**
     * 获取指定部门指定年月的SPI详细报表
     * @param eid
     * @param depId
     * @param year
     * @param month
     * @return
     */
    @Override
    public SearchResultVO<SpiReportDetailDTO> findSpiReportDetailForOne(Long eid, Long depId, Integer year, Integer month) {
        SearchResultVO<SpiReportDetailDTO> resultVO = new SearchResultVO<>();
        resultVO.setUsePage(false);
        resultVO.setCode(ResultCodes.OK.getCode());
        if(depId == null || depId < 0 || year == null || month == null){
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_NULL.getDesc());
            return resultVO;
        }
        if(year < 2000 || year > 9999 || month < 0 || month > 12){
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc());
            return resultVO;
        }
        SpiReportDetail detail = spiReportDetailService.findOneByMonth(eid,depId,year,month);
        if(detail != null){
            SpiReportDetailDTO detailDTO = new SpiReportDetailDTO();
            BeanUtils.copyProperties(detail,detailDTO);
            resultVO.setCount(1);
            resultVO.setData(detailDTO);
        }
        return resultVO;
    }
 
    /**
     * 获取指定部门指定日期区间内的SPI报表列表
     * @param eid
     * @param depId
     * @param startYear
     * @param startMonth
     * @param endYear
     * @param endMonth
     * @return
     */
    @Override
    public SearchResultVO<List<SpiReportDetailDTO>> listSpiReportDetailByCondition(Long eid, Long depId, Integer startYear,
                                                                                   Integer startMonth,Integer endYear,
                                                                                   Integer endMonth) {
        SearchResultVO<List<SpiReportDetailDTO>> resultVO = new SearchResultVO<>();
        resultVO.setCode(ResultCodes.OK.getCode());
        resultVO.setUsePage(false);
        if(depId == null || depId < 0 || startYear == null || startMonth == null || endYear == null || endMonth == null){
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_NULL.getDesc());
            return resultVO;
        }
        if(startYear < 2000 || startYear > 9999 || startMonth < 1 || startMonth > 12){
            resultVO.setMsg(ResultCodes.CLIENT_PARAM_ERROR.getDesc());
            return resultVO;
        }
        LocalDate countBeginDate = LocalDate.of(startYear,startMonth,1);
        LocalDate countEndDate = LocalDate.of(endYear,endMonth,1);
        List<SpiReportDetail> detailList = spiReportDetailService.listByConditions(eid,depId,countBeginDate,
                countEndDate);
        if(detailList != null && !detailList.isEmpty()){
            List<SpiReportDetailDTO> detailDTOList = new ArrayList<>();
            for(SpiReportDetail detail : detailList){
                if(detail != null){
                    SpiReportDetailDTO detailDTO = new SpiReportDetailDTO();
                    BeanUtils.copyProperties(detail,detailDTO);
                    detailDTOList.add(detailDTO);
                }
            }
            resultVO.setData(detailDTOList);
            resultVO.setCount(detailDTOList.size());
        }
        return resultVO;
    }
 
    /**
     * 将数据库取出的SPI月报对象重新包装成接口需要的DTO
     * @param riskSpiReport
     * @return
     */
    private SpiValueRespDTO packageSpiDto(RiskSpiReport riskSpiReport){
        if(riskSpiReport == null || riskSpiReport.getDepId() == null ){
            return null;
        }
        SpiValueRespDTO spiValueRespDTO = new SpiValueRespDTO();
        spiValueRespDTO.setDepId(riskSpiReport.getDepId());
        spiValueRespDTO.setYear(riskSpiReport.getYear());
        spiValueRespDTO.setMonth(riskSpiReport.getMonth());
        spiValueRespDTO.setSpiVal(riskSpiReport.getVal());
        return spiValueRespDTO;
    }
 
 
 
 
}