<?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.tmerclub.cloud.biz.mapper.AttachFileMapper">
  <resultMap id="attachFileMap" type="com.tmerclub.cloud.biz.model.AttachFile">
    <id column="file_id" property="fileId" />
    <result column="file_path" property="filePath"/>
    <result column="file_type" property="fileType"/>
    <result column="file_name" property="fileName"/>
    <result column="file_size" property="fileSize"/>
    <result column="shop_id" property="shopId"/>
    <result column="type" property="type"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `file_id`,`file_path`,`file_type`,`file_name`,`file_size`,`shop_id`,`type`,`create_time`,`update_time`,`attach_file_group_id`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.biz.vo.AttachFileVO">
    select <include refid="Vo_Column_List"/> from attach_file
    <where>
      <if test="attachFile.shopId != null">
        AND shop_id = #{attachFile.shopId}
      </if>
      <if test="attachFile.uid != null and attachFile.uid != ''">
        AND uid = #{attachFile.uid}
      </if>
      <if test="attachFile.type != null and attachFile.type != ''">
        AND `type` = #{attachFile.type}
      </if>
      <if test="attachFile.fileName != null and attachFile.fileName != ''">
        AND file_name LIKE CONCAT('%',#{attachFile.fileName},'%')
      </if>
      <if test="attachFile.attachFileGroupId != null and attachFile.attachFileGroupId != ''">
        AND attach_file_group_id = #{attachFile.attachFileGroupId}
      </if>
      <if test="attachFile.sysType != null and attachFile.sysType != ''">
        AND `sys_type` = #{attachFile.sysType}
      </if>
    </where>
    ORDER BY update_time DESC, file_id DESC
  </select>
  <insert id="save">
    insert into attach_file (`file_path`,`file_type`,`file_name`,`file_size`,`shop_id`,`uid`,`type`,`attach_file_group_id`, `sys_type`)
    values
    <foreach collection="attachFiles" item="attachFile" separator=",">
      (#{attachFile.filePath},#{attachFile.fileType},#{attachFile.fileName},#{attachFile.fileSize},#{shopId},#{uid},#{attachFile.type},#{attachFile.attachFileGroupId},#{sysType})
    </foreach>
  </insert>
  <update id="update">
    update attach_file
    <set>
      <if test="attachFile.fileName != null">
        `file_name` = #{attachFile.fileName},
      </if>
      <if test="attachFile.attachFileGroupId != null">
        `attach_file_group_id` = #{attachFile.attachFileGroupId},
      </if>
    </set>
    where file_id = #{attachFile.fileId}
  </update>
  <delete id="deleteById">
  delete from attach_file where file_id = #{fileId}
</delete>
  <delete id="batchDeleteByIds">
    delete from attach_file where file_id in
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </delete>
  <select id="getById" resultMap="attachFileMap">
    select  <include refid="Vo_Column_List"/> from attach_file where file_id = #{fileId}
  </select>
  <select id="getByIds" resultType="com.tmerclub.cloud.biz.vo.AttachFileVO">
    select <include refid="Vo_Column_List"/> from attach_file
    where file_id in
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
  </select>
  <select id="getfileTypeByfilePath" resultType="java.lang.String">
    select file_type from attach_file
    where file_path = #{filePath}
  </select>

  <update id="updateBatchByAttachFileGroupId">
    update attach_file
    set attach_file_group_id = 0
    where attach_file_group_id = #{attachFileGroupId}
  </update>
  <update id="batchMoveByShopIdAndIdsAndGroupId">
    update attach_file set `attach_file_group_id` = #{groupId} where shop_id = #{shopId} and sys_type = #{sysType} and file_id IN
    <foreach collection="ids" item="id" separator="," open="(" close=")">
      #{id}
    </foreach>
  </update>
  <update id="updateShopIdByUid">
    update attach_file set shop_id = #{shopId} where uid = #{uid}
  </update>
  <insert id="saveFile" useGeneratedKeys="true" keyProperty="attachFile.fileId">
    insert into attach_file (`file_path`,`file_type`,`file_name`,`file_size`,`shop_id`,`uid`,`type`,`attach_file_group_id`, `sys_type`)
    values(#{attachFile.filePath},#{attachFile.fileType},#{attachFile.fileName},#{attachFile.fileSize},#{attachFile.shopId},#{uid},#{attachFile.type},#{attachFile.attachFileGroupId}, #{attachFile.sysType})
  </insert>
</mapper>
