jinpengyong
2023-11-01 74c430e012ade8363b57949357e3cf653b944a2d
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
package com.moral.api.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.config.Interceptor.UserHelper;
import com.moral.api.entity.*;
import com.moral.api.exception.BusinessException;
import com.moral.api.mapper.AllocationMapper;
import com.moral.api.mapper.ResponsibilityUnitMapper;
import com.moral.api.mapper.SysDictDataMapper;
import com.moral.api.mapper.SysDictTypeMapper;
import com.moral.api.mapper.UserMapper;
import com.moral.api.pojo.bean.BaseInvalidEntity;
import com.moral.api.pojo.dto.allocation.AllocationUnitViewDto;
import com.moral.api.pojo.enums.*;
import com.moral.api.pojo.ext.allocation.AllocationExt;
import com.moral.api.pojo.ext.allocation.AllocationListExt;
import com.moral.api.pojo.ext.allocation.AllocationPageExt;
import com.moral.api.pojo.query.allocation.*;
import com.moral.api.pojo.query.allocationextension.AllocationExtensionAddCond;
import com.moral.api.pojo.query.app.AppAllocationFileCond;
import com.moral.api.pojo.query.app.AppAllocationPageCond;
import com.moral.api.pojo.vo.allocation.AllocationFindVo;
import com.moral.api.pojo.vo.app.AppAllocationFindVo;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.*;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
 
@Service
@Slf4j
public class AllocationServiceImpl extends ServiceImpl<AllocationMapper, Allocation> implements AllocationService {
 
    private final Integer NEW_BUILT = 10;
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private ResponsibilityUnitMapper responsibilityUnitMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private AllocationMapper allocationMapper;
    @Autowired
    private  SysAreaService sysAreaService;
    @Autowired
    private ApproveTableService approveTableService;
    @Autowired
    private FileTableService fileTableService;
    @Autowired
    private AllocationExtensionService allocationExtensionService;
    @Autowired
    private ResponsibilityUnitService responsibilityUnitService;
    @Autowired
    private SysDictTypeService sysDictTypeService;
 
    /**
     * 根据字典类型获取字典数据
     * @param dictType
     * @return
     */
    @Override
    public List<Map<String, Object>> sysDictData(String dictType) {
        QueryWrapper<SysDictType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.select("id").eq("name", dictType);
        SysDictType sysDictType = sysDictTypeMapper.selectOne(typeQueryWrapper);
        QueryWrapper<SysDictData> dataQueryWrapper = new QueryWrapper<>();
        dataQueryWrapper.select("dataKey", "dataValue").eq("dict_type_id", sysDictType.getId()).eq("is_delete", Constants.NOT_DELETE);
        return sysDictDataMapper.selectMaps(dataQueryWrapper);
    }
 
    /**
     * 查询责任主体
     * @return
     */
    @Override
    public List<ResponsibilityUnit> seleteUnit() {
        QueryWrapper<ResponsibilityUnit> wrapper = new QueryWrapper<>();
        wrapper.select("unit_name","unit_id");
        wrapper.eq("is_del",Constants.NOT_DELETE);
        wrapper.eq("state",0);
        wrapper.eq("is_invalid",0);
 
        //获取用户信息
        List<Integer> unitList = unitResult();
        if (CollectionUtils.isNotEmpty(unitList)){
            wrapper.in("unit_id",unitList);
        }
        List<ResponsibilityUnit> responsibilityUnits = responsibilityUnitMapper.selectList(wrapper);
        return responsibilityUnits;
    }
 
 
    /**
     * 添加交办单
     * @param allocationCond
     * @return
     */
    @Override
    @Transactional
    public Integer insertAllocation(AllocationAddCond allocationCond) {
        Allocation allocation = allocationCond.convert();
        allocation.setEscalationType(AllocationEscalationTypeEnum.SELF.getValue());
        String dateString = DateUtils.dateToDateString(new Date(), DateUtils.yyyyMMdd_EN);
        Object o = redisTemplate.opsForValue().get(RedisConstants.JBD_DATA);
        int i = ObjectUtils.isEmpty(o)?1:Integer.parseInt(o.toString()) + 1;
        //单号
        String allocationNum = "JBD-" + dateString + String.format("%04d", i);
        allocation.setAllocationNum(allocationNum);
        if(Objects.nonNull(allocationCond.getEscalationUnitId())&& escalationType(allocationCond.getEscalationUnitId())) {
            allocation.setEscalationType(AllocationEscalationTypeEnum.CHECK.getValue());
        }
        if(NEW_BUILT.equals(allocationCond.getState())) {
            allocation.setState(AllocationApproveEnum.UNDER_RECTIFICATION.value);
        }
        //获取新建图片
        allocationMapper.insert(allocation);
        redisTemplate.opsForValue().set(RedisConstants.JBD_DATA,i);
        if(NEW_BUILT.equals(allocationCond.getState())) {
            //添加流程数据
            ApproveTable approveTable = new ApproveTable();
            approveTable.setRelationId(allocation.getAllocationId());
            approveTable.setState(NEW_BUILT);
            approveTable.setApproveModule(FileTableEnum.ALLOCATION_FOUNDATION.value);
            approveTable.setStateName("新建");
            approveTableService.saveResult(approveTable);
        }
        fileTableService.upDateResult(allocationCond.getFileBaseList(),allocation.getAllocationId(), FileTableEnum.ALLOCATION_FOUNDATION.value);
        return allocation.getAllocationId();
    }
 
 
    /**
     * 修改交办单
     * @param allocationUpdateCond
     */
    @Override
    @Transactional
    public void updateAll(AllocationUpdateCond allocationUpdateCond) {
        AllocationExt allocationExt = oneAllocation(allocationUpdateCond.getAllocationId());
        if(!AllocationApproveEnum.DRAFT.value.equals(allocationExt.getState())) {
            throw new BusinessException("新建未提交状态,才能编辑!");
        }
        //获取用户信息
        Allocation allocation = allocationUpdateCond.convert();
        if(Objects.nonNull(allocationUpdateCond.getEscalationUnitId())&& escalationType(allocationUpdateCond.getEscalationUnitId())) {
            allocation.setEscalationType(AllocationEscalationTypeEnum.CHECK.getValue());
        }
        if(NEW_BUILT.equals(allocationUpdateCond.getState())) {
            //添加流程数据
            ApproveTable approveTable = new ApproveTable();
            approveTable.setRelationId(allocation.getAllocationId());
            approveTable.setState(NEW_BUILT);
            approveTable.setApproveModule(FileTableEnum.ALLOCATION_FOUNDATION.value);
            approveTable.setStateName("新建");
            approveTableService.saveResult(approveTable);
        }
        if(NEW_BUILT.equals(allocationUpdateCond.getState())) {
            allocation.setState(AllocationApproveEnum.UNDER_RECTIFICATION.value);
        }
        fileTableService.upDateResult(allocationUpdateCond.getFileBaseList(),allocation.getAllocationId(), FileTableEnum.ALLOCATION_FOUNDATION.value);
        allocationMapper.updateById(allocation);
    }
 
 
 
 
 
    /**
     * 表单总览
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> selectUnitView(Map<String, Object> map) {
        QueryWrapper<Allocation> wrapper = new QueryWrapper<>();
        //获取用户信息
        List<Integer> list = this.unitResult();
        if (!ObjectUtils.isEmpty(map.get("unitId"))){
            wrapper.in("unit_id",map.get("unitId").toString());
        }else {
            if (!ObjectUtils.isEmpty(list)){
                Integer integer = list.get(0);
                if (integer==0){
                    return null;
                }else {
                    wrapper.in("unit_id",list);
                }
            }
        }
 
//        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
//        Object unitId = userInfo.get("unitId");
//        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
//        Integer orgId = (Integer) orgInfo.get("id");
//        if (!ObjectUtils.isEmpty(map.get("unitId"))){
//            wrapper.in("unit_id",map.get("unitId").toString());
//        }else {
//            if (orgId!=24){
//                if (ObjectUtils.isEmpty(unitId)){
//                    return null;
//                }
//                ResponsibilityUnit responsibilityUnit1 = responsibilityUnitMapper.selectById(Integer.parseInt(unitId.toString()));
//                Integer areaCode = responsibilityUnit1.getAreaCode();
//                List<ResponsibilityUnit> responsibilityUnits = responsibilityUnitService.selectUnit(areaCode);
//                if (ObjectUtils.isEmpty(responsibilityUnits)){
//                    List<ResponsibilityUnit> responsibilityUnits1 = responsibilityUnitService.selectAreaUnit(areaCode);
//                    Map<Integer, List<ResponsibilityUnit>> collect = responsibilityUnits1.stream().collect(Collectors.groupingBy(o -> o.getUnitId()));
//                    List<Integer> collect1 = collect.keySet().stream().collect(Collectors.toList());
//                    wrapper.in("unit_id",collect1);
//                }else {
//                    Map<Integer, List<ResponsibilityUnit>> collect = responsibilityUnits.stream().collect(Collectors.groupingBy(o -> o.getUnitId()));
//                    List<Integer> collect1 = collect.keySet().stream().collect(Collectors.toList());
//                    wrapper.in("unit_id",collect1);
//                }
//            }
//        }
 
        HashMap<String, Object> rsMap = new HashMap<>();
        ArrayList<Map<String, Object>> polluteArrayList = new ArrayList<>();
        ArrayList<Map<String, Object>> escalationTypeList = new ArrayList<>();
        ArrayList<AllocationUnitViewDto> allocationUnitViewDtos = new ArrayList<>();
 
        Object number1 = map.get("number");
        String startTime=null;
        String endTime=null;
        if (!ObjectUtils.isEmpty(map.get("startTime")) || !ObjectUtils.isEmpty(map.get("endTime"))){
             startTime = map.get("startTime").toString();
             endTime = map.get("endTime").toString();
 
        }
        if (!ObjectUtils.isEmpty(number1)){
            String s = "-"+number1;
            //获取当前时间
            endTime = DateUtils.getCurDateTime();
            startTime = DateUtils.getDateStringOfMon(Integer.parseInt(s), DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
        }
 
        wrapper.between("escalation_time",startTime,endTime);
        wrapper.eq("is_del",0);
        wrapper.eq("is_invalid",0);
        List<Allocation> allocations = allocationMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(allocations)){
            return null;
        }
         int complete=0;
         int unComplete=0;
        for (Allocation allocation : allocations) {
            String state = allocation.getState()+"";
            if (state.equals("40")||state.equals("50")){
                 complete++;
            }else  {
                 unComplete++;
            }
        }
        //根据上报类型分类
        Map<Integer, List<Allocation>> escalationTypeMap = allocations.stream().collect(Collectors.groupingBy(o -> o.getEscalationType()));
        Set<Integer> escalationTypes = escalationTypeMap.keySet();
        for (Integer escalationType : escalationTypes) {
            List<Allocation> allocations1 = escalationTypeMap.get(escalationType);
            String value = AllocationEscalationTypeEnum.SELF.value.toString();
            HashMap<String, Object> typeMap = new HashMap<>();
            if (value.equals(escalationType+"")){
                typeMap.put("name",AllocationEscalationTypeEnum.SELF.name);
            }else {
                typeMap.put("name",AllocationEscalationTypeEnum.CHECK.name);
            }
            typeMap.put("value",allocations1.size());
            escalationTypeList.add(typeMap);
        }
        //根据污染类型分类
        SysDictType sysDictType = sysDictTypeService.slectFind(Constants.WU_RAN_LEI_XING);
        Map<Integer, List<Allocation>> polluteTypeMap = allocations.stream().collect(Collectors.groupingBy(o -> o.getPolluteType()));
        Set<Integer> polluteTypes = polluteTypeMap.keySet();
        for (Integer polluteType : polluteTypes) {
            HashMap<String, Object> typeMap = new HashMap<>();
            List<Allocation> allocations1 = polluteTypeMap.get(polluteType);
            QueryWrapper<SysDictData> sysDictDataQueryWrapper = new QueryWrapper<>();
            sysDictDataQueryWrapper.eq("dict_type_id",sysDictType.getId());
            sysDictDataQueryWrapper.eq("dataKey",polluteType);
            SysDictData sysDictData = sysDictDataMapper.selectOne(sysDictDataQueryWrapper);
            typeMap.put("name",sysDictData.getDataValue());
            typeMap.put("value",allocations1.size());
            polluteArrayList.add(typeMap);
        }
        //根据责任单位分类
        Map<Integer, List<Allocation>> unitMap = allocations.stream().collect(Collectors.groupingBy(o -> o.getUnitId()));
        Set<Integer> unitList = unitMap.keySet();
        //逾期次数
        int overdue=0;
        for (Integer integer : unitList) {
            //获取城市
            ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(integer);
            SysArea areaByCode = sysAreaService.getAreaByCode(responsibilityUnit.getAreaCode());
            AllocationUnitViewDto allocationUnitViewDto = new AllocationUnitViewDto();
            List<Allocation> allocations1 = unitMap.get(integer);
            //加分
            int marks=0;
            //减分
            int deduction=0;
            int number=0;
            for (Allocation allocation : allocations1) {
                String state = allocation.getState()+"";
                Integer changeType = allocation.getChangeType();
                String value = ChangeEnum.DEADLINE.value.toString();
                if (value.equals(changeType+"")){
                    Date dateOfDay = DateUtils.getDateOfDay(allocation.getEscalationTime(), allocation.getChangeDay());
                    Date date = new Date();
                    boolean timeBefor = DateUtils.isTimeBefor(date, dateOfDay);
                    if (timeBefor && !state.equals("40") && !state.equals("50") && !state.equals("9")){
                        //逾期次数
                        overdue++;
                    }
                }
                if (state.equals("40")||state.equals("50")){
                    if (allocation.getCheckScore()>0){
                        marks = marks + allocation.getCheckScore();
                    }else {
                        deduction = deduction + allocation.getCheckScore();
                    }
                    number++;
                }
            }
            int total = allocations1.size();
            allocationUnitViewDto.setDeduction(deduction);
            allocationUnitViewDto.setMarks(marks);
            allocationUnitViewDto.setTotal(total);
            allocationUnitViewDto.setTotalPoints(marks+deduction);
            allocationUnitViewDto.setNumber(number);
            String rate = (number * 100 /total)+"%";
            allocationUnitViewDto.setRate(rate);
            allocationUnitViewDto.setUnNumber(total-number);
            allocationUnitViewDto.setUnitName(areaByCode.getAreaName());
            allocationUnitViewDtos.add(allocationUnitViewDto);
        }
        rsMap.put("unitView",allocationUnitViewDtos);
        //总数
        rsMap.put("total",allocations.size());
        //已完成
        rsMap.put("complete",complete);
        //进行中
        rsMap.put("unComplete",unComplete);
        //预期
        rsMap.put("overdue",overdue);
        //污染类型
        rsMap.put("polluteType",polluteArrayList);
        //上报类型
        rsMap.put("escalationType",escalationTypeList);
        return rsMap;
    }
 
 
    /**
     * 导出
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> unitExel(Map<String, Object> map) {
        QueryWrapper<Allocation> wrapper = new QueryWrapper<>();
        //获取用户信息
        List<Integer> list = this.unitResult();
        if (!ObjectUtils.isEmpty(map.get("unitId"))){
            wrapper.in("unit_id",map.get("unitId").toString());
        }else {
            if (!ObjectUtils.isEmpty(list)){
                Integer integer = list.get(0);
                if (integer==0){
                    return null;
                }else {
                    wrapper.in("unit_id",list);
                }
            }
        }
//        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
//        Object unitId = userInfo.get("unitId");
//        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
//        Integer orgId = (Integer) orgInfo.get("id");
//
//        if (ObjectUtils.isEmpty(unitId)){
//            return null;
//        }
//        if (orgId!=24){
//            ResponsibilityUnit responsibilityUnit1 = responsibilityUnitMapper.selectById(Integer.parseInt(unitId.toString()));
//            Integer areaCode = responsibilityUnit1.getAreaCode();
//            List<ResponsibilityUnit> responsibilityUnits = responsibilityUnitService.selectUnit(areaCode);
//            if (ObjectUtils.isEmpty(responsibilityUnits)){
//                List<ResponsibilityUnit> responsibilityUnits1 = responsibilityUnitService.selectAreaUnit(areaCode);
//                Map<Integer, List<ResponsibilityUnit>> collect = responsibilityUnits1.stream().collect(Collectors.groupingBy(o -> o.getUnitId()));
//                List<Integer> collect1 = collect.keySet().stream().collect(Collectors.toList());
//                wrapper.in("unit_id",collect1);
//            }else {
//                Map<Integer, List<ResponsibilityUnit>> collect = responsibilityUnits.stream().collect(Collectors.groupingBy(o -> o.getUnitId()));
//                List<Integer> collect1 = collect.keySet().stream().collect(Collectors.toList());
//                wrapper.in("unit_id",collect1);
//            }
//        }
 
        ArrayList<Map<String, Object>> rsMap = new ArrayList<>();
        Object number1 = map.get("number");
        String startTime=null;
        String endTime=null;
        if (!ObjectUtils.isEmpty(map.get("startTime")) || !ObjectUtils.isEmpty(map.get("endTime"))){
            startTime = map.get("startTime").toString();
            endTime = map.get("endTime").toString();
 
        }
        if (!ObjectUtils.isEmpty(number1)){
            String s = "-"+number1;
            //获取当前时间
            endTime = DateUtils.getCurDateTime();
            startTime = DateUtils.getDateStringOfMon(Integer.parseInt(s), DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
 
        }
        wrapper.between("escalation_time",startTime,endTime);
        wrapper.eq("is_del",0);
        wrapper.eq("is_invalid",0);
        List<Allocation> allocations = allocationMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(allocations)){
            return null;
        }
        //根据责任单位分类
        Map<Integer, List<Allocation>> unitMap = allocations.stream().collect(Collectors.groupingBy(o -> o.getUnitId()));
        Set<Integer> unitList = unitMap.keySet();
        for (Integer integer : unitList) {
//            HashMap<String, Object> map1 = new HashMap<>();
            LinkedHashMap<String, Object> map1 = new LinkedHashMap<>();
            //获取城市
            ResponsibilityUnit responsibilityUnit = responsibilityUnitMapper.selectById(integer);
            SysArea areaByCode = sysAreaService.getAreaByCode(responsibilityUnit.getAreaCode());
            List<Allocation> allocations1 = unitMap.get(integer);
            //加分
            int marks=0;
            //减分
            int deduction=0;
            int number=0;
            for (Allocation allocation : allocations1) {
                String state = allocation.getState()+"";
                if (state.equals("40")||state.equals("50")){
                    if (allocation.getCheckScore()>0){
                        marks = marks + allocation.getCheckScore();
                    }else {
                        deduction = deduction + allocation.getCheckScore();
                    }
                    number++;
                }
            }
            int total = allocations1.size();
            String rate = (number * 100 /total)+"%";
 
            map1.put("责任单位",areaByCode.getAreaName());
            map1.put("总工单",total);
            map1.put("完成数",number);
            map1.put("未完成数",total-number);
            map1.put("完成率",rate);
            map1.put("扣分",deduction);
            map1.put("加分",marks);
            map1.put("总得分",marks+deduction);
            rsMap.add(map1);
        }
        return rsMap;
    }
 
    @Override
    public Page<AllocationPageExt> extPage(AllocationPageCond allocationPageCond) {
        List<Integer> unitList = unitResult();
        Integer codeId =  unitAreaCode();
        allocationPageCond.setUnitList(unitList);
        Page<AllocationPageExt> page =  this.baseMapper.extPage(allocationPageCond.getPage().convertPage(), allocationPageCond);
        if(CollectionUtils.isNotEmpty(page.getRecords())){
            page.getRecords().forEach(it->{
                if((CollectionUtils.isEmpty(unitList)&&codeId.equals(1))||codeId.equals(2)){
                    it.setIsApprove(1);
                }else {
                    it.setIsApprove(0);
                }
            });
        }
        return page;
    }
 
    @Override
    public AllocationExt extOne(Integer id) {
        AllocationExt allocationExt = oneAllocation(id);
        allocationExt.setFileBaseList(fileTableService.list(id,FileTableEnum.ALLOCATION_FOUNDATION.value));
        allocationExt.setFileChangeList(fileTableService.list(id,FileTableEnum.ALLOCATION_RECTIFICATION.value));
        allocationExt.setFileApproveList(fileTableService.list(id,FileTableEnum.ALLOCATION_APPROVE.value));
        allocationExt.setApproveList(approveTableService.listAll(id, FileTableEnum.ALLOCATION_FOUNDATION.value));
        return allocationExt;
    }
 
    @Override
    public AllocationExt oneAllocation(Integer id){
        return this.baseMapper.extOne(id);
    }
 
    @Override
    @Transactional
    public void changeAllocation(AllocationChangeCond changeCond) {
        AllocationExt allocationExt = oneAllocation(changeCond.getAllocationId());
        if(!AllocationApproveEnum.UNDER_RECTIFICATION.value.equals(allocationExt.getState())) {
            throw new BusinessException("整改状态,才能提交审批!");
        }
        Allocation allocation = new Allocation();
        allocation.setAllocationId(changeCond.getAllocationId());
        allocation.setIsChange(changeCond.getIsChange());
        allocation.setChangeName(changeCond.getChangeName());
        allocation.setChangeDescribe(changeCond.getChangeDescribe());
        allocation.setState(changeCond.getState());
        allocation.setChangeTime(new Date());
        this.updateById(allocation);
        //添加流程数据
        ApproveTable approveTable = new ApproveTable();
        approveTable.setRelationId(allocation.getAllocationId());
        approveTable.setState(AllocationApproveEnum.UNDER_RECTIFICATION.value);
        approveTable.setStateName(AllocationApproveEnum.UNDER_RECTIFICATION.name);
        approveTable.setApproveModule(FileTableEnum.ALLOCATION_FOUNDATION.value);
        approveTableService.saveResult(approveTable);
        fileTableService.upDateResult(changeCond.getFileChangeList(),allocation.getAllocationId(), FileTableEnum.ALLOCATION_RECTIFICATION.value);
    }
 
    @Override
    @Transactional
    public void checkAllocation(AllocationCheckCond checkCond) {
        AllocationExt allocationExt = oneAllocation(checkCond.getAllocationId());
        if(!AllocationApproveEnum.IN_APPROVAL.value.equals(allocationExt.getState())) {
            throw new BusinessException("审批状态,才能进行审批操作!");
        }
        QxUser qxUser = UserHelper.getCurrentUser();
        Allocation allocation = new Allocation();
        allocation.setCheckScore(checkCond.getCheckScore());
        allocation.setAllocationId(checkCond.getAllocationId());
        allocation.setCheckDescribe(checkCond.getCheckDescribe());
        allocation.setCheckName(qxUser.getUserName());
        allocation.setCheckTime(new Date());
        allocation.setState(checkCond.getState());
        this.updateById(allocation);
        //添加流程数据
        ApproveTable approveTable = new ApproveTable();
        approveTable.setRelationId(allocation.getAllocationId());
        if(AllocationApproveEnum.PASS.value.equals(checkCond.getState())){
            approveTable.setState(AllocationApproveEnum.PASS.value);
            approveTable.setStateName(AllocationApproveEnum.PASS.name);
        }else {
            approveTable.setState(AllocationApproveEnum.REFUSE.value);
            approveTable.setStateName(AllocationApproveEnum.REFUSE.name);
        }
        approveTable.setApproveModule(FileTableEnum.ALLOCATION_FOUNDATION.value);
        approveTableService.saveResult(approveTable);
        fileTableService.upDateResult(checkCond.getFileApproveList(),checkCond.getAllocationId(), FileTableEnum.ALLOCATION_APPROVE.value);
    }
 
    @Override
    @Transactional
    public boolean removeById(Integer id) {
        AllocationExt allocationExt = oneAllocation(id);
        if(!AllocationApproveEnum.DRAFT.value.equals(allocationExt.getState())) {
            throw new BusinessException("新建草稿状态,才能删除!");
        }
        this.baseMapper.deleteById(id);
        return true;
    }
 
    @Override
    @Transactional
    public boolean invalidResult(Integer id,String invalidReason) {
        AllocationExt allocationExt = oneAllocation(id);
        if( AllocationApproveEnum.IN_APPROVAL.value >= allocationExt.getState() ) {
            throw new BusinessException("审批结束才能作废!");
        }
        LambdaUpdateChainWrapper<Allocation> wrapper =lambdaUpdate();
        wrapper.eq(Allocation::getAllocationId,id).set(Allocation::getInvalidReason,invalidReason).set(BaseInvalidEntity::getIsInvalid, YesOrNo.YES.value);
        return wrapper.update();
    }
 
    @Override
    public List<AllocationListExt> extList(AllocationListCond allocationListCond) {
        return null;
    }
 
    @Override
    @Transactional
    public boolean applyFor(AllocationExtensionAddCond allocationExtensionAddCond) {
        AllocationExt allocationExt = oneAllocation(allocationExtensionAddCond.getAllocationId());
        boolean type = false;
        if( !AllocationApproveEnum.UNDER_RECTIFICATION.value.equals(allocationExt.getState())) {
            type = true;
        }
        if(!ChangeEnum.DEADLINE.value.equals(allocationExt.getChangeType())){
            type = true;
        }
        if(type){
            throw new BusinessException("只有限期整改类型,状态为整改中,才能发起延期申请!");
        }
        AllocationExtension allocationExtension = allocationExtensionAddCond.convert();
        allocationExtension.setState(AllocationExtensionApproveEnum.APPLYING.value);
        allocationExtension.setExtensionOldNum(allocationExt.getChangeDay());
        allocationExtensionService.save(allocationExtension);
        fileTableService.upDateResult(allocationExtensionAddCond.getFileList(),allocationExtension.getId(), FileTableEnum.ALLOCATION_EXTENSION.value);
        return true;
    }
 
    @Override
    public List<AllocationFindVo> selectSmallRoutine(Integer state,String startTime,String endTime) {
        ArrayList<AllocationFindVo> allocationFindVos = new ArrayList<>();
        LambdaQueryWrapper<Allocation> wrapper = new LambdaQueryWrapper<>();
        //获取用户信息
        List<Integer> list = unitResult();
        if (!ObjectUtils.isEmpty(list)){
            wrapper.in(Allocation::getUnitId,list);
        }
        Integer integer = unitAreaCode();
        // 管理员
        if (integer==1){
            if (state==3){
                wrapper.in(Allocation::getState,AllocationApproveEnum.PASS.value,AllocationApproveEnum.REFUSE.value);
            }else if (state==1){
                wrapper.in(Allocation::getState,AllocationApproveEnum.UNDER_RECTIFICATION.value,AllocationApproveEnum.DRAFT.value,AllocationApproveEnum.IN_APPROVAL.value);
            }else {
                wrapper.eq(Allocation::getState,0);
            }
        }else if(integer == 0) {
            if (state==3){
                wrapper.in(Allocation::getState,AllocationApproveEnum.PASS.value,AllocationApproveEnum.REFUSE.value);
            }else if (state==2){
                wrapper.eq(Allocation::getState,AllocationApproveEnum.IN_APPROVAL.value);
            }else if (state==1){
                wrapper.in(Allocation::getState,AllocationApproveEnum.UNDER_RECTIFICATION.value,AllocationApproveEnum.DRAFT.value);
            }
        }else if(integer == 2){
            if (state==3){
                wrapper.in(Allocation::getState,AllocationApproveEnum.PASS.value,AllocationApproveEnum.REFUSE.value);
            }else if (state==2){
                wrapper.in(Allocation::getState,AllocationApproveEnum.UNDER_RECTIFICATION.value,AllocationApproveEnum.DRAFT.value);
            }else if (state==1){
                wrapper.eq(Allocation::getState,AllocationApproveEnum.IN_APPROVAL.value);
            }
        }
        if (!ObjectUtils.isEmpty(startTime) && !ObjectUtils.isEmpty(endTime)){
            wrapper.between(Allocation::getEscalationTime,startTime,endTime);
        }
        wrapper.eq(Allocation::getIsDel,0).eq(Allocation::getIsInvalid,0);
        wrapper.orderByDesc(Allocation::getEscalationTime,Allocation::getAllocationId);
        List<Allocation> allocations = allocationMapper.selectList(wrapper);
        for (Allocation allocation : allocations) {
            AllocationFindVo allocationFindVo = new AllocationFindVo();
            BeanUtils.copyProperties(allocation,allocationFindVo);
            Integer state1 = allocationFindVo.getState();
            if ( integer==2){
                if (state1.equals(AllocationApproveEnum.DRAFT.value)||state1.equals(AllocationApproveEnum.UNDER_RECTIFICATION.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_COMPLETED.name);
                }else if (state1.equals(AllocationApproveEnum.IN_APPROVAL.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1.equals(AllocationApproveEnum.PASS.value)||state1.equals(AllocationApproveEnum.REFUSE.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.COMPLETED.name);
                }
            }else if(integer==1){
                if (state1.equals(AllocationApproveEnum.DRAFT.value)||state1.equals(AllocationApproveEnum.UNDER_RECTIFICATION.value)||state1.equals(AllocationApproveEnum.IN_APPROVAL.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1.equals(AllocationApproveEnum.PASS.value)||state1.equals(AllocationApproveEnum.REFUSE.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.COMPLETED.name);
                }
            }else if(integer==0) {
                if (state1.equals(AllocationApproveEnum.DRAFT.value)||state1.equals(AllocationApproveEnum.UNDER_RECTIFICATION.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if (state1.equals(AllocationApproveEnum.IN_APPROVAL.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.TO_BE_COMPLETED.name);
                }else if (state1.equals(AllocationApproveEnum.PASS.value)||state1.equals(AllocationApproveEnum.REFUSE.value)){
                    allocationFindVo.setStateName(AppAllocationStateEnum.COMPLETED.name);
                }
            }
            allocationFindVos.add(allocationFindVo);
        }
        return allocationFindVos;
    }
 
    @Override
    public Page<AllocationPageExt> pageApp(AppAllocationPageCond allocationPageCond) {
        List<Integer> listUnit = unitResult();
        Integer integer = unitAreaCode();
        allocationPageCond.setUnitList(listUnit);
        allocationPageCond.setStateList(stateAllocationList(allocationPageCond.getState(),integer));
        Page<AllocationPageExt> extAppPage = this.baseMapper.extAppPage(allocationPageCond.getPage().convertPage(),allocationPageCond);
        extAppPage.getRecords().forEach(it->{
            Integer state = it.getState();
            if (state.equals(AllocationApproveEnum.DRAFT.value)||state.equals(AllocationApproveEnum.UNDER_RECTIFICATION.value)){
                if(integer==0||integer==1){
                    it.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if(integer==2){
                    it.setStateName(AppAllocationStateEnum.TO_BE_COMPLETED.name);
                }
            }else if(state.equals(AllocationApproveEnum.IN_APPROVAL.value)){
                if(integer==2||integer==1){
                    it.setStateName(AppAllocationStateEnum.TO_BE_PROCESSED.name);
                }else if(integer==0){
                    it.setStateName(AppAllocationStateEnum.TO_BE_COMPLETED.name);
                }
            }else{
                it.setStateName(AppAllocationStateEnum.COMPLETED.name);
            }
        });
 
        return extAppPage;
    }
 
    @Override
    @Transactional
    public boolean saveFile(AppAllocationFileCond appAllocationFileCond) {
        fileTableService.upDateResult(Arrays.asList(appAllocationFileCond.getFile()),appAllocationFileCond.getUserId(), FileTableEnum.APP_ALLOCATION.value);
        return true;
    }
 
    private List<Integer> stateAllocationList(Integer type, Integer integer){
        List<Integer> integerList = new ArrayList<>();
        // type 0 全部 1待办 2待处理 3 已完成
        if(integer.equals(0)){
            if(type.equals(1)){
                integerList.add(AllocationApproveEnum.UNDER_RECTIFICATION.value);
                integerList.add(AllocationApproveEnum.DRAFT.value);
            }else if(type.equals(2)){
                integerList.add(AllocationApproveEnum.IN_APPROVAL.value);
            }
        }else if(integer.equals(2)){
            if(type.equals(1)){
                integerList.add(AllocationApproveEnum.IN_APPROVAL.value);
            }else if(type.equals(2)){
                integerList.add(AllocationApproveEnum.UNDER_RECTIFICATION.value);
                integerList.add(AllocationApproveEnum.DRAFT.value);
            }
        }else if(integer.equals(1)){
            if(type.equals(1)){
                integerList.add(AllocationApproveEnum.IN_APPROVAL.value);
                integerList.add(AllocationApproveEnum.UNDER_RECTIFICATION.value);
                integerList.add(AllocationApproveEnum.DRAFT.value);
            }else if(type.equals(2)){
                integerList.add(0);
            }
        }
        if(type.equals(3)){
            integerList.add(AllocationApproveEnum.PASS.value);
            integerList.add(AllocationApproveEnum.REFUSE.value);
        }
        return integerList;
    }
 
    /**
     * 小程序整改
     *
     * @param changeCond
     */
    @Override
    @Transactional
    public void changeSmallRoutine(AllocationChangeCond changeCond) {
        Integer integer = unitAreaCode();
        if (integer==2){
            throw new BusinessException("此账户只能审核,不能整改!");
        }
        AllocationExt allocationExt = oneAllocation(changeCond.getAllocationId());
        if(!AllocationApproveEnum.UNDER_RECTIFICATION.value.equals(allocationExt.getState())) {
            throw new BusinessException("整改状态,才能提交审批!");
        }
        Allocation allocation = new Allocation();
        allocation.setAllocationId(changeCond.getAllocationId());
        allocation.setIsChange(changeCond.getIsChange());
        allocation.setChangeName(changeCond.getChangeName());
        allocation.setChangeDescribe(changeCond.getChangeDescribe());
        allocation.setState(changeCond.getState());
        allocation.setChangeTime(new Date());
        this.updateById(allocation);
        //添加流程数据
        ApproveTable approveTable = new ApproveTable();
        approveTable.setRelationId(allocation.getAllocationId());
        approveTable.setState(AllocationApproveEnum.UNDER_RECTIFICATION.value);
        approveTable.setStateName(AllocationApproveEnum.UNDER_RECTIFICATION.name);
        approveTable.setApproveModule(FileTableEnum.ALLOCATION_FOUNDATION.value);
        approveTableService.saveResult(approveTable);
        fileTableService.upDateResult(changeCond.getFileChangeList(),allocation.getAllocationId(), FileTableEnum.ALLOCATION_RECTIFICATION.value);
    }
 
    /**
     * 小程序审批
     *
     * @param checkCond
     */
    @Override
    @Transactional
    public void checkSmallRoutine(AllocationCheckCond checkCond) {
        Integer integer = unitAreaCode();
        if (integer==0){
            throw new BusinessException("此账户只能整改,不能审批!");
        }
        AllocationExt allocationExt = oneAllocation(checkCond.getAllocationId());
        if(!AllocationApproveEnum.IN_APPROVAL.value.equals(allocationExt.getState())) {
            throw new BusinessException("审批状态,才能进行审批操作!");
        }
        QxUser qxUser = UserHelper.getCurrentUser();
        Allocation allocation = new Allocation();
        allocation.setCheckScore(checkCond.getCheckScore());
        allocation.setAllocationId(checkCond.getAllocationId());
        allocation.setCheckDescribe(checkCond.getCheckDescribe());
        allocation.setCheckName(qxUser.getUserName());
        allocation.setCheckTime(new Date());
        allocation.setState(checkCond.getState());
        this.updateById(allocation);
        //添加流程数据
        ApproveTable approveTable = new ApproveTable();
        approveTable.setRelationId(allocation.getAllocationId());
        if(AllocationApproveEnum.PASS.value.equals(checkCond.getState())){
            approveTable.setState(AllocationApproveEnum.PASS.value);
            approveTable.setStateName(AllocationApproveEnum.PASS.name);
        }else {
            approveTable.setState(AllocationApproveEnum.REFUSE.value);
            approveTable.setStateName(AllocationApproveEnum.REFUSE.name);
        }
        approveTable.setApproveModule(FileTableEnum.ALLOCATION_FOUNDATION.value);
        approveTableService.saveResult(approveTable);
        fileTableService.upDateResult(checkCond.getFileApproveList(),checkCond.getAllocationId(), FileTableEnum.ALLOCATION_APPROVE.value);
    }
 
    @Override
    public boolean authority(String allocationNum,Integer userId) {
        //获取用户信息
        QxUser user = UserHelper.getCurrentUser();
        List<Integer> userList = responsibilityUnitMapper.selectCodeList(SysDictTypeEnum.SYS_JBD.getValue(),user.getUserId());
        if(CollectionUtils.isNotEmpty(userList)){
            return true;
        }
        if(Objects.nonNull(user.getUnitId())){
            LambdaQueryWrapper<Allocation> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Allocation::getAllocationNum,allocationNum);
            wrapper.eq(Allocation::getIsDel,0);
            wrapper.eq(Allocation::getIsInvalid,0);
            Allocation allocation = allocationMapper.selectOne(wrapper);
            if (allocation.getUnitId().equals(user.getUnitId())){
                return true;
            }
            String code = responsibilityUnitMapper.selectAreaCode(user.getUnitId());
            if(code.length()>6){
                return false;
            }else {
                return true;
            }
        }
        return true;
    }
 
    @Override
    public List<Integer>  unitResult() {
        QxUser user = UserHelper.getCurrentUser();
        Integer unitId = Objects.nonNull(user.getUnitId())?user.getUnitId():0;
        List<Integer> userList = responsibilityUnitMapper.selectCodeList(SysDictTypeEnum.SYS_JBD.getValue(),user.getUserId());
        if(CollectionUtils.isNotEmpty(userList)){
            return new ArrayList<>();
        }
        userList = responsibilityUnitMapper.selectResultList(unitId);
        if(CollectionUtils.isNotEmpty(userList)){
            return userList;
        }
        return Arrays.asList(0);
    }
 
    @Override
    public Integer unitAreaCode() {
        QxUser user = UserHelper.getCurrentUser();
        List<Integer> userList = responsibilityUnitMapper.selectCodeList(SysDictTypeEnum.SYS_JBD.getValue(),user.getUserId());
        if(CollectionUtils.isNotEmpty(userList)){
            return 1;
        }
        if(Objects.nonNull(user.getUnitId())){
            String code = responsibilityUnitMapper.selectAreaCode(user.getUnitId());
            if(code.length()>6){
                return 0;
            }else {
                return 2;
            }
        }
        return 1;
    }
    private boolean escalationType(Integer id){
        boolean f = false;
        String code = responsibilityUnitMapper.selectAreaCode(id);
        if(code.length() == 6){
            return true;
        }
        return f;
    }
}