<?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.xinelu.familydoctor.mapper.EvaluateQuestionMapper">

    <resultMap id="BaseResultMap" type="com.xinelu.familydoctor.entity.EvaluateQuestion">
            <id property="id" column="id" jdbcType="BIGINT"/>
            <result property="surveyId" column="survey_id" jdbcType="BIGINT"/>
            <result property="questionName" column="question_name" jdbcType="VARCHAR"/>
            <result property="questionType" column="question_type" jdbcType="VARCHAR"/>
            <result property="questionSort" column="question_sort" jdbcType="INTEGER"/>
            <result property="requiredFlag" column="required_flag" jdbcType="VARCHAR"/>
            <collection property="options" ofType="com.xinelu.familydoctor.entity.EvaluateOption"
                        column="id" select="com.xinelu.familydoctor.mapper.EvaluateOptionMapper.getOptions">

            </collection>
    </resultMap>

    <sql id="Base_Column_List">
        id,survey_id,question_name,
        question_type,question_sort,required_flag
    </sql>

    <select id="getQuestions" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from evaluate_question
        where  survey_id = #{surveyId,jdbcType=BIGINT}
    </select>

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.xinelu.familydoctor.entity.EvaluateQuestion" useGeneratedKeys="true">
        insert into evaluate_question
        (survey_id,question_name
        ,question_type,question_sort,required_flag)
        values (#{surveyId,jdbcType=BIGINT},#{questionName,jdbcType=VARCHAR}
        ,#{questionType,jdbcType=VARCHAR},#{questionSort,jdbcType=INTEGER},#{requiredFlag,jdbcType=VARCHAR})
    </insert>
    <update id="update" parameterType="com.xinelu.familydoctor.entity.EvaluateQuestion">
        update evaluate_question
        <set>
                <if test="surveyId != null">
                    survey_id = #{surveyId,jdbcType=BIGINT},
                </if>
                <if test="questionName != null">
                    question_name = #{questionName,jdbcType=VARCHAR},
                </if>
                <if test="questionType != null">
                    question_type = #{questionType,jdbcType=VARCHAR},
                </if>
                <if test="questionSort != null">
                    question_sort = #{questionSort,jdbcType=INTEGER},
                </if>
                <if test="requiredFlag != null">
                    required_flag = #{requiredFlag,jdbcType=VARCHAR},
                </if>
        </set>
        where   id = #{id,jdbcType=BIGINT}
    </update>

    <update id="clearSurveyId">
        update evaluate_question set survey_id = null where survey_id = #{surveyId,jdbcType=BIGINT}
    </update>
</mapper>
