licailing пре 4 година
родитељ
комит
107f8238d1

+ 4 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -135,4 +135,8 @@ public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpec
     @Transactional
     @Modifying
     int updateVipQuota(Long id, int vipQuota);
+
+    @Query("select c.vipQuota from Collection c where c.id = ?1")
+    Integer getVipQuota(Long id);
+
 }

+ 12 - 0
src/main/java/com/izouma/nineth/service/CollectionService.java

@@ -427,4 +427,16 @@ public class CollectionService {
             cacheService.clearCollection(id);
         }
     }
+
+    public synchronized Long decreaseQuota(Long id, int number) {
+        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_QUOTA + id);
+        if (ops.get() == null) {
+            Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getVipQuota(id))
+                    .orElse(0), 7, TimeUnit.DAYS);
+            log.info("创建redis额度:{}", success);
+        }
+        Long stock = ops.increment(-number);
+        rocketMQTemplate.convertAndSend(generalProperties.getUpdateQuotaTopic(), id);
+        return stock;
+    }
 }

+ 2 - 7
src/main/java/com/izouma/nineth/service/UserService.java

@@ -5,7 +5,6 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
 import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
 import com.huifu.adapay.core.exception.BaseAdaPayException;
 import com.izouma.nineth.TokenHistory;
-import com.izouma.nineth.config.AdapayProperties;
 import com.izouma.nineth.config.Constants;
 import com.izouma.nineth.domain.Collection;
 import com.izouma.nineth.domain.*;
@@ -34,7 +33,6 @@ import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.cache.annotation.CacheEvict;
-import org.springframework.context.ApplicationContext;
 import org.springframework.context.event.EventListener;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageImpl;
@@ -62,22 +60,19 @@ public class UserService {
     private SmsService            smsService;
     private StorageService        storageService;
     private JwtTokenUtil          jwtTokenUtil;
-    private CaptchaService        captchaService;
     private FollowService         followService;
     private FollowRepo            followRepo;
     private IdentityAuthRepo      identityAuthRepo;
     private SysConfigService      sysConfigService;
-    private AdapayService         adapayService;
     private UserBankCardRepo      userBankCardRepo;
     private InviteRepo            inviteRepo;
     private NFTService            nftService;
     private CacheService          cacheService;
-    private ApplicationContext    context;
     private TokenHistoryRepo      tokenHistoryRepo;
     private CollectionRepo        collectionRepo;
-    private AdapayProperties      adapayProperties;
     private AdapayMerchantService adapayMerchantService;
     private PointRecordRepo       pointRecordRepo;
+    private CollectionService     collectionService;
 
     public User update(User user) {
         User orig = userRepo.findById(user.getId()).orElseThrow(new BusinessException("无记录"));
@@ -214,7 +209,7 @@ public class UserService {
                                     .point(1)
                                     .build());
                             // 扣除藏品额度
-                            collectionRepo.updateVipQuota(collectionId, -1);
+                            collectionService.decreaseQuota(collectionId, 1);
                         }
                     }
                 }