<?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.storlead.user.mapper.RoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" extends="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.BaseResultMap" type="com.storlead.user.pojo.entity.RoleEntity">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="desc" property="desc"/>
        <result column="data_scope" property="dataScope"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, `name`, `desc`, data_scope, remark, <include refid="com.storlead.framework.mybatis.mapper.SysBaseFieldMapper.Base_Column_List"></include>
    </sql>

    <!-- 根据用户ID 查询所有的全部菜单权限 也就是用户角色所有的菜单的 permission 字段 -->
    <select id="findMenuPermissionsByUserId" resultType="java.lang.String">
        SELECT m.id
        FROM user
                 INNER JOIN user_role ur ON UR.user_id = user.id
                 INNER JOIN role_menu rm on ur.role_id = rm.role_id
                 INNER JOIN menu m on rm.menu_id = m.id
        WHERE user.id= #{userId}
        UNION
        SELECT DISTINCT sr.name
        FROM user_role sur
                 INNER JOIN role sr on sur.role_id = sr.id
        WHERE sur.user_id = #{userId}
    </select>

    <!--  根据系统用户ID 查询所有权限 -->
    <select id="findByUserId" resultType="com.storlead.user.pojo.entity.RoleEntity">
        SELECT DISTINCT sr.*
        FROM user_role sur
                 LEFT JOIN role sr on sur.role_id = sr.id
        WHERE sur.user_id = #{userId}
    </select>

    <!--查询当前用户所拥有的自定义角色中的部门ID集的权限列表-->
    <select id="getDeptIdsByUserId" resultType="java.lang.Long">
        SELECT dr.dept_id
        FROM user_role ur
                 LEFT JOIN dept_role dr on ur.role_id = dr.role_id
        WHERE dr.dept_id IS NOT NULL AND ur.user_id = #{userId}
    </select>

    <!--查询当前用户所拥有的自定义角色中的部门ID集的权限列表-->
    <select id="getDeptIdsByRoleId" resultType="java.lang.Long">
        SELECT dept_id FROM dept_role dr where dr.role_id = #{roleId}
    </select>

    <!--查询当前用户所拥有的自定义角色中的部门ID集的权限列表-->
    <select id="getMenuIdsByRoleId" resultType="java.lang.Long">
        select menu_id from role_menu where role_id = #{roleId}
        <if test="isApp != null">
            AND is_app = #{isApp}
        </if>
    </select>

</mapper>
