-- 规则版本停用状态字段化迁移脚本
-- 将停用状态从审计表迁移到版本表中的状态字段

-- 1. 为 cfg_rule_version 表添加状态相关字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'dbo.cfg_rule_version') AND name = N'status')
BEGIN
    ALTER TABLE dbo.cfg_rule_version ADD status INT NOT NULL DEFAULT 1; -- 1:Active, 0:Disabled
    PRINT '已添加 status 字段到 cfg_rule_version 表';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'dbo.cfg_rule_version') AND name = N'disabled_at_utc')
BEGIN
    ALTER TABLE dbo.cfg_rule_version ADD disabled_at_utc DATETIME2 NULL;
    PRINT '已添加 disabled_at_utc 字段到 cfg_rule_version 表';
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'dbo.cfg_rule_version') AND name = N'disabled_by')
BEGIN
    ALTER TABLE dbo.cfg_rule_version ADD disabled_by NVARCHAR(64) NULL;
    PRINT '已添加 disabled_by 字段到 cfg_rule_version 表';
END

-- 2. 迁移现有的停用状态数据
UPDATE v
SET v.status = 0,
    v.disabled_at_utc = a.action_at_utc,
    v.disabled_by = a.operator_name
FROM dbo.cfg_rule_version v
INNER JOIN (
    -- 获取每个版本的最新停用记录
    SELECT 
        product_type_code,
        version_no,
        action_at_utc,
        operator_name,
        ROW_NUMBER() OVER (PARTITION BY product_type_code, version_no ORDER BY action_at_utc DESC) as rn
    FROM dbo.cfg_rule_version_audit
    WHERE action_type = 'DISABLE'
) a ON v.product_type_code = a.product_type_code AND v.version_no = a.version_no AND a.rn = 1
WHERE EXISTS (
    SELECT 1 FROM dbo.cfg_rule_version_audit a2 
    WHERE a2.product_type_code = v.product_type_code 
      AND a2.version_no = v.version_no 
      AND a2.action_type = 'DISABLE'
);

PRINT '已迁移现有停用状态数据';

-- 3. 为新字段添加索引
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'dbo.cfg_rule_version') AND name = N'IX_cfg_rule_version_status')
BEGIN
    CREATE INDEX IX_cfg_rule_version_status ON dbo.cfg_rule_version(status);
    PRINT '已创建 status 字段索引';
END

IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'dbo.cfg_rule_version') AND name = N'IX_cfg_rule_version_disabled_at')
BEGIN
    CREATE INDEX IX_cfg_rule_version_disabled_at ON dbo.cfg_rule_version(disabled_at_utc);
    PRINT '已创建 disabled_at_utc 字段索引';
END

-- 4. 更新现有查询以使用新的状态字段（示例）
/*
-- 旧的查询方式：
SELECT version_no, snapshot_json, published_at_utc, published_by
      ,disabled_audit.action_at_utc AS disabled_at_utc
      ,disabled_audit.operator_name AS disabled_by
FROM dbo.cfg_rule_version
OUTER APPLY (
    SELECT TOP(1) action_at_utc, operator_name
    FROM dbo.cfg_rule_version_audit
    WHERE product_type_code = @product_type_code
      AND version_no = dbo.cfg_rule_version.version_no
      AND action_type = 'DISABLE'
    ORDER BY action_at_utc DESC
) disabled_audit
WHERE product_type_code = @product_type_code
ORDER BY published_at_utc DESC

-- 新的查询方式：
SELECT version_no, snapshot_json, published_at_utc, published_by
      ,disabled_at_utc, disabled_by
      ,CASE WHEN status = 0 THEN 1 ELSE 0 END AS is_disabled
FROM dbo.cfg_rule_version
WHERE product_type_code = @product_type_code
ORDER BY published_at_utc DESC
*/

PRINT '规则版本停用状态字段化迁移完成';
