-- 通过ak_users的名字反查学生所在的部门数组，并映射到班级ID
-- 处理member_user表中的dept_ids数组字段，通过system_dept的name字段进行映射

-- 方案1: 基础查询 - 处理dept_ids数组，通过system_dept.name进行映射
SELECT 
    au.id as ak_user_id,
    au.username as student_name,
    au.email as ak_email,
    mu.dept_ids as original_dept_ids,
    unnest(mu.dept_ids) as dept_id,  -- 展开数组
    sd.name as dept_name,  -- 这是真正的条件字段
    sd.id as dept_table_id,
    mu.name as original_name,
    mu.email as original_email,
    -- 通过system_dept.name字段进行班级ID映射
    CASE 
        WHEN sd.name = '2022001' THEN 'bc333301-78cd-4ef0-a123-456789012345'::uuid
        WHEN sd.name = '2022002' THEN 'bc333302-78cd-4ef0-a123-456789012345'::uuid  
        WHEN sd.name = '2022003' THEN 'bc333303-78cd-4ef0-a123-456789012345'::uuid
        WHEN sd.name = '2022004' THEN 'bc333304-78cd-4ef0-a123-456789012345'::uuid
        WHEN sd.name = '2022005' THEN 'bc333305-78cd-4ef0-a123-456789012345'::uuid
        WHEN sd.name = '2022006' THEN 'bc333306-78cd-4ef0-a123-456789012345'::uuid
        ELSE NULL
    END as mapped_class_id
FROM public.ak_users au
LEFT JOIN public.member_user mu ON au.username = mu.name
LEFT JOIN LATERAL unnest(mu.dept_ids) AS dept_id ON true
LEFT JOIN public.system_dept sd ON dept_id = sd.id
WHERE au.role = 'student'
    AND mu.dept_ids IS NOT NULL
ORDER BY au.username;

-- 方案2: 使用子查询处理数组 - 通过system_dept.name进行映射
WITH dept_expanded AS (
    SELECT 
        au.id as ak_user_id,
        au.username as student_name,
        mu.name as original_name,
        mu.dept_ids as original_dept_ids,
        dept_id,
        sd.name as dept_name,  -- 这是映射的关键字段
        sd.id as dept_table_id
    FROM public.ak_users au
    LEFT JOIN public.member_user mu ON au.username = mu.name
    LEFT JOIN LATERAL unnest(mu.dept_ids) AS dept_id ON true
    LEFT JOIN public.system_dept sd ON dept_id = sd.id
    WHERE au.role = 'student' AND mu.dept_ids IS NOT NULL
)
SELECT 
    ak_user_id,
    student_name,
    original_name,
    array_agg(dept_name) as all_dept_names,  -- 显示所有部门名称
    array_agg(dept_table_id) as all_dept_ids,
    -- 通过dept_name进行班级映射，优先选择第一个匹配的
    CASE 
        WHEN '2022001' = ANY(array_agg(dept_name)) THEN 'bc333301-78cd-4ef0-a123-456789012345'::uuid
        WHEN '2022002' = ANY(array_agg(dept_name)) THEN 'bc333302-78cd-4ef0-a123-456789012345'::uuid  
        WHEN '2022003' = ANY(array_agg(dept_name)) THEN 'bc333303-78cd-4ef0-a123-456789012345'::uuid
        WHEN '2022004' = ANY(array_agg(dept_name)) THEN 'bc333304-78cd-4ef0-a123-456789012345'::uuid
        WHEN '2022005' = ANY(array_agg(dept_name)) THEN 'bc333305-78cd-4ef0-a123-456789012345'::uuid
        WHEN '2022006' = ANY(array_agg(dept_name)) THEN 'bc333306-78cd-4ef0-a123-456789012345'::uuid
        ELSE NULL
    END as mapped_class_id
FROM dept_expanded
GROUP BY ak_user_id, student_name, original_name
ORDER BY student_name;

-- 方案3: 直接更新ak_users表 - 通过system_dept.name进行映射
UPDATE public.ak_users 
SET class_id = dept_mapping.mapped_class_id
FROM (
    WITH dept_expanded AS (
        SELECT 
            au.id as ak_user_id,
            sd.name as dept_name  -- 使用dept_name进行映射
        FROM public.ak_users au
        LEFT JOIN public.member_user mu ON au.username = mu.name
        LEFT JOIN LATERAL unnest(mu.dept_ids) AS dept_id ON true
        LEFT JOIN public.system_dept sd ON dept_id = sd.id
        WHERE au.role = 'student' AND mu.dept_ids IS NOT NULL
    )
    SELECT 
        ak_user_id,
        CASE 
            WHEN dept_name = '2022001' THEN 'bc333301-78cd-4ef0-a123-456789012345'::uuid
            WHEN dept_name = '2022002' THEN 'bc333302-78cd-4ef0-a123-456789012345'::uuid  
            WHEN dept_name = '2022003' THEN 'bc333303-78cd-4ef0-a123-456789012345'::uuid
            WHEN dept_name = '2022004' THEN 'bc333304-78cd-4ef0-a123-456789012345'::uuid
            WHEN dept_name = '2022005' THEN 'bc333305-78cd-4ef0-a123-456789012345'::uuid
            WHEN dept_name = '2022006' THEN 'bc333306-78cd-4ef0-a123-456789012345'::uuid
            ELSE NULL
        END as mapped_class_id
    FROM dept_expanded
    WHERE dept_name IN ('2022001', '2022002', '2022003', '2022004', '2022005', '2022006')
    -- 如果一个学生属于多个相关部门，按优先级选择
    ORDER BY ak_user_id, 
        CASE dept_name 
            WHEN '2022001' THEN 1
            WHEN '2022002' THEN 2  
            WHEN '2022003' THEN 3
            WHEN '2022004' THEN 4
            WHEN '2022005' THEN 5
            WHEN '2022006' THEN 6
            ELSE 999
        END
) dept_mapping
WHERE ak_users.id = dept_mapping.ak_user_id 
    AND dept_mapping.mapped_class_id IS NOT NULL;

-- 方案4: 查看数组字段和部门名称统计
SELECT 
    sd.name as dept_name,
    COUNT(DISTINCT au.id) as student_count,
    array_agg(DISTINCT au.username) as sample_students
FROM public.ak_users au
LEFT JOIN public.member_user mu ON au.username = mu.name
LEFT JOIN LATERAL unnest(mu.dept_ids) AS dept_id ON true
LEFT JOIN public.system_dept sd ON dept_id = sd.id
WHERE au.role = 'student' AND mu.dept_ids IS NOT NULL
GROUP BY sd.name
ORDER BY sd.name;

-- 方案5: 检查包含特定部门名称的学生
SELECT 
    au.username,
    array_agg(sd.name) as dept_names,  -- 显示所有相关的部门名称
    CASE 
        WHEN '2022001' = ANY(array_agg(sd.name)) THEN '初三(1)班'
        WHEN '2022002' = ANY(array_agg(sd.name)) THEN '初三(2)班'
        WHEN '2022003' = ANY(array_agg(sd.name)) THEN '初三(3)班'
        WHEN '2022004' = ANY(array_agg(sd.name)) THEN '初三(4)班'
        WHEN '2022005' = ANY(array_agg(sd.name)) THEN '初三(5)班'
        WHEN '2022006' = ANY(array_agg(sd.name)) THEN '初三(6)班'
        ELSE '未分配班级'
    END as target_class_name
FROM public.ak_users au
LEFT JOIN public.member_user mu ON au.username = mu.name
LEFT JOIN LATERAL unnest(mu.dept_ids) AS dept_id ON true
LEFT JOIN public.system_dept sd ON dept_id = sd.id
WHERE au.role = 'student' 
    AND mu.dept_ids IS NOT NULL
GROUP BY au.username
HAVING 
    '2022001' = ANY(array_agg(sd.name)) OR
    '2022002' = ANY(array_agg(sd.name)) OR
    '2022003' = ANY(array_agg(sd.name)) OR
    '2022004' = ANY(array_agg(sd.name)) OR
    '2022005' = ANY(array_agg(sd.name)) OR
    '2022006' = ANY(array_agg(sd.name))
ORDER BY au.username;
