郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
package com.gkhy.safePlatform.specialWork.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountDepartmentService;
import com.gkhy.safePlatform.account.rpc.apimodel.AccountUserService;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.DepInfoRPCRespDTO;
import com.gkhy.safePlatform.account.rpc.apimodel.model.resp.UserInfoRPCRespDTO;
import com.gkhy.safePlatform.commons.co.ContextCacheUser;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.query.PageQuery;
import com.gkhy.safePlatform.commons.utils.RequestContextHolder;
import com.gkhy.safePlatform.commons.vo.ResultVO;
import com.gkhy.safePlatform.commons.vo.SearchResultVO;
import com.gkhy.safePlatform.specialWork.entity.ApprovalRule;
import com.gkhy.safePlatform.specialWork.entity.ApprovalRuleStep;
import com.gkhy.safePlatform.specialWork.entity.ApprovalRuleUnit;
import com.gkhy.safePlatform.specialWork.enums.ApprovalStepPersonTypeEnum;
import com.gkhy.safePlatform.specialWork.enums.RuleContinueTimeUnitEnum;
import com.gkhy.safePlatform.specialWork.enums.WorkTypeEnum;
import com.gkhy.safePlatform.specialWork.model.bo.ApprovalRuleBO;
import com.gkhy.safePlatform.specialWork.model.dto.req.*;
import com.gkhy.safePlatform.specialWork.entity.*;
import com.gkhy.safePlatform.specialWork.enums.*;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleStepRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleUnitItemRespDTO;
import com.gkhy.safePlatform.specialWork.model.dto.resp.ApprovalRuleUnitRespDTO;
import com.gkhy.safePlatform.specialWork.model.query.db.*;
import com.gkhy.safePlatform.specialWork.service.RuleService;
import com.gkhy.safePlatform.specialWork.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 org.springframework.transaction.annotation.Transactional;
 
import java.security.Guard;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@Service("ruleService")
public class RuleServiceImpl implements RuleService {
    @DubboReference
    private AccountDepartmentService accountDepartmentService;
 
    @DubboReference
    private AccountUserService accountUserService;
    @Autowired
    private ApprovalRuleService approvalRuleService;
 
    @Autowired
    private ApprovalRuleStepService approvalRuleStepService;
 
    @Autowired
    private ApprovalRuleUnitItemService approvalRuleUnitItemService;
 
    @Autowired
    private ApprovalRuleUnitService approvalRuleUnitService;
 
    @Autowired
    private ApprovalRuleStandService approvalRuleStandService;
 
    @Autowired
    private ApprovalRuleItemMeasureService approvalRuleItemMeasureService;
 
    @Autowired
    private RedissonClient redissonClient;
 
 
    @Override
    @Transactional
    public ResultVO save(ApprovalRuleAddReqDTO ruleReqDTO) {
 
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_RULE_INSERT");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            // 部门验证
            DepInfoRPCRespDTO depInfoByDepId = getDepInfoByDepId(ruleReqDTO.getDepId());
            // 作业类型验证
            WorkTypeEnum workType = WorkTypeEnum.parse(ruleReqDTO.getWorkType());
            if(workType == null){
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"作业类型不合法");
            }
            // 作业等级验证
            // 是否是特殊作业
            boolean specialWork = this.isSpecialWork(workType);
            WorkLevelEnum workLevel = WorkLevelEnum.parse(ruleReqDTO.getWorkLevel());
            if (specialWork) {
                // 检查等级 不能是无等级
                if (workLevel == null || workLevel == WorkLevelEnum.DEFAULT) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"特殊作业需要作业等级");
                }
            }else{
                // 不是特殊作业默认无等级
                workLevel = WorkLevelEnum.DEFAULT;
            }
 
            // 同一个部门下审批规则有且仅有一个
            // 验证该部门下是否创建审批流程
            ApprovalRuleListDbQuery query = new ApprovalRuleListDbQuery();
            query.setDepId(ruleReqDTO.getDepId());
            query.setWorkType(ruleReqDTO.getWorkType());
            query.setWorkLevel(workLevel.getLevel());
            // todo 改成count
            List<ApprovalRule> ruleList = approvalRuleService.listRule(query);
            if (ruleList.size() > 0) {
                throw new AusinessException(E.DATA_DATABASE_EXIST, "该部门下该项已存在,不可重复");
            }
 
 
            // 层级
            List<ApprovalRuleStepAddReqDTO> stepReqDTOList = ruleReqDTO.getStepList();
 
            if (stepReqDTOList == null || stepReqDTOList.size() == 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "至少存在一个审批层级");
            }
 
            List<UserInfoRPCRespDTO> userList;
            List<ApprovalRuleItemMeasureDO> measureList;
            List<ApprovalRuleItemStandDO> standList;
 
            // 循环出数据用于后面验证
 
            List<Long> uIdList = new ArrayList<>();
 
            Set<Long> mIdList = new HashSet<>();
 
            Set<Long> sIdList = new HashSet<>();
            stepReqDTOList.forEach(step -> {
                //单元
                List<ApprovalRuleUnitAddReqDTO> units = step.getUnitList();
                if (null != units && units.size() > 0) {
                    units.forEach(unit -> {
                        uIdList.add(unit.getBindUid());
                    });
                }
 
                //审批项
                List<ApprovalRuleUnitItemAddReqDTO> items = step.getItemList();
                if (null != items && items.size() > 0) {
                    items.forEach(item -> {
                        if (null != item.getMeasureId()) {
                            mIdList.add(item.getMeasureId());
                        }
                        if (null != item.getStandId()) {
                            sIdList.add(item.getStandId());
                        }
                    });
                }
 
 
            });
            // 用于后面验证
            measureList = new ArrayList<>(mIdList.size());
            standList = new ArrayList<>(sIdList.size());
            userList = listAccountUsers(uIdList);
            if (mIdList.size() > 0) {
                measureList = approvalRuleItemMeasureService.listItemMeasureByIds(mIdList);
            }
            if (sIdList.size() > 0) {
                standList = approvalRuleStandService.listItemStandByIds(sIdList);
            }
 
            // 0.层级排序
            // 1.八大作业都有监护人层级
            // 2.动火作业 和 受限弓箭 需要分析人层级
            checkRuleStepValid(workType, stepReqDTOList, userList, standList, measureList);
 
            ContextCacheUser currentUser = RequestContextHolder.contextUserLocal.get();
            // exc
            // rule
            ApprovalRule rule = new ApprovalRule();
            rule.setRuleId(IdUtil.getSnowflake(0,0).nextId());
            rule.setDepId(ruleReqDTO.getDepId());
            rule.setRuleName(ruleReqDTO.getRuleName());
            rule.setWorkType(ruleReqDTO.getWorkType());
            rule.setWorkLevel(workLevel.getLevel());
            rule.setCreateUid(currentUser.getUid());
            rule.setCreateUname(currentUser.getUsername());
            // 上一层 id
            Long preStepId = null;
            // 层级List
            List<ApprovalRuleStep> saveStepList = new ArrayList<>(stepReqDTOList.size());
            // 单元List
            List<ApprovalRuleUnit> saveUnitList = new ArrayList<>();
            // 审批项List
            List<ApprovalRuleUnitItem> saveUnitItemList = new ArrayList<>();
            for (ApprovalRuleStepAddReqDTO stepReqDTO : stepReqDTOList) {
                // 填充层级
                ApprovalRuleStep step = new ApprovalRuleStep();
                // 填充规则id
                step.setStepId(IdUtil.getSnowflake(0, 0).nextId());
                step.setRuleId(rule.getRuleId());
                step.setStepName(stepReqDTO.getStepName());
                step.setStepSerial(stepReqDTO.getStepSerial());
                step.setApprovalType(stepReqDTO.getApprovalType());
                step.setPersonType(stepReqDTO.getPersonType());
                step.setContentType(stepReqDTO.getContentType());
                step.setGmtCreate(LocalDateTime.now());
                step.setStatus(RuleStepStatusEnum.VALID.getCode());
                step.setCreateUname(currentUser.getUsername());
                step.setCreateUid(currentUser.getUid());
                step.setAuditType(stepReqDTO.getAuditType());
                step.setPreStepId(preStepId);
                if (stepReqDTO.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_ANALYST.getType())) {
                    // 分析人 标准
                    step.setContinueTime(stepReqDTO.getContinueTime());
                    step.setContinueTimeUnit(stepReqDTO.getContinueTimeUnit());
                }
                saveStepList.add(step);
                preStepId = step.getStepId();
 
                //单元
                List<ApprovalRuleUnitAddReqDTO> unitReqDTOList = stepReqDTO.getUnitList();
                //遍历审批单元
                for (ApprovalRuleUnitAddReqDTO unitReqDTO : unitReqDTOList) {
                    // 筛选用户
                    List<UserInfoRPCRespDTO> uList = userList.stream()
                            .filter(user -> user.getUid().equals(unitReqDTO.getBindUid()))
                            .collect(Collectors.toList());
                    //填充单元数据
                    ApprovalRuleUnit unit = new ApprovalRuleUnit();
                    unit.setUnitId(IdUtil.getSnowflake(0, 0).nextId());
                    unit.setRuleId(rule.getRuleId());
                    unit.setStepId(step.getStepId());
                    unit.setBindDepId(ruleReqDTO.getDepId());
                    unit.setBindDepName(depInfoByDepId.getDepName());
                    unit.setBindUid(unitReqDTO.getBindUid());
                    unit.setBindUname(uList.get(0).getUsername());
                    unit.setGmtCreate(LocalDateTime.now());
                    unit.setStatus(RuleUnitStatusEnum.VALID.getCode());
                    unit.setCreateUid(currentUser.getUid());
                    unit.setCreateUname(currentUser.getUsername());
                    saveUnitList.add(unit);
                }
 
 
                // 审批项
                List<ApprovalRuleUnitItemAddReqDTO> itemReqDTOList = stepReqDTO.getItemList();
                //遍历审批项
                if (itemReqDTOList != null && itemReqDTOList.size() > 0) {
                    for (ApprovalRuleUnitItemAddReqDTO item : itemReqDTOList) {
                        // 填充审批项数据
                        ApprovalRuleUnitItem itemVo = new ApprovalRuleUnitItem();
                        itemVo.setId(IdUtil.getSnowflake(0, 0).nextId());
                        itemVo.setItemName(item.getItemName());
                        itemVo.setRuleId(rule.getRuleId());
                        itemVo.setStepId(step.getStepId());
                        itemVo.setType(item.getType());
                        itemVo.setStatus(RuleItemStatusEnum.VALID.getCode());
                        itemVo.setGmtCreate(LocalDateTime.now());
                        itemVo.setCreateUid(currentUser.getUid());
                        itemVo.setCreateUname(currentUser.getUsername());
                        // 是否分析人审批
                        if (stepReqDTO.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_ANALYST.getType())) {
                            // 分析人 标准
                            itemVo.setStandId(item.getStandId());
                        } else {
                            // 安全措施
                            itemVo.setMeasureId(item.getMeasureId());
                        }
                        saveUnitItemList.add(itemVo);
                    }
                }
            }
            // 插入规则
            approvalRuleService.saveOneRule(rule);
            // 批量插入层级
            if (saveStepList.size() > 0) {
                approvalRuleStepService.saveStepList(saveStepList);
            }
            // 批量插入单元
            if (saveUnitList.size() > 0) {
                approvalRuleUnitService.saveUnitList(saveUnitList);
            }
            // 批量插入审批项
            if (saveUnitItemList.size() > 0) {
                approvalRuleUnitItemService.batchSaveItemList(saveUnitItemList);
            }
 
            //创建成功,释放锁
            lock.unlock();
 
        }catch (AusinessException e) {
            e.printStackTrace();
            throw new AusinessException(e.getCode(), e.getMessage());
        }catch (BusinessException e) {
            e.printStackTrace();
            throw new BusinessException(e.getCode(), e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ResultCodes.SERVER_ERROR);
        }finally {
            if(lock.isLocked()){
                lock.unlock();
            }
        }
 
        return new ResultVO(ResultCodes.OK);
    }
 
    private boolean isSpecialWork(WorkTypeEnum workType) {
        boolean result = false;
        // 动火
        if (workType == WorkTypeEnum.WORK_FIRE ||
                // 高处
                workType == WorkTypeEnum.WORK_HIGH_SPACE ||
                // 吊装
                workType == WorkTypeEnum.WORK_HANG ||
                // 盲板
                workType == WorkTypeEnum.WORK_BLIND_PLATE) {
            result = true;
        }
        return result;
    }
 
 
 
    // 1.八大作业都有监护人层级 监护人有且仅有一个
    // 2.动火作业 和 受限弓箭 需要分析人层级 其他不需要分析人层级 分析人有且仅有一个
    private void checkRuleStepValid(WorkTypeEnum workType,
                                    List<ApprovalRuleStepAddReqDTO> stepReqDTOList,
                                    List<UserInfoRPCRespDTO> userList,
                                    List<ApprovalRuleItemStandDO> standList,
                                    List<ApprovalRuleItemMeasureDO> measureList) {
        // 检查序号是否存在重复和重新排序
        List<Integer> collect = stepReqDTOList.stream()
                // 序号
                .map(ApprovalRuleStepAddReqDTO::getStepSerial)
                // 去重
                .distinct()
                // 集合
                .collect(Collectors.toList());
 
        if (stepReqDTOList.size() > collect.size()) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "排序号不可重复!");
        }
        // 按照升序排序
        Collections.sort(stepReqDTOList, new Comparator<ApprovalRuleStepAddReqDTO>() {
            @Override
            public int compare(ApprovalRuleStepAddReqDTO o1, ApprovalRuleStepAddReqDTO o2) {
                //升序
                return o1.getStepSerial().compareTo(o2.getStepSerial());
            }
        });
        // 监护人层级个数
        int guardianStepCount = 0;
        // 分析人层级个数
        int analystStepCount = 0;
        for (ApprovalRuleStepAddReqDTO step : stepReqDTOList) {
            // 层级类型
            if (step.getPersonType() == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"层级人员类型为空");
            }
            if (ApprovalStepPersonTypeEnum.parse(step.getPersonType()) == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"层级人员类型不合法");
            }
            // 审批单位
            if (step.getUnitList() == null || step.getUnitList().size() == 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"层级人员为空");
            }
            // 审批内容类型
            if (step.getContentType() == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"审批内容类型为空");
            }
            ApprovalStepContentTypeEnum stepContentType = ApprovalStepContentTypeEnum.parse(step.getContentType());
            if (stepContentType == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"审批内容类型不合法");
            }
            // 审批类型 多人 还是单人
            ApprovalStepApprovalTypeEnum approvalType = ApprovalStepApprovalTypeEnum.parse(step.getApprovalType());
            if (approvalType == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"审批人数类型不合法");
            }
            AuditTypeEnum auditTypeEnum = AuditTypeEnum.parse(step.getAuditType());
            if (auditTypeEnum == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"并行审批类型不合法");
            }
 
            // 审批层级单元
            List<ApprovalRuleUnitAddReqDTO> unitList = step.getUnitList();
            if (unitList == null || unitList.size() == 0) {
                throw new AusinessException(E.DATA_PARAM_NULL, step.getStepName() + "未选择审批人!");
            }
            ///  审批类型(单人审批 | 多人审批)  匹配 选择人数
            this.checkPersonTypeMatchCounts(approvalType, unitList);
            // 分析人层级
            if (step.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_ANALYST.getType())) {
                // 分析人层级 ++
                analystStepCount++;
                // 有效时间 有效时间单位
                if (null == step.getContinueTime() || null == step.getContinueTimeUnit()) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "分析审批人层级,有效时间或者有效时间单位不可为空!");
                }
                // 有效时间单位
                if (RuleContinueTimeUnitEnum.parse(step.getContinueTimeUnit()) == null) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "有效时间单位不合法!");
                }
                if (approvalType == ApprovalStepApprovalTypeEnum.TYPE_MULTIPLE_PEOPLE && auditTypeEnum == AuditTypeEnum.CONCURRENT_JOINT_TRIAL) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "分析人选择多人只能并行单审");
                }
 
                // 审批内容类型为审批项审批
                if (stepContentType != ApprovalStepContentTypeEnum.APP_ITEM) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "分析层级必须为审批项审批");
                }
                // 审批(分析)项不可为空
                if (null == step.getItemList() || step.getItemList().size() == 0) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "分析审批人层级,请添加分析项!");
                }
                // 校验审批项的标准
                for (ApprovalRuleUnitItemAddReqDTO item : step.getItemList()) {
                    RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(item.getType());
                    if (typeEnum == null) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法");
                    }
                    if (!item.getType().equals(RuleItemTypeEnum.NUMERIC.getCode())) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项只适用数值类型标准");
                    }
                    assert item.getStandId() != null;
                    long count = standList.stream().filter(stand -> stand.getId().equals(item.getStandId())).count();
                    if (count < 1) {
                        throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, step.getStepName() + ",审批项标准不存在!");
                    }
                }
 
 
 
            }
            // 监护人层级
            if (step.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_GUARDIAN.getType())) {
                // 监护人层级 ++
                guardianStepCount++;
                // 1.审批项审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_ITEM) {
                    // 审批(分析)项不可为空
                    if (null == step.getItemList() || step.getItemList().size() == 0) {
                        throw new AusinessException(E.DATA_PARAM_NULL, "审批项目审批,审批项不可为空!");
                    }
                    for (ApprovalRuleUnitItemAddReqDTO item : step.getItemList()) {
                        RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(item.getType());
                        if (typeEnum == null) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法");
                        }
                        // 审批项 => 填空 | 选择
                        if (typeEnum != RuleItemTypeEnum.FILL && typeEnum != RuleItemTypeEnum.OPTION) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "监护层审批项类型不合法");
                        }
                        if (item.getMeasureId() == null) {
                            throw new AusinessException(E.DATA_PARAM_NULL,"安全措施id为空");
                        }
                        List<ApprovalRuleItemMeasureDO> measures = measureList.stream()
                                .filter(measure -> measure.getId().equals(item.getMeasureId()))
                                .collect(Collectors.toList());
                        if (measures.size() < 1) {
                            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, step.getStepName() + ",审批项安全措施不存在!");
                        }
                        if (typeEnum == RuleItemTypeEnum.FILL && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择填空类型安全措施");
                        }
                        if (typeEnum == RuleItemTypeEnum.OPTION && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择选择类型安全措施");
                        }
                    }
                }
                // 2.简单审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_SIMPLE) {
 
                }
            }
 
            // 普通人审批
            if (step.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_NORMAL.getType())) {
 
                // 审批人数类型
                // 单人审批 <=> 1人 单审
                if (approvalType == ApprovalStepApprovalTypeEnum.TYPE_SINGLE_PERSON) {
                    if (step.getUnitList().size() != 1) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, step.getStepName() + "层单人审批只能选择一个审批人");
                    }
                    if (auditTypeEnum != AuditTypeEnum.CONCURRENT_SINGLE_TRIAL) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, step.getStepName() + "层单人审批只能选择并行单审");
                    }
                }
                // 多人审批
                if (approvalType == ApprovalStepApprovalTypeEnum.TYPE_MULTIPLE_PEOPLE) {
                    if (step.getUnitList().size() < 2) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, step.getStepName() + "层多人审批需选择多个审批人");
                    }
                }
 
                // 1.审批项审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_ITEM) {
                    // 审批(分析)项不可为空
                    if (null == step.getItemList() || step.getItemList().size() == 0) {
                        throw new AusinessException(E.DATA_PARAM_NULL, "审批项目审批,审批项不可为空!");
                    }
                    for (ApprovalRuleUnitItemAddReqDTO item : step.getItemList()) {
                        RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(item.getType());
                        if (typeEnum == null) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法");
                        }
                        // 审批项 => 填空 | 选择
                        if (typeEnum != RuleItemTypeEnum.FILL && typeEnum != RuleItemTypeEnum.OPTION) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "监护层审批项类型不合法");
                        }
                        if (item.getMeasureId() == null) {
                            throw new AusinessException(E.DATA_PARAM_NULL,"安全措施id为空");
                        }
                        List<ApprovalRuleItemMeasureDO> measures = measureList.stream()
                                .filter(measure -> measure.getId().equals(item.getMeasureId()))
                                .collect(Collectors.toList());
 
                        if (measures.size() < 1) {
                            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, step.getStepName() + ",审批项安全措施不存在!");
                        }
 
                        if (typeEnum == RuleItemTypeEnum.FILL && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择填空类型安全措施");
                        }
                        if (typeEnum == RuleItemTypeEnum.OPTION && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择选择类型安全措施");
                        }
                    }
                }
                // 2.简单审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_SIMPLE) {
 
                }
            }
 
 
 
 
            // 统一做 人员id 是否存在
            for (ApprovalRuleUnitAddReqDTO unit : unitList) {
                long count = userList.stream().filter(user -> user.getUid().equals(unit.getBindUid())).count();
                if (count < 1) {
                    throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批人员不存在!");
                }
            }
 
 
        }
        // 动火 || 受限
        if (workType == WorkTypeEnum.WORK_FIRE || workType == WorkTypeEnum.WORK_CLOSE_SPACE) {
            if (analystStepCount == 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "缺少分析人层级");
            }
            if (analystStepCount > 1) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "分析人层级有且仅有一层");
            }
        }else{
            // !动火 && !受限
            if (analystStepCount > 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "非动火和受限空间作业,不需要分析人层级");
            }
        }
 
        if (guardianStepCount == 0) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "缺少监护人层级");
        }
        if (guardianStepCount > 1) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "监护人层级有且仅有一层");
        }
 
 
    }
 
    @Override
    @Transactional
    public ResultVO update(ApprovalRuleModReqDTO ruleReqDTO) {
 
        //主键
        if(null == ruleReqDTO.getRuleId()){
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        ApprovalRule approvalRule = approvalRuleService.getById(ruleReqDTO.getRuleId());
        if (approvalRule == null) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"作业规则不存在");
        }
        if (!approvalRule.getDepId().equals(ruleReqDTO.getDepId())) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"部门不能修改");
        }
 
        //部门验证
        DepInfoRPCRespDTO depInfoByDepId = getDepInfoByDepId(ruleReqDTO.getDepId());
 
        //根据规则id获取所有层级
        List<ApprovalRuleStep> stepList = approvalRuleStepService.listByRuleId(ruleReqDTO.getRuleId());
        //根据规则id获取所有单元
        List<ApprovalRuleUnit> unitList = approvalRuleUnitService.listByRuleId(ruleReqDTO.getRuleId());
        //根据规则id获取所有审批项
        List<ApprovalRuleUnitItem> unitItemList = approvalRuleUnitItemService.listByRuleId(ruleReqDTO.getRuleId());
 
        ContextCacheUser currentUser = RequestContextHolder.contextUserLocal.get();
 
        // 是否是特殊作业
        WorkTypeEnum workType= WorkTypeEnum.parse(ruleReqDTO.getWorkType());
        boolean specialWork = this.isSpecialWork(workType);
        WorkLevelEnum workLevel = WorkLevelEnum.parse(ruleReqDTO.getWorkLevel());
        if (specialWork) {
            // 检查等级 不能是无等级
            if (workLevel == null || workLevel == WorkLevelEnum.DEFAULT) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"特殊作业需要作业等级");
            }
        }else{
            // 不是特殊作业默认无等级
            workLevel = WorkLevelEnum.DEFAULT;
        }
 
        //填充规则信息
        ApprovalRule rule = new ApprovalRule();
        rule.setRuleId(ruleReqDTO.getRuleId());
        rule.setRuleName(ruleReqDTO.getRuleName());
        rule.setModifiedUid(currentUser.getUid());
        rule.setModifiedUname(currentUser.getUsername());
 
        //层级List
        List<ApprovalRuleStep> saveStepList = new ArrayList<>();
        List<ApprovalRuleStep> updateStepList = new ArrayList<>();
        //单元List
        List<ApprovalRuleUnit> saveUnitList = new ArrayList<>();
        //审批项List
        List<ApprovalRuleUnitItem> saveUnitItemList = new ArrayList<>();
        List<ApprovalRuleUnitItem> updateUnitItemList = new ArrayList<>();
 
        //层级
        List<ApprovalRuleStepModReqDTO> stepModReqDTOList = ruleReqDTO.getStepList();
        if(stepModReqDTOList != null && stepModReqDTOList.size()>0) {
 
            List<Long> uIdList = new ArrayList<>();
            Set<Long> mIdList = new HashSet<>();
            Set<Long> sIdList = new HashSet<>();
            //循环出数据用于后面验证
            stepModReqDTOList.forEach(step ->{
                //单元
                List<ApprovalRuleUnitModReqDTO> units = step.getUnitList();
                if(null != units && units.size()>0){
                    units.forEach(unit ->{
                        uIdList.add(unit.getBindUid());
                    });
                }
 
                //审批项
                List<ApprovalRuleUnitItemModReqDTO> items = step.getItemList();
                if(null != items && items.size()>0){
                    items.forEach(item ->{
                        if (null != item.getMeasureId()){
                            mIdList.add(item.getMeasureId());
                        }
                        if (null != item.getStandId()){
                            sIdList.add(item.getStandId());
                        }
                    });
                }
 
 
            });
            //用于后面验证
            List<UserInfoRPCRespDTO> userList = new ArrayList<>();
            List<ApprovalRuleItemMeasureDO> measureList = new ArrayList<>();
            List<ApprovalRuleItemStandDO> standList = new ArrayList<>();
            if(uIdList.size()>0){
                userList = listAccountUsers(uIdList);
            }
            if(mIdList.size()>0){
                measureList = approvalRuleItemMeasureService.listItemMeasureByIds(mIdList);
            }
            if(sIdList.size()>0){
                standList = approvalRuleStandService.listItemStandByIds(sIdList);
            }
            //检查序号是否存在重复
            List<Integer> collect = stepModReqDTOList.stream().map(ApprovalRuleStepModReqDTO::getStepSerial).distinct().collect(Collectors.toList());
            if (stepModReqDTOList.size() > collect.size()) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "排序号不可重复!");
            }
            //按照升序排序
            Collections.sort(stepModReqDTOList, new Comparator<ApprovalRuleStepModReqDTO>() {
                @Override
                public int compare(ApprovalRuleStepModReqDTO o1, ApprovalRuleStepModReqDTO o2) {
                    //升序
                    return o1.getStepSerial().compareTo(o2.getStepSerial());
                }
            });
 
            //过滤出要删除的层级 做删除以及解绑操作
            List<ApprovalRuleStep> deleteOldStepList = stepList.stream().parallel().filter(a ->
                            stepModReqDTOList.stream().noneMatch(b ->
                                    a.getStepId().equals(b.getStepId())))
                    .collect(Collectors.toList());
            List<Long> deleteOldStepIdList = deleteOldStepList.stream().map(step -> step.getStepId()).collect(Collectors.toList());
            if(deleteOldStepIdList != null && deleteOldStepIdList.size()>0){
                //删除关联的审批项
                approvalRuleUnitItemService.updateStatusByStepIds(deleteOldStepIdList);
                //删除关联的单元
                approvalRuleUnitService.updateStatusByStepIds(deleteOldStepIdList);
                //先删除原有层级
                approvalRuleStepService.updateStatusByStepIds(deleteOldStepIdList);
            }
 
            Long stepId = null;
            WorkTypeEnum workTypeEnum = WorkTypeEnum.parse(ruleReqDTO.getWorkType());
 
            checkRuleStepValid2(workTypeEnum, ruleReqDTO.getStepList(), userList, standList, measureList);
 
            //循环层级 更新的层级
            for (ApprovalRuleStepModReqDTO stepReqDTO : stepModReqDTOList) {
                //填充层级数据
                ApprovalRuleStep step = new ApprovalRuleStep();
 
                List<ApprovalRuleUnit> oldUnitList = new ArrayList<>();
                List<ApprovalRuleUnitItem> oldUnitItemList = new ArrayList<>();
 
                //填充规则id
                step.setRuleId(rule.getRuleId());
                step.setStepName(stepReqDTO.getStepName());
                step.setStepSerial(stepReqDTO.getStepSerial());
                step.setPersonType(stepReqDTO.getPersonType());
                step.setApprovalType(stepReqDTO.getApprovalType());
                step.setContentType(stepReqDTO.getContentType());
                step.setAuditType(stepReqDTO.getAuditType());
                if (stepReqDTO.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_ANALYST.getType())) {
                    // 分析人 标准
                    step.setContinueTime(stepReqDTO.getContinueTime());
                    step.setContinueTimeUnit(stepReqDTO.getContinueTimeUnit());
                }
                //判断是否第一个节点
                if (null != stepId) {
                    step.setPreStepId(stepId);
                }
                //层级主键不为空 做更新操作
                if(null != stepReqDTO.getStepId()){
                    step.setModifiedUid(currentUser.getUid());
                    step.setModifiedUname(currentUser.getUsername());
                    step.setStepId(stepReqDTO.getStepId());
                    updateStepList.add(step);
 
                    //过滤出层级原有的单元人员
                    oldUnitList = unitList.stream().filter(unit -> unit.getStepId().equals(stepReqDTO.getStepId())).collect(Collectors.toList());
                    //过滤出层级原有的审批项
                    oldUnitItemList = unitItemList.stream().filter(item -> item.getStepId().equals(stepReqDTO.getStepId())).collect(Collectors.toList());
                }else{
                    step.setStepId(IdUtil.getSnowflake(0,0).nextId());
                    step.setStatus(RuleStepStatusEnum.VALID.getCode());
                    step.setGmtCreate(LocalDateTime.now());
                    step.setCreateUid(currentUser.getUid());
                    step.setCreateUname(currentUser.getUsername());
                    saveStepList.add(step);
 
                }
                //赋值
                stepId = step.getStepId();
 
                //单元
                List<ApprovalRuleUnitModReqDTO> unitModReqDTOList = stepReqDTO.getUnitList();
                //过滤出原来存在的单元
                List<ApprovalRuleUnit> undeleteOldUnitList = oldUnitList.stream().parallel().filter(oldUnit -> unitModReqDTOList.stream()
                        .anyMatch(newUnit-> newUnit.getBindUid().equals(oldUnit.getBindUid()))).collect(Collectors.toList());
                //过滤出要删除的单元 做删除操作
               List<ApprovalRuleUnit> deleteOldUnitList = oldUnitList.stream().parallel().filter(a ->
                               unitModReqDTOList.stream().noneMatch(b ->
                                       a.getBindUid().equals(b.getBindUid())))
                       .collect(Collectors.toList());
               if(deleteOldUnitList.size()>0 && deleteOldUnitList != null){
                   List<Long> deleteOldUnitIdList = deleteOldUnitList.stream().map(unit->unit.getUnitId()).collect(Collectors.toList());
                   //删除审批单元
                   approvalRuleUnitService.updateStatusByUnitIds(deleteOldUnitIdList);
               }
 
                //遍历单元
                for (ApprovalRuleUnitModReqDTO unitReqDTO : unitModReqDTOList) {
                    List<UserInfoRPCRespDTO> uList = userList.stream().filter(user -> user.getUid().equals(unitReqDTO.getBindUid())).collect(Collectors.toList());
                   if (uList.size() == 0) {
                       throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批人员不存在!");
                   }
                    List<ApprovalRuleUnit> selectUnit = undeleteOldUnitList.stream().filter(u -> u.getBindUid().equals(unitReqDTO.getBindUid())).collect(Collectors.toList());
                   if(selectUnit.size() == 0){ //如果非原有单元,则插入
                       ApprovalRuleUnit unit = new ApprovalRuleUnit();
                       unit.setUnitId(IdUtil.getSnowflake(0,0).nextId());
                       unit.setRuleId(rule.getRuleId());
                       unit.setStepId(step.getStepId());
                       unit.setBindDepId(ruleReqDTO.getDepId());
                       unit.setBindDepName(depInfoByDepId.getDepName());
                       unit.setBindUid(unitReqDTO.getBindUid());
                       unit.setBindUname(uList.get(0).getUsername());
                       unit.setGmtCreate(LocalDateTime.now());
                       unit.setStatus(RuleUnitStatusEnum.VALID.getCode());
                       unit.setCreateUid(currentUser.getUid());
                       unit.setCreateUname(currentUser.getUsername());
                       saveUnitList.add(unit);
                   }
                }
 
                //审批项
                List<ApprovalRuleUnitItemModReqDTO> itemModReqDTOList = stepReqDTO.getItemList();
                if(itemModReqDTOList != null){
                    //过滤出要删除的审批项 做删除操作
                    List<ApprovalRuleUnitItem> deleteOldItemList = oldUnitItemList.stream().parallel().filter(a ->
                                    itemModReqDTOList.stream().noneMatch(b ->
                                            a.getId().equals(b.getId())))
                            .collect(Collectors.toList());
                    if(deleteOldItemList.size()>0 && deleteOldItemList != null){
                        List<Long> deleteOldUnitIdList = deleteOldItemList.stream().map(item->item.getId()).collect(Collectors.toList());
                        //删除审批项
                        approvalRuleUnitItemService.updateStatusByIds(deleteOldUnitIdList);
 
                    }
                    //遍历审批项
                    for (ApprovalRuleUnitItemModReqDTO itemReqDTO : itemModReqDTOList) {
 
                        ApprovalRuleUnitItem itemVo = new ApprovalRuleUnitItem();
                        itemVo.setItemName(itemReqDTO.getItemName());
                        itemVo.setRuleId(rule.getRuleId());
                        itemVo.setStepId(step.getStepId());
                        itemVo.setType(itemReqDTO.getType());
                        // 是否分析人审批
                        if (stepReqDTO.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_ANALYST.getType())) {
                            // 分析人 标准
                            itemVo.setStandId(itemReqDTO.getStandId());
                        } else {
                            // 安全措施
                            itemVo.setMeasureId(itemReqDTO.getMeasureId());
                        }
                        //主键为空 则新增
                        if(null == itemReqDTO.getId()){
                            itemVo.setId(IdUtil.getSnowflake(0,0).nextId());
                            itemVo.setStatus(RuleItemStatusEnum.VALID.getCode());
                            itemVo.setGmtCreate(LocalDateTime.now());
                            itemVo.setCreateUid(currentUser.getUid());
                            itemVo.setCreateUname(currentUser.getUsername());
                            saveUnitItemList.add(itemVo);
                        }else{ //修改操作
                            //填充主键
                            itemVo.setId(itemReqDTO.getId());
                            itemVo.setGmtModified(LocalDateTime.now());
                            itemVo.setModifiedUid(currentUser.getUid());
                            itemVo.setModifiedUname(currentUser.getUsername());
                            updateUnitItemList.add(itemVo);
                        }
                    }
                }
            }
        }
        //更新规则
        approvalRuleService.updateRule(rule);
 
        if(saveStepList.size()>0){
            //批量插入层级
            approvalRuleStepService.saveStepList(saveStepList);
        }
        if(updateStepList.size()>0){
            //批量更新层级
            approvalRuleStepService.updateBatchStep(updateStepList);
        }
        if(saveUnitList.size()>0){
            //批量插入单元
            approvalRuleUnitService.saveUnitList(saveUnitList);
        }
        if(saveUnitItemList.size()>0){
            //批量插入审批项
            approvalRuleUnitItemService.batchSaveItemList(saveUnitItemList);
        }
        if(updateUnitItemList.size()>0){
            //批量更新审批项
            approvalRuleUnitItemService.updateBatchItem(updateUnitItemList);
        }
        return new ResultVO<>(ResultCodes.OK);
    }
 
    @Override
    @Transactional
    public ResultVO removeBatchRule(DeleteForm deleteForm) {
 
        //删除规则表中数据
        approvalRuleService.updateStutsByRuleIds(deleteForm.getIds());
 
        //删除关联该规则的层级
        approvalRuleStepService.updateStatusByRuleIds(deleteForm.getIds());
 
        //删除单元
        approvalRuleUnitService.updateStatusByRuleIds(deleteForm.getIds());
 
        //删除审批项
        approvalRuleUnitItemService.updateStatusByRuleIds(deleteForm.getIds());
 
        return new ResultVO<>(ResultCodes.OK);
    }
 
 
 
    // 1.八大作业都有监护人层级 监护人有且仅有一个
    // 2.动火作业 和 受限弓箭 需要分析人层级 其他不需要分析人层级 分析人有且仅有一个
    private void checkRuleStepValid2(WorkTypeEnum workType,
                                    List<ApprovalRuleStepModReqDTO> stepReqDTOList,
                                    List<UserInfoRPCRespDTO> userList,
                                    List<ApprovalRuleItemStandDO> standList,
                                    List<ApprovalRuleItemMeasureDO> measureList) {
        // 检查序号是否存在重复和重新排序
        List<Integer> collect = stepReqDTOList.stream()
                // 序号
                .map(ApprovalRuleStepModReqDTO::getStepSerial)
                // 去重
                .distinct()
                // 集合
                .collect(Collectors.toList());
 
        if (stepReqDTOList.size() > collect.size()) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "排序号不可重复!");
        }
        // 按照升序排序
        Collections.sort(stepReqDTOList, new Comparator<ApprovalRuleStepModReqDTO>() {
            @Override
            public int compare(ApprovalRuleStepModReqDTO o1, ApprovalRuleStepModReqDTO o2) {
                //升序
                return o1.getStepSerial().compareTo(o2.getStepSerial());
            }
        });
        // 监护人层级个数
        int guardianStepCount = 0;
        // 分析人层级个数
        int analystStepCount = 0;
        for (ApprovalRuleStepModReqDTO step : stepReqDTOList) {
            // 层级类型
            if (step.getPersonType() == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"层级人员类型为空");
            }
            if (ApprovalStepPersonTypeEnum.parse(step.getPersonType()) == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"层级人员类型不合法");
            }
            // 审批单位
            if (step.getUnitList() == null || step.getUnitList().size() == 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"层级人员为空");
            }
            // 审批内容类型
            if (step.getContentType() == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"审批内容类型为空");
            }
            ApprovalStepContentTypeEnum stepContentType = ApprovalStepContentTypeEnum.parse(step.getContentType());
            if (stepContentType == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"审批内容类型不合法");
            }
            // 审批类型 多人 还是单人
            ApprovalStepApprovalTypeEnum approvalType = ApprovalStepApprovalTypeEnum.parse(step.getApprovalType());
            if (approvalType == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"审批人数类型不合法");
            }
            AuditTypeEnum auditTypeEnum = AuditTypeEnum.parse(step.getAuditType());
            if (auditTypeEnum == null) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"并行审批类型不合法");
            }
 
            // 审批层级单元
            List<ApprovalRuleUnitModReqDTO> unitList = step.getUnitList();
            if (unitList == null || unitList.size() == 0) {
                throw new AusinessException(E.DATA_PARAM_NULL, step.getStepName() + "未选择审批人!");
            }
            // 多人审批 单人审批 匹配上 人数
            this.checkPersonTypeMatchCounts(approvalType, unitList);
 
 
            // 分析人层级
            if (step.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_ANALYST.getType())) {
                // 分析人层级 ++
                analystStepCount++;
                // 有效时间 有效时间单位
                if (null == step.getContinueTime() || null == step.getContinueTimeUnit()) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "分析审批人层级,有效时间或者有效时间单位不可为空!");
                }
                // 有效时间单位
                if (RuleContinueTimeUnitEnum.parse(step.getContinueTimeUnit()) == null) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "有效时间单位不合法!");
                }
 
                if (approvalType == ApprovalStepApprovalTypeEnum.TYPE_MULTIPLE_PEOPLE && auditTypeEnum == AuditTypeEnum.CONCURRENT_JOINT_TRIAL) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "分析人选择多人只能并行单审");
                }
 
                // 审批内容类型为审批项审批
                if (stepContentType != ApprovalStepContentTypeEnum.APP_ITEM) {
                    throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "分析层级必须为审批项审批");
                }
                // 审批(分析)项不可为空
                if (null == step.getItemList() || step.getItemList().size() == 0) {
                    throw new AusinessException(E.DATA_PARAM_NULL, "分析审批人层级,请添加分析项!");
                }
                // 校验审批项的标准
                for (ApprovalRuleUnitItemModReqDTO item : step.getItemList()) {
                    RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(item.getType());
                    if (typeEnum == null) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法");
                    }
                    if (!item.getType().equals(RuleItemTypeEnum.NUMERIC.getCode())) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项只适用数值类型标准");
                    }
                    assert item.getStandId() != null;
                    long count = standList.stream().filter(stand -> stand.getId().equals(item.getStandId())).count();
                    if (count < 1) {
                        throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, step.getStepName() + ",审批项标准不存在!");
                    }
                }
 
 
 
            }
            // 监护人层级
            if (step.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_GUARDIAN.getType())) {
                // 监护人层级 ++
                guardianStepCount++;
 
                // 1.审批项审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_ITEM) {
                    // 审批(分析)项不可为空
                    if (null == step.getItemList() || step.getItemList().size() == 0) {
                        throw new AusinessException(E.DATA_PARAM_NULL, "审批项目审批,审批项不可为空!");
                    }
                    for (ApprovalRuleUnitItemModReqDTO item : step.getItemList()) {
                        RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(item.getType());
                        if (typeEnum == null) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法");
                        }
                        // 审批项 => 填空 | 选择
                        if (typeEnum != RuleItemTypeEnum.FILL && typeEnum != RuleItemTypeEnum.OPTION) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "监护层审批项类型不合法");
                        }
                        if (item.getMeasureId() == null) {
                            throw new AusinessException(E.DATA_PARAM_NULL,"安全措施id为空");
                        }
                        List<ApprovalRuleItemMeasureDO> measures = measureList.stream()
                                .filter(measure -> measure.getId().equals(item.getMeasureId()))
                                .collect(Collectors.toList());
                        if (measures.size() < 1) {
                            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, step.getStepName() + ",审批项安全措施不存在!");
                        }
                        if (typeEnum == RuleItemTypeEnum.FILL && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择填空类型安全措施");
                        }
                        if (typeEnum == RuleItemTypeEnum.OPTION && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择选择类型安全措施");
                        }
                    }
                }
                // 2.简单审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_SIMPLE) {
 
                }
            }
 
            // 普通人审批
            if (step.getPersonType().equals(ApprovalStepPersonTypeEnum.TYPE_NORMAL.getType())) {
 
                // 审批人数类型
                // 单人审批 <=> 1人 单审
                if (approvalType == ApprovalStepApprovalTypeEnum.TYPE_SINGLE_PERSON) {
                    if (step.getUnitList().size() != 1) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, step.getStepName() + "层单人审批只能选择一个审批人");
                    }
                    if (auditTypeEnum != AuditTypeEnum.CONCURRENT_SINGLE_TRIAL) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, step.getStepName() + "层单人审批只能选择并行单审");
                    }
                }
                // 多人审批
                if (approvalType == ApprovalStepApprovalTypeEnum.TYPE_MULTIPLE_PEOPLE) {
                    if (step.getUnitList().size() < 2) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, step.getStepName() + "层多人审批需选择多个审批人");
                    }
                }
 
                // 1.审批项审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_ITEM) {
                    // 审批(分析)项不可为空
                    if (null == step.getItemList() || step.getItemList().size() == 0) {
                        throw new AusinessException(E.DATA_PARAM_NULL, "审批项目审批,审批项不可为空!");
                    }
                    for (ApprovalRuleUnitItemModReqDTO item : step.getItemList()) {
                        RuleItemTypeEnum typeEnum = RuleItemTypeEnum.parse(item.getType());
                        if (typeEnum == null) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法");
                        }
                        // 审批项 => 填空 | 选择
                        if (typeEnum != RuleItemTypeEnum.FILL && typeEnum != RuleItemTypeEnum.OPTION) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "监护层审批项类型不合法");
                        }
                        if (item.getMeasureId() == null) {
                            throw new AusinessException(E.DATA_PARAM_NULL,"安全措施id为空");
                        }
                        List<ApprovalRuleItemMeasureDO> measures = measureList.stream()
                                .filter(measure -> measure.getId().equals(item.getMeasureId()))
                                .collect(Collectors.toList());
 
                        if (measures.size() < 1) {
                            throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, step.getStepName() + ",审批项安全措施不存在!");
                        }
 
                        if (typeEnum == RuleItemTypeEnum.FILL && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择填空类型安全措施");
                        }
                        if (typeEnum == RuleItemTypeEnum.OPTION && !measures.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())) {
                            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "请选择选择类型安全措施");
                        }
                    }
                }
                // 2.简单审批
                if (stepContentType == ApprovalStepContentTypeEnum.APP_SIMPLE) {
 
                }
            }
 
 
 
 
            // 统一做 人员id 是否存在
            for (ApprovalRuleUnitModReqDTO unit : unitList) {
                long count = userList.stream().filter(user -> user.getUid().equals(unit.getBindUid())).count();
                if (count < 1) {
                    throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批人员不存在!");
                }
            }
 
 
        }
        // 动火 || 受限
        if (workType == WorkTypeEnum.WORK_FIRE || workType == WorkTypeEnum.WORK_CLOSE_SPACE) {
            if (analystStepCount == 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "缺少分析人层级");
            }
            if (analystStepCount > 1) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "分析人层级有且仅有一层");
            }
        }else{
            // !动火 && !受限
            if (analystStepCount > 0) {
                throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "非动火和受限空间作业,不需要分析人层级");
            }
        }
 
        if (guardianStepCount == 0) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "缺少监护人层级");
        }
        if (guardianStepCount > 1) {
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "监护人层级有且仅有一层");
        }
 
 
    }
 
 
 
    @Override
    public SearchResultVO<List<ApprovalRuleRespDTO>> listRuleByPage(PageQuery<ApprovalRuleListDbQuery> pageQuery) {
        Page<ApprovalRule> page = new Page<>(pageQuery.getPageIndex(),pageQuery.getPageSize());
        List<ApprovalRule> approvalRules = approvalRuleService.listRuleByPage(page,pageQuery.getSearchParams());
        //获取层级列表
        List<ApprovalRuleStep> stepList = approvalRuleStepService.listStepByNoConditions();
        //获取单元列表
        List<ApprovalRuleUnit> unitList = approvalRuleUnitService.listByNoConditions();
        //获取审批项列表
        List<ApprovalRuleUnitItem> itemList = approvalRuleUnitItemService.listByNoConditions();
        //获取对应的审批标准
        //获取安全措施列表
 
        List<ApprovalRuleRespDTO> ruleRespList = new ArrayList<>();
        for (ApprovalRule rule : approvalRules) {
            //复制
            ApprovalRuleRespDTO ruleRespDTO = new ApprovalRuleRespDTO();
            BeanUtils.copyProperties(rule,ruleRespDTO);
            //刷选层级
            List<ApprovalRuleStep> selectStepList = stepList.stream().filter(step -> step.getRuleId().equals(rule.getRuleId())).collect(Collectors.toList());
            List<ApprovalRuleStepRespDTO> stepRespList = new ArrayList<>();
            for (ApprovalRuleStep step : selectStepList) {
                //复制
                ApprovalRuleStepRespDTO stepRespDTO = new ApprovalRuleStepRespDTO();
                BeanUtils.copyProperties(step,stepRespDTO);
                //过滤单元
                List<ApprovalRuleUnit> selectUnitList = unitList.stream().filter(unit -> unit.getStepId().equals(step.getStepId())).collect(Collectors.toList());
                List<ApprovalRuleUnitRespDTO> unitRespDTOList = new ArrayList<>();
                for (ApprovalRuleUnit unit: selectUnitList) {
                    //复制
                    ApprovalRuleUnitRespDTO unitRespDTO = new ApprovalRuleUnitRespDTO();
                    BeanUtils.copyProperties(unit,unitRespDTO);
                    unitRespDTOList.add(unitRespDTO);
                }
                //过滤审批项
                List<ApprovalRuleUnitItem> selectItemList = itemList.stream().filter(item -> item.getStepId().equals(step.getStepId())).collect(Collectors.toList());
                List<ApprovalRuleUnitItemRespDTO> itemRespDTOList = new ArrayList<>();
                for (ApprovalRuleUnitItem item: selectItemList) {
                    //复制
                    ApprovalRuleUnitItemRespDTO itemRespDTO = new ApprovalRuleUnitItemRespDTO();
                    BeanUtils.copyProperties(item,itemRespDTO);
                    itemRespDTOList.add(itemRespDTO);
                }
                stepRespDTO.setUnitList(unitRespDTOList);
                stepRespDTO.setItemList(itemRespDTOList);
                stepRespList.add(stepRespDTO);
            }
            ruleRespDTO.setStepList(stepRespList);
            ruleRespList.add(ruleRespDTO);
        }
 
        //需要获取层级、单元、审批项、以及审批规则 等后面封装进去
        return new SearchResultVO<>(true,
                page.getCurrent(),
                page.getSize(),
                page.getPages(),
                page.getTotal(),
                ruleRespList,
                ResultCodes.OK);
    }
 
    @Override
    public ApprovalRuleBO recursiveQueryRule(Long depId, Byte workType, Byte workLevel) {
        ApprovalRuleBO ruleBO = null;
        // rpc获取部门
        if (depId == null) {
            return null;
        }
        ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(depId);
 
        if (rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                DepInfoRPCRespDTO department =  (DepInfoRPCRespDTO)rpcResult.getData();
                ApprovalRule rule = approvalRuleService.getApprovalRuleInfo(depId, workType, workLevel);
                if (rule == null) {
                    ruleBO = recursiveQueryRule(department.getParentDepId(), workType, workLevel);
                }else{
                    ruleBO = new ApprovalRuleBO();
                    ruleBO.setDepId(rule.getDepId());
                    ruleBO.setDepName(department.getDepName());
                    ruleBO.setEid(rule.getEid());
                    ruleBO.setRuleId(rule.getRuleId());
                    ruleBO.setRuleName(rule.getRuleName());
                    ruleBO.setStatus(rule.getStatus());
                    ruleBO.setWorkLevel(rule.getWorkLevel());
                    ruleBO.setWorkType(rule.getWorkType());
                }
            }
        }else{
            throw new BusinessException(rpcResult.getCode(), rpcResult.getMsg());
        }
 
 
        return ruleBO;
 
    }
    //调用Rpc 获取部门
    private DepInfoRPCRespDTO getDepInfoByDepId(Long deptId) {
        DepInfoRPCRespDTO dep = new DepInfoRPCRespDTO();
        ResultVO<DepInfoRPCRespDTO> rpcResult = accountDepartmentService.getDepInfoByDepId(deptId);
        if (rpcResult != null && rpcResult.getCode().equals(ResultCodes.OK.getCode())) {
            if (rpcResult.getData() != null) {
                dep = (DepInfoRPCRespDTO) rpcResult.getData();
            }else{
                throw new BusinessException(ResultCodes.CLIENT_DEP_NOT_EXIST);
            }
        } else {
            throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
        }
        return dep;
    }
 
    //调用RPC 获取去人员
    private List<UserInfoRPCRespDTO> listAccountUsers(List<Long> userIds){
        List<UserInfoRPCRespDTO> userInfoRPCRespDTOList = new ArrayList<>();
        ResultVO<List<UserInfoRPCRespDTO>> listResultVO = accountUserService.listUserInfoByUids(userIds);
        if (listResultVO != null && listResultVO.getCode().equals(ResultCodes.OK.getCode())) {
            if (listResultVO.getData() != null) {
                userInfoRPCRespDTOList = (List<UserInfoRPCRespDTO>) listResultVO.getData();
            }else{
                throw new AusinessException(E.DATA_DATABASE_NO_EXISTENT,"审批人员不存在");
            }
        } else {
            throw new BusinessException(ResultCodes.RPC_ACCESS_EXCEPTION);
        }
        return userInfoRPCRespDTOList;
    }
 
 
    /**
     * @Description: 匹配 审批类型(单人审批|多人审批) 和 人员个数
     */
    private void checkPersonTypeMatchCounts(ApprovalStepApprovalTypeEnum stepApprovalTypeEnum,List<?> persons) {
        if (persons != null && stepApprovalTypeEnum != null) {
            switch (stepApprovalTypeEnum) {
                case TYPE_SINGLE_PERSON:
                    if (persons.size() != 1) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID,"单人审批情况下只能选择一人");
                    }
                case TYPE_MULTIPLE_PEOPLE:
                    if (persons.size() < 1) {
                        throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "多人审批情况下人数请选择多人");
                    }
            }
        }else{
            throw new AusinessException(E.DATA_PARAM_CHECK_INVALID, "人员入参为空");
        }
    }
}