kimi
2020-05-27 c007f0ca1785db093d48f4846cda82fe8e955765
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
package com.highdatas.mdm.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.highdatas.mdm.controller.MasterAuthorController;
import com.highdatas.mdm.entity.Character;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.mapper.MasterAuthorMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.ActivitiStatus;
import com.highdatas.mdm.pojo.MasterAuthorType;
import com.highdatas.mdm.pojo.Page;
import com.highdatas.mdm.pojo.kettle.UnBigDataDataSourceInfo;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.ContentBuilder;
import com.highdatas.mdm.util.DbUtils;
import com.highdatas.mdm.util.RedisClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kimi
 * @since 2020-03-23
 */
@Service
@Slf4j
public class MasterAuthorServiceImpl extends ServiceImpl<MasterAuthorMapper, MasterAuthor> implements IMasterAuthorService {
    public static final String characterId = "characterId";
    @Autowired
    IMasterAuthorDetailService authorDetailService;
    @Autowired
    ISysMenuService menuService;
    @Autowired
    ISysFieldService fieldService;
    @Autowired
    IMaintainFieldService maintainFieldService;
    @Autowired
    IMaintainService maintainService;
    @Autowired
    IFlowsService flowsService;
    @Autowired
    UnBigDataDataSourceInfo unBigDataDataSourceInfo;
    @Autowired
    IMenuMappingService menuMappingService;
    @Autowired
    IMaintainDetailService maintainDetailService;
    @Autowired
    MasterDataService masterDataService;
    @Autowired
    TableInfoMapper tableInfoMapper;
    @Autowired
    RedisClient redisClient;
    /**
     *
     * @description:  根据权限获取分页对象
     * @param user 用户
     * @param masterAuthor 权限对象
     * @param maintain 数据版本
     * @return: 获取分页对象
     *
     */
    @Override
    public Page getInitPageInfo(MasterAuthor masterAuthor, Maintain maintain, TUser user, boolean getIncrement) {
        String realRedisKey = RedisClient.getRealRedisKey(user.getUserId() + "-" + maintain.getId() + "-" + getIncrement);
        Object redisValObj = redisClient.getRedisValObj(realRedisKey);
        if (redisValObj != null) {
            JSONObject object = (JSONObject) redisValObj;
            Page page = JSONObject.parseObject(object.toJSONString(), Page.class);
            return page;
        }
        Page page = new Page(0);
        List<TableSchemaResult> tableField = tableInfoMapper.getTableField(masterAuthor.getTableName());
 
        List<SysField> fieldByMaintain = fieldService.getFieldByMaintain(maintain.getId());
        List<String> authorFieldList = fieldByMaintain.stream().map(sysField -> sysField.getField()).collect(Collectors.toList());
        int totalLength = 0;
        for (TableSchemaResult tableSchemaResult : tableField) {
            String fieldName = tableSchemaResult.getFieldName();
            if (!authorFieldList.contains(fieldName)) {
                continue;
            }
            int length = tableSchemaResult.getLength();
            totalLength += length;
        }
 
        int pageSize = Constant.MaxDispenseSize / totalLength;
        page.setPageSize(pageSize);
        long totalCnt;
        if (getIncrement) {
            totalCnt = maintainDetailService.selectCount(new EntityWrapper<MaintainDetail>().eq(Constant.PARENT_ID, maintain.getId()));
        }else {
            String filter = getFilter(user, maintain.getId());
            totalCnt = masterDataService.getCountByVersion(user, maintain.getTableName(), filter, maintain.getVersion(), false);
        }
        page.setRecordCount(Long.valueOf(totalCnt).intValue());
        if (totalCnt == 0) {
            page.setRecordCount(0);
            redisClient.putRedisValObj(realRedisKey, page);
            return page;
        }
        int pages;
        if (totalCnt % pageSize == 0) {
            pages = Long.valueOf(totalCnt/pageSize).intValue();
        }else  {
            pages =  (Long.valueOf(totalCnt/pageSize).intValue())  + 1;
        }
        page.setPages(pages);
 
        redisClient.putRedisValObj(realRedisKey, page);
        return page;
    }
    /**
     *
     * @description:  根据角色id获取混合后的权限
     * @param roleIds 角色id
     * @return: 混合后的权限
     *
     */
    @Override
    public HashMap<String, MasterAuthor> merageRoleAuthor(Set<String> roleIds) {
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIds));
        HashMap<String, MasterAuthor> resultMap = new HashMap<>();
 
        for (MasterAuthor masterAuthor : masterAuthors) {
            List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().eq(Constant.PARENT_ID, masterAuthor.getId()));
            masterAuthor.setFields(masterAuthorDetails);
 
            String key = DbUtils.getFieldRedisKey(masterAuthor.getTableName(), masterAuthor.getMaintainFieldId());
            MasterAuthor preMerageMasterAuthor = resultMap.get(key);
            if (preMerageMasterAuthor == null) {
                resultMap.put(key, masterAuthor);
            }
            else {
                preMerageMasterAuthor = merage(preMerageMasterAuthor, masterAuthor);
                resultMap.put(key, preMerageMasterAuthor);
            }
        }
        return resultMap;
    }
    /**
     *
     * @description:  根据用户获取有权限的主题
     * @param character 用户
     * @return: 主题列表
     *
     */
    @Override
    public List<SysMenu> getMenu(Character character) {
        return getMenu(character, false);
    }
    /**
     *
     * @description:  根据用户获取有权限的主题 不包括父主题
     * @param character 用户
     * @return: 不包括夫主题的主题列表
     *
     */
    @Override
    public List<SysMenu> getMenuUnParent(Character character) {
        return getMenuUnParent(character, false);
    }
 
    /**
     *
     * @description:  根据用户获取有权限的主题 不包括父主题
     * @param character 用户
     * @param isTableMenu 是否已存在表的主题
     * @return: 不包括夫主题的主题列表
     *
     */
    @Override
    public List<SysMenu> getMenuUnParent(Character character, boolean isTableMenu) {
        String key = DbUtils.StrJoinLink(Constant.UnderLine,character.getId(),"menuUnParent",  String.valueOf(isTableMenu));
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null) {
            List<SysMenu> result = new ArrayList<>();
            for (Object o : redisList) {
                JSONObject object = (JSONObject) o;
                SysMenu menu = JSONObject.parseObject(object.toJSONString(), SysMenu.class);
                result.add(menu);
            }
            return result;
        }
        MasterAuthorType type = character.getType();
        List<MasterAuthor> masterAuthors = null;
        switch (type){
            case role:
                masterAuthors = getRoleAuthors(character.getId(), null);
                break;
            case groupInfo:
                masterAuthors = getOneGroupAuthors(character.getId(), null);
                break;
            case user:
                masterAuthors = getUserAuthor(character.getId(), null);
        }
        if (masterAuthors == null || masterAuthors.isEmpty()) {
            return null;
        }
        List<String> menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList());
 
        if (isTableMenu) {
            List<String> tempList = new ArrayList<>();
            for (String menuId : menuIds) {
                String tableNameByMenu = menuMappingService.getTableNameByMenu(menuId);
                boolean exists = unBigDataDataSourceInfo.checkTableExists(tableNameByMenu);
                if (exists) {
                    tempList.add(menuId);
                }
            }
            menuIds = tempList;
        }
        List<SysMenu> sysMenus = menuService.selectBatchIds(menuIds);
        redisClient.putRedisList(key, (sysMenus));
        return sysMenus;
    }
    /**
     *
     * @description:  根据用户获取有权限的主题
     * @param character 用户
     * @param isTableMenu 是否为已经生成表的主题
     * @return: 主题列表
     *
     */
    @Override
    public List<SysMenu> getMenu(Character character, boolean isTableMenu) {
 
        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(),"menu", String.valueOf(isTableMenu));
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null) {
            List<SysMenu> result = new ArrayList<>();
            for (Object o : redisList) {
                JSONObject object = (JSONObject) o;
                SysMenu menu = JSONObject.parseObject(object.toJSONString(), SysMenu.class);
                result.add(menu);
            }
            return result;
        }
 
        MasterAuthorType type = character.getType();
        List<MasterAuthor> masterAuthors = null;
        switch (type){
            case role:
                masterAuthors = getRoleAuthors(character.getId(), null);
                break;
            case groupInfo:
                masterAuthors = getOneGroupAuthors(character.getId(), null);
                break;
            case user:
                masterAuthors = getUserAuthor(character.getId(), null);
        }
        if (masterAuthors == null || masterAuthors.isEmpty()) {
            return null;
        }
        List<String> menuIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getMenuId()).collect(Collectors.toList());
 
        if (isTableMenu) {
            List<String> tempList = new ArrayList<>();
            for (String menuId : menuIds) {
                String tableNameByMenu = menuMappingService.getTableNameByMenu(menuId);
                boolean exists = unBigDataDataSourceInfo.checkTableExists(tableNameByMenu);
                if (exists) {
                    tempList.add(menuId);
                }
            }
            menuIds = tempList;
        }
 
        LinkedHashSet<String> strings = new LinkedHashSet<>(menuIds);
        List<SysMenu> sysMenus = menuService.getMenuByParentId(strings);
        if (sysMenus == null) {
            return null;
        }
        redisClient.putRedisList(key, (sysMenus));
        return sysMenus;
    }
    /**
     *
     * @description:  根据用户,字段版本获取权限列表
     * @param characterId 用户id
     * @param maintainField 字段版本
     * @return: 不包括夫主题的主题列表
     *
     */
    @Override
    public List<MasterAuthor> getUserAuthor(String characterId, MaintainField maintainField) {
        return getUserAuthor(characterId, maintainField, null);
    }
    /**
     *
     * @description:  根据用户,字段版本获取权限列表
     * @param characterId 用户id
     * @param maintainField 字段版本
     * @param uuid 日志使用
     * @return: 权限列表
     *
     */
    @Override
    public List<MasterAuthor> getUserAuthor(String characterId, MaintainField maintainField, String uuid) {
        Date startDate = new Date();
        Wrapper<MasterAuthor> userWrapper = new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, characterId);
        int i = selectCount(userWrapper);
        boolean isUser;
        if (i == 0) {
            isUser = false;
        }else {
            isUser = true;
        }
        if (maintainField != null) {
            if (maintainField.getId() != null) {
                userWrapper.eq("maintain_field_id", maintainField.getId());
            }
            if (maintainField.getTableName() != null) {
                userWrapper.eq("table_name",maintainField.getTableName());
            }
        }
        List<MasterAuthor> masterAuthors = selectList(userWrapper);
        Date endDate = new Date();
        log.info(MessageFormat.format("master tag:{0}  select user author:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
        if (masterAuthors.isEmpty() && !isUser) {
            //user 获取role
            Set<String> roleIdSet = DbUtils.getRoleByUser(characterId, uuid);
            if (roleIdSet.isEmpty()) {
                return new ArrayList<>();
            }else {
                Wrapper<MasterAuthor> roleWrapper = new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).in(MasterAuthorController.character_id, roleIdSet);
                if (maintainField != null) {
                    if (!StringUtils.isEmpty(maintainField.getId())) {
                        roleWrapper.eq("maintain_field_id", maintainField.getId());
                    }
                    if (!StringUtils.isEmpty(maintainField.getTableName())) {
                        roleWrapper.eq("table_name",maintainField.getTableName());
                    }
                }
                masterAuthors = selectList(roleWrapper);
            }
 
            startDate = new Date();
            log.info(MessageFormat.format("master tag:{0}  select role author by user:{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
 
        }
        List<MasterAuthor> groupAuthors = getUserGroupAuthor(characterId, null, uuid);
 
 
        endDate = new Date();
        log.info(MessageFormat.format("master tag:{0}  merage group author:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
        masterAuthors.addAll(groupAuthors);
        return masterAuthors;
    }
    /**
     *
     * @description:  根据角色,字段版本获取权限列表
     * @param characterId 角色id
     * @param maintainField 字段版本
     * @return: 权限列表
     *
     */
    private List<MasterAuthor> getRoleAuthors(String characterId, MaintainField maintainField) {
        Wrapper<MasterAuthor> roleWrapper = new EntityWrapper<MasterAuthor>().eq(Constant.TYPE, MasterAuthorType.role).eq(MasterAuthorController.character_id, characterId);
        if (maintainField != null) {
            if (maintainField.getId() != null) {
                roleWrapper.eq("maintain_field_id", maintainField.getId());
            }
            if (maintainField.getTableName() != null) {
                roleWrapper.eq("table_name",maintainField.getTableName());
            }
 
        }
 
        return selectList(roleWrapper);
    }
 
    /**
     *
     * @description:  根据用户,字段版本获取权限列表
     * @param userId 用户id
     * @param maintainField 字段版本
     * @param uuid 日志使用
     * @return: 权限列表
     *
     */
        private List<MasterAuthor> getUserGroupAuthor(String userId, MaintainField maintainField, String uuid) {
        Set<String> groupIdList = DbUtils.getGroupByUser(userId, uuid);
        List<MasterAuthor> result = new ArrayList<>();
        if (groupIdList == null) {
            return result;
        }
 
        for (String groupId : groupIdList) {
            List<MasterAuthor> oneGroupAuthors = getOneGroupAuthors(groupId, maintainField, uuid);
            if (oneGroupAuthors == null || oneGroupAuthors.isEmpty()) {
                continue;
            }
            result.addAll(oneGroupAuthors);
        }
 
        return result;
    }
    /**
     *
     * @description:  根据用户组,字段版本获取权限列表
     * @param groupId 用户组id
     * @param maintainField 字段版本
     * @return: 权限列表
     *
     */
    @Override
    public List<MasterAuthor> getOneGroupAuthors(String groupId, MaintainField maintainField) {
        return getOneGroupAuthors(groupId, maintainField, null);
    }
    /**
     *
     * @description:  根据用户组,字段版本获取权限列表
     * @param groupId 用户组id
     * @param maintainField 字段版本
     * @param uuid 日志使用
     * @return: 权限列表
     *
     */
        @Override
    public List<MasterAuthor> getOneGroupAuthors(String groupId, MaintainField maintainField, String uuid) {
            Date startDate = new Date();
            Wrapper<MasterAuthor> userWrapper = new EntityWrapper<MasterAuthor>().eq("user_group", true).eq(Constant.TYPE, MasterAuthorType.user).eq(MasterAuthorController.character_id, groupId);
            Wrapper<MasterAuthor> roleWrapper = new EntityWrapper<MasterAuthor>().eq("user_group", true).eq(Constant.TYPE, MasterAuthorType.role).eq(MasterAuthorController.character_id, groupId);
            int i = selectCount(userWrapper);
            boolean isUser;
            if (i == 0) {
                isUser = false;
            }else {
                isUser = true;
            }
            if (maintainField != null) {
            if (maintainField.getId() != null) {
                userWrapper.eq("maintain_field_id", maintainField.getId());
                roleWrapper.eq("maintain_field_id", maintainField.getId());
            }
            if (maintainField.getTableName() != null) {
                userWrapper.eq("table_name",maintainField.getTableName());
                roleWrapper.eq("table_name",maintainField.getTableName());
            }
 
        }
        List<MasterAuthor> groupAuthors = selectList(userWrapper);
            Date endDate = new Date();
            log.info(MessageFormat.format("master tag:{0}  select group author:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
            if (groupAuthors.isEmpty()  && !isUser) {
            Set<String> roleByGroup = DbUtils.getRoleByGroup(groupId, uuid);
            if(roleByGroup.isEmpty()) {
                groupAuthors = new ArrayList<>();
            }else {
                groupAuthors = selectList(roleWrapper);
            }
 
                startDate = new Date();
                log.info(MessageFormat.format("master tag:{0} select role author by group :{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
 
            }
        return groupAuthors;
    }
    /**
     *
     * @description:  根据用户,字段版本获取字段列表
     * @param character 用户
     * @param maintainId 数据版本id
     * @return: 字段列表
     *
     */
    @Override
    public List<SysField> getField(Character character, String maintainId) {
        return getField(character, maintainId, null);
    }
    /**
     *
     * @description:  根据用户,字段版本获取字段列表
     * @param character 用户
     * @param maintainId 数据版本id
     * @param uuid 日志使用
     * @return: 字段列表
     *
     */
    @Override
    public List<SysField> getField(Character character, String maintainId, String uuid) {
        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(),"field", maintainId);
        List<Object> redisList = redisClient.getRedisList(key);
        if (redisList != null) {
            List<SysField> result = new ArrayList<>();
            for (Object o : redisList) {
                JSONObject object = (JSONObject) o;
                SysField sysField = JSONObject.parseObject(object.toJSONString(), SysField.class);
                result.add(sysField);
            }
            return result;
        }
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null){
            return new ArrayList<>();
        }
        Date startDate = new Date();
        boolean isAll = checkUnFilterAuthor(character, maintain.getTableName(), uuid);
        Date endDate = new Date();
        log.info(MessageFormat.format("master tag:{0} field check isAll:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
        if (isAll) {
            List<SysField> total = fieldService.getFieldByMaintain(maintainId);
            redisClient.putRedisList(key, (total));
            return total;
        }
 
        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
        if (maintainField == null){
            return new ArrayList<>();
        }
        String maintainFieldId = maintainField.getId();
 
        MasterAuthorType type = character.getType();
        List<MasterAuthor> masterAuthors = null;
        switch (type){
            case role:
                masterAuthors = getRoleAuthors(character.getId(), maintainField);
                break;
            case groupInfo:
                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
                break;
            case user:
                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
        }
        startDate = new Date();
        log.info(MessageFormat.format("master tag:{0} field get author:{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
 
        if (masterAuthors == null || masterAuthors.isEmpty()) {
            return null;
        }
 
        List<String> authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList());
        List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().in(Constant.PARENT_ID, authorIds));
        Set<String> codes = masterAuthorDetails.stream().map(masterAuthorDetail -> masterAuthorDetail.getField()).collect(Collectors.toSet());
        if (codes.isEmpty()) {
            return new ArrayList<>();
        }
        Wrapper<SysField> wrapper = new EntityWrapper<SysField>().in(Constant.FIELD, codes).eq("table_name", maintain.getTableName());
 
        if (!maintainFieldId.equalsIgnoreCase(Constant.Default)) {
            wrapper.eq("maintain_field_id", maintainFieldId);
        }else {
            wrapper.isNull("maintain_field_id");
        }
        wrapper.orderBy("order_no");
 
        List<SysField> fieldList = fieldService.selectList(wrapper);
        endDate = new Date();
        log.info(MessageFormat.format("master tag:{0} field get field:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
        redisClient.putRedisList(key, ((fieldList)));
        return fieldList;
    }
    /**
     *
     * @description:  检测是否需要筛选数据
     * @param character 用户
     * @param tableName 表名
     * @param uuid 日志使用
     * @return: 是否需要筛选数据
     *
     */
    private boolean checkUnFilterAuthor(Character character, String tableName, String uuid) {
        String key = DbUtils.StrJoinLink(Constant.UnderLine,  character.getId(),"checkUnFilterAuthor", tableName);
        String redisVal = redisClient.getRedisVal(key);
        if (!StringUtils.isEmpty(redisVal)) {
            return Boolean.valueOf(redisVal);
        }
        MasterAuthorType type = character.getType();
        List<MasterAuthor> masterAuthors = null;
        MaintainField maintainField = new MaintainField().setTableName(tableName);
        switch (type){
            case role:
                masterAuthors = getRoleAuthors(character.getId(), maintainField);
                break;
            case groupInfo:
                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
                break;
            case user:
                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
        }
        if (masterAuthors.isEmpty()) {
            return false;
        }
        long isAll = masterAuthors.stream()
                .filter(masterAuthor -> !StringUtils.isEmpty(masterAuthor.getMaintainFieldId()))
                .filter(masterAuthor -> masterAuthor.getMaintainFieldId().equalsIgnoreCase(Constant.All)).count();
        if (isAll > 0) {
            redisClient.putRedisVal(key, "true");
            return true;
        }else {
            redisClient.putRedisVal(key, "false");
            return false;
        }
 
    }
    /**
     *
     * @description:  检验用户是否拥有数据版本的权限
     * @param character 用户
     * @param maintainId 数据版本id
     * @return: 是否有权限
     *
     */
    @Override
    public boolean checkMaintainAuthor(Character character, String maintainId) {
        return checkMaintainAuthor(character, maintainId, null);
    }
    @Override
    public boolean checkMaintainAuthor(Character character, String maintainId, String uuid) {
        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(), "checkMaintainAuthor",  maintainId);
        String redisVal = redisClient.getRedisVal(key);
        if (!StringUtils.isEmpty(redisVal)) {
            return Boolean.valueOf(redisVal);
        }
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null) {
            return false;
        }
        int checked = 0;
        boolean isAll = checkUnFilterAuthor(character, maintain.getTableName(), uuid);
        if (isAll){
            return true;
        }
        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
        if (maintainField == null) {
            return false;
        }
        MasterAuthorType type = character.getType();
        List<MasterAuthor> masterAuthors = null;
        switch (type){
            case role:
                masterAuthors = getRoleAuthors(character.getId(), maintainField);
                break;
            case groupInfo:
                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
                break;
            case user:
                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
        }
        if (masterAuthors.isEmpty()) {
            redisClient.putRedisVal(key, "false");
            return false;
        }else {
            redisClient.putRedisVal(key, "true");
            return true;
        }
    }
    /**
     *
     * @description:  根据用户,字段版本获取筛选条件
     * @param character 用户
     * @param maintainId 数据版本id
     * @return: 筛选条件
     *
     */
    @Override
    public String getFilter(Character character, String maintainId) {
 
        return getFilter(character, maintainId, null);
    }
    /**
     *
     * @description:  根据用户,字段版本获取筛选条件
     * @param character 用户
     * @param maintainId 数据版本id
     * @param uuid 日志使用
     * @return: 筛选条件
     *
     */
    @Override
    public String getFilter(Character character, String maintainId, String uuid) {
        String key = DbUtils.StrJoinLink(Constant.UnderLine, character.getId(),"filter",maintainId);
        String redisVal = redisClient.getRedisVal(key);
        if (!StringUtils.isEmpty(redisVal)) {
            return redisVal;
        }
        Maintain maintain = maintainService.selectById(maintainId);
        if (maintain == null){
            return Constant.WHERE_DEFAULTUN;
        }
        Date startDate = new Date();
 
        boolean isAll = checkUnFilterAuthor(character, maintain.getTableName(), uuid);
        Date endDate = new Date();
        log.info(MessageFormat.format("master tag:{0} filter check isAll:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
        if (isAll) {
            redisClient.putRedisVal(key, Constant.WHERE_DEFAULT);
            return Constant.WHERE_DEFAULT;
        }
        MaintainField maintainField = fieldService.getMaintainFieldByMaintain(maintainId);
        MasterAuthorType type = character.getType();
        List<MasterAuthor> masterAuthors = null;
        switch (type){
            case role:
                masterAuthors = getRoleAuthors(character.getId(), maintainField);
                break;
            case groupInfo:
                masterAuthors = getOneGroupAuthors(character.getId(), maintainField, uuid);
                break;
            case user:
                masterAuthors = getUserAuthor(character.getId(), maintainField, uuid);
        }
        startDate = new Date();
        log.info(MessageFormat.format("master tag:{0} filter get author:{1} ms",uuid, (startDate.getTime() - endDate.getTime())));
 
        if (masterAuthors.isEmpty()) {
            redisClient.putRedisVal(key, Constant.WHERE_DEFAULTUN);
            return Constant.WHERE_DEFAULTUN;
        }
 
        List<String> authorIds = masterAuthors.stream().map(masterAuthor -> masterAuthor.getId()).collect(Collectors.toList());
        List<MasterAuthorDetail> masterAuthorDetails = authorDetailService.selectList(new EntityWrapper<MasterAuthorDetail>().in(Constant.PARENT_ID, authorIds));
        if (masterAuthorDetails.isEmpty()) {
            redisClient.putRedisVal(key, Constant.WHERE_DEFAULTUN);
            return Constant.WHERE_DEFAULTUN;
        }
        HashMap<String, List<String>> segmentMap = new HashMap<>();
        HashMap<String, Boolean> segmentAllMap = new HashMap<>();
 
        for (MasterAuthorDetail detail : masterAuthorDetails) {
            String field = detail.getField();
            Boolean all = detail.getAll();
            if (!(segmentAllMap.containsKey(field) && segmentAllMap.get(field))) {
                segmentAllMap.put(field, all);
            }
 
            Boolean preAll = segmentAllMap.get(field);
            if (preAll) {
                if (segmentMap.containsKey(field)) {
                    segmentMap.remove(field);
                }
                continue;
            }
 
            String val = detail.getVal();
            List<String> split = DbUtils.split(val);
            segmentMap.put(field,split);
        }
        ContentBuilder builder = new ContentBuilder(Constant.AND);
        Set<String> keySet = segmentMap.keySet();
        for (String code : keySet) {
            List<String> vals = segmentMap.get(code);
            String val = vals.stream()
                    .filter(s -> !StringUtils.isEmpty(s))
                    .map(s -> DbUtils.quotedStr(s)).collect(Collectors.joining(Constant.COMMA));
            String format = MessageFormat.format(Constant.InSql, code, val);
            builder.append(format);
        }
        String filter = builder.toString();
        endDate = new Date();
        log.info(MessageFormat.format("master tag:{0} filter merage filter:{1} ms",uuid, (endDate.getTime() - startDate.getTime())));
 
        if (StringUtils.isEmpty(filter)) {
            redisClient.putRedisVal(key, Constant.WHERE_DEFAULT);
            return Constant.WHERE_DEFAULT;
        }
 
        redisClient.putRedisVal(key, filter);
        return filter;
    }
    /**
     *
     * @description:  根据用户获取最大权限的数据版本
     * @param character 用户
     * @param tableName 表名
     * @return: 最大权限的数据版本
     *
     */
    @Override
    public Maintain getMaxVersionMaintain(Character character, String tableName) {
        List<Maintain> maintainList = maintainService.selectList(new EntityWrapper<Maintain>().eq("table_name", tableName).orderBy("order_no desc"));
        for (Maintain maintain : maintainList) {
            ActivitiStatus status = flowsService.getStatusByBusinessId(maintain.getId());
            if (!ActivitiStatus.open.equals(status)) {
                continue;
            }
            boolean b = checkMaintainAuthor(character, maintain.getId());
            if (b) {
                return maintain;
            }
        }
        return null;
    }
    /**
     *
     * @description:  根据用户,字段版本获取数据版本列表
     * @param user 用户
     * @param tableName 数据版本id
     * @return: 数据版本列表
     *
     */
    @Override
    public LinkedHashSet<Maintain> getMaintainSet(String tableName, TUser user) {
        List<MasterAuthor> userAuthor = this.getUserAuthor(user.getUserId(), null);
        LinkedHashSet<Maintain> maintainSet = new LinkedHashSet<>();
        Collections.reverse(userAuthor);
 
        for (MasterAuthor masterAuthor : userAuthor) {
            String tabName = masterAuthor.getTableName();
            String maintainFieldId = masterAuthor.getMaintainFieldId();
            if (StringUtils.isEmpty(tabName) || !tableName.equalsIgnoreCase(tableName) || StringUtils.isEmpty(maintainFieldId)) {
                continue;
            }
            List<Maintain> maintainByMaintainField = maintainFieldService.getMaintainByMaintainField(maintainFieldId, tableName);
            if (maintainByMaintainField != null) {
                maintainSet.addAll(maintainByMaintainField);
            }
 
        }
        return maintainSet;
    }
    /**
     *
     * @description:  生成版本的需要检测哪些需要自动加上权限
     * @param status 流程状态
     * @param maintainId 数据版本id
     * @return:
     *
     */
    public void dealFlow(String maintainId, ActivitiStatus status) {
        Maintain maintain = maintainService.selectById(maintainId);
        MaintainField maintainFieldByMaintain = fieldService.getMaintainFieldByMaintain(maintainId);
        SysMenu menuByTableName = menuMappingService.getMenuByTableName(maintain.getTableName());
 
        List<MasterAuthor> masterAuthors = selectList(new EntityWrapper<MasterAuthor>()
                .eq("maintain_field_id", maintainFieldByMaintain.getId())
                .eq("table_name", maintain.getTableName())
                .eq("menu_id", menuByTableName.getId())
                .eq("maintain_auto", true));
        for (MasterAuthor masterAuthor : masterAuthors) {
 
        }
    }
 
    /**
     *
     * @description:  两个权限混合
     * @param preMerageMasterAuthor 混合前
     * @param masterAuthor 待混合
     * @return: 混合后的权限
     *
     */
    private MasterAuthor merage(MasterAuthor preMerageMasterAuthor, MasterAuthor masterAuthor) {
        // table name  masterField 一样 只有字段不同了
        List<MasterAuthorDetail> preFields = preMerageMasterAuthor.getFields();
        Map<String, MasterAuthorDetail> fieldMap = masterAuthor.getFields().stream().collect(Collectors.toMap(MasterAuthorDetail::getField, masterAuthorDetail -> masterAuthorDetail));
 
        HashSet<String> valSet = new HashSet<>();
        for (MasterAuthorDetail preField : preFields) {
            valSet.clear();
            String fieldId = preField.getField();
            String val = preField.getVal();
            if (val.equalsIgnoreCase(Constant.All)) {
                continue;
            }
            MasterAuthorDetail detail = fieldMap.get(fieldId);
            if (detail == null) {
                continue;
            }
            String preAddVal = detail.getVal();
            if (preAddVal.equalsIgnoreCase(Constant.All)) {
                preField.setVal(preAddVal);
                continue;
            }
 
            valSet.addAll(DbUtils.split(val));
            valSet.addAll(DbUtils.split(preAddVal));
 
            String resultVal = valSet.stream().collect(Collectors.joining(Constant.SEMICOLON));
            preField.setVal(resultVal);
        }
        return preMerageMasterAuthor;
    }
}