<?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.zxdmy.excite.system.mapper.cloud.space.CloudFileRoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.zxdmy.excite.system.entity.cloud.space.CloudFileRole">
        <id column="id" property="id" />
        <result column="file_id" property="fileId" />
        <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, file_id, user_id
    </sql>


    <select id="getFileRole" resultType="com.zxdmy.excite.system.entity.vo.space.CloudFileRoleVo" >
        select r.*,u.username
        from cloud_file_role r
        LEFT JOIN sys_user u on u.id = r.user_id
        where r.file_id = #{id}
    </select>

    <select id="getRoleFile" resultType="com.zxdmy.excite.system.entity.vo.space.CloudRoleFileVo" >
        select r.*,f.file_name
        from cloud_role_file r
                 LEFT JOIN cloud_file f on f.id = r.file_id
        where r.role_id = #{id}
    </select>


    <select id="getRoleFileCheck" resultType="com.zxdmy.excite.system.entity.vo.space.CloudRoleFileVo" >
        SELECT *
        FROM (
                 SELECT
                     r.file_id,
                     r.role_id,
                     f.id,
                     f.file_name,
                     f.file_sort,
                     f.file_folder,
                     f.file_parent_id,
                     (SELECT COUNT(0) FROM cloud_file c WHERE c.file_parent_id=f.id AND c.delete_time IS NULL) as c
                 FROM cloud_file f
                          LEFT JOIN cloud_role_file r ON r.file_id = f.id
                          LEFT JOIN cloud_role cr on cr.id = r.role_id
                 WHERE f.delete_time IS NULL
                   AND f.file_folder = 0
                   AND cr.id = #{id}
             ) AS t
        WHERE t.c = 0
    </select>
</mapper>
