heheng
2025-01-13 a27162cb82ef0cabf9b43cbfd1f3eb8c177d1e14
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
package com.gkhy.labRiskManage.domain.account.service.impl;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gkhy.labRiskManage.api.controller.account.query.UserQuery;
import com.gkhy.labRiskManage.commons.domain.Result;
import com.gkhy.labRiskManage.commons.domain.SearchResult;
import com.gkhy.labRiskManage.commons.enums.ResultCode;
import com.gkhy.labRiskManage.commons.enums.SystemCacheKeyEnum;
import com.gkhy.labRiskManage.commons.exception.BusinessException;
import com.gkhy.labRiskManage.commons.model.PageQuery;
import com.gkhy.labRiskManage.commons.utils.BeanCopyUtils;
import com.gkhy.labRiskManage.domain.account.converter.UserInfoDomainConverter;
import com.gkhy.labRiskManage.domain.account.entity.SysUserIdentityBind;
import com.gkhy.labRiskManage.domain.account.entity.SysUserRoleBind;
import com.gkhy.labRiskManage.domain.account.entity.User;
import com.gkhy.labRiskManage.domain.account.enums.IdentityStatusEnum;
import com.gkhy.labRiskManage.domain.account.enums.UserIdTypeEnum;
import com.gkhy.labRiskManage.domain.account.enums.UserStatusEnum;
import com.gkhy.labRiskManage.domain.account.model.bo.CreateUserBO;
import com.gkhy.labRiskManage.domain.account.model.bo.UpdateUserBO;
import com.gkhy.labRiskManage.domain.account.model.dto.SysDepartmentDomainDTO;
import com.gkhy.labRiskManage.domain.account.repository.jpa.UserRepository;
import com.gkhy.labRiskManage.domain.account.service.RoleDomainService;
import com.gkhy.labRiskManage.domain.account.service.SysDepartmentDomainService;
import com.gkhy.labRiskManage.domain.account.service.UserDomainService;
import com.gkhy.labRiskManage.domain.account.model.dto.UserInfoDomainDTO;
import com.google.common.collect.Range;
import com.google.common.hash.Hashing;
import org.redisson.api.RMapCache;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
 
import javax.persistence.criteria.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
 
@Service
public class UserDomainServiceImpl implements UserDomainService {
 
    @Autowired
    private UserRepository userRepository;
 
    @Autowired
    private RedissonClient redissonClient;
 
    @Autowired
    private ObjectMapper objectMapper;
 
    @Autowired
    private UserInfoDomainConverter userInfoDomainConverter;
 
    @Autowired
    private RoleDomainService roleDomainService;
 
    @Autowired
    private SysDepartmentDomainService departmentDomainService;
 
    @Override
    @Transactional
    public UserInfoDomainDTO newUser(CreateUserBO createUserBO) {
        if(createUserBO == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        User user = new User();
        user.setName(createUserBO.getName());
        user.setRealName(createUserBO.getRealName());
        user.setStatus(UserStatusEnum.STATUS_ACTIVE.getStatus());
        user.setSalt(genPasswordSalt());
        user.setHash(genPasswordHash(createUserBO.getPwd(), user.getSalt()));
        
        //校验用户名称
        User userInfo = userRepository.findUserByName(createUserBO.getName());
        if(userInfo != null){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR,"该用户名称已存在");
        }
        //部门校验
        SysDepartmentDomainDTO dep = departmentDomainService.findById(createUserBO.getDepId());
        if(dep == null){
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_OBJECT_NOT_EXIST.getCode(),"部门不存在");
        }
        user.setDepId(dep.getId());
        //校验证件
        if(UserIdTypeEnum.prase(createUserBO.getIdType()) == null){
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"证件类型不支持");
        }
        /*User checkUser = userRepository.findByIdTypeAndIdSerial(createUserBO.getIdType(), createUserBO.getIdSerial());
        if(checkUser != null){
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"证件已经被使用");
        }*/
        //手机号校验
        User checkUser = userRepository.findUserByPhone(createUserBO.getPhone());
        if(checkUser != null){
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(),"手机号已经被使用");
        }
        if(IdentityStatusEnum.prase(createUserBO.getIdentityStatus()) == null){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"用户用户身份不合法");
        }
        user.setPhone(createUserBO.getPhone());
        user.setIdType(createUserBO.getIdType());
        user.setIdSerial(createUserBO.getIdSerial());
        user.setGmtCreate(LocalDateTime.now());
        user.setGmtModified(LocalDateTime.now());
        user.setIdentityStatus(createUserBO.getIdentityStatus());
        user.setQualificationAttId(createUserBO.getQualificationAttId());
 
        if(userRepository.save(user) != null){
            UserInfoDomainDTO userInfoDomainDTO = new UserInfoDomainConverter().toUserInfoDTO(user);
            return userInfoDomainDTO;
        }else {
            throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(), "数据库错误");
        }
    }
 
    @Transactional
    @Override
    public UserInfoDomainDTO updateUserInfo(UpdateUserBO updateUserBO) {
        if(updateUserBO == null || updateUserBO.getId() == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        if(updateUserBO.getName() == null || updateUserBO.getName().isEmpty())
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "用户名不能为空");
        if(updateUserBO.getRealName() == null || updateUserBO.getRealName().isEmpty())
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "真实姓名不能为空");
        if(updateUserBO.getIdType() == null || UserIdTypeEnum.prase(updateUserBO.getIdType()) == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "证件类型不支持");
        SysDepartmentDomainDTO dep = departmentDomainService.findById(updateUserBO.getDepId());
        if(dep == null){
            throw new BusinessException(this.getClass(),ResultCode.BUSINESS_ERROR_OBJECT_NOT_EXIST.getCode(),"部门不存在");
        }
        Optional<User> userOptional = userRepository.findById(updateUserBO.getId());
        if(!userOptional.isPresent()){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        }
        //校验手机号
        UserInfoDomainDTO u = findUserByPhone(updateUserBO.getPhone());
        if(u != null && !u.getId().equals(updateUserBO.getId())){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED, "手机号已经被使用");
        }
 
        User user = userOptional.get();
        user.setName(updateUserBO.getName());
        user.setRealName(updateUserBO.getRealName());
        user.setIdType(updateUserBO.getIdType());
        user.setIdSerial(updateUserBO.getIdSerial());
        user.setDepId(updateUserBO.getDepId());
        user.setPhone(updateUserBO.getPhone());
        user.setIdentityStatus(updateUserBO.getIdentityStatus());
        user.setQualificationAttId(updateUserBO.getQualificationAttId());
        if (!ObjectUtils.isEmpty(updateUserBO.getPwd())){
            user.setHash(genPasswordHash(updateUserBO.getPwd(), user.getSalt()));
        }
        //写库
        User saveUserRs = userRepository.save(user);
        return userInfoDomainConverter.toUserInfoDTO(saveUserRs);
    }
 
    @Override
    public UserInfoDomainDTO getUserInfoById(Long uid) {
        if(uid == null || uid < 0)
            return null;
        //1、先从redis缓存加载
//        Object cacheUserObj = redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey()).get(""+uid);
//        if(cacheUserObj != null){
//            String cacheUserJson = (String)cacheUserObj;
//            User cacheUser = null;
//            try {
//                cacheUser = objectMapper.readValue(cacheUserJson,User.class);
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//            //2、缓存命中,直接返回
//            if(cacheUser != null){
//                UserInfoDTO userInfoDTO = new UserInfoConverter().toUserInfoDTO(cacheUser);
//                return userInfoDTO;
//            }
//        }
        //3、缓存未命中,入库查
        Optional<User> userOptional = userRepository.findById(uid);
        if(!userOptional.isPresent()){
            return null;
        }
        User user = userOptional.get();
        //获取角色信息
       // RoleInfoDoaminDTO roleInfoDoaminDTO = roleDomainService.findRoleById(user.getRoleId());
        UserInfoDomainDTO userInfoDomainDTO = new UserInfoDomainConverter().toUserInfoDTO(user);
//        if(roleInfoDoaminDTO != null)
//            userInfoDomainDTO.setRole(roleInfoDoaminDTO);
        //重置缓存数据
//        resetUserCache(user);
        return userInfoDomainDTO;
    }
 
    @Override
    public UserInfoDomainDTO getUserInfoByName(String name) {
        if(name == null || name.isEmpty())
            return null;
        User user = userRepository.findUserByName(name);
        if(user == null)
            return null;
        UserInfoDomainDTO userInfoDomainDTO = new UserInfoDomainConverter().toUserInfoDTO(user);
        return userInfoDomainDTO;
    }
 
    @Override
    public List<UserInfoDomainDTO> findUserListByRealName(String name) {
        if(name == null || name.isEmpty())
            return null;
        List<User> userList = userRepository.getUsersByRealName(name);
        if(userList == null || userList.isEmpty())
            return null;
        List<UserInfoDomainDTO> dtoList = new ArrayList<>();
        userList.forEach(u -> {
            dtoList.add(userInfoDomainConverter.toUserInfoDTO(u));
        });
        return dtoList;
    }
 
    @Override
    public SearchResult<List<UserInfoDomainDTO>> findUserListByRole(Long roleId, boolean usePage, Integer page,
                                                                   Integer pageSize) {
        SearchResult<List<UserInfoDomainDTO>> result = new SearchResult<>();
        result.setSuccess();
        result.setUsePage(usePage);
        if(roleId == null || roleId < 0)
            return null;
        Specification<User> userSpecification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(root.get("status").in(UserStatusEnum.getActiveUserStatus()));
                Join<User, SysUserRoleBind> userRoleJion = root.join("sysUserRoleBinds", JoinType.LEFT);
                predicateList.add(criteriaBuilder.equal(userRoleJion.get("roleId"), roleId));
 
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        PageRequest pageRequest = null;
        if(usePage == true){
            if(page <= 0){
                page = 1;
                result.setPageIndex(1);
            }
            if(pageSize <= 0 || pageSize > 50){
                pageSize = 20;
                result.setPageSize(20);
            }
            pageRequest = PageRequest.of(page-1,pageSize);
        }
        List<User> userList = null;
        if(pageRequest != null){
            Page<User> userListPage = userRepository.findAll(userSpecification,pageRequest);
            result.setTotal(userListPage.getTotalElements());
            result.setPages(userListPage.getTotalPages());
            result.setPageIndex(page);
            result.setPageSize(userListPage.getSize());
            userList = userListPage.getContent();
        }else {
            userList = userRepository.findAll(userSpecification);
        }
        if(userList != null && !userList.isEmpty()){
            result.setCount(userList.size());
            result.setData(userInfoDomainConverter.toDomainUserInfoList(userList));
        }
        return result;
    }
 
    @Override
    public List<UserInfoDomainDTO> getUserInfoListByIds(List<Long> uidList) {
        if(uidList == null || uidList.size() == 0)
            return null;
        List<User> userList = userRepository.findAllByIdIn(uidList);
        if(userList == null || userList.size() == 0)
            return null;
        List<UserInfoDomainDTO> doList = new ArrayList<>();
        UserInfoDomainConverter converter = new UserInfoDomainConverter();
        for(User u : userList){
            UserInfoDomainDTO userInfoDomainDTO = converter.toUserInfoDTO(u);
            doList.add(userInfoDomainDTO);
        }
        return doList;
    }
    //2024 修改密码弱口令问题
    @Override
    @Transactional
    public boolean updateUserPwd(Long uid, String oldPwd, String newPwd) {
        if(uid == null || oldPwd == null || newPwd == null || oldPwd.isEmpty() || newPwd.isEmpty())
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
 
        if (newPwd.length() < 8){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"密码长度不够");
        }
        if (!newPwd.matches(".*[A-Z].*")){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"密码至少包含大小写字母、数字、特殊字符");
        }
        if (!newPwd.matches(".*[a-z].*")){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"密码至少包含大小写字母、数字、特殊字符");
        }
        if (!newPwd.matches(".*\\d.*")){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"密码至少包含大小写字母、数字、特殊字符");
        }
        if (!newPwd.matches(".*[!@#$%^&*.()?+`~<>,-].*")){
            throw new BusinessException(this.getClass(),ResultCode.PARAM_ERROR_ILLEGAL.getCode(),"密码至少包含大小写字母、数字、特殊字符");
        }
 
        Optional<User> userOptional = userRepository.findById(uid);
        if(!userOptional.isPresent()){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        }
        User user = userOptional.get();
        //验证旧密码
        String hash = genPasswordHash(oldPwd, user.getSalt());
        if(!hash.equals(user.getHash()))
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "旧密码错误");
 
        String newHash = genPasswordHash(newPwd, user.getSalt());
        if(userRepository.updatePassword(uid,newHash, user.getSalt(), LocalDateTime.now()) == 1){
//            deleteUserCache(uid);
            return true;
        }else {
            throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(), "数据库错误");
        }
    }
 
    // todo 2024 密码重置问题
    @Override
    public boolean resetUserPassword(Long uid, Long currentUserId) {
 
        if(uid == null){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        }
 
        Optional<User> userOptional = userRepository.findById(uid);
        //验证用户是否存在
        if(!userOptional.isPresent()){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        }
        User user = userOptional.get();
 
        //设置初始密码
        String newPwd = "Gs@123456";
        String newHash = genPasswordHash(newPwd, user.getSalt());
 
//        Integer integer = userRepository.resetPassword(uid, newHash, LocalDateTime.now());
 
        if(userRepository.resetPassword(uid, newHash, LocalDateTime.now()) == 1){
            return true;
        }else {
            throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(), "数据库错误");
        }
    }
 
    @Override
    @Transactional
    public boolean updateUserStatus(Long uid, Byte status) {
        if(uid == null || status == null || UserStatusEnum.prase(status) == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        Optional<User> userOptional = userRepository.findById(uid);
        if(!userOptional.isPresent()){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        }
        User user = userOptional.get();
        if(user.getStatus().equals(status))
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "用户状态未发生改变");
        if(userRepository.updateUserStatus(uid,status,LocalDateTime.now()) == 1){
//            deleteUserCache(uid);
            return true;
        }
        return false;
    }
 
    @Override
    @Transactional
    public boolean updateUserRole(Long uid, Long roleId) {
        if(uid == null || roleId == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        Optional<User> userOptional = userRepository.findById(uid);
        if(!userOptional.isPresent()){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        }
        User user = userOptional.get();
        /*if(user.getRoleId() != null && user.getRoleId().equals(roleId))
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "用户角色未发生改变");*/
        //校验角色信息
        /*if(userRepository.updateUserRole(uid,roleId,LocalDateTime.now()) != null){
//            deleteUserCache(uid);
            return true;
        }*/
        return false;
    }
    //2024 登录校验问题
    @Override
    public boolean checkPassword(String pwd, String hash, String salt) {
        if(pwd == null || pwd.isEmpty() || salt == null || salt.isEmpty() || hash == null || hash.isEmpty())
            return false;
        if(Hashing.hmacMd5(salt.getBytes(StandardCharsets.UTF_8)).hashString(pwd, StandardCharsets.UTF_8).toString().equals(hash)){
            return true;
        }else {
            return false;
        }
    }
 
    @Override
    public UserInfoDomainDTO findUserByPhone(String phoneNumber) {
        if(phoneNumber == null || phoneNumber.isEmpty() || phoneNumber.length() < 11)
            return null;
        User user = userRepository.findUserByPhone(phoneNumber);
        if(user == null)
            return null;
        UserInfoDomainDTO userInfoDomainDTO = userInfoDomainConverter.toUserInfoDTO(user);
        return userInfoDomainDTO;
    }
 
    @Override
    public UserInfoDomainDTO findUserByIdSerial(Byte idType, String idSerial) {
        if(idType == null || idSerial == null || idSerial.isEmpty() || idSerial.length() < 10)
            return null;
        User user = userRepository.findByIdTypeAndIdSerial(idType,idSerial);
        if(user == null)
            return null;
        UserInfoDomainDTO userInfoDomainDTO = userInfoDomainConverter.toUserInfoDTO(user);
        return userInfoDomainDTO;
    }
 
    @Override
    @Transactional
    public boolean updateUserPhoneNumber(Long uid, String phoneNumber) {
        if(uid == null || phoneNumber == null || phoneNumber.isEmpty())
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        User user = userRepository.findUserByPhone(phoneNumber);
        if(user != null && !user.getId().equals(uid))
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "手机号码已经被使用");
        Optional<User> userOptional = userRepository.findById(uid);
        if(!userOptional.isPresent())
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        user = userOptional.get();
        if(user.getPhone().equals(phoneNumber))
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "手机号码未发生变化");
        if(userRepository.updateUserPhone(uid,phoneNumber) == 1){
//            deleteUserCache(uid);
            return true;
        }else {
            throw new BusinessException(this.getClass(),ResultCode.SYSTEM_ERROR_DATABASE_FAIL.getCode(),"数据库更新出错");
        }
    }
 
    @Override
    @Transactional
    public boolean deleteUser(Long uid) {
        if(uid == null)
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        Optional<User> userOptional = userRepository.findById(uid);
        if(!userOptional.isPresent())
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_ACCOUNT_NOT_EXIST.getCode(), "用户不存在");
        User user = userOptional.get();
        if(user.getStatus().equals(UserStatusEnum.STATUS_DELETE.getStatus())){
            throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "用户不存在");
        }
        if(userRepository.updateUserStatus(uid,UserStatusEnum.STATUS_DELETE.getStatus(), LocalDateTime.now()) ==1){
//            deleteUserCache(uid);
            return true;
        }
        return false;
    }
 
    /**
     * 用户列表
     */
    @Override
    public List<UserInfoDomainDTO> getUserList() {
 
        return BeanCopyUtils.copyBeanList(userRepository.getUserList(), UserInfoDomainDTO.class);
    }
    /**
     * 根据用户id获取信息
     */
    public UserInfoDomainDTO getUserById(Long id){
        if(id == null)
            return null;
        User user = userRepository.getById(id);
        UserInfoDomainDTO userInfoDomainDTO = userInfoDomainConverter.toUserInfoDTO(user);
        return userInfoDomainDTO;
    }
 
 
    /**
     * 根据用户获取数据
     */
    public List<UserInfoDomainDTO> getUserByRoleId(Long roleId){
        if(null == roleId){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失");
        }
        Specification<User> userSpecification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(root.get("status").in(UserStatusEnum.getActiveUserStatus()));
                Join<User, SysUserRoleBind> userRoleJion = root.join("sysUserRoleBinds", JoinType.LEFT);
                predicateList.add(criteriaBuilder.equal(userRoleJion.get("roleId"), roleId));
 
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        List<User> userList = userRepository.findAll(userSpecification);
        return userInfoDomainConverter.toDomainUserInfoList(userList);
 
    }
 
    /**
     *
     * @param pageQuery
     * @return
     */
    @Override
    public SearchResult<List<UserInfoDomainDTO>> findUserList(PageQuery<UserQuery> pageQuery) {
        SearchResult searchResult = new SearchResult<>();
        searchResult.setPageIndex(pageQuery.getPageIndex());
        searchResult.setPageSize(pageQuery.getPageSize());
        searchResult.setSuccess();
 
        UserQuery userQuery = pageQuery.getSearchParams();
        Specification<User> userSpecification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if(query != null){
                    if(!ObjectUtils.isEmpty(userQuery.getRoleId())){
                        Join<User, SysUserRoleBind> userRoleJion = root.join("sysUserRoleBinds", JoinType.LEFT);
                        predicateList.add(criteriaBuilder.equal(userRoleJion.get("roleId"), userQuery.getRoleId()));
                    }
                    if(!ObjectUtils.isEmpty(userQuery.getName())){
                        predicateList.add(criteriaBuilder.like(root.get("name"),userQuery.getName()));
                    }
                    if(!ObjectUtils.isEmpty(userQuery.getRealName())){
                        predicateList.add(criteriaBuilder.like(root.get("realName"),userQuery.getRealName()));
                    }
                }
                predicateList.add(root.get("status").in(UserStatusEnum.getActiveUserStatus()));
 
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "gmtCreate");
        Page<User> pageResult = userRepository.findAll(userSpecification, pageable);
 
        searchResult.setTotal(pageResult.getTotalElements());
        searchResult.setPages(pageResult.getTotalPages());
        searchResult.setData(userInfoDomainConverter.toDomainUserInfoList(pageResult.getContent()));
 
        return searchResult;
    }
 
    @Override
    public SearchResult<List<UserInfoDomainDTO>> findExpertList(PageQuery<UserQuery> pageQuery) {
        SearchResult searchResult = new SearchResult<>();
        searchResult.setPageIndex(pageQuery.getPageIndex());
        searchResult.setPageSize(pageQuery.getPageSize());
        searchResult.setSuccess();
 
        UserQuery userQuery = pageQuery.getSearchParams();
        Specification<User> userSpecification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(root.get("status").in(UserStatusEnum.getActiveUserStatus()));
                predicateList.add(criteriaBuilder.equal(root.get("identityStatus"),IdentityStatusEnum.EXPERT.getStatus()));
                if(query != null){
                    if(!ObjectUtils.isEmpty(userQuery.getRoleId())){
                        Join<User, SysUserRoleBind> userRoleJion = root.join("sysUserRoleBinds", JoinType.LEFT);
                        predicateList.add(criteriaBuilder.equal(userRoleJion.get("roleId"), userQuery.getRoleId()));
                    }
                    if(!ObjectUtils.isEmpty(userQuery.getName())){
                        predicateList.add(criteriaBuilder.like(root.get("name"),userQuery.getName()));
                    }
                    if(!ObjectUtils.isEmpty(userQuery.getRealName())){
                        predicateList.add(criteriaBuilder.like(root.get("realName"),userQuery.getRealName()));
                    }
                    if(!ObjectUtils.isEmpty(userQuery.getUserIndentityId())){
                        Join<User, SysUserIdentityBind> userIdentityBindJion = root.join("sysUserIdentityBinds", JoinType.LEFT);
                        predicateList.add(criteriaBuilder.equal(userIdentityBindJion.get("userIdentityId"), userQuery.getUserIndentityId()));
                    }
                }
 
 
                return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
        Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "gmtCreate");
        Page<User> pageResult = userRepository.findAll(userSpecification, pageable);
 
        searchResult.setTotal(pageResult.getTotalElements());
        searchResult.setPages(pageResult.getTotalPages());
        searchResult.setData(userInfoDomainConverter.toDomainUserInfoList(pageResult.getContent()));
 
        return searchResult;
    }
 
 
 
    /**
     * 用户查询
     */
    @Override
    public UserInfoDomainDTO getUserInfoByIdAndSellInfo(Long evaluateUserId, String info) {
 
        if (ObjectUtils.isEmpty(evaluateUserId)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "请求参数不能为空");
        }
 
        User userInfo = userRepository.getUserInfoByIdAndSellInfo(evaluateUserId);
 
        if (ObjectUtils.isEmpty(userInfo)){
            throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR.getCode(), info + "不存在,请检查是否输入有误或人员已被删除");
        }
        return BeanCopyUtils.copyBean(userInfo, UserInfoDomainDTO.class);
    }
 
 
    /**
     * 清除REDIS缓存的用户数据
     * @param userId
     * @return
     */
    public Result deleteUserCache(Long userId){
        Result result = new Result<>();
        if(userId == null){
            return result;
        }
        RMapCache<String,Object> userCacheMap = redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey());
        String userKey = ""+userId;
        userCacheMap.remove(userKey);
        result.setSuccess();
        return result;
    }
 
    /**
     * 重置缓存的用户数据
     * @param user
     * @return
     */
    public boolean resetUserCache(User user){
        if(user == null)
            return false;
        String cacheJson = null;
        try {
            cacheJson = objectMapper.writeValueAsString(user);
        } catch (JsonProcessingException e) {
            throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR.getCode(),"序列化对象出错");
        }
        RMapCache<String,Object> userCacheMap = redissonClient.getMapCache(SystemCacheKeyEnum.KEY_CACHE_USER.getKey());
        String userKey = ""+user.getId();
        //写入redis缓存,有效期1小时
        userCacheMap.put(userKey,cacheJson,60, TimeUnit.MINUTES);
        return true;
    }
 
    /**
     * 生成salt
     * @return
     */
    private String genPasswordSalt(){
        String seed = ""+System.nanoTime();
        String m = ""+ Range.atLeast(1).toString();
        String salt = Hashing.hmacMd5(seed.getBytes(StandardCharsets.UTF_8)).hashBytes(m.getBytes(StandardCharsets.UTF_8)).toString();
        return salt;
    }
 
    /**
     * 生成hash
     * @param password
     * @param salt
     * @return
     */
    private String genPasswordHash(String password,String salt){
        if(password == null || salt == null || password.isEmpty() || salt.isEmpty())
            return null;
        String hash = Hashing.hmacMd5(salt.getBytes(StandardCharsets.UTF_8)).hashBytes(password.getBytes(StandardCharsets.UTF_8)).toString();
        return hash;
    }
 
 
 
}