一、功能概述
商品迭代记录功能用于跟踪和管理叮咚买菜平台上商品的变更历史,包括商品信息修改、上下架状态变化、价格调整等关键操作记录,以提高商品管理的透明度和可追溯性。
二、系统架构设计
1. 数据库设计
```sql
-- 商品迭代记录表
CREATE TABLE `product_iteration_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 主键ID,
`product_id` bigint(20) NOT NULL COMMENT 商品ID,
`operation_type` tinyint(4) NOT NULL COMMENT 操作类型:1-新增 2-修改 3-上架 4-下架 5-删除 6-价格调整 7-库存变更,
`operation_field` varchar(50) DEFAULT NULL COMMENT 操作字段(修改时记录具体字段),
`old_value` text COMMENT 旧值,
`new_value` text COMMENT 新值,
`operator_id` bigint(20) NOT NULL COMMENT 操作人ID,
`operator_name` varchar(50) NOT NULL COMMENT 操作人姓名,
`operation_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 操作时间,
`operation_ip` varchar(50) DEFAULT NULL COMMENT 操作IP,
`remark` varchar(255) DEFAULT NULL COMMENT 备注,
PRIMARY KEY (`id`),
KEY `idx_product_id` (`product_id`),
KEY `idx_operation_time` (`operation_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=商品迭代记录表;
```
2. 微服务架构
```
商品管理服务
├── 商品CRUD接口
├── 商品状态变更接口
├── 价格调整接口
└── 迭代记录服务(AOP切面实现)
迭代记录服务
├── 记录新增商品
├── 记录修改商品信息
├── 记录上下架操作
├── 记录价格变更
└── 记录库存调整
```
三、核心功能实现
1. 基于AOP的自动记录实现
```java
@Aspect
@Component
public class ProductIterationAspect {
@Autowired
private ProductIterationLogService iterationLogService;
// 记录商品修改
@AfterReturning(pointcut = "execution(* com.dingdong.product.service.ProductService.updateProduct(..))",
returning = "result")
public void afterUpdateProduct(JoinPoint joinPoint, Object result) {
Object[] args = joinPoint.getArgs();
if (args.length > 0 && args[0] instanceof ProductUpdateDTO) {
ProductUpdateDTO updateDTO = (ProductUpdateDTO) args[0];
// 获取修改前后的差异(可通过缓存或数据库查询获取旧值)
// 构建迭代记录并保存
ProductIterationLog log = buildIterationLog(updateDTO, OperationType.UPDATE);
iterationLogService.save(log);
}
}
// 记录商品上下架
@AfterReturning(pointcut = "execution(* com.dingdong.product.service.ProductService.updateStatus(..))",
returning = "result")
public void afterUpdateStatus(JoinPoint joinPoint, Object result) {
// 类似实现...
}
private ProductIterationLog buildIterationLog(Object dto, OperationType type) {
// 构建迭代记录逻辑
// 包含操作类型、操作字段、新旧值、操作人等信息
}
}
```
2. 手动记录实现(对于复杂操作)
```java
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductIterationLogService iterationLogService;
@Override
public Boolean updatePrice(Long productId, BigDecimal newPrice, String operator) {
// 1. 获取当前价格
Product product = productRepository.findById(productId).orElseThrow(...);
BigDecimal oldPrice = product.getPrice();
// 2. 更新价格
product.setPrice(newPrice);
productRepository.save(product);
// 3. 记录迭代
ProductIterationLog log = new ProductIterationLog();
log.setProductId(productId);
log.setOperationType(OperationType.PRICE_CHANGE);
log.setOperationField("price");
log.setOldValue(oldPrice.toString());
log.setNewValue(newPrice.toString());
log.setOperatorName(operator);
iterationLogService.save(log);
return true;
}
}
```
四、前端展示实现
1. 商品详情页迭代记录标签页
```vue
商品迭代记录
{{ formatOperationType(row.operationType) }}
旧值: {{ row.oldValue }}
新值: {{ row.newValue }}
<script>
export default {
data() {
return {
iterationLogs: []
}
},
created() {
this.fetchIterationLogs(this.$route.params.productId);
},
methods: {
async fetchIterationLogs(productId) {
const response = await api.getIterationLogs(productId);
this.iterationLogs = response.data;
},
formatOperationType(type) {
const typeMap = {
1: 新增商品,
2: 修改信息,
3: 上架商品,
4: 下架商品,
5: 删除商品,
6: 价格调整,
7: 库存变更
};
return typeMap[type] || 未知操作;
}
}
}
```
五、高级功能实现
1. 差异对比可视化
```javascript
// 前端差异对比组件
function DiffViewer({ oldValue, newValue }) {
const diff = calculateDiff(oldValue, newValue);
return (
{diff.map((part, index) => (
key={index}
className={part.type === removed ? diff-removed :
part.type === added ? diff-added : diff-common}
>
{part.value}
))}
);
}
function calculateDiff(oldStr, newStr) {
// 实现差异计算算法(可使用现成的diff库)
// 返回格式:[{type: common/removed/added, value: 文本内容}]
}
```
2. 迭代记录导出功能
```java
@Service
public class IterationLogExportService {
public ByteArrayResource exportProductIterations(Long productId, Date startDate, Date endDate) {
List
logs = iterationLogRepository.findByProductIdAndTimeRange(
productId, startDate, endDate);
// 使用Apache POI或EasyExcel生成Excel
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try (Workbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("商品迭代记录");
// 创建表头
Row headerRow = sheet.createRow(0);
String[] headers = {"操作时间", "操作人", "操作类型", "操作字段", "旧值", "新值"};
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}
// 填充数据
int rowNum = 1;
for (ProductIterationLog log : logs) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(log.getOperationTime().toString());
row.createCell(1).setCellValue(log.getOperatorName());
// ... 其他单元格填充
}
workbook.write(outputStream);
} catch (IOException e) {
throw new RuntimeException("导出失败", e);
}
return new ByteArrayResource(outputStream.toByteArray()) {
@Override
public String getFilename() {
return "商品迭代记录_" + productId + "_" +
DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDate.now()) + ".xlsx";
}
};
}
}
```
六、性能优化方案
1. 异步记录:对非实时性要求高的操作采用异步记录
2. 批量写入:将短时间内多次操作合并批量写入
3. 索引优化:为常用查询字段建立索引
4. 分表策略:按时间或商品ID分表存储历史记录
5. 缓存策略:缓存频繁访问的商品当前状态
七、安全与合规考虑
1. 操作审计:确保所有修改都有完整记录
2. 权限控制:不同角色对迭代记录的查看权限
3. 数据保留:根据法规要求设置数据保留期限
4. 敏感信息:对价格等敏感信息的脱敏处理
八、测试用例示例
```java
@Test
public void testPriceChangeLogging() {
// 准备测试数据
Product product = productRepository.findById(1L).get();
BigDecimal oldPrice = product.getPrice();
// 执行价格修改
productService.updatePrice(1L, new BigDecimal("29.99"), "test_operator");
// 验证迭代记录
List logs = iterationLogRepository.findByProductIdOrderByOperationTimeDesc(1L);
assertEquals(1, logs.size());
assertEquals(OperationType.PRICE_CHANGE, logs.get(0).getOperationType());
assertEquals(oldPrice.toString(), logs.get(0).getOldValue());
assertEquals("29.99", logs.get(0).getNewValue());
}
```
九、部署与监控
1. 日志监控:监控迭代记录服务的调用频率和错误率
2. 告警机制:对异常操作(如批量修改)设置告警
3. 备份策略:定期备份迭代记录数据
4. 容量规划:根据业务增长预测存储需求
十、扩展功能建议
1. 变更影响分析:分析商品变更对销售的影响
2. 自动化回滚:基于迭代记录实现部分操作的自动回滚
3. AI异常检测:使用机器学习检测异常变更模式
4. 多维度报表:生成商品变更趋势分析报表
该实现方案提供了叮咚买菜系统商品迭代记录功能的完整开发路径,可根据实际业务需求和技术栈进行调整和扩展。