一、模块概述 价格管理模块是生鲜电商系统的核心功能之一,负责处理商品价格的全生命周期管理,包括定价、调价、促销、价格计算等业务场景。针对生鲜行业特点,需特别考虑价格波动频繁、分时定价、区域定价等特殊需求。 二、核心功能设计 1.基础价格管理 -多维度定价:支持按商品、品类
一、模块概述
价格管理模块是生鲜电商系统的核心功能之一,负责处理商品价格的全生命周期管理,包括定价、调价、促销、价格计算等业务场景。针对生鲜行业特点,需特别考虑价格波动频繁、分时定价、区域定价等特殊需求。
二、核心功能设计
1. 基础价格管理
- 多维度定价:支持按商品、品类、供应商、区域、时间等维度设置基础价格
- 价格版本控制:记录价格变更历史,支持回滚到历史版本
- 价格有效期:设置价格生效和失效时间,支持预约调价
2. 动态定价机制
- 成本加成定价:基于采购成本+毛利率自动计算建议售价
- 市场参考定价:对接第三方数据源获取市场均价作为参考
- 智能调价引擎:根据库存、销量、竞品价格等因素自动触发调价
3. 促销管理
- 多种促销类型:满减、折扣、直降、买赠、第二件半价等
- 叠加规则:定义促销活动间的优先级和叠加规则
- 预算控制:设置促销活动预算和成本上限
4. 区域价格管理
- 地理围栏定价:按城市、行政区、配送范围设置差异化价格
- 运费联动:价格与配送区域、运费政策联动计算
- 区域促销:针对特定区域开展专属促销活动
三、技术实现方案
1. 数据库设计
```sql
-- 商品价格主表
CREATE TABLE product_price (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
product_id BIGINT NOT NULL,
region_id BIGINT,
supplier_id BIGINT,
base_price DECIMAL(10,2) NOT NULL,
cost_price DECIMAL(10,2),
market_price DECIMAL(10,2),
status TINYINT DEFAULT 1,
create_time DATETIME,
update_time DATETIME,
INDEX idx_product (product_id),
INDEX idx_region (region_id)
);
-- 价格历史表
CREATE TABLE price_history (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
price_id BIGINT NOT NULL,
old_price DECIMAL(10,2),
new_price DECIMAL(10,2),
change_type VARCHAR(20),
operator VARCHAR(50),
change_time DATETIME,
remark TEXT
);
-- 促销活动表
CREATE TABLE promotion (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
promo_type VARCHAR(20) NOT NULL, -- DISCOUNT, FULL_REDUCTION等
start_time DATETIME NOT NULL,
end_time DATETIME NOT NULL,
status TINYINT DEFAULT 0, -- 0未开始 1进行中 2已结束
priority INT DEFAULT 0,
budget DECIMAL(12,2),
used_amount DECIMAL(12,2) DEFAULT 0,
creator VARCHAR(50),
create_time DATETIME
);
-- 促销规则表
CREATE TABLE promotion_rule (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
promotion_id BIGINT NOT NULL,
product_id BIGINT,
category_id BIGINT,
discount_rate DECIMAL(5,2), -- 折扣率
discount_amount DECIMAL(10,2), -- 直减金额
min_quantity INT, -- 满足条件的最小数量
max_quantity INT, -- 满足条件的最大数量
is_exclusive BOOLEAN DEFAULT FALSE -- 是否互斥
);
```
2. 核心服务实现
```java
// 价格计算服务接口
public interface PriceCalculationService {
/
* 计算商品最终价格
* @param productId 商品ID
* @param quantity 购买数量
* @param regionId 区域ID
* @param customerId 客户ID(可选)
* @return 价格计算结果
*/
PriceCalculationResult calculatePrice(Long productId, int quantity,
Long regionId, Long customerId);
/
* 批量计算商品价格
*/
List
batchCalculatePrice(List requests);
}
// 价格计算结果
@Data
public class PriceCalculationResult {
private Long productId;
private BigDecimal originalPrice;
private BigDecimal finalPrice;
private BigDecimal discountAmount;
private String promotionInfo;
private LocalDateTime effectiveTime;
private LocalDateTime expireTime;
}
// 价格服务实现
@Service
public class PriceCalculationServiceImpl implements PriceCalculationService {
@Autowired
private ProductRepository productRepository;
@Autowired
private PromotionService promotionService;
@Autowired
private RegionPriceRepository regionPriceRepository;
@Override
public PriceCalculationResult calculatePrice(Long productId, int quantity,
Long regionId, Long customerId) {
// 1. 获取基础信息
Product product = productRepository.findById(productId)
.orElseThrow(() -> new BusinessException("商品不存在"));
// 2. 获取区域价格(优先)
RegionPrice regionPrice = regionPriceRepository.findByProductIdAndRegionId(
productId, regionId);
BigDecimal basePrice = regionPrice != null ?
regionPrice.getPrice() : product.getBasePrice();
// 3. 检查促销活动
List activePromotions = promotionService.getActivePromotions(
productId, regionId, LocalDateTime.now());
// 4. 应用最优促销
Promotion appliedPromo = selectBestPromotion(activePromotions, quantity);
BigDecimal finalPrice = appliedPromo != null ?
applyPromotion(basePrice, appliedPromo, quantity) : basePrice;
// 5. 返回结果
return buildResult(product, basePrice, finalPrice, appliedPromo);
}
private Promotion selectBestPromotion(List promotions, int quantity) {
// 实现促销选择逻辑(优先级、叠加规则等)
// ...
}
private BigDecimal applyPromotion(BigDecimal basePrice, Promotion promo, int quantity) {
// 实现各种促销类型的计算逻辑
// ...
}
}
```
3. 缓存策略
```java
@Configuration
@EnableCaching
public class CacheConfig {
@Bean
public CacheManager cacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(
new ConcurrentMapCache("productPrice"),
new ConcurrentMapCache("promotionRules")
));
return cacheManager;
}
}
@Service
public class CachedPriceService {
@Autowired
private PriceCalculationService priceService;
@Cacheable(value = "productPrice", key = " productId + _ + regionId")
public PriceCalculationResult getCachedPrice(Long productId, Long regionId) {
return priceService.calculatePrice(productId, 1, regionId);
}
@CacheEvict(value = "productPrice", key = " productId + _ + regionId")
public void invalidatePriceCache(Long productId, Long regionId) {
// 当价格变更时清除缓存
}
}
```
四、关键业务逻辑实现
1. 价格计算流程
```
开始
│
├─ 获取商品基础信息
│ ├─ 商品ID、名称、单位等
│ └─ 获取当前基础价格
│
├─ 检查是否有有效促销
│ ├─ 按优先级排序促销活动
│ └─ 选择最优促销方案
│
├─ 应用促销规则
│ ├─ 折扣计算
│ ├─ 满减计算
│ └─ 组合优惠计算
│
├─ 考虑区域定价差异
│ ├─ 检查区域价格覆盖
│ └─ 应用区域加价/减价
│
├─ 最终价格计算
│ ├─ 汇总所有优惠
│ └─ 确保不低于成本价
│
└─ 返回价格计算结果
├─ 最终价格
├─ 优惠明细
└─ 价格有效期
结束
```
2. 促销规则引擎实现
```java
public class PromotionRuleEngine {
public PromotionResult evaluate(List promotions,
Product product,
int quantity,
BigDecimal basePrice) {
// 1. 过滤有效促销(时间、区域、商品匹配)
List validPromos = filterValidPromotions(promotions, product);
// 2. 按优先级排序
validPromos.sort(Comparator.comparingInt(Promotion::getPriority).reversed());
// 3. 尝试应用每个促销,记录最佳结果
PromotionResult bestResult = null;
for (Promotion promo : validPromos) {
PromotionResult currentResult = applyPromotion(promo, product, quantity, basePrice);
if (bestResult == null ||
(currentResult.getFinalPrice().compareTo(bestResult.getFinalPrice()) < 0)) {
bestResult = currentResult;
}
}
return bestResult != null ? bestResult :
new PromotionResult(basePrice, Collections.emptyList());
}
private PromotionResult applyPromotion(Promotion promo,
Product product,
int quantity,
BigDecimal basePrice) {
switch (promo.getType()) {
case DISCOUNT:
return applyDiscount(promo, basePrice);
case FULL_REDUCTION:
return applyFullReduction(promo, basePrice, quantity);
case GIFT:
return applyGiftPromotion(promo, product);
// 其他促销类型...
default:
throw new IllegalArgumentException("Unknown promotion type");
}
}
private PromotionResult applyDiscount(Promotion promo, BigDecimal basePrice) {
BigDecimal discountRate = promo.getDiscountRate();
BigDecimal finalPrice = basePrice.multiply(
BigDecimal.valueOf(1 - discountRate.doubleValue()));
return new PromotionResult(
finalPrice,
Collections.singletonMap("折扣", discountRate),
"享受" + (discountRate * 100) + "折优惠"
);
}
// 其他促销应用方法...
}
```
五、系统优化与扩展
1. 性能优化
- 缓存策略:对频繁查询的价格数据实施多级缓存(本地缓存+分布式缓存)
- 异步计算:对批量价格计算任务采用异步处理方式
- 数据分片:按区域/品类对价格数据进行分片存储
2. 扩展功能
- 价格预测:基于历史数据和机器学习模型预测价格走势
- 竞争对手价格监控:集成第三方数据源监控竞品价格
- 自动化调价:根据预设规则自动调整价格
3. 监控与告警
- 价格异常检测:监控价格波动异常情况
- 调价失败告警:当自动调价失败时及时通知相关人员
- 审计日志:完整记录所有价格变更操作
六、实施路线图
1. 第一阶段(2周):完成基础价格管理功能
- 实现商品基础价格设置与查询
- 建立价格变更历史记录
2. 第二阶段(3周):开发促销管理功能
- 实现多种促销类型支持
- 开发促销活动管理界面
3. 第三阶段(2周):实现动态定价与区域定价
- 开发成本加成定价功能
- 支持按区域设置不同价格
4. 第四阶段(2周):优化与测试
- 性能优化与压力测试
- 完善监控与告警机制
七、技术选型建议
- 后端框架:Spring Boot + MyBatis/JPA
- 数据库:MySQL(主) + Redis(缓存)
- 搜索引擎:Elasticsearch(用于快速价格查询)
- 消息队列:Kafka(用于异步处理价格变更事件)
- 监控系统:Prometheus + Grafana
此方案可根据实际业务需求和技术栈进行调整,重点在于构建灵活、可扩展的价格管理体系,以适应生鲜行业快速变化的市场需求。