一、需求分析 针对川味冻品行业特点,批量价格调整功能需要满足以下核心需求: 1.支持按品类、品牌、规格等多维度筛选商品 2.支持多种价格调整策略(固定金额调整、百分比调整、阶梯定价等) 3.支持价格调整的预览和确认机制 4.记录完整的价格调整历史 5.考虑冻品行业的特殊属性(
一、需求分析
针对川味冻品行业特点,批量价格调整功能需要满足以下核心需求:
1. 支持按品类、品牌、规格等多维度筛选商品
2. 支持多种价格调整策略(固定金额调整、百分比调整、阶梯定价等)
3. 支持价格调整的预览和确认机制
4. 记录完整的价格调整历史
5. 考虑冻品行业的特殊属性(如保质期、批次管理)
二、系统架构设计
1. 数据库设计
```sql
-- 商品表扩展
ALTER TABLE products ADD COLUMN is_frozen TINYINT(1) DEFAULT 0 COMMENT 是否冻品;
ALTER TABLE products ADD COLUMN storage_condition VARCHAR(50) COMMENT 储存条件;
-- 价格调整记录表
CREATE TABLE price_adjustment_logs (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
adjustment_name VARCHAR(100) NOT NULL COMMENT 调整名称,
adjustment_type TINYINT NOT NULL COMMENT 1-固定金额 2-百分比 3-阶梯定价,
operator VARCHAR(50) NOT NULL COMMENT 操作人,
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
status TINYINT DEFAULT 0 COMMENT 0-待执行 1-已执行 2-已取消,
remark VARCHAR(255) COMMENT 备注
);
-- 价格调整明细表
CREATE TABLE price_adjustment_items (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
log_id BIGINT NOT NULL COMMENT 关联调整记录ID,
product_id BIGINT NOT NULL COMMENT 商品ID,
original_price DECIMAL(10,2) NOT NULL COMMENT 原价,
adjusted_price DECIMAL(10,2) NOT NULL COMMENT 调整后价格,
adjustment_value VARCHAR(50) NOT NULL COMMENT 调整值(存储具体调整数值或百分比),
FOREIGN KEY (log_id) REFERENCES price_adjustment_logs(id)
);
```
2. 核心模块设计
价格调整策略服务
```java
public interface PriceAdjustmentStrategy {
BigDecimal calculateNewPrice(BigDecimal originalPrice, String adjustmentValue);
}
// 固定金额调整策略
@Service("fixedAmountStrategy")
public class FixedAmountStrategy implements PriceAdjustmentStrategy {
@Override
public BigDecimal calculateNewPrice(BigDecimal originalPrice, String adjustmentValue) {
BigDecimal amount = new BigDecimal(adjustmentValue);
return originalPrice.add(amount);
}
}
// 百分比调整策略
@Service("percentageStrategy")
public class PercentageStrategy implements PriceAdjustmentStrategy {
@Override
public BigDecimal calculateNewPrice(BigDecimal originalPrice, String adjustmentValue) {
BigDecimal percentage = new BigDecimal(adjustmentValue.replace("%", "")).divide(new BigDecimal(100));
return originalPrice.multiply(BigDecimal.ONE.add(percentage)).setScale(2, RoundingMode.HALF_UP);
}
}
```
批量价格调整服务
```java
@Service
public class BatchPriceAdjustmentService {
@Autowired
private PriceAdjustmentStrategyFactory strategyFactory;
@Autowired
private ProductRepository productRepository;
@Autowired
private PriceAdjustmentLogRepository logRepository;
@Autowired
private PriceAdjustmentItemRepository itemRepository;
@Transactional
public Long initiateBatchAdjustment(PriceAdjustmentRequest request) {
// 1. 验证参数
validateRequest(request);
// 2. 查询符合条件的商品
List
products = productRepository.findByCriteria(request.getCriteria());
// 3. 创建调整记录
PriceAdjustmentLog log = new PriceAdjustmentLog();
log.setAdjustmentName(request.getAdjustmentName());
log.setAdjustmentType(request.getAdjustmentType());
log.setOperator(request.getOperator());
log.setStatus(0); // 待执行
logRepository.save(log);
// 4. 计算并保存调整明细
PriceAdjustmentStrategy strategy = strategyFactory.getStrategy(request.getAdjustmentType());
for (Product product : products) {
BigDecimal originalPrice = product.getPrice();
BigDecimal newPrice = strategy.calculateNewPrice(originalPrice, request.getAdjustmentValue());
PriceAdjustmentItem item = new PriceAdjustmentItem();
item.setLogId(log.getId());
item.setProductId(product.getId());
item.setOriginalPrice(originalPrice);
item.setAdjustedPrice(newPrice);
item.setAdjustmentValue(request.getAdjustmentValue());
itemRepository.save(item);
}
return log.getId();
}
@Transactional
public boolean executeAdjustment(Long logId) {
PriceAdjustmentLog log = logRepository.findById(logId)
.orElseThrow(() -> new RuntimeException("调整记录不存在"));
if (log.getStatus() != 0) {
throw new RuntimeException("只能执行待执行的调整记录");
}
List items = itemRepository.findByLogId(logId);
for (PriceAdjustmentItem item : items) {
productRepository.updatePrice(item.getProductId(), item.getAdjustedPrice());
}
log.setStatus(1); // 已执行
logRepository.save(log);
return true;
}
}
```
三、前端实现要点
1. 价格调整页面设计
```html
固定金额
百分比
阶梯定价
:placeholder="adjustmentForm.type === 2 ? 例如: 10% : 例如: 5">
预览调整
执行调整
```
2. 前端逻辑实现
```javascript
export default {
data() {
return {
adjustmentForm: {
name: ,
type: 1, // 默认固定金额
value:
},
selectedProducts: [],
previewData: [],
canExecute: false
}
},
methods: {
handleProductFilter(products) {
this.selectedProducts = products;
},
async previewAdjustment() {
if (!this.validateForm()) return;
try {
const response = await api.previewPriceAdjustment({
...this.adjustmentForm,
productIds: this.selectedProducts.map(p => p.id)
});
this.previewData = response.data.map(item => ({
productName: item.productName,
originalPrice: item.originalPrice,
adjustedPrice: item.adjustedPrice,
adjustmentValue: item.adjustmentValue
}));
this.canExecute = true;
} catch (error) {
this.$message.error(预览失败: + error.message);
}
},
async executeAdjustment() {
if (!this.previewData.length) {
this.$message.warning(请先预览调整结果);
return;
}
try {
await this.$confirm(确认执行价格调整吗?此操作不可撤销, 提示, {
confirmButtonText: 确定,
cancelButtonText: 取消,
type: warning
});
const response = await api.executePriceAdjustment({
logId: this.currentLogId // 从预览响应中获取
});
this.$message.success(价格调整执行成功);
this.previewData = [];
this.canExecute = false;
} catch (error) {
if (error !== cancel) {
this.$message.error(执行失败: + error.message);
}
}
},
validateForm() {
if (!this.adjustmentForm.name) {
this.$message.warning(请输入调整名称);
return false;
}
if (!this.adjustmentForm.value) {
this.$message.warning(请输入调整值);
return false;
}
if (this.adjustmentForm.type === 2 && !this.adjustmentForm.value.endsWith(%)) {
this.$message.warning(百分比调整请以%结尾);
return false;
}
if (!this.selectedProducts.length) {
this.$message.warning(请选择要调整的商品);
return false;
}
return true;
}
}
}
```
四、特殊业务考虑
1. 冻品行业特性处理:
- 在价格调整时考虑保质期因素,对临近保质期的商品可设置不同的调整策略
- 添加批次管理,确保价格调整能精确到具体批次
2. 价格调整审批流程:
- 对于大额价格调整,可集成工作流引擎实现审批流程
- 设置不同级别的调整权限
3. 价格调整影响分析:
- 预览时显示调整对毛利率的影响
- 提供按客户类型的价格调整影响分析
4. 历史价格保护:
- 记录商品的历史价格轨迹
- 设置价格调整的最小间隔天数
五、测试要点
1. 功能测试:
- 验证三种调整策略的正确性
- 测试多维度筛选条件组合
- 验证预览和执行流程
2. 性能测试:
- 测试批量调整1000+商品时的响应时间
- 验证高并发场景下的数据一致性
3. 安全测试:
- 验证权限控制是否有效
- 测试SQL注入等安全漏洞
4. 兼容性测试:
- 不同浏览器下的显示和功能正常
- 移动端适配情况
六、部署与运维
1. 部署方案:
- 建议采用蓝绿部署或金丝雀发布方式
- 准备回滚方案
2. 监控指标:
- 价格调整操作成功率
- 平均处理时间
- 错误率
3. 日志记录:
- 完整记录每次价格调整的操作日志
- 记录调整前后的价格对比
通过以上方案,可以实现一个稳定、高效、符合川味冻品行业特点的批量价格调整系统,帮助企业灵活应对市场价格变化,提高运营效率。