-- ============================================================
-- VIP 订单修复脚本
-- 场景：甲方开发的 VIP 支付可能未正确设置 product_type
-- 会员价：1980 元
-- 执行前请先备份数据库！
-- ============================================================

-- 1. 诊断：查看当前疑似 VIP 订单的状态（执行后人工确认）
-- --------------------------------------------------------
SELECT id, order_sn, user_id, product_type, amount, status, pay_time, created_at
FROM orders
WHERE amount = 1980
  AND (status = 'paid' OR status = 'completed')
ORDER BY pay_time DESC;

-- 2. 统计：有多少条需要修复
-- --------------------------------------------------------
SELECT COUNT(*) AS need_fix_count
FROM orders
WHERE amount = 1980
  AND (status = 'paid' OR status = 'completed')
  AND (product_type IS NULL OR product_type = '' OR product_type NOT IN ('vip', 'fullbook'));

-- 3. 修复：将 1980 元已支付订单的 product_type 设为 'vip'
-- --------------------------------------------------------
-- 条件：金额=1980 且 已支付 且 product_type 不是 vip/fullbook
UPDATE orders
SET product_type = 'vip'
WHERE amount = 1980
  AND (status = 'paid' OR status = 'completed')
  AND (product_type IS NULL OR product_type = '' OR product_type NOT IN ('vip', 'fullbook'));

-- 4. 兼容大小写：若 product_type 为 'VIP'、'Vip' 等，统一为小写
-- --------------------------------------------------------
UPDATE orders
SET product_type = 'vip'
WHERE amount = 1980
  AND (status = 'paid' OR status = 'completed')
  AND LOWER(TRIM(product_type)) = 'vip'
  AND product_type != 'vip';

-- 5. 验证：修复后应无遗漏
-- --------------------------------------------------------
SELECT id, order_sn, user_id, product_type, amount, status
FROM orders
WHERE amount = 1980
  AND (status = 'paid' OR status = 'completed')
  AND product_type NOT IN ('vip', 'fullbook');
-- 期望结果：0 行

-- ============================================================
-- 可选：若线上 next-project 用 users 表存 is_vip，需确保字段存在
-- 执行前请确认 users 表是否已有这些列！
-- ============================================================

-- 6. 检查 users 表是否有 VIP 相关列（MySQL）
-- SHOW COLUMNS FROM users LIKE 'is_vip';
-- SHOW COLUMNS FROM users LIKE 'vip_expire_date';

-- 7. 若 users 表无 VIP 列，可执行以下 ALTER（按需取消注释）
-- --------------------------------------------------------
-- ALTER TABLE users ADD COLUMN is_vip TINYINT(1) DEFAULT 0;
-- ALTER TABLE users ADD COLUMN vip_expire_date DATETIME NULL;
-- ALTER TABLE users ADD COLUMN vip_name VARCHAR(100) NULL;
-- ALTER TABLE users ADD COLUMN vip_avatar VARCHAR(500) NULL;
-- ALTER TABLE users ADD COLUMN vip_project VARCHAR(200) NULL;
-- ALTER TABLE users ADD COLUMN vip_contact VARCHAR(100) NULL;
-- ALTER TABLE users ADD COLUMN vip_bio TEXT NULL;

-- 8. 从 orders 同步到 users（仅当用 users 表存 VIP 时）
-- 将 1980 元已支付订单对应的用户标记为 VIP，过期日 = pay_time + 365 天
-- --------------------------------------------------------
-- UPDATE users u
-- INNER JOIN (
--   SELECT user_id, MAX(pay_time) AS last_pay
--   FROM orders
--   WHERE amount = 1980
--     AND (status = 'paid' OR status = 'completed')
--     AND product_type IN ('vip', 'fullbook')
--   GROUP BY user_id
-- ) o ON u.id = o.user_id
-- SET u.is_vip = 1,
--     u.vip_expire_date = DATE_ADD(o.last_pay, INTERVAL 365 DAY);
