<?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.InformationMapper" >

    <!--    与我相关的文件列表-->
    <select id="relatedToMeFile" resultType="jnpf.model.doclibrary.vo.InformationVo">
        SELECT i.F_Id id,i.F_FileName fileName,i.F_FileUrl fileUrl,F_Owner owner,i.F_FileSuffix fileSuffix,i.F_Classification classification ,i.F_FileType fileType ,a.F_LimitsOfAuthority limitsOfAuthority,i.F_FileSize fileSize , i.F_CreatorUserId creatorUserId
             ,(SELECT MAX(F_CreatorTime) FROM ftb_information_log
                WHERE F_InformationId = i.F_Id  ) AS creatorTime
             ,(SELECT F_Content FROM ftb_information_log
               WHERE F_InformationId = i.F_Id
               AND F_CreatorTime = (SELECT MAX(F_CreatorTime) FROM ftb_information_log                              WHERE F_InformationId =  i.F_Id ) limit 1) AS logContent
        FROM ftb_information_authority a
        LEFT JOIN ftb_information  i ON a.F_InformationId = i.F_Id
        WHERE a.F_UserId = #{fileQueryDto.userId} and F_EnabledMark = 0
        and (F_Classification = 3 or F_Classification = 4)
        <if test="null != enabledMarkIds and enabledMarkIds.size > 0">
            and i.F_Id not in (
                select F_Id from ftb_information where (
                <foreach collection="enabledMarkIds" index="index" item="item" separator="or">
                    find_in_set(#{item}, F_FilePath)
                </foreach>
                )
            )
        </if>
        <if test="null != fileQueryDto.fileName and '' != fileQueryDto.fileName">
            AND INSTR(i.F_FileName,#{fileQueryDto.fileName}) >0
        </if>
        <if test="null != fileQueryDto.fileType and '' != fileQueryDto.fileType">
            AND i.F_FileType = #{fileQueryDto.fileType}
        </if>
        ORDER BY creatorTime DESC
    </select>
    <!--    获取最近查看列表-->
    <select id="recentlyViewed" resultType="jnpf.model.doclibrary.vo.InformationVo">
        SELECT i.F_Id id,i.F_FileName fileName,i.F_FileUrl fileUrl,F_Owner owner,i.F_FileSuffix fileSuffix,
               i.F_FileType fileType,  i.F_CreatorUserId creatorUserId,i.F_Classification classification ,
               i.F_FileSize fileSize , v.F_LastModifyTime creatorTime
        ,(SELECT F_Content FROM ftb_information_log
        WHERE F_InformationId = i.F_Id
        AND F_CreatorTime = (SELECT MAX(F_CreatorTime) FROM ftb_information_log
        WHERE F_InformationId =  i.F_Id  ) limit 1) AS logContent
        FROM ftb_information_recently_viewed v

        LEFT JOIN ftb_information  i ON v.F_InformationId = i.F_Id
        WHERE   v.F_UserId =  #{fileQueryDto.userId} AND  i.F_EnabledMark = 0 and (i.F_Classification = 3 or i.F_Classification = 4)
        <if test="null != enabledMarkIds and enabledMarkIds.size > 0">
            and i.F_Id not in (
                select F_Id from ftb_information where (
                <foreach collection="enabledMarkIds" index="index" item="item" separator="or">
                    find_in_set(#{item}, F_FilePath)
                </foreach>
                )
            )
        </if>
        <if test="null != fileQueryDto.fileName and '' != fileQueryDto.fileName">
            AND INSTR(i.F_FileName,#{fileQueryDto.fileName}) >0
        </if>
        <if test="null != fileQueryDto.fileType and '' != fileQueryDto.fileType">
            AND i.F_FileType = #{fileQueryDto.fileType}
        </if>
        ORDER BY creatorTime DESC
    </select>
<!--    通过文件id获取文件信息-->
    <select id="getInformationById" resultType="jnpf.model.doclibrary.vo.InformationVo">
        SELECT i.F_Id id,i.F_FileName fileName,i.F_FileUrl fileUrl,F_Owner `owner`,i.F_FileSuffix fileSuffix,
               i.F_Classification classification ,i.F_FileSize fileSize ,i.F_FileType fileType,F_FilePath filePath
        FROM ftb_information  i
        WHERE F_Id = #{id}
    </select>
<!--    获取文件夹及其下级路径信息-->
    <select id="getSubordinate" resultType="jnpf.model.doclibrary.vo.InformationVo">
        SELECT F_Id id, F_FilePath filePath
        FROM ftb_information
        WHERE FIND_IN_SET(#{id},F_FilePath)
    </select>
    <select id="recentlyViewedMember" resultType="jnpf.model.doclibrary.vo.InformationVo">
        SELECT i.F_Id id,i.F_FileName fileName,i.F_FileUrl fileUrl,F_Owner owner,i.F_FileSuffix fileSuffix,
        i.F_FileType fileType,  i.F_CreatorUserId creatorUserId,i.F_Classification classification ,
        i.F_FileSize fileSize , v.F_LastModifyTime creatorTime
        ,(SELECT F_Content FROM ftb_information_log
        WHERE F_InformationId = i.F_Id
        AND F_CreatorTime = (SELECT MAX(F_CreatorTime) FROM ftb_information_log
        WHERE F_InformationId =  i.F_Id  ) limit 1) AS logContent
        FROM ftb_information_recently_viewed v
        LEFT JOIN ftb_information_authority a ON v.F_InformationId = a.F_InformationId
        LEFT JOIN ftb_information  i ON v.F_InformationId = i.F_Id
        WHERE   v.F_UserId =  #{fileQueryDto.userId} AND  i.F_EnabledMark = 0 and (i.F_Classification = 3 or i.F_Classification = 4)  AND a.F_UserId = #{fileQueryDto.userId}
        <if test="null != enabledMarkIds and enabledMarkIds.size > 0">
            and i.F_Id not in (
                select F_Id from ftb_information where (
                <foreach collection="enabledMarkIds" index="index" item="item" separator="or">
                    find_in_set(#{item}, F_FilePath)
                </foreach>
                )
            )
        </if>
        <if test="null != fileQueryDto.fileName and '' != fileQueryDto.fileName">
            AND INSTR(i.F_FileName,#{fileQueryDto.fileName}) >0
        </if>
        <if test="null != fileQueryDto.fileType and '' != fileQueryDto.fileType">
            AND i.F_FileType = #{fileQueryDto.fileType}
        </if>
        ORDER BY creatorTime DESC
    </select>
<!--    得到企业下所有被标记为删除的id集合-->
    <select id="getEnabledMarkInformation" resultType="java.lang.String">
        SELECT F_Id
        FROM ftb_information
        WHERE F_EnabledMark = 1
    </select>
    <select id="getDeleteIds" resultType="java.lang.String">

    </select>
    <!--    重命名-->
    <update id="rename">
        update ftb_information
        set F_FileName = #{fileQueryDto.fileName},
            F_LastModifyTime = now(),
            F_LastModifyUserId =#{fileQueryDto.userId}
        where F_Id = #{id}
    </update>
<!--    删除空间/文件夹/文件-->
    <update id="delete">
        update ftb_information
        set F_DeleteUserId = #{userId},
            F_DeleteTime = now(),
            F_EnabledMark = 1
        where F_Id = #{id}
    </update>
<!--    查看用户是不是指定文件的成员-->
    <update id="updateOwner">
        update ftb_information
        set F_Owner = #{userId},
            F_LastModifyTime = now(),
            F_LastModifyUserId = #{updateUserId}
        where F_Id = #{id}
    </update>
<!--    批量修改文件路径-->
    <update id="updateFilePath">
        update ftb_information
        set F_FilePath = #{path},
            F_LastModifyTime = now(),
            F_LastModifyUserId = #{userId}
        where F_Id = #{id}
    </update>
<!--    修改指定文件的父级id-->
    <update id="updateFilePId">
        update ftb_information
        set F_FilePId = #{toInformationId},
            F_LastModifyTime = now(),
            F_LastModifyUserId = #{userId}
        where F_Id = #{id}
    </update>

    <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 (#{spaceDto.id},#{spaceDto.name},#{spaceDto.fileSize},#{spaceDto.fileUrl},#{userId},
                #{spaceDto.fileSuffix},#{spaceDto.classification},#{spaceDto.fileType},#{spaceDto.filePath},
                #{spaceDto.filePId},#{userId},#{userId})
    </insert>

</mapper>