fix(coupon): 修复优惠券适用商品类型为空时的处理逻辑

- 添加空数组检查,当适用商品类型为空时不进行过滤
- 修复商品类型为空时直接返回全部商品总价的逻辑
- 保持原有商品类型过滤功能的完整性
This commit is contained in:
2026-01-08 17:11:07 +08:00
parent 07593694c8
commit d7c2c5b830

View File

@@ -125,6 +125,10 @@ public class CouponServiceImpl implements ICouponService {
List<String> applicableProductTypes = objectMapper.readValue( List<String> applicableProductTypes = objectMapper.readValue(
coupon.getApplicableProducts(), new TypeReference<List<String>>() {}); coupon.getApplicableProducts(), new TypeReference<List<String>>() {});
// 空数组表示不限制商品类型,适用于所有商品
if (applicableProductTypes == null || applicableProductTypes.isEmpty()) {
// 不过滤,使用全部商品
} else {
discountableProducts = products.stream() discountableProducts = products.stream()
.filter(product -> applicableProductTypes.contains(product.getProductType().getCode())) .filter(product -> applicableProductTypes.contains(product.getProductType().getCode()))
.toList(); .toList();
@@ -132,6 +136,7 @@ public class CouponServiceImpl implements ICouponService {
if (discountableProducts.isEmpty()) { if (discountableProducts.isEmpty()) {
return false; return false;
} }
}
} catch (Exception e) { } catch (Exception e) {
log.error("解析适用商品类型失败", e); log.error("解析适用商品类型失败", e);
return false; return false;
@@ -198,6 +203,13 @@ public class CouponServiceImpl implements ICouponService {
List<String> applicableProductTypes = objectMapper.readValue( List<String> applicableProductTypes = objectMapper.readValue(
coupon.getApplicableProducts(), new TypeReference<List<String>>() {}); coupon.getApplicableProducts(), new TypeReference<List<String>>() {});
// 空数组表示不限制商品类型,返回所有商品总价
if (applicableProductTypes == null || applicableProductTypes.isEmpty()) {
return products.stream()
.map(ProductItem::getSubtotal)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
// 计算适用商品的总价 // 计算适用商品的总价
return products.stream() return products.stream()
.filter(product -> applicableProductTypes.contains(product.getProductType().getCode())) .filter(product -> applicableProductTypes.contains(product.getProductType().getCode()))