|
|
@@ -47,6 +47,8 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
import com.etotem.cfc.dto.ChildInfoDTO;
|
|
|
+import com.etotem.cfc.dto.GiftConfigVO;
|
|
|
+import com.etotem.cfc.dto.MembershipLevelDTO;
|
|
|
import com.etotem.cfc.service.EnergyService;
|
|
|
import com.etotem.cfc.service.UnlockGateService;
|
|
|
import com.etotem.cfc.service.UserService;
|
|
|
@@ -54,6 +56,10 @@ import com.alibaba.fastjson.JSON;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import com.etotem.cfc.util.SortUtil;
|
|
|
+import com.etotem.cfc.entity.ProductGiftItem;
|
|
|
+import com.etotem.cfc.entity.ProductOrderGift;
|
|
|
+import com.etotem.cfc.mapper.ProductOrderGiftMapper;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
@Service
|
|
|
public class ProductOrderService {
|
|
|
@@ -159,11 +165,22 @@ public class ProductOrderService {
|
|
|
@Resource
|
|
|
private com.etotem.cfc.mapper.ConsigneeMapper consigneeMapper;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private ProductOrderGiftMapper productOrderGiftMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ProductGiftService productGiftService;
|
|
|
+
|
|
|
public Result<ProductOrderDTO> create(CreateProductOrderDTO dto, Long buyerId) {
|
|
|
if (buyerId == null) {
|
|
|
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);
|
|
|
}
|
|
|
@@ -248,6 +265,46 @@ public class ProductOrderService {
|
|
|
order.setCreatedAt(new Date());
|
|
|
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);
|
|
|
+ }
|
|
|
+ }
|
|
|
return Result.success(ProductOrderDTO.from(order));
|
|
|
}
|
|
|
|