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

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

    <select id="getSymptopInfo" resultType="com.diagbot.dto.RetrievalDTO">
        SELECT a.`name` as retrieval_name,c.id as question_id,b.show_type FROM icss_retrieval a
        LEFT JOIN icss_retrieval_mapping b ON a.id = b.retrieval_id
        LEFT JOIN icss_question_info c ON b.question_id = c.id
        WHERE a.is_deleted = 'N' AND b.is_deleted = 'N' AND c.is_deleted = 'N'
        AND (a.spell = #{InputStr} or a.`name` = #{InputStr})
        AND c.type = #{type}
        <if test="age != null and age != ''">
            <![CDATA[ and c.age_begin <= #{age} ]]>
            <![CDATA[ and c.age_end >= #{age} ]]>
        </if>
        <if test="tagType != null and tagType != ''">
            <![CDATA[ and c.tag_type = #{tagType} ]]>
        </if>
        <if test="itemType != null">
            <![CDATA[ and c.item_type = #{itemType} ]]>
        </if>
        <if test="sexType == 3">
            and c.sex_type in ('1','2','3')
        </if>
        <if test="sexType != 3">
            and c.sex_type in ('3',#{sexType})
        </if>
        <if test="inputIds != null and inputIds.size > 0">
            and c.id not in
            <foreach item="id" collection="inputIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        UNION
        SELECT a.`name` as retrieval_name,c.id as question_id,b.show_type FROM icss_retrieval a
        LEFT JOIN icss_retrieval_mapping b ON a.id = b.retrieval_id
        LEFT JOIN icss_question_info c ON b.question_id = c.id
        WHERE a.is_deleted = 'N' AND b.is_deleted = 'N' AND c.is_deleted = 'N'
        AND (a.spell LIKE CONCAT(#{InputStr},'%') or a.`name` LIKE CONCAT(#{InputStr},'%'))
        AND c.type = #{type}
        <if test="age != null and age != ''">
            <![CDATA[ and c.age_begin <= #{age} ]]>
            <![CDATA[ and c.age_end >= #{age} ]]>
        </if>
        <if test="tagType != null and tagType != ''">
            <![CDATA[ and c.tag_type = #{tagType} ]]>
        </if>
        <if test="itemType != null">
            <![CDATA[ and c.item_type = #{itemType} ]]>
        </if>
        <if test="sexType == 3">
            and c.sex_type in ('1','2','3')
        </if>
        <if test="sexType != 3">
            and c.sex_type in ('3',#{sexType})
        </if>
        <if test="inputIds != null and inputIds.size > 0">
            and c.id not in
            <foreach item="id" collection="inputIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        UNION
        SELECT a.`name` as retrieval_name,c.id as question_id,b.show_type FROM icss_retrieval a
        LEFT JOIN icss_retrieval_mapping b ON a.id = b.retrieval_id
        LEFT JOIN icss_question_info c ON b.question_id = c.id
        WHERE a.is_deleted = 'N' AND b.is_deleted = 'N' AND c.is_deleted = 'N'
        AND (a.spell LIKE CONCAT('%',#{InputStr},'%') or a.`name` LIKE CONCAT('%',#{InputStr},'%'))
        AND c.type = #{type}
        <if test="age != null and age != ''">
            <![CDATA[ and c.age_begin <= #{age} ]]>
            <![CDATA[ and c.age_end >= #{age} ]]>
        </if>
        <if test="tagType != null and tagType != ''">
            <![CDATA[ and c.tag_type = #{tagType} ]]>
        </if>
        <if test="itemType != null">
            <![CDATA[ and c.item_type = #{itemType} ]]>
        </if>
        <if test="sexType == 3">
            and c.sex_type in ('1','2','3')
        </if>
        <if test="sexType != 3">
            and c.sex_type in ('3',#{sexType})
        </if>
        <if test="inputIds != null and inputIds.size > 0">
            and c.id not in
            <foreach item="id" collection="inputIds" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
    </select>
</mapper>
