-- MES 到货通知单增加采购订单编号字段
-- 将采购订单号从手动输入改为关联 ERP 采购订单

-- 添加采购订单编号字段
ALTER TABLE mes_wm_arrival_notice ADD COLUMN purchase_order_id BIGINT COMMENT '采购订单编号' AFTER name;

-- 数据迁移：将原有的采购订单号（purchase_order_code）与 ERP 采购订单表（erp_purchase_order）关联
-- 注意：此迁移脚本假设 purchase_order_code 存储的是 ERP 采购订单的 no 字段
-- UPDATE mes_wm_arrival_notice a
-- SET a.purchase_order_id = (SELECT p.id FROM erp_purchase_order p WHERE p.no = a.purchase_order_code LIMIT 1)
-- WHERE a.purchase_order_code IS NOT NULL AND a.purchase_order_code != '';