<?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.SysUserMapper">

    <!--根据用户id 和角色id 查询  系统角色用户对照表-->
    <select id="list" resultType="java.util.Map">
        select * from sys_user
        where 1=1
        <if test="orgid != null">
            and orgid = #{orgid}
        </if>
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="mobile != null">
            and mobile = #{mobile}
        </if>
        <if test="status != null">
            and status = #{status}
        </if>
        ORDER BY orgid,code
        <if test="total != null and size != null">
            LIMIT #{total},#{size}
        </if>

    </select>


    <!--用户分配角色 系统角色用户对照新增数据-->
    <insert id="addUserRoles">
        insert into sys_role_users value (#{id},#{roleid},#{userid})
    </insert>

    <!--根据用户id 和角色id 查询  系统角色用户对照表-->
    <select id="getRoleUsersByid" resultType="java.util.Map">
      select id,roleid,userid from sys_role_users where roleid=#{roleid}  and userid=#{userid}
    </select>

    <!--根据用户表id查询角色表所有角色-->
    <select id="getLevel" resultType="java.util.Map">
        select id,level,rolename, from sys_role where id in (select roleid from sys_role_users where userid=#{userid} )
    </select>

    <!--根据用户表id查询角色表所有角色id-->
    <select id="getRoleid" resultType="String">
       select roleid from sys_role_users where userid=#{userid}
    </select>

    <!--根据用户表id查询角色表最大级别-->
    <select id="getMaxLevel" resultType="String">
        select min(level) from sys_role where id in (select roleid from sys_role_users where userid=#{userid})
    </select>
    <select id="queryUsers" resultType="java.util.Map">
        SELECT DISTINCT
        u.id,
        u.usertype,
        u.username,
        u.nickname,
        u.sex,
        u.email,
        u.phone,
        u.avatar,
        u.orgid,
        u.`status`,
        u.lastmodifier,
        u.lastmodifydate
        FROM
        sys_user u
        where 1=1
        and u.usertype != 0
        <if test="orgid != null">
            and u.orgid = #{orgid}
        </if>
        <if test="nickname != null">
            and u.nickname  like concat('%',#{nickname},'%')
        </if>
        ORDER BY u.lastmodifydate DESC
    </select>

    <select id="getOrganizationByid" resultType="java.util.Map">
       SELECT DISTINCT
        u.id,u.username,u.nickname,u.email,u.phone,u.avatar,u.institution_id,u.institution_type,o.orgname,o.id oid,
            op.orgname porgname ,op.id poid
        FROM
            sys_user u
            INNER JOIN sys_organization o ON u.orgid = o.id
            INNER JOIN sys_organization op ON o.parentid = op.id
        WHERE
            u.id = #{id}
    </select>

    <select id="getRegionsByUserId" resultType="java.util.Map">
        SELECT r.id,r.busscope
        FROM sys_user a
        JOIN sys_role_users b ON (a.id=b.userid)
        JOIN sys_role r ON (b.roleid=r.id)
        WHERE a.username=#{username}
    </select>
    <select id="queryUserRole" resultType="java.util.Map">
        SELECT
        user.institution_id,
        user.institution_type,
        sysrole.`level`
        FROM sys_user user
        INNER JOIN sys_role_users role ON role.userid  = user.id
        INNER JOIN sys_role sysrole ON sysrole.id  = role.roleid
        WHERE user.id=#{userId}
    </select>
    <select id="queryUserRoleList" resultType="java.util.Map">
        SELECT
        user.institution_id,
        user.institution_type,
        sysrole.`level`
        FROM sys_user user
        INNER JOIN sys_role_users role ON role.userid  = user.id
        INNER JOIN sys_role sysrole ON sysrole.id  = role.roleid
        WHERE user.id=#{userId} order by sysrole.`level`
    </select>

    <delete id="delRoleUsersByUserid">
        delete from sys_role_users where userid=#{userid}
    </delete>

    <delete id="delInRoleUsersByUserid">
        delete from sys_role_users
        where
        userid=#{userid}
        and roleid not in
        <foreach collection="roleids" item="roleids" open="(" separator=","
                 close=")">
            #{roleids}
        </foreach>
    </delete>
    <delete id="delRoleUsersByUserIds">
        DELETE FROM sys_role_users WHERE userid IN
        <foreach collection="ids" item="id" open="(" separator=","
                 close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
