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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.QcTypeCasesEntry">
        <id column="id" property="id" />
        <result column="type_id" property="typeId" />
        <result column="case_entry_id" property="caseEntryId" />
        <result column="remark" property="remark" />
        <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>

    <select id="getByTypeId" resultType="com.diagbot.dto.QcCasesEntrySimpleDTO">
        SELECT
            a1.*,IF (a2.case_entry_id IS NULL,NULL,1) sel
        FROM
            (
                SELECT
                    t1.`name`,
                    t1.id,
                    t3.`name` mode_name,
        <choose>
            <when test='isPlacefile == "0"'>
                t2.is_used_run AS isUsed,
            </when>
            <otherwise>
                t2.is_used AS isUsed,
            </otherwise>
        </choose>
                    t3.order_no mode_order,
                    t1.order_no
                FROM
                    qc_cases_entry t1,
                    qc_cases_entry_hospital t2,
                    qc_mode t3
                WHERE
                    t1.is_deleted = 'N'
                AND t2.is_deleted = 'N'
                AND t3.is_deleted = 'N'
                AND t1.id = t2.cases_entry_id
                AND t1.mode_id = t3.id
                AND t2.hospital_id = #{hospitalId}
            ) a1
        LEFT JOIN (
            SELECT
                q2.case_entry_id
            FROM
                qc_type q1,
                qc_type_cases_entry q2
            WHERE
                q1.is_deleted = 'N'
            AND q2.is_deleted = 'N'
            AND q1.id = q2.type_id
            AND q1.hospital_id = #{hospitalId}
            AND q1.id = #{id}
        ) a2 ON a1.id = a2.case_entry_id
        ORDER BY a1.mode_order, a1.order_no
    </select>
</mapper>
