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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.QuestionInfo">
        <id column="id" property="id" />
        <result column="name" property="name" />
        <result column="tag_name" property="tagName" />
        <result column="sex_type" property="sexType" />
        <result column="age_begin" property="ageBegin" />
        <result column="age_end" property="ageEnd" />
        <result column="type" property="type" />
        <result column="text_generate" property="textGenerate" />
        <result column="control_type" property="controlType" />
        <result column="sub_type" property="subType" />
        <result column="item_type" property="itemType" />
        <result column="tag_type" property="tagType" />
        <result column="label_prefix" property="labelPrefix" />
        <result column="label_suffix" property="labelSuffix" />
        <result column="min_value" property="minValue" />
        <result column="max_value" property="maxValue" />
        <result column="copy_type" property="copyType" />
        <result column="show_add" property="showAdd" />
        <result column="show_info" property="showInfo" />
        <result column="joint" property="joint" />
        <result column="formula_code" property="formulaCode" />
        <result column="remark" property="remark" />
    </resultMap>

    <select id="getByParam" parameterType="java.util.Map" resultMap="BaseResultMap">
        select * from icss_question_info  where is_deleted = 'N'
        and id = #{id}
        <if test="sexType != null and sexType != 3 and sexType != ''">
            and sex_type in (3, #{sexType})
        </if>
        <if test="age != null and age != ''">
            <![CDATA[ and age_begin <= #{age} ]]>
            <![CDATA[ and age_end >= #{age} ]]>
        </if>
    </select>


    <select id="getByQuestionMapping" parameterType="java.util.Map" resultType="com.diagbot.entity.wrapper.QuestionInfoWrapper">
        select t.*, m.parent_question, m.position, m.form_position , m.exclusion_type, m.symptom_type from icss_question_info t, icss_question_mapping m
        where t.is_deleted = 'N' and m.is_deleted = 'N' and m.son_question = t.id
        <if test="id != null">
            and m.parent_question = #{id}
        </if>
        <if test="ids != null and ids.size > 0">
            <foreach item="myId" collection="ids" open="and(" separator="or" close=")" >
                m.parent_question = #{myId}
            </foreach>
        </if>
        <if test="sexType != null and sexType != 3 and sexType != ''">
            and t.sex_type in (3, #{sexType})
        </if>
        <if test="age != null">
            <![CDATA[ and t.age_begin <= #{age} ]]>
            <![CDATA[ and t.age_end >= #{age} ]]>
        </if>
        <if test="id != null">
            order by m.order_no
        </if>
        <if test="ids != null and ids.size > 0">
            order by m.parent_question, m.order_no
        </if>
    </select>



    <select id="getByQuestionRelation" parameterType="java.util.Map" resultMap="BaseResultMap">
        select t.* from icss_question_info t, icss_question_relation r
        where t.is_deleted = 'N' and r.is_deleted = 'N' and r.sibling_question = t.id
        and r.curent_question = #{id}
        <if test="sexType != null">
            and t.sex_type in (3, #{sexType})
        </if>
        <if test="age != null">
            <![CDATA[ and t.age_begin <= #{age} ]]>
            <![CDATA[ and t.age_end >= #{age} ]]>
        </if>
        order by r.order_no
    </select>

</mapper>
