-- ============================================
-- 改名方案无数据问题 - 一键修复脚本
-- ============================================

-- 第一步：检查当前状态
SELECT '=== 当前数据库状态检查 ===' as step;

-- 1. 检查表是否存在
SELECT 
    '表存在性检查' as check_item,
    CASE WHEN COUNT(*) > 0 THEN '✓ 表存在' ELSE '✗ 表不存在' END as result
FROM information_schema.tables 
WHERE table_name = 'yifan_naming_solutions';

-- 2. 检查数据量
SELECT 
    '数据量检查' as check_item,
    CONCAT('共 ', COUNT(*), ' 条数据') as result
FROM yifan_naming_solutions;

-- 3. 检查启用状态的数据
SELECT 
    '启用数据检查' as check_item,
    CONCAT('启用: ', SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END), ' 条, ',
           '停用: ', SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END), ' 条') as result
FROM yifan_naming_solutions;

-- 4. 检查未删除的数据
SELECT 
    '删除状态检查' as check_item,
    CONCAT('未删除: ', SUM(CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END), ' 条, ',
           '已删除: ', SUM(CASE WHEN is_deleted = 1 THEN 1 ELSE 0 END), ' 条') as result
FROM yifan_naming_solutions;

-- 5. 检查报告表数据
SELECT 
    '报告表检查' as check_item,
    CONCAT('共 ', COUNT(*), ' 条报告') as result
FROM yifan_naming_reports;

-- ============================================
-- 第二步：插入测试数据
-- ============================================

SELECT '=== 开始插入测试数据 ===' as step;

-- 先确保有报告数据
INSERT INTO yifan_naming_reports 
(category, service_type, surname, given_name, gender, birthday, address, user_id, status, is_deleted)
VALUES 
('personal', 'naming', '测试', '用户', 'male', '2024-01-01 10:00:00', '北京市', 1, 0, 0)
ON DUPLICATE KEY UPDATE id=id;

-- 获取报告ID
SET @test_report_id = (SELECT id FROM yifan_naming_reports WHERE surname = '测试' AND given_name = '用户' LIMIT 1);

-- 插入测试方案（如果不存在）
INSERT INTO yifan_naming_solutions 
(report_id, name, pinyin, total_score, star_rating, wuxing, shuxiang, tags, name_meaning, poetry_source, is_recommended, sort_order, status, is_deleted)
SELECT 
    @test_report_id,
    '测试名字一',
    'Ce Shi Ming Zi Yi',
    95,
    5,
    '金木',
    '龙',
    '["吉祥如意","前程似锦"]',
    '这是第一个测试方案，用于验证系统功能',
    '出自测试诗词',
    1,
    1,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM yifan_naming_solutions WHERE name = '测试名字一'
);

INSERT INTO yifan_naming_solutions 
(report_id, name, pinyin, total_score, star_rating, wuxing, shuxiang, tags, name_meaning, poetry_source, is_recommended, sort_order, status, is_deleted)
SELECT 
    @test_report_id,
    '测试名字二',
    'Ce Shi Ming Zi Er',
    92,
    5,
    '水火',
    '龙',
    '["事业有成","财源广进"]',
    '这是第二个测试方案，用于验证列表显示',
    '出自测试典籍',
    0,
    2,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM yifan_naming_solutions WHERE name = '测试名字二'
);

INSERT INTO yifan_naming_solutions 
(report_id, name, pinyin, total_score, star_rating, wuxing, shuxiang, tags, name_meaning, poetry_source, is_recommended, sort_order, status, is_deleted)
SELECT 
    @test_report_id,
    '测试名字三',
    'Ce Shi Ming Zi San',
    88,
    4,
    '土金',
    '龙',
    '["平安顺遂","健康长寿"]',
    '这是第三个测试方案，用于验证分页功能',
    '出自测试经典',
    0,
    3,
    0,
    0
WHERE NOT EXISTS (
    SELECT 1 FROM yifan_naming_solutions WHERE name = '测试名字三'
);

-- ============================================
-- 第三步：验证插入结果
-- ============================================

SELECT '=== 插入后数据验证 ===' as step;

-- 显示所有测试数据
SELECT 
    id,
    report_id,
    name,
    pinyin,
    total_score,
    star_rating,
    CASE status WHEN 0 THEN '启用' ELSE '停用' END as status_text,
    CASE is_deleted WHEN 0 THEN '未删除' ELSE '已删除' END as deleted_text,
    CASE is_recommended WHEN 1 THEN '是' ELSE '否' END as recommended_text
FROM yifan_naming_solutions
WHERE name LIKE '测试%'
ORDER BY sort_order;

-- 统计信息
SELECT 
    '总数据量' as stat_type,
    COUNT(*) as count
FROM yifan_naming_solutions
UNION ALL
SELECT 
    '可查询数据(status=0 AND is_deleted=0)' as stat_type,
    COUNT(*) as count
FROM yifan_naming_solutions
WHERE status = 0 AND is_deleted = 0;

-- ============================================
-- 第四步：提供清理命令（可选）
-- ============================================

SELECT '=== 如需清理测试数据，执行以下命令 ===' as step;
SELECT 'DELETE FROM yifan_naming_solutions WHERE name LIKE "测试%";' as cleanup_sql;

-- ============================================
-- 完成提示
-- ============================================

SELECT '=== 修复完成 ===' as step;
SELECT '请刷新前端页面，应该能看到测试数据了' as message;
SELECT '如果还是看不到，请检查：' as tip;
SELECT '1. 浏览器开发者工具 Network 标签，查看 API 响应' as tip1;
SELECT '2. 后端日志是否有错误' as tip2;
SELECT '3. 用户是否有 module_yifan:yifan_naming_solutions:query 权限' as tip3;
