# 订单变更记录表(store_order_change_log)使用说明 ## 表设计目的 记录每次下单/更新订单时商品种类和数量的变化,用于展示每次操作都加了什么商品。 ## 核心字段说明 ### 1. batch_no(批次号) - **作用**:同一时间点的操作使用同一批次号,用于分组展示 - **格式建议**:`ORDER{orderId}_{timestamp}` 或 `ORDER{orderId}_{yyyyMMddHHmmss}` - **示例**:`ORDER123_20250202143025` 表示订单123在2025-02-02 14:30:25的操作批次 ### 2. operation_type(操作类型) - **1:首次下单** - 创建订单时的操作 - **3:更新订单** - 更新订单时重新下单(只记录新增或数量增加的商品) ### 3. quantity_change(数量变化) - **正数**:新增的数量 - **负数**:减少的数量(如果支持减少) - **示例**:`+2` 表示新增2份,`-1` 表示减少1份 ### 4. quantity_before / quantity_after(变化前后数量) - **quantity_before**:变化前的数量 - **quantity_after**:变化后的数量 - **用途**:用于展示变化前后对比 ## 使用场景 ### 场景1:查询订单的所有变更记录(按时间排序) ```sql SELECT batch_no, operation_type, operation_time, cuisine_name, quantity_change, quantity_before, quantity_after, amount_change FROM store_order_change_log WHERE order_id = 123 AND delete_flag = 0 ORDER BY operation_time ASC; ``` ### 场景2:按批次分组展示每次操作 ```sql SELECT batch_no, operation_type, operation_time, COUNT(*) as item_count, SUM(quantity_change) as total_quantity_change, SUM(amount_change) as total_amount_change, GROUP_CONCAT(CONCAT(cuisine_name, ' x', quantity_change) SEPARATOR ', ') as items FROM store_order_change_log WHERE order_id = 123 AND delete_flag = 0 GROUP BY batch_no, operation_type, operation_time ORDER BY operation_time ASC; ``` ### 场景3:查询某次操作的所有商品 ```sql SELECT cuisine_name, quantity_change, unit_price, amount_change FROM store_order_change_log WHERE batch_no = 'ORDER123_20250202143025' AND delete_flag = 0 ORDER BY cuisine_id; ``` ### 场景4:查询更新订单记录 ```sql SELECT batch_no, operation_time, cuisine_name, quantity_change, amount_change FROM store_order_change_log WHERE order_id = 123 AND operation_type = 3 -- 更新订单 AND delete_flag = 0 ORDER BY operation_time DESC; ``` ## 数据记录示例 假设订单123的操作历史: ### 首次下单(batch_no: ORDER123_20250202140000) | cuisine_name | quantity_change | quantity_before | quantity_after | amount_change | |-------------|----------------|-----------------|----------------|---------------| | 石板肉酱豆腐 | 1 | 0 | 1 | 19.90 | | 经典三杯鸡 | 1 | 0 | 1 | 26.90 | ### 第一次更新订单(batch_no: ORDER123_20250202143000) | cuisine_name | quantity_change | quantity_before | quantity_after | amount_change | |-------------|----------------|-----------------|----------------|---------------| | 经典三杯鸡 | 1 | 1 | 2 | 26.90 | | 宫保鸡丁 | 1 | 0 | 1 | 28.00 | ### 第二次更新订单(batch_no: ORDER123_20250202150000) | cuisine_name | quantity_change | quantity_before | quantity_after | amount_change | |-------------|----------------|-----------------|----------------|---------------| | 石板肉酱豆腐 | 1 | 1 | 2 | 19.90 | ## 前端展示建议 ### 展示格式1:时间线展示 ``` 14:00 - 首次下单 • 石板肉酱豆腐 x1 • 经典三杯鸡 x1 14:30 - 更新订单 • 经典三杯鸡 x1(累计:2) • 宫保鸡丁 x1 15:00 - 更新订单 • 石板肉酱豆腐 x1(累计:2) ``` ### 展示格式2:批次卡片展示 ``` 批次1:首次下单(14:00) 石板肉酱豆腐 x1 ¥19.90 经典三杯鸡 x1 ¥26.90 小计:¥46.80 批次2:更新订单(14:30) 经典三杯鸡 x1 ¥26.90 宫保鸡丁 x1 ¥28.00 小计:¥54.90 批次3:更新订单(15:00) 石板肉酱豆腐 x1 ¥19.90 小计:¥19.90 ``` ## 注意事项 1. **批次号生成**:同一时间点的所有商品变化应该使用同一个批次号 2. **数量计算**:quantity_change 应该只记录本次变化,不是累计数量 3. **逻辑删除**:使用 delete_flag 进行逻辑删除,保留历史记录 4. **索引优化**:已创建必要的索引,查询性能有保障