|
|
@@ -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());
|