<?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="vip.xiaonuo.disk.mapper.UserFileMapper">

    <select id="selectPageVo" parameterType="vip.xiaonuo.disk.domain.UserFile" resultType="vip.xiaonuo.disk.vo.file.FileListVO">
        select * from userfile a
        left join image on a.FILE_ID = image.FILE_ID
        left join file on file.FILE_ID = a.FILE_ID
        <where>
            <if test="fileTypeId != null">
                <choose>
                    <when test="fileTypeId != 5">
                        EXTEND_NAME in (select FILE_EXTEND_NAME from fileclassification where FILE_TYPE_ID = #{fileTypeId})
                    </when>
                    <otherwise>
                        EXTEND_NAME not in (select FILE_EXTEND_NAME from fileclassification where FILE_TYPE_ID in (1, 2, 3, 4))
                    </otherwise>
                </choose>
                and a.IS_DIR = 0
            </if>
            <if test="userFile.userId != null">
                and a.USER_ID = #{userFile.userId}
            </if>
            <if test="userFile.filePath != null">
                and a.FILE_PATH = #{userFile.filePath}
            </if>
            <if test="userFile.extendName != null">
                and a.EXTEND_NAME = #{userFile.extendName}
            </if>
            <if test="userFile.userFileId != null">
                and a.USER_FILE_ID = #{userFile.userFileId}
            </if>
            <if test="userFile.fileName != null">
                and a.FILE_NAME = #{userFile.fileName}
            </if>
            and a.DELETE_FLAG = 0
        </where>
        ORDER BY IS_DIR desc
    </select>

    <select id="selectUserFileByLikeRightFilePath" resultType="vip.xiaonuo.disk.domain.UserFile">
        select * from userfile
        where (FILE_PATH = #{filePath} or FILE_PATH like concat(#{filePath},'/%')) and USER_ID = #{userId} and DELETE_FLAG = 0
    </select>

    <select id="selectStorageSizeByUserId" resultType="java.lang.Long" parameterType="java.lang.String">
        SELECT SUM(FILE_SIZE) FROM userfile
        LEFT JOIN file ON file.FILE_ID = userfile.FILE_ID
        WHERE userfile.USER_ID = #{userId}
    </select>

</mapper>
