<?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.nasc.custom.mapper.NascPreQuestionOptionMapper">

    <resultMap id="preQuestionOptionResult" type="NascPreQuestionOption">
        <id          property="id"              column="id"                />
        <result      property="questionId"      column="question_id"       />
        <result      property="desc"            column="desc"              />
        <result      property="sortNum"         column="sort_num"          />
        <result      property="jumpToMatterId"  column="jump_to_matter_id" />
        <association property="material"  javaType="NascMaterial"  resultMap="materialResult" />
    </resultMap>

    <resultMap id="materialResult" type="NascMaterial">
        <id     property="id"          column="materialId"         />
        <result property="optionId"    column="option_id"  />
        <result property="matterId"    column="matter_id"  />
        <result property="conditions"  column="conditions" />
        <result property="dealWith"    column="deal_with"  />
    </resultMap>

    <insert id="insertPreQuestionOption" parameterType="NascPreQuestionOption" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO nasc_pre_question_option (question_id, `desc`, jump_to_matter_id, sort_num)
        VALUES (#{questionId}, #{desc}, #{jumpToMatterId}, #{sortNum})
    </insert>

    <delete id="deleteOptionByQuestionId" parameterType="Long">
        DELETE FROM nasc_pre_question_option
        WHERE question_id = #{questionId}
    </delete>

    <select id="getPreQuestionOptionListByQuestionId" parameterType="Long" resultMap="preQuestionOptionResult">
        SELECT npqo.id, npqo.question_id, npqo.`desc`, npqo.jump_to_matter_id, npqo.sort_num,
               nm.id AS materialId, nm.option_id, nm.conditions, nm.deal_with
        FROM nasc_pre_question_option npqo
        LEFT JOIN nasc_material nm ON nm.option_id = npqo.id
        WHERE npqo.question_id = #{questionId}
        ORDER BY npqo.sort_num DESC
    </select>

    <select id="getQuestionByJumpNum" parameterType="Long" resultType="Long">
        SELECT COUNT(1)
        FROM nasc_pre_question_option
        WHERE jump_to_matter_id = #{questionId}
    </select>

</mapper> 