xiongzhu 3 vuotta sitten
vanhempi
commit
f757ec1b9d

+ 14 - 0
src/main/java/com/izouma/nineth/config/RedisKeys.java

@@ -0,0 +1,14 @@
+package com.izouma.nineth.config;
+
+public class RedisKeys {
+    public static final String COLLECTION = "collection::";
+
+    public static final String CREATE_ORDER = "createOrder::";
+
+    public static final String COLLECTION_STOCK = "collectionStock::";
+
+    public static final String COLLECTION_SALE = "collectionSale::";
+
+    public static final String PAY_RECORD = "payRecord::";
+
+}

+ 2 - 1
src/main/java/com/izouma/nineth/listener/CreateOrderListener.java

@@ -1,5 +1,6 @@
 package com.izouma.nineth.listener;
 
+import com.izouma.nineth.config.RedisKeys;
 import com.izouma.nineth.domain.Order;
 import com.izouma.nineth.event.CreateOrderEvent;
 import com.izouma.nineth.exception.BusinessException;
@@ -47,6 +48,6 @@ public class CreateOrderListener implements RocketMQListener<CreateOrderEvent> {
             map.put("success", false);
             map.put("data", e.getMessage());
         }
-        redisTemplate.boundValueOps("createOrder::" + event.getId()).set(map, 1, TimeUnit.DAYS);
+        redisTemplate.boundValueOps(RedisKeys.CREATE_ORDER + event.getId()).set(map, 1, TimeUnit.DAYS);
     }
 }

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

@@ -3,6 +3,7 @@ package com.izouma.nineth.service;
 import com.alibaba.fastjson.JSON;
 import com.izouma.nineth.annotations.Debounce;
 import com.izouma.nineth.config.GeneralProperties;
+import com.izouma.nineth.config.RedisKeys;
 import com.izouma.nineth.domain.Collection;
 import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.CollectionDTO;
@@ -74,11 +75,11 @@ public class CollectionService {
             onShelfTask(collection);
         }
         for (CollectionStockAndSale collection : collectionRepo.getStockAndSale()) {
-            if (redisTemplate.opsForValue().get("collectionStock::" + collection.getId()) == null) {
-                redisTemplate.opsForValue().set("collectionStock::" + collection.getId(), collection.getStock());
+            if (redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + collection.getId()) == null) {
+                redisTemplate.opsForValue().set(RedisKeys.COLLECTION_STOCK + collection.getId(), collection.getStock());
             }
-            if (redisTemplate.opsForValue().get("collectionSale::" + collection.getId()) == null) {
-                redisTemplate.opsForValue().set("collectionSale::" + collection.getId(), collection.getSale());
+            if (redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + collection.getId()) == null) {
+                redisTemplate.opsForValue().set(RedisKeys.COLLECTION_SALE + collection.getId(), collection.getSale());
             }
         }
     }
@@ -138,8 +139,8 @@ public class CollectionService {
         }
         record = collectionRepo.save(record);
         onShelfTask(record);
-        redisTemplate.opsForValue().set("collectionStock::" + record.getId(), record.getStock());
-        redisTemplate.opsForValue().set("collectionSale::" + record.getId(), record.getSale());
+        redisTemplate.opsForValue().set(RedisKeys.COLLECTION_STOCK + record.getId(), record.getStock());
+        redisTemplate.opsForValue().set(RedisKeys.COLLECTION_SALE + record.getId(), record.getSale());
         return record;
     }
 
@@ -359,7 +360,7 @@ public class CollectionService {
     }
 
     public synchronized Long increaseStock(Long id, int number) {
-        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("collectionStock::" + id);
+        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_STOCK + id);
         if (ops.get() == null) {
             Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
                     .orElse(0), 7, TimeUnit.DAYS);
@@ -375,7 +376,7 @@ public class CollectionService {
     }
 
     public synchronized Long increaseSale(Long id, int number) {
-        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("collectionSale::" + id);
+        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps(RedisKeys.COLLECTION_SALE + id);
         if (ops.get() == null) {
             Boolean success = ops.setIfAbsent(Optional.ofNullable(collectionRepo.getStock(id))
                     .orElse(0), 7, TimeUnit.DAYS);
@@ -392,7 +393,7 @@ public class CollectionService {
 
     @Debounce(key = "#id", delay = 500)
     public void syncStock(Long id) {
-        Integer stock = (Integer) redisTemplate.opsForValue().get("collectionStock::" + id);
+        Integer stock = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + id);
         if (stock != null) {
             log.info("同步库存信息{}", id);
             collectionRepo.updateStock(id, stock);
@@ -402,7 +403,7 @@ public class CollectionService {
 
     @Debounce(key = "#id", delay = 500)
     public void syncSale(Long id) {
-        Integer sale = (Integer) redisTemplate.opsForValue().get("collectionSale::" + id);
+        Integer sale = (Integer) redisTemplate.opsForValue().get(RedisKeys.COLLECTION_SALE + id);
         if (sale != null) {
             log.info("同步销量信息{}", id);
             collectionRepo.updateSale(id, sale);

+ 21 - 19
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -17,10 +17,7 @@ import com.huifu.adapay.core.exception.BaseAdaPayException;
 import com.huifu.adapay.model.AdapayCommon;
 import com.huifu.adapay.model.Payment;
 import com.huifu.adapay.model.Refund;
-import com.izouma.nineth.config.AdapayProperties;
-import com.izouma.nineth.config.AlipayProperties;
-import com.izouma.nineth.config.GeneralProperties;
-import com.izouma.nineth.config.WxPayProperties;
+import com.izouma.nineth.config.*;
 import com.izouma.nineth.domain.Collection;
 import com.izouma.nineth.domain.*;
 import com.izouma.nineth.dto.PageQuery;
@@ -425,7 +422,7 @@ public class OrderService {
             log.info("createOrderResponse {}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
             AdapayService.checkSuccess(response);
 
-            BoundSetOperations<String, Object> ops = redisTemplate.boundSetOps("orderNotify::" + order.getId());
+            BoundSetOperations<String, Object> ops = redisTemplate.boundSetOps(RedisKeys.PAY_RECORD + order.getId());
             ops.add(MapUtils.getString(response, "id"));
             ops.expire(7, TimeUnit.DAYS);
         }
@@ -478,9 +475,7 @@ public class OrderService {
     public void notifyOrder(Long orderId, PayMethod payMethod, String transactionId) {
         log.info("订单回调 orderId: {}, payMethod: {}, transactionId: {}", orderId, payMethod, transactionId);
 
-        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("orderLock::" + orderId);
-        Boolean flag = ops.setIfAbsent(1, 1, TimeUnit.DAYS);
-        if (!Boolean.TRUE.equals(flag)) {
+        if (!getOrderLock(orderId)) {
             log.info("订单回调失败 orderId: {} redis锁定, 重新发送到队列", orderId);
             rocketMQTemplate.syncSend(generalProperties.getOrderNotifyTopic(),
                     new OrderNotifyEvent(orderId, payMethod, transactionId, System.currentTimeMillis()));
@@ -502,7 +497,7 @@ public class OrderService {
                     BlindBoxItem winItem = null;
                     try {
                         winItem = collectionService.draw(collection.getId());
-                    } catch (BusinessException e) {
+                    } catch (BusinessException ignored) {
                     }
                     if (winItem == null) {
                         log.info("抽卡失败退款 orderId: {}", orderId);
@@ -519,7 +514,7 @@ public class OrderService {
                             e.printStackTrace();
                         }
                         orderRepo.save(order);
-                        return;
+                        throw new BusinessException("抽卡失败, 已退款 " + orderId);
                     }
                     log.info("抽卡成功 orderId: {}, collectionId: {}, winCollectionId: {}", orderId, collection.getId(), winItem.getCollectionId());
                     order.setWinCollectionId(winItem.getCollectionId());
@@ -565,7 +560,7 @@ public class OrderService {
             errorOrderRepo.save(errorOrder);
 
         }
-        redisTemplate.delete("orderLock::" + orderId);
+        releaseOrderLock(orderId);
     }
 
 
@@ -605,9 +600,7 @@ public class OrderService {
     }
 
     public void cancel(Order order) {
-        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("orderLock::" + order.getId());
-        Boolean flag = ops.setIfAbsent(1, 1, TimeUnit.DAYS);
-        if (!Boolean.TRUE.equals(flag)) {
+        if (!getOrderLock(order.getId())) {
             log.error("订单取消失败 {}, redis锁了", order.getId());
             return;
         }
@@ -617,7 +610,7 @@ public class OrderService {
                 throw new BusinessException("已支付订单无法取消");
             }
 
-            Set<Object> transactionIds = redisTemplate.opsForSet().members("orderNotify::" + order.getId());
+            Set<Object> transactionIds = redisTemplate.opsForSet().members(RedisKeys.PAY_RECORD + order.getId());
             if (transactionIds != null && transactionIds.size() > 0) {
                 if (transactionIds.parallelStream().anyMatch(transactionId -> {
                     try {
@@ -629,8 +622,7 @@ public class OrderService {
                     }
                     return false;
                 })) {
-                    log.info("订单已经支付成功或待支付,不能取消 {}", order.getId());
-                    return;
+                    throw new BusinessException("订单已经支付成功或待支付,不能取消 " + order.getId());
                 }
             }
 
@@ -664,7 +656,7 @@ public class OrderService {
         } catch (Exception e) {
             log.error("订单取消错误 orderId: " + order.getId(), e);
         }
-        redisTemplate.delete("orderLock::" + order.getId());
+        releaseOrderLock(order.getId());
     }
 
     public void refundCancelled(Order order) {
@@ -739,6 +731,16 @@ public class OrderService {
     }
 
     public Object queryCreateOrder(String id) {
-        return redisTemplate.opsForValue().get("createOrder::" + id);
+        return redisTemplate.opsForValue().get(RedisKeys.CREATE_ORDER + id);
+    }
+
+    public boolean getOrderLock(Long orderId) {
+        BoundValueOperations<String, Object> ops = redisTemplate.boundValueOps("orderLock::" + orderId);
+        Boolean flag = ops.setIfAbsent(1, 1, TimeUnit.DAYS);
+        return Boolean.TRUE.equals(flag);
+    }
+
+    public void releaseOrderLock(Long orderId) {
+        redisTemplate.delete("orderLock::" + orderId);
     }
 }

+ 3 - 2
src/test/java/com/izouma/nineth/RedisTest.java

@@ -1,5 +1,6 @@
 package com.izouma.nineth;
 
+import com.izouma.nineth.config.RedisKeys;
 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
@@ -10,11 +11,11 @@ public class RedisTest extends ApplicationTests {
 
     @Test
     public void testOrder() {
-        System.out.println(redisTemplate.opsForValue().get("createOrder::931502789098471424"));
+        System.out.println(redisTemplate.opsForValue().get(RedisKeys.CREATE_ORDER + "931502789098471424"));
     }
 
     @Test
     public void testasdf() {
-        System.out.println(redisTemplate.opsForValue().get("collectionStock::sdfasdf"));
+        System.out.println(redisTemplate.opsForValue().get(RedisKeys.COLLECTION_STOCK + "sdfasdf"));
     }
 }