<?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="jnpf.doclibrary.mapper.SpaceMapper">
    <!-- 新增用户浏览记录 -->
    <insert id="addRecentlyViewRecord">
        insert into ftb_information_recently_viewed (F_Id, F_InformationId, F_UserId, F_CreatorTime, F_LastModifyTime)
        values (#{id}, #{informationId}, #{userId}, now(), now())
    </insert>
    <!-- 新增资源 -->
    <insert id="addInformation">
        insert into ftb_information (F_Id, F_FileName, F_FileSize, F_FileUrl, F_Owner, F_FileSuffix, F_Classification,
                                     F_FileType,
                                     F_FilePath, F_FilePId, F_CreatorUserId, F_LastModifyUserId)
        values (#{id}, #{fileName}, #{filesize}, #{fileUrl}, #{owner}, #{fileSuffix}, #{classification}, #{fileType},
                #{filePath}, #{filePid}, #{creatorUserId}, #{lastModifyUserId})
    </insert>
    <!-- 更新用户浏览记录 -->
    <update id="updateRecentlyViewInfo">
        update ftb_information_recently_viewed
        set F_LastModifyTime = now()
        where F_Id = #{id}
    </update>
    <!-- 恢复为未删除 -->
    <update id="updateToUndelete">
        update ftb_information
        set
        F_EnabledMark = 0,
        F_LastModifyUserId = #{userId},
        F_LastModifyTime = now(),
        F_DeleteUserId = null,
        F_DeleteTime = null
        where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!-- 恢复为未删除 -->
    <update id="updateToUndeleteById">
        update ftb_information
        set F_EnabledMark      = 0,
            F_LastModifyUserId = #{userId},
            F_LastModifyTime   = now(),
            F_DeleteUserId     = null,
            F_DeleteTime       = null
        where F_Id = #{informationId}
    </update>
    <!-- 恢复文件到指定父级 -->
    <update id="updateUndeleteToSpace">
        update ftb_information
        set F_EnabledMark      = 0,
            F_LastModifyUserId = #{userId},
            F_FilePath         = #{path},
            F_FilePId          = #{pid},
            F_LastModifyTime   = now(),
            F_DeleteUserId     = null,
            F_DeleteTime       = null
        where F_Id = #{informationId}
    </update>
    <!-- 更新资源大小 -->
    <update id="updateFileSize">
        update ftb_information
        set F_FileSize         = #{fileSize},
            F_LastModifyUserId = #{userId},
            F_LastModifyTime   = now()
        where F_Id = #{informationId}
    </update>
    <!-- 删除回收站记录 -->
    <delete id="deleteRubbishRecord">
        delete from ftb_information_rubbish
        where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!-- 根据资料id删除回收站记录 -->
    <delete id="deleteRubbishByInfoIds">
        delete from ftb_information_rubbish
        where F_InformationId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!-- 根据资料id删除浏览记录 -->
    <delete id="deleteViewByInfoIds">
        delete from ftb_information_recently_viewed
        where F_InformationId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!-- 根据资料id删除资料相关权限 -->
    <delete id="deleteAuthorityByInfoIds">
        delete from ftb_information_authority
        where F_InformationId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!-- 根据资料id删除资料记录 -->
    <delete id="deleteInformationByInfoIds">
        delete from ftb_information
        where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>
    <!-- 删除过期的浏览记录 -->
    <delete id="deleteOverdueViewRecord">
        delete
        from ftb_information_recently_viewed
        where TIMESTAMPDIFF(DAY, DATE_FORMAT(F_LastModifyTime, '%Y-%m-%d'), DATE_FORMAT(now(), '%Y-%m-%d')) >=
              (select F_DataJson from ftb_common_config where F_Code = 'viewRecordKeep')
    </delete>
    <!-- 查询我的资源的路径 -->
    <select id="getMyInfoIdByPath" resultType="java.lang.String">
        select F_InformationId
        from ftb_information_authority
        where F_UserId = #{userId} and F_InformationId in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询资源列表 -->
    <select id="getInformationList" resultType="jnpf.model.doclibrary.vo.MiniInformationVo">
        select F_Id id, F_FileName fileName, F_FileSize fileSize, F_FileUrl fileUrl, F_Owner `owner`, F_FileSuffix
        fileSuffix, F_Classification classification,
        F_FileType fileType, F_FilePath filePath, F_FilePId filePid, F_LastModifyTime lastModifyTime, F_LastModifyUserId
        lastModifyUserId, F_CreatorUserId creatorUserId
        from ftb_information fi
        where F_EnabledMark = 0 and F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="null != queryCondition and '' != queryCondition">
            and instr(IFNULL(F_FileName,""), #{queryCondition}) > 0
        </if>
        <if test="null != queryType and '' != queryType and '全部' != queryType">
            and F_FileType = #{queryType}
        </if>
        order by F_Classification asc, F_LastModifyTime desc
    </select>
    <!-- 根据关键字查询配置信息 -->
    <select id="getCommonConfigByKey" resultType="java.lang.String">
        select F_DataJson
        from ftb_common_config
        where F_Code = #{key}
    </select>
    <!-- 查询已使用空间 -->
    <select id="getUsedSpaceSize" resultType="java.lang.Double">
        select ifnull(sum(F_FileSize), 0) usedSpaceSize from ftb_information
        where find_in_set(#{id}, F_FilePath) and F_Classification not in (1, 2) and F_EnabledMark = 0
        <if test="null != list and list.size > 0">
            and F_Id not in (
            select F_Id from ftb_information where (
            <foreach collection="list" index="index" item="item" separator="or">
                find_in_set(#{item}, F_FilePath)
            </foreach>
            )
            )
        </if>
    </select>
    <!-- 查询被删除的部分 -->
    <select id="getDeletedFileIds" resultType="java.lang.String">
        select F_Id
        from ftb_information
        where find_in_set(#{id}, F_FilePath)
          and F_EnabledMark = 1
    </select>
    <!-- 查询资源关联的成员 -->
    <select id="getInformationMemberList" resultType="jnpf.model.doclibrary.vo.UserInfoVo">
        select F_UserId userId, F_LimitsOfAuthority limitsOfAuthority
        from ftb_information_authority
        where F_InformationId = #{id}
    </select>
    <!-- 查看空间/文件夹/文件信息 -->
    <select id="getInformation" resultType="jnpf.model.doclibrary.vo.InformationDetailVo">
        select F_Id               id,
               F_FileName         fileName,
               F_FileUrl          fileUrl,
               F_FilePath         filePath,
               F_Owner            `owner`,
               F_FileSuffix       fileSuffix,
               F_Classification   classification,
               F_FileType         fileType,
               F_LastModifyTime   lastModifyTime,
               F_LastModifyUserId lastModifyUserId,
               F_CreatorTime      creatorTime,
               F_CreatorUserId    creatorUserId
        from ftb_information
        where F_Id = #{id}
    </select>
    <!-- 查看最近浏览记录 -->
    <select id="getRecentlyViewInfo" resultType="jnpf.model.doclibrary.vo.RecentlyViewedVo">
        select F_Id             id,
               F_InformationId  informationId,
               F_UserId         userId,
               F_CreatorTime    creatorTime,
               F_LastModifyTime lastModifyTime
        from ftb_information_recently_viewed
        where F_InformationId = #{id}
        order by F_LastModifyTime desc
        limit 1
    </select>
    <!-- 查看用户最近浏览记录 -->
    <select id="getRecentlyViewByUser" resultType="jnpf.model.doclibrary.vo.RecentlyViewedVo">
        select F_Id             id,
               F_InformationId  informationId,
               F_UserId         userId,
               F_CreatorTime    creatorTime,
               F_LastModifyTime lastModifyTime
        from ftb_information_recently_viewed
        where F_InformationId = #{informationId}
          and F_UserId = #{userId}
    </select>
    <!-- 回收站 - 列表 -->
    <select id="getRubbishList" resultType="jnpf.model.doclibrary.vo.RubbishInfoVo">
        select ir.F_Id rubbishId, ir.F_InformationId informationId, ir.F_CreatorTime creatorTime, i.F_Classification
        classification,
        i.F_FileType fileType, i.F_FileName fileName, i.F_FileSize fileSize, i.F_FileSuffix fileSuffix, i.F_FilePath
        filePath
        from ftb_information_rubbish ir
        left join ftb_information i on i.F_Id = ir.F_InformationId
        where 1 = 1
        <if test="null != userId">
            and ir.F_CreatorUserId = #{userId}
        </if>
        <if test="null != queryType and queryType == 1">
            and F_Classification not in (1)
        </if>
        <if test="null != queryType and queryType == 2">
            and F_Classification in (1)
        </if>
        <if test="null != queryCondition and '' != queryCondition">
            and instr(IFNULL(i.F_FileName,""), #{queryCondition}) > 0
        </if>
        order by i.F_Classification asc, ir.F_CreatorTime desc
    </select>
    <!-- 通过ids查询回收站记录 -->
    <select id="getRubbishListByIds" resultType="jnpf.model.doclibrary.vo.RubbishInfoVo">
        select ir.F_Id rubbishId, ir.F_InformationId informationId, ir.F_CreatorTime creatorTime
        from ftb_information_rubbish ir
        where ir.F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询回收站记录 -->
    <select id="getRubbishById" resultType="jnpf.model.doclibrary.vo.RubbishInfoVo">
        select ir.F_Id rubbishId, ir.F_InformationId informationId, ir.F_CreatorTime creatorTime
        from ftb_information_rubbish ir
        where ir.F_Id = #{rubbishId}
    </select>
    <!-- 根据path查询资源 -->
    <select id="getInformationByPath" resultType="java.lang.String">
        select F_Id
        from ftb_information
        where find_in_set(#{informationId}, F_FilePath)
    </select>
    <!-- 根据ids的path查询资源 -->
    <select id="getInformationByIdsPath" resultType="java.lang.String">
        select distinct F_Id
        from ftb_information
        where (
        <foreach collection="list" index="index" item="item" separator="or">
            find_in_set(#{item}, F_FilePath)
        </foreach>
        )
    </select>
    <!-- 查询资源path -->
    <select id="getInformationPath" resultType="java.lang.String">
        select F_FilePath
        from ftb_information
        where F_Id = #{informationId}
    </select>
    <!-- 根据文件路径查询路径上的删除情况 -->
    <select id="getDeleteInfoByPath" resultType="jnpf.model.doclibrary.vo.DeleteInfoVo">
        select F_Id informationId, F_EnabledMark enabledMark
        from ftb_information
        where find_in_set(F_Id, (#{filePath}))
        order by find_in_set(F_Id, (#{filePath}))
    </select>
    <!-- 查询空间信息 -->
    <select id="getSpaceNameList" resultType="jnpf.model.doclibrary.vo.SpaceInfoVo">
        select F_Id id, F_FileName spaceName
        from ftb_information where F_Id in
        <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <!-- 查询过期的垃圾文件 -->
    <select id="getExpireRubbish" resultType="jnpf.entity.InformationRubbish">
        select F_Id id, F_InformationId informationId, F_CreatorTime creatorTime, F_CreatorUserId creatorUserId
        from ftb_information_rubbish
        where TIMESTAMPDIFF(DAY, DATE_FORMAT(F_CreatorTime, '%Y-%m-%d'), DATE_FORMAT(now(), '%Y-%m-%d')) >= #{keepDay}
        <if test="null != userId">
            and F_CreatorUserId = #{userId}
        </if>
    </select>
    <!-- 查出完整路径中包含infoIds的并且我拥有权限的文件/文件夹/空间 -->
    <select id="getMyResourcePath" resultType="java.lang.String">
        select F_FilePath from ftb_information
        <where>
            <foreach collection="list" index="index" item="item" separator="or">
                find_in_set(#{item}, F_FilePath)
            </foreach>
        </where>
    </select>
    <!-- 查询所有资源中我拥有权限的 -->
    <select id="getMyRootInfoIdByPath" resultType="java.lang.String">
        select F_InformationId
        from ftb_information_authority
        where F_UserId = #{userId}
    </select>
    <!-- 查询本级和子级资源 -->
    <select id="getChildrenInfoList" resultType="java.lang.String">
        select F_Id
        from ftb_information
        where find_in_set(#{informationId}, F_FilePath)
          and F_EnabledMark = 0
    </select>
    <!-- 查询所有子级 -->
    <select id="getAllByPid" resultType="java.lang.String">
        select F_Id
        from ftb_information
        where F_FilePId = #{pid}
          and F_EnabledMark = 0
    </select>
    <!-- 查询回收站资源占用空间 -->
    <select id="getRubbishUsedSpace" resultType="java.lang.Double">
        select ifnull(sum(F_FileSize), 0) usedSpaceSize
        from ftb_information
        where find_in_set(#{informationId}, F_FilePath)
    </select>

    <select id="queryMyResourceFolder" resultType="jnpf.model.doclibrary.vo.MiniInformationTreeVo">
        SELECT
        F_Id AS id,
        F_FileName AS fileName,
        F_FileUrl AS fileUrl,
        F_Owner AS owner,
        F_FileSuffix AS fileSuffix,
        F_Classification AS classification,
        F_FileType AS fileType,
        F_FilePath AS filePath,
        F_FilePId AS filePid
        FROM ftb_information fi
        WHERE F_EnabledMark = 0
        AND F_Classification IN (1, 2)
        <if test="folderName != null and folderName != ''">
            AND F_FileName LIKE CONCAT('%', #{folderName}, '%')
        </if>
        <if test="ids != null and !ids.isEmpty()">
            AND F_Id IN
            <foreach collection="ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        ORDER BY F_LastModifyTime DESC
    </select>

    <select id="queryAllPowerInfoIds" resultType="java.lang.String">
        select F_InformationId
        from ftb_information_authority
        where F_UserId = #{userId}
    </select>

    <select id="queryMyFilesForFolderId" resultType="jnpf.model.doclibrary.vo.MiniInformationVo">
        SELECT
        F_Id AS id,
        F_FileName AS fileName,
        F_FileSize AS fileSize,
        F_FileUrl AS fileUrl,
        F_Owner AS owner,
        F_FileSuffix AS fileSuffix,
        F_Classification AS classification,
        F_FileType AS fileType,
        F_FilePath AS filePath,
        F_FilePId AS filePid,
        F_LastModifyTime AS lastModifyTime,
        F_LastModifyUserId AS lastModifyUserId,
        F_CreatorUserId AS creatorUserId
        FROM ftb_information fi
        WHERE F_EnabledMark = 0
        AND F_FilePId = #{params.folderId}
        AND F_Classification IN (3, 4)
        <if test="params.keyWord != null and params.keyWord != ''">
            AND F_FileName LIKE CONCAT('%', #{params.keyWord}, '%')
        </if>
        and F_FileSuffix in
        ('docx','doc','xls','xlsx','ppt','pptx','pdf','jpg','jpeg','png','mp3','mp4','mov')
        ORDER BY F_LastModifyTime DESC
    </select>
</mapper>