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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.QcCasesEntry">
        <id column="id" property="id" />
        <result column="cases_id" property="casesId" />
        <result column="cases_name" property="casesName" />
        <result column="mode_id" property="modeId" />
        <result column="code" property="code" />
        <result column="name" property="name" />
        <result column="drgs" property="drgs" />
        <result column="type" property="type" />
        <result column="rule_type" property="ruleType" />
        <result column="dev_type" property="devType" />
        <result column="accuracy_type" property="accuracyType" />
        <result column="precond" property="precond" />
        <result column="order_no" property="orderNo" />
        <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="getQcCasesEntry" parameterType="com.diagbot.vo.AnalyzeVO" resultType="com.diagbot.dto.QcCasesEntryDTO">
        SELECT DISTINCT
        t4.`code`,
        t4.mode_id,
        t4.precond,
        t3.msg
        FROM
        med_behospital_info t1,
        qc_type_cases_entry t2,
        qc_cases_entry_hospital t3,
        qc_cases_entry t4
        WHERE
        t1.qc_type_id = t2.type_id
        AND t2.case_entry_id = t3.cases_entry_id
        AND t1.hospital_id = t3.hospital_id
        AND t3.cases_entry_id = t4.id
        AND ((t1.is_placefile='0' and t3.is_used_run=1) or (t1.is_placefile='1' and t3.is_used=1))
        AND t1.is_deleted = 'N'
        AND t2.is_deleted = 'N'
        AND t3.is_deleted = 'N'
        AND t4.is_deleted = 'N'
        <if test="hospitalId != null and hospitalId != ''">
            AND t1.hospital_id = #{hospitalId}
        </if>
        <if test="behospitalCode != null and behospitalCode != ''">
            AND t1.behospital_code =  #{behospitalCode}
        </if>
        <if test="modeId != null">
            AND t4.mode_id = #{modeId}
        </if>
	</select>

    <select id="getQcResultAlgVO" parameterType="com.diagbot.vo.QcResultAlgQueryVO" resultType="com.diagbot.vo.QcResultAlgVO">
        SELECT DISTINCT
            t4.cases_id AS casesId,
            t4.score AS casesScore,
            t1.cases_entry_id AS casesEntryId,
            <choose>
                <when test='isPlacefile != null and isPlacefile =="0"'>
                    t1.score_run AS score,
                </when>
                <otherwise>
                    t1.score AS score,
                </otherwise>
            </choose>
            t1.msg AS msg,
            t1.is_reject AS isReject,
            t2.code
        FROM
            qc_cases_entry_hospital t1,
            qc_cases_entry t2,
            qc_cases t3,
            qc_cases_hospital t4
        WHERE
            t1.cases_entry_id = t2.id
        AND t2.cases_id = t3.id
        AND t3.id = t4.cases_id
        AND t1.is_deleted = 'N'
        AND t2.is_deleted = 'N'
        AND t3.is_deleted = 'N'
        AND t4.is_deleted = 'N'
        <if test="hospitalId != null and hospitalId != ''">
            AND t1.hospital_id = #{hospitalId}
            AND t4.hospital_id = #{hospitalId}
        </if>
        <if test="codeList != null and codeList.size() > 0">
            AND t2.`code` IN
            <foreach collection="codeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="index" resultType="com.diagbot.dto.QcCasesEntrySimpleDTO">
        SELECT distinct t3.msg name, t2.id
        FROM
        qc_cases_entry t2,
        qc_cases_entry_hospital t3
        where t2.is_deleted = 'N'
        and t3.is_deleted = 'N'
        and t2.id = t3.cases_entry_id
        and t3.is_used = 1
        <if test="hospitalId != null">
            and t3.hospital_id = #{hospitalId}
        </if>
        <if test="input != null and input != ''">
            and t2.name like concat('%', #{input}, '%')
        </if>
        <if test="notIds != null and notIds.size > 0">
            and t2.id not in
            <foreach item="item" collection="notIds" open="(" separator="," close=")" >
                #{item}
            </foreach>
        </if>
        order by t2.order_no
    </select>

    <select id="getEntryByHospital" resultType="com.diagbot.dto.QcCasesEntrySimpleDTO">
        SELECT
            t1.`name`, t1.id, t3.`name` mode_name, t2.is_used, 1 as sel
        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}
        ORDER BY t3.order_no,t1.order_no
    </select>
</mapper>
