-- ============================================
-- 诊疗项目数据检查SQL语句
-- ============================================

-- 1. 查询所有诊疗项目（基础查询）
SELECT 
    T1.id,
    T1.bus_no,
    T1.name AS advice_name,
    T1.py_str,
    T1.wb_str,
    T1.status_enum,
    T1.pricing_flag,
    T1.delete_flag,
    T1.org_id,
    T1.category_code,
    T1.type_enum AS activity_type
FROM wor_activity_definition AS T1
WHERE T1.delete_flag = '0'
ORDER BY T1.id;

-- 2. 查询状态为ACTIVE的诊疗项目（status_enum = 1）
SELECT 
    T1.id,
    T1.bus_no,
    T1.name AS advice_name,
    T1.status_enum,
    T1.pricing_flag,
    T1.org_id
FROM wor_activity_definition AS T1
WHERE T1.delete_flag = '0'
  AND T1.status_enum = 1
ORDER BY T1.id;

-- 3. 查询有划价标记的诊疗项目（pricing_flag = 1）
SELECT 
    T1.id,
    T1.bus_no,
    T1.name AS advice_name,
    T1.pricing_flag,
    T1.status_enum
FROM wor_activity_definition AS T1
WHERE T1.delete_flag = '0'
  AND T1.status_enum = 1
  AND (T1.pricing_flag = 1 OR T1.pricing_flag IS NULL)
ORDER BY T1.id;

-- 4. 查询关联了费用定价表的诊疗项目
SELECT 
    T1.id,
    T1.bus_no,
    T1.name AS advice_name,
    T2.id AS charge_item_definition_id,
    T2.instance_table,
    T2.status_enum AS charge_status
FROM wor_activity_definition AS T1
LEFT JOIN adm_charge_item_definition AS T2
    ON T2.instance_id = T1.ID
    AND T2.delete_flag = '0' 
    AND T2.status_enum = 1
    AND T2.instance_table = 'wor_activity_definition'
WHERE T1.delete_flag = '0'
  AND T1.status_enum = 1
ORDER BY T1.id;

-- 5. 完整查询（模拟后端实际执行的SQL，用于门诊划价）
-- 注意：需要替换 #{statusEnum} 为实际值（通常是 1），#{activityTableName} 为 'wor_activity_definition'
SELECT DISTINCT ON (T1.ID)
    T1.tenant_id,
    3 AS advice_type,
    T1.bus_no AS bus_no,
    T1.category_code AS category_code,
    '' AS pharmacology_category_code,
    1 AS part_percent,
    0 AS unit_conversion_ratio,
    null AS part_attribute_enum,
    null AS tho_part_attribute_enum,
    null AS skin_test_flag,
    null AS inject_flag,
    T1.ID AS advice_definition_id,
    T1.NAME AS advice_name,
    T1.bus_no AS advice_bus_no,
    T1.py_str AS py_str,
    T1.wb_str AS wb_str,
    T1.yb_no AS yb_no,
    '' AS product_name,
    T1.type_enum AS activity_type,
    '' AS unit_code,
    '' AS min_unit_code,
    '' AS volume,
    '' AS method_code,
    '' AS rate_code,
    T1.org_id AS org_id,
    T1.location_id AS location_id,
    '' AS dose,
    '' AS dose_unit_code,
    '' AS supplier,
    null AS supplier_id,
    '' AS manufacturer,
    T2.ID AS charge_item_definition_id,
    T2.instance_table AS advice_table_name,
    T3.organization_id AS position_id,
    0 AS restricted_flag,
    '' AS restricted_scope,
    '' AS dosage_instruction,
    T1.chrgitm_lv as chrgitm_lv
FROM wor_activity_definition AS T1
LEFT JOIN adm_charge_item_definition AS T2
    ON T2.instance_id = T1.ID
    AND T2.delete_flag = '0' 
    AND T2.status_enum = 1
    AND T2.instance_table = 'wor_activity_definition'
LEFT JOIN adm_organization_location AS T3 
    ON T3.activity_definition_id = T1.ID
    AND T3.delete_flag = '0' 
    AND (CURRENT_TIME :: time (6) BETWEEN T3.start_time AND T3.end_time)
WHERE T1.delete_flag = '0'
  AND (T1.pricing_flag = 1 OR T1.pricing_flag IS NULL)
  AND T1.status_enum = 1
ORDER BY T1.ID;

-- 6. 统计查询 - 查看各状态的数量
SELECT 
    COUNT(*) AS total_count,
    COUNT(CASE WHEN status_enum = 1 THEN 1 END) AS active_count,
    COUNT(CASE WHEN pricing_flag = 1 THEN 1 END) AS pricing_flag_count,
    COUNT(CASE WHEN pricing_flag IS NULL THEN 1 END) AS pricing_flag_null_count
FROM wor_activity_definition
WHERE delete_flag = '0';

-- 7. 检查是否有费用定价关联
SELECT 
    COUNT(DISTINCT T1.id) AS activity_count,
    COUNT(DISTINCT T2.id) AS charge_item_count,
    COUNT(DISTINCT CASE WHEN T2.id IS NOT NULL THEN T1.id END) AS linked_count
FROM wor_activity_definition AS T1
LEFT JOIN adm_charge_item_definition AS T2
    ON T2.instance_id = T1.ID
    AND T2.delete_flag = '0' 
    AND T2.status_enum = 1
    AND T2.instance_table = 'wor_activity_definition'
WHERE T1.delete_flag = '0'
  AND T1.status_enum = 1;

-- 8. 按科室查询诊疗项目（如果前端传了organizationId）
-- 注意：需要替换 #{organizationId} 为实际的科室ID
-- SELECT DISTINCT ON (T1.ID)
--     T1.id,
--     T1.name AS advice_name,
--     T1.org_id,
--     T2.id AS charge_item_definition_id
-- FROM wor_activity_definition AS T1
-- LEFT JOIN adm_charge_item_definition AS T2
--     ON T2.instance_id = T1.ID
--     AND T2.delete_flag = '0' 
--     AND T2.status_enum = 1
--     AND T2.instance_table = 'wor_activity_definition'
-- WHERE T1.delete_flag = '0'
--   AND (T1.pricing_flag = 1 OR T1.pricing_flag IS NULL)
--   AND T1.status_enum = 1
--   AND T1.org_id = #{organizationId}  -- 替换为实际科室ID
-- ORDER BY T1.ID;

-- 9. 模糊查询（模拟前端搜索）
-- 注意：需要替换 #{searchKey} 为实际的搜索关键字
-- SELECT DISTINCT ON (T1.ID)
--     T1.id,
--     T1.name AS advice_name,
--     T1.py_str,
--     T1.wb_str
-- FROM wor_activity_definition AS T1
-- LEFT JOIN adm_charge_item_definition AS T2
--     ON T2.instance_id = T1.ID
--     AND T2.delete_flag = '0' 
--     AND T2.status_enum = 1
--     AND T2.instance_table = 'wor_activity_definition'
-- WHERE T1.delete_flag = '0'
--   AND (T1.pricing_flag = 1 OR T1.pricing_flag IS NULL)
--   AND T1.status_enum = 1
--   AND (
--       T1.advice_name LIKE '%' || #{searchKey} || '%' 
--       OR T1.py_str LIKE '%' || #{searchKey} || '%' 
--       OR T1.wb_str LIKE '%' || #{searchKey} || '%'
--   )
-- ORDER BY T1.ID;




