Browse Source

feat(hisStore): 添加购物车商品限购功能

- 在 FsStoreCartScrmMapper 中添加了 purchase_limit 字段的查询
- 在 FsStoreCartScrmServiceImpl 中实现了限购检查逻辑
- 在添加购物车、修改购物车数量时增加了限购检查
- 在 FsStoreCartVO 中添加了 purchaseLimit 字段用于展示限购数量
xw 1 tháng trước cách đây
mục cha
commit
e7ab799bdc

+ 3 - 2
fs-service/src/main/java/com/fs/hisStore/mapper/FsStoreCartScrmMapper.java

@@ -1,6 +1,5 @@
 package com.fs.hisStore.mapper;
 
-import java.util.List;
 import com.fs.hisStore.domain.FsStoreCartScrm;
 import com.fs.hisStore.param.FsStoreCartCountParam;
 import com.fs.hisStore.vo.FsStoreCartQueryVO;
@@ -10,6 +9,8 @@ import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.annotations.Select;
 import org.apache.ibatis.annotations.Update;
 
+import java.util.List;
+
 /**
  * 购物车Mapper接口
  *
@@ -66,7 +67,7 @@ public interface FsStoreCartScrmMapper
      */
     public int deleteFsStoreCartByIds(Long[] ids);
 
-    @Select("select c.*,p.product_type,p.product_name,p.image as product_image,v.price,v.sku as product_attr_name,v.image as product_attr_image,v.stock from fs_store_cart_scrm c inner join fs_store_product_scrm p on p.product_id=c.product_id inner join fs_store_product_attr_value_scrm v on v.id=c.product_attr_value_id where c.is_pay=0 and c.is_del=0 and c.is_buy=0 and p.is_show=1 and p.is_del=0 and c.user_id= #{uid}")
+    @Select("select c.*,p.product_type,p.product_name,p.image as product_image,v.price,v.sku as product_attr_name,v.image as product_attr_image,v.stock,v.purchase_limit from fs_store_cart_scrm c inner join fs_store_product_scrm p on p.product_id=c.product_id inner join fs_store_product_attr_value_scrm v on v.id=c.product_attr_value_id where c.is_pay=0 and c.is_del=0 and c.is_buy=0 and p.is_show=1 and p.is_del=0 and c.user_id= #{uid}")
     List<FsStoreCartVO> selectFsStoreCartListByUid(long uid);
     @Delete({"<script>"+
             "delete from fs_store_cart_scrm where id in"+

+ 55 - 1
fs-service/src/main/java/com/fs/hisStore/service/impl/FsStoreCartScrmServiceImpl.java

@@ -24,6 +24,7 @@ import com.fs.hisStore.param.FsStoreCartDelParam;
 import com.fs.hisStore.param.FsStoreCartNumParam;
 import com.fs.hisStore.param.FsStoreCartParam;
 import com.fs.hisStore.service.IFsStoreCartScrmService;
+import com.fs.hisStore.service.IFsStoreOrderScrmService;
 import com.fs.hisStore.vo.FsStoreCartVO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -64,6 +65,41 @@ public class FsStoreCartScrmServiceImpl implements IFsStoreCartScrmService
 
     @Autowired
     private ConfigUtil configUtil;
+    @Autowired
+    private IFsStoreOrderScrmService orderService;
+
+    // 添加限购检查方法
+    private R checkPurchaseLimit(long userId, Long productAttrValueId, Integer cartNum) {
+        // 检查商品规格是否限购
+        FsStoreProductAttrValueScrm attrValue = valueMapper.selectFsStoreProductAttrValueById(productAttrValueId);
+
+        if (attrValue != null && attrValue.getPurchaseLimit() != null && attrValue.getPurchaseLimit() > 0) {
+            // 查询用户已购买该规格的数量
+            Integer purchasedCount = orderService.selectUserPurchasedCount(userId, productAttrValueId);
+            if (purchasedCount == null) {
+                purchasedCount = 0;
+            }
+
+            // 计算总购买数量(已购买 + 本次购买数量)
+            int totalPurchased = purchasedCount + cartNum;
+
+            // 检查是否超过限购数量
+            if (totalPurchased > attrValue.getPurchaseLimit()) {
+                int remaining = attrValue.getPurchaseLimit() - purchasedCount;
+                String message = "商品规格【" + attrValue.getSku() + "】限购" + attrValue.getPurchaseLimit() + "件";
+                message += ",您已购买" + purchasedCount + "件";
+
+                if (remaining > 0) {
+                    message += ",本次最多可购买" + remaining + "件";
+                } else {
+                    message += ",已达到购买上限";
+                }
+
+                return R.error(message);
+            }
+        }
+        return R.ok();
+    }
 
 
 
@@ -143,6 +179,12 @@ public class FsStoreCartScrmServiceImpl implements IFsStoreCartScrmService
 
     @Override
     public R addCart(long uid, FsStoreCartParam cartParam) {
+        // 添加限购检查
+        R limitCheckResult = checkPurchaseLimit(uid, cartParam.getAttrValueId(), cartParam.getCartNum());
+        if (!limitCheckResult.equals(R.ok())) {
+            return limitCheckResult;
+        }
+
         //如果是直接购买,直接写入记录
         if(cartParam.getIsBuy()==1){
             FsStoreCartScrm storeCart = FsStoreCartScrm.builder()
@@ -187,8 +229,15 @@ public class FsStoreCartScrmServiceImpl implements IFsStoreCartScrmService
                 return R.ok().put("id",storeCart.getId());
             }
             else{
+                // 对于已存在的购物车项,也需要检查限购
+                int newCartNum = cartParam.getCartNum() + cart.get(0).getCartNum();
+                R limitCheckResultForUpdate = checkPurchaseLimit(uid, cartParam.getAttrValueId(), newCartNum);
+                if (!limitCheckResultForUpdate.equals(R.ok())) {
+                    return limitCheckResultForUpdate;
+                }
+
                 storeCart=cart.get(0);
-                storeCart.setCartNum(cartParam.getCartNum() + cart.get(0).getCartNum());
+                storeCart.setCartNum(newCartNum);
                 storeCart.setUpdateTime(new Date());
                 checkProductStock(cartParam.getProductId(),storeCart.getProductAttrValueId());
                 fsStoreCartMapper.updateFsStoreCart(storeCart);
@@ -212,6 +261,11 @@ public class FsStoreCartScrmServiceImpl implements IFsStoreCartScrmService
     @Override
     public R changeNum(long userId, FsStoreCartNumParam cartParam) {
         FsStoreCartScrm cart=fsStoreCartMapper.selectFsStoreCartById(cartParam.getId());
+        // 添加限购检查
+        R limitCheckResult = checkPurchaseLimit(userId, cart.getProductAttrValueId(), cartParam.getNumber());
+        if (!limitCheckResult.equals(R.ok())) {
+            return limitCheckResult;
+        }
         checkProductStock(cart.getProductId(),cart.getProductAttrValueId());
         cart.setCartNum(cartParam.getNumber());
         cart.setUpdateTime(new Date());

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

@@ -38,4 +38,6 @@ import java.math.BigDecimal;
     private Integer stock;
 
     private Integer productType;
+
+    private Integer purchaseLimit;
 }