feat(pricing): 添加全局配置查询功能并优化价格计算逻辑

- 在PriceProductConfigMapper中新增selectGlobalByProductTypeAndId方法,用于查询全局配置(排除景区级配置)
- 在PriceTierConfigMapper中新增selectGlobalByProductTypeAndQuantity方法,用于查询全局阶梯价格配置
- 移除价格计算服务中的default配置兜底逻辑,简化价格计算流程
- 更新ProductConfigServiceImpl中的配置查询逻辑,使用新的全局配置查询方法替代原有查询方式
- 优化配置查找顺序,提高全局配置的优先级和查询效率
This commit is contained in:
2026-02-14 13:48:31 +08:00
parent cbbdd02003
commit 143185926c
4 changed files with 33 additions and 36 deletions

View File

@@ -34,6 +34,12 @@ public interface PriceProductConfigMapper extends BaseMapper<PriceProductConfig>
@Select("SELECT * FROM price_product_config WHERE product_type = #{productType} AND product_id = #{productId} AND is_active = 1") @Select("SELECT * FROM price_product_config WHERE product_type = #{productType} AND product_id = #{productId} AND is_active = 1")
PriceProductConfig selectByProductTypeAndId(String productType, String productId); 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查询配置(支持景区维度) * 根据商品类型、商品ID和景区ID查询配置(支持景区维度)
*/ */

View File

@@ -27,6 +27,17 @@ public interface PriceTierConfigMapper extends BaseMapper<PriceTierConfig> {
@Param("productId") String productId, @Param("productId") String productId,
@Param("quantity") Integer quantity); @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查询匹配的阶梯价格(支持景区维度) * 根据商品类型、商品ID、数量和景区ID查询匹配的阶梯价格(支持景区维度)
*/ */

View File

@@ -401,41 +401,21 @@ public class PriceCalculationServiceImpl implements IPriceCalculationService {
log.warn("未找到具体商品配置: productType={}, productId={}, scenicId={}, 尝试使用通用配置", log.warn("未找到具体商品配置: productType={}, productId={}, scenicId={}, 尝试使用通用配置",
productType, productId, scenicId); productType, productId, scenicId);
// 兜底:使用default配置(带景区ID // 最后兜底:使用通用配置(向后兼容
try { List<PriceProductConfig> configs = productConfigService.getProductConfig(productType.getCode());
PriceProductConfig defaultConfig = productConfigService.getProductConfig(productType.getCode(), "default", scenicId); if (!configs.isEmpty()) {
if (defaultConfig != null) { PriceProductConfig baseConfig = configs.getFirst();
actualPrice = defaultConfig.getBasePrice(); actualPrice = baseConfig.getBasePrice();
originalPrice = defaultConfig.getOriginalPrice(); originalPrice = baseConfig.getOriginalPrice();
if (isQuantityBasedPricing(capability)) { if (isQuantityBasedPricing(capability)) {
actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity())); actualPrice = actualPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
if (originalPrice != null) { if (originalPrice != null) {
originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity())); originalPrice = originalPrice.multiply(BigDecimal.valueOf(product.getQuantity()));
}
} }
} else {
throw new PriceCalculationException("无法找到default配置");
}
} catch (Exception defaultEx) {
log.warn("未找到default配置: productType={}, scenicId={}", productType.getCode(), scenicId);
// 最后兜底:使用通用配置(向后兼容)
List<PriceProductConfig> 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);
} }
} }
} }

View File

@@ -98,7 +98,7 @@ public class ProductConfigServiceImpl implements IProductConfigService {
// 3. 全局+商品ID (兜底) // 3. 全局+商品ID (兜底)
try { try {
config = productConfigMapper.selectByProductTypeAndId(productType, productId); config = productConfigMapper.selectGlobalByProductTypeAndId(productType, productId);
if (config != null) { if (config != null) {
log.debug("使用全局商品配置: productType={}, productId={}", productType, productId); log.debug("使用全局商品配置: productType={}, productId={}", productType, productId);
return config; return config;
@@ -108,7 +108,7 @@ public class ProductConfigServiceImpl implements IProductConfigService {
} }
// 4. 全局+默认 (最后兜底) // 4. 全局+默认 (最后兜底)
config = productConfigMapper.selectByProductTypeAndId(productType, "default"); config = productConfigMapper.selectGlobalByProductTypeAndId(productType, "default");
if (config != null) { if (config != null) {
log.debug("使用全局默认配置: productType={}", productType); log.debug("使用全局默认配置: productType={}", productType);
return config; return config;
@@ -152,7 +152,7 @@ public class ProductConfigServiceImpl implements IProductConfigService {
} }
// 3. 全局+商品ID (兜底) // 3. 全局+商品ID (兜底)
config = tierConfigMapper.selectByProductTypeAndQuantity(productType, productId, quantity); config = tierConfigMapper.selectGlobalByProductTypeAndQuantity(productType, productId, quantity);
if (config != null) { if (config != null) {
log.debug("使用全局阶梯定价: productType={}, productId={}, quantity={}", log.debug("使用全局阶梯定价: productType={}, productId={}, quantity={}",
productType, productId, quantity); productType, productId, quantity);
@@ -160,7 +160,7 @@ public class ProductConfigServiceImpl implements IProductConfigService {
} }
// 4. 全局+默认 (最后兜底) // 4. 全局+默认 (最后兜底)
config = tierConfigMapper.selectByProductTypeAndQuantity(productType, "default", quantity); config = tierConfigMapper.selectGlobalByProductTypeAndQuantity(productType, "default", quantity);
if (config != null) { if (config != null) {
log.debug("使用全局默认阶梯定价: productType={}, quantity={}", productType, quantity); log.debug("使用全局默认阶梯定价: productType={}, quantity={}", productType, quantity);
} }