<?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.TbFileMapper">
    
    <resultMap type="TbFile" id="TbFileResult">
        <result property="id"    column="id"    />
        <result property="businessId"    column="business_id"    />
        <result property="businessType"    column="business_type"    />
        <result property="realFileName"    column="real_file_name"    />
        <result property="serverFileName"    column="server_file_name"    />
        <result property="filePath"    column="file_path"    />
        <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="selectTbFileVo">
        select id, business_id, business_type, real_file_name, server_file_name, file_path, del_flag, create_time, create_by, update_time, update_by, remark from tb_file
    </sql>

    <select id="selectTbFileList" parameterType="TbFile" resultMap="TbFileResult">
        <include refid="selectTbFileVo"/>
        <where>  
            <if test="businessId != null "> and business_id = #{businessId}</if>
            <if test="businessType != null  and businessType != ''"> and business_type = #{businessType}</if>
            <if test="realFileName != null  and realFileName != ''"> and real_file_name like concat('%', #{realFileName}, '%')</if>
            <if test="serverFileName != null  and serverFileName != ''"> and server_file_name like concat('%', #{serverFileName}, '%')</if>
            <if test="filePath != null  and filePath != ''"> and file_path = #{filePath}</if>
             and del_flag = '0'
        </where>
    </select>
    
    <select id="selectTbFileById" parameterType="Long" resultMap="TbFileResult">
        <include refid="selectTbFileVo"/>
        where id = #{id}
    </select>
    <select id="selectTbFileListByBuzIdAndBuzType" resultMap="TbFileResult">
        select id,
               business_id,
               business_type,
               real_file_name,
               server_file_name,
               file_path,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark
        from tb_file
        where business_id = #{businessId}
          and business_type = #{businessType}
          and del_flag = '0'
    </select>

    <insert id="insertTbFile" parameterType="TbFile" useGeneratedKeys="true" keyProperty="id">
        insert into tb_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="businessId != null">business_id,</if>
            <if test="businessType != null">business_type,</if>
            <if test="realFileName != null">real_file_name,</if>
            <if test="serverFileName != null">server_file_name,</if>
            <if test="filePath != null">file_path,</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="businessId != null">#{businessId},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="realFileName != null">#{realFileName},</if>
            <if test="serverFileName != null">#{serverFileName},</if>
            <if test="filePath != null">#{filePath},</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="batchInsertTbFile">
        insert into tb_file
        (business_id,
        business_type,
        real_file_name,
        server_file_name,
        file_path,
        del_flag,
        create_time,
        create_by,
        update_time,
        update_by,
        remark) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.businessId},
            #{item.businessType},
            #{item.realFileName},
            #{item.serverFileName},
            #{item.filePath},
            '0',
            #{item.createTime},
            #{item.createBy},
            #{item.updateTime},
            #{item.updateBy},
            #{item.remark})
        </foreach>
    </insert>

    <update id="updateTbFile" parameterType="TbFile">
        update tb_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="businessId != null">business_id = #{businessId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="realFileName != null">real_file_name = #{realFileName},</if>
            <if test="serverFileName != null">server_file_name = #{serverFileName},</if>
            <if test="filePath != null">file_path = #{filePath},</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="deleteTbFileById" parameterType="Long">
        update tb_file set del_flag = '1' where id = #{id}
    </delete>

    <delete id="deleteTbFileByIds" parameterType="String">
        update tb_file set del_flag = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="deleteTbFileByBusinessIdAndType">
        update tb_file set del_flag = '1' where business_id = #{businessId}
        and business_type = #{businessType}
    </update>

    <update id="deleteTbFileByBusinessIdsAndType">
        update tb_file set del_flag = '1' where business_type = #{businessType} and business_id in
        <foreach item="businessId" collection="businessIds" open="(" separator="," close=")">
            #{businessId}
        </foreach>
    </update>

    <update id="updateTbFileByAccountId">
        update tb_file set business_id = #{accountHistoryId}, business_type = #{businessType}
        where business_id = #{accountId}
    </update>

</mapper>