<?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.ruoyi.project.training.elearn.mapper.TElPaperQuMapper">

    <resultMap type="TElPaperQu" id="TElPaperQuResult">
        <result property="id"    column="id"    />
        <result property="paperId"    column="paper_id"    />
        <result property="quId"    column="qu_id"    />
        <result property="quType"    column="qu_type"    />
        <result property="answered"    column="answered"    />
        <result property="answer"    column="answer"    />
        <result property="sort"    column="sort"    />
        <result property="score"    column="score"    />
        <result property="actualScore"    column="actual_score"    />
        <result property="isRight"    column="is_right"    />
    </resultMap>

    <sql id="selectTElPaperQuVo">
        select d.id, d.paper_id, d.qu_id, d.qu_type, d.answered, d.answer, d.sort, d.score, d.actual_score, d.is_right from t_el_paper_qu d
    </sql>

    <select id="selectTElPaperQuList" parameterType="TElPaperQu" resultMap="TElPaperQuResult">
        <include refid="selectTElPaperQuVo"/>
        <where>
            <if test="paperId != null "> and paper_id = #{paperId}</if>
            <if test="quId != null "> and qu_id = #{quId}</if>
            <if test="quType != null "> and qu_type = #{quType}</if>
            <if test="answered != null "> and answered = #{answered}</if>
            <if test="answer != null  and answer != ''"> and answer = #{answer}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="actualScore != null "> and actual_score = #{actualScore}</if>
            <if test="isRight != null "> and is_right = #{isRight}</if>
        </where>
    </select>

    <select id="selectTElPaperQuListByPaper" parameterType="TElPaperQu" resultMap="TElPaperQuResult">
        select d.id, d.paper_id, d.qu_id, d.qu_type, d.answered, d.answer, d.sort, d.score, d.actual_score, d.is_right,eq.content,eq.image from t_el_paper_qu d
        LEFT JOIN t_el_qu eq ON d.qu_id = eq.qu_id
        <where>
            <if test="paperId != null "> and paper_id = #{paperId}</if>
            <if test="quId != null "> and qu_id = #{quId}</if>
            <if test="quType != null "> and qu_type = #{quType}</if>
            <if test="answered != null "> and answered = #{answered}</if>
            <if test="answer != null  and answer != ''"> and answer = #{answer}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="score != null "> and score = #{score}</if>
            <if test="actualScore != null "> and actual_score = #{actualScore}</if>
            <if test="isRight != null "> and is_right = #{isRight}</if>
        </where>
    </select>



    <select id="selectTElPaperQuById" parameterType="Long" resultMap="TElPaperQuResult">
        <include refid="selectTElPaperQuVo"/>
        where id = #{id}
    </select>

    <select id="selectTElPaperQuByPaperIdAndQuId" parameterType="TElPaperQu" resultMap="TElPaperQuResult">
        <include refid="selectTElPaperQuVo"/>
        where paper_id = #{paperId} and qu_id = #{quId} and ROWNUM = 1
    </select>


    <insert id="insertTElPaperQu" parameterType="TElPaperQu">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_el_paper_qu.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_el_paper_qu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="paperId != null">paper_id,</if>
            <if test="quId != null">qu_id,</if>
            <if test="quType != null">qu_type,</if>
            <if test="answered != null">answered,</if>
            <if test="answer != null">answer,</if>
            <if test="sort != null">sort,</if>
            <if test="score != null">score,</if>
            <if test="actualScore != null">actual_score,</if>
            <if test="isRight != null">is_right,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="paperId != null">#{paperId},</if>
            <if test="quId != null">#{quId},</if>
            <if test="quType != null">#{quType},</if>
            <if test="answered != null">#{answered},</if>
            <if test="answer != null">#{answer},</if>
            <if test="sort != null">#{sort},</if>
            <if test="score != null">#{score},</if>
            <if test="actualScore != null">#{actualScore},</if>
            <if test="isRight != null">#{isRight},</if>
         </trim>
    </insert>

    <update id="updateTElPaperQu" parameterType="TElPaperQu">
        update t_el_paper_qu
        <trim prefix="SET" suffixOverrides=",">
            <if test="paperId != null">paper_id = #{paperId},</if>
            <if test="quId != null">qu_id = #{quId},</if>
            <if test="quType != null">qu_type = #{quType},</if>
            <if test="answered != null">answered = #{answered},</if>
            <if test="answer != null">answer = #{answer},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="score != null">score = #{score},</if>
            <if test="actualScore != null">actual_score = #{actualScore},</if>
            <if test="isRight != null">is_right = #{isRight},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTElPaperQuByKey" parameterType="TElPaperQu">
        update t_el_paper_qu
        <trim prefix="SET" suffixOverrides=",">
            <if test="paperId != null">paper_id = #{paperId},</if>
            <if test="quId != null">qu_id = #{quId},</if>
            <if test="quType != null">qu_type = #{quType},</if>
            <if test="answered != null">answered = #{answered},</if>
            <if test="answer != null">answer = #{answer},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="score != null">score = #{score},</if>
            <if test="actualScore != null">actual_score = #{actualScore},</if>
            <if test="isRight != null">is_right = #{isRight},</if>
        </trim>
        where paper_id = #{paperId} and qu_id = #{quId}
    </update>



    <delete id="deleteTElPaperQuById" parameterType="Long">
        delete from t_el_paper_qu where id = #{id}
    </delete>

    <delete id="deleteTElPaperQuByIds" parameterType="String">
        delete from t_el_paper_qu  where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="sumObjective" resultType="int">
        SELECT NVL(SUM(actual_score), 0) as total
        FROM t_el_paper_qu
        WHERE paper_id = #{paperId}
        AND is_right = 1
        AND  4 > qu_type
    </select>
</mapper>
