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

    <resultMap id="SysRole" type="com.chushang.security.entity.po.SysRole">
        <id     property="roleId"             column="role_id"               />
        <result property="roleName"           column="role_name"             />
        <result property="roleKey"            column="role_key"              />
        <result property="orderNum"           column="order_num"             />
        <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="delState"            column="del_state"              />
        <result property="createBy"           column="create_by"             />
        <result property="createTime"         column="create_time"           />
        <result property="updateTime"         column="update_time"           />
        <result property="remark"             column="remark"                />
    </resultMap>

    <sql id="selectRoleVo">
        select distinct r.role_id, r.role_name, r.role_key, r.order_num, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,
                        r.status, r.del_state, r.create_time, r.remark, r.update_time, r.create_by
        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
    </sql>

    <select id="selectRoleList" resultMap="SysRole"
            parameterType="com.chushang.security.entity.po.SysRole">
        select distinct r.role_id, r.role_name, r.role_key, r.order_num, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,
        r.status, r.del_state, r.create_time, r.remark, r.update_time, r.create_by
        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

        where r.del_state = FALSE
        <if test="roleId != null and roleId != 0">
            AND r.role_id = #{roleId}
        </if>
        <if test="roleName != null and roleName != ''">
            AND r.role_name like concat('%', #{roleName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND r.status = #{status}
        </if>
        <if test="roleKey != null and roleKey != ''">
            AND r.role_key like concat('%', #{roleKey}, '%')
        </if>
        <!-- 数据范围过滤 -->
        ${sqlParam.get('dataScope')}
        order by r.order_num
    </select>

    <select id="listRole" resultMap="SysRole">
        select distinct r.role_id, r.role_name, r.role_key, r.order_num, r.data_scope, r.menu_check_strictly, r.dept_check_strictly,
        r.status, r.del_state, r.create_time, r.remark, r.update_time, r.create_by
        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

        where r.del_state = FALSE
        <if test="listRole.roleName != null and listRole.roleName != ''">
            AND r.role_name like concat('%', #{listRole.roleName}, '%')
        </if>
        <if test="listRole.status != null and listRole.status != ''">
            AND r.status = #{listRole.status}
        </if>
        <if test="listRole.roleKey != null and listRole.roleKey != ''">
            AND r.role_key like concat('%', #{listRole.roleKey}, '%')
        </if>
        <!-- 数据范围过滤 -->
        ${listRole.sqlParam.get('dataScope')}
        order by r.order_num
    </select>

    <select id="selectRoleById" resultMap="SysRole">
        <include refid="selectRoleVo"/>
        where r.role_id = #{roleId}
    </select>

    <select id="selectRoleByUsername" resultMap="SysRole">
        <include refid="selectRoleVo"/>
        where r.del_state = FALSE AND u.username = #{username}
    </select>
</mapper>
