-- =============================================
-- 批次追溯功能修复 - DDL 脚本
-- 日期: 2026-07-13
-- 说明: 添加 feedback_id 字段用于批次追溯的精确关联
-- =============================================

-- 1. mes_wm_item_consume 表添加缺失字段
ALTER TABLE mes_wm_item_consume ADD COLUMN task_id bigint COMMENT '生产任务编号' AFTER work_order_id;
ALTER TABLE mes_wm_item_consume ADD COLUMN workstation_id bigint COMMENT '工作站编号' AFTER task_id;
ALTER TABLE mes_wm_item_consume ADD COLUMN process_id bigint COMMENT '工序编号' AFTER workstation_id;
ALTER TABLE mes_wm_item_consume ADD COLUMN feedback_id bigint COMMENT '报工记录编号' AFTER process_id;
ALTER TABLE mes_wm_item_consume ADD COLUMN consume_date datetime COMMENT '消耗日期' AFTER feedback_id;

-- 2. mes_wm_product_produce 表添加 feedback_id 字段
ALTER TABLE mes_wm_product_produce ADD COLUMN feedback_id bigint COMMENT '报工记录 ID' AFTER work_order_id;

-- 3. 为反馈 ID 添加索引（提升追溯查询性能）
CREATE INDEX idx_item_consume_feedback_id ON mes_wm_item_consume(feedback_id);
CREATE INDEX idx_product_produce_feedback_id ON mes_wm_product_produce(feedback_id);
