<?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.pwdvalidperiod,
        u.loginip,
        u.orgid,
        u.`status`,
        u.lastmodifier,
        u.lastmodifydate
        FROM
        sys_user u
        where status != '3'
        and u.usertype != 0
        <if test="orgid != null and orgid !=''">
            and u.orgid = #{orgid}
        </if>
        <if test="username != null and username !=''">
            and u.username like concat('%', #{username},'%')
        </if>
        <if test="status != null and status !=''">
            and u.status = #{status}
        </if>
        ORDER BY u.username
    </select>
    <select id="getOrganizationByid" resultType="java.util.Map">
                   SELECT DISTINCT
                    u.id,u.username,u.nickname,u.email,u.phone,u.custom1,u.avatar,o.orgname,o.id oid,o.orgcode orgcode
            FROM
            sys_user u
            INNER JOIN sys_organization o ON u.orgid = o.id
            WHERE
            u.id = #{id}
    </select>

    <!-- 获取审计管理用户 -->
    <select id="getAuditManagement" resultType="com.yfd.platform.system.domain.SysUser">
        SELECT
            su.id,
	        su.email,
	        su.phone,
	        su.username
        FROM
            sys_user su
            INNER JOIN sys_role_users ru ON su.id = ru.userid
            INNER JOIN sys_role sr ON ru.roleid = sr.id
        WHERE
            sr.isvaild = '1'
            AND sr.`level` = '4'
    </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>
