<?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.business.mapper.TtTrainFileMapper">
    
    <resultMap type="TtTrainFile" id="TtTrainFileResult">
        <result property="id"    column="id"    />
        <result property="fileId"    column="file_id"    />
        <result property="trainId"    column="train_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectTtTrainFileVo">
        select id, file_id, train_id, del_flag, create_time, create_by, update_time, update_by, remark from tt_train_file
    </sql>

    <sql id="selectTtTrainFileVoWithFileName">
        select f.*, d.file_name,d.file_path
        from tt_train_file f
        left join tt_file_database d on d.id = f.file_id
    </sql>

    <select id="selectTtTrainFileList" parameterType="TtTrainFile" resultType="com.ruoyi.project.business.domain.train.TtTrainFile">
        <include refid="selectTtTrainFileVoWithFileName"/>
        <where>
            f.del_flag = '0'
            <if test="fileId != null "> and f.file_id = #{fileId}</if>
            <if test="trainId != null "> and f.train_id = #{trainId}</if>
        </where>
    </select>
    
    <select id="selectTtTrainFileById" parameterType="Long" resultMap="TtTrainFileResult">
        <include refid="selectTtTrainFileVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTtTrainFile" parameterType="TtTrainFile" useGeneratedKeys="true" keyProperty="id">
        insert into tt_train_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="fileId != null">file_id,</if>
            <if test="trainId != null">train_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="fileId != null">#{fileId},</if>
            <if test="trainId != null">#{trainId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <insert id="insertTtTrainFileBatch" parameterType="TtTrainFile" useGeneratedKeys="true" keyProperty="id">
            insert into tt_train_file
            (file_id,train_id,del_flag,create_time, create_by,update_time,update_by,remark) values
            <foreach collection="list" item="item" index="index" separator=",">
                (#{item.fileId},#{item.trainId},#{item.delFlag},#{item.createTime},#{item.createBy},#{item.updateTime},#{item.updateBy},#{item.remark})
            </foreach>
    </insert>

    <update id="updateTtTrainFile" parameterType="TtTrainFile">
        update tt_train_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileId != null">file_id = #{fileId},</if>
            <if test="trainId != null">train_id = #{trainId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTtTrainFileById" parameterType="Long">
        delete from tt_train_file where id = #{id}
    </delete>

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

    <update id="deleteTtTrainFileByTrainIds">
        update tt_train_file set del_flag = '1' where train_id in
        <foreach item="trainId" collection="array" open="(" separator="," close=")">
            #{trainId}
        </foreach>
    </update>
</mapper>