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

    <!--    获取文件权限-->
    <select id="getInformationAuthority" resultType="java.lang.Integer">
        SELECT F_LimitsOfAuthority FROM ftb_information_authority
        WHERE F_UserId = #{userId} AND F_InformationId = #{informationId}
    </select>
<!--    向上查看文件有无权限-->
    <select id="getUpPathAuthority" resultType="jnpf.model.doclibrary.vo.InformationAuthorityVo">
        SELECT F_InformationId informationId, F_LimitsOfAuthority limitsOfAuthority
        FROM ftb_information_authority
        WHERE  FIND_IN_SET(F_InformationId,(
            SELECT F_FilePath
            FROM ftb_information
            WHERE F_Id = #{informationId})
        ) AND F_UserId = #{userId}
    </select>
<!--    向下查看文件有无权限-->
    <select id="getDownPathAuthority" resultType="java.lang.Integer">
        SELECT COUNT(0)
        FROM ftb_information_authority
        WHERE F_InformationId IN (
            SELECT F_Id
            FROM ftb_information
            WHERE INSTR(F_FilePath,(
                SELECT F_FilePath
                FROM ftb_information
                WHERE F_Id =  #{informationId}
            ))
        ) AND F_UserId = #{userId}
    </select>
<!--    空间/文件夹/文件下成员列表-->
    <select id="getUserList" resultType="jnpf.model.doclibrary.vo.InformationAuthorityVo">
        SELECT F_UserId userId ,F_LimitsOfAuthority limitsOfAuthority ,F_Id id
        FROM ftb_information_authority
        WHERE F_InformationId = #{id}
        ORDER BY limitsOfAuthority ASC
    </select>
    <!--    修改用户权限-->
    <update id="updateUserAuthority">
        update ftb_information_authority
        set
            F_LimitsOfAuthority = #{limitsOfAuthority}
        where F_InformationId = #{id} and F_UserId = #{userId}
    </update>
    <!--    查看用户是不是指定文件的成员-->
    <select id="getIdByUserId" resultType="java.lang.String">
        select  F_Id
        from ftb_information_authority
        where F_InformationId = #{id} and F_UserId = #{userId}
    </select>
<!--    验证用户是不是空间及齐下的成员-->
    <select id="checkSpaceMember" resultType="java.lang.Integer">
        SELECT COUNT(0) FROM ftb_information_authority WHERE F_InformationId IN (
            SELECT F_Id
            FROM ftb_information
            WHERE  FIND_IN_SET(#{id},F_FilePath)
        ) AND F_UserId =  #{userId}
    </select>
<!--    验证用户是不是空间的成员-->
    <select id="spaceMember" resultType="jnpf.model.doclibrary.vo.InformationAuthorityVo">
        SELECT F_LimitsOfAuthority limitsOfAuthority
        FROM ftb_information_authority WHERE F_InformationId = #{id} AND F_UserId =  #{userId}
    </select>
    <!--    新增用户权限-->
    <insert id="addUserAuthority">
        insert into ftb_information_authority (F_Id,F_InformationId,F_UserId,F_LimitsOfAuthority,F_CreatorUserId)
        values (#{userAuthorityId},#{id},#{toUserId},#{limitsOfAuthority},#{userId})
    </insert>
<!--    批量新增用户权限信息-->
    <insert id="addUserAuthorityList">
        insert into ftb_information_authority (F_Id,F_InformationId,F_UserId,F_LimitsOfAuthority,F_CreatorUserId)
        values
        <foreach collection="userList" item="item" separator=",">
            (#{item.id},#{id},#{item.userId},#{item.limitsOfAuthority},#{userId})
        </foreach>
    </insert>
    <!--    修改指定的权限-->
    <update id="updateById">
        update ftb_information_authority
        set
            F_LimitsOfAuthority = #{limitsOfAuthority},
            F_LastModifyTime = now(),
            F_LastModifyUserId = #{updateUserId}
        where F_Id = #{idByUserId}
    </update>
<!--    通过用户与权限关联id删除信息-->
    <delete id="deleteById">
        delete from ftb_information_authority where  F_Id = #{id}
    </delete>
<!--    退出空间-->
    <delete id="deleteByUserAndId">
        delete from ftb_information_authority where  F_InformationId = #{id} and F_UserId = #{userId}
    </delete>
    <!--    批量退出空间-->
    <delete id="deleteByUsersAndId">
        delete from ftb_information_authority where  F_InformationId = #{id}
        <if test="ids != null and ids.size()>0">
            AND F_UserId in
            <foreach collection="ids" item="value" separator="," open="(" close=")">
                #{value}
            </foreach>
        </if>
    </delete>


</mapper>