zf
2023-08-04 869edb425d2e931d756c756c16ee5b3f42ee027a
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
package com.gk.hotwork.Service.ServiceImpl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.hotwork.Domain.*;
import com.gk.hotwork.Domain.Do.SafetySelfInspectionItemQualifiedCountDO;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Utils.BeanUtils;
import com.gk.hotwork.Domain.Utils.StringUtils;
import com.gk.hotwork.Domain.Vo.UserVo;
import com.gk.hotwork.Domain.dto.resp.SafetySelfInspectionElementRespDTO;
import com.gk.hotwork.Domain.dto.resp.SafetySelfInspectionRespDTO;
import com.gk.hotwork.Mapper.*;
import com.gk.hotwork.Service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service("SafetySelfInspectionService")
@Transactional
public class SafetySelfInspectionImpl extends ServiceImpl<SafetySelfInspectionMapper,SafetySelfInspection> implements SafetySelfInspectionService {
 
    @Autowired
    private SafetySelfInspectionMapper safetySelfInspectionMapper;
    @Autowired
    private SafetySelfInspectionItemMapper safetySelfInspectionItemMapper;
    @Autowired
    private SafetySelfInspectionItemDeductionMapper safetySelfInspectionItemDeductionMapper;
    @Autowired
    private SafetyInspectionItemDeductionMapper safetyInspectionItemDeductionMapper;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ElementManagementService elementManagementService;
    @Autowired
    private SafetyInspectionElementAService safetyInspectionElementAService;
    @Autowired
    private UserService userService;
    @Autowired
    private InspectionExpertGroupService expertGroupService;
    @Autowired
    private InspectionExpertService expertService;
    @Autowired
    private InspectionHiddenDangerService dangerService;
    @Autowired
    private AttachmentInfoService attachmentInfoService;
 
    /**
     * @Description: 自查分页
     */
    @Override
    public IPage<SafetySelfInspection> selectPage(Page<SafetySelfInspection> page, Map<String, Object> filter, UserInfo user) {
        Integer type = user.getType();
        //企业用户
        if (type.equals(3)) {
            Long companyid = user.getCompanyid();
            filter.put("checkedCompanyId",companyid);
        }
        //监管用户
        if (type.equals(2)) {
            //获取企业信息
            filter.put("province",user.getProvince());
            filter.put("city",user.getCity());
            filter.put("area",user.getCounty());
        }
        filter.put("flag",(byte)2);
 
        IPage<SafetySelfInspection> res = safetySelfInspectionMapper.selectPages(page, filter);
        List<SafetySelfInspection> records = res.getRecords();
        if (CollectionUtils.isNotEmpty(records)){
            DecimalFormat df = new DecimalFormat("0.00%");
            records = records.stream().map((safetySelfInspection) -> {
                Long id = safetySelfInspection.getId();
                SafetySelfInspectionItemQualifiedCountDO countDO = safetySelfInspectionItemMapper.countQualifiedDataById(id);
                safetySelfInspection.setUnqualifiedItem(countDO.getItemSum() - countDO.getQualifiedItem());
                if (countDO != null && countDO.getItemSum() != 0 && countDO.getItemSum() != null) {
                    BigDecimal rate = new BigDecimal(countDO.getQualifiedItem())
                            .divide(new BigDecimal(countDO.getItemSum()), 4, BigDecimal.ROUND_HALF_UP);
                    String qualifiedRate = df.format(rate);
                    safetySelfInspection.setQualifiedRate(qualifiedRate);
                    safetySelfInspection.setItemSum(countDO.getItemSum());
                }
                return safetySelfInspection;
            }).collect(Collectors.toList());
            res.setRecords(records);
        }
        return res;
    }
 
    /**
     * @Description: 监管检查分页
     */
    @Override
    public IPage<SafetySelfInspection> selectSupervisePage(Page<SafetySelfInspection> page, Map<String, Object> filter, UserInfo user) {
        Integer type = user.getType();
        //普通用户
        if (type.equals(3)) {
            Long companyid = user.getCompanyid();
            filter.put("checkedCompanyId",companyid);
        }
        //监管用户
        if (type.equals(2)) {
            //获取企业信息
            filter.put("province",user.getProvince());
            filter.put("city",user.getCity());
            filter.put("area",user.getCounty());
        }
        filter.put("flag",(byte)1);
 
        IPage<SafetySelfInspection> res = safetySelfInspectionMapper.selectPages(page, filter);
        List<SafetySelfInspection> records = res.getRecords();
        if (CollectionUtils.isNotEmpty(records)){
            DecimalFormat df = new DecimalFormat("0.00%");
            records = records.stream().map((safetySelfInspection) -> {
                Long id = safetySelfInspection.getId();
                SafetySelfInspectionItemQualifiedCountDO countDO = safetySelfInspectionItemMapper.countQualifiedDataById(id);
                safetySelfInspection.setUnqualifiedItem(countDO.getItemSum() - countDO.getQualifiedItem());
                if (countDO != null && countDO.getItemSum() != 0 && countDO.getItemSum() != null) {
                    BigDecimal rate = new BigDecimal(countDO.getQualifiedItem())
                            .divide(new BigDecimal(countDO.getItemSum()), 4, BigDecimal.ROUND_HALF_UP);
                    String qualifiedRate = df.format(rate);
                    safetySelfInspection.setQualifiedRate(qualifiedRate);
                    safetySelfInspection.setItemSum(countDO.getItemSum());
                }
                return safetySelfInspection;
            }).collect(Collectors.toList());
            res.setRecords(records);
        }
        return res;
    }
 
    //累计过出不合格扣分
    private Integer unqualifiedItemDelScore(List<SafetySelfInspectionItem> itemList){
        int delScore = 0;
        //过滤出不合格
        List<SafetySelfInspectionItem> unqualifiedItemList = itemList
                .stream()
                .filter(item -> item.getSafetyInspectionItemResult().equals(1))
                .collect(Collectors.toList());
        for (SafetySelfInspectionItem unqualifiedItem : unqualifiedItemList){
            List<SafetySelfInspectionItemDeduction> itemDeductionList = safetySelfInspectionItemDeductionMapper
                    .selectList(new LambdaQueryWrapper<SafetySelfInspectionItemDeduction>()
                            .eq(SafetySelfInspectionItemDeduction::getSafetySelfInspectionItemId, unqualifiedItem.getId())
                            .eq(SafetySelfInspectionItemDeduction::getValidFlag,1));
            for (SafetySelfInspectionItemDeduction itemDeduction : itemDeductionList) {
                delScore += itemDeduction.getPoint();
            }
        }
        return delScore;
    }
 
 
 
    /**
     * @Description: 新增
     */
    @Override
    @Transactional
    public void addOne(SafetySelfInspection param, UserInfo user) {
        requiredVerification(param);
 
        Date date = new Date();
        String username = user.getRealname();
        param.setValidFlag(Boolean.TRUE);
        param.setFlag((byte) 2);
        param.setUpdateBy(username);
        param.setCreateBy(username);
        param.setUpdateTime(date);
        param.setCreateTime(date);
        param.setCheckUnit(user.getCompany());
        this.save(param);
 
        if (CollectionUtils.isNotEmpty(param.getItemList())) {
            for (SafetySelfInspectionItem safetySelfInspectionItem : param.getItemList()) {
                //默认合格
                safetySelfInspectionItem.setSafetyInspectionItemResult(2);
                safetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                safetySelfInspectionItem.setUpdateBy(username);
                safetySelfInspectionItem.setCreateBy(username);
                safetySelfInspectionItem.setUpdateTime(date);
                safetySelfInspectionItem.setCreateTime(date);
                safetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(safetySelfInspectionItem);
            }
        }
        if(CollectionUtils.isNotEmpty(param.getElementAList())){
            for (SafetyInspectionElementA inspectionElementA : param.getElementAList()) {
                SafetyInspectionElementA safetyInspectionElementA = new SafetyInspectionElementA();
                safetyInspectionElementA.setSafetyInspectionId(param.getId());
                safetyInspectionElementA.setElementA(inspectionElementA.getElementA());
                safetyInspectionElementAService.save(safetyInspectionElementA);
            }
        }
 
    }
 
    /**
     * 监管检查-新增
     * @param param
     * @param user
     */
    @Transactional
    @Override
    public void addSupervise(SafetySelfInspection param, UserInfo user) {
        requiredSuperviseVerification(param);
 
        Date date = new Date();
        String username = user.getRealname();
        param.setValidFlag(Boolean.TRUE);
        param.setFlag((byte) 1);
        param.setInspector(user.getId());
        param.setUpdateBy(username);
        param.setCreateBy(username);
        param.setUpdateTime(date);
        param.setCreateTime(date);
        param.setCheckUnit(user.getCompany());
        this.save(param);
 
        //生成专家组记录
        InspectionExpertGroup inspectionExpertGroup = new InspectionExpertGroup();
        inspectionExpertGroup.setInspectionName(param.getInspectionName());
        inspectionExpertGroup.setInspectionTime(param.getInspectionTime());
        inspectionExpertGroup.setCheckedCompanyId(param.getCheckedCompanyId());
        inspectionExpertGroup.setCheckedCompanyName(param.getCheckedCompanyName());
        inspectionExpertGroup.setCheckUnit(user.getCompany());
        inspectionExpertGroup.setSelfInspectionId(param.getId());
        inspectionExpertGroup.setValidFlag(true);
        inspectionExpertGroup.setCreateTime(new Date());
        List<InspectionExpert> collect = param.getExpertList().stream().filter(inspectionExpert -> inspectionExpert.getIsLeader() == true).collect(Collectors.toList());
        if (collect.size() > 0) {
            UserVo userVo = userService.selectUserVoById(collect.get(0).getExpertId());
            inspectionExpertGroup.setExpertLeader(userVo.getRealname());
        }
        expertGroupService.save(inspectionExpertGroup);
 
 
        if (CollectionUtils.isNotEmpty(param.getItemList())) {
            for (SafetySelfInspectionItem safetySelfInspectionItem : param.getItemList()) {
                //默认合格
                safetySelfInspectionItem.setSafetyInspectionItemResult(2);
                safetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                safetySelfInspectionItem.setUpdateBy(username);
                safetySelfInspectionItem.setCreateBy(username);
                safetySelfInspectionItem.setUpdateTime(date);
                safetySelfInspectionItem.setCreateTime(date);
                safetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(safetySelfInspectionItem);
            }
        }
        if(CollectionUtils.isNotEmpty(param.getElementAList())){
            for (SafetyInspectionElementA inspectionElementA : param.getElementAList()) {
                SafetyInspectionElementA safetyInspectionElementA = new SafetyInspectionElementA();
                safetyInspectionElementA.setSafetyInspectionId(param.getId());
                safetyInspectionElementA.setElementA(inspectionElementA.getElementA());
                safetyInspectionElementAService.save(safetyInspectionElementA);
            }
        }
 
        if(CollectionUtils.isNotEmpty(param.getExpertList())){
            for (InspectionExpert inspectionExpert : param.getExpertList()) {
                UserVo userVo = userService.selectUserVoById(inspectionExpert.getExpertId());
                inspectionExpert.setCompanyName(userVo.getCompany());
                inspectionExpert.setIdcard(userVo.getIdcard());
                inspectionExpert.setName(userVo.getRealname());
                inspectionExpert.setPhone(userVo.getUsername());
                inspectionExpert.setType(userVo.getType());
                inspectionExpert.setProfessionalLevel(user.getProfessionalLevel());
                inspectionExpert.setSpecialityName(userVo.getSpecialityName());
                inspectionExpert.setSelfInspectionId(param.getId());
                inspectionExpert.setExpertGropId(inspectionExpertGroup.getId());
                expertService.save(inspectionExpert);
            }
        }
    }
 
    /**
     * @Description: 修改
     */
    @Override
    public void modOne(SafetySelfInspection param, UserInfo user) {
        selectVerification(param.getId());
        requiredVerification(param);
        //更新主表
        Date date = new Date();
        String username = user.getRealname();
        param.setUpdateTime(date);
        param.setUpdateBy(username);
        this.updateById(param);
        //更新检查项表
        //1.新增新的元素
        //2.删除旧的不存在的元素
        List<SafetySelfInspectionItem> oldFileList = safetySelfInspectionItemMapper.getBySafetySelfInspectionId(param.getId());
        List<SafetySelfInspectionItem> newFileList = param.getItemList();
 
        List<Long> oldIdList = new ArrayList<>();
        List<Long> newIdList = new ArrayList<>();
 
        for (SafetySelfInspectionItem oldSafetySelfInspectionItem :oldFileList ){
            oldIdList.add(oldSafetySelfInspectionItem.getId());
        }
        for (SafetySelfInspectionItem newSafetySelfInspectionItem :newFileList ){
            if (newSafetySelfInspectionItem.getId()==null){
                //1.添加新增的元素
                newSafetySelfInspectionItem.setValidFlag(Boolean.TRUE);
                newSafetySelfInspectionItem.setUpdateBy(username);
                newSafetySelfInspectionItem.setCreateBy(username);
                newSafetySelfInspectionItem.setUpdateTime(date);
                newSafetySelfInspectionItem.setCreateTime(date);
                newSafetySelfInspectionItem.setSafetySelfInspectionId(param.getId());
                safetySelfInspectionItemMapper.insert(newSafetySelfInspectionItem);
            }else{
                newIdList.add(newSafetySelfInspectionItem.getId());
            }
        }
        //2.删除不存在的元素
        List<Long> diffList = getDif(oldIdList,newIdList);
        if (CollectionUtils.isNotEmpty(diffList)){
            for (Long id : diffList){
                safetySelfInspectionItemMapper.deleteById(id,username,date);
            }
        }
    }
 
    public  List<Long> getDif(List<Long> oldIdList , List<Long> newIdList ){
        ArrayList<Long> dif = new ArrayList<>();
        //查找出oldIdList表中不包含newIdList的元素
        for (Long id : oldIdList) {
            if (!(newIdList.contains(id))) {
                dif.add(id);
            }
        }
        return dif;
    }
 
    /**
     * @Description: 删除
     */
    @Transactional
    @Override
    public void delOne(Long id, UserInfo user) {
        selectVerification(id);
 
        SafetySelfInspection delOne = new SafetySelfInspection();
        delOne.setId(id);
        delOne.setUpdateTime(new Date());
        delOne.setUpdateBy(user.getRealname());
        delOne.setValidFlag(Boolean.FALSE);
        this.updateById(delOne);
        InspectionExpertGroup expertGroup = expertGroupService.getBySelfInspectionId(id);
        if(expertGroup != null){
            expertGroup.setValidFlag(false);
            expertGroupService.updateById(expertGroup);
        }
 
    }
 
  /*  @Override
    public SafetySelfInspection infoOne(Long id,String unqualified, UserInfo user) {
        SafetySelfInspection safetySelfInspection=selectVerification(id);
        //组装检查项
        List<SafetySelfInspectionItem> itemList= safetySelfInspectionItemMapper.getDetailBySafetySelfInspectionId(id,unqualified);
        if(CollectionUtils.isNotEmpty(itemList)){
            //组装扣分项
            for (SafetySelfInspectionItem safetySelfInspectionItem : itemList){
                if (safetySelfInspectionItem.getSafetyInspectionItemResult()==null){
                    //根据检查项id获取原始扣分项
                    List<SafetyInspectionItemDeduction> safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId());
                    if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){
                        List<SafetySelfInspectionItemDeduction> oldSafetySelfInspectionItemDeductionList=new ArrayList<>();
                        for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){
                            SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction();
                            safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId());
                            safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName());
                            safetySelfInspectionItemDeduction.setPoint(0);
                            oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction);
                        }
                        safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList);
                    }else{
                        safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>());
                    }
                }else if (safetySelfInspectionItem.getSafetyInspectionItemResult().equals(1)){
                    //根据自查清单记录的检查项id获取其扣分项
                    List<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
                    if (CollectionUtils.isNotEmpty(selfDeductionList)){
                        safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
                    }
                }
            }
            safetySelfInspection.setItemList(itemList);
        }else{
            safetySelfInspection.setItemList(new ArrayList<>());
        }
        return safetySelfInspection;
    }*/
 
    @Override
    public SafetySelfInspectionRespDTO infoOne(Long id, String unqualified, UserInfo user) {
        SafetySelfInspection safetySelfInspection = selectVerification(id);
        SafetySelfInspectionRespDTO safetySelfInspectionRespDTO = new SafetySelfInspectionRespDTO();
        BeanUtils.copyProperties(safetySelfInspection,safetySelfInspectionRespDTO);
        //获取所有检查项
        List<SafetySelfInspectionItem> itemList= safetySelfInspectionItemMapper.getDetailBySafetySelfInspectionId(id,unqualified);
        //获取要素树
        List<ElementTree> elementTreeList = elementManagementService.getMenuType1Tree();
        //获取附件
        List<AttachmentInfo> byBusinessId = attachmentInfoService.findByBusinessId(id);
        safetySelfInspectionRespDTO.setAttachmentList(byBusinessId);
 
        List<SafetySelfInspectionElementRespDTO> arespDTOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(elementTreeList)) {
            for (ElementTree elementA : elementTreeList) {
 
                List<SafetySelfInspectionItem> selectElementList = itemList
                        .stream()
                        .filter(item -> item.getElementA() != null &&
                        item.getElementA().equals(elementA.getValue()))
                        .collect(Collectors.toList());
                if (CollectionUtils.isNotEmpty(selectElementList)) {
                    SafetySelfInspectionElementRespDTO elementARespDTO = new SafetySelfInspectionElementRespDTO();
                    elementARespDTO.setElementId(elementA.getValue());
                    elementARespDTO.setElementName(elementA.getLabel());
                    elementARespDTO.setType(elementA.getType());
 
                    List<SafetySelfInspectionElementRespDTO> brespDTOList = new ArrayList<>();
                    //二级要素
                    List<ElementTree> elementBList = elementA.getChildren();
                    if(CollectionUtils.isNotEmpty(elementBList)){
                        for (ElementTree stree : elementBList) {
                            SafetySelfInspectionElementRespDTO elementBRespDTO = new SafetySelfInspectionElementRespDTO();
                            elementBRespDTO.setElementId(stree.getValue());
                            elementBRespDTO.setElementName(stree.getLabel());
                            elementBRespDTO.setType(stree.getType());
                            elementBRespDTO.setPid(stree.getPid());
 
                            List<SafetySelfInspectionElementRespDTO> cRespDTOList = new ArrayList<>();
                            //三级要素
                            List<ElementTree> elementCList = stree.getChildren();
                            if(CollectionUtils.isEmpty(elementCList)){
                                //扣分
                                int delScore = 0;
                                int totalScore = stree.getPoint();
                                //无三级要素过滤出二级要素检查内容
                                List<SafetySelfInspectionItem> sItemList = itemList
                                        .stream()
                                        .filter(item -> item.getElementC() == null
                                                && item.getElementB() != null
                                                && item.getElementB().equals(stree.getValue()))
                                        .collect(Collectors.toList());
                                //填充检查项
                                elementBRespDTO.setItemList(assemblyDeduction(sItemList));
 
                                //判断是否有否决项
                                List<SafetySelfInspectionItem> rejectList = sItemList
                                        .stream()
                                        .filter(item -> item.getSafetyInspectionItemResult().equals(0))
                                        .collect(Collectors.toList());
                                if(rejectList.size() > 0){
                                    delScore = stree.getPoint();
                                }else {
                                    //计算出不合格扣分
                                    delScore += unqualifiedItemDelScore(sItemList);
                                }
                                //总分
                                elementBRespDTO.setTotalScore(stree.getPoint());
                                //得分
                                elementBRespDTO.setScore(totalScore-delScore);
 
 
                            }else {
                                for(ElementTree ttree : elementCList){
                                    SafetySelfInspectionElementRespDTO elementCRespDTO = new SafetySelfInspectionElementRespDTO();
                                    elementCRespDTO.setElementId(ttree.getValue());
                                    elementCRespDTO.setElementName(ttree.getLabel());
                                    elementCRespDTO.setType(ttree.getType());
                                    elementCRespDTO.setPid(ttree.getPid());
 
                                    //扣分
                                    int delScore = 0;
                                    int totalScore = ttree.getPoint();
 
                                    //过滤该三级要素检查项目
                                    List<SafetySelfInspectionItem> tItemList = itemList
                                            .stream()
                                            .filter(item -> item.getElementC() != null && item.getElementC().equals(ttree.getValue()))
                                            .collect(Collectors.toList());
                                    //填充检查项
                                    elementCRespDTO.setItemList(assemblyDeduction(tItemList));
 
                                    //判断是否有被否决
                                    List<SafetySelfInspectionItem> rejectList = tItemList
                                            .stream()
                                            .filter(item -> item.getSafetyInspectionItemResult().equals(0))
                                            .collect(Collectors.toList());
                                    if(rejectList.size() > 0){
                                        delScore = ttree.getPoint();
                                    }else {
                                        //计算出不合格扣分
                                        delScore = unqualifiedItemDelScore(tItemList);
                                    }
                                    //总分
                                    elementCRespDTO.setTotalScore(ttree.getPoint());
                                    //得分
                                    elementCRespDTO.setScore(totalScore-delScore);
                                    cRespDTOList.add(elementCRespDTO);
                                }
                            }
                            elementBRespDTO.setChildren(cRespDTOList);
                            brespDTOList.add(elementBRespDTO);
                        }
                    }
                    elementARespDTO.setChildren(brespDTOList);
                    arespDTOList.add(elementARespDTO);
                }
            }
 
        }
        safetySelfInspectionRespDTO.setElementList(arespDTOList);
 
        return safetySelfInspectionRespDTO;
    }
 
 
    //组装扣分项
    private List<SafetySelfInspectionItem> assemblyDeduction( List<SafetySelfInspectionItem> itemList){
        for (SafetySelfInspectionItem safetySelfInspectionItem : itemList){
            if (safetySelfInspectionItem.getSafetyInspectionItemResult().equals(2)){
                //根据检查项id获取原始扣分项
                List<SafetyInspectionItemDeduction> safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId());
                if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){
                    List<SafetySelfInspectionItemDeduction> oldSafetySelfInspectionItemDeductionList=new ArrayList<>();
                    for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){
                        SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction();
                        safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId());
                        safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName());
                        safetySelfInspectionItemDeduction.setPoint(0);
                        oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction);
                    }
                    safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList);
                }else{
                    safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>());
                }
            }else if (safetySelfInspectionItem.getSafetyInspectionItemResult().equals(1)){
                //根据自查清单记录的检查项id获取其扣分项
                List<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
                if (CollectionUtils.isNotEmpty(selfDeductionList)){
                    safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
                }
            }
        }
        return itemList;
    }
 
    @Override
    public SafetySelfInspectionItem itemInfoOne(Long id, UserInfo user){
        SafetySelfInspectionItem safetySelfInspectionItem = safetySelfInspectionItemMapper.getDetailById(id);
        List<SafetySelfInspectionItemDeduction> selfDeductionList = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(safetySelfInspectionItem.getId());
        List<InspectionHiddenDanger> inspectionHiddenDangerList = dangerService.getBySafetySelfInspectionItemId(id);
        List<AttachmentInfo> byBusinessId = attachmentInfoService.findByBusinessId(id);
 
        if (CollectionUtils.isNotEmpty(selfDeductionList)){
            safetySelfInspectionItem.setSelfDeductionList(selfDeductionList);
        }else{
            //根据自查清单检查项里的检查项表去找原有的扣分项
            List<SafetyInspectionItemDeduction> safetyInspectionItemDeductionList = safetyInspectionItemDeductionMapper.getBySafetyInspectionItemId(safetySelfInspectionItem.getSafetyInspectionItemId());
            if (CollectionUtils.isNotEmpty(safetyInspectionItemDeductionList)){
                List<SafetySelfInspectionItemDeduction> oldSafetySelfInspectionItemDeductionList=new ArrayList<>();
                for (SafetyInspectionItemDeduction safetyInspectionItemDeduction : safetyInspectionItemDeductionList){
                    SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction = new SafetySelfInspectionItemDeduction();
                    safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetyInspectionItemDeduction.getId());
                    safetySelfInspectionItemDeduction.setName(safetyInspectionItemDeduction.getName());
                    safetySelfInspectionItemDeduction.setPoint(0);
                    oldSafetySelfInspectionItemDeductionList.add(safetySelfInspectionItemDeduction);
                }
                safetySelfInspectionItem.setSelfDeductionList(oldSafetySelfInspectionItemDeductionList);
            }else{
                safetySelfInspectionItem.setSelfDeductionList(new ArrayList<>());
            }
        }
        for (InspectionHiddenDanger inspectionHiddenDanger : inspectionHiddenDangerList) {
            if(StringUtils.isNotBlank(inspectionHiddenDanger.getUrl())){
                List<Long> attachmentIds = new ArrayList<>();
                String[] split = inspectionHiddenDanger.getUrl().split(",");
                for (String s : split) {
                    attachmentIds.add(Long.valueOf(s));
                }
 
                List<AttachmentInfo> dangerAttachList = attachmentInfoService.findByIds(attachmentIds);
                inspectionHiddenDanger.setAttachmentList(dangerAttachList);
            }
 
        }
        safetySelfInspectionItem.setAttachmentList(byBusinessId);
        safetySelfInspectionItem.setDangerList(inspectionHiddenDangerList);
        return safetySelfInspectionItem;
    }
 
    @Transactional
    @Override
    public void modItemInfo(SafetySelfInspectionItem param, UserInfo user) {
 
        if (param.getSafetyInspectionItemResult()==0 && StringUtils.isBlank(param.getSafetyInspectionItemResultDesc())){
            throw new BusinessException("否决说明必填!");
        }
        Date date = new Date();
        String username = user.getRealname();
        param.setUpdateTime(date);
        param.setUpdateBy(username);
        safetySelfInspectionItemMapper.updateById(param);
        //附件
        List<AttachmentInfo> attachmentInfoList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(param.getAttachmentList())){
            for (AttachmentInfo attachmentInfo : param.getAttachmentList()) {
                attachmentInfo.setBusinessId(param.getId());
                attachmentInfoList.add(attachmentInfo);
            }
        }
 
        if (param.getSafetyInspectionItemResult()==0 || param.getSafetyInspectionItemResult() == 2){
            //否决 合格--删除扣分记录
            safetySelfInspectionItemDeductionMapper.delBySafetySelfInspectionItemId(param.getId(),username,date);
            //删除隐患
            dangerService.delByInspectionItemId(param.getId(),user);
            //删除附件
            attachmentInfoService.deleteByBusinessId(param.getId());
        }else{
            //扣分
            List<SafetySelfInspectionItemDeduction> list = safetySelfInspectionItemDeductionMapper.getBySafetySelfInspectionItemId(param.getId());
            if (CollectionUtils.isEmpty(list)){
                //新增扣分记录
                for (SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction : param.getSelfDeductionList()){
                    safetySelfInspectionItemDeduction.setValidFlag(Boolean.TRUE);
                    safetySelfInspectionItemDeduction.setUpdateBy(username);
                    safetySelfInspectionItemDeduction.setCreateBy(username);
                    safetySelfInspectionItemDeduction.setUpdateTime(date);
                    safetySelfInspectionItemDeduction.setCreateTime(date);
                    safetySelfInspectionItemDeduction.setSafetySelfInspectionItemId(param.getId());
                    safetySelfInspectionItemDeduction.setSafetyInspectionItemDeductionId(safetySelfInspectionItemDeduction.getSafetyInspectionItemDeductionId());
                    safetySelfInspectionItemDeductionMapper.insert(safetySelfInspectionItemDeduction);
                }
            }else{
                //更新扣分记录
                for (SafetySelfInspectionItemDeduction safetySelfInspectionItemDeduction : param.getSelfDeductionList()){
                    safetySelfInspectionItemDeduction.setUpdateBy(username);
                    safetySelfInspectionItemDeduction.setUpdateTime(date);
                    safetySelfInspectionItemDeductionMapper.updateById(safetySelfInspectionItemDeduction);
                }
            }
            //隐患
            if(CollectionUtils.isNotEmpty(param.getDangerList())){
                List<InspectionHiddenDanger> oldDangerList = dangerService.getBySafetySelfInspectionItemId(param.getId());
                //过滤出要删除数据
                List<InspectionHiddenDanger> deleteOldDangerList = oldDangerList.stream().parallel().filter(a ->
                                param.getDangerList().stream().noneMatch(b ->
                                        a.getId().equals(b.getId())))
                        .collect(Collectors.toList());
                //删除
                if (CollectionUtils.isNotEmpty(deleteOldDangerList)) {
                    List<Long> idList = deleteOldDangerList.stream().map(InspectionHiddenDanger::getId).collect(Collectors.toList());
                    dangerService.delByIds(idList);
                }
                for (InspectionHiddenDanger inspectionHiddenDanger : param.getDangerList()) {
                    StringBuffer stringBuffer = new StringBuffer();
                    //附件
                    if(CollectionUtils.isNotEmpty(inspectionHiddenDanger.getAttachmentList())){
                        for (AttachmentInfo attachmentInfo : inspectionHiddenDanger.getAttachmentList()) {
                            stringBuffer.append(attachmentInfo.getId().toString()).append(",");
                        }
                        stringBuffer = stringBuffer.deleteCharAt(stringBuffer.length()-1);
                    }
 
                    if(inspectionHiddenDanger.getId() == null){
                        inspectionHiddenDanger.setCreateBy(user.getRealname());
                        inspectionHiddenDanger.setCreateTime(new Date());
                        inspectionHiddenDanger.setUpdateBy(user.getRealname());
                        inspectionHiddenDanger.setUpdateTime(new Date());
                        inspectionHiddenDanger.setStatus((byte)-1);
                        inspectionHiddenDanger.setValidFlag(true);
                        inspectionHiddenDanger.setSelfInspectionId(param.getSafetySelfInspectionId());
                        inspectionHiddenDanger.setSelfInspectionItemId(param.getId());
                        inspectionHiddenDanger.setUrl(stringBuffer.toString());
                        dangerService.save(inspectionHiddenDanger);
                        //附件
                        if(StringUtils.isNotBlank(inspectionHiddenDanger.getUrl())){
                            for (AttachmentInfo attachmentInfo : inspectionHiddenDanger.getAttachmentList()) {
                                attachmentInfo.setBusinessId(inspectionHiddenDanger.getId());
                                attachmentInfoList.add(attachmentInfo);
                            }
                        }
                    }else {
                        inspectionHiddenDanger.setUpdateBy(user.getRealname());
                        inspectionHiddenDanger.setUpdateTime(new Date());
                        inspectionHiddenDanger.setUrl(stringBuffer.toString());
                        dangerService.updateById(inspectionHiddenDanger);
                        //附件
                        if(CollectionUtils.isNotEmpty(inspectionHiddenDanger.getAttachmentList())){
                            for (AttachmentInfo attachmentInfo : inspectionHiddenDanger.getAttachmentList()) {
                                attachmentInfo.setBusinessId(inspectionHiddenDanger.getId());
                                attachmentInfoList.add(attachmentInfo);
                            }
                        }
                    }
                }
            }
 
        }
        //附件信息更新
        if(CollectionUtils.isNotEmpty(attachmentInfoList)){
            attachmentInfoService.updateBusinessIdBatch(attachmentInfoList);
 
        }
    }
 
 
    @Override
    public void finish(Long id, UserInfo user) {
        SafetySelfInspection safetySelfInspection=selectVerification(id);
        Date date = new Date();
        String username = user.getRealname();
        safetySelfInspection.setStatus(2);
        safetySelfInspection.setUpdateTime(date);
        safetySelfInspection.setUpdateBy(username);
        safetySelfInspection.setInspectionEndTime(new Date());
        safetySelfInspectionMapper.updateById(safetySelfInspection);
    }
 
    @Override
    public void addItemExpert(List<SafetySelfInspectionItem> itemList, UserInfo user) {
        itemList.forEach(item -> {
            item.setUpdateBy(user.getRealname());
            item.setUpdateTime(new Date());
        });
        safetySelfInspectionItemMapper.updateBatch(itemList);
    }
 
    @Override
    public void addCheckAttachment(List<AttachmentInfo> attachmentList, UserInfo user) {
        requiredAttachmentVerification(attachmentList);
        for (AttachmentInfo attachmentInfo : attachmentList) {
            attachmentInfo.setUpdateTime(new Date());
            attachmentInfo.setUpdateUid(user.getId());
            attachmentInfo.setUpdateUname(user.getRealname());
        }
        attachmentInfoService.updateBusinessIdBatch(attachmentList);
    }
 
    private void requiredAttachmentVerification(List<AttachmentInfo> attachmentList) {
        if(CollectionUtils.isEmpty(attachmentList)){
            throw new BusinessException("附件信息为空");
        }
        for (AttachmentInfo attachmentInfo : attachmentList) {
            if(attachmentInfo.getId() == null){
                throw new BusinessException("附件主键信息不能为空");
            }
            if(attachmentInfo.getBusinessId() == null){
                throw new BusinessException("附件信息关联业务id不能为空");
            }
        }
 
    }
 
    /**
     * 查询验证
     * 验证对象存在
     */
    public SafetySelfInspection selectVerification(Long id) {
        if (id == null) throw new BusinessException("id传参不能为空");
        SafetySelfInspection SafetySelfInspection = this.getById(id);
        if (SafetySelfInspection == null) throw new BusinessException("找不到对应实体");
        return SafetySelfInspection;
    }
 
    /**
     * 操作验证
     * 验证必填项
     */
    public void requiredVerification(SafetySelfInspection param) {
        if (StringUtils.isBlank(param.getInspectionName())) throw new BusinessException("请填写自查清单名称");
        if (param.getInspectionTime() == null) throw new BusinessException("请选择自查时间");
        if (param.getInspector() == null) throw new BusinessException("请选择自查人员");
        if (CollectionUtils.isEmpty(param.getItemList())) {
            throw new BusinessException("请选择检查项");
        }
        if (param.getCheckedCompanyId() == null){
            throw new BusinessException("请选择被检查公司");
        }
        if(CollectionUtils.isEmpty(param.getElementAList())){
            throw new BusinessException("请选择A检查要素");
        }
        for(SafetyInspectionElementA elementA : param.getElementAList()){
            if(elementA.getElementA() == null){
                throw new BusinessException("请选择A检查要素");
            }
        }
    }
    public void requiredSuperviseVerification(SafetySelfInspection param) {
        if (StringUtils.isBlank(param.getInspectionName())) throw new BusinessException("请填写排查清单名称");
        if (param.getInspectionTime() == null) throw new BusinessException("请选择排查时间");
        if (CollectionUtils.isEmpty(param.getExpertList())) throw new BusinessException("请选择检查专家");
        for(InspectionExpert inspectionExpert : param.getExpertList()){
            if(inspectionExpert.getExpertId() == null){
                throw new BusinessException("请选择检查专家");
            }
            if(StringUtils.isBlank(inspectionExpert.getName())){
                throw new BusinessException("请选择检查专家名字");
            }
            if(inspectionExpert.getIsLeader() == null){
                throw new BusinessException("请选择组长或者组员");
            }
        }
        if (CollectionUtils.isEmpty(param.getItemList())) {
            throw new BusinessException("请选择检查项");
        }
        if (param.getCheckedCompanyId() == null){
            throw new BusinessException("请选择被检查公司");
        }
    }
 
 
}