功能概述
商品更新记录功能用于跟踪和管理小象买菜系统中商品信息的变更历史,包括价格调整、库存变动、商品上下架等操作记录。
数据库设计
商品更新记录表(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 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 Date updateTime;
private String remark;
}
```
2. 服务层实现
```java
@Service
public class ProductUpdateLogService {
@Autowired
private ProductUpdateLogMapper productUpdateLogMapper;
/
* 记录商品更新
*/
public void recordUpdate(Long productId, String updateType,
Object oldValue, Object newValue,
Long operatorId, String remark) {
ProductUpdateLog log = new ProductUpdateLog();
log.setProductId(productId);
log.setUpdateType(updateType);
log.setOldValue(JSON.toJSONString(oldValue));
log.setNewValue(JSON.toJSONString(newValue));
log.setUpdateBy(operatorId);
log.setRemark(remark);
productUpdateLogMapper.insert(log);
}
/
* 查询商品更新记录
*/
public List
getUpdateLogs(Long productId,
Date startTime,
Date endTime,
Integer pageNum,
Integer pageSize) {
// 实现分页查询逻辑
// ...
}
}
```
3. 商品服务中集成更新记录
```java
@Service
public class ProductService {
@Autowired
private ProductUpdateLogService updateLogService;
/
* 更新商品价格示例
*/
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. 记录更新日志
Map oldValue = new HashMap<>();
oldValue.put("price", oldPrice);
Map newValue = new HashMap<>();
newValue.put("price", newPrice);
updateLogService.recordUpdate(
productId,
"price",
oldValue,
newValue,
operatorId,
"修改商品价格"
);
}
// 其他更新操作类似...
}
```
前端实现
1. 更新记录列表页面
```vue
商品更新记录
v-model="dateRange"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期">
查询
{{ formatUpdateType(row.updateType) }}
查看详情
@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">
变更前:
{{ oldValue }} 变更后:
{{ newValue }}
<script>
export default {
data() {
return {
dateRange: [],
logList: [],
pagination: {
currentPage: 1,
pageSize: 10,
total: 0
},
detailVisible: false,
oldValue: ,
newValue: ,
currentLog: null
}
},
methods: {
formatUpdateType(type) {
const map = {
price: 价格变更,
stock: 库存变更,
status: 上下架状态变更,
info: 商品信息变更
};
return map[type] || type;
},
search() {
this.fetchLogs();
},
fetchLogs() {
const params = {
productId: this.$route.params.id,
startTime: this.dateRange?.[0] || null,
endTime: this.dateRange?.[1] || null,
pageNum: this.pagination.currentPage,
pageSize: this.pagination.pageSize
};
api.getProductUpdateLogs(params).then(res => {
this.logList = res.data.list;
this.pagination.total = res.data.total;
});
},
showDetail(log) {
this.currentLog = log;
this.oldValue = this.formatJson(log.oldValue);
this.newValue = this.formatJson(log.newValue);
this.detailVisible = true;
},
formatJson(jsonStr) {
try {
const obj = JSON.parse(jsonStr);
return JSON.stringify(obj, null, 2);
} catch (e) {
return jsonStr || 无;
}
},
handleSizeChange(val) {
this.pagination.pageSize = val;
this.fetchLogs();
},
handleCurrentChange(val) {
this.pagination.currentPage = val;
this.fetchLogs();
}
},
created() {
this.fetchLogs();
}
}
<style scoped>
.detail-content {
max-height: 500px;
overflow-y: auto;
}
pre {
background: f5f5f5;
padding: 10px;
border-radius: 4px;
white-space: pre-wrap;
word-wrap: break-word;
}
```
关键点说明
1. 更新类型分类:将更新操作分为价格(price)、库存(stock)、状态(status)、信息(info)等类型
2. JSON存储变更:使用JSON格式存储变更前后的值,便于扩展和解析
3. 操作人记录:记录每次变更的操作人ID,便于追溯
4. 时间戳记录:精确记录变更时间,支持按时间范围查询
5. 分页查询:支持大数据量的分页查询
6. 前端展示:提供友好的变更前后对比展示
扩展功能建议
1. 变更通知:对重要变更(如价格大幅调整)发送通知给相关人员
2. 变更审批:对关键商品信息的变更设置审批流程
3. 变更回滚:支持基于更新记录回滚到指定版本
4. 数据可视化:对价格、销量等关键指标的变更趋势进行可视化展示
5. 操作日志导出:支持将更新记录导出为Excel等格式
通过实现商品更新记录功能,可以显著提升小象买菜系统的商品管理可追溯性,便于问题排查和审计追踪。