-- ============================================
-- 表结构字段检查与修复SQL
-- 确保表字段与代码一致
-- ============================================

-- 检查 eb_dynamic 表是否有正确的字段
-- 如果表存在但字段不对，需要重建

-- 1. 检查并修复 eb_dynamic 表
DROP TABLE IF EXISTS `eb_dynamic_backup`;
CREATE TABLE IF NOT EXISTS `eb_dynamic` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `uid` int UNSIGNED NOT NULL COMMENT '用户ID',
  `nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户昵称',
  `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '用户头像',
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '动态内容',
  `images` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '图片列表',
  `video` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '视频地址',
  `location` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '位置信息',
  `like_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '点赞数',
  `comment_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '评论数',
  `share_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '分享数',
  `view_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '浏览数',
  `is_top` tinyint NOT NULL DEFAULT 0 COMMENT '是否置顶 0=否 1=是',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态 0=已删除 1=正常 2=审核中',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE,
  KEY `idx_status` (`status`) USING BTREE,
  KEY `idx_create_time` (`create_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '动态管理表' ROW_FORMAT = DYNAMIC;

-- 2. 检查并修复 eb_dynamic_comment 表
CREATE TABLE IF NOT EXISTS `eb_dynamic_comment` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `dynamic_id` int UNSIGNED NOT NULL COMMENT '动态ID',
  `uid` int UNSIGNED NOT NULL COMMENT '评论用户ID',
  `nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户昵称',
  `avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '用户头像',
  `content` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '评论内容',
  `reply_id` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '回复的评论ID',
  `reply_uid` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '回复的用户ID',
  `like_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '点赞数',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态 0=已删除 1=正常',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_dynamic_id` (`dynamic_id`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '动态评论表' ROW_FORMAT = DYNAMIC;

-- 3. 检查并修复 eb_fan_group 表
CREATE TABLE IF NOT EXISTS `eb_fan_group` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `anchor_id` int UNSIGNED NOT NULL COMMENT '主播ID',
  `anchor_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '主播昵称',
  `name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '粉丝团名称',
  `badge` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '粉丝团徽章',
  `badge_color` varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '#FF6B6B' COMMENT '徽章颜色',
  `member_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '成员数量',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态 0=已解散 1=正常',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_anchor_id` (`anchor_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '粉丝团表' ROW_FORMAT = DYNAMIC;

-- 4. 检查并修复 eb_fan_group_member 表
CREATE TABLE IF NOT EXISTS `eb_fan_group_member` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `group_id` int UNSIGNED NOT NULL COMMENT '粉丝团ID',
  `uid` int UNSIGNED NOT NULL COMMENT '用户ID',
  `nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户昵称',
  `level` int UNSIGNED NOT NULL DEFAULT 1 COMMENT '粉丝等级',
  `intimacy` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '亲密度',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态 0=已退出 1=正常',
  `join_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_group_id` (`group_id`) USING BTREE,
  KEY `idx_uid` (`uid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '粉丝团成员表' ROW_FORMAT = DYNAMIC;

-- 5. 检查并修复 eb_conversation 表 (会话表)
CREATE TABLE IF NOT EXISTS `eb_conversation` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `user1_id` int UNSIGNED NOT NULL COMMENT '用户1 ID',
  `user2_id` int UNSIGNED NOT NULL COMMENT '用户2 ID',
  `last_message` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '最后一条消息',
  `last_message_time` datetime NULL DEFAULT NULL COMMENT '最后消息时间',
  `message_count` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '消息数量',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态 0=已删除 1=正常',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_user1_id` (`user1_id`) USING BTREE,
  KEY `idx_user2_id` (`user2_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '会话记录表' ROW_FORMAT = DYNAMIC;

-- 6. 检查并修复 eb_private_message 表 (私聊消息表)
CREATE TABLE IF NOT EXISTS `eb_private_message` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `conversation_id` int UNSIGNED NOT NULL COMMENT '会话ID',
  `sender_id` int UNSIGNED NOT NULL COMMENT '发送者ID',
  `receiver_id` int UNSIGNED NOT NULL COMMENT '接收者ID',
  `content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '消息内容',
  `message_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'text' COMMENT '消息类型',
  `is_read` tinyint NOT NULL DEFAULT 0 COMMENT '是否已读 0=未读 1=已读',
  `is_recalled` tinyint NOT NULL DEFAULT 0 COMMENT '是否撤回 0=否 1=是',
  `original_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '撤回前的原始内容',
  `recall_time` datetime NULL DEFAULT NULL COMMENT '撤回时间',
  `status` tinyint NOT NULL DEFAULT 1 COMMENT '状态 0=已删除 1=正常',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_conversation_id` (`conversation_id`) USING BTREE,
  KEY `idx_sender_id` (`sender_id`) USING BTREE,
  KEY `idx_receiver_id` (`receiver_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '私聊消息表' ROW_FORMAT = DYNAMIC;

-- 7. 检查并修复 eb_call_record 表 (通话记录表)
CREATE TABLE IF NOT EXISTS `eb_call_record` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `call_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '通话唯一标识',
  `caller_id` int UNSIGNED NOT NULL COMMENT '主叫用户ID',
  `callee_id` int UNSIGNED NOT NULL COMMENT '被叫用户ID',
  `call_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'voice' COMMENT '通话类型 voice/video',
  `status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'calling' COMMENT '状态',
  `duration` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '通话时长(秒)',
  `call_time` datetime NULL DEFAULT NULL COMMENT '发起时间',
  `connect_time` datetime NULL DEFAULT NULL COMMENT '接通时间',
  `end_time` datetime NULL DEFAULT NULL COMMENT '结束时间',
  `end_reason` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '结束原因',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_caller_id` (`caller_id`) USING BTREE,
  KEY `idx_callee_id` (`callee_id`) USING BTREE,
  KEY `idx_call_time` (`call_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '通话记录表' ROW_FORMAT = DYNAMIC;

-- 8. 检查并修复 eb_follow_record 表 (关注记录表)
CREATE TABLE IF NOT EXISTS `eb_follow_record` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `follower_id` int UNSIGNED NOT NULL COMMENT '关注人ID',
  `follower_nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '关注人昵称',
  `follower_avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '关注人头像',
  `follower_phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '关注人手机号',
  `follower_level` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '关注人粉丝数',
  `followed_id` int UNSIGNED NOT NULL COMMENT '被关注人ID',
  `followed_nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '被关注人昵称',
  `followed_avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '被关注人头像',
  `followed_phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '被关注人手机号',
  `followed_level` int UNSIGNED NOT NULL DEFAULT 0 COMMENT '被关注人粉丝数',
  `follow_status` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '关注' COMMENT '关注状态',
  `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '关注时间',
  `cancel_time` datetime NULL DEFAULT NULL COMMENT '取消关注时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_follower_id` (`follower_id`) USING BTREE,
  KEY `idx_followed_id` (`followed_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '关注记录表' ROW_FORMAT = DYNAMIC;

-- 9. 检查并修复 eb_gift_reward_record 表 (礼物打赏记录表)
CREATE TABLE IF NOT EXISTS `eb_gift_reward_record` (
  `id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `giver_id` int UNSIGNED NOT NULL COMMENT '打赏人ID',
  `giver_nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '打赏人昵称',
  `giver_avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '打赏人头像',
  `giver_phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '打赏人手机号',
  `receiver_id` int UNSIGNED NOT NULL COMMENT '被打赏人ID',
  `receiver_nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '被打赏人昵称',
  `receiver_avatar` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '被打赏人头像',
  `receiver_phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '被打赏人手机号',
  `gift_id` int UNSIGNED NOT NULL COMMENT '礼物ID',
  `gift_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '礼物名称',
  `gift_count` int UNSIGNED NOT NULL DEFAULT 1 COMMENT '礼物数量',
  `reward_value` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '礼物单价',
  `reward_amount` decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '打赏总额',
  `member_type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '会员类型',
  `reward_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '打赏时间',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `idx_giver_id` (`giver_id`) USING BTREE,
  KEY `idx_receiver_id` (`receiver_id`) USING BTREE,
  KEY `idx_reward_time` (`reward_time`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '礼物打赏记录表' ROW_FORMAT = DYNAMIC;

-- 完成提示
SELECT '表结构检查与修复完成' AS message;
