-- 测试手术表的插入，验证 apply_doctor_name 和 apply_dept_name 字段
-- 执行时间：2025-01-05

-- 1. 先检查字段是否存在
SELECT 
    column_name, 
    data_type, 
    character_maximum_length,
    is_nullable
FROM information_schema.columns 
WHERE table_name = 'cli_surgery' 
AND column_name IN ('apply_doctor_id', 'apply_doctor_name', 'apply_dept_id', 'apply_dept_name')
ORDER BY column_name;

-- 2. 插入测试数据（如果字段存在）
INSERT INTO cli_surgery (
    surgery_no,
    patient_id,
    encounter_id,
    apply_doctor_id,
    apply_doctor_name,
    apply_dept_id,
    apply_dept_name,
    surgery_name,
    status_enum,
    delete_flag,
    create_time,
    update_time
) VALUES (
    'TEST202501050001',
    (SELECT id FROM public.adm_patient WHERE delete_flag = '0' LIMIT 1),
    (SELECT id FROM public.adm_encounter WHERE delete_flag = '0' LIMIT 1),
    1,
    '测试医生',
    1,
    '测试科室',
    '测试手术',
    0,
    '0',
    NOW(),
    NOW()
)
ON CONFLICT DO NOTHING;

-- 3. 查询刚才插入的测试数据
SELECT 
    id,
    surgery_no,
    apply_doctor_id,
    apply_doctor_name,
    apply_dept_id,
    apply_dept_name,
    surgery_name
FROM public.cli_surgery
WHERE surgery_no = 'TEST202501050001';

-- 4. 清理测试数据（如果测试成功）
-- DELETE FROM public.cli_surgery WHERE surgery_no = 'TEST202501050001';
