-- ==================== 创建测试学生数据 ====================
-- 问题: student表中没有id=1的学生记录
-- 解决: 插入测试学生数据

-- 1. 先检查是否已存在
SELECT 
    id,
    user_id,
    student_name,
    deleted
FROM student
WHERE user_id = 1 OR id = 1;

-- 2. 如果不存在,插入测试学生
-- 注意: 如果已存在,这条SQL会报错,可以忽略
INSERT INTO student (
    id,
    user_id,
    student_name,
    gender,
    birth_date,
    grade,
    school,
    class_name,
    parent_name,
    parent_phone,
    parent_relation,
    learning_status,
    status,
    deleted,
    tenant_id,
    create_time,
    update_time
) VALUES (
    1,                          -- id (固定为1)
    1,                          -- user_id (家长ID)
    '小明',                     -- student_name
    1,                          -- gender (1=男)
    '2015-05-15',              -- birth_date
    '小学三年级',               -- grade
    '实验小学',                 -- school
    '三年级1班',                -- class_name
    '张女士',                   -- parent_name
    '13800000001',             -- parent_phone
    '母亲',                     -- parent_relation
    '学习状态良好',             -- learning_status
    1,                          -- status (1=正常)
    0,                          -- deleted (0=未删除)
    1,                          -- tenant_id
    NOW(),                      -- create_time
    NOW()                       -- update_time
)
ON DUPLICATE KEY UPDATE
    deleted = 0,                -- 确保未删除
    student_name = '小明',
    update_time = NOW();

-- 3. 验证插入结果
SELECT 
    id,
    user_id,
    student_name,
    gender,
    birth_date,
    grade,
    school,
    deleted,
    create_time
FROM student
WHERE id = 1;

-- 4. 检查成长记录是否能关联到学生
SELECT 
    gr.id,
    gr.student_id,
    gr.student_name as gr_student_name,
    s.id as student_id_exists,
    s.student_name as s_student_name,
    gr.record_type,
    gr.record_date,
    gr.deleted
FROM growth_record gr
LEFT JOIN student s ON gr.student_id = s.id
WHERE gr.student_id = 1
  AND gr.deleted = 0
ORDER BY gr.record_date DESC
LIMIT 10;

-- 5. 如果成长记录能关联到学生,说明修复成功
SELECT 
    CASE 
        WHEN COUNT(*) > 0 THEN '✅ 修复成功!学生数据已创建,成长记录可以正常查询'
        ELSE '❌ 还有问题,请检查数据'
    END as status
FROM growth_record gr
INNER JOIN student s ON gr.student_id = s.id
WHERE gr.student_id = 1
  AND gr.deleted = 0
  AND s.deleted = 0;
