|
|
@@ -176,11 +176,6 @@ public class ProductOrderService {
|
|
|
return Result.error("请先登录");
|
|
|
}
|
|
|
|
|
|
- if (dto.getItems() != null && !dto.getItems().isEmpty()
|
|
|
- && dto.getGiftItemIds() != null && !dto.getGiftItemIds().isEmpty()) {
|
|
|
- return Result.error("赠品仅支持单商品下单");
|
|
|
- }
|
|
|
-
|
|
|
if (dto.getItems() != null && !dto.getItems().isEmpty()) {
|
|
|
return createMultiItem(dto, buyerId);
|
|
|
}
|
|
|
@@ -266,44 +261,9 @@ public class ProductOrderService {
|
|
|
order.setUpdatedAt(new Date());
|
|
|
orderMapper.insert(order);
|
|
|
// ===== 赠品校验与记录 =====
|
|
|
- if (dto.getGiftItemIds() != null && !dto.getGiftItemIds().isEmpty()) {
|
|
|
- MembershipLevelDTO currentLevel = order.getFamilyId() != null
|
|
|
- ? membershipService.getCurrentLevel(order.getFamilyId())
|
|
|
- : null;
|
|
|
- String userLevel = currentLevel != null ? currentLevel.getLevelCode() : null;
|
|
|
- if (userLevel == null || "FREE".equals(userLevel)) {
|
|
|
- return Result.error("您暂无会员等级,无法享受赠品");
|
|
|
- }
|
|
|
- List<GiftConfigVO> activeConfigs = productGiftService.getActiveConfigsByProductAndLevel(
|
|
|
- product.getId(), userLevel);
|
|
|
- if (activeConfigs.isEmpty()) {
|
|
|
- return Result.error("您所在等级暂无该商品赠品");
|
|
|
- }
|
|
|
- Set<Long> allowedItemIds = activeConfigs.stream()
|
|
|
- .flatMap(c -> c.getItems().stream())
|
|
|
- .map(GiftConfigVO.GiftItemVO::getId)
|
|
|
- .collect(Collectors.toSet());
|
|
|
- for (Long itemId : dto.getGiftItemIds()) {
|
|
|
- if (!allowedItemIds.contains(itemId)) {
|
|
|
- return Result.error("赠品项不存在或超出权限");
|
|
|
- }
|
|
|
- ProductGiftItem giftItem = productGiftService.getActiveItemById(itemId);
|
|
|
- if (giftItem == null) {
|
|
|
- return Result.error("赠品项不存在");
|
|
|
- }
|
|
|
- ProductOrderGift orderGift = new ProductOrderGift();
|
|
|
- orderGift.setOrderId(order.getId());
|
|
|
- orderGift.setOrderNo(order.getOrderNo());
|
|
|
- orderGift.setGiftType(giftItem.getGiftType());
|
|
|
- orderGift.setGiftProductId(giftItem.getGiftProductId());
|
|
|
- orderGift.setGiftProductName(giftItem.getGiftProductName());
|
|
|
- orderGift.setGiftProductImage(giftItem.getGiftProductImage());
|
|
|
- orderGift.setCfValue(giftItem.getCfValue() != null ? giftItem.getCfValue() : 0);
|
|
|
- orderGift.setQuantity(1);
|
|
|
- orderGift.setFulfilled(0);
|
|
|
- orderGift.setCreatedAt(new Date());
|
|
|
- productOrderGiftMapper.insert(orderGift);
|
|
|
- }
|
|
|
+ Result<Void> giftResult = processGifts(order, dto, product);
|
|
|
+ if (giftResult.getCode() != 200) {
|
|
|
+ return Result.error(giftResult.getMessage());
|
|
|
}
|
|
|
return Result.success(ProductOrderDTO.from(order));
|
|
|
}
|
|
|
@@ -575,9 +535,69 @@ public class ProductOrderService {
|
|
|
order.setCreatedAt(new Date());
|
|
|
order.setUpdatedAt(new Date());
|
|
|
orderMapper.insert(order);
|
|
|
+ // ===== 赠品校验与记录(仅单商品下单支持赠品) =====
|
|
|
+ if (dto.getGiftItemIds() != null && !dto.getGiftItemIds().isEmpty()) {
|
|
|
+ if (dto.getItems().size() > 1) {
|
|
|
+ return Result.error("赠品仅支持单商品下单");
|
|
|
+ }
|
|
|
+ Result<Void> giftResult = processGifts(order, dto, firstProduct);
|
|
|
+ if (giftResult.getCode() != 200) {
|
|
|
+ return Result.error(giftResult.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
return Result.success(ProductOrderDTO.from(order));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 赠品校验与记录:校验 giftItemIds 属于该商品该等级启用配置,写入 product_order_gifts(fulfilled=0)。
|
|
|
+ * 返回 code!=200 表示校验失败(调用方返回 Result.error(message))。
|
|
|
+ */
|
|
|
+ private Result<Void> processGifts(ProductOrder order, CreateProductOrderDTO dto, Product product) {
|
|
|
+ if (dto.getGiftItemIds() == null || dto.getGiftItemIds().isEmpty()) {
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+ // 家庭创建者等级(MembershipService.getCurrentLevel 无会员时返回 FREE)
|
|
|
+ MembershipLevelDTO currentLevel = order.getFamilyId() != null
|
|
|
+ ? membershipService.getCurrentLevel(order.getFamilyId())
|
|
|
+ : null;
|
|
|
+ String userLevel = currentLevel != null ? currentLevel.getLevelCode() : null;
|
|
|
+ if (userLevel == null || "FREE".equals(userLevel)) {
|
|
|
+ return Result.error("您暂无会员等级,无法享受赠品");
|
|
|
+ }
|
|
|
+ // 查询该商品该等级下启用的赠品项(含库存校验)
|
|
|
+ List<GiftConfigVO> activeConfigs = productGiftService.getActiveConfigsByProductAndLevel(
|
|
|
+ product.getId(), userLevel);
|
|
|
+ if (activeConfigs.isEmpty()) {
|
|
|
+ return Result.error("您所在等级暂无该商品赠品");
|
|
|
+ }
|
|
|
+ Set<Long> allowedItemIds = activeConfigs.stream()
|
|
|
+ .flatMap(c -> c.getItems().stream())
|
|
|
+ .map(GiftConfigVO.GiftItemVO::getId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ for (Long itemId : dto.getGiftItemIds()) {
|
|
|
+ if (!allowedItemIds.contains(itemId)) {
|
|
|
+ return Result.error("赠品项不存在或超出权限");
|
|
|
+ }
|
|
|
+ ProductGiftItem giftItem = productGiftService.getActiveItemById(itemId);
|
|
|
+ if (giftItem == null) {
|
|
|
+ return Result.error("赠品项不存在");
|
|
|
+ }
|
|
|
+ ProductOrderGift orderGift = new ProductOrderGift();
|
|
|
+ orderGift.setOrderId(order.getId());
|
|
|
+ orderGift.setOrderNo(order.getOrderNo());
|
|
|
+ orderGift.setGiftType(giftItem.getGiftType());
|
|
|
+ orderGift.setGiftProductId(giftItem.getGiftProductId());
|
|
|
+ orderGift.setGiftProductName(giftItem.getGiftProductName());
|
|
|
+ orderGift.setGiftProductImage(giftItem.getGiftProductImage());
|
|
|
+ orderGift.setCfValue(giftItem.getCfValue() != null ? giftItem.getCfValue() : 0);
|
|
|
+ orderGift.setQuantity(1);
|
|
|
+ orderGift.setFulfilled(0);
|
|
|
+ orderGift.setCreatedAt(new Date());
|
|
|
+ productOrderGiftMapper.insert(orderGift);
|
|
|
+ }
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+
|
|
|
public Result<Map<String, Object>> pay(String orderNo, Long userId) {
|
|
|
ProductOrder order = orderMapper.selectOne(
|
|
|
new LambdaQueryWrapper<ProductOrder>().eq(ProductOrder::getOrderNo, orderNo));
|