xiongzhu před 4 roky
rodič
revize
2cfaf13fbd

+ 6 - 1
src/main/java/com/izouma/nineth/domain/Order.java

@@ -33,7 +33,12 @@ import java.util.List;
 
 
 @Data
 @Data
 @Entity
 @Entity
-@Table(name = "order_info")
+@Table(name = "order_info", indexes = {
+        @Index(columnList = "userId"),
+        @Index(columnList = "status"),
+        @Index(columnList = "assetId"),
+        @Index(columnList = "collectionId")
+})
 @AllArgsConstructor
 @AllArgsConstructor
 @NoArgsConstructor
 @NoArgsConstructor
 @Builder
 @Builder

+ 22 - 0
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -222,6 +222,28 @@ public class OrderService {
         }
         }
     }
     }
 
 
+    public Object checkLimit(Long collectionId, Long userId) {
+        Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
+        int limit = collection.getMaxCount();
+        int count = 0;
+        if (collection.getMaxCount() > 0) {
+            if (StringUtils.isNotBlank(collection.getCountId())) {
+                count = orderRepo.countByUserIdAndCountIdAndStatusIn(userId, collection.getCountId(),
+                        Arrays.asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
+            } else {
+                count = orderRepo.countByUserIdAndCollectionIdAndStatusIn(userId, collectionId,
+                        Arrays.asList(OrderStatus.FINISH, OrderStatus.NOT_PAID, OrderStatus.PROCESSING));
+            }
+            if (count >= collection.getMaxCount()) {
+                throw new BusinessException("限购" + collection.getMaxCount() + "件");
+            }
+        }
+        Map<String, Object> map = new HashMap<>();
+        map.put("limit", limit);
+        map.put("count", count);
+        return map;
+    }
+
     public void payOrderAlipay(Long id, Model model) {
     public void payOrderAlipay(Long id, Model model) {
         try {
         try {
             Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));
             Order order = orderRepo.findByIdAndDelFalse(id).orElseThrow(new BusinessException("订单不存在"));

+ 5 - 3
src/main/java/com/izouma/nineth/web/OrderController.java

@@ -22,15 +22,12 @@ import org.springframework.data.domain.Page;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
-import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.IOException;
-import java.time.OffsetTime;
 import java.util.HashMap;
 import java.util.HashMap;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Optional;
-import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 @RestController
 @RestController
@@ -160,5 +157,10 @@ public class OrderController extends BaseController {
     public Object createResult(@RequestParam String id) {
     public Object createResult(@RequestParam String id) {
         return orderService.queryCreateOrder(id);
         return orderService.queryCreateOrder(id);
     }
     }
+
+    @GetMapping("/checkLimit")
+    public Object checkLimit(@RequestParam Long collectionId) {
+        return orderService.checkLimit(collectionId, SecurityUtils.getAuthenticatedUser().getId());
+    }
 }
 }