广州市综治平台后端
xusd
2025-06-07 36306491396230522fa20585c2621a7fc899849a
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
package cn.huge.module.ctuser.service;
 
import cn.huge.base.common.dto.SelectTermDTO;
import cn.huge.base.common.exception.MethodException;
import cn.huge.base.common.exception.ServiceException;
import cn.huge.base.common.utils.DateUtils;
import cn.huge.base.common.utils.IdUtils;
import cn.huge.base.common.utils.ObjectUtils;
import cn.huge.base.common.utils.SelectTermUtils;
import cn.huge.module.client.api.impl.UtilsClientImpl;
import cn.huge.module.constant.BaseConsts;
import cn.huge.module.ctrole.domain.po.CtRole;
import cn.huge.module.ctuser.dao.mapper.CtUserMapper;
import cn.huge.module.ctuser.domain.po.*;
import cn.huge.module.ctuser.dto.CtUserSaveDTO;
import cn.huge.module.cust.constant.RoleBaseEnum;
import cn.huge.module.cust.constant.UserBaseConsts;
import cn.huge.module.cust.dto.CtUserDTO;
import cn.huge.module.redis.constant.RedisKeyConsts;
import cn.huge.module.rsoper.domain.po.RsRole;
import cn.huge.module.rsoper.service.RsRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
 
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * @title: 客户用户表业务逻辑处理
 * @Description 客户用户表业务逻辑处理
 * @company hugeinfo
 * @author liyj
 * @Time 2024-08-19 20:04:19
 * @version 1.0.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CtUserService extends ServiceImpl<CtUserMapper, CtUser>{
 
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
 
    @Autowired
    private CtUserMapper mapper;
    @Autowired
    private CtAccountService ctAccountService;
    @Autowired
    private CtUnitService ctUnitService;
    @Autowired
    private CtUseroleService ctUseroleService;
    @Autowired
    private CtUsepostService ctUsepostService;
    @Autowired
    private CtDeptService ctDeptService;
    @Autowired
    private RsRoleService rsRoleService;
 
    @Autowired
    private UtilsClientImpl utilsClient;
 
    /**
    * 更新对象
    * @param entity 对象
    */
    public void updateCtUser(CtUser entity){
        try{
            mapper.updateCtUser(entity);
        }catch (Exception e){
            log.error("[CtUserService.updateCtUser]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.updateCtUser", e);
        }
    }
 
    /**
    * 条件更新对象
    * @param entity 对象
    * @param terms 条件
    */
    public void updateCtUserTerms(CtUser entity, Map<String, Object> terms){
        try{
            mapper.updateCtUserTerms(entity, terms);
        }catch (Exception e){
            log.error("[CtUserService.updateCtUserTerms]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.updateCtUserTerms", e);
        }
    }
 
    /**
    * 根据编号物理删除
    * @param id 查询条件集合
    */
    public void deleteCtUser(String id){
        try{
            mapper.deleteCtUser(id);
        }catch (Exception e){
            log.error("[CtUserService.deleteCtUser]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.deleteCtUser", e);
        }
    }
 
    /**
    * 按条件查询
    * @param terms 条件
    * @return List
    */
    public List<CtUser> listTerms(Map<String, Object> terms){
        return mapper.listTerms(terms);
    }
 
    /**
    * 按条件统计
    * @param terms 条件
    * @return long
    */
    public long countTerms(Map<String, Object> terms){
        return mapper.countTerms(terms);
    }
 
    /**
    * 按条件分页查询
    * @param page 分页对象
    * @param terms 条件
    * @return Page
    */
    public Page<CtUser> pageQuery(PageRequest page, Map<String, Object> terms){
        long total = mapper.countTerms(terms);
        List<CtUser> content = mapper.pageTerms(page, terms);
        return new PageImpl<CtUser>(content, page, total);
    }
 
    /**
     * 新增对象
     * @param ctUser 实体对象
     */
    public void saveToRedis(CtUser ctUser){
        try{
            mapper.insert(ctUser);
            // 清楚redis缓存
            String key = RedisKeyConsts.CTUSER + ctUser.getId();
            redisTemplate.delete(key);
            // 重新set
            redisTemplate.opsForValue().set(key, ctUser);
        }catch (Exception e){
            log.error("[CtUserService.saveCtUser]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.saveCtUser", e);
        }
    }
 
    /**
     * 更新对象
     * @param ctUser 实体对象
     */
    public void updateToRedis(CtUser ctUser){
        try{
            mapper.updateById(ctUser);
            // 清楚redis缓存
            String key = RedisKeyConsts.CTUSER + ctUser.getId();
            redisTemplate.delete(key);
            // 重新set
            redisTemplate.opsForValue().set(key, ctUser);
        }catch (Exception e){
            log.error("[CtUserService.saveCtUser]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.saveCtUser", e);
        }
    }
 
    /**
     * 根据id查询
     * @param id 编号
     * @return
     */
    public CtUser getByIdFromRedis(String id){
        String key = RedisKeyConsts.CTUSER + id;
        // 从redis获取
        CtUser ctUser = (CtUser) redisTemplate.opsForValue().get(key);
        if (ObjectUtils.isEmpty(ctUser)){
            ctUser = mapper.selectById(id);
            if (ObjectUtils.isNotEmpty(ctUser)){
                redisTemplate.opsForValue().set(key, ctUser);
            }
        }
        return ctUser;
    }
 
    /**
     * 重写根据id获取用户(id带有角色信息,如:1001&01)
     * @param userId 登录用户编号
     * @return Object
     */
    public CtUser getById(String userId) {
        String loginUserId = "";
        if (userId.indexOf(BaseConsts.AND) != -1){
            String[] userIds = userId.split(BaseConsts.AND);
            loginUserId = userIds[0];
        }else {
            loginUserId = userId;
        }
        return mapper.selectById(loginUserId);
    }
 
    public CtUser getByIdCard(String idCard) {
        return mapper.selectByIdCard(idCard);
    }
 
    /**
     * web端-获取人员信息列表查询条件
     * @param userId 当前登录用户编号
     * @return
     */
    public Map<String, Object> webListSelectTerm(String userId){
        try {
            Map<String, Object> result = Maps.newHashMap();
            // 获取当前登录用户
            CtUserDTO loginUser = this.clientGetUserAll(userId);
            // 组织条件
            List<CtUnit> ctUnits = ctUnitService.list();
            List<SelectTermDTO> unitSelectTerms = new ArrayList<>();
            for (CtUnit ctUnit : ctUnits) {
                SelectTermDTO unitSelectTerm = new SelectTermDTO();
                unitSelectTerm.setValue(ctUnit.getId());
                unitSelectTerm.setLabel(ctUnit.getUnitName());
                unitSelectTerm.setParentId(ctUnit.getParentId());
                unitSelectTerms.add(unitSelectTerm);
 
                // 部门条件
                QueryWrapper<CtDept> ctDeptQueryWrapper = new QueryWrapper<>();
                ctDeptQueryWrapper.eq("unit_id", ctUnit.getId());
                List<CtDept> ctDepts = ctDeptService.list(ctDeptQueryWrapper);
                if (CollectionUtils.isNotEmpty(ctDepts)) {
                    List<SelectTermDTO> deptSelectTerms = new ArrayList<>();
                    for (CtDept ctDept : ctDepts) {
                        SelectTermDTO deptSelectTerm = new SelectTermDTO();
                        deptSelectTerm.setValue(ctDept.getId());
                        deptSelectTerm.setLabel(ctDept.getName());
                        deptSelectTerm.setParentId(ctDept.getParentId());
                        deptSelectTerms.add(deptSelectTerm);
                    }
                    List<SelectTermDTO> depts = SelectTermUtils.createTreeByRoot(deptSelectTerms);
                    unitSelectTerm.setChainList(depts);
                }
            }
            result.put("units", SelectTermUtils.createTreeByFirst(unitSelectTerms, loginUser.getUnitId()));
 
            // 查询可选角色
            List<SelectTermDTO> roleSelectTerms = new ArrayList<>();
            List<RsRole> rsRoleList = rsRoleService.list();
            for (RsRole rsRole : rsRoleList) {
                SelectTermDTO selectTerm = new SelectTermDTO();
                selectTerm.setValue(rsRole.getId());
                selectTerm.setLabel(rsRole.getName());
                roleSelectTerms.add(selectTerm);
            }
            result.put("roles", roleSelectTerms);
            return result;
        }catch (Exception e){
            log.error("[CtUserService.webListSelectTerm]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.webListSelectTerm", e);
        }
    }
 
    /**
     * web端-人员信息管理-人员列表
     * @param page 分页对象
     * @param terms 条件
     * @return Page
     */
    public Map<String, Object> webPageQuery(int page, int size, Map<String, Object> terms, String userId){
        try {
            Map<String, Object> result = Maps.newHashMap();
            // 获取当前登录用户
            CtUserDTO loginUser = this.clientGetUserAll(userId);
            if(loginUser==null || StringUtils.isEmpty(loginUser.getId())){
                result.put("ctUserPage","非系统用户禁止获取数据");
                return result;
            }
            String roleCodes = loginUser.getRoleCodes();
            if(StringUtils.isNotEmpty(roleCodes)&&!(roleCodes.contains("22_00024-3")||roleCodes.contains("22_00024-2")))
           {
                result.put("ctUserPage","非管理员禁止获取数据");
                return result;
            }
 
            Sort sort = Sort.by(Sort.Direction.DESC, "t1.create_time");
            PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
            Page<CtUser> ctUserPage = this.pageUserAndRole(pageRequest, terms);
            QueryWrapper<CtAccount> accountQueryWrapper = new QueryWrapper<>();
            QueryWrapper<CtUserole> ctUseroleQueryWrapper = new QueryWrapper<>();
            QueryWrapper<CtUsepost> ctUsepostQueryWrapper = new QueryWrapper<>();
            for (CtUser ctUser : ctUserPage) {
                accountQueryWrapper.clear();
                // 密码
                accountQueryWrapper.eq("acc_type", UserBaseConsts.ACC_TYPE_1);
                accountQueryWrapper.eq("user_id", ctUser.getId());
                CtAccount ctAccount = ctAccountService.getOne(accountQueryWrapper);
                if (ObjectUtils.isNotEmpty(ctAccount)) {
//                    ctUser.setAcc(ctAccount.getAcc());
//                    ctUser.setCipher(ctAccount.getCipherOpen());
                }
                // 角色
                ctUseroleQueryWrapper.clear();
                ctUseroleQueryWrapper.eq("user_id", ctUser.getId());
                List<CtUserole> ctUseroleList = ctUseroleService.list(ctUseroleQueryWrapper);
                if (ObjectUtils.isNotEmpty(ctUseroleList)) {
                    for (CtUserole ctUserole : ctUseroleList) {
                        ctUserole.setRoleId(ctUserole.getRoleCode());
                    }
                    ctUser.setCtUseroleList(ctUseroleList);
                }
                // 岗位
                ctUsepostQueryWrapper.clear();
                ctUsepostQueryWrapper.eq("user_id", ctUser.getId());
                List<CtUsepost> ctUsepostList = ctUsepostService.list(ctUsepostQueryWrapper);
                if (ObjectUtils.isNotEmpty(ctUsepostList)) {
                    ctUser.setCtUsepostList(ctUsepostList);
                }
                ctUser.setMobile("");
                ctUser.setIdcard("");
            }
            result.put("ctUserPage", ctUserPage);
            terms = Maps.newHashMap();
            // 生效名额
            terms.put("status", UserBaseConsts.USER_STATUS_1);
            // todo 角色数据权限
 
            long countZzStatus = this.countTerms(terms);
            result.put("countZzStatus", countZzStatus);
            return result;
        }catch (Exception e){
            log.error("[CtUserService.webPageQuery]调用失败,异常信息:"+e, e);
            throw new ServiceException("CtUserService.webPageQuery", e);
        }
    }
 
    /**
     * web端-人员信息管理-新增/新用户信息
     * @param ctUserSaveDTO 提交用户信息
     * @param userId 当前登录用户编号
     */
    public void webSaveCtUser(CtUserSaveDTO ctUserSaveDTO, String userId) {
        try {
            // 获取当前登录用户
            CtUser loginUser = this.getById(userId);
            Date nowDate = DateUtils.getNowDate();
            // 判断是否新增
            CtUser ctUser = new CtUser();
            BeanUtils.copyProperties(ctUserSaveDTO, ctUser);
            ctUser.setUpdateTime(nowDate);
            if (IdUtils.checkNewId(ctUser.getId())) {
                ctUser.setId(utilsClient.getNewTimeId());
                ctUser.setCreateTime(nowDate);
                ctUser.setDeleteStatus(BaseConsts.DELETE_STATUS_0);
                ctUser.setJoinWay(UserBaseConsts.JOIN_WAY_1);
                ctUser.setRealStatus(UserBaseConsts.REAL_STATUS_0);
                ctUser.setStatus(UserBaseConsts.USER_STATUS_1);
                if(loginUser!=null){
                    ctUser.setCustId(loginUser.getCustId());
                }else {
                    ctUser.setCustId("-1");
                }
                mapper.insert(ctUser);
 
                // 创建账号
                CtAccount ctAccount = ctAccountService.getByAccAndType(ctUser.getAcc(), UserBaseConsts.ACC_TYPE_1);
                if (ObjectUtils.isNotEmpty(ctAccount)) {
                    throw new ServiceException("账号已存在,请重新输入!");
                } else {
                    ctAccount = new CtAccount();
                    ctAccount.setCustId(ctUser.getCustId());
                    ctAccount.setId(utilsClient.getNewTimeId());
                    ctAccount.setUserId(ctUser.getId());
                    ctAccount.setAccType(UserBaseConsts.ACC_TYPE_1);
                    ctAccount.setAcc(ctUser.getAcc());
                    ctAccount.setCipher(DigestUtils.md5DigestAsHex(ctUserSaveDTO.getCipher().getBytes()));
                    ctAccount.setCipherOpen(ctUserSaveDTO.getCipher());
                    ctAccount.setDeleteStatus(BaseConsts.DELETE_STATUS_0);
                    ctAccount.setCreateTime(nowDate);
                    ctAccount.setCipherTime(nowDate);
                    ctAccount.setUpdateTime(nowDate);
                    ctAccountService.save(ctAccount);
                }
 
                // 保存角色
                List<CtUserole> ctUseroleList = ctUserSaveDTO.getCtUseroleList();
                for (CtUserole ctUserole : ctUseroleList) {
                    ctUserole.setId(utilsClient.getNewTimeId());
                    ctUserole.setUserId(ctUser.getId());
                    RsRole rsRole = rsRoleService.getById(ctUserole.getRoleId());
                    ctUserole.setRoleId(rsRole.getId());
                    ctUserole.setRoleCode(rsRole.getId());
                    ctUserole.setCustId(ctUser.getCustId());
                    ctUserole.setCreateTime(nowDate);
                    ctUserole.setUpdateTime(nowDate);
                    ctUseroleService.save(ctUserole);
                }
 
                // 岗位
                List<CtUsepost> ctUsepostList = ctUserSaveDTO.getCtUsepostList();
                if (ObjectUtils.isNotEmpty(ctUsepostList)) {
                    for (CtUsepost ctUsepost : ctUsepostList) {
                        ctUsepost.setId(utilsClient.getNewTimeId());
                        ctUsepost.setUserId(ctUser.getId());
                        ctUsepost.setUnitId(ctUser.getUnitId());
                        ctUsepost.setDeptId(ctUser.getDeptId());
                        ctUsepost.setMatchType(UserBaseConsts.MATCH_TYPE_1);
                        ctUsepost.setCustId(ctUser.getCustId());
                        ctUsepost.setCreateTime(nowDate);
                        ctUsepost.setUpdateTime(nowDate);
                        ctUsepostService.save(ctUsepost);
                    }
                }
            } else {
                // 修改用户信息
                mapper.updateCtUser(ctUser);
 
                // 修改账号信息
                CtAccount ctAccount = ctAccountService.getByUserIdAndType(ctUser.getId(), UserBaseConsts.ACC_TYPE_1);
                if (!ctAccount.getAcc().equals(ctUser.getAcc())) {
                    CtAccount oldAccount = ctAccountService.getByAccAndType(ctUser.getAcc(), UserBaseConsts.ACC_TYPE_1);
                    if (ObjectUtils.isNotEmpty(oldAccount)) {
                        throw new MethodException("修改后的账号已存在,请重新输入!");
                    } else {
                        ctAccount.setAcc(ctUser.getAcc());
                    }
                }
                ctAccount.setCipher(DigestUtils.md5DigestAsHex(ctUserSaveDTO.getCipher().getBytes()));
                ctAccount.setCipherOpen(ctUserSaveDTO.getCipher());
                ctAccount.setCipherTime(nowDate);
                ctAccount.setUpdateTime(nowDate);
                ctAccountService.updateById(ctAccount);
 
                // 修改角色信息
                ctUseroleService.deleteByUserId(ctUser.getId());
                List<CtUserole> ctUseroleList = ctUserSaveDTO.getCtUseroleList();
                for (CtUserole ctUserole : ctUseroleList) {
                    ctUserole.setId(utilsClient.getNewTimeId());
                    ctUserole.setUserId(ctUser.getId());
                    ctUserole.setCustId(ctUser.getCustId());
                    RsRole rsRole = rsRoleService.getById(ctUserole.getRoleId());
                    ctUserole.setRoleId(rsRole.getId());
                    ctUserole.setRoleCode(rsRole.getId());
                    ctUserole.setCreateTime(nowDate);
                    ctUserole.setUpdateTime(nowDate);
                    ctUseroleService.save(ctUserole);
                }
 
                // 修改岗位信息
                ctUsepostService.deleteByUserId(ctUser.getId());
                List<CtUsepost> ctUsepostList = ctUserSaveDTO.getCtUsepostList();
                if (ObjectUtils.isNotEmpty(ctUsepostList)) {
                    for (CtUsepost ctUsepost : ctUsepostList) {
                        ctUsepost.setId(utilsClient.getNewTimeId());
                        ctUsepost.setUserId(ctUser.getId());
                        ctUsepost.setUnitId(ctUser.getUnitId());
                        ctUsepost.setDeptId(ctUser.getDeptId());
                        ctUsepost.setMatchType(UserBaseConsts.MATCH_TYPE_1);
                        ctUsepost.setCustId(ctUser.getCustId());
                        ctUsepost.setCreateTime(nowDate);
                        ctUsepost.setUpdateTime(nowDate);
                        ctUsepostService.save(ctUsepost);
                    }
                }
            }
        } catch (Exception e) {
            log.error("service方法[CtUserService.webSaveCtUser]调用异常:" + e, e);
            throw new MethodException(e.getMessage());
        }
    }
 
    /**
     * 按条件分页查询
     *
     * @param page  分页对象
     * @param terms 条件
     * @return Page
     */
    public Page<CtUser> pageUserAndRole(PageRequest page, Map<String, Object> terms) {
        long total = mapper.countQueryCtUser(terms);
        List<CtUser> content = mapper.pageQueryCtUser(page, terms);
        return new PageImpl<CtUser>(content, page, total);
    }
 
    /**
     * 获取当前登录用户,获取用户所有信息(用户信息、角色等)
     * @param userId 登录用户编号
     * @return Object
     */
    public CtUserDTO clientGetUserAll(String userId) {
        String loginUserId = "";
        String loginRoleCode = "";
        if (userId.indexOf(BaseConsts.AND) != -1){
            String[] userIds = userId.split(BaseConsts.AND);
            loginUserId = userIds[0];
            loginRoleCode = userIds[1];
        }else {
            loginUserId = userId;
        }
        // 用户信息
        CtUser ctUser = mapper.selectById(loginUserId);
        if (ObjectUtils.isNotEmpty(ctUser)){
            CtUserDTO ctUserDTO = new CtUserDTO();
            BeanUtils.copyProperties(ctUser, ctUserDTO);
            // 角色代码
            if (StringUtils.isEmpty(loginRoleCode)) {
                List<String>  roleCodeList = ctUseroleService.listRoleCode(loginUserId);
                String userRole = roleCodeList.stream().map(String::valueOf).collect(Collectors.joining(","));
                ctUserDTO.setRoleCodes(userRole);
            }else {
                ctUserDTO.setRoleCodes(loginRoleCode);
            }
            return ctUserDTO;
        }else {
            return null;
        }
    }
 
    /**
     * 批量删除
     * @param data 批量id对象
     */
    public void removeListId(List<String> data) {
        for (String id : data) {
            // 查询人员信息
            CtUser ctUser = this.getById(id);
            // 删除人员
            this.removeById(id);
            // 删除账号
            UpdateWrapper<CtAccount> ctAccountUpdateWrapper = new UpdateWrapper();
            ctAccountUpdateWrapper.eq("user_id", ctUser.getId());
            ctAccountService.remove(ctAccountUpdateWrapper);
            // 删除人员岗位
            UpdateWrapper<CtUsepost> ctUsepostUpdateWrapper = new UpdateWrapper();
            ctUsepostUpdateWrapper.eq("user_id", ctUser.getId());
            ctUsepostService.remove(ctUsepostUpdateWrapper);
            // 删除人员角色
            UpdateWrapper<CtUserole> ctUseroleUpdateWrapper = new UpdateWrapper();
            ctUseroleUpdateWrapper.eq("user_id", ctUser.getId());
            ctUseroleService.remove(ctUseroleUpdateWrapper);
            //更新部门和组织的调解专长和范围
            this.updateField("unit", ctUser.getUnitId());
            this.updateField("dept", ctUser.getDeptId());
        }
    }
 
    private void updateField(String tag, String targetId){
        QueryWrapper query = new QueryWrapper();
        if("unit".equals(tag)){
            query.eq("unit_id", targetId);
        }else if("dept".equals(tag)){
            query.eq("dept_id", targetId);
        }
        List<CtUser> ctUser1 = mapper.selectList(query);
        Set<String> setGoodField = new HashSet<>();
        Set<String> setGoodFieldName = new HashSet<>();
        Set<String> setCanField = new HashSet<>();
        Set<String> setCanFieldName = new HashSet<>();
        String goodField = new String();
        String goodFieldName = new String();
        String canField = new String();
        String canFieldName = new String();
        if(CollectionUtils.isNotEmpty(ctUser1)){
            for(CtUser ctUsers1: ctUser1){
                if (StringUtils.isNotEmpty(ctUsers1.getGoodField())) {
                    setGoodField.addAll(Stream.of(ctUsers1.getGoodField().split(",")).collect(Collectors.toSet()));
                    setGoodFieldName.addAll(Stream.of(ctUsers1.getGoodFieldName().split(",")).collect(Collectors.toSet()));
                }
                if (StringUtils.isNotEmpty(ctUsers1.getCanField())) {
                    setCanField.addAll(Stream.of(ctUsers1.getCanField().split(",")).collect(Collectors.toSet()));
                    setCanFieldName.addAll(Stream.of(ctUsers1.getCanFieldName().split(",")).collect(Collectors.toSet()));
                }
            }
            goodField = String.join(",", setGoodField);
            goodFieldName = String.join(",", setGoodFieldName);
            canField = String.join(",", setCanField);
            canFieldName = String.join(",", setCanFieldName);
        }
        if("unit".equals(tag)){
            CtUnit ctUnit = new CtUnit();
            ctUnit.setId(targetId);
            ctUnit.setGoodField(goodField);
            ctUnit.setGoodFieldName(goodFieldName);
            ctUnit.setCanField(canField);
            ctUnit.setCanFieldName(canFieldName);
            ctUnit.setUpdateTime(DateUtils.getNowDate());
            ctUnitService.updateTerms(ctUnit);
        }else if("dept".equals(tag)){
            CtDept ctDept = new CtDept();
            ctDept.setId(targetId);
            ctDept.setGoodField(goodField);
            ctDept.setGoodFieldName(goodFieldName);
            ctDept.setCanField(canField);
            ctDept.setCanFieldName(canFieldName);
            ctDept.setUpdateTime(DateUtils.getNowDate());
            ctDeptService.updateCtDept(ctDept);
        }
    }
 
    /**
     * 根据部门和角色查询
     * @param unitId 组织编号
     * @param roleCode 角色代码
     * @return List<SelectTermDTO>
     */
    public List<CtUser> listUserByDeptRoleList(String unitId, String roleCode){
        return mapper.listUserByUnitRoleList(unitId, roleCode);
    }
 
    /**
     * 选择人员
     * @param userId
     * @param roleCode
     * @return
     */
    public List<SelectTermDTO> userChoose(String userId, String roleCode) {
        try {
            // 获取当前登录用户
            CtUserDTO loginUser = clientGetUserAll(userId);
            List<SelectTermDTO> result = new ArrayList<>();
            // 人员条件
            List<CtUser> ctUsers = null;
            if (StringUtils.isNotEmpty(roleCode)){
                ctUsers = listUserByDeptRoleList(loginUser.getUnitId(), roleCode);
            }else {
                QueryWrapper<CtUser> ctUserQueryWrapper = new QueryWrapper<>();
                ctUserQueryWrapper.in("unit_id", loginUser.getUnitId()).select("id", "true_name",  "dept_id", "dept_name");
                ctUsers = this.list(ctUserQueryWrapper);
            }
            SelectTermDTO unitSelectTerm = new SelectTermDTO();
            unitSelectTerm.setValue(loginUser.getUnitId());
            unitSelectTerm.setLabel(loginUser.getUnitName());
            unitSelectTerm.setCheckable(false);
            List<SelectTermDTO> depetSelectTerms = new ArrayList<>();
            if("白云区人民法院".equals(loginUser.getUnitName())){
                if(ObjectUtils.isNotEmpty(ctUsers)){
                    for (CtUser ctUser : ctUsers) {
                        if(ObjectUtils.isEmpty(ctUser.getDeptId())){
                            ctUser.setDeptId("0");
                            ctUser.setDeptName("无部门");
                        }
                    }
                    Map<String, List<CtUser>> collect = ctUsers.stream().collect(Collectors.groupingBy(CtUser::getDeptId));
                    for (String deptId : collect.keySet()) {
                        List<CtUser> ctUsers1 = collect.get(deptId);
                        CtUser depet = ctUsers1.get(0);
                        SelectTermDTO depetSelectTerm = new SelectTermDTO();
                        depetSelectTerm.setParentId(loginUser.getUnitId());
                        depetSelectTerm.setValue(depet.getDeptId());
                        depetSelectTerm.setCheckable(false);
                        depetSelectTerm.setLabel(depet.getDeptName());
                        List<SelectTermDTO> userSelectTerms = new ArrayList<>();
                        for (CtUser ctUser : ctUsers1) {
                            SelectTermDTO userSelectTerm = new SelectTermDTO();
                            userSelectTerm.setValue(ctUser.getId());
                            userSelectTerm.setCheckable(true);
                            userSelectTerm.setLabel(ctUser.getTrueName());
                            userSelectTerm.setParentId(depet.getDeptId());
                            userSelectTerms.add(userSelectTerm);
                        }
                        depetSelectTerm.setChildren(userSelectTerms);
                        depetSelectTerms.add(depetSelectTerm);
                    }
                }
            }else{
                for(CtUser ctUser : ctUsers){
                    SelectTermDTO userSelectTerm = new SelectTermDTO();
                    userSelectTerm.setValue(ctUser.getId());
                    userSelectTerm.setCheckable(true);
                    userSelectTerm.setLabel(ctUser.getTrueName());
                    depetSelectTerms.add(userSelectTerm);
                }
            }
            unitSelectTerm.setChildren(depetSelectTerms);
            result.add(unitSelectTerm);
            return result;
        }catch (Exception e) {
            log.error("[CtUserService.userChoose]调用失败,异常信息:" + e, e);
            throw new ServiceException("CtUserService.userChoose", e);
        }
    }
}