-- S0 租户隔离补齐：为 12 张缺 tenant_id 的 S0 列表主表补列 + 索引 + 存量行归属回填 + 唯一键租户化。
--
-- 归属证据（2026-08-25 只读核对，全部行可 100%% 回推，无法回推 = 0）：
--   这些表的存量行引用 SysOrg 1329900200001/…002/…003，而这三个组织由 SeedData/S0OrgSeedData.cs
--   灌到 OrderFlowConstants.DemoTenantId = 797403760988230（AIDOP-Demo），**不是** AIDOP 797403760988229。
--   合计回填 90 行：PersonSkill 36 / S0ContractReviewCycleBreakdown 18 / S0ContractReviewCycle 10 /
--   S0OrderScheduleCycle 5 / S0OrderReviewCycle 5 / S0ProductDesignCycle 5 / S0CategoryLeadTime 5 /
--   S0MaterialPlanCycle 5 / S0ContractReviewCycleSyncFlag 1。
--   预期副作用：这批行归位 AIDOP-Demo 后，将不再出现在 AIDOP(797…229) 的对应页面——这是正确的租户归属，不是缺陷。
--
-- 回填口径：不写死租户 ID，一律 JOIN SysOrg 按 factory 列反查 TenantId；解析不出的行保持 NULL（不猜）。
-- PriorityCode 例外：该表本就有 tenant_id 且 4 行均为 797403760988229，无 factory 列，故只做唯一键租户化、不回填。
-- 空表（WorkOrderControl / ado_s0_sales_order_priority_rule）只做 ADD COLUMN + 索引，不伪造回填。
--
-- 唯一键：原唯一键均不含租户，而应用层唯一性预检已改为租户内判重，二者口径必须一致，故统一前置 tenant_id。
-- 索引名保持不变，使 AidopS0ExcelColumnMigration.EnsureUniqueIndex（按名判存在）不会把旧结构建回来。
--
-- 幂等：ADD COLUMN / ADD INDEX 均有 information_schema 存在性保护；回填带 WHERE tenant_id IS NULL；
--       唯一键重建按实际列清单比对，已是目标结构则跳过。重复执行无副作用。

-- [1/12] S0OrderScheduleCycle
SET @t1 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderScheduleCycle');
SET @c1 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderScheduleCycle' AND COLUMN_NAME='tenant_id');
SET @a1 := CASE WHEN @t1=1 AND @c1=0
  THEN 'ALTER TABLE `S0OrderScheduleCycle` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p1a FROM @a1; EXECUTE p1a; DEALLOCATE PREPARE p1a;

SET @i1 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderScheduleCycle' AND INDEX_NAME='idx_tenant');
SET @b1 := CASE WHEN @t1=1 AND @i1=0
  THEN 'ALTER TABLE `S0OrderScheduleCycle` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p1b FROM @b1; EXECUTE p1b; DEALLOCATE PREPARE p1b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d1 := CASE WHEN @t1=1
  THEN 'UPDATE `S0OrderScheduleCycle` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p1d FROM @d1; EXECUTE p1d; DEALLOCATE PREPARE p1d;

SET @u1 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderScheduleCycle' AND INDEX_NAME='uk_S0OrderScheduleCycle_factory_ordertype');
SET @e1 := CASE
  WHEN @t1<>1 THEN 'SELECT 1'
  WHEN @u1 = 'tenant_id,factory_ref_id,order_type' THEN 'SELECT 1'
  WHEN @u1 IS NULL THEN 'ALTER TABLE `S0OrderScheduleCycle` ADD UNIQUE INDEX `uk_S0OrderScheduleCycle_factory_ordertype` (`tenant_id`,`factory_ref_id`,`order_type`)'
  ELSE 'ALTER TABLE `S0OrderScheduleCycle` DROP INDEX `uk_S0OrderScheduleCycle_factory_ordertype`, ADD UNIQUE INDEX `uk_S0OrderScheduleCycle_factory_ordertype` (`tenant_id`,`factory_ref_id`,`order_type`)' END;
PREPARE p1e FROM @e1; EXECUTE p1e; DEALLOCATE PREPARE p1e;

-- [2/12] WorkOrderControl
SET @t2 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='WorkOrderControl');
SET @c2 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='WorkOrderControl' AND COLUMN_NAME='tenant_id');
SET @a2 := CASE WHEN @t2=1 AND @c2=0
  THEN 'ALTER TABLE `WorkOrderControl` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p2a FROM @a2; EXECUTE p2a; DEALLOCATE PREPARE p2a;

SET @i2 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='WorkOrderControl' AND INDEX_NAME='idx_tenant');
SET @b2 := CASE WHEN @t2=1 AND @i2=0
  THEN 'ALTER TABLE `WorkOrderControl` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p2b FROM @b2; EXECUTE p2b; DEALLOCATE PREPARE p2b;

-- 回填：按 FactoryRefId 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d2 := CASE WHEN @t2=1
  THEN 'UPDATE `WorkOrderControl` t JOIN `SysOrg` o ON o.`Id` = t.`FactoryRefId` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p2d FROM @d2; EXECUTE p2d; DEALLOCATE PREPARE p2d;

SET @u2 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='WorkOrderControl' AND INDEX_NAME='uk_WorkOrderControl_code');
SET @e2 := CASE
  WHEN @t2<>1 THEN 'SELECT 1'
  WHEN @u2 = 'tenant_id,CompanyRefId,FactoryRefId,Code' THEN 'SELECT 1'
  WHEN @u2 IS NULL THEN 'ALTER TABLE `WorkOrderControl` ADD UNIQUE INDEX `uk_WorkOrderControl_code` (`tenant_id`,`CompanyRefId`,`FactoryRefId`,`Code`)'
  ELSE 'ALTER TABLE `WorkOrderControl` DROP INDEX `uk_WorkOrderControl_code`, ADD UNIQUE INDEX `uk_WorkOrderControl_code` (`tenant_id`,`CompanyRefId`,`FactoryRefId`,`Code`)' END;
PREPARE p2e FROM @e2; EXECUTE p2e; DEALLOCATE PREPARE p2e;

-- [3/12] PersonSkill
SET @t3 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PersonSkill');
SET @c3 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PersonSkill' AND COLUMN_NAME='tenant_id');
SET @a3 := CASE WHEN @t3=1 AND @c3=0
  THEN 'ALTER TABLE `PersonSkill` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p3a FROM @a3; EXECUTE p3a; DEALLOCATE PREPARE p3a;

SET @i3 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PersonSkill' AND INDEX_NAME='idx_tenant');
SET @b3 := CASE WHEN @t3=1 AND @i3=0
  THEN 'ALTER TABLE `PersonSkill` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p3b FROM @b3; EXECUTE p3b; DEALLOCATE PREPARE p3b;

-- 回填：按 FactoryRefId 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d3 := CASE WHEN @t3=1
  THEN 'UPDATE `PersonSkill` t JOIN `SysOrg` o ON o.`Id` = t.`FactoryRefId` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p3d FROM @d3; EXECUTE p3d; DEALLOCATE PREPARE p3d;

SET @u3 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PersonSkill' AND INDEX_NAME='uk_PersonSkill_code');
SET @e3 := CASE
  WHEN @t3<>1 THEN 'SELECT 1'
  WHEN @u3 = 'tenant_id,CompanyRefId,FactoryRefId,Code' THEN 'SELECT 1'
  WHEN @u3 IS NULL THEN 'ALTER TABLE `PersonSkill` ADD UNIQUE INDEX `uk_PersonSkill_code` (`tenant_id`,`CompanyRefId`,`FactoryRefId`,`Code`)'
  ELSE 'ALTER TABLE `PersonSkill` DROP INDEX `uk_PersonSkill_code`, ADD UNIQUE INDEX `uk_PersonSkill_code` (`tenant_id`,`CompanyRefId`,`FactoryRefId`,`Code`)' END;
PREPARE p3e FROM @e3; EXECUTE p3e; DEALLOCATE PREPARE p3e;

-- [4/12] S0ContractReviewCycle
SET @t4 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycle');
SET @c4 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycle' AND COLUMN_NAME='tenant_id');
SET @a4 := CASE WHEN @t4=1 AND @c4=0
  THEN 'ALTER TABLE `S0ContractReviewCycle` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p4a FROM @a4; EXECUTE p4a; DEALLOCATE PREPARE p4a;

SET @i4 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycle' AND INDEX_NAME='idx_tenant');
SET @b4 := CASE WHEN @t4=1 AND @i4=0
  THEN 'ALTER TABLE `S0ContractReviewCycle` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p4b FROM @b4; EXECUTE p4b; DEALLOCATE PREPARE p4b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d4 := CASE WHEN @t4=1
  THEN 'UPDATE `S0ContractReviewCycle` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p4d FROM @d4; EXECUTE p4d; DEALLOCATE PREPARE p4d;

SET @u4 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycle' AND INDEX_NAME='uk_S0ContractReviewCycle_factory_stage');
SET @e4 := CASE
  WHEN @t4<>1 THEN 'SELECT 1'
  WHEN @u4 = 'tenant_id,factory_ref_id,stage_code' THEN 'SELECT 1'
  WHEN @u4 IS NULL THEN 'ALTER TABLE `S0ContractReviewCycle` ADD UNIQUE INDEX `uk_S0ContractReviewCycle_factory_stage` (`tenant_id`,`factory_ref_id`,`stage_code`)'
  ELSE 'ALTER TABLE `S0ContractReviewCycle` DROP INDEX `uk_S0ContractReviewCycle_factory_stage`, ADD UNIQUE INDEX `uk_S0ContractReviewCycle_factory_stage` (`tenant_id`,`factory_ref_id`,`stage_code`)' END;
PREPARE p4e FROM @e4; EXECUTE p4e; DEALLOCATE PREPARE p4e;

-- [5/12] S0ContractReviewCycleBreakdown
SET @t5 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleBreakdown');
SET @c5 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleBreakdown' AND COLUMN_NAME='tenant_id');
SET @a5 := CASE WHEN @t5=1 AND @c5=0
  THEN 'ALTER TABLE `S0ContractReviewCycleBreakdown` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p5a FROM @a5; EXECUTE p5a; DEALLOCATE PREPARE p5a;

SET @i5 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleBreakdown' AND INDEX_NAME='idx_tenant');
SET @b5 := CASE WHEN @t5=1 AND @i5=0
  THEN 'ALTER TABLE `S0ContractReviewCycleBreakdown` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p5b FROM @b5; EXECUTE p5b; DEALLOCATE PREPARE p5b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d5 := CASE WHEN @t5=1
  THEN 'UPDATE `S0ContractReviewCycleBreakdown` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p5d FROM @d5; EXECUTE p5d; DEALLOCATE PREPARE p5d;

SET @u5 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleBreakdown' AND INDEX_NAME='uk_S0CRCBreakdown_factory_parent_group');
SET @e5 := CASE
  WHEN @t5<>1 THEN 'SELECT 1'
  WHEN @u5 = 'tenant_id,factory_ref_id,parent_stage_code,group_code' THEN 'SELECT 1'
  WHEN @u5 IS NULL THEN 'ALTER TABLE `S0ContractReviewCycleBreakdown` ADD UNIQUE INDEX `uk_S0CRCBreakdown_factory_parent_group` (`tenant_id`,`factory_ref_id`,`parent_stage_code`,`group_code`)'
  ELSE 'ALTER TABLE `S0ContractReviewCycleBreakdown` DROP INDEX `uk_S0CRCBreakdown_factory_parent_group`, ADD UNIQUE INDEX `uk_S0CRCBreakdown_factory_parent_group` (`tenant_id`,`factory_ref_id`,`parent_stage_code`,`group_code`)' END;
PREPARE p5e FROM @e5; EXECUTE p5e; DEALLOCATE PREPARE p5e;

-- [6/12] S0ContractReviewCycleSyncFlag
SET @t6 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleSyncFlag');
SET @c6 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleSyncFlag' AND COLUMN_NAME='tenant_id');
SET @a6 := CASE WHEN @t6=1 AND @c6=0
  THEN 'ALTER TABLE `S0ContractReviewCycleSyncFlag` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p6a FROM @a6; EXECUTE p6a; DEALLOCATE PREPARE p6a;

SET @i6 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleSyncFlag' AND INDEX_NAME='idx_tenant');
SET @b6 := CASE WHEN @t6=1 AND @i6=0
  THEN 'ALTER TABLE `S0ContractReviewCycleSyncFlag` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p6b FROM @b6; EXECUTE p6b; DEALLOCATE PREPARE p6b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d6 := CASE WHEN @t6=1
  THEN 'UPDATE `S0ContractReviewCycleSyncFlag` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p6d FROM @d6; EXECUTE p6d; DEALLOCATE PREPARE p6d;

SET @u6 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ContractReviewCycleSyncFlag' AND INDEX_NAME='uk_S0CRCSyncFlag_factory');
SET @e6 := CASE
  WHEN @t6<>1 THEN 'SELECT 1'
  WHEN @u6 = 'tenant_id,factory_ref_id' THEN 'SELECT 1'
  WHEN @u6 IS NULL THEN 'ALTER TABLE `S0ContractReviewCycleSyncFlag` ADD UNIQUE INDEX `uk_S0CRCSyncFlag_factory` (`tenant_id`,`factory_ref_id`)'
  ELSE 'ALTER TABLE `S0ContractReviewCycleSyncFlag` DROP INDEX `uk_S0CRCSyncFlag_factory`, ADD UNIQUE INDEX `uk_S0CRCSyncFlag_factory` (`tenant_id`,`factory_ref_id`)' END;
PREPARE p6e FROM @e6; EXECUTE p6e; DEALLOCATE PREPARE p6e;

-- [7/12] S0OrderReviewCycle
SET @t7 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderReviewCycle');
SET @c7 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderReviewCycle' AND COLUMN_NAME='tenant_id');
SET @a7 := CASE WHEN @t7=1 AND @c7=0
  THEN 'ALTER TABLE `S0OrderReviewCycle` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p7a FROM @a7; EXECUTE p7a; DEALLOCATE PREPARE p7a;

SET @i7 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderReviewCycle' AND INDEX_NAME='idx_tenant');
SET @b7 := CASE WHEN @t7=1 AND @i7=0
  THEN 'ALTER TABLE `S0OrderReviewCycle` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p7b FROM @b7; EXECUTE p7b; DEALLOCATE PREPARE p7b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d7 := CASE WHEN @t7=1
  THEN 'UPDATE `S0OrderReviewCycle` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p7d FROM @d7; EXECUTE p7d; DEALLOCATE PREPARE p7d;

SET @u7 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0OrderReviewCycle' AND INDEX_NAME='uk_S0OrderReviewCycle_factory_ordertype');
SET @e7 := CASE
  WHEN @t7<>1 THEN 'SELECT 1'
  WHEN @u7 = 'tenant_id,factory_ref_id,order_type' THEN 'SELECT 1'
  WHEN @u7 IS NULL THEN 'ALTER TABLE `S0OrderReviewCycle` ADD UNIQUE INDEX `uk_S0OrderReviewCycle_factory_ordertype` (`tenant_id`,`factory_ref_id`,`order_type`)'
  ELSE 'ALTER TABLE `S0OrderReviewCycle` DROP INDEX `uk_S0OrderReviewCycle_factory_ordertype`, ADD UNIQUE INDEX `uk_S0OrderReviewCycle_factory_ordertype` (`tenant_id`,`factory_ref_id`,`order_type`)' END;
PREPARE p7e FROM @e7; EXECUTE p7e; DEALLOCATE PREPARE p7e;

-- [8/12] PriorityCode
SET @t8 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PriorityCode');
SET @c8 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PriorityCode' AND COLUMN_NAME='tenant_id');
SET @a8 := CASE WHEN @t8=1 AND @c8=0
  THEN 'ALTER TABLE `PriorityCode` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p8a FROM @a8; EXECUTE p8a; DEALLOCATE PREPARE p8a;

SET @i8 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PriorityCode' AND INDEX_NAME='idx_tenant');
SET @b8 := CASE WHEN @t8=1 AND @i8=0
  THEN 'ALTER TABLE `PriorityCode` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p8b FROM @b8; EXECUTE p8b; DEALLOCATE PREPARE p8b;

-- 不回填：PriorityCode 无 factory 列，存量 tenant_id 已正确（797403760988229）。

SET @u8 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='PriorityCode' AND INDEX_NAME='uk_PriorityCode_scope');
SET @e8 := CASE
  WHEN @t8<>1 THEN 'SELECT 1'
  WHEN @u8 = 'tenant_id,Descr' THEN 'SELECT 1'
  WHEN @u8 IS NULL THEN 'ALTER TABLE `PriorityCode` ADD UNIQUE INDEX `uk_PriorityCode_scope` (`tenant_id`,`Descr`)'
  ELSE 'ALTER TABLE `PriorityCode` DROP INDEX `uk_PriorityCode_scope`, ADD UNIQUE INDEX `uk_PriorityCode_scope` (`tenant_id`,`Descr`)' END;
PREPARE p8e FROM @e8; EXECUTE p8e; DEALLOCATE PREPARE p8e;

-- [9/12] ado_s0_sales_order_priority_rule
SET @t9 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='ado_s0_sales_order_priority_rule');
SET @c9 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='ado_s0_sales_order_priority_rule' AND COLUMN_NAME='tenant_id');
SET @a9 := CASE WHEN @t9=1 AND @c9=0
  THEN 'ALTER TABLE `ado_s0_sales_order_priority_rule` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p9a FROM @a9; EXECUTE p9a; DEALLOCATE PREPARE p9a;

SET @i9 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='ado_s0_sales_order_priority_rule' AND INDEX_NAME='idx_tenant');
SET @b9 := CASE WHEN @t9=1 AND @i9=0
  THEN 'ALTER TABLE `ado_s0_sales_order_priority_rule` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p9b FROM @b9; EXECUTE p9b; DEALLOCATE PREPARE p9b;

-- 回填：按 FactoryRefId 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d9 := CASE WHEN @t9=1
  THEN 'UPDATE `ado_s0_sales_order_priority_rule` t JOIN `SysOrg` o ON o.`Id` = t.`FactoryRefId` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p9d FROM @d9; EXECUTE p9d; DEALLOCATE PREPARE p9d;

-- ado_s0_sales_order_priority_rule 无业务唯一键，无需租户化。

-- [10/12] S0ProductDesignCycle
SET @t10 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ProductDesignCycle');
SET @c10 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ProductDesignCycle' AND COLUMN_NAME='tenant_id');
SET @a10 := CASE WHEN @t10=1 AND @c10=0
  THEN 'ALTER TABLE `S0ProductDesignCycle` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p10a FROM @a10; EXECUTE p10a; DEALLOCATE PREPARE p10a;

SET @i10 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ProductDesignCycle' AND INDEX_NAME='idx_tenant');
SET @b10 := CASE WHEN @t10=1 AND @i10=0
  THEN 'ALTER TABLE `S0ProductDesignCycle` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p10b FROM @b10; EXECUTE p10b; DEALLOCATE PREPARE p10b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d10 := CASE WHEN @t10=1
  THEN 'UPDATE `S0ProductDesignCycle` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p10d FROM @d10; EXECUTE p10d; DEALLOCATE PREPARE p10d;

SET @u10 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0ProductDesignCycle' AND INDEX_NAME='uk_S0ProductDesignCycle_factory_type_attr');
SET @e10 := CASE
  WHEN @t10<>1 THEN 'SELECT 1'
  WHEN @u10 = 'tenant_id,factory_ref_id,item_type,owner_application' THEN 'SELECT 1'
  WHEN @u10 IS NULL THEN 'ALTER TABLE `S0ProductDesignCycle` ADD UNIQUE INDEX `uk_S0ProductDesignCycle_factory_type_attr` (`tenant_id`,`factory_ref_id`,`item_type`,`owner_application`)'
  ELSE 'ALTER TABLE `S0ProductDesignCycle` DROP INDEX `uk_S0ProductDesignCycle_factory_type_attr`, ADD UNIQUE INDEX `uk_S0ProductDesignCycle_factory_type_attr` (`tenant_id`,`factory_ref_id`,`item_type`,`owner_application`)' END;
PREPARE p10e FROM @e10; EXECUTE p10e; DEALLOCATE PREPARE p10e;

-- [11/12] S0CategoryLeadTime
SET @t11 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0CategoryLeadTime');
SET @c11 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0CategoryLeadTime' AND COLUMN_NAME='tenant_id');
SET @a11 := CASE WHEN @t11=1 AND @c11=0
  THEN 'ALTER TABLE `S0CategoryLeadTime` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p11a FROM @a11; EXECUTE p11a; DEALLOCATE PREPARE p11a;

SET @i11 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0CategoryLeadTime' AND INDEX_NAME='idx_tenant');
SET @b11 := CASE WHEN @t11=1 AND @i11=0
  THEN 'ALTER TABLE `S0CategoryLeadTime` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p11b FROM @b11; EXECUTE p11b; DEALLOCATE PREPARE p11b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d11 := CASE WHEN @t11=1
  THEN 'UPDATE `S0CategoryLeadTime` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p11d FROM @d11; EXECUTE p11d; DEALLOCATE PREPARE p11d;

SET @u11 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0CategoryLeadTime' AND INDEX_NAME='uk_S0CategoryLeadTime_factory_category_owner');
SET @e11 := CASE
  WHEN @t11<>1 THEN 'SELECT 1'
  WHEN @u11 = 'tenant_id,factory_ref_id,category_code,owner_application' THEN 'SELECT 1'
  WHEN @u11 IS NULL THEN 'ALTER TABLE `S0CategoryLeadTime` ADD UNIQUE INDEX `uk_S0CategoryLeadTime_factory_category_owner` (`tenant_id`,`factory_ref_id`,`category_code`,`owner_application`)'
  ELSE 'ALTER TABLE `S0CategoryLeadTime` DROP INDEX `uk_S0CategoryLeadTime_factory_category_owner`, ADD UNIQUE INDEX `uk_S0CategoryLeadTime_factory_category_owner` (`tenant_id`,`factory_ref_id`,`category_code`,`owner_application`)' END;
PREPARE p11e FROM @e11; EXECUTE p11e; DEALLOCATE PREPARE p11e;

-- [12/12] S0MaterialPlanCycle
SET @t12 := (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0MaterialPlanCycle');
SET @c12 := (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0MaterialPlanCycle' AND COLUMN_NAME='tenant_id');
SET @a12 := CASE WHEN @t12=1 AND @c12=0
  THEN 'ALTER TABLE `S0MaterialPlanCycle` ADD COLUMN `tenant_id` BIGINT NULL COMMENT ''租户Id'''
  ELSE 'SELECT 1' END;
PREPARE p12a FROM @a12; EXECUTE p12a; DEALLOCATE PREPARE p12a;

SET @i12 := (SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0MaterialPlanCycle' AND INDEX_NAME='idx_tenant');
SET @b12 := CASE WHEN @t12=1 AND @i12=0
  THEN 'ALTER TABLE `S0MaterialPlanCycle` ADD INDEX `idx_tenant` (`tenant_id`)'
  ELSE 'SELECT 1' END;
PREPARE p12b FROM @b12; EXECUTE p12b; DEALLOCATE PREPARE p12b;

-- 回填：按 factory_ref_id 反查 SysOrg.TenantId（解析不出则保持 NULL）
SET @d12 := CASE WHEN @t12=1
  THEN 'UPDATE `S0MaterialPlanCycle` t JOIN `SysOrg` o ON o.`Id` = t.`factory_ref_id` SET t.`tenant_id` = o.`TenantId` WHERE t.`tenant_id` IS NULL AND o.`TenantId` IS NOT NULL'
  ELSE 'SELECT 1' END;
PREPARE p12d FROM @d12; EXECUTE p12d; DEALLOCATE PREPARE p12d;

SET @u12 := (SELECT GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='S0MaterialPlanCycle' AND INDEX_NAME='uk_S0MaterialPlanCycle_factory_ordertype');
SET @e12 := CASE
  WHEN @t12<>1 THEN 'SELECT 1'
  WHEN @u12 = 'tenant_id,factory_ref_id,order_type' THEN 'SELECT 1'
  WHEN @u12 IS NULL THEN 'ALTER TABLE `S0MaterialPlanCycle` ADD UNIQUE INDEX `uk_S0MaterialPlanCycle_factory_ordertype` (`tenant_id`,`factory_ref_id`,`order_type`)'
  ELSE 'ALTER TABLE `S0MaterialPlanCycle` DROP INDEX `uk_S0MaterialPlanCycle_factory_ordertype`, ADD UNIQUE INDEX `uk_S0MaterialPlanCycle_factory_ordertype` (`tenant_id`,`factory_ref_id`,`order_type`)' END;
PREPARE p12e FROM @e12; EXECUTE p12e; DEALLOCATE PREPARE p12e;
