-- 详细检查诊疗项目检索问题
-- 已知：wor_activity_definition 表中有4条数据

-- 1. 检查这4条诊疗项目定义的详细信息
SELECT 
    id,
    name,
    bus_no,
    delete_flag,
    tenant_id,
    create_time
FROM wor_activity_definition
ORDER BY id;

-- 2. 检查收费项目中是否有诊疗项目类型的记录
-- 注意：ChargeItemContext.ACTIVITY.getValue() 返回的是 Integer 3
-- 但数据库中 context_enum 可能是字符串类型，需要检查是 '3' 还是 3
SELECT 
    COUNT(*) as total_activity_charge_items,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) as active_activity_charge_items,
    COUNT(CASE WHEN delete_flag != '0' THEN 1 END) as deleted_activity_charge_items
FROM adm_charge_item
WHERE context_enum::text = '3' OR context_enum = 3;  -- 诊疗项目类型是3

-- 3. 检查收费项目中的product_id是否能匹配到诊疗项目定义
-- 注意：诊疗项目类型的 context_enum = 3（ChargeItemContext.ACTIVITY.getValue()）
SELECT 
    aci.id as charge_item_id,
    aci.encounter_id,
    aci.context_enum,
    aci.product_id as charge_product_id,
    aci.status_enum,
    aci.delete_flag as charge_delete_flag,
    wad.id as activity_def_id,
    wad.name as activity_name,
    wad.delete_flag as activity_delete_flag,
    CASE 
        WHEN wad.id IS NULL THEN '❌ 诊疗项目定义不存在'
        WHEN wad.delete_flag != '0' THEN '❌ 诊疗项目定义已删除'
        WHEN aci.delete_flag != '0' THEN '❌ 收费项目已删除'
        ELSE '✅ 正常'
    END as match_status
FROM adm_charge_item aci
LEFT JOIN wor_activity_definition wad 
    ON aci.product_id = wad.id
WHERE (aci.context_enum::text = '3' OR aci.context_enum = 3)  -- 诊疗项目类型是3
LIMIT 20;

-- 4. 检查context_enum的所有可能值（确认诊疗项目的枚举值是什么）
SELECT DISTINCT 
    context_enum,
    COUNT(*) as count
FROM adm_charge_item
WHERE delete_flag = '0'
GROUP BY context_enum
ORDER BY context_enum;

-- 5. 检查是否有收费项目，但无法匹配到诊疗项目定义
SELECT 
    COUNT(*) as unmatched_count,
    STRING_AGG(DISTINCT CAST(product_id AS VARCHAR), ', ') as unmatched_product_ids,
    STRING_AGG(DISTINCT CAST(id AS VARCHAR), ', ') as unmatched_charge_item_ids
FROM adm_charge_item aci
WHERE (aci.context_enum::text = '3' OR aci.context_enum = 3)  -- 诊疗项目类型是3
    AND aci.delete_flag = '0'
    AND NOT EXISTS (
        SELECT 1 
        FROM wor_activity_definition wad
        WHERE wad.id = aci.product_id
            AND wad.delete_flag = '0'
    );

-- 6. 检查具体某个就诊的诊疗项目（如果有具体的encounterId）
-- SELECT 
--     T1.encounter_id,
--     T1.id as charge_item_id,
--     T1.context_enum,
--     T1.product_id,
--     T1.status_enum,
--     T1.delete_flag,
--     T2.id as activity_def_id,
--     T2.name as activity_name,
--     T2.delete_flag as activity_delete_flag
-- FROM adm_charge_item AS T1
-- LEFT JOIN wor_activity_definition AS T2
--     ON T1.product_id = T2.id
-- WHERE T1.encounter_id = :encounterId  -- 替换为实际的encounterId
--     AND T1.context_enum IN ('ACTIVITY', '1', 'ACTIVITY_CODE')  -- 尝试多种可能的值
--     AND T1.delete_flag = '0';

-- 7. 检查后端代码中使用的context_enum值
-- ChargeItemContext.ACTIVITY.getValue() 的实际返回值需要查看枚举类
-- 可能是：'ACTIVITY', '1', 'ACTIVITY_CODE', 或其他值
-- 建议先运行查询4，查看数据库中实际使用的context_enum值

-- 8. 检查status_enum的值（查询条件中需要匹配的状态）
-- SQL查询中要求 status_enum IN (1, 2, 3, 4, 5, 6)
-- 检查收费项目中的状态值是否在这个范围内
SELECT 
    status_enum,
    COUNT(*) as count,
    CASE 
        WHEN status_enum IN (1, 2, 3, 4, 5, 6) THEN '✅ 在查询范围内'
        ELSE '❌ 不在查询范围内'
    END as status_check
FROM adm_charge_item
WHERE context_enum IN (
    SELECT DISTINCT context_enum 
    FROM adm_charge_item 
    WHERE delete_flag = '0'
    LIMIT 10
)
AND delete_flag = '0'
GROUP BY status_enum
ORDER BY status_enum;

