Просмотр исходного кода

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

autumnal_wind 11 месяцев назад
Родитель
Сommit
0606ce5179

+ 6 - 0
ruoyi-api/ruoyi-api-consume/src/main/java/org/dromara/consume/api/RemoteConsumeService.java

@@ -5,6 +5,8 @@ import org.dromara.common.core.domain.model.ErrorInfo;
 import org.dromara.consume.api.domain.bo.RemoteConsumeBo;
 import org.dromara.consume.api.domain.bo.RemoteResultDto;
 
+import java.math.BigDecimal;
+
 /**
  * @ClassName RemoteConsumeService
  * @Description 消费远程服务接口
@@ -54,4 +56,8 @@ public interface RemoteConsumeService {
      * @return 返回一个包含错误信息的响应对象,如果操作成功则data为null,如果失败则data包含具体的错误信息
      */
     RemoteResultDto dealHikUploadOffLineRecord(RemoteConsumeBo remoteBo);
+
+    BigDecimal getUserTotalBalance(Long userId);
+
+    BigDecimal cacheUserTotalBalance(Long userId, BigDecimal balance);
 }

+ 18 - 9
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/business/errfill/ErrFillBusiness.java

@@ -4,6 +4,8 @@ import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.json.JSONUtil;
 import lombok.RequiredArgsConstructor;
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.config.annotation.DubboService;
 import org.dromara.backstage.basics.domain.vo.PtMealTypeVo;
 import org.dromara.backstage.basics.service.IPtMealtypeService;
 import org.dromara.backstage.cardCenter.service.IPtCardService;
@@ -32,6 +34,7 @@ import org.dromara.common.core.exception.consume.BagException;
 import org.dromara.common.core.exception.consume.ConsumeException;
 import org.dromara.common.core.utils.RecordIdUtils;
 import org.dromara.common.redis.utils.RedisUtils;
+import org.dromara.consume.api.RemoteConsumeService;
 import org.jetbrains.annotations.NotNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -67,6 +70,9 @@ public class ErrFillBusiness {
     private final IXfUserTotalService userTotalService;
     private final IXfTermTotalService termTotalService;
 
+    @DubboReference
+    private final RemoteConsumeService remoteConsumeService;
+
     @Transactional(rollbackFor = ConsumeException.class)
     public PtBagVo createErrFillRecord(ConsumptionBo bo) {
         java.lang.String message;
@@ -205,7 +211,7 @@ public class ErrFillBusiness {
      * @param consumeId 消费明细Id
      * @param balance   卡余
      */
-    private void updateConsumeRecordBalance(java.lang.String consumeId, BigDecimal balance){
+    private void updateConsumeRecordBalance(String consumeId, BigDecimal balance){
         XfConsumeDetailBo consumeDetailBo = new XfConsumeDetailBo();
         consumeDetailBo.setConsumeId(consumeId);
         consumeDetailBo.setCardValue(balance);
@@ -299,18 +305,21 @@ public class ErrFillBusiness {
      * @return 生成的记录Id
      */
     @NotNull
-    private java.lang.String getRecordKeyId(ConsumptionBo bo, PtUserAccountVo accountVo, PtBagVo bagVo) {
+    private String getRecordKeyId(ConsumptionBo bo, PtUserAccountVo accountVo, PtBagVo bagVo) {
         return RecordIdUtils.getRecordId(new Date(), Short.parseShort(bo.getTermNo().toString()), 0, accountVo.getUserNo().intValue(), Integer.parseInt(bagVo.getBagCode()));
     }
 
     private void updateCacheBalanceByUserId(Long id, BigDecimal dealMoney) {
-        String userId = String.valueOf(id);
-        BigDecimal balance = BigDecimal.ZERO;
-        if (RedisUtils.getCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId) != null) {
-            balance = RedisUtils.getCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId);
-        }
+        BigDecimal balance = remoteConsumeService.getUserTotalBalance(id);
         balance = balance.add(dealMoney);
-        RedisUtils.delCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId);
-        RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId, balance);
+        remoteConsumeService.cacheUserTotalBalance(id, balance);
+        // String userId = String.valueOf(id);
+        // BigDecimal balance = BigDecimal.ZERO;
+        // if (RedisUtils.getCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId) != null) {
+        //     balance = RedisUtils.getCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId);
+        // }
+        // balance = balance.add(dealMoney);
+        // RedisUtils.delCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId);
+        // RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId, balance);
     }
 }

+ 7 - 8
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/business/payments/PayOrderBusiness.java

@@ -6,6 +6,7 @@ import cn.hutool.core.util.ObjUtil;
 import cn.hutool.json.JSONUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.dubbo.config.annotation.DubboReference;
 import org.dromara.backstage.basics.domain.vo.PtWorkstationVo;
 import org.dromara.backstage.cardCenter.domain.bo.PtSubsidyitemBo;
 import org.dromara.backstage.cardCenter.domain.vo.PtSubsidyitemVo;
@@ -21,6 +22,7 @@ import org.dromara.common.core.domain.R;
 import org.dromara.common.core.enums.*;
 import org.dromara.common.core.exception.consume.ConsumeException;
 import org.dromara.common.redis.utils.RedisUtils;
+import org.dromara.consume.api.RemoteConsumeService;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -46,10 +48,12 @@ import java.util.Map;
 @Service
 @Slf4j
 public class PayOrderBusiness {
-
     private final PayBaseBusiness payBaseBusiness;
     private final ThreadPoolTaskExecutor threadPoolTaskExecutor;
 
+    @DubboReference
+    private final RemoteConsumeService remoteConsumeService;
+
     /**
      * 生成收支原始订单信息,写t_xf_creditAccountBack表
      *
@@ -288,13 +292,8 @@ public class PayOrderBusiness {
     }
 
     private void updateCacheBalanceByUserId(Long id, BigDecimal dealMoney) {
-        String userId = String.valueOf(id);
-        BigDecimal balance = BigDecimal.ZERO;
-        if (RedisUtils.getCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId) != null) {
-            balance = RedisUtils.getCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId);
-        }
+        BigDecimal balance = remoteConsumeService.getUserTotalBalance(id);
         balance = balance.add(dealMoney);
-        RedisUtils.delCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId);
-        RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, userId, balance);
+        remoteConsumeService.cacheUserTotalBalance(id, balance);
     }
 }

+ 1 - 2
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/payment/service/IPtBagService.java

@@ -6,6 +6,7 @@ import org.dromara.backstage.payment.domain.vo.PtBagVo;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
 
+import java.math.BigDecimal;
 import java.util.Collection;
 import java.util.List;
 
@@ -105,6 +106,4 @@ public interface IPtBagService {
      * @return 账户钱包
      */
     PtBagVo queryByUserBagCode(Long userId,String bagCode);
-
-
 }

+ 3 - 0
ruoyi-modules/ruoyi-backstage/src/main/java/org/dromara/backstage/payment/service/impl/PtBagServiceImpl.java

@@ -13,6 +13,7 @@ import org.dromara.backstage.payment.domain.bo.PtBagBo;
 import org.dromara.backstage.payment.domain.vo.PtBagVo;
 import org.dromara.backstage.payment.mapper.PtBagMapper;
 import org.dromara.backstage.payment.service.IPtBagService;
+import org.dromara.common.core.constant.CacheNames;
 import org.dromara.common.core.enums.BalanceUpdateEnum;
 import org.dromara.common.core.exception.consume.BagException;
 import org.dromara.common.core.utils.MapstructUtils;
@@ -21,6 +22,7 @@ import org.dromara.common.core.utils.StringUtils;
 import org.dromara.common.encrypt.utils.YcEncryptUtil;
 import org.dromara.common.mybatis.core.page.PageQuery;
 import org.dromara.common.mybatis.core.page.TableDataInfo;
+import org.springframework.cache.annotation.CachePut;
 import org.springframework.stereotype.Service;
 
 import java.math.BigDecimal;
@@ -278,6 +280,7 @@ public class PtBagServiceImpl implements IPtBagService {
         }
         return entity;
     }
+
     /**
      * 组装充值钱包数据
      * 1.设置充值后余额=账户原余额+充值金额

+ 0 - 3
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/business/InitBusiness.java

@@ -176,11 +176,8 @@ public class InitBusiness {
             sumMap.merge(userId, amount, BigDecimal::add);
         }
         for (Map.Entry<Long, BigDecimal> entry : sumMap.entrySet()) {
-            // RedisUtils.setCacheMapValue(CacheNames.USER_TOTAL_BALANCE, entry.getKey(), entry.getValue());
             bagService.cacheUserTotalBalance(entry.getKey(), entry.getValue());
         }
-
-        // RedisUtils.expire(CacheNames.USER_TOTAL_BALANCE, Duration.ofHours(5));
         log.info("初始化人员余额参数完成");
     }
 

+ 14 - 0
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/dubbo/RemoteConsumeServiceImpl.java

@@ -19,8 +19,11 @@ import org.dromara.server.consume.business.ArcFaceBusiness;
 import org.dromara.server.consume.business.BaseBusiness;
 import org.dromara.server.consume.business.ConsumeBusiness;
 import org.dromara.server.consume.domain.convert.RemoteConsumeBoConvert;
+import org.dromara.server.consume.service.IPtBagService;
 import org.springframework.stereotype.Service;
 
+import java.math.BigDecimal;
+
 /**
  * @ClassName RemoteConsumeServiceImpl
  * @Description TODO
@@ -38,6 +41,7 @@ public class RemoteConsumeServiceImpl implements RemoteConsumeService {
     private final ArcFaceBusiness faceBusiness;
     private final BaseBusiness baseBusiness;
     private final DefaultConfig defaultConfig;
+    private final IPtBagService bagService;
 
 
     /**
@@ -101,4 +105,14 @@ public class RemoteConsumeServiceImpl implements RemoteConsumeService {
 
         return new RemoteResultDto(result, updatedRemote);
     }
+
+    @Override
+    public BigDecimal getUserTotalBalance(Long userId) {
+        return bagService.getUserTotalBalance(userId);
+    }
+
+    @Override
+    public BigDecimal cacheUserTotalBalance(Long userId, BigDecimal balance) {
+        return bagService.cacheUserTotalBalance(userId, balance);
+    }
 }

+ 5 - 6
ruoyi-server/ruoyi-server-consume/src/main/java/org/dromara/server/consume/service/impl/PtBagServiceImpl.java

@@ -342,7 +342,11 @@ public class PtBagServiceImpl implements IPtBagService {
 
         return baseMapper.selectVoList(queryWrapper);
     }
-
+    @Override
+    @CachePut(cacheNames = CacheNames.USER_TOTAL_BALANCE, key = "#userId")
+    public BigDecimal cacheUserTotalBalance(Long userId, BigDecimal balance) {
+        return balance;
+    }
     /**
      * 组装充值钱包数据
      * 1.设置充值后余额=账户原余额+充值金额
@@ -446,9 +450,4 @@ public class PtBagServiceImpl implements IPtBagService {
         bag.setEncryptBalance(encryptValue);
     }
 
-    @Override
-    @CachePut(cacheNames = CacheNames.USER_TOTAL_BALANCE, key = "#userId")
-    public BigDecimal cacheUserTotalBalance(Long userId, BigDecimal balance) {
-        return balance;
-    }
 }