-- 诊断：门诊划价检索不出诊疗项目的问题
-- 问题描述：在门诊划价页面，检索不出诊疗项目

-- 1. 检查是否有诊疗项目定义数据
SELECT 
    COUNT(*) as total_count,
    COUNT(CASE WHEN delete_flag = '0' THEN 1 END) as active_count,
    COUNT(CASE WHEN delete_flag != '0' THEN 1 END) as deleted_count
FROM wor_activity_definition;

-- 2. 检查是否有收费项目（诊疗项目类型）
SELECT 
    T1.id,
    T1.encounter_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.context_enum = 'ACTIVITY'  -- 诊疗项目类型
    AND T1.product_id = T2.id
    AND T2.delete_flag = '0'
WHERE T1.context_enum = 'ACTIVITY'
    AND T1.delete_flag = '0'
LIMIT 20;

-- 3. 检查是否有诊疗项目定义，但收费项目中的product_id无法匹配
SELECT 
    '诊疗项目定义存在，但收费项目无法匹配' as issue_type,
    COUNT(*) as count
FROM wor_activity_definition wad
WHERE wad.delete_flag = '0'
    AND NOT EXISTS (
        SELECT 1 
        FROM adm_charge_item aci
        WHERE aci.context_enum = 'ACTIVITY'
            AND aci.product_id = wad.id
            AND aci.delete_flag = '0'
    );

-- 4. 检查收费项目中的诊疗项目，但定义表中没有对应数据
SELECT 
    '收费项目存在，但诊疗项目定义缺失' as issue_type,
    COUNT(*) as count
FROM adm_charge_item aci
WHERE aci.context_enum = 'ACTIVITY'
    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'
    );

-- 5. 检查某个具体就诊的诊疗项目（替换encounterId为实际值）
-- SELECT 
--     T1.encounter_id,
--     T1.id as charge_item_id,
--     T1.context_enum,
--     T1.product_id,
--     T1.status_enum,
--     T2.id as activity_def_id,
--     T2.name as activity_name,
--     T2.delete_flag as activity_delete_flag,
--     CASE 
--         WHEN T2.id IS NULL THEN '诊疗项目定义不存在或已删除'
--         WHEN T2.delete_flag != '0' THEN '诊疗项目定义已删除'
--         ELSE '正常'
--     END as status
-- FROM adm_charge_item AS T1
-- LEFT JOIN wor_activity_definition AS T2
--     ON T1.context_enum = 'ACTIVITY'
--     AND T1.product_id = T2.id
-- WHERE T1.encounter_id = :encounterId  -- 替换为实际的encounterId
--     AND T1.context_enum = 'ACTIVITY'
--     AND T1.delete_flag = '0'
--     AND T1.status_enum IN (1, 2, 3, 4, 5, 6);  -- PLANNED, BILLABLE, BILLED, REFUNDING, REFUNDED, PART_REFUND





















