<?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.fs.file.mapper.MiniappUploadFileMapper">

    <resultMap type="com.fs.file.domain.MiniappUploadFile" id="MiniappUploadFileResult">
        <id property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileSize" column="file_size"/>
        <result property="fileType" column="file_type"/>
        <result property="fileExt" column="file_ext"/>
        <result property="cosKey" column="cos_key"/>
        <result property="cosUrl" column="cos_url"/>
        <result property="fileMd5" column="file_md5"/>
        <result property="bizType" column="biz_type"/>
        <result property="bizId" column="biz_id"/>
        <result property="status" column="status"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectMiniappUploadFileVo">
        select id, user_id, file_name, file_size, file_type, file_ext, cos_key, cos_url, file_md5,
               biz_type, biz_id, status, remark, create_time, update_time,company_id
        from miniapp_upload_file
    </sql>

    <insert id="insert" parameterType="com.fs.file.domain.MiniappUploadFile" useGeneratedKeys="true" keyProperty="id">
        insert into miniapp_upload_file
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="fileName != null and fileName != ''">file_name,</if>
            <if test="fileSize != null">file_size,</if>
            <if test="fileType != null and fileType != ''">file_type,</if>
            <if test="fileExt != null and fileExt != ''">file_ext,</if>
            <if test="cosKey != null and cosKey != ''">cos_key,</if>
            <if test="cosUrl != null and cosUrl != ''">cos_url,</if>
            <if test="fileMd5 != null and fileMd5 != ''">file_md5,</if>
            <if test="bizType != null and bizType != ''">biz_type,</if>
            <if test="bizId != null and bizId != ''">biz_id,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            create_time,
            update_time,
            company_id
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="fileName != null and fileName != ''">#{fileName},</if>
            <if test="fileSize != null">#{fileSize},</if>
            <if test="fileType != null and fileType != ''">#{fileType},</if>
            <if test="fileExt != null and fileExt != ''">#{fileExt},</if>
            <if test="cosKey != null and cosKey != ''">#{cosKey},</if>
            <if test="cosUrl != null and cosUrl != ''">#{cosUrl},</if>
            <if test="fileMd5 != null and fileMd5 != ''">#{fileMd5},</if>
            <if test="bizType != null and bizType != ''">#{bizType},</if>
            <if test="bizId != null and bizId != ''">#{bizId},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            now(),
            now(),
            <if test="companyId != null">#{companyId}</if>
        </trim>
    </insert>

    <update id="update" parameterType="com.fs.file.domain.MiniappUploadFile">
        update miniapp_upload_file
        <trim prefix="SET" suffixOverrides=",">
            <if test="fileName != null and fileName != ''">file_name = #{fileName},</if>
            <if test="fileSize != null">file_size = #{fileSize},</if>
            <if test="fileType != null and fileType != ''">file_type = #{fileType},</if>
            <if test="fileExt != null and fileExt != ''">file_ext = #{fileExt},</if>
            <if test="cosKey != null and cosKey != ''">cos_key = #{cosKey},</if>
            <if test="cosUrl != null and cosUrl != ''">cos_url = #{cosUrl},</if>
            <if test="fileMd5 != null and fileMd5 != ''">file_md5 = #{fileMd5},</if>
            <if test="bizType != null and bizType != ''">biz_type = #{bizType},</if>
            <if test="bizId != null and bizId != ''">biz_id = #{bizId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_time = now(),
            <if test="companyId != null">company_id = #{companyId}</if>
        </trim>
        where id = #{id}
    </update>

    <select id="selectById" parameterType="Long" resultMap="MiniappUploadFileResult">
        <include refid="selectMiniappUploadFileVo"/>
        where id = #{id}
    </select>

    <select id="selectByMd5" resultMap="MiniappUploadFileResult">
        <include refid="selectMiniappUploadFileVo"/>
        where file_md5 = #{fileMd5}
          and user_id = #{userId}
          and status = 1
        limit 1
    </select>

    <select id="selectByBiz" resultMap="MiniappUploadFileResult">
        <include refid="selectMiniappUploadFileVo"/>
        where biz_type = #{bizType}
          and biz_id = #{bizId}
          and user_id = #{userId}
          and status = 1
        order by create_time desc
    </select>

    <select id="selectByUserId" parameterType="String" resultMap="MiniappUploadFileResult">
        <include refid="selectMiniappUploadFileVo"/>
        where user_id = #{userId}
          and status = 1
        order by create_time desc
    </select>

    <delete id="deleteById" parameterType="Long">
        delete from miniapp_upload_file where id = #{id}
    </delete>

    <delete id="deleteByIds" parameterType="java.util.List">
        delete from miniapp_upload_file where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
