<?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="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="tag_name" property="tagName" />
        <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="tag_type" property="tagType" />
        <result column="control_type" property="controlType" />
        <result column="type" property="type" />
        <result column="sub_type" property="subType" />
        <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="judge_type" property="judgeType" />
        <result column="text_generate" property="textGenerate" />
        <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="remark" property="remark" />
    </resultMap>



    <delete id="deleteByIds" parameterType="java.util.Map">
        update `icss_question_info`
        set gmt_modified = #{gmtModified}, modifier = #{modifier}, is_deleted = #{delete}
        where is_deleted = 'N'
        and id in
        <foreach item="id" collection="ids" open="(" separator="," close=")" >
            #{id}
        </foreach>
    </delete>

    <select id="index" parameterType="java.util.Map" resultType="com.diagbot.entity.QuestionInfo">
        select * from `icss_question_info`
        where is_deleted = 'N'
        <if test="tagName != null and tagName != ''">
            and tag_name like concat("%", #{tagName}, "%")
        </if>
        <if test="sexType != null 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>
        <if test="type != null and type != ''">
            and type = #{type}
        </if>
        <if test="controlType != null and controlType != ''">
            and control_type = #{controlType}
        </if>
        <if test="notIds != null and notIds.size() > 0 ">
            and id not in
            <foreach  collection="notIds" item="id" open="("  separator=","  close=")">
                #{id}
            </foreach>
        </if>
        order by gmt_modified desc
    </select>

    <select id="getQuestiongInfoByName" resultType="com.diagbot.dto.GetQuestionInfoDTO">
        SELECT
        a.*
        FROM
        `icss_question_info` a
        WHERE
        a.is_deleted = 'N'
        AND a.type = '1'
        AND a.sub_type = '0'
        <if test="deptId != null and deptId != ''">
            AND a.id NOT IN (
            SELECT
            b.question_id
            FROM
            `icss_question_usual` b
            WHERE
            b.is_deleted = 'N'
            AND b.dept_id = #{deptId}
            )
        </if>
        <if test="tagName != null and tagName != ''">
            AND a.tag_name like CONCAT('%', #{tagName}, '%')
        </if>
    </select>



    <select id="getList" resultType="com.diagbot.dto.QuestionPageDTO">
        SELECT a.* FROM `icss_question_info` a WHERE
        a.is_deleted = 'N'
        <if test="type != null and type != ''">
            AND a.type = #{type}
        </if>
        <if test="tagName != null and tagName != ''">
            AND a.name like concat ('%', #{tagName}, '%')
        </if>
        order by a.gmt_create desc

    </select>

    <select id="getQuestionUsualsByDept" resultType="com.diagbot.dto.GetQuestionInfoDTO">
        SELECT a.* FROM `icss_question_info` a
        LEFT JOIN icss_question_usual b ON a.id = b.question_id
        WHERE b.dept_id = #{deptId}
        GROUP BY a.id
    </select>


    <select id="getSpecial" parameterType="java.util.Map" resultMap="BaseResultMap">
        SELECT DISTINCT t1.* FROM icss_question_info t1, `icss_question_detail` t2
        where t1.is_deleted = 'N' and t2.is_deleted = 'N' and
        t1.id = t2.question_id and t2.`code` in (1,2)
        <if test="sexType != null and sexType != ''">
            and t1.sex_type in (3, #{sexType})
        </if>
        <if test="age != null and age != ''">
            <![CDATA[ and t1.age_begin <= #{age} ]]>
            <![CDATA[ and t1.age_end >= #{age} ]]>
        </if>
        <if test="type != null and type != ''">
            and t1.type = #{type}
        </if>
        <if test="notIds != null and notIds.size() > 0 ">
            and t1.id not in
            <foreach  collection="notIds" item="id" open="("  separator=","  close=")">
                #{id}
            </foreach>
        </if>
    </select>


</mapper>
