功能概述
商品更新记录功能用于跟踪小象买菜系统中商品信息的变更历史,包括价格调整、库存变化、商品上下架等操作,便于审计和问题追溯。
数据库设计
商品更新记录表(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/status/info,
old_value TEXT COMMENT 旧值(JSON格式),
new_value TEXT COMMENT 新值(JSON格式),
update_by BIGINT COMMENT 操作人ID,
update_time DATETIME NOT NULL 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
@Service
public class ProductUpdateLogService {
@Autowired
private ProductUpdateLogMapper productUpdateLogMapper;
/
* 记录商品更新
* @param productId 商品ID
* @param updateType 更新类型
* @param oldValue 旧值
* @param newValue 新值
* @param operatorId 操作人ID
* @param remark 备注
*/
public void recordUpdate(Long productId, String updateType,
String oldValue, String newValue,
Long operatorId, String remark) {
ProductUpdateLog log = new ProductUpdateLog();
log.setProductId(productId);
log.setUpdateType(updateType);
log.setOldValue(oldValue);
log.setNewValue(newValue);
log.setUpdateBy(operatorId);
log.setRemark(remark);
productUpdateLogMapper.insert(log);
}
/
* 查询商品更新记录
* @param productId 商品ID
* @param startTime 开始时间
* @param endTime 结束时间
* @param pageNum 页码
* @param pageSize 每页大小
* @return 更新记录列表
*/
public PageInfo
queryUpdateLogs(Long productId,
Date startTime,
Date endTime,
Integer pageNum,
Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List logs = productUpdateLogMapper.selectByConditions(
productId, startTime, endTime);
return new PageInfo<>(logs);
}
}
```
2. 在商品服务中集成更新记录
```java
@Service
public class ProductService {
@Autowired
private ProductUpdateLogService productUpdateLogService;
@Transactional
public void updateProductPrice(Long productId, BigDecimal newPrice, Long operatorId) {
// 1. 获取商品当前信息
Product product = productMapper.selectById(productId);
BigDecimal oldPrice = product.getPrice();
// 2. 更新商品价格
product.setPrice(newPrice);
productMapper.updateById(product);
// 3. 记录更新日志
JSONObject oldValue = new JSONObject();
oldValue.put("price", oldPrice);
JSONObject newValue = new JSONObject();
newValue.put("price", newPrice);
productUpdateLogService.recordUpdate(
productId,
"price",
oldValue.toJSONString(),
newValue.toJSONString(),
operatorId,
"调整商品价格"
);
}
// 类似实现其他更新操作(库存、状态等)
}
```
前端实现
1. 更新记录列表页面
```vue
商品更新记录
v-model="dateRange"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
@change="handleDateChange"
/>
查询
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="pagination.currentPage"
:page-sizes="[10, 20, 50, 100]"
:page-size="pagination.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="pagination.total"
/>
<script>
export default {
data() {
return {
dateRange: [],
logList: [],
pagination: {
currentPage: 1,
pageSize: 10,
total: 0
},
productId: this.$route.params.id
}
},
created() {
this.fetchLogs();
},
methods: {
fetchLogs() {
const params = {
productId: this.productId,
startTime: this.dateRange ? this.dateRange[0] : null,
endTime: this.dateRange ? this.dateRange[1] : null,
pageNum: this.pagination.currentPage,
pageSize: this.pagination.pageSize
};
api.getProductUpdateLogs(params).then(response => {
this.logList = response.data.list;
this.pagination.total = response.data.total;
});
},
handleDateChange() {
this.pagination.currentPage = 1;
this.fetchLogs();
},
searchLogs() {
this.pagination.currentPage = 1;
this.fetchLogs();
},
handleSizeChange(val) {
this.pagination.pageSize = val;
this.fetchLogs();
},
handleCurrentChange(val) {
this.pagination.currentPage = val;
this.fetchLogs();
}
}
}
```
关键点说明
1. 更新类型定义:
- price: 价格变更
- stock: 库存变更
- status: 商品状态变更(上架/下架)
- info: 商品基本信息变更
2. 数据存储:
- 使用JSON格式存储旧值和新值,便于扩展和查询
- 记录操作人和操作时间,便于审计
3. 性能考虑:
- 对商品ID和更新时间建立索引
- 分页查询避免一次性加载过多数据
4. 扩展性:
- 可以添加更详细的变更字段对比
- 可以实现变更差异高亮显示
- 可以添加撤销变更功能
部署与监控
1. 确保数据库有足够的存储空间,因为更新记录会不断增长
2. 考虑定期归档或清理过期的更新记录
3. 监控更新记录的写入性能,避免影响主业务操作
通过实现商品更新记录功能,小象买菜系统可以更好地追踪商品信息变更,提高系统可维护性和问题排查效率。