|
|
@@ -14,6 +14,11 @@ import com.fs.hisStore.vo.FsStoreCartVO;
|
|
|
import com.fs.hisStore.vo.FsStoreProductAttrValueQueryVO;
|
|
|
import com.fs.hisStore.vo.FsStoreProductListQueryVO;
|
|
|
import com.fs.hisStore.vo.FsStoreProductQueryVO;
|
|
|
+import com.fs.hisStore.param.CartPurchaseLimitCheckParam;
|
|
|
+import com.fs.hisStore.domain.FsStoreOrderItemScrm;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.HashMap;
|
|
|
import com.github.pagehelper.PageHelper;
|
|
|
import com.github.pagehelper.PageInfo;
|
|
|
import io.swagger.annotations.Api;
|
|
|
@@ -57,6 +62,9 @@ public class ProductScrmController extends AppBaseController {
|
|
|
|
|
|
@Autowired
|
|
|
private IFsStoreProductPurchaseLimitScrmService purchaseLimitService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IFsStoreOrderItemScrmService orderItemService;
|
|
|
/**
|
|
|
* 获取用户信息
|
|
|
* @param storeId
|
|
|
@@ -359,6 +367,202 @@ public class ProductScrmController extends AppBaseController {
|
|
|
return R.ok().put("data",listPageInfo);
|
|
|
}
|
|
|
|
|
|
+ @Login
|
|
|
+ @ApiOperation("购物车结算前批量校验限购")
|
|
|
+ @PostMapping("/checkCartPurchaseLimit")
|
|
|
+ public R checkCartPurchaseLimit(@Validated @RequestBody CartPurchaseLimitCheckParam param) {
|
|
|
+ try {
|
|
|
+ String userId = getUserId();
|
|
|
+ if (userId == null) {
|
|
|
+ return R.error("用户未登录");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (param.getProducts() == null || param.getProducts().isEmpty()) {
|
|
|
+ return R.error("商品列表不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userIdLong = Long.parseLong(userId);
|
|
|
+ List<String> errorMessages = new ArrayList<>();
|
|
|
+
|
|
|
+ // 按productId分组,累加同一商品不同规格的数量
|
|
|
+ Map<Long, Integer> productNumMap = new HashMap<>();
|
|
|
+ Map<Long, String> productNameMap = new HashMap<>();
|
|
|
+
|
|
|
+ for (CartPurchaseLimitCheckParam.ProductItem item : param.getProducts()) {
|
|
|
+ Long productId = item.getProductId();
|
|
|
+ Integer num = item.getNum();
|
|
|
+
|
|
|
+ if (productId == null || num == null || num <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 累加同一商品的数量(不同规格算在一起)
|
|
|
+ productNumMap.put(productId, productNumMap.getOrDefault(productId, 0) + num);
|
|
|
+
|
|
|
+ // 保存商品名称(用于错误提示)
|
|
|
+ if (!productNameMap.containsKey(productId)) {
|
|
|
+ FsStoreProductScrm product = productService.selectFsStoreProductById(productId);
|
|
|
+ if (product != null && product.getProductName() != null) {
|
|
|
+ productNameMap.put(productId, product.getProductName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历分组后的商品,检查限购
|
|
|
+ for (Map.Entry<Long, Integer> entry : productNumMap.entrySet()) {
|
|
|
+ Long productId = entry.getKey();
|
|
|
+ Integer totalNum = entry.getValue(); // 同一商品所有规格的总数量
|
|
|
+
|
|
|
+ // 查询商品信息
|
|
|
+ FsStoreProductScrm product = productService.selectFsStoreProductById(productId);
|
|
|
+ if (product == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否限购
|
|
|
+ if (product.getPurchaseLimit() == null || product.getPurchaseLimit() <= 0) {
|
|
|
+ // 商品不限购,跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 商品有限购,查询用户已购买数量
|
|
|
+ FsStoreProductPurchaseLimitScrm purchaseLimit = purchaseLimitService.selectByProductIdAndUserId(
|
|
|
+ productId, userIdLong);
|
|
|
+ int purchasedNum = 0;
|
|
|
+ if (purchaseLimit != null) {
|
|
|
+ purchasedNum = purchaseLimit.getNum();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算剩余可购买数量
|
|
|
+ int remainingPurchaseLimit = product.getPurchaseLimit() - purchasedNum;
|
|
|
+ if (remainingPurchaseLimit < totalNum) {
|
|
|
+ // 超过限购,添加错误信息
|
|
|
+ String productName = productNameMap.getOrDefault(productId,
|
|
|
+ product.getProductName() != null ? product.getProductName() : "商品ID:" + productId);
|
|
|
+ String errorMsg = String.format("商品【%s】限购%d件,您已购买%d件,本次购买%d件,超出限购数量",
|
|
|
+ productName,
|
|
|
+ product.getPurchaseLimit(),
|
|
|
+ purchasedNum,
|
|
|
+ totalNum);
|
|
|
+ errorMessages.add(errorMsg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有错误信息,拼接并返回
|
|
|
+ if (!errorMessages.isEmpty()) {
|
|
|
+ String errorMsg = String.join(";", errorMessages);
|
|
|
+ return R.error(errorMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 所有商品都通过限购检查
|
|
|
+ return R.ok();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return R.error("检查限购失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ @Login
|
|
|
+ @ApiOperation("订单支付前校验限购(商城订单)")
|
|
|
+ @GetMapping("/checkOrderPurchaseLimit")
|
|
|
+ public R checkOrderPurchaseLimit(@RequestParam(value = "orderCode") String orderCode) {
|
|
|
+ try {
|
|
|
+ String userId = getUserId();
|
|
|
+ if (userId == null) {
|
|
|
+ return R.error("用户未登录");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (orderCode == null || orderCode.trim().isEmpty()) {
|
|
|
+ return R.error("订单号不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ Long userIdLong = Long.parseLong(userId);
|
|
|
+
|
|
|
+ // 根据orderCode查询订单商品
|
|
|
+ FsStoreOrderItemScrm queryParam = new FsStoreOrderItemScrm();
|
|
|
+ queryParam.setOrderCode(orderCode);
|
|
|
+ List<FsStoreOrderItemScrm> orderItems = orderItemService.selectFsStoreOrderItemList(queryParam);
|
|
|
+
|
|
|
+ if (orderItems == null || orderItems.isEmpty()) {
|
|
|
+ return R.error("订单不存在或订单中没有商品");
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> errorMessages = new ArrayList<>();
|
|
|
+
|
|
|
+ // 按productId分组,累加同一商品不同规格的数量
|
|
|
+ Map<Long, Integer> productNumMap = new HashMap<>();
|
|
|
+ Map<Long, String> productNameMap = new HashMap<>();
|
|
|
+
|
|
|
+ for (FsStoreOrderItemScrm orderItem : orderItems) {
|
|
|
+ Long productId = orderItem.getProductId();
|
|
|
+ Integer num = orderItem.getNum();
|
|
|
+
|
|
|
+ if (productId == null || num == null || num <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 累加同一商品的数量(不同规格算在一起)
|
|
|
+ productNumMap.put(productId, productNumMap.getOrDefault(productId, 0) + num);
|
|
|
+
|
|
|
+ // 保存商品名称(用于错误提示)
|
|
|
+ if (!productNameMap.containsKey(productId)) {
|
|
|
+ FsStoreProductScrm product = productService.selectFsStoreProductById(productId);
|
|
|
+ if (product != null && product.getProductName() != null) {
|
|
|
+ productNameMap.put(productId, product.getProductName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历分组后的商品,检查限购
|
|
|
+ for (Map.Entry<Long, Integer> entry : productNumMap.entrySet()) {
|
|
|
+ Long productId = entry.getKey();
|
|
|
+ Integer totalNum = entry.getValue(); // 同一商品所有规格的总数量
|
|
|
+
|
|
|
+ // 查询商品信息
|
|
|
+ FsStoreProductScrm product = productService.selectFsStoreProductById(productId);
|
|
|
+ if (product == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否限购
|
|
|
+ if (product.getPurchaseLimit() == null || product.getPurchaseLimit() <= 0) {
|
|
|
+ // 商品不限购,跳过
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 商品有限购,查询用户已购买数量
|
|
|
+ FsStoreProductPurchaseLimitScrm purchaseLimit = purchaseLimitService.selectByProductIdAndUserId(
|
|
|
+ productId, userIdLong);
|
|
|
+ int purchasedNum = 0;
|
|
|
+ if (purchaseLimit != null) {
|
|
|
+ purchasedNum = purchaseLimit.getNum();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 计算剩余可购买数量(订单中的数量也要计算在内)
|
|
|
+ int remainingPurchaseLimit = product.getPurchaseLimit() - purchasedNum;
|
|
|
+ if (remainingPurchaseLimit < totalNum) {
|
|
|
+ // 超过限购,添加错误信息
|
|
|
+ String productName = productNameMap.getOrDefault(productId,
|
|
|
+ product.getProductName() != null ? product.getProductName() : "商品ID:" + productId);
|
|
|
+ String errorMsg = String.format("商品【%s】限购%d件,您已购买%d件,订单中购买%d件,超出限购数量",
|
|
|
+ productName,
|
|
|
+ product.getPurchaseLimit(),
|
|
|
+ purchasedNum,
|
|
|
+ totalNum);
|
|
|
+ errorMessages.add(errorMsg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有错误信息,拼接并返回
|
|
|
+ if (!errorMessages.isEmpty()) {
|
|
|
+ String errorMsg = String.join(";", errorMessages);
|
|
|
+ return R.error(errorMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 所有商品都通过限购检查
|
|
|
+ return R.ok();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return R.error("检查限购失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|