-- ==========================================
-- 消息系统重构数据迁移脚本
-- 执行时间：建议在业务低峰期执行
-- 注意：此脚本会删除关注消息数据，不考虑历史数据
-- ==========================================

BEGIN TRANSACTION;

-- 步骤1：备份现有数据（可选，建议执行）
PRINT '==========================================';
PRINT '步骤1：备份现有消息数据...';
PRINT '==========================================';

IF OBJECT_ID('T_Messages_Backup_BeforeRefactor', 'U') IS NOT NULL
BEGIN
    DROP TABLE T_Messages_Backup_BeforeRefactor;
    PRINT '已删除旧的备份表';
END

SELECT * INTO T_Messages_Backup_BeforeRefactor FROM T_Messages;
PRINT '备份完成，备份表：T_Messages_Backup_BeforeRefactor';
PRINT '';

-- 步骤2：删除关注消息（MessageType = 3）
PRINT '==========================================';
PRINT '步骤2：删除关注消息（MessageType = 3）...';
PRINT '==========================================';

DECLARE @DeletedFollowCount INT;
SELECT @DeletedFollowCount = COUNT(*) FROM T_Messages WHERE MessageType = 3;
PRINT '待删除关注消息数量：' + CAST(@DeletedFollowCount AS NVARCHAR(10));

DELETE FROM T_Messages WHERE MessageType = 3;
PRINT '已删除关注消息';
PRINT '';

-- 步骤3：调整系统通知类型：4 → 3
PRINT '==========================================';
PRINT '步骤3：调整系统通知类型（4 → 3）...';
PRINT '==========================================';

DECLARE @SystemNotificationCount INT;
SELECT @SystemNotificationCount = COUNT(*) FROM T_Messages WHERE MessageType = 4;
PRINT '待调整系统通知数量：' + CAST(@SystemNotificationCount AS NVARCHAR(10));

UPDATE T_Messages SET MessageType = 3 WHERE MessageType = 4;
PRINT '已将系统通知类型从 4 调整为 3';
PRINT '';

-- 步骤4：增加"我的评论内容"字段
PRINT '==========================================';
PRINT '步骤4：增加 MyCommentContent 字段...';
PRINT '==========================================';

IF NOT EXISTS (
    SELECT * FROM sys.columns 
    WHERE object_id = OBJECT_ID(N'dbo.T_Messages') 
    AND name = 'MyCommentContent'
)
BEGIN
    ALTER TABLE T_Messages 
    ADD MyCommentContent NVARCHAR(MAX) NULL;
    
    PRINT '已添加 MyCommentContent 字段（NVARCHAR(MAX) NULL）';
    
    -- 添加字段注释
    EXEC sp_addextendedproperty 
        @name = N'MS_Description', 
        @value = N'我的评论内容（回复消息展示被回复的内容）',
        @level0type = N'SCHEMA', @level0name = N'dbo',
        @level1type = N'TABLE',  @level1name = N'T_Messages',
        @level2type = N'COLUMN', @level2name = N'MyCommentContent';
    
    PRINT '已添加字段注释';
END
ELSE
BEGIN
    PRINT 'MyCommentContent 字段已存在，跳过添加';
END
PRINT '';

-- 步骤5：更新字段注释
PRINT '==========================================';
PRINT '步骤5：更新字段注释...';
PRINT '==========================================';

-- 更新 MessageType 字段注释
IF EXISTS (
    SELECT * FROM sys.extended_properties 
    WHERE major_id = OBJECT_ID('dbo.T_Messages') 
    AND name = 'MS_Description' 
    AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('dbo.T_Messages') AND name = 'MessageType')
)
BEGIN
    EXEC sp_updateextendedproperty 
        @name = N'MS_Description', 
        @value = N'消息类型：1-回复消息，2-点赞消息（含送花），3-系统消息',
        @level0type = N'SCHEMA', @level0name = N'dbo',
        @level1type = N'TABLE',  @level1name = N'T_Messages',
        @level2type = N'COLUMN', @level2name = N'MessageType';
    
    PRINT '已更新 MessageType 字段注释';
END
ELSE
BEGIN
    EXEC sp_addextendedproperty 
        @name = N'MS_Description', 
        @value = N'消息类型：1-回复消息，2-点赞消息（含送花），3-系统消息',
        @level0type = N'SCHEMA', @level0name = N'dbo',
        @level1type = N'TABLE',  @level1name = N'T_Messages',
        @level2type = N'COLUMN', @level2name = N'MessageType';
    
    PRINT '已添加 MessageType 字段注释';
END

-- 更新 ContentType 字段注释
IF EXISTS (
    SELECT * FROM sys.extended_properties 
    WHERE major_id = OBJECT_ID('dbo.T_Messages') 
    AND name = 'MS_Description' 
    AND minor_id = (SELECT column_id FROM sys.columns WHERE object_id = OBJECT_ID('dbo.T_Messages') AND name = 'ContentType')
)
BEGIN
    EXEC sp_updateextendedproperty 
        @name = N'MS_Description', 
        @value = N'内容类型：1-帖子，2-评论，3-送花',
        @level0type = N'SCHEMA', @level0name = N'dbo',
        @level1type = N'TABLE',  @level1name = N'T_Messages',
        @level2type = N'COLUMN', @level2name = N'ContentType';
    
    PRINT '已更新 ContentType 字段注释';
END
ELSE
BEGIN
    EXEC sp_addextendedproperty 
        @name = N'MS_Description', 
        @value = N'内容类型：1-帖子，2-评论，3-送花',
        @level0type = N'SCHEMA', @level0name = N'dbo',
        @level1type = N'TABLE',  @level1name = N'T_Messages',
        @level2type = N'COLUMN', @level2name = N'ContentType';
    
    PRINT '已添加 ContentType 字段注释';
END
PRINT '';

-- 步骤6：验证结果
PRINT '==========================================';
PRINT '步骤6：验证迁移结果...';
PRINT '==========================================';

PRINT '当前消息类型统计：';
SELECT 
    MessageType,
    CASE MessageType
        WHEN 1 THEN '回复消息'
        WHEN 2 THEN '点赞消息'
        WHEN 3 THEN '系统消息'
        ELSE '未知类型'
    END AS TypeName,
    COUNT(*) as Count
FROM T_Messages
GROUP BY MessageType
ORDER BY MessageType;

PRINT '';
PRINT '内容类型统计：';
SELECT 
    ContentType,
    CASE ContentType
        WHEN 0 THEN '无内容'
        WHEN 1 THEN '帖子'
        WHEN 2 THEN '评论'
        WHEN 3 THEN '送花'
        ELSE '其他'
    END AS TypeName,
    COUNT(*) as Count
FROM T_Messages
GROUP BY ContentType
ORDER BY ContentType;

PRINT '';
PRINT '==========================================';
PRINT '数据迁移完成！';
PRINT '==========================================';
PRINT '';
PRINT '提示：';
PRINT '1. 请在应用层清理 Redis 队列：DEL message:events:follow';
PRINT '2. 如需回滚，可从备份表 T_Messages_Backup_BeforeRefactor 恢复';
PRINT '3. 备份表可在确认无误后手动删除';
PRINT '';

COMMIT TRANSACTION;
PRINT '事务已提交';

