-- 添加作品热门相关字段
-- 执行时间：2026-01-08

USE `zhibo`;

-- 检查字段是否已存在，如果不存在则添加
-- 添加 is_hot 字段（如果不存在）
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists 
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'zhibo' 
  AND TABLE_NAME = 'eb_works' 
  AND COLUMN_NAME = 'is_hot';

SET @sql = IF(@col_exists = 0,
    'ALTER TABLE `eb_works` ADD COLUMN `is_hot` tinyint DEFAULT 0 COMMENT ''是否热门：1-是 0-否'' AFTER `status`',
    'SELECT ''字段 is_hot 已存在'' AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 添加 hot_time 字段（如果不存在）
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists 
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'zhibo' 
  AND TABLE_NAME = 'eb_works' 
  AND COLUMN_NAME = 'hot_time';

SET @sql = IF(@col_exists = 0,
    'ALTER TABLE `eb_works` ADD COLUMN `hot_time` datetime DEFAULT NULL COMMENT ''设置热门的时间'' AFTER `is_hot`',
    'SELECT ''字段 hot_time 已存在'' AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 添加索引（如果不存在）
SET @index_exists = 0;
SELECT COUNT(*) INTO @index_exists 
FROM information_schema.STATISTICS 
WHERE TABLE_SCHEMA = 'zhibo' 
  AND TABLE_NAME = 'eb_works' 
  AND INDEX_NAME = 'idx_is_hot';

SET @sql = IF(@index_exists = 0,
    'ALTER TABLE `eb_works` ADD INDEX `idx_is_hot` (`is_hot`)',
    'SELECT ''索引 idx_is_hot 已存在'' AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 查看最终结果
SHOW COLUMNS FROM `eb_works` LIKE '%hot%';
SELECT '作品热门字段添加完成！' AS result;
