-- 🚀 智能修复 user_package 表 - 2026-01-23
-- 根据实际情况添加缺失的字段

-- 1. 先检查表结构
SELECT '=== 当前表结构 ===' AS '';
DESC user_package;

-- 2. 尝试添加 update_time 字段（如果不存在）
SET @sql = 'ALTER TABLE user_package ADD COLUMN `update_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ''更新时间''';

-- 检查字段是否存在
SET @column_exists = (
    SELECT COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'peixue' 
      AND TABLE_NAME = 'user_package' 
      AND COLUMN_NAME = 'update_time'
);

-- 如果字段不存在，则执行添加
SET @sql = IF(@column_exists = 0, @sql, 'SELECT ''update_time 字段已存在'' AS message');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 3. 检查 create_time 字段的默认值
SELECT 
    COLUMN_NAME AS '字段名',
    COLUMN_TYPE AS '字段类型',
    COLUMN_DEFAULT AS '默认值',
    EXTRA AS '额外属性'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'peixue'
  AND TABLE_NAME = 'user_package'
  AND COLUMN_NAME IN ('create_time', 'update_time', 'created_time', 'updated_time');

-- 4. 如果 create_time 存在但没有默认值，修改它
SET @fix_create_time = (
    SELECT COUNT(*) 
    FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_SCHEMA = 'peixue' 
      AND TABLE_NAME = 'user_package' 
      AND COLUMN_NAME = 'create_time'
      AND COLUMN_DEFAULT IS NULL
);

SET @sql = IF(@fix_create_time > 0, 
    'ALTER TABLE user_package MODIFY COLUMN `create_time` DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT ''创建时间''',
    'SELECT ''create_time 字段配置正确'' AS message'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 5. 验证修复结果
SELECT '=== 修复后的表结构 ===' AS '';
DESC user_package;

SELECT '✅ user_package 表已修复' AS '状态';
