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 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 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()); //写库 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 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 findUserListByRealName(String name) { if(name == null || name.isEmpty()) return null; List userList = userRepository.getUsersByRealName(name); if(userList == null || userList.isEmpty()) return null; List dtoList = new ArrayList<>(); userList.forEach(u -> { dtoList.add(userInfoDomainConverter.toUserInfoDTO(u)); }); return dtoList; } @Override public SearchResult> findUserListByRole(Long roleId, boolean usePage, Integer page, Integer pageSize) { SearchResult> result = new SearchResult<>(); result.setSuccess(); result.setUsePage(usePage); if(roleId == null || roleId < 0) return null; Specification userSpecification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { List predicateList = new ArrayList<>(); predicateList.add(root.get("status").in(UserStatusEnum.getActiveUserStatus())); Join 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 userList = null; if(pageRequest != null){ Page 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 getUserInfoListByIds(List uidList) { if(uidList == null || uidList.size() == 0) return null; List userList = userRepository.findAllByIdIn(uidList); if(userList == null || userList.size() == 0) return null; List doList = new ArrayList<>(); UserInfoDomainConverter converter = new UserInfoDomainConverter(); for(User u : userList){ UserInfoDomainDTO userInfoDomainDTO = converter.toUserInfoDTO(u); doList.add(userInfoDomainDTO); } return doList; } @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(), "参数缺失"); Optional 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 = String.valueOf(Hashing.hmacMd5(user.getSalt().getBytes(StandardCharsets.UTF_8)).hashString(oldPwd, StandardCharsets.UTF_8)); if(!hash.equals(user.getHash())) throw new BusinessException(this.getClass(), ResultCode.BUSINESS_ERROR_NOT_ALLOWED.getCode(), "旧密码错误"); String newSalt = String.valueOf(Hashing.hmacMd5("".getBytes()).hashString(""+uid+Range.atLeast(1)+System.nanoTime(), StandardCharsets.UTF_8)); String newHash = String.valueOf(Hashing.hmacMd5(newSalt.getBytes(StandardCharsets.UTF_8)).hashString(newPwd, StandardCharsets.UTF_8)); if(userRepository.updatePassword(uid,newHash,newSalt, LocalDateTime.now()) == 1){ // deleteUserCache(uid); 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 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 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(), "用户角色未发生改变");*/ //todo:校验角色信息 /*if(userRepository.updateUserRole(uid,roleId,LocalDateTime.now()) != null){ // deleteUserCache(uid); return true; }*/ return false; } @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 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 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 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 getUserByRoleId(Long roleId){ if(null == roleId){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(), "参数缺失"); } Specification userSpecification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { List predicateList = new ArrayList<>(); predicateList.add(root.get("status").in(UserStatusEnum.getActiveUserStatus())); Join userRoleJion = root.join("sysUserRoleBinds", JoinType.LEFT); predicateList.add(criteriaBuilder.equal(userRoleJion.get("roleId"), roleId)); return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])); } }; List userList = userRepository.findAll(userSpecification); return userInfoDomainConverter.toDomainUserInfoList(userList); } /** * * @param pageQuery * @return */ @Override public SearchResult> findUserList(PageQuery pageQuery) { SearchResult searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); UserQuery userQuery = pageQuery.getSearchParams(); Specification userSpecification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { List predicateList = new ArrayList<>(); if(query != null){ if(!ObjectUtils.isEmpty(userQuery.getRoleId())){ Join 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 pageResult = userRepository.findAll(userSpecification, pageable); searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); searchResult.setData(userInfoDomainConverter.toDomainUserInfoList(pageResult.getContent())); return searchResult; } @Override public SearchResult> findExpertList(PageQuery pageQuery) { SearchResult searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); UserQuery userQuery = pageQuery.getSearchParams(); Specification userSpecification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { List 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 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 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 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 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 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; } }