<?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.yfd.platform.system.mapper.SysRoleMapper">
    <insert id="addRoleMenu">
        INSERT INTO sys_role_menu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">
                id,
            </if>
            <if test="roleid != null and roleid != ''">
                roleid,
            </if>
            <if test="menuid != null and menuid != ''">
                menuid
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">
                #{id},
            </if>
            <if test="roleid != null and roleid != ''">
                #{roleid},
            </if>
            <if test="menuid != null and menuid != ''">
                #{menuid}
            </if>
        </trim>
    </insert>

    <!--根据角色id查询该角色下的所有用户-->
    <select id="isRoleUsersByroleid" resultType="java.util.Map">
        select id,roleid,userid from sys_role_users where roleid=#{roleid}
    </select>

    <!--根据角色id查询该角色下的所有权限-->
    <select id="isRoleMenuByRoleId" resultType="java.util.Map">
        select id,roleid,menuid from sys_role_menu where roleid=#{roleid}
    </select>

    <select id="listRoleUsers" resultType="java.util.Map">
        select a.id roleusersid,                <!--分配权限id-->
        a.roleid roleid,                        <!--角色id-->
        a.userid userid,                        <!--用户id-->

        b.code usercode,                    <!--用户编号-->
        b.username username,                    <!--用户名称-->
        b.orgid userorgid,                    <!--所属组织-->
        b.avatar useravatar,                    <!--用户头像-->
        b.email useremail,                    <!--用户邮箱-->
        b.mobile usermobile,                    <!--手机号-->
        b.account useraccount,                <!--登录账号-->
        b.lastupdatetime userlastupdatetime,    <!--密码修改时间-->
        b.status userstatus,                <!--用户状态-->
        b.lastmodifier userlastmodifier,        <!--最近修改者-->
        b.lastmodifydate userlastmodifydate,    <!--最近修改日期-->

        c.rolename rolename,            <!--角色名称-->
        c.level rolelevel,            <!--角色级别-->
        c.description roledescription,    <!--角色描述-->
        c.orgscope roleorgscope,    <!--组织范围-->
        c.optscope roleoptscope,    <!--操作权限-->
        c.busscope rolebusscope,    <!--业务范围-->
        c.isvaild roleisvaild,    <!--是否有效-->
        c.lastmodifier rolelastmodifier,        <!--最近修改者-->
        c.lastmodifydate rolelastmodifydate        <!--最近修改日期-->
        from sys_role_users a LEFT JOIN sys_user b on a.userid =b.id left join
        sys_role c on a.roleid = c.id
        where
        1=1
        <if test="orgid != null">
            and b.orgid = #{orgid}
        </if>
        <if test="username != null">
            and b.username like "%"#{username}"%"
        </if>
        <if test="status != null">
            and b.status=#{status}
        </if>
        <if test="level != null">
            and c.level=#{level}
        </if>
        <if test="rolename != null">
            and c.rolename=#{rolename}
        </if>
        <if test="isvaild != null">
            and c.isvaild=#{isvaild}
        </if>
        ORDER BY c.level,b.orgid,b.code
    </select>
    <!--根据用户id获取角色信息-->
    <select id="getRoleByUserId"
            resultType="com.yfd.platform.system.domain.SysRole">
        SELECT r.id,r.rolename,r.orgscope FROM sys_role_users ru INNER JOIN sys_role r ON ru.roleid =r.id WHERE ru.userid =#{id}
    </select>

    <!--根据角色id获取用户id-->
    <select id="getUserIdById" resultType="java.lang.String">
        select userid from sys_role_users where roleid=#{roleid}
    </select>

    <!--根据用户id获取角色id-->
    <select id="getRoleUsers" resultType="java.lang.String">
          select roleid from sys_role_users where userid=#{userid}
    </select>

    <!--根据 角色id和用户id 删除系统角色用户对照 （admin除外）-->
    <delete id="deleteRoleUsers">
        delete from sys_role_users where userid !=(select u.id from sys_user u where u.account="admin") and  roleid=#{roleid} and userid=#{urserid}
    </delete>

    <!--根据角色ID删除菜单与角色关联信息-->
    <delete id="deleteRoleMenus">
        DELETE FROM sys_role_menu WHERE roleid= #{id}
    </delete>

    <!--根据角色ID删除用户与角色关联信息-->
    <delete id="deleteRoleUser">
        DELETE FROM sys_role_users WHERE roleid= #{id}
    </delete>
</mapper>
