功能概述
商品更新记录功能用于跟踪和管理小象买菜系统中商品信息的变更历史,包括价格调整、库存变化、商品上下架等操作记录。
数据库设计
商品更新记录表(product_update_log)
```sql
CREATE TABLE product_update_log (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
product_id BIGINT NOT NULL COMMENT 商品ID,
update_type VARCHAR(20) NOT NULL COMMENT 更新类型(price/stock/status/info),
old_value TEXT COMMENT 旧值(JSON格式),
new_value TEXT COMMENT 新值(JSON格式),
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 更新时间,
operator_id BIGINT COMMENT 操作人ID,
operator_name VARCHAR(50) COMMENT 操作人姓名,
remark VARCHAR(255) COMMENT 备注,
INDEX idx_product_id (product_id),
INDEX idx_update_time (update_time)
) ENGINE=InnoDB COMMENT=商品更新记录表;
```
后端实现
1. 实体类
```java
public class ProductUpdateLog {
private Long id;
private Long productId;
private String updateType;
private String oldValue; // JSON字符串
private String newValue; // JSON字符串
private Date updateTime;
private Long operatorId;
private String operatorName;
private String remark;
// getters and setters
}
```
2. 记录更新服务
```java
@Service
public class ProductUpdateLogService {
@Autowired
private ProductUpdateLogMapper productUpdateLogMapper;
public void recordUpdate(Long productId, String updateType,
String oldValue, String newValue,
Long operatorId, String operatorName, String remark) {
ProductUpdateLog log = new ProductUpdateLog();
log.setProductId(productId);
log.setUpdateType(updateType);
log.setOldValue(oldValue);
log.setNewValue(newValue);
log.setOperatorId(operatorId);
log.setOperatorName(operatorName);
log.setRemark(remark);
productUpdateLogMapper.insert(log);
}
public List
getUpdateHistory(Long productId) {
return productUpdateLogMapper.selectByProductId(productId);
}
}
```
3. 商品服务中集成记录功能
```java
@Service
public class ProductService {
@Autowired
private ProductUpdateLogService updateLogService;
public void updateProductPrice(Product product, BigDecimal newPrice, Operator operator) {
// 获取旧价格
BigDecimal oldPrice = product.getPrice();
// 更新商品价格
product.setPrice(newPrice);
// ... 保存商品逻辑
// 记录更新日志
JSONObject oldValue = new JSONObject();
oldValue.put("price", oldPrice);
JSONObject newValue = new JSONObject();
newValue.put("price", newPrice);
updateLogService.recordUpdate(
product.getId(),
"price",
oldValue.toJSONString(),
newValue.toJSONString(),
operator.getId(),
operator.getName(),
"修改商品价格"
);
}
// 其他更新方法类似...
}
```
前端实现
1. 商品详情页展示更新历史
```javascript
// 调用API获取更新历史
function fetchUpdateHistory(productId) {
axios.get(`/api/products/${productId}/update-history`)
.then(response => {
const history = response.data;
renderHistoryTable(history);
});
}
// 渲染历史表格
function renderHistoryTable(history) {
const tableBody = document.getElementById(history-table-body);
tableBody.innerHTML = ;
history.forEach(log => {
const row = document.createElement(tr);
const typeCell = document.createElement(td);
typeCell.textContent = getUpdateTypeText(log.updateType);
const timeCell = document.createElement(td);
timeCell.textContent = formatDateTime(log.updateTime);
const operatorCell = document.createElement(td);
operatorCell.textContent = log.operatorName;
const changeCell = document.createElement(td);
changeCell.innerHTML = `
旧值: ${formatChangeValue(log.updateType, log.oldValue)}
新值: ${formatChangeValue(log.updateType, log.newValue)}
`;
row.appendChild(typeCell);
row.appendChild(timeCell);
row.appendChild(operatorCell);
row.appendChild(changeCell);
tableBody.appendChild(row);
});
}
// 辅助函数
function getUpdateTypeText(type) {
const map = {
price: 价格变更,
stock: 库存变更,
status: 上下架状态变更,
info: 商品信息变更
};
return map[type] || type;
}
function formatChangeValue(type, value) {
try {
const obj = JSON.parse(value);
if (type === price) {
return `¥${obj.price}`;
} else if (type === stock) {
return obj.stock;
}
// 其他类型处理...
return JSON.stringify(obj);
} catch (e) {
return value;
}
}
```
2. 更新历史页面HTML结构
```html
```
高级功能扩展
1. 变更对比视图:对于复杂变更,提供可视化的差异对比
2. 版本回滚:允许回滚到特定历史版本
3. 变更通知:重要变更时发送通知给相关人员
4. 操作审计:记录操作IP、设备信息等增强安全性
5. 数据导出:支持导出更新历史为Excel/CSV
实施建议
1. 使用AOP切面简化日志记录代码,减少重复代码
2. 对于高频更新操作,考虑异步记录日志以提高性能
3. 定期归档或清理过旧的更新记录
4. 实现分页查询以提高大数据量时的查询性能
通过以上实现,小象买菜系统可以完整记录商品信息的变更历史,便于追溯问题、分析数据变化趋势和满足合规性要求。