-- ==================== 检查学生数据 ====================

-- 1. 检查student表中是否有数据
SELECT 
    id,
    user_id,
    student_name,
    gender,
    birth_date,
    grade,
    school,
    deleted,
    create_time
FROM student
ORDER BY id
LIMIT 20;

-- 2. 检查是否有deleted=0的学生
SELECT COUNT(*) as total_students
FROM student
WHERE deleted = 0;

-- 3. 检查user_id=1对应的学生
SELECT 
    id,
    user_id,
    student_name,
    gender,
    deleted
FROM student
WHERE user_id = 1;

-- 4. 如果没有学生数据,插入测试学生
INSERT INTO student (
    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,                          -- 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
);

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

-- 6. 更新成长记录的student_id
-- 如果成长记录中的student_id不正确,需要更新
UPDATE growth_record
SET student_id = (SELECT id FROM student WHERE user_id = 1 LIMIT 1)
WHERE student_id = 1
  AND NOT EXISTS (SELECT 1 FROM student WHERE id = 1);

-- 7. 验证成长记录
SELECT 
    gr.id,
    gr.student_id,
    gr.student_name,
    s.id as actual_student_id,
    s.student_name as actual_student_name
FROM growth_record gr
LEFT JOIN student s ON gr.student_id = s.id
WHERE gr.deleted = 0
LIMIT 10;
