refactor(pricing): 重构自动发券服务方法命名及逻辑

- 将 autoGrantFirstPrintCoupon 方法重命名为 autoGrantCoupon
- 修改 findFirstPrintCouponId 方法名为 findFirstCouponId
- 调整优惠券名称匹配逻辑,移除对"first"关键字的检查
- 更新调用方 PrinterServiceImpl 中的方法引用
- 优化自动发券异常处理,确保不影响主流程
This commit is contained in:
2025-11-15 14:28:56 +08:00
parent 19fae4bd00
commit 932081abf0
3 changed files with 20 additions and 23 deletions

View File

@@ -17,5 +17,5 @@ public interface IAutoCouponService {
* @param productType 商品类型
* @return 是否成功发券
*/
boolean autoGrantFirstPrintCoupon(Long memberId, Long faceId, Long scenicId, ProductType productType);
boolean autoGrantCoupon(Long memberId, Long faceId, Long scenicId, ProductType productType);
}

View File

@@ -27,7 +27,7 @@ public class AutoCouponServiceImpl implements IAutoCouponService {
private final ICouponService couponService;
@Override
public boolean autoGrantFirstPrintCoupon(Long memberId, Long faceId, Long scenicId, ProductType productType) {
public boolean autoGrantCoupon(Long memberId, Long faceId, Long scenicId, ProductType productType) {
try {
// 1. 校验参数
if (memberId == null || faceId == null || scenicId == null || productType == null) {
@@ -37,7 +37,7 @@ public class AutoCouponServiceImpl implements IAutoCouponService {
}
// 2. 查找该景区、该商品类型的首次打印优惠券配置
Long couponId = findFirstPrintCouponId(scenicId, productType);
Long couponId = findFirstCouponId(scenicId, productType);
if (couponId == null) {
log.debug("景区未配置首次打印优惠券: scenicId={}, productType={}", scenicId, productType);
return false;
@@ -60,7 +60,7 @@ public class AutoCouponServiceImpl implements IAutoCouponService {
memberId,
couponId,
scenicId.toString(),
"AUTO_FIRST_PRINT" // 标记为自动发券来源
"AUTO_GRANT" // 标记为自动发券来源
);
couponService.claimCoupon(request);
@@ -85,7 +85,7 @@ public class AutoCouponServiceImpl implements IAutoCouponService {
* @param productType 商品类型
* @return 优惠券ID,未找到返回null
*/
private Long findFirstPrintCouponId(Long scenicId, ProductType productType) {
private Long findFirstCouponId(Long scenicId, ProductType productType) {
try {
// 查询该景区的有效优惠券
List<PriceCouponConfig> coupons = couponConfigMapper.selectValidCouponsByScenicId(
@@ -94,9 +94,7 @@ public class AutoCouponServiceImpl implements IAutoCouponService {
for (PriceCouponConfig coupon : coupons) {
// 检查优惠券名称是否包含"首次"关键字
if (coupon.getCouponName() != null &&
(coupon.getCouponName().contains("首次") ||
coupon.getCouponName().contains("first"))) {
if (coupon.getCouponName() != null && (coupon.getCouponName().contains("首次"))) {
// 检查适用商品类型
String applicableProducts = coupon.getApplicableProducts();

View File

@@ -441,6 +441,20 @@ public class PrinterServiceImpl implements PrinterService {
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());
}
}
// 使用统一价格计算服务
PriceCalculationResult result = priceCalculationService.calculatePrice(request);
@@ -589,21 +603,6 @@ public class PrinterServiceImpl implements PrinterService {
throw new BaseException("没有可打印的照片");
}
// 【新增】检测并自动发放首次打印优惠券
if (mobileCount > 0) {
try {
autoCouponService.autoGrantFirstPrintCoupon(
memberId,
faceId,
scenicId,
ProductType.PHOTO_PRINT_MU
);
} catch (Exception e) {
log.warn("自动发券失败,不影响下单流程: memberId={}, faceId={}, scenicId={}, error={}",
memberId, faceId, scenicId, e.getMessage());
}
}
OrderEntity order = new OrderEntity();
Long orderId = SnowFlakeUtil.getLongId();
redisTemplate.opsForValue().set("printer_size:"+orderId, printer.getPreferPaper(), 60, TimeUnit.SECONDS);