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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.Symptom">
        <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="sex_type" property="sexType"/>
        <result column="age_begin" property="ageBegin"/>
        <result column="age_end" property="ageEnd"/>
        <result column="remark" property="remark"/>
    </resultMap>


    <select id="getByPartIds" parameterType="java.util.Map" resultType="com.diagbot.entity.SymptomWrapper">
        SELECT a.*, b.part_id FROM `triage_symptom` a, triage_part_symptom b where a.is_deleted = 'N' and b.is_deleted = 'N'
        and b.symptom_id = a.id
        and b.part_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        and a.sex_type in (3, #{sexType})
        <if test="age != null">
            <![CDATA[ and a.age_begin <= #{age} ]]>
            <![CDATA[ and a.age_end >= #{age} ]]>
        </if>
        order by b.part_id, b.order_no
    </select>

    <select id="getUsual"  parameterType="java.util.Map" resultMap="BaseResultMap">
        SELECT a.*  FROM `triage_symptom` a, triage_symptom_usual b where a.is_deleted = 'N' and b.is_deleted = 'N'
        and b.symptom_id = a.id
        and a.sex_type in (3, #{sexType})
        <if test="age != null">
            <![CDATA[ and a.age_begin <= #{age} ]]>
            <![CDATA[ and a.age_end >= #{age} ]]>
        </if>
        order by b.order_no
    </select>


    <select id="search" parameterType="java.util.Map" resultType="com.diagbot.dto.SymptomSearchDTO">
        select c.*,b.show_type,if(show_type=1, '本体', '同义词') show_type_cn,a.name search_name
        from triage_retrieval a, triage_retrieval_mapping b, triage_symptom c
        where a.is_deleted = 'N' and b.is_deleted = 'N' and c.is_deleted = 'N'
        and a.id = b.retrieval_id and b.item_id = c.id
        and (a.`name` = #{name} or a.spell = #{name})
        and c.sex_type in (3, #{sexType})
        <if test="age != null">
            <![CDATA[ and c.age_begin <= #{age} ]]>
            <![CDATA[ and c.age_end >= #{age} ]]>
        </if>

        union

        select c.*,b.show_type,if(show_type=1, '本体', '同义词') show_type_cn, a.name search_name from triage_retrieval a, triage_retrieval_mapping b, triage_symptom c
        where a.is_deleted = 'N' and b.is_deleted = 'N' and c.is_deleted = 'N'
        and a.id = b.retrieval_id and b.item_id = c.id
        and (a.`name` like concat('%',#{name} , '%') or a.spell like concat('%',#{name} , '%'))
        and c.sex_type in (3, #{sexType})
        <if test="age != null">
            <![CDATA[ and c.age_begin <= #{age} ]]>
            <![CDATA[ and c.age_end >= #{age} ]]>
        </if>
    </select>
</mapper>
