郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.specialWork.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.hotwork.Domain.Exception.E;
import com.gk.hotwork.Domain.Vo.ResultVO;
import com.gk.hotwork.Domain.co.ContextCacheUser;
import com.gk.hotwork.Domain.dto.DepInfoRPCRespDTO;
import com.gk.hotwork.Domain.co.ContextCacheUser;
import com.gk.hotwork.Domain.Exception.E;
import com.gk.hotwork.Domain.Enum.ResultCodes;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Vo.PageQuery;
import com.gk.hotwork.Domain.Vo.ResultVO;
import com.gk.hotwork.Domain.Vo.SearchResultVO;
import com.gk.hotwork.Domain.dto.UserInfoRPCRespDTO;
import com.gk.hotwork.Service.Middle.AccountAuthService;
import com.gk.hotwork.Service.Middle.AccountDepartmentService;
import com.gk.hotwork.specialWork.entity.*;
import com.gk.hotwork.specialWork.entity.ApprovalRule;
import com.gk.hotwork.specialWork.entity.ApprovalRuleStep;
import com.gk.hotwork.specialWork.entity.ApprovalRuleUnit;
import com.gk.hotwork.specialWork.enums.*;
import com.gk.hotwork.specialWork.enums.ApprovalStepTypeEnum;
import com.gk.hotwork.specialWork.enums.RuleContinueTimeUnitEnum;
import com.gk.hotwork.specialWork.enums.WorkTypeEnum;
import com.gk.hotwork.specialWork.model.bo.ApprovalRuleBO;
import com.gk.hotwork.specialWork.model.dto.req.*;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleStepRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleUnitItemRespDTO;
import com.gk.hotwork.specialWork.model.dto.resp.ApprovalRuleUnitRespDTO;
import com.gk.hotwork.specialWork.model.query.db.*;
import com.gk.hotwork.specialWork.service.RuleService;
import com.gk.hotwork.specialWork.service.baseService.*;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
 
@Service("ruleService")
public class RuleServiceImpl implements RuleService {
 
    @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;
 
    @Autowired
    private AccountAuthService accountUserService;
 
    @Autowired
    private AccountDepartmentService accountDepartmentService;
 
 
    @Override
    @Transactional
    public ResultVO save(ContextCacheUser currentUser, ApprovalRuleAddReqDTO ruleReqDTO) {
 
        //加分布式锁,防止重复创建规则
        RLock lock = redissonClient.getLock("LOCK_RULE_INSERT");
        try {
            lock.lock(10, TimeUnit.SECONDS);
            //部门验证
            ResultVO<DepInfoRPCRespDTO> resultVO = accountDepartmentService.getDepInfoByDepId(null, ruleReqDTO.getDepId());
            DepInfoRPCRespDTO depInfoByDepId = (DepInfoRPCRespDTO) resultVO.getData();
            if (null == depInfoByDepId) {
                throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "部门不存在!");
            }
            //作业类型验证
            if (!WorkTypeEnum.checkWorkType(ruleReqDTO.getWorkType())) {
                throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "作业类型不合法!");
            }
            //作业等级验证
            if (null != ruleReqDTO.getWorkLevel()) {//如果作业级别不为空
                if (!WorkLevelEnum.checkWorkLevel(ruleReqDTO.getWorkLevel())) {
                    throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "作业级别不合法!");
                }
            }
            //验证该部门下是否创建审批流程
            ApprovalRuleListDbQuery query = new ApprovalRuleListDbQuery();
            query.setDepId(ruleReqDTO.getDepId());
            query.setWorkType(ruleReqDTO.getWorkType());
            query.setWorkLevel(ruleReqDTO.getWorkLevel());
            List<ApprovalRule> ruleList = approvalRuleService.listRule(query);
            if (ruleList.size() > 0) {
                throw new BusinessException(E.DATA_DATABASE_EXIST, "该部门下该项已存在,不可重复");
            }
            //填充规则信息
            ApprovalRule rule = new ApprovalRule();
            rule.setRuleId(IdUtil.getSnowflake(0, 0).nextId());
            rule.setDepId(ruleReqDTO.getDepId());
            rule.setRuleName(ruleReqDTO.getRuleName());
            rule.setWorkType(ruleReqDTO.getWorkType());
            if (null == ruleReqDTO.getWorkLevel()) {
                rule.setWorkLevel((byte) 0);
            } else {
                rule.setWorkLevel(ruleReqDTO.getWorkLevel());
            }
            rule.setCreateUid(currentUser.getUid());
            rule.setCreateUname(currentUser.getRealName());
 
            //层级List
            List<ApprovalRuleStep> saveStepList = new ArrayList<>();
            //单元List
            List<ApprovalRuleUnit> saveUnitList = new ArrayList<>();
            //审批项List
            List<ApprovalRuleUnitItem> saveUnitItemList = new ArrayList<>();
 
            //层级
            List<ApprovalRuleStepAddReqDTO> stepReqDTOList = ruleReqDTO.getStepList();
            if (stepReqDTOList != null && stepReqDTOList.size() > 0) {
                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());
                            }
                        });
                    }
 
 
                });
                //用于后面验证
                List<UserInfoRPCRespDTO> userList = new ArrayList<>();
                List<ApprovalRuleItemMeasureDO> measureList = new ArrayList<>();
                List<ApprovalRuleItemStandDO> standList = new ArrayList<>();
                if (uIdList.size() > 0) {
                    userList = (List<UserInfoRPCRespDTO>) accountUserService.listUserInfoByUids(uIdList).getData();
                }
                if (mIdList.size() > 0) {
                    measureList = approvalRuleItemMeasureService.listItemMeasureByIds(mIdList);
                }
                if (sIdList.size() > 0) {
                    standList = approvalRuleStandService.listItemStandByIds(sIdList);
                }
 
                //检查序号是否存在重复
                List<Integer> collect = stepReqDTOList.stream().map(ApprovalRuleStepAddReqDTO::getStepSerial).distinct().collect(Collectors.toList());
                if (stepReqDTOList.size() > collect.size()) {
                    throw new BusinessException(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());
                    }
                });
                //循环层级
                Long stepId = null;
                for (ApprovalRuleStepAddReqDTO stepReqDTO : stepReqDTOList) {
                    //填充层级
                    ApprovalRuleStep step = new ApprovalRuleStep();
 
                    if (!ApprovalStepTypeEnum.checkStepType(stepReqDTO.getType())) {
                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "审批类型层级类型不合法!");
                    }
                    //如果是分析人审批,有效时间,以及有效时间单位必须传
                    if (stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())) {
                        if (null == stepReqDTO.getContinueTime() || null == stepReqDTO.getContinueTimeUnit()) {
                            throw new BusinessException(E.DATA_PARAM_NULL, "当前是分析审批人层级,有效时间或者有效时间单位不可为空!");
                        }
                        if (!RuleContinueTimeUnitEnum.checkTimeUnitType(stepReqDTO.getType())) {
                            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "有效时间单位不合法!");
                        }
                        //如果是分析人审批审批项不可为空
                        if(null == stepReqDTO.getItemList() || stepReqDTO.getItemList().size() == 0 ){
                            throw new BusinessException(E.DATA_PARAM_NULL,"分析审批人层级,请添加审批项!");
                        }
                        step.setAuditType(stepReqDTO.getAuditType());
                    }
                    //如果是多人审批并行审核不可为空
                    if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())){
                        if(null == stepReqDTO.getAuditType() ){
                            throw new BusinessException(E.DATA_PARAM_NULL,"多人审批时审核类型不可为空!");
                        }
                        if(null == AuditTypeEnum.parse(stepReqDTO.getAuditType())){
                            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"审核类型不合法!");
                        }
                        step.setAuditType(stepReqDTO.getAuditType());
                    }
                    //填充规则id
                    step.setStepId(IdUtil.getSnowflake(0, 0).nextId());
                    step.setRuleId(rule.getRuleId());
                    step.setStepName(stepReqDTO.getStepName());
                    step.setStepSerial(stepReqDTO.getStepSerial());
                    step.setType(stepReqDTO.getType());
                    step.setContinueTime(stepReqDTO.getContinueTime());
                    step.setContinueTimeUnit(stepReqDTO.getContinueTimeUnit());
                    step.setGmtCreate(LocalDateTime.now());
                    step.setStatus(RuleStepStatusEnum.VALID.getCode());
                    step.setCreateUname(currentUser.getRealName());
                    step.setCreateUid(currentUser.getUid());
                    if (null != stepId) {
                        step.setPreStepId(stepId);
                    }
                    saveStepList.add(step);
                    stepId = step.getStepId();
 
                    //单元
                    List<ApprovalRuleUnitAddReqDTO> unitReqDTOList = stepReqDTO.getUnitList();
                    //遍历审批单元
                    if (unitReqDTOList != null && unitReqDTOList.size() > 0) {
                        //单人审批
                        if (stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_SINGLE_PERSON.getType())) {
                            if (unitReqDTOList.size() > 1) {
                                throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "单人审批类型或者分析审批类型时不可设置多人!");
                            }
                        }
                        //分析人审批有可能是单人,也可能是多人
                        //多人审批
                        if (stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())) {
                            if (unitReqDTOList.size() < 2) {
                                throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "多人审批类型时,需设置多人!");
                            }
                        }
                        for (ApprovalRuleUnitAddReqDTO unitReqDTO : unitReqDTOList) {
                            List<UserInfoRPCRespDTO> uList = userList.stream().filter(user -> user.getUid().equals(unitReqDTO.getBindUid())).collect(Collectors.toList());
                            if (uList.size() == 0) {
                                throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT, "审批人员不存在!");
                            }
                            //填充单元数据
                            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).getRealName());
                            unit.setGmtCreate(LocalDateTime.now());
                            unit.setStatus(RuleUnitStatusEnum.VALID.getCode());
                            unit.setCreateUid(currentUser.getUid());
                            unit.setCreateUname(currentUser.getRealName());
                            saveUnitList.add(unit);
 
                        }
                    } else {
                        throw new BusinessException(E.DATA_PARAM_NULL, step.getStepName() + "未选择审批人!");
                    }
 
 
                    //审批项
                    List<ApprovalRuleUnitItemAddReqDTO> itemReqDTOList = stepReqDTO.getItemList();
                    //遍历审批项
                    if (itemReqDTOList != null && itemReqDTOList.size() > 0) {
                        for (ApprovalRuleUnitItemAddReqDTO item : itemReqDTOList) {
                            //验证类型是否合法
                            if (null == RuleItemTypeEnum.parse(item.getType())) {
                                throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "审批项类型不合法!");
                            }
 
                            //填充审批项数据
                            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.getRealName());
 
                            //是否分析人审批
                            if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())){//分析人
                                //数值
                                if(item.getType().equals(RuleItemTypeEnum.NUMERIC.getCode())) {
                                    //验证标准是否为空
                                    if(null == item.getStandId()){
                                        throw new BusinessException(E.DATA_PARAM_NULL,"分析人审批层级和审批项类型不对应,未关联标准项!");
                                    }
                                    //验证审批标准是否存在
                                    List<ApprovalRuleItemStandDO> sList = standList.stream().filter(stand -> stand.getId().equals(item.getStandId())).collect(Collectors.toList());
                                    if(sList.size() == 0){
                                        throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"标准不存在,请联系管理员!");
                                    }
                                    itemVo.setStandId(item.getStandId());
                                }
                                //选项
                                if(item.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
                                    throw new BusinessException(E.DATA_PARAM_NULL,"审批项类型与分析人类型不一致,请重新选择!");
                                }
                                //填空暂时不操作
                                if(item.getType().equals(RuleItemTypeEnum.FILL.getCode())){
                                    throw new BusinessException(E.DATA_PARAM_NULL,"分析人层级暂不支持填空,请重新选择!");
                                }
 
                            }else{//非分析人
                                //验证安全措施是否为空
                                if(null == item.getMeasureId()){
                                    throw new BusinessException(E.DATA_PARAM_NULL,"多人或单人审批层级和审批项类型不对应,未关联安全措施项!");
                                }
                                //验证安全措施类型是否存在
                                List<ApprovalRuleItemMeasureDO> mList = measureList.stream().filter(measure -> measure.getId().equals(item.getMeasureId())).collect(Collectors.toList());
                                if(mList.size() == 0){
                                    throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"安全措施不存在,请联系管理员!");
                                }
                                //选项
                                if(item.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
                                    //验证审批项类型是否与安全措施一致
                                    if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())){
                                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
                                    }
                                }
                                //填空
                                if(item.getType().equals(RuleItemTypeEnum.FILL.getCode())){
                                    //验证审批项类型是否与安全措施一致
                                    if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())){
                                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
                                    }
                                }
                                itemVo.setMeasureId(item.getMeasureId());
                            }
                            saveUnitItemList.add(itemVo);
                        }
                    }
                }
            }
            //插入规则
            approvalRuleService.saveRuleInfo(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 (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);
    }
 
    @Override
    @Transactional
    public ResultVO update(ContextCacheUser currentUser,ApprovalRuleModReqDTO ruleReqDTO) {
 
        //主键
        if(null == ruleReqDTO.getRuleId()){
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        //部门验证
        ResultVO<DepInfoRPCRespDTO> resultVO = accountDepartmentService.getDepInfoByDepId(null,ruleReqDTO.getDepId());
        DepInfoRPCRespDTO depInfoByDepId= (DepInfoRPCRespDTO)resultVO.getData();
        if(null == depInfoByDepId){
            throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"部门不存在!");
        }
 
 
        //根据规则id获取所有层级
        List<ApprovalRuleStep> stepList = approvalRuleStepService.listByRuleId(ruleReqDTO.getRuleId());
        //根据规则id获取所有单元
        List<ApprovalRuleUnit> unitList = approvalRuleUnitService.listByRuleId(ruleReqDTO.getRuleId());
        //根据规则id获取所有审批项
        List<ApprovalRuleUnitItem> unitItemList = approvalRuleUnitItemService.listByRuleId(ruleReqDTO.getRuleId());
 
        //填充规则信息
        ApprovalRule rule = new ApprovalRule();
        rule.setRuleId(ruleReqDTO.getRuleId());
        rule.setRuleName(ruleReqDTO.getRuleName());
        rule.setModifiedUid(currentUser.getUid());
        rule.setModifiedUname(currentUser.getRealName());
 
        //层级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= (List<UserInfoRPCRespDTO>)accountUserService.listUserInfoByUids(uIdList).getData();
            }
            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 BusinessException(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;
            //循环层级 更新的层级
            for (ApprovalRuleStepModReqDTO stepReqDTO : stepModReqDTOList) {
                //填充层级数据
                ApprovalRuleStep step = new ApprovalRuleStep();
 
                //校验
                if (!ApprovalStepTypeEnum.checkStepType(stepReqDTO.getType())) {
                    throw new BusinessException(E.DATA_PARAM_CHECK_INVALID, "审批类型层级类型不合法!");
                }
                //如果是分析人审批,有效时间,以及有效时间单位必须传
                if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())){
                    if(null == stepReqDTO.getContinueTime() || null == stepReqDTO.getContinueTimeUnit()){
                        throw new BusinessException(E.DATA_PARAM_NULL,"当前是分析审批人层级,有效时间或者有效时间单位不可为空!");
                    }
                    if(!RuleContinueTimeUnitEnum.checkTimeUnitType(stepReqDTO.getType())){
                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"有效时间单位不合法!");
                    }
                    //如果是分析人审批审批项不可为空
                    if(null == stepReqDTO.getItemList() || stepReqDTO.getItemList().size() == 0 ){
                        throw new BusinessException(E.DATA_PARAM_NULL,"分析审批人层级,请添加审批项!");
                    }
                    step.setAuditType(AuditTypeEnum.CONCURRENT_JOINT_TRIAL.getType());
                }
                //如果是多人审批并行审核不可为空
                if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())){
                    if(null == stepReqDTO.getAuditType() ){
                        throw new BusinessException(E.DATA_PARAM_NULL,"多人审批时并行审核类型不可为空!");
                    }
                    if(null == AuditTypeEnum.parse(stepReqDTO.getAuditType())){
                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"审核类型不合法!");
                    }
                    step.setAuditType(stepReqDTO.getAuditType());
                }
                //单人审核默认为空
                if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_SINGLE_PERSON.getType())){
                    step.setAuditType(null);
                }
                List<ApprovalRuleUnit> oldUnitList = new ArrayList<>();
                List<ApprovalRuleUnitItem> oldUnitItemList = new ArrayList<>();
 
                //填充规则id
                step.setRuleId(rule.getRuleId());
                step.setStepName(stepReqDTO.getStepName());
                step.setStepSerial(stepReqDTO.getStepSerial());
                step.setType(stepReqDTO.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.getRealName());
                    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.getRealName());
                    saveStepList.add(step);
 
                }
                //赋值
                stepId = step.getStepId();
 
                //单元
                List<ApprovalRuleUnitModReqDTO> unitModReqDTOList = stepReqDTO.getUnitList();
                if(unitModReqDTOList != null && unitModReqDTOList.size()>0){
                    //单人审批
                    if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_SINGLE_PERSON.getType())){
                        if(unitModReqDTOList.size()>1){
                            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"单人审批类型或者分析审批类型时不可设置多人!");
                        }
                    }
                    //多人审批
                    if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_MULTIPLE_PEOPLE.getType())){
                        if(unitModReqDTOList.size() < 2){
                            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"多人审批类型时,需设置多人!");
                        }
                    }
                    //过滤出原来存在的单元
                    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 BusinessException(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).getRealName());
                           unit.setGmtCreate(LocalDateTime.now());
                           unit.setStatus(RuleUnitStatusEnum.VALID.getCode());
                           unit.setCreateUid(currentUser.getUid());
                           unit.setCreateUname(currentUser.getRealName());
                           saveUnitList.add(unit);
                       }
                    }
 
                }else{
                    throw new BusinessException(E.DATA_PARAM_NULL, step.getStepName()+"未选择审批人!");
                }
                //审批项
                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) {
                        //验证类型是否合法
                        if(null == RuleItemTypeEnum.parse(itemReqDTO.getType())){
                            throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"审批项类型不合法!");
                        }
 
                        ApprovalRuleUnitItem itemVo = new ApprovalRuleUnitItem();
                        itemVo.setItemName(itemReqDTO.getItemName());
                        itemVo.setRuleId(rule.getRuleId());
                        itemVo.setStepId(step.getStepId());
                        itemVo.setType(itemReqDTO.getType());
                        //是否分析人审批
                        if(stepReqDTO.getType().equals(ApprovalStepTypeEnum.TYPE_ANALYST.getType())){//分析人
                            //数值
                            if(itemReqDTO.getType().equals(RuleItemTypeEnum.NUMERIC.getCode())) {
                                //验证标准是否为空
                                if(null == itemReqDTO.getStandId()){
                                    throw new BusinessException(E.DATA_PARAM_NULL,"分析人审批层级未关联标准项!");
                                }
                                //验证审批标准是否存在
                                List<ApprovalRuleItemStandDO> sList = standList.stream().filter(stand -> stand.getId().equals(itemReqDTO.getStandId())).collect(Collectors.toList());
                                if(sList.size() == 0){
                                    throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"标准不存在,请联系管理员!");
                                }
                                itemVo.setStandId(itemReqDTO.getStandId());
                            }
 
                            //选项
                            if(itemReqDTO.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
                                throw new BusinessException(E.DATA_PARAM_NULL,"审批项类型与分析人类型不一致,请重新选择!");
                            }
                            //填空
                            if(itemReqDTO.getType().equals(RuleItemTypeEnum.FILL.getCode())){
                                throw new BusinessException(E.DATA_PARAM_NULL,"分析人层级暂不支持填空,请重新选择!");
                            }
                            //填空暂不操作
 
                        }else{//非分析人
                            //验证安全措施是否为空
                            if(null == itemReqDTO.getMeasureId()){
                                throw new BusinessException(E.DATA_PARAM_NULL,"多人或单人审批层级未关联安全措施项!");
                            }
                            //验证安全措施类型是否存在
                            List<ApprovalRuleItemMeasureDO> mList = measureList.stream().filter(measure -> measure.getId().equals(itemReqDTO.getMeasureId())).collect(Collectors.toList());
                            if(mList.size() == 0){
                                throw new BusinessException(E.DATA_DATABASE_NO_EXISTENT,"安全措施不存在,请联系管理员!");
                            }
                            //选项
                            if(itemReqDTO.getType().equals(RuleItemTypeEnum.OPTION.getCode())){
                                //验证审批项类型是否与安全措施一致
                                if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_CHOSE.getType())){
                                    throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
                                }
                            }
                            //填空
                            if(itemReqDTO.getType().equals(RuleItemTypeEnum.FILL.getCode())){
                                //验证审批项类型是否与安全措施一致
                                if(!mList.get(0).getType().equals(MeasureTypeEnum.TYPE_INPUT.getType())){
                                    throw new BusinessException(E.DATA_PARAM_CHECK_INVALID,"安全措施类型与审批项类型不一致!");
                                }
                            }
                            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.getRealName());
                            saveUnitItemList.add(itemVo);
                        }else{ //修改操作
                            //填充主键
                            itemVo.setId(itemReqDTO.getId());
                            itemVo.setGmtModified(LocalDateTime.now());
                            itemVo.setModifiedUid(currentUser.getUid());
                            itemVo.setModifiedUname(currentUser.getRealName());
                            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);
    }
 
 
 
    @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(null,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;
 
 
    }
}