-- S6 Phase 1A · 生产指令中台消费层：mdp_std_work_order_schedule 扩列 + 生产指令单列表菜单重指（幂等）
-- 说明：S2 转换服务用 CREATE TABLE IF NOT EXISTS 建表，不会 ALTER 既有表；此脚本为存量库补列。
--       新列全 nullable，兼容历史行；重新跑 S2_MDP_SYNC_TRANSFORM 后由转换 SQL 回填。

-- ① mdp_std_work_order_schedule 增列（逐列 information_schema 守卫，幂等）
SET @tbl := 'mdp_std_work_order_schedule';

SET @col := 'specification';
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=@tbl AND COLUMN_NAME=@col);
SET @sql := IF(@exist=0, 'ALTER TABLE mdp_std_work_order_schedule ADD COLUMN specification VARCHAR(200) NULL AFTER prod_line', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := 'lot_serial';
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=@tbl AND COLUMN_NAME=@col);
SET @sql := IF(@exist=0, 'ALTER TABLE mdp_std_work_order_schedule ADD COLUMN lot_serial VARCHAR(100) NULL AFTER specification', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := 'drawing_no';
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=@tbl AND COLUMN_NAME=@col);
SET @sql := IF(@exist=0, 'ALTER TABLE mdp_std_work_order_schedule ADD COLUMN drawing_no VARCHAR(100) NULL AFTER lot_serial', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := 'project';
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=@tbl AND COLUMN_NAME=@col);
SET @sql := IF(@exist=0, 'ALTER TABLE mdp_std_work_order_schedule ADD COLUMN project VARCHAR(200) NULL AFTER drawing_no', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := 'work_order_type';
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=@tbl AND COLUMN_NAME=@col);
SET @sql := IF(@exist=0, 'ALTER TABLE mdp_std_work_order_schedule ADD COLUMN work_order_type VARCHAR(50) NULL AFTER project', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col := 'labor_variance';
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME=@tbl AND COLUMN_NAME=@col);
SET @sql := IF(@exist=0, 'ALTER TABLE mdp_std_work_order_schedule ADD COLUMN labor_variance DECIMAL(18,6) NULL AFTER work_order_type', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ② 生产指令单列表菜单：占位组件改真实页（存量库既有菜单行的 Component 更新；种子仅 INSERT 不 UPDATE 既有行）
UPDATE sys_menu
SET Component = '/aidop/s6/production-record/productionInstructionList',
    Remark = 'S6 生产指令单列表（数据中台标准层只读）'
WHERE Id = 1329016010001
  AND Component = '/aidop/planning/index';
