Kaynağa Gözat

api次数逻辑优化

dsx 2 yıl önce
ebeveyn
işleme
ee4949f34d

+ 17 - 14
src/main/java/com/fdkankan/openApi/service/system/impl/AccountServiceImpl.java

@@ -129,7 +129,7 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
            throw new BusinessException(ErrorCode.PARAM_ERROR);
         }
         Integer count = param.getCount();
-        if(CountType.NO_LIMIT.code() != param.getCountType() && Objects.isNull(count)){
+        if(CountType.NO_LIMIT.code() != param.getCountType() && (Objects.isNull(count) || count < 1)){
             throw new BusinessException(ErrorCode.FAILURE_CODE_10004);
         }else{
             count = -1;
@@ -182,35 +182,38 @@ public class AccountServiceImpl extends ServiceImpl<IAccountMapper, Account> imp
 
 
         UserAuthInfo userAuthInfo = userAuthService.findByAccountId(account.getId());
-
-        Integer totalCount = userAuthInfo.getTotalCount();
-        if(totalCount < 0){
-            totalCount = 0;
-        }
-        Integer count = param.getCount();
         if(Objects.nonNull(param.getCountType())){
+            boolean initCallCount = false;
+            boolean dbIsInfinite = false;//原数据中是否无限制
+            Integer totalCount = userAuthInfo.getTotalCount();
+            if(totalCount < 0){
+                totalCount = 0;
+                dbIsInfinite = true;
+            }
+            boolean currentIsInfinite = false;//当前修改数据中是否无限制
+            Integer count = param.getCount();
             if(CountType.NO_LIMIT.code() == param.getCountType()){
                 totalCount = -1;
+                currentIsInfinite = true;
             }else if(CountType.REDUCE.code() == param.getCountType()){
                 totalCount  -= count;
                 totalCount = totalCount < 0 ? 0 : totalCount;
             }else{
                 totalCount += count;
             }
+            userAuthInfo.setTotalCount(totalCount);
+            if(dbIsInfinite != currentIsInfinite){//无限制改为有限制或者有限制改为无限制时,需要初始化call_count为0
+                userAuthInfo.setCallCount(0);
+            }
+            //设置redis缓存
+            userAuthService.setReidsCount(userAuthInfo.getAppKey(), count, CountType.NO_LIMIT.code() == param.getCountType());
         }
 
         userAuthInfo.setUserId(account.getUserId());
-        userAuthInfo.setTotalCount(totalCount);
-        if(totalCount == -1){//如果由限制改为无限制,调用次数置为初始状态0
-            userAuthInfo.setCallCount(0);
-        }
         userAuthInfo.setState(param.getState());
         userAuthInfo.setUpdaterId(param.getUpdaterId());
         userAuthService.updateById(userAuthInfo);
 
-        //设置redis缓存
-        userAuthService.setReidsCount(userAuthInfo.getAppKey(), count, CountType.NO_LIMIT.code() == param.getCountType());
-
         return ResultData.ok();
     }