<?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.ruoyi.file.mapper.UserFileMapper">

    <resultMap id="UserFileMap" type="com.ruoyi.file.domain.UserFile">
        <id property="userFileId" column="userFileId"/>
        <result property="createTime" column="createTime"/>
        <result property="createUserId" column="createUserId"/>
        <result property="deleteBatchNum" column="deleteBatchNum"/>
        <result property="deleteFlag" column="deleteFlag"/>
        <result property="deleteTime" column="deleteTime"/>
        <result property="extendName" column="extendName"/>
        <result property="fileId" column="fileId"/>
        <result property="fileName" column="fileName"/>
        <result property="filePath" column="filePath"/>
        <result property="isDir" column="isDir"/>
        <result property="modifyTime" column="modifyTime"/>
        <result property="modifyUserId" column="modifyUserId"/>
        <result property="uploadTime" column="uploadTime"/>
        <result property="fileSize" column="fileSize"/>
        <result property="userId" column="userId"/>
        <result property="accessPermission" column="accessPermission"/>
    </resultMap>

    <resultMap id="UserFileDtoMap" type="com.ruoyi.file.dto.file.UserFileDTO">
        <id property="userFileId" column="userFileId"/>
        <result property="createTime" column="createTime"/>
        <result property="createUserId" column="createUserId"/>
        <result property="deleteBatchNum" column="deleteBatchNum"/>
        <result property="deleteFlag" column="deleteFlag"/>
        <result property="deleteTime" column="deleteTime"/>
        <result property="extendName" column="extendName"/>
        <result property="fileId" column="fileId"/>
        <result property="fileName" column="fileName"/>
        <result property="filePath" column="filePath"/>
        <result property="isDir" column="isDir"/>
        <result property="modifyTime" column="modifyTime"/>
        <result property="modifyUserId" column="modifyUserId"/>
        <result property="uploadTime" column="uploadTime"/>
        <result property="fileSize" column="fileSize"/>
        <result property="userId" column="userId"/>
        <result property="accessPermission" column="accessPermission"/>
    </resultMap>

    <select id="selectPageVo" resultType="com.ruoyi.file.vo.file.FileListVO">
        select * from userfile a
        left join image on a.fileId = image.fileId
        left join file on file.fileId = a.fileId
        <where>
            <if test="fileTypeId != null">
                <choose>
                    <when test="fileTypeId != 5">
                        extendName in (select fileExtendName from fileclassification where fileTypeId = #{fileTypeId})
                    </when>
                    <otherwise>
                        extendName not in (select fileExtendName from fileclassification where fileTypeId in (1, 2, 3, 4))
                    </otherwise>
                </choose>
                and a.isDir = 0
            </if>
            <if test="userFile.userId != null">
                and a.userId = #{userFile.userId}
            </if>
            <if test="userFile.filePath != null">
                and a.filePath = #{userFile.filePath}
            </if>
            <if test="userFile.extendName != null">
                and a.extendName = #{userFile.extendName}
            </if>
            <if test="userFile.userFileId != null">
                and a.userFileId = #{userFile.userFileId}
            </if>
            <if test="userFile.fileName != null">
                and a.fileName = #{userFile.fileName}
            </if>
            and a.deleteFlag = 0
        </where>
        ORDER BY isDir desc
    </select>

    <select id="selectUserFileByLikeRightFilePath" resultType="com.ruoyi.file.domain.UserFile">
        select * from userfile
        where (filePath = #{filePath} or filePath like concat(#{filePath},'/%')) and userId = #{userId} and deleteFlag = 0
    </select>

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

    <select id="selectByUserFileId" resultMap="UserFileMap" parameterType="java.lang.String">
        SELECT userfile.*, file.fileSize as fileSize FROM userfile
        LEFT JOIN file ON file.fileId = userfile.fileId
        WHERE userfile.userFileId = #{userFileId}
    </select>

    <select id="selectFileTreeByUserId" resultMap="UserFileDtoMap" parameterType="java.lang.Long">
        SELECT userfile.*, file.fileSize as fileSize FROM userfile
        LEFT JOIN file ON file.fileId = userfile.fileId
        WHERE userfile.userId = #{userId}
    </select>

</mapper>
