-- ============================================
-- 查询今日门诊患者数据（用于调试分诊排队页面）
-- 简化版：直接查询，不考虑登录和租户
-- ============================================

-- 1. 最简单查询：查看今天所有的挂号记录（不关联患者表）
SELECT 
    enc.id AS encounterId,
    enc.patient_id AS patientId,
    enc.start_time AS registerTime,
    enc.status_enum AS statusEnum,
    enc.organization_id AS departmentId,
    enc.tenant_id AS tenantId,
    enc.delete_flag AS deleteFlag,
    enc.bus_no AS encounterBusNo
FROM adm_encounter enc
WHERE enc.delete_flag = '0'
  AND enc.start_time::DATE = CURRENT_DATE
ORDER BY enc.start_time DESC
LIMIT 100;

-- ============================================
-- 2. 完整查询：直接查询患者信息（简化版，不考虑租户）
-- ============================================
SELECT 
    enc.id AS encounterId,
    enc.patient_id AS patientId,
    pt.name AS patientName,
    pt.gender_enum AS genderEnum,
    pt.birth_date AS birthDate,
    pt.id_card AS idCard,
    pt.phone AS phone,
    enc.bus_no AS encounterBusNo,
    enc.start_time AS registerTime,
    enc.reception_time AS receptionTime,
    enc.status_enum AS statusEnum,
    enc.subject_status_enum AS subjectStatusEnum,
    CASE 
        WHEN enc.reception_time IS NOT NULL 
        THEN EXTRACT(EPOCH FROM (enc.reception_time - enc.start_time)) / 60 
        ELSE EXTRACT(EPOCH FROM (NOW() - enc.start_time)) / 60 
    END AS waitingDuration,
    CASE 
        WHEN enc.end_time IS NOT NULL AND enc.reception_time IS NOT NULL 
        THEN EXTRACT(EPOCH FROM (enc.end_time - enc.reception_time)) / 60 
        ELSE NULL 
    END AS visitDuration,
    pt.type_code AS typeCode,
    enc.important_flag AS importantFlag,
    -- 医生信息（左关联）
    ep.practitioner_id AS doctorId
FROM adm_encounter enc
INNER JOIN adm_patient pt 
    ON enc.patient_id = pt.id 
    AND pt.delete_flag = '0'
LEFT JOIN adm_encounter_participant ep 
    ON enc.id = ep.encounter_id 
    AND ep.type_code = 'admitter' 
    AND ep.delete_flag = '0'
WHERE enc.delete_flag = '0'
  AND enc.start_time::DATE = CURRENT_DATE
ORDER BY enc.start_time DESC
LIMIT 10000;

-- ============================================
-- 3. 检查数据条件：查看可能影响查询结果的数据
-- ============================================

-- 3.1 检查今天的挂号记录总数
SELECT 
    COUNT(*) AS total_count,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) AS not_deleted_count,
    COUNT(CASE WHEN delete_flag = '1' THEN 1 END) AS deleted_count
FROM adm_encounter
WHERE start_time::DATE = CURRENT_DATE;

-- 3.2 检查不同租户的数据分布
SELECT 
    tenant_id,
    COUNT(*) AS count,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) AS not_deleted_count
FROM adm_encounter
WHERE start_time::DATE = CURRENT_DATE
GROUP BY tenant_id
ORDER BY tenant_id;

-- 3.3 检查不同状态的数据分布
SELECT 
    status_enum,
    COUNT(*) AS count,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) AS not_deleted_count
FROM adm_encounter
WHERE start_time::DATE = CURRENT_DATE
  AND delete_flag = '0'
GROUP BY status_enum
ORDER BY status_enum;

-- 3.4 检查是否有患者数据关联
SELECT 
    COUNT(DISTINCT enc.id) AS encounter_count,
    COUNT(DISTINCT pt.id) AS patient_count,
    COUNT(DISTINCT CASE WHEN pt.delete_flag = '0' THEN pt.id END) AS patient_not_deleted_count
FROM adm_encounter enc
LEFT JOIN adm_patient pt ON enc.patient_id = pt.id
WHERE enc.start_time::DATE = CURRENT_DATE
  AND enc.delete_flag = '0';

-- 3.5 检查医生参与信息
SELECT 
    COUNT(DISTINCT enc.id) AS encounter_count,
    COUNT(DISTINCT ep.encounter_id) AS has_doctor_count,
    COUNT(DISTINCT ep.practitioner_id) AS doctor_count
FROM adm_encounter enc
LEFT JOIN adm_encounter_participant ep 
    ON enc.id = ep.encounter_id 
    AND ep.type_code = 'admitter' 
    AND ep.delete_flag = '0'
WHERE enc.start_time::DATE = CURRENT_DATE
  AND enc.delete_flag = '0';

-- ============================================
-- 4. 快速查询：直接查看患者姓名和挂号信息（最简单）
-- ============================================
SELECT 
    enc.id AS 就诊记录ID,
    pt.name AS 患者姓名,
    pt.id_card AS 身份证号,
    pt.phone AS 联系电话,
    enc.start_time AS 挂号时间,
    enc.create_time AS 创建时间,
    enc.status_enum AS 状态,
    enc.bus_no AS 就诊流水号
FROM adm_encounter enc
INNER JOIN adm_patient pt ON enc.patient_id = pt.id
WHERE enc.delete_flag = '0'
  AND pt.delete_flag = '0'
  AND enc.start_time::DATE = CURRENT_DATE
ORDER BY enc.start_time DESC;

-- ============================================
-- 4.1 使用 create_time 查询（和门诊挂号页面一致）
-- ============================================
SELECT 
    enc.id AS 就诊记录ID,
    pt.name AS 患者姓名,
    pt.id_card AS 身份证号,
    pt.phone AS 联系电话,
    enc.create_time AS 创建时间,
    enc.start_time AS 开始时间,
    enc.status_enum AS 状态,
    enc.bus_no AS 就诊流水号
FROM adm_encounter enc
INNER JOIN adm_patient pt ON enc.patient_id = pt.id
WHERE enc.delete_flag = '0'
  AND pt.delete_flag = '0'
  AND enc.create_time::DATE = CURRENT_DATE
ORDER BY enc.create_time DESC;

-- ============================================
-- 4.2 完全模拟门诊挂号页面的查询（包含支付状态）
-- ============================================
SELECT 
    enc.id AS 就诊记录ID,
    pt.name AS 患者姓名,
    pt.id_card AS 身份证号,
    pt.phone AS 联系电话,
    enc.create_time AS 挂号时间,
    enc.status_enum AS 状态,
    enc.bus_no AS 就诊流水号,
    ci.total_price AS 挂号金额,
    pr.status_enum AS 支付状态
FROM adm_encounter enc
INNER JOIN adm_patient pt ON enc.patient_id = pt.id AND pt.delete_flag = '0'
LEFT JOIN adm_charge_item ci ON enc.id = ci.encounter_id AND ci.delete_flag = '0' AND ci.context_enum = 1
LEFT JOIN fin_payment_reconciliation pr ON ci.id::TEXT = ANY(string_to_array(pr.charge_item_ids,','))
    AND pr.delete_flag = '0'
    AND pr.status_enum = 1
WHERE enc.delete_flag = '0'
  AND enc.class_enum = 1  -- 门诊
  AND enc.create_time::DATE = CURRENT_DATE
ORDER BY enc.create_time DESC;

-- ============================================
-- 5. 快速检查：查看最近7天的挂号记录数量（使用 start_time）
-- ============================================
SELECT 
    start_time::DATE AS register_date,
    COUNT(*) AS total_count,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) AS not_deleted_count
FROM adm_encounter
WHERE start_time::DATE >= CURRENT_DATE - INTERVAL '7 days'
GROUP BY start_time::DATE
ORDER BY start_time::DATE DESC;

-- ============================================
-- 5.1 快速检查：查看最近7天的挂号记录数量（使用 create_time）
-- ============================================
SELECT 
    create_time::DATE AS register_date,
    COUNT(*) AS total_count,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) AS not_deleted_count
FROM adm_encounter
WHERE create_time::DATE >= CURRENT_DATE - INTERVAL '7 days'
  AND class_enum = 1  -- 门诊
GROUP BY create_time::DATE
ORDER BY create_time::DATE DESC;

-- ============================================
-- 6. 对比 start_time 和 create_time 的差异
-- ============================================
SELECT 
    id AS 就诊记录ID,
    start_time AS 开始时间,
    create_time AS 创建时间,
    (start_time::DATE) AS 开始日期,
    (create_time::DATE) AS 创建日期,
    status_enum AS 状态,
    delete_flag AS 是否删除
FROM adm_encounter
WHERE (start_time::DATE = CURRENT_DATE OR create_time::DATE = CURRENT_DATE)
  AND delete_flag = '0'
ORDER BY create_time DESC
LIMIT 20;

