<?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.persagy.dmp.file.dao.FileMapper">
    <resultMap id="FileMap" type="com.persagy.dmp.file.model.FileInfo">
        <result column="id" property="id"/>
        <result column="group_code" property="groupCode"/>
        <result column="file_name" property="fileName"/>
        <result column="business_id" property="businessId"/>
        <result column="file_md5" property="fileMd5"/>
        <result column="expire_date" property="expireDate"/>
        <result column="ts" property="ts"/>
        <result column="creator" property="creator"/>
        <result column="creation_time" property="creationTime"/>
        <result column="modifier" property="modifier"/>
        <result column="modified_time" property="modifiedTime"/>
        <result column="valid" property="valid"/>
        <result column="file_type" property="fileType"/>
        <result column="file_bucket" property="fileBucket"/>
        <result column="file_path" property="filePath"/>
        <result column="file_size" property="fileSize"/>
        <result column="upload_status" property="uploadStatus"/>
    </resultMap>
    <sql id="selectPart">
        SELECT dt_file_info.*,dt_file_md5.file_bucket,dt_file_md5.file_path,dt_file_md5.file_size,dt_file_md5.file_type,dt_file_md5.upload_status
    </sql>
    <sql id="fromPart">
        FROM dt_file_info INNER JOIN dt_file_md5 ON (dt_file_info.file_md5 = dt_file_md5.file_md5 AND dt_file_info.valid = TRUE AND dt_file_md5.valid = TRUE)
    </sql>
    <select id="getFileInfoById" resultMap="FileMap">
        <include refid="selectPart"/>
        <include refid="fromPart"/>
        WHERE dt_file_info.id = #{fileId};
    </select>

    <sql id="selectList">
        FROM dt_file_info INNER JOIN dt_file_md5 ON (dt_file_info.valid = TRUE AND dt_file_md5.valid = TRUE AND dt_file_info.file_md5 = dt_file_md5.file_md5
        AND dt_file_md5.upload_status = 0 AND dt_file_md5.file_bucket = #{bucket}
        <if test="null!=prefix and prefix.length>0">
            AND dt_file_md5.file_path LIKE CONCAT('%',#{prefix},'%')
        </if>) ORDER BY dt_file_info.creation_time DESC
    </sql>
    <select id="getPageSizeByPrefixAndFrom" resultType="java.lang.Integer">
        SET @rowNum = 0;
        SELECT FLOOR((@rowNum := @rowNum + 1)/ #{size}) AS rowNum FROM (SELECT dt_file_info.id AS id
        <include refid="selectList">
            <property name="bucket" value="#{bucket}"/>
            <property name="prefix" value="#{prefix}"/>
        </include>) AS df GROUP BY df.id HAVING df.id = #{from}
    </select>
    <select id="queryListByPrefixAndFrom" resultMap="FileMap">
        <include refid="selectPart"/>
        <include refid="selectList">
            <property name="bucket" value="#{bucket}"/>
            <property name="prefix" value="#{prefix}"/>
        </include>
    </select>
    <select id="getFileInfoByIds" resultMap="FileMap">
        <include refid="selectPart"/>
        <include refid="fromPart"/>
        <foreach collection="fileIds" separator="," open="WHERE dt_file_info.id IN(" close=")" item="fileId">
            #{fileId}
        </foreach>
    </select>
    <select id="queryFileKeysByInfos" resultType="java.lang.String">
        SELECT
        <choose>
            <when test="extractInfos!=null and extractInfos.size()>0">
                <foreach collection="extractInfos" item="item" open="JSON_EXTRACT(JSON_ARRAYAGG(JSON_EXTRACT(infos, '$.*.key','$.*[*].key'," close=")),'$[*][*]')" separator=",">
                    CONCAT('$.',#{item})
                </foreach>
            </when>
            <otherwise>
                JSON_EXTRACT(JSON_ARRAYAGG(JSON_EXTRACT(infos, '$.*.key','$.*[*].key')),'$[*][*]')
            </otherwise>
        </choose> AS fileKeys
        FROM `${schema}_${groupCode}`.dt_object WHERE
        <choose>
            <when test="extractInfos!=null and extractInfos.size()>0">
                <foreach collection="extractInfos" item="item" open="JSON_EXTRACT(infos, '$.*.key','$.*[*].key'," close=") IS NOT NULL" separator=",">
                    CONCAT('$.',#{item})
                </foreach>
            </when>
            <otherwise>
                JSON_EXTRACT(infos, '$.*.key','$.*[*].key') IS NOT NULL
            </otherwise>
        </choose>;
    </select>
</mapper>

