<?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.QcCasesEntryHospitalMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.QcCasesEntryHospital">
        <id column="id" property="id"/>
        <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"/>
        <result column="cases_entry_id" property="casesEntryId"/>
        <result column="hospital_id" property="hospitalId"/>
        <result column="score" property="score"/>
        <result column="msg" property="msg"/>
        <result column="is_used" property="isUsed"/>
        <result column="is_reject" property="isReject"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <select id="getQcCasesEntryAlls" resultType="com.diagbot.dto.QcCasesEntryHospitalDTO">
        select
        b.id as id,
        a.cases_id as casesId,
        a.cases_name As casesName,
        a.name as name,
        b.msg As msg,
        <choose>
            <when test='isPlacefile == "0"'>
                b.score_run AS score,
            </when>
            <otherwise>
                b.score AS score,
            </otherwise>
        </choose>
        <choose>
            <when test='isPlacefile == "0"'>
                b.is_used_run AS isUsed,
            </when>
            <otherwise>
                b.is_used AS isUsed,
            </otherwise>
        </choose>
        b.is_reject As isReject,
        a.rule_type
        from
        qc_cases_entry a
        join qc_cases_entry_hospital b
        on a.id = b.cases_entry_id
        where a.is_deleted = "N"
        AND b.is_deleted = "N"
        <if test="hospitalId != null and hospitalId != ''">
            AND b.hospital_id = #{hospitalId}
        </if>
        <if test="casesId != null and casesId != ''">
            AND a.cases_id = #{casesId}
        </if>
        <if test="name != null and name != ''">
            AND UPPER(a.name) LIKE CONCAT('%', UPPER(trim(#{name})), '%')
        </if>
        <choose>
            <when test='isUsed != null and isPlacefile == "0"'>
                AND b.is_used_run =#{isUsed}
            </when>
            <when test='isUsed != null and isPlacefile == "1"'>
                AND b.is_used =#{isUsed}
            </when>
        </choose>

        <if test="isReject != null ">
            AND b.is_reject = #{isReject}
        </if>
        <if test="ruleType != null">
            AND a.rule_type = #{ruleType}
        </if>
        ORDER BY a.cases_id,a.order_no,a.gmt_modified,b.gmt_modified DESC
    </select>
    <select id="findQcCasesEntryAll" resultType="com.diagbot.dto.QcCasesEntryFindDTO">
        SELECT DISTINCT
        b.cases_id AS casesId,
        b.cases_name AS casesName,
        b.mode_id AS modeId,
        b.id AS casesEntryId,
        a.hospital_id AS hospitalId,
        b. NAME AS entryName,
        a.msg AS msg,
        <choose>
            <when test='isPlacefile == "0"'>
                a.score_run AS score,
            </when>
            <otherwise>
                a.score AS score,
            </otherwise>
        </choose>
        a.is_reject AS isReject,
        c.score AS caseScore
        FROM
        qc_cases_entry_hospital a,
        qc_cases_entry b,
        qc_cases_hospital c
        WHERE
        a.is_deleted = "N"
        AND b.is_deleted = "N"
        AND c.is_deleted = "N"
        AND a.cases_entry_id = b.id
        AND c.cases_id = b.cases_id
        AND c.hospital_id = a.hospital_id
        <choose>
            <when test='isPlacefile == "0"'>
                AND a.is_used_run = 1
            </when>
            <otherwise>
                AND a.is_used = 1
            </otherwise>
        </choose>
        <if test="hospitalId != null and hospitalId != ''">
            AND a.hospital_id = #{hospitalId}
        </if>
        <if test="modeId != null and modeId != ''">
            and b.mode_id =#{modeId}
        </if>
        <if test="entryName != null and entryName != ''">
            AND UPPER(b.name) LIKE CONCAT('%', UPPER(trim(#{entryName})), '%')
        </if>
        and a.cases_entry_id NOT IN
        (SELECT
        cases_entry_id
        FROM
        med_qcresult_detail
        WHERE 1=1
        <if test="behospitalCode != null and behospitalCode != ''">
            AND behospital_code = #{behospitalCode}
        </if>
        <if test="hospitalId != null and hospitalId != ''">
            AND hospital_id = #{hospitalId}
        </if>
        )
    </select>
</mapper>
