From 143185926c3cda589211a91df4b04dad68b791ef Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sat, 14 Feb 2026 13:48:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(pricing):=20=E6=B7=BB=E5=8A=A0=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E9=85=8D=E7=BD=AE=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BB=B7=E6=A0=BC=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在PriceProductConfigMapper中新增selectGlobalByProductTypeAndId方法,用于查询全局配置(排除景区级配置) - 在PriceTierConfigMapper中新增selectGlobalByProductTypeAndQuantity方法,用于查询全局阶梯价格配置 - 移除价格计算服务中的default配置兜底逻辑,简化价格计算流程 - 更新ProductConfigServiceImpl中的配置查询逻辑,使用新的全局配置查询方法替代原有查询方式 - 优化配置查找顺序,提高全局配置的优先级和查询效率 --- .../mapper/PriceProductConfigMapper.java | 6 +++ .../pricing/mapper/PriceTierConfigMapper.java | 11 +++++ .../impl/PriceCalculationServiceImpl.java | 44 +++++-------------- .../impl/ProductConfigServiceImpl.java | 8 ++-- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/ycwl/basic/pricing/mapper/PriceProductConfigMapper.java b/src/main/java/com/ycwl/basic/pricing/mapper/PriceProductConfigMapper.java index 3cb09c25..dc719718 100644 --- a/src/main/java/com/ycwl/basic/pricing/mapper/PriceProductConfigMapper.java +++ b/src/main/java/com/ycwl/basic/pricing/mapper/PriceProductConfigMapper.java @@ -34,6 +34,12 @@ public interface PriceProductConfigMapper extends BaseMapper @Select("SELECT * FROM price_product_config WHERE product_type = #{productType} AND product_id = #{productId} AND is_active = 1") PriceProductConfig selectByProductTypeAndId(String productType, String productId); + /** + * 根据商品类型和商品ID查询全局配置(排除景区级配置) + */ + @Select("SELECT * FROM price_product_config WHERE product_type = #{productType} AND product_id = #{productId} AND (scenic_id IS NULL OR scenic_id = '') AND is_active = 1") + PriceProductConfig selectGlobalByProductTypeAndId(String productType, String productId); + /** * 根据商品类型、商品ID和景区ID查询配置(支持景区维度) */ diff --git a/src/main/java/com/ycwl/basic/pricing/mapper/PriceTierConfigMapper.java b/src/main/java/com/ycwl/basic/pricing/mapper/PriceTierConfigMapper.java index cd745477..d25d4685 100644 --- a/src/main/java/com/ycwl/basic/pricing/mapper/PriceTierConfigMapper.java +++ b/src/main/java/com/ycwl/basic/pricing/mapper/PriceTierConfigMapper.java @@ -27,6 +27,17 @@ public interface PriceTierConfigMapper extends BaseMapper { @Param("productId") String productId, @Param("quantity") Integer quantity); + /** + * 根据商品类型、商品ID和数量查询全局匹配的阶梯价格(排除景区级配置) + */ + @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " + + "AND product_id = #{productId} AND (scenic_id IS NULL OR scenic_id = '') " + + "AND #{quantity} >= min_quantity AND #{quantity} <= max_quantity " + + "AND is_active = 1 ORDER BY sort_order ASC LIMIT 1") + PriceTierConfig selectGlobalByProductTypeAndQuantity(@Param("productType") String productType, + @Param("productId") String productId, + @Param("quantity") Integer quantity); + /** * 根据商品类型、商品ID、数量和景区ID查询匹配的阶梯价格(支持景区维度) */ diff --git a/src/main/java/com/ycwl/basic/pricing/service/impl/PriceCalculationServiceImpl.java b/src/main/java/com/ycwl/basic/pricing/service/impl/PriceCalculationServiceImpl.java index 4789826c..a333f775 100644 --- a/src/main/java/com/ycwl/basic/pricing/service/impl/PriceCalculationServiceImpl.java +++ b/src/main/java/com/ycwl/basic/pricing/service/impl/PriceCalculationServiceImpl.java @@ -401,41 +401,21 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService { log.warn("未找到具体商品配置: productType={}, productId={}, scenicId={}, 尝试使用通用配置", productType, productId, scenicId); - // 兜底:使用default配置(带景区ID) - try { - PriceProductConfig defaultConfig = productConfigService.getProductConfig(productType.getCode(), "default", scenicId); - if (defaultConfig != null) { - actualPrice = defaultConfig.getBasePrice(); - originalPrice = defaultConfig.getOriginalPrice(); + // 最后兜底:使用通用配置(向后兼容) + List configs = productConfigService.getProductConfig(productType.getCode()); + if (!configs.isEmpty()) { + PriceProductConfig baseConfig = configs.getFirst(); + actualPrice = baseConfig.getBasePrice(); + originalPrice = baseConfig.getOriginalPrice(); - if (isQuantityBasedPricing(capability)) { - actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity())); - if (originalPrice != null) { - originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity())); - } + if (isQuantityBasedPricing(capability)) { + actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity())); + if (originalPrice != null) { + originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity())); } - } else { - throw new PriceCalculationException("无法找到default配置"); - } - } catch (Exception defaultEx) { - log.warn("未找到default配置: productType={}, scenicId={}", productType.getCode(), scenicId); - - // 最后兜底:使用通用配置(向后兼容) - List configs = productConfigService.getProductConfig(productType.getCode()); - if (!configs.isEmpty()) { - PriceProductConfig baseConfig = configs.getFirst(); // 使用第一个配置作为默认 - actualPrice = baseConfig.getBasePrice(); - originalPrice = baseConfig.getOriginalPrice(); - - if (isQuantityBasedPricing(capability)) { - actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity())); - if (originalPrice != null) { - originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity())); - } - } - } else { - throw new PriceCalculationException("无法计算商品价格: " + productType.getDescription() + ", productId: " + productId); } + } else { + throw new PriceCalculationException("无法计算商品价格: " + productType.getDescription() + ", productId: " + productId); } } } diff --git a/src/main/java/com/ycwl/basic/pricing/service/impl/ProductConfigServiceImpl.java b/src/main/java/com/ycwl/basic/pricing/service/impl/ProductConfigServiceImpl.java index 04d7012a..d0b186c8 100644 --- a/src/main/java/com/ycwl/basic/pricing/service/impl/ProductConfigServiceImpl.java +++ b/src/main/java/com/ycwl/basic/pricing/service/impl/ProductConfigServiceImpl.java @@ -98,7 +98,7 @@ public class ProductConfigServiceImpl implements IProductConfigService { // 3. 全局+商品ID (兜底) try { - config = productConfigMapper.selectByProductTypeAndId(productType, productId); + config = productConfigMapper.selectGlobalByProductTypeAndId(productType, productId); if (config != null) { log.debug("使用全局商品配置: productType={}, productId={}", productType, productId); return config; @@ -108,7 +108,7 @@ public class ProductConfigServiceImpl implements IProductConfigService { } // 4. 全局+默认 (最后兜底) - config = productConfigMapper.selectByProductTypeAndId(productType, "default"); + config = productConfigMapper.selectGlobalByProductTypeAndId(productType, "default"); if (config != null) { log.debug("使用全局默认配置: productType={}", productType); return config; @@ -152,7 +152,7 @@ public class ProductConfigServiceImpl implements IProductConfigService { } // 3. 全局+商品ID (兜底) - config = tierConfigMapper.selectByProductTypeAndQuantity(productType, productId, quantity); + config = tierConfigMapper.selectGlobalByProductTypeAndQuantity(productType, productId, quantity); if (config != null) { log.debug("使用全局阶梯定价: productType={}, productId={}, quantity={}", productType, productId, quantity); @@ -160,7 +160,7 @@ public class ProductConfigServiceImpl implements IProductConfigService { } // 4. 全局+默认 (最后兜底) - config = tierConfigMapper.selectByProductTypeAndQuantity(productType, "default", quantity); + config = tierConfigMapper.selectGlobalByProductTypeAndQuantity(productType, "default", quantity); if (config != null) { log.debug("使用全局默认阶梯定价: productType={}, quantity={}", productType, quantity); }