<?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">
<!--  Copyright (c) 京北方信息技术股份有限公司 Corporation 2022 . All rights reserved. -->
<!--  Mybatis数据库持久层（业务层映射文件）：附件 -->
<!--  version: 1.0  -->
<!--  author:  maven-cctp-plugin    -->
<!--  date:  2022-03-08 10:25:33  -->
<mapper namespace="net.northking.cctp.attachment.db.dao.AttachmentDao">


  <!-- 请在本文件内添加自定义SQL语句 -->
  <select id="findByPK" resultMap="BaseResultMap" parameterType="net.northking.cctp.attachment.db.entity.Attachment">
    select
    <include refid="Base_Column_List" />
    from
    <include refid="Table_Name"/>
    where id = #{id,jdbcType=VARCHAR}
  </select>

  <select id="existTable" parameterType="String" resultType="Integer">
    select count(*)
    from information_schema.TABLES
    where table_name = #{tableName}
  </select>

  <update id="updateAllNewFile" parameterType="net.northking.cctp.attachment.db.entity.Attachment">
    update
    <include refid="Table_Name"/>
    set new_file = #{newFile, jdbcType=VARCHAR}
    where new_file = #{id,jdbcType=VARCHAR}
  </update>

  <update id="createNewTable" parameterType="String">
    create table attachment_${tenantId}
    (
      id               varchar(128) not null comment '主键',
      file_name        varchar(512) not null comment '文件名称',
      content_type     varchar(100) not null comment '文件类型',
      length           BIGINT   comment '文件长度',
      business_code    varchar(50)  DEFAULT NULL,
      obj_id           varchar(50)  DEFAULT NULL COMMENT '关联对象Id',
      over_time        DATETIME DEFAULT NULL,
      created_by       VARCHAR(32)    COMMENT '创建人' ,
      created_time     DATETIME    COMMENT '创建时间' ,
      is_deleted       bool         not null    DEFAULT 0 COMMENT '是否删除' ,
      status           int      comment '状态',
      tenant_id        varchar(32) not null default '${tenantId}' comment '租户',
      new_file         varchar(128) DEFAULT NULL COMMENT '更新文件id',
      primary key (id)
    );
  </update>
    <update id="updateByObjIds">
      update
      <include refid="Table_Name"/>
      set obj_id = -1
      where obj_id in
      <foreach collection="objectIds" open="(" index="index" item="objId" close=")" separator=",">
        #{objId}
      </foreach>
      and is_deleted = 0
    </update>
  <update id="clearAttachmentsByIds">
    update
    <include refid="Table_Name"/>
    set obj_id = -1
    where id in
    <foreach collection="deleteIds" open="(" index="index" item="id" close=")" separator=",">
      #{id}
    </foreach>
    and is_deleted = 0
  </update>
  <update id="associatedFiles">
    update
    <include refid="Table_Name"/>
    set obj_id = #{objectId}
    where id in
    <foreach collection="fileIds" open="(" index="index" item="id" close=")" separator=",">
      #{id}
    </foreach>
    and is_deleted = 0
  </update>

  <select id="pagingQuery" resultMap="BaseResultMap" parameterType="net.northking.cctp.attachment.db.entity.Attachment">
    select
    id
    ,file_name
    ,content_type
    ,length
    ,created_by
    ,created_time
    ,is_deleted
    ,status
    ,tenant_id
    ,new_file
    from
    <include refid="Table_Name"/>
    <trim prefix="WHERE" prefixOverrides="AND">
      <if test="id != null">
        AND id = #{id,jdbcType=VARCHAR}
      </if>
      <if test="fileName != null">
        AND file_name = #{fileName,jdbcType=VARCHAR}
      </if>
      <if test="contentType != null">
        AND content_type = #{contentType,jdbcType=VARCHAR}
      </if>
      <if test="length != null">
        AND length = #{length,jdbcType=BIGINT}
      </if>
      <if test="createdBy != null">
        AND created_by = #{createdBy,jdbcType=VARCHAR}
      </if>
      <if test="createdTime != null">
        AND created_time = #{createdTime,jdbcType=TIMESTAMP}
      </if>
      <if test="deleted != null">
        AND is_deleted = #{deleted,jdbcType=BIT}
      </if>
      <if test="status != null">
        AND status = #{status,jdbcType=INTEGER}
      </if>
      <if test="tenantId != null">
        AND tenant_id = #{tenantId,jdbcType=VARCHAR}
      </if>
      <if test="newFile != null">
        new_file = #{newFile, jdbcType=VARCHAR},
      </if>
    </trim>
  </select>
    <select id="queryClearRecordScreenshot" resultType="net.northking.cctp.attachment.db.entity.Attachment">
      SELECT
      *
      FROM
      <include refid="Table_Name"/>
      WHERE
      business_code IN
      <foreach collection="businessCodes" open="(" index="index" item="item" close=")" separator=",">
        #{item}
      </foreach>
      AND obj_id IS NULL
      AND created_time &lt; DATE_FORMAT(#{date},'%Y-%m-%d')
      AND is_deleted = 0
    </select>

  <select id="queryByObjId" resultType="net.northking.cctp.attachment.db.entity.Attachment">
    SELECT
    *
    FROM
    <include refid="Table_Name"/>
    WHERE
    obj_id = #{objId}
    AND is_deleted = 0
    AND created_time &lt; DATE_FORMAT(#{date},'%Y-%m-%d')
  </select>
  <select id="queryByOverTime" resultType="net.northking.cctp.attachment.db.entity.Attachment">
    SELECT
    *
    FROM
    <include refid="Table_Name"/>
    WHERE
    over_time &lt; DATE_FORMAT(#{overTime},'%Y-%m-%d %H:%i:%s')
    AND is_deleted = 0
  </select>
    <select id="queryByBusiness" resultType="net.northking.cctp.attachment.db.entity.Attachment">
      SELECT
      *
      FROM
      <include refid="Table_Name"/>
      WHERE
      is_deleted = 0
      AND created_time &lt; DATE_FORMAT(#{date},'%Y-%m-%d')
      AND business_code IN (
      'MOBILE_TASK_SCREENSHOT',
      'MOBILE_TASK_VIDEO',
      'MOBILE_TASK_DEVICE_PERFORMANCE',
      'MOBILE_TASK_APP_PERFORMANCE',
      'MOBILE_TASK_PERFORMANCE_SUMMARY',
      'BATCH_TASK_PERFORMANCE_SUMMARY',
      'PC_TASK_SCREENSHOT',
      'PC_TASK_VIDEO',
      'TASK_EXECUTE_RESULT',
      'SCENE_TASK_REFRESH_FILE'
      )
      limit 5000
    </select>
</mapper>