瀏覽代碼

商品限购

yuhongqi 1 月之前
父節點
當前提交
058d02ce12

+ 5 - 0
fs-service/src/main/java/com/fs/hisStore/mapper/FsStoreOrderItemScrmMapper.java

@@ -305,4 +305,9 @@ public interface FsStoreOrderItemScrmMapper
     List<FsStoreOrderItemVO> selectFsStoreOrderItemListByOrderIds(@Param("orderIds")List<Long> orderIds);
 
     String selectFsStoreOrderItemByOrderId(@Param("orderId") Long orderId);
+
+
+    @Select("select product_id from fs_store_order_item_scrm where order_code = #{orderCode}")
+    Long selectProductIdByOrderCode(@Param("orderCode") String orderCode);
+
 }

+ 2 - 0
fs-service/src/main/java/com/fs/hisStore/service/IFsStoreOrderItemScrmService.java

@@ -77,4 +77,6 @@ public interface IFsStoreOrderItemScrmService
     List<FsStoreOrderItemListDVO> selectFsStoreOrderItemListDVOByOrderId(Long orderId);
 
     String selectFsStoreOrderItemByOrderId(Long orderId);
+
+    Long selectProductIdByOrderCode(String orderCode);
 }

+ 16 - 0
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreAfterSalesScrmServiceImpl.java

@@ -964,10 +964,26 @@ public class FsStoreAfterSalesScrmServiceImpl implements IFsStoreAfterSalesScrmS
         if(order.getTuiUserId()!=null&&order.getTuiUserId()>0){
             userService.subTuiMoney(order);
         }
+        // 删除限购记录
+        this.deletePurchaseLimitRecordsForStoreOrder(order);
         return R.ok();
     }
 
+    @Autowired
+    private IFsStoreProductPurchaseLimitScrmService purchaseLimitService;
 
+    /**
+     * 删除限购记录(用于直播订单)
+     * @param fsStoreOrderScrm 订单
+     */
+    private void deletePurchaseLimitRecordsForStoreOrder(FsStoreOrderScrm fsStoreOrderScrm) {
+        Long productId = fsStoreOrderItemMapper.selectProductIdByOrderCode(fsStoreOrderScrm.getOrderCode());
+
+        // 减少限购数量
+        Long userId = fsStoreOrderScrm.getUserId();
+        Integer num = Math.toIntExact(fsStoreOrderScrm.getTotalNum());
+        purchaseLimitService.decreasePurchaseLimit(productId, userId, num);
+    }
     @Autowired
     private FsJstAftersalePushScrmMapper fsJstAftersalePushMapper;
 

+ 5 - 0
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreOrderItemScrmServiceImpl.java

@@ -136,4 +136,9 @@ public class FsStoreOrderItemScrmServiceImpl implements IFsStoreOrderItemScrmSer
     public String selectFsStoreOrderItemByOrderId(Long orderId) {
         return fsStoreOrderItemMapper.selectFsStoreOrderItemByOrderId(orderId);
     }
+
+    @Override
+    public Long selectProductIdByOrderCode(String orderCode) {
+        return fsStoreOrderItemMapper.selectProductIdByOrderCode(orderCode);
+    }
 }

+ 8 - 9
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreOrderScrmServiceImpl.java

@@ -1096,12 +1096,7 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
                 }
                 fsStoreOrderItemMapper.insertFsStoreOrderItem(item);
                 listOrderItem.add(item);
-                
-                // 记录限购数量(订单创建成功后记录)
-                FsStoreProductScrm product = productService.selectFsStoreProductById(vo.getProductId());
-                if (product != null && product.getPurchaseLimit() != null && product.getPurchaseLimit() > 0) {
-                    purchaseLimitService.increasePurchaseLimit(vo.getProductId(), userId, vo.getCartNum());
-                }
+
             }
             if (listOrderItem.size() > 0) {
                 String itemJson = JSONUtil.toJsonStr(listOrderItem);
@@ -1297,8 +1292,6 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
             this.refundCoupon(order);
             //退回库存
             this.refundStock(order);
-            // 删除限购记录
-            this.deletePurchaseLimitRecords(order);
             fsStoreOrderMapper.cancelOrder(orderId);
             //添加记录
             orderStatusService.create(order.getId(), OrderLogEnum.CANCEL_ORDER.getValue(),
@@ -2052,7 +2045,13 @@ public class FsStoreOrderScrmServiceImpl implements IFsStoreOrderScrmService {
         if (storeOrder.getCompanyId() != null) {
             addOrderAudit(order);
         }
-
+        try {
+            // 记录限购数量(订单创建成功后记录)
+            Long productId = storeOrderItemService.selectProductIdByOrderCode(order.getOrderCode());
+            purchaseLimitService.increasePurchaseLimit(productId, order.getUserId(), Math.toIntExact(order.getTotalNum()));
+        } catch (Exception e) {
+            log.error("创建限购商品失败:{}",e.getMessage());
+        }
 
     return "SUCCESS";
 

+ 102 - 22
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreProductPurchaseLimitScrmServiceImpl.java

@@ -1,10 +1,22 @@
 package com.fs.hisStore.service.impl;
 
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import com.fs.common.constant.LiveKeysConstant;
+import com.fs.common.core.redis.RedisCache;
 import com.fs.common.utils.DateUtils;
+import com.fs.hisStore.domain.FsStoreProductAttrScrm;
+import com.fs.hisStore.domain.FsStoreProductAttrValueScrm;
 import com.fs.hisStore.domain.FsStoreProductPurchaseLimitScrm;
+import com.fs.hisStore.domain.FsStoreProductScrm;
+import com.fs.hisStore.mapper.FsStoreProductAttrScrmMapper;
+import com.fs.hisStore.mapper.FsStoreProductAttrValueScrmMapper;
 import com.fs.hisStore.mapper.FsStoreProductPurchaseLimitScrmMapper;
+import com.fs.hisStore.mapper.FsStoreProductScrmMapper;
 import com.fs.hisStore.service.IFsStoreProductPurchaseLimitScrmService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -21,6 +33,16 @@ public class FsStoreProductPurchaseLimitScrmServiceImpl implements IFsStoreProdu
     @Autowired
     private FsStoreProductPurchaseLimitScrmMapper fsStoreProductPurchaseLimitMapper;
 
+    @Autowired
+    private RedisCache redisCache;
+    @Autowired
+    private FsStoreProductScrmMapper fsStoreProductScrmMapper;
+    @Autowired
+    private FsStoreProductAttrScrmMapper fsStoreProductAttrScrmMapper;
+    @Autowired
+    private FsStoreProductAttrValueScrmMapper fsStoreProductAttrValueScrmMapper;
+
+
     /**
      * 查询商品限购
      *
@@ -119,19 +141,48 @@ public class FsStoreProductPurchaseLimitScrmServiceImpl implements IFsStoreProdu
     @Override
     public int increasePurchaseLimit(Long productId, Long userId, Integer num)
     {
-        FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
-        if (limit == null) {
-            // 创建新记录
-            limit = new FsStoreProductPurchaseLimitScrm();
-            limit.setProductId(productId);
-            limit.setUserId(userId);
-            limit.setNum(num);
-            return insertFsStoreProductPurchaseLimit(limit);
+        String cacheKey = String.format(LiveKeysConstant.PRODUCT_DETAIL_CACHE, productId);
+        Map<String, Object> cachedData = redisCache.getCacheObject(cacheKey);
+
+        FsStoreProductScrm product;
+        List<FsStoreProductAttrScrm> productAttr;
+        List<FsStoreProductAttrValueScrm> productValues;
+
+        if (cachedData != null) {
+            // 从缓存中获取数据
+            product = (FsStoreProductScrm) cachedData.get("product");
         } else {
-            // 更新现有记录
-            limit.setNum(limit.getNum() + num);
-            return updateFsStoreProductPurchaseLimit(limit);
+            // 缓存中没有,从数据库查询
+            product = fsStoreProductScrmMapper.selectFsStoreProductById(productId);
+            if(product==null){
+                return -1;
+            }
+            productAttr = fsStoreProductAttrScrmMapper.selectFsStoreProductAttrByProductId(productId);
+            productValues = fsStoreProductAttrValueScrmMapper.selectFsStoreProductAttrValueByProductId(productId);
+
+            // 将数据存入缓存
+            Map<String, Object> cacheData = new HashMap<>();
+            cacheData.put("product", product);
+            cacheData.put("productAttr", productAttr);
+            cacheData.put("productValues", productValues);
+            redisCache.setCacheObject(cacheKey, cacheData, LiveKeysConstant.PRODUCT_DETAIL_CACHE_EXPIRE, TimeUnit.SECONDS);
         }
+        if (product != null && product.getPurchaseLimit() != null && product.getPurchaseLimit() > 0) {
+            FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
+            if (limit == null) {
+                // 创建新记录
+                limit = new FsStoreProductPurchaseLimitScrm();
+                limit.setProductId(productId);
+                limit.setUserId(userId);
+                limit.setNum(num);
+                return insertFsStoreProductPurchaseLimit(limit);
+            } else {
+                // 更新现有记录
+                limit.setNum(limit.getNum() + num);
+                return updateFsStoreProductPurchaseLimit(limit);
+            }
+        }
+        return -1;
     }
 
     /**
@@ -145,19 +196,48 @@ public class FsStoreProductPurchaseLimitScrmServiceImpl implements IFsStoreProdu
     @Override
     public int decreasePurchaseLimit(Long productId, Long userId, Integer num)
     {
-        FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
-        if (limit != null) {
-            int newNum = limit.getNum() - num;
-            if (newNum <= 0) {
-                // 如果数量为0或负数,删除记录
-                return deleteFsStoreProductPurchaseLimitById(limit.getId());
-            } else {
-                // 更新数量
-                limit.setNum(newNum);
-                return updateFsStoreProductPurchaseLimit(limit);
+        String cacheKey = String.format(LiveKeysConstant.PRODUCT_DETAIL_CACHE, productId);
+        Map<String, Object> cachedData = redisCache.getCacheObject(cacheKey);
+
+        FsStoreProductScrm product;
+        List<FsStoreProductAttrScrm> productAttr;
+        List<FsStoreProductAttrValueScrm> productValues;
+
+        if (cachedData != null) {
+            // 从缓存中获取数据
+            product = (FsStoreProductScrm) cachedData.get("product");
+        } else {
+            // 缓存中没有,从数据库查询
+            product = fsStoreProductScrmMapper.selectFsStoreProductById(productId);
+            if(product==null){
+                return -1;
+            }
+            productAttr = fsStoreProductAttrScrmMapper.selectFsStoreProductAttrByProductId(productId);
+            productValues = fsStoreProductAttrValueScrmMapper.selectFsStoreProductAttrValueByProductId(productId);
+
+            // 将数据存入缓存
+            Map<String, Object> cacheData = new HashMap<>();
+            cacheData.put("product", product);
+            cacheData.put("productAttr", productAttr);
+            cacheData.put("productValues", productValues);
+            redisCache.setCacheObject(cacheKey, cacheData, LiveKeysConstant.PRODUCT_DETAIL_CACHE_EXPIRE, TimeUnit.SECONDS);
+        }
+        if (product != null && product.getPurchaseLimit() != null && product.getPurchaseLimit() > 0) {
+            FsStoreProductPurchaseLimitScrm limit = selectByProductIdAndUserId(productId, userId);
+            if (limit != null) {
+                int newNum = limit.getNum() - num;
+                if (newNum <= 0) {
+                    // 如果数量为0或负数,删除记录
+                    return deleteFsStoreProductPurchaseLimitById(limit.getId());
+                } else {
+                    // 更新数量
+                    limit.setNum(newNum);
+                    return updateFsStoreProductPurchaseLimit(limit);
+                }
             }
+            return 0;
         }
-        return 0;
+        return -1;
     }
 }
 

+ 4 - 11
fs-service/src/main/java/com/fs/live/service/impl/LiveOrderServiceImpl.java

@@ -786,6 +786,8 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
             baseMapper.updateLiveOrder(order);
             try {
                 this.updateLiveWatchLog(order);
+                // 记录限购数量(订单创建成功后记录)
+                purchaseLimitService.increasePurchaseLimit(order.getProductId(), Long.valueOf(order.getUserId()), Integer.valueOf(order.getTotalNum()));
                 this.createOmsOrderCall(order);
             } catch (Exception e) {
                 log.error("推送erp失败:{}",e.getMessage());
@@ -1431,6 +1433,8 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
                 userService.subTuiMoney(storeOrder);
             }
         }
+        // 删除限购记录
+        deletePurchaseLimitRecordsForLiveOrder(order);
 
         return R.ok();
     }
@@ -4039,17 +4043,6 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
      * @param liveOrder 订单
      */
     private void deletePurchaseLimitRecordsForLiveOrder(LiveOrder liveOrder) {
-        // 查询商品信息
-        FsStoreProductScrm product = fsStoreProductService.selectFsStoreProductById(liveOrder.getProductId());
-        if (product == null) {
-            return;
-        }
-        
-        // 如果商品没有设置限购,跳过
-        if (product.getPurchaseLimit() == null || product.getPurchaseLimit() <= 0) {
-            return;
-        }
-        
         // 减少限购数量
         Long userId = Long.parseLong(liveOrder.getUserId());
         Integer num = Integer.parseInt(liveOrder.getTotalNum());

+ 45 - 0
fs-user-app/src/main/java/com/fs/app/controller/store/ProductScrmController.java

@@ -239,6 +239,50 @@ public class ProductScrmController extends AppBaseController {
                 .put("purchasedNum", purchasedNum); // 已购买数量
     }
 
+    @Login
+    @ApiOperation("检查商品限购")
+    @GetMapping("/checkPurchaseLimit")
+    public R checkPurchaseLimit(@RequestParam(value="productId") Long productId){
+        try {
+            // 查询商品信息
+            FsStoreProductScrm product = productService.selectFsStoreProductById(productId);
+            if(product == null){
+                return R.error("商品不存在");
+            }
+            
+            // 检查是否限购
+            if(product.getPurchaseLimit() == null || product.getPurchaseLimit() <= 0){
+                // 商品不限购,直接返回成功
+                return R.ok();
+            }
+            
+            // 商品有限购,查询用户已购买数量
+            String userId = getUserId();
+            if(userId == null){
+                return R.error("用户未登录");
+            }
+            
+            FsStoreProductPurchaseLimitScrm purchaseLimit = purchaseLimitService.selectByProductIdAndUserId(
+                    productId, Long.parseLong(userId));
+            int purchasedNum = 0;
+            if (purchaseLimit != null) {
+                purchasedNum = purchaseLimit.getNum();
+            }
+            
+            // 计算剩余可购买数量
+            int remainingPurchaseLimit = product.getPurchaseLimit() - purchasedNum;
+            if (remainingPurchaseLimit <= 0) {
+                // 剩余可购买数量不足
+                return R.error("该商品限购" + product.getPurchaseLimit() + "件,目前剩余可购买数量不足");
+            }
+            
+            // 剩余可购买数量充足
+            return R.ok();
+        } catch (Exception e){
+            return R.error("检查限购失败:" + e.getMessage());
+        }
+    }
+
     @Login
     @ApiOperation("添加购物车")
     @PostMapping("/addCart")
@@ -266,6 +310,7 @@ public class ProductScrmController extends AppBaseController {
     }
 
 
+
     @Login
     @ApiOperation("获取商品购物车数量")
     @GetMapping("/getCartCount")