-- 门诊预约挂号功能测试数据插入脚本
-- 执行前请确保已创建相关表结构

-- 设置当前模式
SET search_path TO hisdev;

-- ====================================
-- 说明：
-- 1. 此脚本主要包含号源表(clinical_ticket)的测试数据
-- 2. 其他相关表(医生、患者、科室等)请确保系统中已有基础数据
-- 3. 如需要完整测试，建议先在系统中创建相应的医生和患者信息
-- ====================================

-- ====================================
-- 注意：以下是可选的表结构验证
-- 如果不确定表结构是否正确，请先执行这些查询
-- ====================================
-- SELECT column_name FROM information_schema.columns WHERE table_name = 'clinical_ticket';
-- SELECT column_name FROM information_schema.columns WHERE table_name = 'adm_practitioner';
-- SELECT column_name FROM information_schema.columns WHERE table_name = 'adm_patient';


-- ====================================
-- 5. 号源数据 (clinical_ticket)
-- 包含不同状态的号源：未预约、已预约、已取号
-- ====================================

-- 获取当前日期和明天日期
DO $$
DECLARE
    current_date DATE := CURRENT_DATE;
    tomorrow_date DATE := CURRENT_DATE + INTERVAL '1 day';
BEGIN
    -- 当前日期号源
    -- 未预约状态的号源（用于测试正常预约流程）
    INSERT INTO clinical_ticket (bus_no, department, doctor, ticket_type, time, status, fee, appointment_date, appointment_time, department_id, doctor_id, create_time, tenant_id, delete_flag)
    SELECT 
        'TICKET' || lpad(i::text, 6, '0'),
        '内科', '张三', '普通', '08:00-' || (7 + i) || ':50', 
        'unbooked', '50', current_date, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0'
    FROM generate_series(1, 10) i
    WHERE NOT EXISTS (SELECT 1 FROM clinical_ticket WHERE bus_no = 'TICKET' || lpad(i::text, 6, '0'));
    
    -- 已预约状态的号源（用于测试取消预约流程）
    INSERT INTO clinical_ticket (bus_no, department, doctor, ticket_type, time, status, fee, patient_id, patient_name, medical_card, phone, appointment_date, appointment_time, department_id, doctor_id, create_time, tenant_id, delete_flag)
    VALUES
    ('TICKET000011', '内科', '张三', '普通', '09:00-09:50', 'booked', '50', 1, '患者A', 'PAT001', '13900139001', current_date, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0'),
    ('TICKET000012', '内科', '张三', '普通', '09:50-10:40', 'booked', '50', 2, '患者B', 'PAT002', '13900139002', current_date, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0');
    
    -- 已取号状态的号源（用于测试逆向用例：取消已取号的预约）
    INSERT INTO clinical_ticket (bus_no, department, doctor, ticket_type, time, status, fee, patient_id, patient_name, medical_card, phone, appointment_date, appointment_time, department_id, doctor_id, create_time, tenant_id, delete_flag)
    VALUES
    ('TICKET000013', '内科', '张三', '普通', '10:40-11:30', 'checked', '50', 3, '患者C', 'PAT003', '13900139003', current_date, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0');
    
    -- 专家号源（用于测试号源类型切换功能）
    INSERT INTO clinical_ticket (bus_no, department, doctor, ticket_type, time, status, fee, appointment_date, appointment_time, department_id, doctor_id, create_time, tenant_id, delete_flag)
    VALUES
    ('TICKET000014', '内科', '张三', '专家', '08:00-08:50', 'unbooked', '150', CURRENT_DATE, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0'),
    ('TICKET000015', '内科', '张三', '专家', '09:00-09:50', 'unbooked', '150', CURRENT_DATE, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0'),
    ('TICKET000016', '内科', '张三', '专家', '10:00-10:50', 'unbooked', '150', CURRENT_DATE, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0'),
    ('TICKET000017', '内科', '张三', '专家', '11:00-11:50', 'unbooked', '150', CURRENT_DATE, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0'),
    ('TICKET000018', '内科', '张三', '专家', '14:00-14:50', 'unbooked', '150', CURRENT_DATE, CURRENT_TIMESTAMP, 1, 1, CURRENT_TIMESTAMP, 1, '0');
    
    -- 明天日期的号源（用于测试日期选择功能）
    INSERT INTO clinical_ticket (bus_no, department, doctor, ticket_type, time, status, fee, appointment_date, appointment_time, department_id, doctor_id, create_time, tenant_id, delete_flag)
    SELECT 
        'TICKET' || lpad((20 + i)::text, 6, '0'),
        '外科', '王五', '普通', '08:00-' || (7 + i) || ':50', 
        'unbooked', '60', tomorrow_date, CURRENT_TIMESTAMP, 2, 3, CURRENT_TIMESTAMP, 1, '0'
    FROM generate_series(1, 10) i
    WHERE NOT EXISTS (SELECT 1 FROM clinical_ticket WHERE bus_no = 'TICKET' || lpad((20 + i)::text, 6, '0'));
END $$;

-- ==================================== 
 -- 6. 预约记录数据 (cli_reservation_record) 
 -- 用于测试预约历史查询 
 -- ==================================== 
 INSERT INTO cli_reservation_record (patient_name, patient_tel, chief_complaint, reservation_time, org_id, practitioner_id, tenant_id, create_time, create_by, update_time, update_by) 
 VALUES 
 ('患者A', '13900139001', '感冒发烧', CURRENT_TIMESTAMP, 1, 1, 1, CURRENT_TIMESTAMP, 'system', CURRENT_TIMESTAMP, 'system'), 
 ('患者B', '13900139002', '腹痛', CURRENT_TIMESTAMP, 1, 1, 1, CURRENT_TIMESTAMP, 'system', CURRENT_TIMESTAMP, 'system');

-- ====================================
-- 数据插入完成提示
-- ====================================
SELECT '门诊预约测试数据插入完成！' AS message;
SELECT '未预约号源数量：' || COUNT(*) FROM clinical_ticket WHERE status = 'unbooked';
SELECT '已预约号源数量：' || COUNT(*) FROM clinical_ticket WHERE status = 'booked';
SELECT '已取号号源数量：' || COUNT(*) FROM clinical_ticket WHERE status = 'checked';