<?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.TElResourceMapper">
    
    <resultMap type="TElResource" id="TElResourceResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="remarks"    column="remarks"    />
        <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="fileId" column="file_id" />
        <result property="fileName" column="file_name" />
        <result property="fileUrl" column="file_url" />
    </resultMap>

    <sql id="selectTElResourceVo">
        select d.id, d.title, d.remarks, d.del_flag, d.create_by, d.create_time, d.update_by, d.update_time, d.dept_id from t_el_resource d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTElResourceList" parameterType="TElResource" resultMap="TElResourceResult">
        <include refid="selectTElResourceVo"/>
        <where>  
            <if test="title != null  and title != ''">
                and title like concat(concat('%', #{title}), '%')
            </if>
            <if test="remarks != null  and remarks != ''"> and
                and remarks like concat(concat('%', #{remarks}), '%')
            </if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTElResourceById" parameterType="Long" resultMap="TElResourceResult">
        <include refid="selectTElResourceVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTElResource" parameterType="TElResource">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_el_resource.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_el_resource
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="title != null">title,</if>
            <if test="remarks != null">remarks,</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="title != null">#{title},</if>
            <if test="remarks != null">#{remarks},</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="updateTElResource" parameterType="TElResource">
        update t_el_resource
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="remarks != null">remarks = #{remarks},</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="deleteTElResourceById" parameterType="Long">
        update t_el_resource set del_flag = 2 where id = #{id}
    </update>

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

    <select id="selectFileList" parameterType="TElResource" resultMap="TElResourceResult">
        select d.id as file_id, d.file_name, d.file_url, r.id, r.title
            from t_commonfile d inner join T_EL_RESOURCE r on d.P_ID = r.id
        <where>
            <if test="title != null">and r.title like concat(concat('%', #{title}), '%')</if>
        </where>
        and d.DEL_FLAG = 0
        and r.DEL_FLAG = 0
        and d.P_TYPE='elearnResource'
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
</mapper>