<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.openhis.web.reportmanage.mapper.ReportStatisticsMapper">
    <!--月报明细-->
    <select id="getStaffStat"
            resultType="com.openhis.web.reportmanage.dto.DailyReportStatisticsPageDto$StaffStatDTO">
        SELECT COUNT(DISTINCT T1.id)  AS total_medical_staff,--卫生技术人员总数
               SUM(CASE WHEN EXISTS (
                   SELECT 1 FROM adm_practitioner_role T2
                   WHERE T2.practitioner_id = T1.id
                     AND T2.role_code = #{doctor}
                     AND T2.delete_flag = '0'
               ) THEN 1 ELSE 0 END)  AS total_practicing_doctor,--执业（助理）医师总数
                    --  AS tcm_practicing_doctor,-- 中医执业（助理）医师数
               SUM(CASE WHEN EXISTS (
                   SELECT 1 FROM adm_practitioner_role T2
                   WHERE T2.practitioner_id = T1.id
                     AND T2.role_code = #{nurse}
                     AND T2.delete_flag = '0'
               ) THEN 1 ELSE 0 END)   AS registered_nurse--注册护士数
        FROM adm_practitioner T1
        WHERE T1.delete_flag = '0'
          AND T1.name != '超级管理员'
    </select>
    <select id="getBedStat"
            resultType="com.openhis.web.reportmanage.dto.DailyReportStatisticsPageDto$BedStatDTO">
        SELECT COUNT(DISTINCT T1.id) AS actual_bedCount --实有床位（单位：张）
        --             AS total_open_bed_days,--实际开放总床日数
--             AS total_occupied_bed_days,--实际占用总床日数
--             AS discharged_bed_days                      --出院者占用总床日数
        FROM adm_location T1
        WHERE T1.form_enum = #{bed}
          AND T1.delete_flag = '0'
    </select>
    <select id="getRevenueStat"
            resultType="com.openhis.web.reportmanage.dto.DailyReportStatisticsPageDto$RevenueStatDTO">
        SELECT
--                   AS total_revenue,--总收入
--                   AS total_medical_revenue,--医疗收入合计
--                   AS total_outpatient_revenue,--门急诊收入合计
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{registrationFee} THEN T1.total_price ELSE 0 END)        AS outpatient_registration_revenue,--挂号收入
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{diagnosticFee} THEN T1.total_price ELSE 0 END)            AS outpatient_consultation_revenue,--诊察收入（门急诊）
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{checkFee} THEN T1.total_price ELSE 0 END)           AS outpatient_inspection_revenue,--检查收入（门急诊）
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{diagnosticTestFee} THEN T1.total_price ELSE 0 END)           AS outpatient_lab_test_revenue,--化验收入（门急诊）
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{medicalExpenseFee} THEN T1.total_price ELSE 0 END)           AS outpatient_treatment_revenue,--治疗收入（门急诊）
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{operationFee} THEN T1.total_price ELSE 0 END)           AS outpatient_surgery_revenue,--手术收入（门急诊）
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{sanitaryMaterialsFee} THEN T1.total_price ELSE 0 END)          AS outpatient_medical_material_revenue,--卫生材料收入（门急诊）
                              -- AS outpatient_total_drug_revenue,-- 药品收入合计
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{westMedicine} THEN T1.total_price ELSE 0 END)           AS outpatient_total_western_drug_revenue,--西药收入合计
             0    AS outpatient_vaccine_revenue,--疫苗收入（西药下）
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{chineseMedicineFee} THEN T1.total_price ELSE 0 END)         AS outpatient_chinese_patent_drug_revenue,--中成药收入
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{chineseMedicineSlicesFee} THEN T1.total_price ELSE 0 END)         AS outpatient_chinese_herb_revenue,--中药饮片收入
             SUM(CASE WHEN T3.class_enum = #{amb} AND T2.yb_type = #{otherFee} THEN T1.total_price ELSE 0 END)         AS other_outpatient_revenue,--其他门诊收入
--                   AS total_inpatient_revenue,--住院收入合计
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{bedFee} THEN T1.total_price ELSE 0 END)         AS inpatient_bed_revenue,--床位收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{diagnosticFee} THEN T1.total_price ELSE 0 END)    AS inpatient_consultation_revenue,--诊察收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{checkFee} THEN T1.total_price ELSE 0 END)    AS inpatient_inspection_revenue,--检查收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{diagnosticTestFee} THEN T1.total_price ELSE 0 END)    AS inpatient_lab_test_revenue,--化验收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{medicalExpenseFee} THEN T1.total_price ELSE 0 END)    AS inpatient_treatment_revenue,--治疗收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{operationFee} THEN T1.total_price ELSE 0 END)    AS inpatient_surgery_revenue,--手术收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{nursingFee} THEN T1.total_price ELSE 0 END)    AS inpatient_nursing_revenue,--护理收入（住院）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{sanitaryMaterialsFee} THEN T1.total_price ELSE 0 END)    AS inpatient_medical_material_revenue,--卫生材料收入（住院）
             --  AS inpatient_total_drug_revenue,--药品收入合计
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{westMedicine} THEN T1.total_price ELSE 0 END)    AS inpatient_total_western_drug_revenue,--西药收入合计
             0    AS inpatient_vaccine_revenue,--疫苗收入（西药下）
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{chineseMedicineFee} THEN T1.total_price ELSE 0 END)      AS inpatient_chinese_patent_drug_revenue,--中成药收入
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{chineseMedicineSlicesFee} THEN T1.total_price ELSE 0 END)      AS inpatient_chinese_herb_revenue,--中药饮片收入
             SUM(CASE WHEN T3.class_enum = #{imp} AND T2.yb_type = #{otherFee} THEN T1.total_price ELSE 0 END)      AS other_inpatient_revenue,--其他住院收入
             0    AS settlement_difference--结算差额
        FROM adm_charge_item T1
         LEFT JOIN adm_charge_item_definition T2
           ON T1.definition_id = T2.id
            AND T2.delete_flag = '0'
         LEFT JOIN adm_encounter T3
           ON T1.encounter_id = T3.id
            AND T3.delete_flag = '0'
        WHERE T1.status_enum = #{billed}
          AND TO_CHAR(T1.update_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>
    <select id="getTreatmentStat"
            resultType="com.openhis.web.reportmanage.dto.DailyReportStatisticsPageDto$TreatmentStatDTO">
        SELECT -- AS total_treatment_count,--总诊疗人次数
               COUNT(DISTINCT T1.id) AS total_outpatient_emergency_count--门诊和急诊人次数合计
        --                          AS  reserved_outpatient_count,--预约门诊人次数
--                          AS  online_reserved_outpatient_count,--网上预约门诊人次数
--                          AS  general_outpatient_count,--普通门诊人次数
--                          AS  tcm_treatment_count,--中医诊疗人次数
--                          AS  emergency_count,--急诊人次数
--                          AS  fever_clinic_count,--发热门诊人次数
--                          AS  internet_treatment_count,--互联网诊疗服务人次数
--                          AS  telemedicine_count,--远程医疗服务人次数
--                          AS  discharged_count--出院人次数
        FROM adm_encounter T1
        WHERE T1.class_enum = #{amb}
          AND TO_CHAR(T1.reception_time, 'yyyy-MM-dd HH24:mi:ss') BETWEEN #{startTime} AND #{endTime}
          AND T1.delete_flag = '0'
    </select>


</mapper>