<?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.diagbot.mapper.MedClickInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.MedClickInfo">
        <id column="id" property="id"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <result column="doctor_id" property="doctorId"/>
        <result column="doctor_name" property="doctorName"/>
        <result column="opt_type" property="optType"/>
        <result column="qcresult_info_id" property="qcresultInfoId"/>
        <result column="behospital_code" property="behospitalCode"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="creator" property="creator"/>
        <result column="modifier" property="modifier"/>
    </resultMap>


    <resultMap id="entryDefectImproveMap" type="com.diagbot.dto.EntryDefectImprove">
        <result column="deptId" property="deptId"/>
        <result column="deptName" property="deptName"/>
        <collection property="behospitalCodeInfos"
                    ofType="com.diagbot.dto.BehospitalCodeInfo">
            <result column="behospitalCode" property="behospitalCode"/>
            <collection property="qcResultDetailInfos" ofType="com.diagbot.dto.QcResultDetailInfo">
                <result column="CasesEntryIds" property="CasesEntryIds"/>
            </collection>
        </collection>
    </resultMap>


    <resultMap id="ExportExcelMap" type="com.diagbot.dto.ExportExcelDTO">
        <result column="deptName" property="behDeptName"/>
        <result column="avgScore" property="avgScore"/>
        <collection property="excelBehospitalDTOS"
                    ofType="com.diagbot.dto.ExportExcelBehospitalDTO">
            <result column="doctorName" property="doctorName"/>
            <result column="NAME" property="patName"/>
            <result column="behospitalCode" property="behospitalCode"/>
            <result column="behospitalDate" property="behospitalDate"/>
            <result column="leaveHospitalDate" property="leaveHospitalDate"/>
            <result column="scoreRes" property="score"/>
            <result column="scoreBn" property="scoreBn"/>
            <collection property="exportExcelCaseDTOS" ofType="com.diagbot.dto.ExportExcelCaseDTO">
                <result column="casesName" property="caseName"/>
                <collection property="exportExcelMsgDTOS"
                            ofType="com.diagbot.dto.ExportExcelMsgDTO">
                    <result column="entryName" property="msg"/>
                </collection>
            </collection>
        </collection>
    </resultMap>


    <resultMap id="entryDefectImproveInnerMap" type="com.diagbot.dto.EntryDefectImproveInner">
        <result column="behDeptName" property="behDeptName"/>
        <collection property="behospitalCodeInfos" ofType="com.diagbot.dto.BehospitalCodeDetail">
            <result column="behospitalCode" property="behospitalCode"/>
            <result column="name" property="name"/>
            <result column="fileCode" property="fileCode"/>
            <result column="behospitalDate" property="behospitalDate"/>
            <result column="leaveHospitalDate" property="leaveHospitalDate"/>
            <result column="doctorName" property="doctorName"/>
            <result column="age" property="age"/>
            <result column="level" property="level"/>
            <result column="scoreRes" property="scoreRes"/>
            <result column="gradeTime" property="gradeTime"/>
            <result column="isDaytime" property="isDaytime"/>
            <collection property="qcResultDetailInfos" ofType="com.diagbot.dto.QcResultDetailInfo">
                <result column="CasesEntryIds" property="CasesEntryIds"/>
            </collection>
        </collection>
    </resultMap>


    <resultMap id="entryDefectImproveInnerExportMap" type="com.diagbot.dto.EntryDefectImproveInnerExport">
        <result column="deptId" property="deptId"/>
        <result column="deptName" property="deptName"/>
        <collection property="entryDefectImproveInnerDoctors" ofType="com.diagbot.dto.EntryDefectImproveInnerDoctor">
            <result column="doctorId" property="doctorId"/>
            <result column="doctorName" property="doctorName"/>
            <collection property="behospitalCodeInfos" ofType="com.diagbot.dto.BehospitalCodeInfo">
                <result column="behospitalCode" property="behospitalCode"/>
                    <collection property="qcResultDetailInfos" ofType="com.diagbot.dto.QcResultDetailInfo">
                        <result column="CasesEntryIds" property="CasesEntryIds"/>
                </collection>
            </collection>
        </collection>
    </resultMap>


    <select id="getQcClick" resultType="com.diagbot.dto.GetQcClickDTO">
        SELECT
        a.dept_id AS deptId,
        a.dept_name AS deptName,
        sum(a.opt_type = 0) AS singleModeNum,
        sum(a.opt_type = 1) AS totalModeNum
        FROM
        med_click_info a
        where
        a.is_deleted = 'N'
        <if test="hospitalId != null">
            and a.hospital_id = #{hospitalId}
        </if>
        <if test="deptName != null and deptName != ''">
            and a.dept_name like CONCAT('%',#{deptName},'%')
        </if>
        <if test="startDate != null">
            <![CDATA[ and a.gmt_create >= #{startDate}]]>
        </if>
        <if test="endDate != null">
            <![CDATA[ and a.gmt_create < #{endDate}]]>
        </if>
        group by
        a.dept_id,
        a.dept_name
        <if test="asc != null and asc !=''">
            order by
            <choose>
                <when test='asc=="singleModeNum"'>singleModeNum asc</when>
                <when test='asc=="totalModeNum"'>totalModeNum asc</when>
            </choose>
        </if>
        <if test="desc != null and desc !=''">
            order by
            <choose>
                <when test='desc=="singleModeNum"'>singleModeNum desc</when>
                <when test='desc=="totalModeNum"'>totalModeNum desc</when>
            </choose>
        </if>
    </select>

    <select id="getQcClickInnerPage" resultType="com.diagbot.dto.GetQcClickInnerPageDTO">
        SELECT
        a.dept_id as deptId,
        a.dept_name as deptName,
        a.doctor_id AS doctorId,
        a.doctor_name AS doctorName,
        count(case when a.opt_type = 0 then 1 end) AS singleModeNum,
        count(case when a.opt_type = 1 then 1 end) AS totalModeNum
        FROM
        med_click_info a
        where
        a.is_deleted = 'N'
        <if test="hospitalId != null">
            and a.hospital_id = #{hospitalId}
        </if>
        <if test="deptName != null and deptName != ''">
            and a.dept_name =#{deptName}
        </if>
        <if test="deptId != null and deptId != ''">
            and a.dept_id= #{deptId}
        </if>
        <if test="doctorId != null and doctorId != ''">
            and a.doctor_id like CONCAT('%',#{doctorId},'%')
        </if>
        <if test="doctorName != null and doctorName != ''">
            and a.doctor_name like CONCAT('%',#{doctorName},'%')
        </if>
        <if test="startDate != null">
            <![CDATA[ and a.gmt_create >= #{startDate}]]>
        </if>
        <if test="endDate != null">
            <![CDATA[ and a.gmt_create < #{endDate}]]>
        </if>
        group by
        a.dept_id,
        a.dept_name,
        a.doctor_id,
        a.doctor_name
    </select>

    <select id="getEntryInfo" resultType="com.diagbot.dto.GetEntryInfoDTO">
        SELECT
            a.id AS casesId,
            a. NAME AS casesName,
            b.id AS entryId,
            b. NAME AS entryName
        FROM
            qc_cases a,
            qc_cases_entry b
        WHERE
            a.is_deleted = 'N'
        AND b.is_deleted = 'N'
        AND a.id = b.cases_id
    </select>

    <select id="getEntryDefectImprove" resultMap="entryDefectImproveMap">
        SELECT
        b.hospital_id as hospitalId,
        b.beh_dept_id as deptId,
        b.beh_dept_name as deptName,
        b.behospital_code as behospitalCode,
        ifnull(a.cases_entry_ids,0) as casesEntryIds
        FROM
        med_qcresult_click a,
        med_behospital_info b
        WHERE
        a.is_deleted = 'N'
        AND b.is_deleted = 'N'
        AND a.hospital_id =b.hospital_id
        AND a.behospital_code =b.behospital_code
        <if test="hospitalId != null and hospitalId != ''">
            AND b.hospital_id = #{hospitalId}
        </if>
        <if test="deptName != null and deptName != ''">
            and b.beh_dept_name like CONCAT('%',#{deptName},'%')
        </if>
        <if test="startDate != null and startDate != ''">
            <![CDATA[ AND a.gmt_create >= #{startDate}]]>
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[ AND a.gmt_create <= #{endDate}]]>
        </if>
        order by a.gmt_create
    </select>

    <select id="getEntryDefectImproveInner" resultMap="entryDefectImproveInnerMap">
        SELECT
        t.*,
        IF( n.age IS NULL,NULL,CONCAT(ifnull(n.age, ''),ifnull(n.age_unit, ''))) AS age
        from (
        SELECT
        b.hospital_id as hospitalId,
        b.beh_dept_id as deptId,
        b.beh_dept_name as behDeptName,
        b.file_code AS fileCode,
        b.behospital_code as behospitalCode,
        b.name,
        b.is_daytime as isDaytime,
        b.behospital_date AS behospitalDate,
        b.leave_hospital_date AS leaveHospitalDate,
        b.doctor_id AS doctorId,
        b.doctor_name AS doctorName,
        ifnull(c. level, '未评分') AS `level`,
        c.score_res AS scoreRes,
        c.gmt_create AS gradeTime,
        ifnull(a.cases_entry_ids,0) as casesEntryIds
        FROM
        med_qcresult_click a,
        med_behospital_info b,
        med_qcresult_info c
        WHERE
        a.is_deleted = 'N'
        AND b.is_deleted = 'N'
        AND c.is_deleted = 'N'
        AND a.hospital_id =b.hospital_id
        AND a.hospital_id =c.hospital_id
        AND a.behospital_code =b.behospital_code
        AND a.behospital_code =c.behospital_code
        <if test="isDaytime != null and isDaytime != ''">
            and b.is_daytime = #{isDaytime}
        </if>
        <if test="hospitalId != null and hospitalId != ''">
            AND b.hospital_id = #{hospitalId}
        </if>
        <if test="deptId != null and deptId != ''">
            and b.beh_dept_id = #{deptId}
        </if>
        <if test="deptName != null and deptName != ''">
            and b.beh_dept_name = #{deptName}
        </if>
        <if test="patName != null and patName != ''">
            and b.name like CONCAT('%',#{patName},'%')
        </if>
        <if test="behospitalCode != null and behospitalCode != ''">
            and b.behospital_code like CONCAT('%',#{behospitalCode},'%')
        </if>
        <if test="doctorId != null and doctorId != ''">
            and b.doctor_id like CONCAT('%',#{doctorId},'%')
        </if>
        <if test="doctorName != null and doctorName != ''">
            and b.doctor_name like CONCAT('%',#{doctorName},'%')
        </if>
        <if test="level != null and level != ''">
            and c.level = #{level}
        </if>
        <if test="startDate != null and startDate != ''">
            <![CDATA[ AND a.gmt_create >= #{startDate}]]>
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[ AND a.gmt_create <= #{endDate}]]>
        </if>
        order by a.gmt_create
        )t LEFT JOIN med_home_page n
        ON t.hospitalId = n.hospital_id
        AND t.behospitalCode = n.behospital_code
        AND n.is_deleted = 'N'
    </select>

    <select id="getEntryDefectImproveDept" resultMap="entryDefectImproveInnerExportMap">
        SELECT
        b.hospital_id as hospitalId,
        b.beh_dept_id as deptId,
        b.beh_dept_name as deptName,
        b.behospital_code as behospitalCode,
        b.doctor_id AS doctorId,
        b.doctor_name AS doctorName,
        ifnull(a.cases_entry_ids,0) as casesEntryIds
        FROM
        med_qcresult_click a,
        med_behospital_info b
        WHERE
        a.is_deleted = 'N'
        AND b.is_deleted = 'N'
        AND a.hospital_id =b.hospital_id
        AND a.behospital_code =b.behospital_code
        <if test="hospitalId != null and hospitalId != ''">
            AND b.hospital_id = #{hospitalId}
        </if>
        <if test="deptId != null and deptId != ''">
            and b.beh_dept_id = #{deptId}
        </if>
        <if test="deptName != null and deptName != ''">
            and b.beh_dept_name = #{deptName}
        </if>
        <if test="doctorId != null and doctorId != ''">
            and b.doctor_id like CONCAT('%',#{doctorId},'%')
        </if>
        <if test="doctorName != null and doctorName != ''">
            and b.doctor_name like CONCAT('%',#{doctorName},'%')
        </if>
        <if test="startDate != null and startDate != ''">
            <![CDATA[ AND a.gmt_create >= #{startDate}]]>
        </if>
        <if test="endDate != null and endDate != ''">
            <![CDATA[ AND a.gmt_create <= #{endDate}]]>
        </if>
        order by a.gmt_create
    </select>
</mapper>
