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

    <resultMap id="preQuestionResult" type="NascPreQuestion">
        <id          property="id"          column="id"         />
        <result      property="matterId"    column="matter_id"  />
        <result      property="desc"        column="desc"       />
        <result      property="sortNum"     column="sort_num"   />
    </resultMap>

    <insert id="insertPreQuestion" parameterType="NascPreQuestion" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO nasc_pre_question (matter_id, `desc`, sort_num)
        VALUES (#{matterId}, #{desc}, #{sortNum})
    </insert>

    <delete id="deleteQuestionByMatterId" parameterType="Long">
        DELETE FROM nasc_pre_question
        WHERE matter_id = #{matterId}
    </delete>

    <delete id="deleteQuestionById" parameterType="Long">
        DELETE FROM nasc_pre_question
        WHERE id = #{id}
    </delete>

    <select id="getQuestionByMatterId" parameterType="Long" resultMap="preQuestionResult">
        SELECT npq.id, npq.matter_id, npq.`desc`, npq.sort_num
        FROM nasc_pre_question npq
        WHERE npq.matter_id = #{matterId}
        ORDER BY npq.sort_num DESC
    </select>

    <select id="getQuestionById" parameterType="Long" resultMap="preQuestionResult">
        SELECT npq.id, npq.matter_id, npq.`desc`, npq.sort_num
        FROM nasc_pre_question npq
        WHERE npq.id = #{id}
        ORDER BY npq.sort_num DESC
    </select>

    <select id="checkQuestionDuplicates" parameterType="NascPreQuestion" resultType="Long">
        SELECT COUNT(1)
        FROM nasc_pre_question npq
        WHERE npq.matter_id = #{matterId} AND npq.`desc` = #{desc}
        <if test="id != null and id != 0">
            AND npq.id != #{id}
        </if>
    </select>

    <update id="updatePreQuestion" parameterType="NascPreQuestion">
        UPDATE nasc_pre_question
        SET `desc` = #{desc}, sort_num = #{sortNum}
        WHERE id = #{id}
    </update>

</mapper> 