一、模块概述 价格管理模块是生鲜电商系统的核心功能之一,主要负责商品价格的定义、调整、展示和监控。针对生鲜行业特点,该模块需要支持多维度定价、动态调价、价格监控和促销策略等功能。 二、功能需求分析 1.基础价格管理 -商品基准价设置 -采购价与销售价分离管理 -不同渠
一、模块概述
价格管理模块是生鲜电商系统的核心功能之一,主要负责商品价格的定义、调整、展示和监控。针对生鲜行业特点,该模块需要支持多维度定价、动态调价、价格监控和促销策略等功能。
二、功能需求分析
1. 基础价格管理
- 商品基准价设置
- 采购价与销售价分离管理
- 不同渠道价格策略(B2B/B2C差异定价)
- 区域性价格差异管理
2. 动态定价机制
- 基于市场行情的自动调价
- 库存水平触发调价
- 保质期临近折扣
- 竞争对手价格监控与响应
3. 促销定价功能
- 满减/折扣/特价促销
- 限时抢购定价
- 会员专属价
- 组合商品定价
4. 价格监控与预警
- 价格异常波动监控
- 毛利率预警
- 竞品价格对比
- 历史价格追溯
三、系统架构设计
1. 技术架构
```
前端:React/Vue + Ant Design
后端:Spring Cloud微服务架构
数据库:MySQL(主) + Redis(缓存) + Elasticsearch(搜索)
消息队列:Kafka/RabbitMQ
定时任务:Quartz/Elastic-Job
```
2. 核心表设计
```sql
-- 商品价格主表
CREATE TABLE product_price (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
product_id BIGINT NOT NULL COMMENT 商品ID,
region_id BIGINT COMMENT 区域ID,
channel_type VARCHAR(20) COMMENT 渠道类型,
base_price DECIMAL(10,2) COMMENT 基准价,
current_price DECIMAL(10,2) COMMENT 当前售价,
cost_price DECIMAL(10,2) COMMENT 成本价,
min_price DECIMAL(10,2) COMMENT 最低售价,
max_price DECIMAL(10,2) COMMENT 最高售价,
price_type VARCHAR(20) COMMENT 价格类型,
status TINYINT DEFAULT 1 COMMENT 状态,
create_time DATETIME,
update_time DATETIME,
UNIQUE KEY uk_product_region_channel (product_id, region_id, channel_type)
);
-- 价格调整记录表
CREATE TABLE price_adjust_log (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
price_id BIGINT NOT NULL COMMENT 价格ID,
adjust_type VARCHAR(20) COMMENT 调整类型,
old_price DECIMAL(10,2) COMMENT 原价格,
new_price DECIMAL(10,2) COMMENT 新价格,
adjust_reason VARCHAR(200) COMMENT 调整原因,
operator_id BIGINT COMMENT 操作人ID,
operator_name VARCHAR(50) COMMENT 操作人姓名,
create_time DATETIME
);
-- 促销活动表
CREATE TABLE promotion_activity (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
activity_name VARCHAR(100) COMMENT 活动名称,
activity_type VARCHAR(20) COMMENT 活动类型,
start_time DATETIME COMMENT 开始时间,
end_time DATETIME COMMENT 结束时间,
status TINYINT DEFAULT 0 COMMENT 状态,
create_by BIGINT COMMENT 创建人,
create_time DATETIME,
update_time DATETIME
);
-- 促销商品关联表
CREATE TABLE promotion_product_rel (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
activity_id BIGINT NOT NULL COMMENT 活动ID,
product_id BIGINT NOT NULL COMMENT 商品ID,
promotion_price DECIMAL(10,2) COMMENT 促销价,
discount_rate DECIMAL(5,2) COMMENT 折扣率,
limit_count INT COMMENT 限购数量,
create_time DATETIME
);
```
四、核心功能实现
1. 价格计算服务
```java
@Service
public class PriceCalculationService {
@Autowired
private ProductPriceMapper priceMapper;
@Autowired
private PromotionService promotionService;
/
* 获取商品最终价格
*/
public PriceResult getFinalPrice(Long productId, Long regionId, String channelType,
Long userId, Integer quantity) {
// 1. 获取基础价格
ProductPrice price = priceMapper.selectCurrentPrice(productId, regionId, channelType);
if (price == null) {
throw new BusinessException("商品价格未设置");
}
// 2. 检查促销活动
PromotionResult promotion = promotionService.checkPromotion(
productId, regionId, channelType, userId, quantity);
// 3. 计算最终价格
PriceResult result = new PriceResult();
result.setProductId(productId);
result.setBasePrice(price.getCurrentPrice());
if (promotion != null && promotion.getPromotionPrice() != null) {
result.setFinalPrice(promotion.getPromotionPrice());
result.setPromotionInfo(promotion);
} else {
result.setFinalPrice(price.getCurrentPrice());
}
// 4. 应用会员折扣(如果有)
applyMemberDiscount(result, userId);
return result;
}
private void applyMemberDiscount(PriceResult result, Long userId) {
// 实现会员折扣逻辑
}
}
```
2. 动态调价策略
```java
@Component
public class DynamicPricingStrategy {
@Autowired
private PriceAdjustmentLogMapper logMapper;
@Autowired
private KafkaTemplate
kafkaTemplate;
/
* 基于库存的调价策略
*/
public void adjustByInventory(Long productId, Long regionId, String channelType) {
InventoryInfo inventory = inventoryService.getInventory(productId, regionId);
ProductPrice price = priceMapper.selectCurrentPrice(productId, regionId, channelType);
double ratio = (double)inventory.getAvailable() / inventory.getMaxCapacity();
double newPrice = price.getBasePrice();
// 库存越多,价格越低(示例策略)
if (ratio > 0.8) {
newPrice = price.getBasePrice() * 0.95; // 库存充足打95折
} else if (ratio < 0.3) {
newPrice = price.getBasePrice() * 1.1; // 库存紧张提价10%
}
// 确保在价格区间内
newPrice = Math.max(price.getMinPrice(), Math.min(price.getMaxPrice(), newPrice));
if (newPrice != price.getCurrentPrice()) {
updatePrice(price.getId(), newPrice, "库存动态调价");
}
}
/
* 保质期临近调价
*/
public void adjustByShelfLife(Long productId) {
ProductInfo product = productService.getProduct(productId);
Date productionDate = product.getProductionDate();
Date expiryDate = product.getExpiryDate();
long remainingDays = Duration.between(LocalDate.now().atStartOfDay(),
expiryDate.toInstant()).toDays();
if (remainingDays <= 3) { // 剩余3天
double discount = 1 - (0.1 * (4 - remainingDays)); // 每天增加10%折扣
applyDiscountToAllPrices(productId, discount);
}
}
private void updatePrice(Long priceId, double newPrice, String reason) {
// 更新价格记录
priceMapper.updatePrice(priceId, newPrice);
// 记录调价日志
PriceAdjustLog log = new PriceAdjustLog();
// 设置日志属性...
logMapper.insert(log);
// 发送价格变更消息
PriceChangeEvent event = new PriceChangeEvent(priceId, newPrice);
kafkaTemplate.send("price-change-topic", JSON.toJSONString(event));
}
}
```
3. 价格监控与预警
```java
@Service
public class PriceMonitoringService {
@Autowired
private ProductPriceMapper priceMapper;
@Autowired
private AlertService alertService;
/
* 毛利率监控
*/
@Scheduled(fixedRate = 3600000) // 每小时执行
public void monitorGrossProfit() {
List prices = priceMapper.selectAllActivePrices();
for (ProductPrice price : prices) {
double cost = price.getCostPrice();
double sale = price.getCurrentPrice();
if (cost > 0) {
double grossProfitRate = (sale - cost) / cost * 100;
if (grossProfitRate < 10) { // 毛利率低于10%预警
alertService.sendAlert(
AlertType.PRICE_WARNING,
String.format("商品%d毛利率过低: %.2f%%",
price.getProductId(), grossProfitRate),
AlertLevel.WARNING
);
}
}
}
}
/
* 价格异常波动监控
*/
public void monitorPriceFluctuation(Long productId) {
// 获取最近30天的价格记录
List histories = priceHistoryMapper.selectRecentHistory(productId, 30);
if (histories.size() > 5) { // 至少有5个历史数据点
// 计算价格标准差
double avg = histories.stream().mapToDouble(h -> h.getPrice()).average().orElse(0);
double variance = histories.stream()
.mapToDouble(h -> Math.pow(h.getPrice() - avg, 2))
.average().orElse(0);
double stdDev = Math.sqrt(variance);
// 获取最新价格
double currentPrice = priceMapper.selectCurrentPriceById(productId).getCurrentPrice();
// 如果价格偏离均值超过2个标准差
if (Math.abs(currentPrice - avg) > 2 * stdDev) {
alertService.sendAlert(
AlertType.PRICE_ABNORMAL,
String.format("商品%d价格异常波动: 当前价%.2f, 平均价%.2f, 标准差%.2f",
productId, currentPrice, avg, stdDev),
AlertLevel.DANGER
);
}
}
}
}
```
五、关键技术点
1. 价格计算缓存:使用Redis缓存商品价格,设置合理的过期时间,减少数据库查询
2. 分布式锁:在价格调整时使用分布式锁防止并发修改导致的数据不一致
3. 事务管理:确保价格调整和日志记录的原子性
4. 异步处理:价格变更通知等非实时操作采用消息队列异步处理
5. 数据一致性:通过最终一致性模型保证各系统间价格数据的一致
六、扩展功能建议
1. AI智能定价:集成机器学习模型,根据历史数据和市场趋势自动建议最优价格
2. 价格对比工具:提供竞品价格抓取和对比分析功能
3. 价格弹性分析:分析价格变动对销量的影响,优化定价策略
4. 多货币支持:为跨境业务提供多货币价格管理
5. 价格审批流:设置价格调整的审批流程,加强内控管理
七、实施建议
1. 分阶段实施:先实现基础价格管理,再逐步扩展动态定价和监控功能
2. 数据迁移:制定详细的数据迁移方案,确保历史价格数据完整迁移
3. 测试策略:重点测试价格计算准确性、并发调价场景和边界条件
4. 回滚机制:建立价格调整的回滚机制,应对意外情况
5. 监控体系:建立完善的价格系统监控体系,及时发现和处理异常
该价格管理模块设计充分考虑了生鲜行业的特殊性,如动态定价需求、短保质期商品处理等,同时保证了系统的扩展性和可维护性。