Bladeren bron

Merge branch 'dev' into 2D展厅

licailing 4 jaren geleden
bovenliggende
commit
92256f1494

+ 22 - 2
src/main/java/com/izouma/nineth/listener/UpdateSaleListener.java

@@ -1,5 +1,6 @@
 package com.izouma.nineth.listener;
 
+import com.izouma.nineth.config.RedisKeys;
 import com.izouma.nineth.service.CollectionService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -7,6 +8,9 @@ import org.apache.rocketmq.spring.annotation.ConsumeMode;
 import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
 import org.apache.rocketmq.spring.core.RocketMQListener;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.data.redis.core.BoundHashOperations;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 @Service
@@ -19,10 +23,26 @@ import org.springframework.stereotype.Service;
 @ConditionalOnProperty(value = "general.notify-server", havingValue = "false", matchIfMissing = true)
 public class UpdateSaleListener implements RocketMQListener<Long> {
 
-    private CollectionService collectionService;
+    private CollectionService             collectionService;
+    private RedisTemplate<String, Object> redisTemplate;
 
     @Override
     public void onMessage(Long id) {
-        collectionService.syncSale(id);
+        BoundHashOperations<String, String, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
+        ops.increment(id.toString(), 1);
+    }
+
+    @Scheduled(fixedRate = 10000)
+    public void updateSale() {
+        BoundHashOperations<String, String, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
+        for (String id : ops.keys()) {
+            Long val = ops.increment(id, -1);
+            if (val <= 0) {
+                ops.delete(id);
+            } else if (val > 1) {
+                ops.put(id, 1);
+            }
+            collectionService.syncSale(Long.parseLong(id));
+        }
     }
 }

+ 3 - 22
src/main/java/com/izouma/nineth/listener/UpdateStockListener.java

@@ -1,6 +1,5 @@
 package com.izouma.nineth.listener;
 
-import com.izouma.nineth.config.RedisKeys;
 import com.izouma.nineth.service.CollectionService;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
@@ -8,9 +7,6 @@ import org.apache.rocketmq.spring.annotation.ConsumeMode;
 import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
 import org.apache.rocketmq.spring.core.RocketMQListener;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
-import org.springframework.data.redis.core.BoundHashOperations;
-import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
 
 @Service
@@ -22,26 +18,11 @@ import org.springframework.stereotype.Service;
         consumeMode = ConsumeMode.ORDERLY)
 @ConditionalOnProperty(value = "general.notify-server", havingValue = "false", matchIfMissing = true)
 public class UpdateStockListener implements RocketMQListener<Long> {
-    private CollectionService             collectionService;
-    private RedisTemplate<String, Object> redisTemplate;
+
+    private CollectionService collectionService;
 
     @Override
     public void onMessage(Long id) {
-        BoundHashOperations<String, Long, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
-        ops.increment(id, 1);
-    }
-
-    @Scheduled(fixedRate = 5000)
-    public void updateStock() {
-        BoundHashOperations<String, Long, Integer> ops = redisTemplate.boundHashOps(RedisKeys.UPDATE_SALE);
-        for (Long id : ops.keys()) {
-            Long val = ops.increment(id, -1);
-            if (val <= 0) {
-                ops.delete(id);
-            } else if (val > 1) {
-                ops.put(id, 1);
-            }
-            collectionService.syncStock(id);
-        }
+        collectionService.syncStock(id);
     }
 }

+ 6 - 2
src/main/java/com/izouma/nineth/repo/UserRepo.java

@@ -181,9 +181,13 @@ public interface UserRepo extends JpaRepository<User, Long>, JpaSpecificationExe
 
     List<User> findBySettleAccountIdIsNotNull();
 
+//    @Transactional
+//    @Modifying
+//    @Query("update User set vipPoint = vipPoint + ?2 where id = ?1")
+//    void updateVipPoint(Long id, int num);
+
     @Transactional
     @Modifying
-    @Query("update User set vipPoint = vipPoint + ?2 where id = ?1")
+    @Query("update User set vipPoint = ?2 where id = ?1")
     void updateVipPoint(Long id, int num);
-
 }

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

@@ -380,14 +380,14 @@ public class CollectionService {
         return stock;
     }
 
-    public synchronized Long getStock(Long id) {
+    public synchronized Integer getStock(Long id) {
         BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
-        Long stock = (Long) ops.get();
+        Integer stock = (Integer) ops.get();
         if (stock == null) {
             Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
                     .orElse(0), 7, TimeUnit.DAYS);
             log.info("创建redis库存:{}", success);
-            return (Long) ops.get();
+            return (Integer) ops.get();
         } else {
             return stock;
         }
@@ -414,7 +414,7 @@ public class CollectionService {
         return increaseSale(id, -number);
     }
 
-    //    @Debounce(key = "#id", delay = 500)
+    @Debounce(key = "#id", delay = 500)
     public void syncStock(Long id) {
         Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
         if (stock != null) {
@@ -424,7 +424,7 @@ public class CollectionService {
         }
     }
 
-    @Debounce(key = "#id", delay = 500)
+//    @Debounce(key = "#id", delay = 500)
     public void syncSale(Long id) {
         Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
         if (sale != null) {

+ 3 - 2
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -110,7 +110,7 @@ public class OrderService {
             throw new BusinessException("签名已过期");
         }
 
-        Long stock = collectionService.getStock(collectionId);
+        Integer stock = collectionService.getStock(collectionId);
         if (stock == null || stock <= 0) {
             throw new BusinessException("藏品已售罄", ErrorCode.SOLD_OUT);
         }
@@ -737,8 +737,9 @@ public class OrderService {
                 });
             }
             //加上积分
-            if (order.getVipPoint() > 0) {
+            if (ObjectUtils.isNotEmpty(order.getVipPoint()) && order.getVipPoint() > 0) {
                 userRepo.updateVipPoint(order.getUserId(), order.getVipPoint());
+                log.info("取消加积分用户ID:{},订单ID:{},积分:{}", order.getUserId(), order.getId(), order.getVipPoint());
             }
 
             rocketMQTemplate.syncSend(generalProperties.getUpdateStockTopic(), order.getCollectionId(), 10000);

+ 5 - 4
src/main/java/com/izouma/nineth/service/UserService.java

@@ -202,6 +202,10 @@ public class UserService {
                     if (point <= 0) {
                         long count = userRepo.countAllByCollectionIdAndCollectionInvitor(collectionId, invitor);
                         if (count >= collection.getAssignment()) {
+                            // 扣除藏品额度
+                            if (ObjectUtils.isNotEmpty(collection.getVipQuota())) {
+                                collectionService.decreaseQuota(collectionId, 1);
+                            }
                             userRepo.updateVipPoint(invitor, 1);
                             pointRecordRepo.save(PointRecord.builder()
                                     .collectionId(collectionId)
@@ -209,10 +213,7 @@ public class UserService {
                                     .type("VIP_POINT")
                                     .point(1)
                                     .build());
-                            // 扣除藏品额度
-                            if (ObjectUtils.isNotEmpty(collection.getVipQuota())) {
-                                collectionService.decreaseQuota(collectionId, 1);
-                            }
+
                         }
                     }
                 }

+ 12 - 0
src/test/java/com/izouma/nineth/service/UserServiceTest.java

@@ -185,4 +185,16 @@ public class UserServiceTest extends ApplicationTests {
                 .idNo(identityAuth.getIdNo())
                 .build());
     }
+
+    @Test
+    public void test1() {
+        List<User> users = userRepo.findAll();
+        users.forEach(user -> {
+            if (user.getVipPoint() > 1) {
+                user.setVipPoint(1);
+            } else {
+
+            }
+        });
+    }
 }