<?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.TElLearnMapper">
    
    <resultMap type="TElLearn" id="TElLearnResult">
        <result property="id"    column="id"    />
        <result property="resourceId"    column="resource_id"    />
        <result property="userId"    column="user_id"    />
        <result property="learnTime"    column="learn_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName" column="dept_name" />
        <result property="nickName" column="nick_name" />
    </resultMap>

    <sql id="selectTElLearnVo">
        select d.id, d.resource_id, d.user_id, d.learn_time, d.del_flag, d.create_by, d.create_time, d.update_by, d.update_time, d.dept_id ,
        u.nick_name
        from t_el_learn d
        left join sys_dept s on s.dept_id = d.dept_id
        left join sys_user u on u.USER_ID = d.USER_ID
    </sql>

    <select id="selectTElLearnList" parameterType="TElLearn" resultMap="TElLearnResult">
        <include refid="selectTElLearnVo"/>
        <where>  
            <if test="resourceId != null  and resourceId != ''"> and resource_id = #{resourceId}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="learnTime != null  and learnTime != ''"> and learn_time = #{learnTime}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>

    <select id="selectLearnByUserIdAndResourceId" parameterType="TElLearn" resultMap="TElLearnResult">
        <include refid="selectTElLearnVo"/>
        <where>
            <if test="resourceId != null  and resourceId != ''"> and d.resource_id = #{resourceId}</if>
            <if test="userId != null  and userId != ''"> and d.user_id = #{userId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTElLearnById" parameterType="Long" resultMap="TElLearnResult">
        <include refid="selectTElLearnVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTElLearn" parameterType="TElLearn">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_el_learn.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_el_learn
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="resourceId != null">resource_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="learnTime != null">learn_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="resourceId != null">#{resourceId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="learnTime != null">#{learnTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTElLearn" parameterType="TElLearn">
        update t_el_learn
        <trim prefix="SET" suffixOverrides=",">
            <if test="resourceId != null">resource_id = #{resourceId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="learnTime != null">learn_time = #{learnTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

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

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