Merge branch 'print_sku'

This commit is contained in:
2025-11-15 15:30:57 +08:00
7 changed files with 315 additions and 35 deletions

View File

@@ -912,9 +912,11 @@ public class OrderServiceImpl implements IOrderService {
checkSetAlreadyPurchased(userId, faceId, scenicId, product.getProductType());
break;
case PHOTO_PRINT:
case PHOTO_PRINT_MU:
case PHOTO_PRINT_FX:
case MACHINE_PRINT:
// 打印类商品允许重复购买,跳过检查
log.debug("跳过打印类商品重复购买检查: productType={}, productId={}",
log.debug("跳过打印类商品重复购买检查: productType={}, productId={}",
product.getProductType(), product.getProductId());
break;
default:

View File

@@ -14,6 +14,8 @@ public enum ProductType {
RECORDING_SET("RECORDING_SET", "录像集"),
PHOTO_SET("PHOTO_SET", "照相集"),
PHOTO_PRINT("PHOTO_PRINT", "照片打印"),
PHOTO_PRINT_MU("PHOTO_PRINT_MU", "手机照片打印"),
PHOTO_PRINT_FX("PHOTO_PRINT_FX", "特效照片打印"),
MACHINE_PRINT("MACHINE_PRINT", "一体机打印");
private final String code;

View File

@@ -0,0 +1,21 @@
package com.ycwl.basic.pricing.service;
import com.ycwl.basic.pricing.enums.ProductType;
/**
* 自动发券服务接口
* 负责在特定场景下自动为用户发放优惠券
*/
public interface IAutoCouponService {
/**
* 自动为用户发放首次打印优惠券
*
* @param memberId 用户ID (member_id)
* @param faceId 人脸ID (face_id)
* @param scenicId 景区ID
* @param productType 商品类型
* @return 是否成功发券
*/
boolean autoGrantCoupon(Long memberId, Long faceId, Long scenicId, ProductType productType);
}

View File

@@ -0,0 +1,116 @@
package com.ycwl.basic.pricing.service.impl;
import com.ycwl.basic.pricing.dto.CouponClaimRequest;
import com.ycwl.basic.pricing.entity.PriceCouponClaimRecord;
import com.ycwl.basic.pricing.entity.PriceCouponConfig;
import com.ycwl.basic.pricing.enums.ProductType;
import com.ycwl.basic.pricing.mapper.PriceCouponClaimRecordMapper;
import com.ycwl.basic.pricing.mapper.PriceCouponConfigMapper;
import com.ycwl.basic.pricing.service.IAutoCouponService;
import com.ycwl.basic.pricing.service.ICouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 自动发券服务实现
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class AutoCouponServiceImpl implements IAutoCouponService {
private final PriceCouponConfigMapper couponConfigMapper;
private final PriceCouponClaimRecordMapper couponClaimRecordMapper;
private final ICouponService couponService;
@Override
public boolean autoGrantCoupon(Long memberId, Long faceId, Long scenicId, ProductType productType) {
try {
// 1. 校验参数
if (memberId == null || faceId == null || scenicId == null || productType == null) {
log.warn("自动发券参数不完整: memberId={}, faceId={}, scenicId={}, productType={}",
memberId, faceId, scenicId, productType);
return false;
}
// 2. 查找该景区、该商品类型的首次打印优惠券配置
Long couponId = findFirstCouponId(scenicId, productType);
if (couponId == null) {
log.debug("景区未配置首次打印优惠券: scenicId={}, productType={}", scenicId, productType);
return false;
}
// 3. 检查用户是否已领取过该券(领券即消耗首次资格)
PriceCouponClaimRecord existingRecord = couponClaimRecordMapper.selectUserCouponRecord(
memberId,
couponId
);
if (existingRecord != null) {
log.debug("用户已领取过首次优惠券,不重复发券: memberId={}, couponId={}, claimTime={}",
memberId, couponId, existingRecord.getClaimTime());
return false;
}
// 4. 自动发券
CouponClaimRequest request = new CouponClaimRequest(
memberId,
couponId,
scenicId.toString(),
"AUTO_GRANT" // 标记为自动发券来源
);
couponService.claimCoupon(request);
log.info("成功自动发放首次打印优惠券: memberId={}, faceId={}, scenicId={}, productType={}, couponId={}",
memberId, faceId, scenicId, productType, couponId);
return true;
} catch (Exception e) {
log.error("自动发券失败: memberId={}, faceId={}, scenicId={}, productType={}",
memberId, faceId, scenicId, productType, e);
return false;
}
}
/**
* 查找指定景区、指定商品类型的首次打印优惠券ID
* 规则:优惠券名称包含 "首次" 且 适用商品类型包含目标类型
*
* @param scenicId 景区ID
* @param productType 商品类型
* @return 优惠券ID,未找到返回null
*/
private Long findFirstCouponId(Long scenicId, ProductType productType) {
try {
// 查询该景区的有效优惠券
List<PriceCouponConfig> coupons = couponConfigMapper.selectValidCouponsByScenicId(
scenicId.toString()
);
for (PriceCouponConfig coupon : coupons) {
// 检查优惠券名称是否包含"首次"关键字
if (coupon.getCouponName() != null && (coupon.getCouponName().contains("首次"))) {
// 检查适用商品类型
String applicableProducts = coupon.getApplicableProducts();
if (applicableProducts != null &&
applicableProducts.contains(productType.getCode())) {
return coupon.getId();
}
}
}
log.debug("未找到匹配的首次打印优惠券: scenicId={}, productType={}", scenicId, productType);
return null;
} catch (Exception e) {
log.error("查找首次打印优惠券失败: scenicId={}, productType={}", scenicId, productType, e);
return null;
}
}
}

View File

@@ -83,6 +83,7 @@ public class OnePricePurchaseDiscountProvider implements IDiscountProvider {
discountInfo.setDiscountAmount(onePriceInfo.getActualDiscountAmount());
discountInfo.setDiscountDescription("景区一口价购买,价格更优惠");
discountInfo.setOnePriceInfo(onePriceInfo);
discountInfo.setPriority(getPriority());
discounts.add(discountInfo);

View File

@@ -29,7 +29,18 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
private final IPriceBundleService bundleService;
private final IDiscountDetectionService discountDetectionService;
private final IVoucherService voucherService;
/**
* 判断是否为打印类商品
* 打印类商品的价格计算方式为:单价 × 数量
*/
private boolean isPrintProduct(ProductType productType) {
return productType == ProductType.PHOTO_PRINT
|| productType == ProductType.PHOTO_PRINT_MU
|| productType == ProductType.PHOTO_PRINT_FX
|| productType == ProductType.MACHINE_PRINT;
}
@Override
public PriceCalculationResult calculatePrice(PriceCalculationRequest request) {
if (request.getProducts() == null || request.getProducts().isEmpty()) {
@@ -190,7 +201,7 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
try {
PriceProductConfig baseConfig = productConfigService.getProductConfig(productType.getCode(), productId);
if (baseConfig != null) {
if (productType == ProductType.PHOTO_PRINT || productType == ProductType.MACHINE_PRINT) {
if (isPrintProduct(productType)) {
return baseConfig.getBasePrice().multiply(BigDecimal.valueOf(product.getQuantity()));
} else {
return baseConfig.getBasePrice();
@@ -205,7 +216,7 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
try {
PriceProductConfig defaultConfig = productConfigService.getProductConfig(productType.getCode(), "default");
if (defaultConfig != null) {
if (productType == ProductType.PHOTO_PRINT || productType == ProductType.MACHINE_PRINT) {
if (isPrintProduct(productType)) {
return defaultConfig.getBasePrice().multiply(BigDecimal.valueOf(product.getQuantity()));
} else {
return defaultConfig.getBasePrice();
@@ -219,7 +230,7 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
List<PriceProductConfig> configs = productConfigService.getProductConfig(productType.getCode());
if (!configs.isEmpty()) {
PriceProductConfig baseConfig = configs.get(0); // 使用第一个配置作为默认
if (productType == ProductType.PHOTO_PRINT || productType == ProductType.MACHINE_PRINT) {
if (isPrintProduct(productType)) {
return baseConfig.getBasePrice().multiply(BigDecimal.valueOf(product.getQuantity()));
} else {
return baseConfig.getBasePrice();
@@ -252,8 +263,8 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
if (baseConfig != null) {
actualPrice = baseConfig.getBasePrice();
originalPrice = baseConfig.getOriginalPrice();
if (productType == ProductType.PHOTO_PRINT || productType == ProductType.MACHINE_PRINT) {
if (isPrintProduct(productType)) {
actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
if (originalPrice != null) {
originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
@@ -272,8 +283,8 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
if (defaultConfig != null) {
actualPrice = defaultConfig.getBasePrice();
originalPrice = defaultConfig.getOriginalPrice();
if (productType == ProductType.PHOTO_PRINT || productType == ProductType.MACHINE_PRINT) {
if (isPrintProduct(productType)) {
actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
if (originalPrice != null) {
originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
@@ -291,8 +302,8 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
PriceProductConfig baseConfig = configs.getFirst(); // 使用第一个配置作为默认
actualPrice = baseConfig.getBasePrice();
originalPrice = baseConfig.getOriginalPrice();
if (productType == ProductType.PHOTO_PRINT || productType == ProductType.MACHINE_PRINT) {
if (isPrintProduct(productType)) {
actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
if (originalPrice != null) {
originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity()));

View File

@@ -33,6 +33,7 @@ import com.ycwl.basic.pricing.dto.PriceCalculationRequest;
import com.ycwl.basic.pricing.dto.PriceCalculationResult;
import com.ycwl.basic.pricing.dto.ProductItem;
import com.ycwl.basic.pricing.enums.ProductType;
import com.ycwl.basic.pricing.service.IAutoCouponService;
import com.ycwl.basic.pricing.service.IPriceCalculationService;
import com.ycwl.basic.model.pc.printer.entity.MemberPrintEntity;
import com.ycwl.basic.model.pc.printer.entity.PrintTaskEntity;
@@ -109,6 +110,8 @@ public class PrinterServiceImpl implements PrinterService {
@Autowired
private IPriceCalculationService priceCalculationService;
@Autowired
private IAutoCouponService autoCouponService;
@Autowired
private ScenicRepository scenicRepository;
@Autowired
private OrderRepository orderRepository;
@@ -354,37 +357,104 @@ public class PrinterServiceImpl implements PrinterService {
@Override
public PriceObj queryPrice(Long memberId, Long scenicId, Long faceId) {
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId, faceId);
// 计算照片总数量
long count = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity()))
.mapToInt(MemberPrintResp::getQuantity)
.sum();
PriceObj obj = new PriceObj();
obj.setScenicId(scenicId);
obj.setGoodsId(faceId);
obj.setFaceId(faceId);
obj.setGoodsType(3);
if (count == 0) {
// 按照 sourceId 分类照片
// sourceId > 0: 普通照片打印 (PHOTO_PRINT)
// sourceId == null: 手机照片打印 (PHOTO_PRINT_MU)
// sourceId == 0: 特效照片打印 (PHOTO_PRINT_FX)
long normalCount = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity())
&& item.getSourceId() != null && item.getSourceId() > 0)
.mapToInt(MemberPrintResp::getQuantity)
.sum();
long mobileCount = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity())
&& item.getSourceId() == null)
.mapToInt(MemberPrintResp::getQuantity)
.sum();
long effectCount = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity())
&& item.getSourceId() != null && item.getSourceId() == 0)
.mapToInt(MemberPrintResp::getQuantity)
.sum();
long totalCount = normalCount + mobileCount + effectCount;
if (totalCount == 0) {
// 如果没有照片,返回零价格
obj.setPrice(BigDecimal.ZERO);
obj.setSlashPrice(BigDecimal.ZERO);
obj.setFree(false);
return obj;
}
// 构建价格计算请求
PriceCalculationRequest request = new PriceCalculationRequest();
request.setUserId(memberId);
// 创建照片打印商品项
ProductItem photoItem = new ProductItem();
photoItem.setProductType(ProductType.PHOTO_PRINT);
photoItem.setProductId(scenicId.toString());
photoItem.setQuantity(Long.valueOf(count).intValue());
photoItem.setPurchaseCount(1);
photoItem.setScenicId(scenicId.toString());
// 创建商品项列表
List<ProductItem> productItems = new ArrayList<>();
request.setProducts(Collections.singletonList(photoItem));
// 添加普通照片打印商品项 (sourceId > 0)
if (normalCount > 0) {
ProductItem normalPhotoItem = new ProductItem();
normalPhotoItem.setProductType(ProductType.PHOTO_PRINT);
normalPhotoItem.setProductId(scenicId.toString());
normalPhotoItem.setQuantity(Long.valueOf(normalCount).intValue());
normalPhotoItem.setPurchaseCount(1);
normalPhotoItem.setScenicId(scenicId.toString());
productItems.add(normalPhotoItem);
log.debug("普通照片打印数量: {}", normalCount);
}
// 添加手机照片打印商品项 (sourceId == null)
if (mobileCount > 0) {
ProductItem mobilePhotoItem = new ProductItem();
mobilePhotoItem.setProductType(ProductType.PHOTO_PRINT_MU);
mobilePhotoItem.setProductId(scenicId.toString());
mobilePhotoItem.setQuantity(Long.valueOf(mobileCount).intValue());
mobilePhotoItem.setPurchaseCount(1);
mobilePhotoItem.setScenicId(scenicId.toString());
productItems.add(mobilePhotoItem);
log.debug("手机照片打印数量: {}", mobileCount);
}
// 添加特效照片打印商品项 (sourceId == 0)
if (effectCount > 0) {
ProductItem effectPhotoItem = new ProductItem();
effectPhotoItem.setProductType(ProductType.PHOTO_PRINT_FX);
effectPhotoItem.setProductId(scenicId.toString());
effectPhotoItem.setQuantity(Long.valueOf(effectCount).intValue());
effectPhotoItem.setPurchaseCount(1);
effectPhotoItem.setScenicId(scenicId.toString());
productItems.add(effectPhotoItem);
log.debug("特效照片打印数量: {}", effectCount);
}
request.setProducts(productItems);
if (mobileCount > 0) {
try {
autoCouponService.autoGrantCoupon(
memberId,
faceId,
scenicId,
ProductType.PHOTO_PRINT_MU
);
} catch (Exception e) {
log.warn("自动发券失败,不影响下单流程: memberId={}, faceId={}, scenicId={}, error={}",
memberId, faceId, scenicId, e.getMessage());
}
}
request.setAutoUseCoupon(true);
// 使用统一价格计算服务
PriceCalculationResult result = priceCalculationService.calculatePrice(request);
@@ -505,8 +575,32 @@ public class PrinterServiceImpl implements PrinterService {
}
// 验证照片数量
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId, faceId);
long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum();
if (count == 0) {
// 按照 sourceId 分类照片
// sourceId > 0: 普通照片打印 (PHOTO_PRINT)
// sourceId == null: 手机照片打印 (PHOTO_PRINT_MU)
// sourceId == 0: 特效照片打印 (PHOTO_PRINT_FX)
long normalCount = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity())
&& item.getSourceId() != null && item.getSourceId() > 0)
.mapToInt(MemberPrintResp::getQuantity)
.sum();
long mobileCount = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity())
&& item.getSourceId() == null)
.mapToInt(MemberPrintResp::getQuantity)
.sum();
long effectCount = userPhotoList.stream()
.filter(item -> Objects.nonNull(item.getQuantity())
&& item.getSourceId() != null && item.getSourceId() == 0)
.mapToInt(MemberPrintResp::getQuantity)
.sum();
long totalCount = normalCount + mobileCount + effectCount;
if (totalCount == 0) {
throw new BaseException("没有可打印的照片");
}
@@ -536,15 +630,48 @@ public class PrinterServiceImpl implements PrinterService {
request.setUserId(memberId);
request.setScenicId(scenicId);
// 创建照片打印商品项
ProductItem photoItem = new ProductItem();
photoItem.setProductType(ProductType.PHOTO_PRINT);
photoItem.setProductId(scenicId.toString());
photoItem.setQuantity(Long.valueOf(count).intValue());
photoItem.setPurchaseCount(1);
photoItem.setScenicId(scenicId.toString());
// 创建商品项列表
List<ProductItem> productItems = new ArrayList<>();
request.setProducts(Collections.singletonList(photoItem));
// 添加普通照片打印商品项 (sourceId > 0)
if (normalCount > 0) {
ProductItem normalPhotoItem = new ProductItem();
normalPhotoItem.setProductType(ProductType.PHOTO_PRINT);
normalPhotoItem.setProductId(scenicId.toString());
normalPhotoItem.setQuantity(Long.valueOf(normalCount).intValue());
normalPhotoItem.setPurchaseCount(1);
normalPhotoItem.setScenicId(scenicId.toString());
productItems.add(normalPhotoItem);
log.debug("创建订单-普通照片打印数量: {}", normalCount);
}
// 添加手机照片打印商品项 (sourceId == null)
if (mobileCount > 0) {
ProductItem mobilePhotoItem = new ProductItem();
mobilePhotoItem.setProductType(ProductType.PHOTO_PRINT_MU);
mobilePhotoItem.setProductId(scenicId.toString());
mobilePhotoItem.setQuantity(Long.valueOf(mobileCount).intValue());
mobilePhotoItem.setPurchaseCount(1);
mobilePhotoItem.setScenicId(scenicId.toString());
productItems.add(mobilePhotoItem);
log.debug("创建订单-手机照片打印数量: {}", mobileCount);
}
// 添加特效照片打印商品项 (sourceId == 0)
if (effectCount > 0) {
ProductItem effectPhotoItem = new ProductItem();
effectPhotoItem.setProductType(ProductType.PHOTO_PRINT_FX);
effectPhotoItem.setProductId(scenicId.toString());
effectPhotoItem.setQuantity(Long.valueOf(effectCount).intValue());
effectPhotoItem.setPurchaseCount(1);
effectPhotoItem.setScenicId(scenicId.toString());
productItems.add(effectPhotoItem);
log.debug("创建订单-特效照片打印数量: {}", effectCount);
}
request.setProducts(productItems);
request.setAutoUseCoupon(true);
request.setPreviewOnly(false);
PriceCalculationResult priceResult = priceCalculationService.calculatePrice(request);