功能概述
商品更新记录功能用于跟踪和管理小象买菜系统中商品的变更历史,包括价格调整、库存变化、商品信息修改等操作。这有助于商家追溯商品变更历史、分析销售策略效果,并为系统审计提供支持。
数据库设计
商品更新记录表(product_update_log)
```sql
CREATE TABLE product_update_log (
id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT 记录ID,
product_id BIGINT NOT NULL COMMENT 商品ID,
update_type VARCHAR(20) NOT NULL COMMENT 更新类型(price/stock/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. 使用AOP切面实现自动记录
```java
@Aspect
@Component
public class ProductUpdateLogAspect {
@Autowired
private ProductUpdateLogService logService;
// 拦截商品服务中的更新方法
@AfterReturning(pointcut = "execution(* com.xiaoxiang.service.ProductService.update*(..))",
returning = "result")
public void afterProductUpdate(JoinPoint joinPoint, Object result) {
Object[] args = joinPoint.getArgs();
if (args.length > 0 && args[0] instanceof Product) {
Product product = (Product) args[0];
// 获取操作类型
String methodName = joinPoint.getSignature().getName();
String updateType = getUpdateType(methodName);
// 获取旧值(可以从缓存或数据库查询)
Product oldProduct = getOldProduct(product.getId());
// 创建更新记录
ProductUpdateLog log = new ProductUpdateLog();
log.setProductId(product.getId());
log.setUpdateType(updateType);
log.setOldValue(JSON.toJSONString(oldProduct));
log.setNewValue(JSON.toJSONString(product));
log.setOperatorId(getCurrentUserId()); // 从上下文获取
log.setOperatorName(getCurrentUserName());
logService.save(log);
}
}
private String getUpdateType(String methodName) {
if (methodName.contains("Price")) {
return "price";
} else if (methodName.contains("Stock")) {
return "stock";
} else {
return "info";
}
}
// 其他辅助方法...
}
```
2. 使用服务层手动记录
```java
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductUpdateLogService logService;
@Override
@Transactional
public boolean updateProductPrice(Long productId, BigDecimal newPrice) {
// 1. 查询旧商品信息
Product oldProduct = productMapper.selectById(productId);
// 2. 更新价格
boolean success = productMapper.updatePrice(productId, newPrice) > 0;
if (success) {
// 3. 记录更新日志
ProductUpdateLog log = new ProductUpdateLog();
log.setProductId(productId);
log.setUpdateType("price");
log.setOldValue(JSON.toJSONString(oldProduct.getPrice()));
log.setNewValue(JSON.toJSONString(newPrice));
log.setOperatorId(getCurrentUserId());
logService.save(log);
}
return success;
}
// 其他更新方法...
}
```
前端展示实现
商品详情页更新记录标签页
```html
商品更新记录
更新时间 |
更新类型 |
变更内容 |
操作人 |
|---|
{{ formatDate(log.updateTime) }} |
{{ getUpdateTypeText(log.updateType) }} |
价格从 {{ log.oldValue }} 改为 {{ log.newValue }}
库存从 {{ log.oldValue }} 改为 {{ log.newValue }}
商品信息变更(查看详情)
|
{{ log.operatorName }} |
```
变更详情弹窗
```html
旧价格: {{ currentLog.oldValue }}
新价格: {{ currentLog.newValue }}
旧库存: {{ currentLog.oldValue }}
新库存: {{ currentLog.newValue }}
商品信息变更
class="row" v-if="value.changed">
{{ getFieldName(key) }}
{{ value.old }}
{{ value.new }}
```
高级功能扩展
1. 变更对比可视化:
- 使用Diff算法高亮显示文本变更
- 对于复杂对象,提供树形结构对比视图
2. 变更通知:
- 重要商品变更时发送邮件/短信通知相关人员
- 集成企业微信/钉钉机器人推送变更消息
3. 变更分析:
- 统计各类变更的频率和趋势
- 分析价格变更与销售量的关系
4. 数据恢复:
- 支持基于更新记录回滚商品到指定版本
- 提供变更撤销功能
性能优化建议
1. 对高频更新字段(如库存)采用单独的日志表
2. 异步写入日志,避免阻塞主业务流程
3. 定期归档旧日志到历史表
4. 对JSON字段建立全文索引以提高查询效率
安全考虑
1. 记录操作人IP地址和设备信息
2. 对敏感字段(如成本价)进行脱敏处理
3. 实现细粒度的权限控制,限制日志查看范围
通过以上实现方案,小象买菜系统可以完整记录商品变更历史,为运营分析和问题追溯提供有力支持。