-- 1.0.145.sql
-- S0-LINESKILLMASTER-ENTITY-SCHEMA-ALIGN-1
-- 目标：回填 LineSkillMaster / LineSkillDetail 历史 NULL tenant_id 至 AIDOP 租户。
-- 目标 tenant_id = 797403760988229（SysTenant.Id；SysOrg.Name='AIDOP'；与 6 张 S0 兄弟表 Domain='8010' 取值一致）
-- 证据来源：阶段 0.1 跨表比对 + UpdateScripts/1.0.132.sql:151 + UpdateScripts/1.0.109.sql:18,38
-- 幂等性：
--   * 仅 UPDATE WHERE tenant_id IS NULL；重复执行影响 0 行。
--   * 无 schema 变更；无 DELETE。
-- 安全边界：
--   * 不依赖 line_skill_master_id（历史 257 行全为 0，链接断裂；本批仅治 tenant_id，不修关联）。
--   * Domain='8010' 仅出现在前置校验和后置对账，用于说明本批回填数据均来自旧系统遗留域；
--     新系统数据隔离以 ITenantIdFilter 为准，不把 Domain='8010' 作为业务过滤口径。
--   * 无破坏性操作；无生产库 schema 变更；无跨表 DELETE。
-- 关联前置：
--   * server/Plugins/Admin.NET.Plugin.AiDOP/Entity/S0/Manufacturing/AdoS0LineSkillMaster.cs
--     补齐 Site / Orderby / WType / RunCrew 4 列实体映射（DB 已存在列）。
--   * 控制器 GetPagedAsync 取消强制 Domain 过滤；改 WhereIF 仅在请求显式传入时按 Domain 过滤。

-- ===========================================================================
-- 段 1：前置只读校验（不修改任何数据）
-- ===========================================================================

-- 校验 1.1：LineSkillMaster tenant_id IS NULL 数量（应 = 47，与阶段 0.1 证据一致；> 0 说明确有待回填）
SELECT COUNT(*) AS master_null_total FROM LineSkillMaster WHERE tenant_id IS NULL;

-- 校验 1.2：LineSkillDetail tenant_id IS NULL 数量（应 = 257）
SELECT COUNT(*) AS detail_null_total FROM LineSkillDetail WHERE tenant_id IS NULL;

-- 校验 1.3：NULL 数据的 Domain 分布（应只出现 '8010'；其它 Domain 出现需停止人工评估）
SELECT 'LineSkillMaster' AS src, Domain, COUNT(*) AS n
FROM LineSkillMaster
WHERE tenant_id IS NULL
GROUP BY Domain
UNION ALL
SELECT 'LineSkillDetail' AS src, Domain, COUNT(*) AS n
FROM LineSkillDetail
WHERE tenant_id IS NULL
GROUP BY Domain;

-- 校验 1.4：兄弟 S0 表 Domain='8010' 唯一 tenant_id 候选确认
--   （应全部返回 797403760988229；若出现多个 tenant_id，需停止人工评估，不得继续执行段 2）
SELECT 'LineMaster_8010_tenants'    AS src, tenant_id, COUNT(*) AS n FROM LineMaster     WHERE Domain='8010' AND tenant_id IS NOT NULL GROUP BY tenant_id
UNION ALL SELECT 'EmpSkills_8010_tenants',     tenant_id, COUNT(*) FROM EmpSkills      WHERE Domain='8010' AND tenant_id IS NOT NULL GROUP BY tenant_id
UNION ALL SELECT 'WorkCtrMaster_8010_tenants', tenant_id, COUNT(*) FROM WorkCtrMaster  WHERE Domain='8010' AND tenant_id IS NOT NULL GROUP BY tenant_id
UNION ALL SELECT 'ProdLineDetail_8010_tenants',tenant_id, COUNT(*) FROM ProdLineDetail WHERE Domain='8010' AND tenant_id IS NOT NULL GROUP BY tenant_id
UNION ALL SELECT 'EmployeeMaster_8010_tenants',tenant_id, COUNT(*) FROM EmployeeMaster WHERE Domain='8010' AND tenant_id IS NOT NULL GROUP BY tenant_id
UNION ALL SELECT 'ItemMaster_8010_tenants',    tenant_id, COUNT(*) FROM ItemMaster     WHERE Domain='8010' AND tenant_id IS NOT NULL GROUP BY tenant_id;

-- ===========================================================================
-- 段 2：回填（UPDATE；幂等，重复执行影响 0 行）
-- ===========================================================================

UPDATE LineSkillMaster
SET tenant_id = 797403760988229
WHERE tenant_id IS NULL;

UPDATE LineSkillDetail
SET tenant_id = 797403760988229
WHERE tenant_id IS NULL;

-- ===========================================================================
-- 段 3：执行后对账（只读）
-- ===========================================================================

-- 对账 3.1：LineSkillMaster tenant_id IS NULL 剩余数量（应 = 0）
SELECT COUNT(*) AS master_null_remaining FROM LineSkillMaster WHERE tenant_id IS NULL;

-- 对账 3.2：LineSkillDetail tenant_id IS NULL 剩余数量（应 = 0）
SELECT COUNT(*) AS detail_null_remaining FROM LineSkillDetail WHERE tenant_id IS NULL;

-- 对账 3.3：回填到 AIDOP 租户的行数（master 应 = 47，detail 应 = 257）
SELECT 'LineSkillMaster_aidop' AS src, COUNT(*) AS n FROM LineSkillMaster WHERE tenant_id = 797403760988229
UNION ALL SELECT 'LineSkillDetail_aidop',   COUNT(*)       FROM LineSkillDetail  WHERE tenant_id = 797403760988229;

-- 对账 3.4：回填后两表 tenant_id 分布（应只出现 797403760988229 一行）
SELECT 'LineSkillMaster' AS src, tenant_id, COUNT(*) AS n FROM LineSkillMaster GROUP BY tenant_id
UNION ALL SELECT 'LineSkillDetail', tenant_id, COUNT(*) FROM LineSkillDetail GROUP BY tenant_id;
