<?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.ruoyi.system.mapper.SysRoleMapper">

	<resultMap type="com.ruoyi.common.web.domain.entity.SysRole" id="SysRoleResult">
		<id     property="roleId"             column="role_id"               />
		<result property="roleName"           column="role_name"             />
		<result property="roleKey"            column="role_key"              />
		<result property="roleSort"           column="role_sort"             />
		<result property="dataScope"          column="data_scope"            />
		<result property="menuCheckStrictly"  column="menu_check_strictly"   />
		<result property="deptCheckStrictly"  column="dept_check_strictly"   />
		<result property="status"             column="status"                />
		<result property="createBy"           column="create_by"             />
		<result property="createTime"         column="create_time"           />
		<result property="updateBy"           column="update_by"             />
		<result property="updateTime"         column="update_time"           />
		<result property="delFlag"            column="del_flag"              />
		<result property="remark"             column="remark"                />
	</resultMap>

	<sql id="selectRoleVo">
	    SELECT DISTINCT r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.menu_check_strictly, r.dept_check_strictly, r.status, r.del_flag, r.create_time, r.remark
        FROM sys_role r
		LEFT JOIN sys_user_role ur ON ur.role_id = r.role_id
		LEFT JOIN sys_user u ON u.user_id = ur.user_id
		LEFT JOIN sys_dept d ON u.dept_id = d.dept_id
    </sql>

	<select id="selectRoleList" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		where r.del_flag = #{delFlag}
		<if test="sysRole.roleId != null and sysRole.roleId != 0">
			AND r.role_id = #{sysRole.roleId}
		</if>
		<if test="sysRole.roleName != null and sysRole.roleName != ''">
			AND r.role_name LIKE concat('%', #{sysRole.roleName}, '%')
		</if>
		<if test="sysRole.status != null and sysRole.status != ''">
			AND r.status = #{sysRole.status}
		</if>
		<if test="sysRole.roleKey != null and sysRole.roleKey != ''">
			AND r.role_key LIKE concat('%', #{sysRole.roleKey}, '%')
		</if>
		<!-- 数据权限过滤 -->
		${sysRole.dataScopeSql}
		ORDER BY r.role_sort
	</select>

	<select id="selectRolePermissionByUserId" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.del_flag = #{delFlag}
		AND ur.user_id = #{userId}
	</select>

	<select id="selectRolesByUserName" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.del_flag = #{delFlag}
		AND u.user_name = #{userName}
	</select>

	<select id="selectRoleById" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.del_flag = #{delFlag}
		AND r.role_id = #{roleId}
	</select>

	<select id="checkRoleNameUnique" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.del_flag = #{delFlag}
		AND r.role_name = #{roleName}
		limit 1
	</select>

	<select id="checkRoleKeyUnique" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.del_flag = #{delFlag}
		AND r.role_key = #{roleKey}
		limit 1
	</select>

</mapper>