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
package com.vci.frameworkcore.compatibility.impl;
 
import com.vci.constant.FrameWorkBtmTypeConstant;
import com.vci.corba.common.PLException;
import com.vci.corba.common.data.UserEntityInfo;
import com.vci.corba.framework.data.RoleInfo;
import com.vci.corba.omd.data.BusinessObject;
import com.vci.frameworkcore.compatibility.SmRoleQueryServiceI;
import com.vci.constant.FrameWorkBtmTypeConstant;
import com.vci.dto.SmRoleDTO;
import com.vci.frameworkcore.compatibility.SmUserQueryServiceI;
import com.vci.frameworkcore.enumpck.RoleClassifyEnum;
import com.vci.frameworkcore.enumpck.RoleControlAreaEnum;
import com.vci.model.SmRoleForPlatform1;
import com.vci.pagemodel.SmUserVO;
import com.vci.po.SmRolePO;
import com.vci.pagemodel.SmRoleVO;
import com.vci.omd.utils.ObjectTool;
import com.vci.starter.poi.bo.ReadExcelOption;
import com.vci.starter.poi.constant.ExcelLangCodeConstant;
import com.vci.starter.poi.util.ExcelUtil;
import com.vci.starter.web.constant.QueryOptionConstant;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.*;
import com.vci.starter.web.util.LangBaseUtil;
import com.vci.web.util.BeanUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.starter.web.wrapper.VciQueryWrapperForDO;
import com.vci.web.enumpck.UserTypeEnum;
import com.vci.web.service.WebBoServiceI;
import com.vci.web.util.Func;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
 
import java.io.File;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
 
/**
 * 角色的查询服务,兼容来平台,素有的接口都返回新的角色显示对象
 * @author xiejun
 * @date 2020/3/10
 */
@Service
public class SmRoleQueryServiceImpl implements SmRoleQueryServiceI {
 
    /**
     * 业务数据服务
     */
    @Autowired
    private WebBoServiceI boService;
 
    /**
     * 用户查询服务
     */
    @Autowired
    @Lazy
    private SmUserQueryServiceI smUserQueryServiceI;
 
    /**
     * 平台调用客户端
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
 
    private final Integer QUERY_IN_LIMIT = 1000;
 
    /**
     * 日志
     */
    private Logger logger = LoggerFactory.getLogger(getClass());
 
    /**
     * 根据角色主键获取角色的信息
     * @param conditionMap 角色主键
     * @return 角色的显示对象,如果角色不存在则返回null,不会抛出异常
     * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常
     */
    @Override
    public SmRoleVO getRoleByConditionMap(Map<String,String> conditionMap) throws VciBaseException {
        if(Func.isEmpty(conditionMap)){
            return null;
        }
        VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap, SmRoleForPlatform1.class);
        List<SmRoleForPlatform1> smRoleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
        if(Func.isNotEmpty(smRoleForPlatform1s) && Func.isNotBlank(smRoleForPlatform1s.get(0).getPluid())){
            //角色名不能重复所以直接取第一个元素
            return platformRole2SmRoleVO(smRoleForPlatform1s.get(0));
        }
        return null;
    }
 
    /**
     * 批量获取角色的信息 (根据角色主键)
     * @param roleOidCollections 角色主键的集合,可以超过1000个
     * @return 角色的显示对象,如果角色不存在则返回空的列表,不会抛出异常
     * @throws VciBaseException 参数为空或者数据库存在问题的时候会抛出异常
     */
    @Override
    public List<SmRoleVO> listRoleByRoleOids(
            Collection<String> roleOidCollections) throws VciBaseException {
        if(CollectionUtils.isEmpty(roleOidCollections)){
            return new ArrayList<>();
        }
        List<SmRoleForPlatform1> roles = new ArrayList<>();
        WebUtil.switchCollectionForOracleIn(roleOidCollections).stream().forEach(roleOids->{
            VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(null,SmRoleForPlatform1.class);
            queryWrapper.in("pluid", VciBaseUtil.toInSql(roleOids.toArray(new String[0])));
            List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
            if(!CollectionUtils.isEmpty(roleForPlatform1s)){
                roles.addAll(roleForPlatform1s);
            }
        });
        return     platformRole2SmRoleVOs(roles);
    }
 
    /**
     * 获取角色的列表,默认会以角色名升序排列,角色的编辑页面列表不要使用这个接口
     * @param queryMap 查询条件
     * @param pageHelper 分页和排序的信息,在兼容老平台的时候会自动兼容,如果属性不存在会自动忽略
     * @return 角色的显示对象列表
     * @throws VciBaseException 参数为空的时候会抛出异常
     */
    @Override
    public DataGrid<SmRoleVO> gridRoles(Map<String, String> queryMap, PageHelper pageHelper) throws VciBaseException {
        if(pageHelper == null){
            pageHelper = new PageHelper(-1);
        }
        pageHelper.addDefaultAsc("plname");
        VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class,pageHelper);
        List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
        DataGrid<SmRoleVO> dataGrid = new DataGrid<>();
        if(!CollectionUtils.isEmpty(roleForPlatform1s)){
            dataGrid.setData(platformRole2SmRoleVOs(roleForPlatform1s));
            dataGrid.setTotal(boService.countByQueryWrapper(queryWrapper,SmRoleForPlatform1.class));
        }
        return dataGrid;
    }
 
     /**
     * 根据角色主键获取角色的姓名
     * @param roleOid 角色主键
     * @return 角色姓名,如果不存在会返回null
     */
    @Override
    public String getRoleNameByRoleOid(String roleOid) {
        WebUtil.alertNotNull(roleOid,"角色主键");
        Map<String, String> conditionMap = new HashMap<>();
        conditionMap.put("pluid",roleOid);
        return getRoleByConditionMap(conditionMap).getName();
    }
 
    /**
     * 根据角色名查询角色信息
     * @param roleNameList
     * @param queryMap
     * @return
     */
    @Override
    public List<SmRoleVO> listRoleByRoleName(Collection<String> roleNameList, Map<String, String> queryMap) {
        if(Func.isEmpty(roleNameList)){
            return new ArrayList<>();
        }
        if(queryMap == null){
            queryMap = new HashMap<>();
        }
        List<SmRoleForPlatform1> roles = new ArrayList<>();
        if(roleNameList.size() > QUERY_IN_LIMIT){
            Map<String, String> finalQueryMap = queryMap;
            WebUtil.switchCollectionForOracleIn(roleNameList).stream().forEach(roleNames->{
                Map<String,String> conditionMap = new HashMap<>();
                finalQueryMap.forEach((key,value)->{
                    conditionMap.put(key,value);
                });
                conditionMap.put("plname", QueryOptionConstant.IN + WebUtil.toInSql(roleNames.toArray(new String[0])));
                VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class);
                List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
                if(!CollectionUtils.isEmpty(roleForPlatform1s)){
                    roles.addAll(roleForPlatform1s);
                }
            });
        }else{
            queryMap.put("plname", QueryOptionConstant.IN + WebUtil.toInSql(roleNameList.toArray(new String[0])));
        }
        VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class);
        List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
        if(!CollectionUtils.isEmpty(roleForPlatform1s)){
            roles.addAll(roleForPlatform1s);
        }
        return platformRole2SmRoleVOs(roles);
    }
 
    /**
     * 根据用户主键获取关联的角色
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 角色的显示对象
     */
    @Override
    public List<SmRoleVO> listRoleByUserOid(String userOid,
            Map<String, String> queryMap) {
        return listRoleByUserOid(userOid,queryMap,false);
    }
 
    /**
     * 使用用户主键查询角色
     * @param userOid 用户主键
     * @param queryMap 查询条件
     * @param notIn 是否为不包含
     * @return 角色的显示对象
     */
    private List<SmRoleVO> listRoleByUserOid(String userOid,Map<String,String> queryMap,boolean notIn){
        if(StringUtils.isBlank(userOid)){
            return new ArrayList<>();
        }
        if(queryMap == null){
            queryMap = new HashMap<>();
        }
        List<SmRoleForPlatform1> roles = new ArrayList<>();
        if(userOid.contains(",")){
            Map<String, String> finalQueryMap = queryMap;
            WebUtil.switchCollectionForOracleIn(WebUtil.str2List(userOid)).stream().forEach(userOids->{
                Map<String,String> conditionMap = new HashMap<>();
                finalQueryMap.forEach((key,value)->{
                    conditionMap.put(key,value);
                });
                conditionMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")");
                VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class);
                List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
                if(!CollectionUtils.isEmpty(roleForPlatform1s)){
                    roles.addAll(roleForPlatform1s);
                }
            });
        }else {
            queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid ='" + userOid.trim() + "'");
        }
        VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class);
        List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
        if(!CollectionUtils.isEmpty(roleForPlatform1s)){
            roles.addAll(roleForPlatform1s);
        }
        return platformRole2SmRoleVOs(roles);
    }
 
      /**
     * 根据用户主键获取关联的角色
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序
     * @return 角色的显示对象
     */
    @Override
    public DataGrid<SmRoleVO> gridRoleByUserOid(String userOid,
            Map<String, String> queryMap, PageHelper pageHelper) {
        return gridRoleByUserOid(userOid,queryMap,pageHelper,false);
    }
 
    /**
     * 使用用户查询角色列表
     * @param userOid 用户主键
     * @param queryMap 查询条件
     * @param pageHelper 分页对象
     * @param notIn 不包含
     * @return 列表数据
     */
    private DataGrid<SmRoleVO> gridRoleByUserOid(String userOid,Map<String,String> queryMap,PageHelper pageHelper,boolean notIn){
        if(queryMap == null){
            queryMap = new HashMap<>();
        }
        if(StringUtils.isBlank(userOid)){
            return new DataGrid<>();
        }
        if(userOid.contains(",")){
            String[] userOids = userOid.trim().split(",");
            if(userOids.length>1000){
                //这个方法不支持超过1000个的用户查询
                throw new VciBaseException("这个方法不支持超过1000个用户的主键来查询");
            }
            queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid in (" + WebUtil.toInSql(userOids) + ")");
        }else {
            queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleuid from pluserrole where pluseruid ='" + userOid.trim() + "'");
        }
        return gridRoles(queryMap,pageHelper);
    }
 
     /**
     * 获取未关联某个用户的角色
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 角色的显示对象
     */
    @Override
    public List<SmRoleVO> listRoleUnInUserOid(String userOid,
            Map<String, String> queryMap) {
 
        return listRoleByUserOid(userOid,queryMap,true);
    }
 
      /**
     * 批量根据用户的主键来获取角色
     * @param userOidCollection 用户主键集合
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 角色的显示对象,key是用户主键,value是这个用户关联的角色
     */
    @Override
    public Map<String, List<SmRoleVO>> batchListRoleByUserOids(
            Collection<String> userOidCollection, Map<String, String> queryMap) {
        if(CollectionUtils.isEmpty(userOidCollection)){
            return new HashMap<>();
        }
        List<SmRoleVO> roleVOList = new ArrayList<>();
        Map<String,List<String>> userRoleOidMap = new HashMap<>();
        WebUtil.switchCollectionForOracleIn(userOidCollection).stream().forEach(userOids->{
            List<SmRoleVO> roleVOS = listRoleByUserOid(userOids.stream().collect(Collectors.joining(",")), queryMap, false);
            if(!CollectionUtils.isEmpty(roleVOS)){
                roleVOList.addAll(roleVOS);
                String sql = "select pluseruid,plroleuid from pluserrole where pluseruid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")";
                List<BusinessObject> cbos = boService.queryBySql(sql, null);
                if(!CollectionUtils.isEmpty(cbos)){
                    cbos.stream().forEach(cbo->{
                        String userOid = ObjectTool.getNewBOAttributeValue(cbo,"pluseruid");
                        List<String> roleOids = userRoleOidMap.getOrDefault(userOid,new ArrayList<>());
                        roleOids.add(ObjectTool.getNewBOAttributeValue(cbo,"plroleuid"));
                        userRoleOidMap.put(userOid,roleOids);
                    });
                }
            }
        });
        if(!CollectionUtils.isEmpty(roleVOList)){
            //这儿应该对roleVOList做一次去重处理,因为肯定会有重复的角色被查出来
            Map<String, SmRoleVO> roleVOMap = roleVOList.stream()
                    .collect(Collectors.toMap(SmRoleVO::getOid, Function.identity(), (existing, replacement) -> existing));
            //Map<String, SmRoleVO> roleVOMap = roleVOList.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
            Map<String, List<SmRoleVO>> userRoleVOMap = new HashMap<>();
            userRoleOidMap.forEach((userOid,roleOids)->{
                List<SmRoleVO> roleVOS = new ArrayList<>();
                roleOids.forEach(roleOid->{
                    if(roleVOMap.containsKey(roleOid)){
                        roleVOS.add(roleVOMap.get(roleOid));
                    }
                });
                userRoleVOMap.put(userOid,roleVOS);
            });
            return userRoleVOMap;
        }
        return new HashMap<>();
    }
 
     /**
     * 根据权限主键获取关联的角色
     * @param functionOid 权限主键
     * @param queryMap 查询条件
     * @return 角色的显示对象
     */
    @Override
    public List<SmRoleVO> listRoleByFunctionOid(String functionOid,
            Map<String, String> queryMap) {
        return listRoleByFunctionOid(functionOid,queryMap,false);
    }
 
    /**
     * 使用用户主键查询角色
     * @param functionOid 用户主键
     * @param queryMap 查询条件
     * @param notIn 是否为不包含
     * @return 角色的显示对象
     */
    private List<SmRoleVO> listRoleByFunctionOid(String functionOid,Map<String,String> queryMap,boolean notIn){
        if(StringUtils.isBlank(functionOid)){
            return new ArrayList<>();
        }
        if(queryMap == null){
            queryMap = new HashMap<>();
        }
        List<SmRoleForPlatform1> roles = new ArrayList<>();
        if(functionOid.contains(",")){
            Map<String, String> finalQueryMap = queryMap;
            WebUtil.switchCollectionForOracleIn(WebUtil.str2List(functionOid)).stream().forEach(functionOids->{
                Map<String,String> conditionMap = new HashMap<>();
                finalQueryMap.forEach((key,value)->{
                    conditionMap.put(key,value);
                });
                conditionMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid in (" + WebUtil.toInSql(functionOids.toArray(new String[0])) + ")");
                VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(conditionMap,SmRoleForPlatform1.class);
                List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
                if(!CollectionUtils.isEmpty(roleForPlatform1s)){
                    roles.addAll(roleForPlatform1s);
                }
            });
        }else {
            queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid ='" + functionOid.trim() + "'");
            VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(queryMap,SmRoleForPlatform1.class);
            List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
            if(!CollectionUtils.isEmpty(roleForPlatform1s)){
                roles.addAll(roleForPlatform1s);
            }
        }
        return platformRole2SmRoleVOs(roles);
    }
 
    /**
     * 获取未关联某个权限的角色
     * @param functionOid 权限主键
     * @param queryMap 查询条件
     * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序
     * @return 角色的显示对象
     */
    @Override
    public DataGrid<SmRoleVO> gridRoleByFunctionOid(String functionOid,
            Map<String, String> queryMap, PageHelper pageHelper) {
        return gridRoleByFunctionOid(functionOid,queryMap,pageHelper,false);
    }
 
    /**
     * 使用权限查询角色列表
     * @param functionOid 权限主键
     * @param queryMap 查询条件
     * @param pageHelper 分页对象
     * @param notIn 不包含
     * @return 列表数据
     */
    private DataGrid<SmRoleVO> gridRoleByFunctionOid(String functionOid,Map<String,String> queryMap,PageHelper pageHelper,boolean notIn){
        if(queryMap == null){
            queryMap = new HashMap<>();
        }
        if(StringUtils.isBlank(functionOid)){
            return new DataGrid<>();
        }
        if(functionOid.contains(",")){
            String[] functionOids= functionOid.trim().split(",");
            if(functionOids.length>1000){
                //这个方法不支持超过1000个的用户查询
                throw new VciBaseException("这个方法不支持超过1000个权限的主键来查询");
            }
            queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid in (" + WebUtil.toInSql(functionOids) + ")");
        }else {
            queryMap.put("pluid", notIn ? QueryOptionConstant.NOTIN : QueryOptionConstant.IN + "select plroleoid from plroleright where plfuncoid ='" + functionOid.trim() + "'");
        }
        return gridRoles(queryMap,pageHelper);
    }
 
    /**
     * 获取未关联某个权限的角色
     * @param functionOid 权限主键
     * @param queryMap 查询条件
     * @return 角色的显示对象
     */
    @Override
    public List<SmRoleVO> listRoleUnInFunctionOid(String functionOid,
            Map<String, String> queryMap) {
        return listRoleByFunctionOid(functionOid,queryMap,true);
    }
 
    /**
     * 根据权限主键获取关联的角色
     * @param functionOid 权限主键
     * @param queryMap 查询条件
     * @param pageHelper 分页和排序对象,老平台不支持使用角色编号来排序
     * @return 角色的显示对象
     */
    @Override
    public DataGrid<SmRoleVO> gridRoleUnInFunctionOid(String functionOid,
            Map<String, String> queryMap, PageHelper pageHelper) {
        return gridRoleByFunctionOid(functionOid,queryMap,pageHelper,true);
    }
 
    /**
     * 批量根据权限的主键来获取角色
     * @param functionOidCollection 权限主键集合
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 角色的显示对象,key是权限主键,value是这个权限关联的角色
     */
    @Override
    public Map<String, List<SmRoleVO>> batchListRoleByFunctionOids(
            Collection<String> functionOidCollection,
            Map<String, String> queryMap) {
        if(CollectionUtils.isEmpty(functionOidCollection)){
            return new HashMap<>();
        }
        List<SmRoleVO> roleVOList = new ArrayList<>();
        Map<String,List<String>> funcRoleOidMap = new HashMap<>();
        WebUtil.switchCollectionForOracleIn(functionOidCollection).stream().forEach(userOids->{
            List<SmRoleVO> roleVOS = listRoleByFunctionOid(userOids.stream().collect(Collectors.joining(",")), queryMap, false);
            if(!CollectionUtils.isEmpty(roleVOS)){
                roleVOList.addAll(roleVOS);
                String sql = "select plroleoid,plfuncoid from plroleright where plfuncoid in (" + WebUtil.toInSql(userOids.toArray(new String[0])) + ")";
                List<BusinessObject> cbos = boService.queryBySql(sql, null);
                if(!CollectionUtils.isEmpty(cbos)){
                    cbos.stream().forEach(cbo->{
                        String funcOid = ObjectTool.getBOAttributeValue(cbo,"plfuncoid");
                        List<String> roleOids = funcRoleOidMap.getOrDefault(funcOid,new ArrayList<>());
                        roleOids.add(ObjectTool.getBOAttributeValue(cbo,"plroleoid"));
                        funcRoleOidMap.put(funcOid,roleOids);
                    });
                }
            }
        });
        if(!CollectionUtils.isEmpty(roleVOList)){
            Map<String, SmRoleVO> roleVOMap = roleVOList.stream().collect(Collectors.toMap(s -> s.getOid(), t -> t));
            Map<String, List<SmRoleVO>> userRoleVOMap = new HashMap<>();
            funcRoleOidMap.forEach((funcOid,roleOids)->{
                List<SmRoleVO> roleVOS = new ArrayList<>();
                roleOids.forEach(roleOid->{
                    if(roleVOMap.containsKey(roleOid)){
                        roleVOS.add(roleVOMap.get(roleOid));
                    }
                });
                userRoleVOMap.put(funcOid,roleVOS);
            });
            return userRoleVOMap;
        }
        return new HashMap<>();
    }
 
     /**
     * 参照角色的列表,超管查全部,其他的都是查普通的(不包含三员)
     * @param queryMap 查询条件
     * @param pageHelper 分页和排序的信息,在兼容老平台的时候会自动兼容,如果属性不存在会自动忽略
     * @return 角色的显示对象列表
     * @throws VciBaseException 参数为空的时候会抛出异常
     */
    @Override
    public DataGrid<SmRoleVO> refGridRoles(Map<String, String> queryMap,
            PageHelper pageHelper) throws VciBaseException {
        //老平台没有生效状态
        //根据当前用户来决定能查那些角色:普通用户只能查普通角色,管理员和普通用户只能查普通角色
        String usertype = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUsertype();
        if(!UserTypeEnum.SUPPER_ADMIN.getValue().equals(usertype)){
            queryMap.put("pltype","2");
        }
        return gridRoles(queryMap, pageHelper);
    }
 
      /**
     * 角色的树形展示,常用于角色选择用户的页面使用
     * @param treeQueryObject 树查询对象
     * @return 树的节点
     * @throws VciBaseException 参数为空的时候会抛出异常
     */
    @Override
    public List<Tree> refTreeRoles(TreeQueryObject treeQueryObject)
            throws VciBaseException {
        VciQueryWrapperForDO queryWrapper = new VciQueryWrapperForDO(treeQueryObject.getConditionMap(),SmRoleForPlatform1.class);
        queryWrapper.eq("pltype","2");
        List<SmRoleForPlatform1> roleForPlatform1s = boService.selectByQueryWrapper(queryWrapper, SmRoleForPlatform1.class);
        List<SmRoleVO> roleVOList = platformRole2SmRoleVOs(roleForPlatform1s);
        List<Tree> list=new ArrayList<Tree>();
        if(!CollectionUtils.isEmpty(roleVOList)){
            for (SmRoleVO smRoleVO : roleVOList) {
                 String name=smRoleVO.getName();
                 Tree tree=new Tree(smRoleVO.getOid(),name);
                 tree.setShowCheckbox(treeQueryObject.isShowCheckBox());
                 try {
                     tree.setAttributes(WebUtil.objectToMapString(smRoleVO));
                 } catch (Throwable e) {
                     e.printStackTrace();
                 }
                 list.add(tree);
            }
        }
        return list;
    }
 
    /**
     * 成员管理分配角色调用,保存用户角色关联关系
     * @param userOids
     * @param roleIds
     * @return
     */
    @Override
    public boolean saveRights(String[] userOids, String[] roleIds) throws PLException {
        VciBaseUtil.alertNotNull(userOids,"用户主键");
        //roleIds允许为空,为空时清空当前用户的权限
        UserEntityInfo userEntityInfo = new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(),null);
        return platformClientUtil.getFrameworkService().saveRights(roleIds,userOids,userEntityInfo);
    }
 
    /**
     * 角色管理分配成员接口调用
     * @param userOids
     * @param roleId
     * @return
     * @throws PLException
     */
    @Override
    public boolean saveRight(String[] userOids, String roleId) throws PLException {
        VciBaseUtil.alertNotNull(roleId,"角色主键");
        //roleIds允许为空,为空时清空当前用户的权限
        UserEntityInfo userEntityInfo = new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(),null);
        return platformClientUtil.getFrameworkService().saveRight(roleId,userOids,userEntityInfo);
    }
 
    /**
     * 新增角色
     * @param smRoleDTO
     * @return true成功,false失败
     */
    @Override
    public boolean addRole(SmRoleDTO smRoleDTO) throws PLException {
        //判空
        VciBaseUtil.alertNotNull(
                smRoleDTO,"添加的角色对象",
                smRoleDTO.getName(),"角色名");
        //角色名判重
        Map<String, String> conditionMap = new HashMap<>();
        conditionMap.put("plname",smRoleDTO.getName());
        SmRoleVO dbSmRoleVO = getRoleByConditionMap(conditionMap);
        if(Func.isNotEmpty(dbSmRoleVO)){
            throw new VciBaseException("该角色名称已经存在,请修改!");
        }
        SessionInfo loginUser = WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
        String userId = loginUser.getUserId();
        //角色类型,这里设置了会在changeRoleObjectToRoleInfo中转换
        if(UserTypeEnum.SUPPER_ADMIN.getValue().equals(loginUser.getUsertype())){
            smRoleDTO.setRoleClassify(RoleClassifyEnum.MANAGE.getValue());
        }else {
            smRoleDTO.setRoleClassify(RoleClassifyEnum.BUSINESS.getValue());
        }
        //生成存储的corba对象
        Date date = new Date();
        smRoleDTO.setCreateTime(date);
        smRoleDTO.setCreator(userId);
        smRoleDTO.setLastModifier(userId);
        RoleInfo roleInfo = changeSmRoleDTOToRoleInfo(smRoleDTO);
        UserEntityInfo userEntityInfo = new UserEntityInfo(userId, "");
        String oid = platformClientUtil.getFrameworkService().saveRole(roleInfo, userEntityInfo,null);
        if (Func.isEmpty(oid)) {
            return false;
        }
        return true;
    }
 
    /**
     * 修改角色
     * @param smRoleDTO
     * @return
     */
    @Override
    public boolean updateRole(SmRoleDTO smRoleDTO) throws PLException {
        //判空
        VciBaseUtil.alertNotNull(
                smRoleDTO,"添加的角色对象",
                smRoleDTO.getName(),"角色名");
        //根据角色主键查询,确保修改的角色存在
        Map<String, String> conditionMap = new HashMap<>();
        conditionMap.put("pluid",smRoleDTO.getOid());
        SmRoleVO dbSmRoleVO = getRoleByConditionMap(conditionMap);
        //根据主键没查询到了用户
        if(Func.isEmpty(dbSmRoleVO) || Func.isBlank(dbSmRoleVO.getOid())){
            throw new PLException("500", new String[] { "当前修改的角色不存在!"});
        }
        //查询数据库中的,根据角色名查重
        conditionMap.clear();
        conditionMap.put("plname",smRoleDTO.getName());
        //排除掉当前修改的用户
        conditionMap.put("pluid",QueryOptionConstant.NOTEQUAL + smRoleDTO.getName());
        SmRoleVO reapeatSmRoleVO = getRoleByConditionMap(conditionMap);
        //说明修改为的角色名已存在,不能重复
        if(Func.isNotEmpty(reapeatSmRoleVO) && smRoleDTO.getName().equals(reapeatSmRoleVO)){
            throw new PLException("500", new String[] { "该角色名称已经存在,请修改!"});
        }
        SmRoleDTO smRoleDTO1 = new SmRoleDTO();
        BeanUtil.copy(dbSmRoleVO,smRoleDTO1);
        smRoleDTO1.setName(smRoleDTO.getName());
        smRoleDTO1.setDescription(smRoleDTO.getDescription());
        String loginUserId = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId();; //"developer"
        smRoleDTO.setLastModifier(loginUserId);
        RoleInfo roleInfo = changeSmRoleDTOToRoleInfo(smRoleDTO1);
        boolean updateBoolean = platformClientUtil.getFrameworkService().updateRole(roleInfo, new UserEntityInfo(loginUserId, null));
        return updateBoolean;
    }
 
    /**
     * 删除角色
     * @param ids
     * @return
     * @throws PLException
     */
    @Override
    public boolean deleteRole(String[] ids) throws PLException {
        VciBaseUtil.alertNotNull(ids,"要删除的角色主键");
        //使用主键判断角色是否被用户引用
        for (int i = 0; i < ids.length; i++) {
            String roleName = this.checkIsUsed(ids[i]);
            if(Func.isNotBlank(roleName)){
                throw new PLException("500",new String[]{"当前选中要删除的角色中存在【"+roleName+"】被引用!"});
            }
        }
        //具备连带删除的功能,如角色菜单授权表里面的授权信息
        return platformClientUtil.getFrameworkService().deleteRole(
                ids,
                new UserEntityInfo(WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUserId(), null)
        );
    }
 
    /**
     * 检查角色是否有在引用
     * @param roleOid
     * @return
     */
    private String checkIsUsed(String roleOid){
        List<SmUserVO> smUserVOS = smUserQueryServiceI.listUserByRoleOid(roleOid, null);
        if (Func.isNotEmpty(smUserVOS)) {
            return this.getRoleNameByRoleOid(roleOid);
        }
        return null;
    }
 
    /**
     * 导入角色
     * @param file
     * @return
     * @throws VciBaseException
     */
    @Override
    public BaseResult importRole(File file) throws Exception {
        VciBaseUtil.alertNotNull(file,"excel文件");
        if(!file.exists()){
            throw new VciBaseException("导入的excel文件不存在,{0}",new String[]{file.getPath()});
        }
        try{
            //1、读取excel中的数据,组成对象
            ReadExcelOption excelOption = new ReadExcelOption();
            //读取excel转换为po对象
            List<SmRolePO> poList = ExcelUtil.readDataObjectFromExcel(file, SmRolePO.class,excelOption,(value, po, fieldName)->{});
            //去除都是空的情况
            if(CollectionUtils.isEmpty(poList)){
                return BaseResult.fail(ExcelLangCodeConstant.IMPORT_CONTENT_NULL,new String[]{});
            }
            //数据库查询是否有已存在的角色名,方便后续做判重处理
            List<SmRoleVO> smRoleVOList = this.listRoleByRoleName(poList.stream().map(SmRolePO::getName).collect(Collectors.toSet()),null);
            List<String> repeatRoleName = new ArrayList<>();
            if(Func.isNotEmpty(smRoleVOList)){
                repeatRoleName = smRoleVOList.stream().map(SmRoleVO::getName).collect(Collectors.toList());
            }
            //当前excel中是否重复用的判重Map:(key:账号,value:行号)
            Map<String, String> excelReapeat = new HashMap<>();
            //判断必填属性是否为空,角色是否已存在,以及表格内是否重复
            List<String> finalRepeatroleName = repeatRoleName;
            poList.stream().forEach(smRolePO -> {
                //先对必填属性判空处理
                if(Func.isBlank(smRolePO.getName())){
                    throw new VciBaseException("第【"+smRolePO.getRowIndex()+"】行,rolenameerror");
                }else if(finalRepeatroleName.contains(smRolePO.getName())){//判断角色名是否与库中重复
                    throw new VciBaseException("第【"+smRolePO.getRowIndex()+"】行,角色名在系统中已经存在,请修改!");
                }else if(excelReapeat.containsKey(smRolePO.getName())){//表格中判重
                    throw new VciBaseException("第【"+excelReapeat.get(smRolePO.getName())+"】行和第【"+smRolePO.getRowIndex()+"】行数据,角色名重复");
                }
                excelReapeat.put(smRolePO.getName(),smRolePO.getRowIndex());
            });
            //保存逻辑
            poList.stream().forEach(smRolePO->{
                try {
                    SmRoleDTO smRoleDTO = new SmRoleDTO();
                    BeanUtil.copy(smRolePO,smRoleDTO);
                    SessionInfo loginUser = WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
                    String userId = loginUser.getUserId();
                    //角色类型,这里设置了会在changeRoleObjectToRoleInfo中转换
                    if(UserTypeEnum.SUPPER_ADMIN.getValue().equals("0")){
                        smRoleDTO.setRoleClassify(RoleClassifyEnum.MANAGE.getValue());
                    }else {
                        smRoleDTO.setRoleClassify(RoleClassifyEnum.BUSINESS.getValue());
                    }
                    //生成存储的corba对象
                    Date date = new Date();
                    smRoleDTO.setCreateTime(date);
                    smRoleDTO.setCreator(userId);
                    smRoleDTO.setLastModifier(userId);
                    RoleInfo roleInfo = changeSmRoleDTOToRoleInfo(smRoleDTO);
                    UserEntityInfo userEntityInfo = new UserEntityInfo(userId, "");
                    //执行保存
                    platformClientUtil.getFrameworkService().saveRole(roleInfo, userEntityInfo);
                } catch (PLException e) {
                    e.printStackTrace();
                    throw new VciBaseException("执行到第【"+smRolePO.getRowIndex()+"】行保存逻辑时出现错误,原因:" + VciBaseUtil.getExceptionMessage(e));
                }
            });
        }catch (Exception e){
            if(logger.isErrorEnabled()){
                logger.error("读取excel内容时或保存角色信息时出现了错误,具体原因:",e.getMessage());
            }
            e.printStackTrace();
            return BaseResult.fail(LangBaseUtil.getErrorMsg(e),new String[]{},e);
        }
        return BaseResult.success("角色导入成功!");
    }
 
    /**
     * 角色DTO对象转corba对象
     * @param role
     * @return
     */
    public RoleInfo changeSmRoleDTOToRoleInfo(SmRoleDTO role) {
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.id = role.getOid() == null ? "" : role.getOid();
        roleInfo.name = role.getName();
        roleInfo.description = role.getDescription() == null ? "" : role.getDescription();
        //管理员角色默认为1,普通角色默认为2,但是都需要设置小一个,因为后台判断当type<2时会拿type+1
        if(RoleClassifyEnum.MANAGE.getValue().equals(role.getRoleClassify())){
            roleInfo.type = 0;
        }else{
            roleInfo.type = 1;
        }
        roleInfo.createTime = role.getCreateTime().getTime();
        roleInfo.createUser = role.getCreator() == null ? "" : role.getCreator();
        roleInfo.updateTime = System.currentTimeMillis();
        roleInfo.updateUser = role.getLastModifier() == null ? "" : role.getLastModifier();
        return roleInfo;
    }
 
    /**
     * 角色的转换,默认全部是bs控制的角色
     * @param roleForPlatform1List 原平台的角色
     * @return 角色的显示对象
     */
    private List<SmRoleVO> platformRole2SmRoleVOs(List<SmRoleForPlatform1> roleForPlatform1List){
        List<SmRoleVO> roleVOList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(roleForPlatform1List)){
            roleForPlatform1List.stream().forEach(s -> {
                roleVOList.add(platformRole2SmRoleVO(s));
            });
        }
        return roleVOList;
    }
 
    /**
     * 角色的转换
     * @param roleForPlatform1 原平台的角色
     * @return 新的角色对象
     */
    private SmRoleVO platformRole2SmRoleVO(SmRoleForPlatform1 roleForPlatform1){
        SmRoleVO roleVO = new SmRoleVO();
        roleVO.setOid(roleForPlatform1.getPluid());
        roleVO.setId("");
        roleVO.setName(roleForPlatform1.getPlname());
        if(roleForPlatform1.getPltype() == 1){
            roleVO.setRoleClassify(RoleClassifyEnum.MANAGE.getValue());
        }else{
            roleVO.setRoleClassify(RoleClassifyEnum.BUSINESS.getValue());
        }
        roleVO.setRoleClassifyText(RoleClassifyEnum.getTextByValue(roleVO.getRoleClassify()));
        roleVO.setRoleControlArea(RoleControlAreaEnum.BS.getValue());
        roleVO.setRoleControlAreaText(RoleControlAreaEnum.BS.getText());
        roleVO.setDescription(roleForPlatform1.getPldesc());
        roleVO.setCreateTime(new Date(roleForPlatform1.getPlcreatetime()));
        roleVO.setCreator(roleForPlatform1.getPlcreateuser());
        roleVO.setLastModifier(roleForPlatform1.getPlupdateuser());
        roleVO.setLastModifyTime(new Date(roleForPlatform1.getPlupdatetime()));
        roleVO.setBtmname(FrameWorkBtmTypeConstant.SM_ROLE_BTM_TYPE);
        roleVO.setOwner(roleVO.getCreator());
        roleVO.setTs(roleVO.getLastModifyTime());
        return roleVO;
    }
 
}