|
@@ -1,17 +1,20 @@
|
|
|
package com.qmth.themis.business.service.impl;
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.qmth.themis.business.constant.SpringContextHolder;
|
|
|
import com.qmth.themis.business.constant.SystemConstant;
|
|
|
import com.qmth.themis.business.dto.AuthDto;
|
|
|
+import com.qmth.themis.business.dto.cache.TEStudentCacheDto;
|
|
|
import com.qmth.themis.business.entity.*;
|
|
|
import com.qmth.themis.business.enums.RoleEnum;
|
|
|
import com.qmth.themis.business.service.*;
|
|
|
-import com.qmth.themis.business.util.RedisUtil;
|
|
|
import com.qmth.themis.common.enums.ExceptionResultEnum;
|
|
|
import com.qmth.themis.common.exception.BusinessException;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
|
+import org.springframework.cache.annotation.CachePut;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -51,8 +54,79 @@ public class CacheServiceImpl implements CacheService {
|
|
|
@Resource
|
|
|
TEStudentService teStudentService;
|
|
|
|
|
|
- @Resource
|
|
|
- RedisUtil redisUtil;
|
|
|
+ /**
|
|
|
+ * 添加机构缓存
|
|
|
+ *
|
|
|
+ * @param orgId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Cacheable(value = SystemConstant.orgCache, key = "#p0")
|
|
|
+ public TBOrg addOrgCache(Long orgId) {
|
|
|
+ return tbOrgService.getById(orgId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新机构缓存
|
|
|
+ *
|
|
|
+ * @param orgId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CachePut(value = SystemConstant.orgCache, key = "#p0", condition = "#result != null")
|
|
|
+ public TBOrg updateOrgCache(Long orgId) {
|
|
|
+ return tbOrgService.getById(orgId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加机构编码缓存
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Cacheable(value = SystemConstant.orgCodeCache, key = "#p0")
|
|
|
+ public TBOrg addOrgCodeCache(String code) {
|
|
|
+ QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
|
|
|
+ tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, code);
|
|
|
+ return tbOrgService.getOne(tbOrgQueryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新机构编码缓存
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CachePut(value = SystemConstant.orgCodeCache, key = "#p0", condition = "#result != null")
|
|
|
+ public TBOrg updateOrgCodeCache(String code) {
|
|
|
+ QueryWrapper<TBOrg> tbOrgQueryWrapper = new QueryWrapper<>();
|
|
|
+ tbOrgQueryWrapper.lambda().eq(TBOrg::getCode, code);
|
|
|
+ return tbOrgService.getOne(tbOrgQueryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除机构缓存
|
|
|
+ *
|
|
|
+ * @param orgId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = SystemConstant.orgCache, key = "#p0")
|
|
|
+ public void removeOrgCache(Long orgId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除机构编码缓存
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = SystemConstant.orgCodeCache, key = "#p0")
|
|
|
+ public void removeOrgCodeCache(String code) {
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 添加用户缓存
|
|
@@ -61,11 +135,12 @@ public class CacheServiceImpl implements CacheService {
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- @Cacheable(value = "user:oauth:cache", key = "#p0")
|
|
|
- public AuthDto addAccountCache(Long userId) {
|
|
|
+ @Cacheable(value = SystemConstant.userOauth, key = "#p0")
|
|
|
+ public AuthDto addAccountAuthCache(Long userId) {
|
|
|
AuthDto authDto = null;
|
|
|
try {
|
|
|
- TBUser user = tbUserService.getById(userId);
|
|
|
+ CacheService cacheService = SpringContextHolder.getBean(CacheService.class);
|
|
|
+ TBUser user = cacheService.addAccountCache(userId);
|
|
|
if (Objects.isNull(user)) {
|
|
|
throw new BusinessException(ExceptionResultEnum.USER_NO);
|
|
|
}
|
|
@@ -74,7 +149,7 @@ public class CacheServiceImpl implements CacheService {
|
|
|
uWrapper.lambda().eq(TBUserRole::getUserId, user.getId());
|
|
|
List<TBUserRole> tbUserRoleList = tbUserRoleService.list(uWrapper);
|
|
|
if (Objects.nonNull(tbUserRoleList)) {
|
|
|
- TBOrg tbOrg = Objects.isNull(redisUtil.getOrg(user.getOrgId())) ? tbOrgService.getById(user.getOrgId()) : (TBOrg) redisUtil.getOrg(user.getOrgId());
|
|
|
+ TBOrg tbOrg = this.addOrgCache(user.getOrgId());
|
|
|
//根据角色名查权限
|
|
|
Set<String> roleCodes = tbUserRoleList.stream().map(s -> s.getRoleCode()).collect(Collectors.toSet());
|
|
|
QueryWrapper<TBRolePrivilege> rpWrapper = new QueryWrapper<>();
|
|
@@ -88,38 +163,74 @@ public class CacheServiceImpl implements CacheService {
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("请求出错", e);
|
|
|
- throw new BusinessException("添加用户缓存失败");
|
|
|
+ throw new BusinessException("添加用户鉴权缓存失败");
|
|
|
}
|
|
|
return authDto;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 删除用户鉴权缓存
|
|
|
+ *
|
|
|
+ * @param userId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = SystemConstant.userOauth, key = "#p0")
|
|
|
+ public void removeAccountAuthCache(Long userId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加用户缓存
|
|
|
+ *
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Cacheable(value = SystemConstant.userAccount, key = "#p0")
|
|
|
+ public TBUser addAccountCache(Long userId) {
|
|
|
+ return tbUserService.getById(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户缓存
|
|
|
+ *
|
|
|
+ * @param userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CachePut(value = SystemConstant.userAccount, key = "#p0", condition = "#result != null")
|
|
|
+ public TBUser updateAccountCache(Long userId) {
|
|
|
+ return tbUserService.getById(userId);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 删除用户缓存
|
|
|
*
|
|
|
* @param userId
|
|
|
*/
|
|
|
@Override
|
|
|
- @CacheEvict(value = "user:oauth:cache", key = "#p0")
|
|
|
+ @CacheEvict(value = SystemConstant.userAccount, key = "#p0")
|
|
|
public void removeAccountCache(Long userId) {
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 添加学生缓存
|
|
|
+ * 添加学生鉴权缓存
|
|
|
*
|
|
|
* @param studentId
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- @Cacheable(value = "student:oauth:cache", key = "#p0")
|
|
|
- public AuthDto addStudentCache(Long studentId) {
|
|
|
+ @Cacheable(value = SystemConstant.studentOauth, key = "#p0")
|
|
|
+ public AuthDto addStudentAuthCache(Long studentId) {
|
|
|
AuthDto authDto = null;
|
|
|
try {
|
|
|
- TEStudent teStudent = teStudentService.getById(studentId);
|
|
|
- if (Objects.isNull(teStudent)) {
|
|
|
+ CacheService cacheService = SpringContextHolder.getBean(CacheService.class);
|
|
|
+ TEStudentCacheDto teStudentCacheDto = cacheService.addStudentAccountCache(studentId);
|
|
|
+ if (Objects.isNull(teStudentCacheDto)) {
|
|
|
throw new BusinessException(ExceptionResultEnum.STUDENT_NO);
|
|
|
}
|
|
|
- TBOrg tbOrg = tbOrgService.getById(teStudent.getOrgId());
|
|
|
+ TBOrg tbOrg = cacheService.addOrgCache(teStudentCacheDto.getOrgId());
|
|
|
//根据学生角色查权限
|
|
|
QueryWrapper<TBRolePrivilege> rpWrapper = new QueryWrapper<>();
|
|
|
rpWrapper.lambda().eq(TBRolePrivilege::getRoleCode, RoleEnum.STUDENT.name());
|
|
@@ -133,19 +244,58 @@ public class CacheServiceImpl implements CacheService {
|
|
|
authDto = new AuthDto(roleCodes, tbPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), tbOrg);
|
|
|
} catch (Exception e) {
|
|
|
log.error("请求出错", e);
|
|
|
- throw new BusinessException("添加学生缓存失败");
|
|
|
+ throw new BusinessException("添加学生鉴权缓存失败");
|
|
|
}
|
|
|
return authDto;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 删除学生鉴权缓存
|
|
|
+ *
|
|
|
+ * @param studentId
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CacheEvict(value = SystemConstant.studentOauth, key = "#p0")
|
|
|
+ public void removeStudentAuthCache(Long studentId) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加学生缓存
|
|
|
+ *
|
|
|
+ * @param studentId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Cacheable(value = SystemConstant.studentAccount, key = "#p0")
|
|
|
+ public TEStudentCacheDto addStudentAccountCache(Long studentId) {
|
|
|
+ TEStudent teStudent = teStudentService.getById(studentId);
|
|
|
+ Gson gson = new Gson();
|
|
|
+ return gson.fromJson(gson.toJson(teStudent), TEStudentCacheDto.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新学生缓存
|
|
|
+ *
|
|
|
+ * @param studentId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @CachePut(value = SystemConstant.studentAccount, key = "#p0", condition = "#result != null")
|
|
|
+ public TEStudentCacheDto updateStudentAccountCache(Long studentId) {
|
|
|
+ TEStudent teStudent = teStudentService.getById(studentId);
|
|
|
+ Gson gson = new Gson();
|
|
|
+ return gson.fromJson(gson.toJson(teStudent), TEStudentCacheDto.class);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 删除学生缓存
|
|
|
*
|
|
|
* @param studentId
|
|
|
*/
|
|
|
@Override
|
|
|
- @CacheEvict(value = "student:oauth:cache", key = "#p0")
|
|
|
- public void removeStudentCache(Long studentId) {
|
|
|
+ @CacheEvict(value = SystemConstant.studentAccount, key = "#p0")
|
|
|
+ public void removeStudentAccountCache(Long studentId) {
|
|
|
|
|
|
}
|
|
|
}
|