Răsfoiți Sursa

perf(消费服务): 账户信息的缓存操作不再直接使用RedisUtils操作,而是使用spring-cache 的注解操作

autumnal_wind 11 luni în urmă
părinte
comite
bcfd3953c9

+ 0 - 12
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/business/EmployeeBusiness.java

@@ -2,28 +2,21 @@ package org.dromara.server.consume.business;
 
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.dubbo.config.annotation.DubboReference;
 import org.dromara.backstage.api.domain.vo.RemoteCardVo;
 import org.dromara.backstage.api.domain.vo.RemoteUserAccountVo;
 import org.dromara.common.core.api.ReturnResult;
 import org.dromara.common.core.constant.ApiErrorTypeConstants;
-import org.dromara.common.core.constant.CacheNames;
 import org.dromara.common.core.domain.model.ErrorInfo;
 import org.dromara.common.core.domain.model.ErrorResult;
 import org.dromara.common.core.enums.ResultCodeEnum;
 import org.dromara.common.json.utils.JsonUtils;
-import org.dromara.common.redis.utils.RedisUtils;
 import org.dromara.server.common.constant.ConsumeConstants;
 import org.dromara.server.consume.cache.CardCacheManager;
 import org.dromara.server.consume.cache.UserAccountCacheManager;
 import org.dromara.server.consume.domain.vo.yc.*;
-import org.dromara.system.api.RemoteDeptService;
-import org.dromara.system.api.domain.vo.RemoteDeptVo;
 import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Service;
 
-import java.util.List;
-
 /**
  * @ClassName EmployeeBusiness
  * @Description TODO
@@ -39,9 +32,6 @@ public class EmployeeBusiness {
     private final UserAccountCacheManager userAccountCacheManager;
     private final CardCacheManager cardCacheManager;
 
-    @DubboReference
-    private final RemoteDeptService remoteDeptService;
-
     public ReturnResult getEmployeeVoByNumb(String userNumb) {
         RemoteUserAccountVo userAccountVo = userAccountCacheManager.getByUserNumb(userNumb);
         if (userAccountVo == null) {
@@ -78,10 +68,8 @@ public class EmployeeBusiness {
     }
 
     private Object setReturnEmployeeVo(RemoteUserAccountVo userAccountVo) {
-        // RemoteDeptVo remoteDeptVo = remoteDeptService.selectDeptById(userAccountVo.getDeptId());
         DepartmentVo departmentVo = new DepartmentVo();
         departmentVo.setDepartmentID(userAccountVo.getDeptId().toString());
-        // departmentVo.setParentDepartmentID(remoteDeptVo.getParentId().toString());
         departmentVo.setDepartmentName(userAccountVo.getDeptName());
 
         Long userId = userAccountVo.getUserId();

+ 8 - 32
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/business/InitBusiness.java

@@ -13,6 +13,7 @@ import org.dromara.common.json.utils.JsonUtils;
 import org.dromara.common.redis.utils.RedisUtils;
 import org.dromara.server.common.util.CardDateUtils;
 import org.dromara.server.consume.cache.CardCacheManager;
+import org.dromara.server.consume.cache.UserAccountCacheManager;
 import org.dromara.server.consume.cache.ValidationParam;
 import org.dromara.server.consume.domain.bo.XfCardLimitedBo;
 import org.dromara.server.consume.domain.vo.PtBagVo;
@@ -50,6 +51,7 @@ public class InitBusiness {
     private final IXfTermService xfTermService;
     private final ValidationParam validationParam;
     private final CardCacheManager cardCacheManager;
+    private final UserAccountCacheManager userAccountCacheManager;
 
     @DubboReference
     private final RemotePtParameterService remotePtParameterService;
@@ -223,22 +225,7 @@ public class InitBusiness {
                 && p.getLifespan().getTime() > System.currentTimeMillis()).toList();
 
         if (CollectionUtil.isNotEmpty(filterList)) {
-            RedisUtils.deleteKeys(CacheNames.PT_USER_ACCOUNT_ID);
-            RedisUtils.deleteKeys(CacheNames.PT_USER_ACCOUNT_NUMB);
-            RedisUtils.deleteKeys(CacheNames.PT_USER_ACCOUNT_NO);
-
-            filterList.parallelStream().forEach(p -> {
-                String strUserId = String.valueOf(p.getUserId());
-                String strUserNo = String.valueOf(p.getUserNo());
-                String userNumb = String.valueOf(p.getUserNumb());
-
-                RedisUtils.setCacheMapValue(CacheNames.PT_USER_ACCOUNT_ID, strUserId, p);
-                RedisUtils.setCacheMapValue(CacheNames.PT_USER_ACCOUNT_NO, strUserNo, p);
-                RedisUtils.setCacheMapValue(CacheNames.PT_USER_ACCOUNT_NUMB, userNumb, p);
-            });
-            RedisUtils.expire(CacheNames.PT_USER_ACCOUNT_ID, Duration.ofHours(5));
-            RedisUtils.expire(CacheNames.PT_USER_ACCOUNT_NUMB, Duration.ofHours(5));
-            RedisUtils.expire(CacheNames.PT_USER_ACCOUNT_NO, Duration.ofHours(5));
+            filterList.forEach(userAccountCacheManager::cacheUserAccountVo);
         }
         log.info("初始化人员账户参数完成");
     }
@@ -250,22 +237,11 @@ public class InitBusiness {
      * @param id 人员Id
      */
     public void initUserAccountById(Long id) {
-        // RemoteUserAccountVo accountVo = remoteUserAccountService.getUserAccountVoById(id);
-        // if (ObjectUtil.isNotEmpty(accountVo)) {
-        //     List<RemoteUserAccountVo> list = RedisUtils.getCacheList(CacheNames.PT_USER_ACCOUNT_LIST);
-        // String strUserId = String.valueOf(id);
-        // String strUserNo = String.valueOf(accountVo.getUserNo());
-        //
-        // RedisUtils.delCacheMapValue(CacheNames.PT_USER_ACCOUNT_ID, strUserId);
-        // RedisUtils.delCacheMapValue(CacheNames.PT_USER_ACCOUNT_NO, strUserNo);
-        //
-        // RedisUtils.setCacheMapValue(CacheNames.PT_USER_ACCOUNT_ID, strUserId, accountVo);
-        // RedisUtils.setCacheMapValue(CacheNames.PT_USER_ACCOUNT_NO, strUserNo, accountVo);
-        //
-        // BigDecimal balance = bagService.getUserBalance(id);
-        // RedisUtils.delCacheMapValue(CacheNames.PT_USER_CARD_NO, strUserId);
-        // RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, strUserId, balance);
-        // }
+        RemoteUserAccountVo accountVo = remoteUserAccountService.getUserAccountVoById(id);
+        if (ObjectUtil.isNotEmpty(accountVo)) {
+            userAccountCacheManager.cacheUserAccountVo(accountVo);
+        }
+
         log.info("初始化指定人员账户参数完成,人员Id:{}", id);
     }
 

+ 13 - 0
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/cache/UserAccountCacheManager.java

@@ -9,7 +9,9 @@ import org.dromara.backstage.api.domain.vo.RemoteCardVo;
 import org.dromara.backstage.api.domain.vo.RemoteUserAccountVo;
 import org.dromara.common.core.constant.CacheNames;
 import org.dromara.common.json.utils.JsonUtils;
+import org.springframework.cache.annotation.CachePut;
 import org.springframework.cache.annotation.Cacheable;
+import org.springframework.cache.annotation.Caching;
 import org.springframework.stereotype.Service;
 
 /**
@@ -42,4 +44,15 @@ public class UserAccountCacheManager {
     public RemoteUserAccountVo getByUserNumb(String userNumb) {
         return remoteUserAccountService.getUserAccountVoByUserNumb(userNumb);
     }
+
+    @Caching(
+        put = {
+            @CachePut(cacheNames = CacheNames.PT_USER_ACCOUNT_ID, key = "#accountVo.userId"),
+            @CachePut(cacheNames = CacheNames.PT_USER_ACCOUNT_NO, key = "#accountVo.userNo"),
+            @CachePut(cacheNames = CacheNames.PT_USER_ACCOUNT_NUMB, key = "#accountVo.userNumb")
+        }
+    )
+    public RemoteUserAccountVo cacheUserAccountVo(RemoteUserAccountVo accountVo) {
+        return accountVo;
+    }
 }

+ 7 - 15
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/check/CommonCheck.java

@@ -54,14 +54,6 @@ import java.util.function.Supplier;
 @Service
 @RequiredArgsConstructor
 public class CommonCheck {
-    // 用户校验模式 0-userId 1- userNo 2-userNumb
-    private static final Integer userIdMode = 0;
-    private static final Integer userNoMode = 1;
-    private static final Integer userNumbMode = 2;
-    // 卡片校验模式 0-userId 1- cardNo
-    private static final Integer cardIdMode = 0;
-    private static final int cardNoMode = 1;
-    private static final Integer cardFactoryMode = 2;
     // 异步线程执行超时时间,以毫秒为单位
     private static final long VALIDATION_TIMEOUT = 500;
 
@@ -327,7 +319,7 @@ public class CommonCheck {
     private R<ErrorInfo> checkCardNo(AllowConsumeValidationContext ctx) {
         // 检查卡片
         long cardNo = ctx.getCardNo();
-        R<ErrorInfo> result = checkCard(cardNo, cardNoMode, ctx);
+        R<ErrorInfo> result = checkCard(cardNo, ConsumeConstants.CARD_NO, ctx);
         if (R.isError(result)) {
             return result;
         }
@@ -343,7 +335,7 @@ public class CommonCheck {
         }
         // 获取消费账户信息
         String strUserId = String.valueOf(ctx.getUserCardVo().getUserId());
-        result = checkUser(strUserId, userIdMode, ctx);
+        result = checkUser(strUserId, ConsumeConstants.USER_ID, ctx);
         if (R.isError(result)) {
             return result;
         }
@@ -362,13 +354,13 @@ public class CommonCheck {
     private R<ErrorInfo> checkFactoryId(AllowConsumeValidationContext ctx) {
         // 检查卡片
         long factoryId = ctx.getFactoryId();
-        R<ErrorInfo> result = checkCard(factoryId, cardFactoryMode, ctx);
+        R<ErrorInfo> result = checkCard(factoryId, ConsumeConstants.CARD_FACTORY, ctx);
         if (R.isError(result)) {
             return result;
         }
         // 获取消费账户信息
         String strUserId = String.valueOf(ctx.getUserCardVo().getUserId());
-        result = checkUser(strUserId, userIdMode, ctx);
+        result = checkUser(strUserId, ConsumeConstants.USER_ID, ctx);
         if (R.isError(result)) {
             return result;
         }
@@ -388,7 +380,7 @@ public class CommonCheck {
     private R<ErrorInfo> checkUserNo(AllowConsumeValidationContext ctx) {
         // 校验账户信息
         String userNo = String.valueOf(ctx.getUserNo());
-        R<ErrorInfo> result = checkUser(userNo, userNoMode, ctx);
+        R<ErrorInfo> result = checkUser(userNo, ConsumeConstants.USER_NO, ctx);
         if (R.isError(result)) {
             return result;
         }
@@ -405,7 +397,7 @@ public class CommonCheck {
     private R<ErrorInfo> checkUserNumb(AllowConsumeValidationContext ctx) {
         // 校验账户信息
         String userNumb = ctx.getUserNumb();
-        R<ErrorInfo> result = checkUser(userNumb, userNumbMode, ctx);
+        R<ErrorInfo> result = checkUser(userNumb, ConsumeConstants.USER_NUMB, ctx);
         if (R.isError(result)) {
             return result;
         }
@@ -417,7 +409,7 @@ public class CommonCheck {
     private R<ErrorInfo> checkCardAfterUser(AllowConsumeValidationContext ctx) {
         R<ErrorInfo> result;
         Long userId = ctx.getUserAccountVo().getUserId();
-        result = checkCard(userId, cardIdMode, ctx);
+        result = checkCard(userId, ConsumeConstants.CARD_USER, ctx);
         if (R.isError(result)) {
             return result;
         }