功能概述 商品更新记录功能用于跟踪小象买菜系统中商品信息的变更历史,包括价格调整、库存变化、商品上下架等操作。该功能有助于: -审计商品信息变更 -追溯问题来源 -分析商品管理行为 -满足合规性要求 数据库设计 商品更新记录表(product_update_log)
功能概述
商品更新记录功能用于跟踪小象买菜系统中商品信息的变更历史,包括价格调整、库存变化、商品上下架等操作。该功能有助于:
- 审计商品信息变更
- 追溯问题来源
- 分析商品管理行为
- 满足合规性要求
数据库设计
商品更新记录表(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_by BIGINT COMMENT 操作人ID,
update_by_name VARCHAR(50) COMMENT 操作人姓名,
update_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT 更新时间,
remark VARCHAR(255) COMMENT 备注,
INDEX idx_product_id (product_id),
INDEX idx_update_time (update_time)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=商品更新记录表;
```
后端实现
1. 创建更新记录实体类
```java
@Data
public class ProductUpdateLog {
private Long id;
private Long productId;
private String updateType; // price/stock/status/info
private String oldValue; // JSON格式
private String newValue; // JSON格式
private Long updateBy;
private String updateByName;
private Date updateTime;
private String remark;
}
```
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.setUpdateBy(operatorId);
log.setUpdateByName(operatorName);
log.setRemark(remark);
productUpdateLogMapper.insert(log);
}
/
* 查询商品更新记录
*/
public List
getUpdateLogs(Long productId,
Date startTime, Date endTime) {
// 实现查询逻辑
}
}
```
3. 在商品服务中集成更新记录
```java
@Service
public class ProductService {
@Autowired
private ProductUpdateLogService productUpdateLogService;
/
* 更新商品价格示例
*/
@Transactional
public void updateProductPrice(Long productId, BigDecimal newPrice,
Long operatorId, String operatorName) {
// 1. 获取商品当前价格
Product product = productMapper.selectById(productId);
BigDecimal oldPrice = product.getPrice();
// 2. 更新商品价格
product.setPrice(newPrice);
productMapper.updateById(product);
// 3. 记录更新日志
Map oldValue = new HashMap<>();
oldValue.put("price", oldPrice);
Map newValue = new HashMap<>();
newValue.put("price", newPrice);
productUpdateLogService.recordUpdate(
productId,
"price",
JSON.toJSONString(oldValue),
JSON.toJSONString(newValue),
operatorId,
operatorName,
"调整商品价格"
);
}
// 类似地实现其他更新操作(库存、状态等)的日志记录
}
```
前端实现
1. 商品更新记录列表页面
```javascript
// Vue组件示例
export default {
data() {
return {
productId: this.$route.params.id,
updateLogs: [],
pagination: {
currentPage: 1,
pageSize: 10,
total: 0
},
loading: false
}
},
created() {
this.fetchUpdateLogs();
},
methods: {
async fetchUpdateLogs() {
this.loading = true;
try {
const res = await api.getProductUpdateLogs(
this.productId,
this.pagination.currentPage,
this.pagination.pageSize
);
this.updateLogs = res.data.list;
this.pagination.total = res.data.total;
} catch (error) {
console.error(error);
} finally {
this.loading = false;
}
},
// 分页变化处理
handlePageChange(page) {
this.pagination.currentPage = page;
this.fetchUpdateLogs();
}
}
}
```
2. 更新记录展示组件
```html
商品更新记录
{{ formatDate(row.updateTime) }}
{{ formatUpdateType(row.updateType) }}
查看详情
变更前:
{{ formatJson(row.oldValue) }} 变更后:
{{ formatJson(row.newValue) }}
@current-change="handlePageChange"
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
:total="pagination.total"
layout="prev, pager, next">
<script>
// 导入方法和组件...
export default {
// 组件实现...
}
<style scoped>
.change-detail pre {
background: f5f5f5;
padding: 10px;
border-radius: 4px;
max-height: 200px;
overflow-y: auto;
}
```
关键实现细节
1. 更新类型分类:
- price: 价格变更
- stock: 库存变更
- status: 上下架状态变更
- info: 商品基本信息变更
2. 数据格式化:
- 使用JSON格式存储旧值和新值,便于扩展和解析
- 前端展示时对JSON数据进行格式化,提高可读性
3. 性能优化:
- 对频繁更新的商品(如库存),考虑异步记录日志
- 对历史记录进行分表或归档,提高查询效率
4. 安全考虑:
- 记录操作人ID和姓名,便于追溯
- 对敏感操作(如价格修改)增加额外审计
扩展功能
1. 变更通知:当特定类型变更时发送通知(如价格下调超过10%)
2. 变更分析:统计商品变更频率,识别频繁修改的商品
3. 版本回滚:基于更新记录实现商品信息回滚功能
4. 操作权限:根据更新类型检查操作人权限
部署注意事项
1. 确保数据库有足够的存储空间,特别是高频变更的商品
2. 考虑使用消息队列异步处理日志记录,避免影响主业务
3. 定期备份更新记录数据
4. 设置合理的日志保留策略
通过以上实现,小象买菜系统可以有效地跟踪商品信息的所有变更,为商品管理提供完整的审计轨迹。