-- 检查和填充手术表中的人员字段
-- 执行时间：2025-01-05

-- 1. 查询最近10条手术记录，检查人员字段的填写情况
SELECT 
    id,
    surgery_no,
    main_surgeon_id,
    main_surgeon_name,
    anesthetist_id,
    anesthetist_name,
    assistant_1_id,
    assistant_1_name,
    assistant_2_id,
    assistant_2_name,
    operating_room_id,
    operating_room_name,
    org_id,
    org_name,
    create_time
FROM public.cli_surgery
WHERE delete_flag = '0'
ORDER BY create_time DESC
LIMIT 10;

-- 2. 统计人员字段的填写情况
SELECT 
    COUNT(*) as total_count,
    COUNT(main_surgeon_name) as has_main_surgeon_name_count,
    COUNT(anesthetist_name) as has_anesthetist_name_count,
    COUNT(assistant_1_name) as has_assistant_1_name_count,
    COUNT(assistant_2_name) as has_assistant_2_name_count,
    COUNT(operating_room_name) as has_operating_room_name_count,
    COUNT(org_name) as has_org_name_count,
    -- 计算填写率
    ROUND(COUNT(main_surgeon_name) * 100.0 / COUNT(*), 2) as main_surgeon_name_fill_rate,
    ROUND(COUNT(anesthetist_name) * 100.0 / COUNT(*), 2) as anesthetist_name_fill_rate,
    ROUND(COUNT(assistant_1_name) * 100.0 / COUNT(*), 2) as assistant_1_name_fill_rate,
    ROUND(COUNT(assistant_2_name) * 100.0 / COUNT(*), 2) as assistant_2_name_fill_rate,
    ROUND(COUNT(operating_room_name) * 100.0 / COUNT(*), 2) as operating_room_name_fill_rate,
    ROUND(COUNT(org_name) * 100.0 / COUNT(*), 2) as org_name_fill_rate
FROM public.cli_surgery
WHERE delete_flag = '0';

-- 3. 根据 ID 查询医生表，填充主刀医生姓名
-- 注意：这只是一个示例，实际需要根据您的医生表结构调整
UPDATE public.cli_surgery s
SET main_surgeon_name = u.nick_name
FROM public.sys_user u
WHERE s.main_surgeon_id = u.user_id
AND s.main_surgeon_name IS NULL
AND s.delete_flag = '0';

-- 4. 根据 ID 查询医生表，填充麻醉医生姓名
UPDATE public.cli_surgery s
SET anesthetist_name = u.nick_name
FROM public.sys_user u
WHERE s.anesthetist_id = u.user_id
AND s.anesthetist_name IS NULL
AND s.delete_flag = '0';

-- 5. 根据 ID 查询医生表，填充助手1姓名
UPDATE public.cli_surgery s
SET assistant_1_name = u.nick_name
FROM public.sys_user u
WHERE s.assistant_1_id = u.user_id
AND s.assistant_1_name IS NULL
AND s.delete_flag = '0';

-- 6. 根据 ID 查询医生表，填充助手2姓名
UPDATE public.cli_surgery s
SET assistant_2_name = u.nick_name
FROM public.sys_user u
WHERE s.assistant_2_id = u.user_id
AND s.assistant_2_name IS NULL
AND s.delete_flag = '0';

-- 7. 根据 ID 查询手术室表，填充手术室名称
UPDATE public.cli_surgery s
SET operating_room_name = r.name
FROM public.cli_operating_room r
WHERE s.operating_room_id = r.id
AND s.operating_room_name IS NULL
AND s.delete_flag = '0';

-- 8. 根据 ID 查询机构表，填充执行科室名称
UPDATE public.cli_surgery s
SET org_name = o.name
FROM public.adm_organization o
WHERE s.org_id = o.id
AND s.org_name IS NULL
AND s.delete_flag = '0';

-- 9. 再次查询，验证更新结果
SELECT 
    id,
    surgery_no,
    main_surgeon_id,
    main_surgeon_name,
    anesthetist_id,
    anesthetist_name,
    assistant_1_id,
    assistant_1_name,
    assistant_2_id,
    assistant_2_name,
    operating_room_id,
    operating_room_name,
    org_id,
    org_name
FROM public.cli_surgery
WHERE delete_flag = '0'
ORDER BY create_time DESC
LIMIT 10;
