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

    <resultMap type="TElUserBook" id="TElUserBookResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="examId"    column="exam_id"    />
        <result property="quId"    column="qu_id"    />
        <result property="wrongCount"    column="wrong_count"    />
        <result property="title"    column="title"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="sort"    column="sort"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTElUserBookVo">
        select d.id, d.user_id, d.exam_id, d.qu_id, d.wrong_count, d.title, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.sort  from t_el_user_book d
    </sql>

    <select id="selectTElUserBookList" parameterType="TElUserBook" resultMap="TElUserBookResult">
        <include refid="selectTElUserBookVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="examId != null "> and exam_id = #{examId}</if>
            <if test="quId != null "> and qu_id = #{quId}</if>
            <if test="wrongCount != null "> and wrong_count = #{wrongCount}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="createrCode != null  and createrCode != ''"> and creater_code = #{createrCode}</if>
            <if test="createdate != null "> and createdate = #{createdate}</if>
            <if test="updaterCode != null  and updaterCode != ''"> and updater_code = #{updaterCode}</if>
            <if test="updatedate != null "> and updatedate = #{updatedate}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        order by d.updatedate desc
    </select>

    <select id="selectTElUserBookById" parameterType="Long" resultMap="TElUserBookResult">
        <include refid="selectTElUserBookVo"/>
        where id = #{id}
    </select>

    <select id="selectTElUserBookByLast" parameterType="TElUserBook" resultMap="TElUserBookResult">
        select * from ( select d.id, d.user_id, d.exam_id, d.qu_id, d.wrong_count, d.title, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.sort  from t_el_user_book d
        <where>
            <if test="updatedate != null ">and #{updatedate} > updatedate</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            and d.del_flag = 0
        </where>
        order by d.updatedate desc)
        where rownum = 1
    </select>

    <insert id="insertTElUserBook" parameterType="TElUserBook">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_el_user_book.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_el_user_book
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="examId != null">exam_id,</if>
            <if test="quId != null">qu_id,</if>
            <if test="wrongCount != null">wrong_count,</if>
            <if test="title != null">title,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="sort != null">sort,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="examId != null">#{examId},</if>
            <if test="quId != null">#{quId},</if>
            <if test="wrongCount != null">#{wrongCount},</if>
            <if test="title != null">#{title},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="sort != null">#{sort},</if>
         </trim>
    </insert>

    <update id="updateTElUserBook" parameterType="TElUserBook">
        update t_el_user_book
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="examId != null">exam_id = #{examId},</if>
            <if test="quId != null">qu_id = #{quId},</if>
            <if test="wrongCount != null">wrong_count = #{wrongCount},</if>
            <if test="title != null">title = #{title},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where id = #{id}
    </update>


    <update id="deleteTElUserBookByQuId" parameterType="TElUserBook">
        update t_el_user_book set del_flag = 2 where qu_id = #{quId} and user_id = #{userId}
    </update>

    <update id="deleteTElUserBookById" parameterType="Long">
        update t_el_user_book set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTElUserBookByIds" parameterType="String">
        update t_el_user_book set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
