-- ============================================
-- 查询流水号相关SQL语句
-- ============================================

-- 1. 查询所有挂号记录的流水号（主键ID）及相关信息
SELECT 
    id AS 流水号,
    display_order AS 就诊序号,
    bus_no AS 就诊编码,
    patient_id AS 患者ID,
    organization_id AS 科室ID,
    create_time AS 挂号时间,
    status_enum AS 状态
FROM adm_encounter
WHERE delete_flag = '0'
ORDER BY create_time DESC;

-- 2. 根据流水号（主键ID）查询某条挂号记录的详细信息
SELECT 
    e.id AS 流水号,
    e.display_order AS 就诊序号,
    e.bus_no AS 就诊编码,
    p.name AS 患者姓名,
    p.id_card AS 身份证号,
    p.phone AS 联系电话,
    o.name AS 科室名称,
    pr.name AS 医生姓名,
    e.create_time AS 挂号时间,
    e.status_enum AS 状态
FROM adm_encounter e
LEFT JOIN adm_patient p ON e.patient_id = p.id AND p.delete_flag = '0'
LEFT JOIN adm_organization o ON e.organization_id = o.id AND o.delete_flag = '0'
LEFT JOIN adm_encounter_participant ep ON e.id = ep.encounter_id AND ep.delete_flag = '0' AND ep.type_code = 'PRF'
LEFT JOIN adm_practitioner pr ON ep.practitioner_id = pr.id AND pr.delete_flag = '0'
WHERE e.id = 1234567890123456789  -- 替换为实际的流水号（主键ID）
  AND e.delete_flag = '0';

-- 3. 根据患者姓名或身份证号查询流水号
SELECT 
    e.id AS 流水号,
    e.display_order AS 就诊序号,
    p.name AS 患者姓名,
    p.id_card AS 身份证号,
    e.create_time AS 挂号时间,
    o.name AS 科室名称
FROM adm_encounter e
LEFT JOIN adm_patient p ON e.patient_id = p.id AND p.delete_flag = '0'
LEFT JOIN adm_organization o ON e.organization_id = o.id AND o.delete_flag = '0'
WHERE e.delete_flag = '0'
  AND (p.name LIKE '%张三%' OR p.id_card = '110101199001011234')  -- 替换为实际的患者姓名或身份证号
ORDER BY e.create_time DESC;

-- 4. 查询当日的挂号记录及流水号
SELECT 
    e.id AS 流水号,
    e.display_order AS 就诊序号,
    e.bus_no AS 就诊编码,
    p.name AS 患者姓名,
    p.id_card AS 身份证号,
    o.name AS 科室名称,
    pr.name AS 医生姓名,
    e.create_time AS 挂号时间,
    e.status_enum AS 状态
FROM adm_encounter e
LEFT JOIN adm_patient p ON e.patient_id = p.id AND p.delete_flag = '0'
LEFT JOIN adm_organization o ON e.organization_id = o.id AND o.delete_flag = '0'
LEFT JOIN adm_encounter_participant ep ON e.id = ep.encounter_id AND ep.delete_flag = '0' AND ep.type_code = 'PRF'
LEFT JOIN adm_practitioner pr ON ep.practitioner_id = pr.id AND pr.delete_flag = '0'
WHERE e.delete_flag = '0'
  AND DATE(e.create_time) = CURRENT_DATE  -- 查询当日
ORDER BY e.display_order ASC, e.create_time ASC;

-- 5. 查询指定日期范围的挂号记录及流水号
SELECT 
    e.id AS 流水号,
    e.display_order AS 就诊序号,
    e.bus_no AS 就诊编码,
    p.name AS 患者姓名,
    p.id_card AS 身份证号,
    o.name AS 科室名称,
    e.create_time AS 挂号时间
FROM adm_encounter e
LEFT JOIN adm_patient p ON e.patient_id = p.id AND p.delete_flag = '0'
LEFT JOIN adm_organization o ON e.organization_id = o.id AND o.delete_flag = '0'
WHERE e.delete_flag = '0'
  AND e.create_time >= '2024-01-01 00:00:00'  -- 开始时间
  AND e.create_time <= '2024-01-31 23:59:59'  -- 结束时间
ORDER BY e.create_time DESC;

-- 6. 查询指定科室的挂号记录及流水号
SELECT 
    e.id AS 流水号,
    e.display_order AS 就诊序号,
    p.name AS 患者姓名,
    o.name AS 科室名称,
    e.create_time AS 挂号时间
FROM adm_encounter e
LEFT JOIN adm_patient p ON e.patient_id = p.id AND p.delete_flag = '0'
LEFT JOIN adm_organization o ON e.organization_id = o.id AND o.delete_flag = '0'
WHERE e.delete_flag = '0'
  AND o.name LIKE '%心内科%'  -- 替换为实际的科室名称
  AND DATE(e.create_time) = CURRENT_DATE
ORDER BY e.display_order ASC;

-- 7. 统计每日挂号数量及流水号范围
SELECT 
    DATE(create_time) AS 日期,
    COUNT(*) AS 挂号数量,
    MIN(id) AS 最小流水号,
    MAX(id) AS 最大流水号,
    MIN(display_order) AS 最小就诊序号,
    MAX(display_order) AS 最大就诊序号
FROM adm_encounter
WHERE delete_flag = '0'
GROUP BY DATE(create_time)
ORDER BY 日期 DESC
LIMIT 30;  -- 最近30天




