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

    <resultMap type="TElUserExam" id="TElUserExamResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="examId"    column="exam_id"    />
        <result property="tryCount"    column="try_count"    />
        <result property="maxScore"    column="max_score"    />
        <result property="passed"    column="passed"    />
        <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="deptId"    column="dept_id"    />
        <result property="title"    column="title"    />
        <result property="deptName" column="dept_name" />
        <result property="userName"    column="user_name"    />
    </resultMap>

    <sql id="selectTElUserExamVo">
        select d.id,d.title, d.user_id, d.exam_id, d.try_count, d.max_score, d.passed, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id,d.user_name from t_el_user_exam d
    </sql>

    <select id="selectTElUserExamList" parameterType="TElUserExam" resultMap="TElUserExamResult">
        <include refid="selectTElUserExamVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="examId != null "> and exam_id = #{examId}</if>
            <if test="tryCount != null "> and try_count = #{tryCount}</if>
            <if test="maxScore != null "> and max_score = #{maxScore}</if>
            <if test="title != null  and title != ''"> and title like concat(concat('%', #{title}), '%')</if>

            <if test="passed != null "> and passed = #{passed}</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="deptId != null "> and dept_id = #{deptId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        order by d.updatedate desc
    </select>

    <select id="selectTElUserExamById" parameterType="Long" resultMap="TElUserExamResult">
        <include refid="selectTElUserExamVo"/>
        where id = #{id}
    </select>

    <select id="selectTElUserExamByPaPer" parameterType="TElPaper" resultMap="TElUserExamResult">
        <include refid="selectTElUserExamVo"/>
        where exam_id = #{examId} and user_id = #{userId}
    </select>


    <insert id="insertTElUserExam" parameterType="TElUserExam">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_el_user_exam.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_el_user_exam
        <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="tryCount != null">try_count,</if>
            <if test="maxScore != null">max_score,</if>
            <if test="passed != null">passed,</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="deptId != null">dept_id,</if>
            <if test="title != null">title,</if>
            <if test="userName != null">user_name,</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="tryCount != null">#{tryCount},</if>
            <if test="maxScore != null">#{maxScore},</if>
            <if test="passed != null">#{passed},</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="deptId != null">#{deptId},</if>
            <if test="title != null">#{title},</if>
            <if test="userName != null">#{userName},</if>
         </trim>
    </insert>

    <update id="updateTElUserExam" parameterType="TElUserExam">
        update t_el_user_exam
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="examId != null">exam_id = #{examId},</if>
            <if test="tryCount != null">try_count = #{tryCount},</if>
            <if test="maxScore != null">max_score = #{maxScore},</if>
            <if test="passed != null">passed = #{passed},</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="deptId != null">dept_id = #{deptId},</if>
            <if test="title != null">title = #{title},</if>
            <if test="userName != null">user_name = #{userName},</if>
        </trim>
        where id = #{id}
    </update>

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

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

</mapper>
