-- 修复已存在的手术记录中缺失的名称字段
-- 注意：这只是一个示例，实际执行前请根据您的数据库表结构调整

-- 填充患者姓名
UPDATE public.cli_surgery s
SET patient_name = p.name
FROM public.adm_patient p
WHERE s.patient_id = p.id
AND s.patient_name IS NULL
AND s.delete_flag = '0';

-- 填充主刀医生姓名
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';

-- 填充麻醉医生姓名
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';

-- 填充助手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';

-- 填充助手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';

-- 填充巡回护士姓名
UPDATE public.cli_surgery s
SET scrub_nurse_name = u.nick_name
FROM public.sys_user u
WHERE s.scrub_nurse_id = u.user_id
AND s.scrub_nurse_name IS NULL
AND s.delete_flag = '0';

-- 填充手术室名称
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';

-- 填充执行科室名称
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';

-- 填充申请科室名称
UPDATE public.cli_surgery s
SET apply_dept_name = o.name
FROM public.adm_organization o
WHERE s.apply_dept_id = o.id
AND s.apply_dept_name IS NULL
AND s.delete_flag = '0';

-- 填充申请医生姓名
UPDATE public.cli_surgery s
SET apply_doctor_name = u.nick_name
FROM public.sys_user u
WHERE s.apply_doctor_id = u.user_id
AND s.apply_doctor_name IS NULL
AND s.delete_flag = '0';