<?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="io.dataease.base.mapper.ext.AuthMapper">

    <resultMap id="baseMap" type="io.dataease.auth.entity.SysUserEntity">
        <id column="user_id" property="userId"/>
        <result column="username" jdbcType="VARCHAR" property="username" />
        <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
        <result column="dept_id"  property="deptId" />
        <result column="password" jdbcType="VARCHAR" property="password" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="enabled" property="enabled" />
        <result column="is_admin" jdbcType="BIT" property="isAdmin" />
    </resultMap>

    <resultMap id="roleMap" type="io.dataease.auth.api.dto.CurrentRoleDto" >
        <id column="role_id" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name" />
    </resultMap>


    <select id="findUser" resultMap="baseMap">
        select user_id, username,nick_name, dept_id, password, enabled,email, phone, language,is_admin from sys_user where user_id = #{userId}
    </select>

    <select id="findUserByName" resultMap="baseMap">
        select user_id, username,nick_name, dept_id, password, enabled,email, phone, language ,is_admin from sys_user where username = #{username}
    </select>

    <select id="roleCodes" resultType="String">
        select r.id from sys_role r
            left join sys_users_roles sur on sur.role_id = r.role_id
            where sur.user_id = #{userId}
    </select>

    <select id="permissions"  resultType="String">
        select sm.permission
            from sys_menu sm
            left join sys_roles_menus srm on srm.menu_id = sm.menu_id
            left join sys_users_roles sur on sur.role_id = srm.role_id
            where sur.user_id = #{userId}
    </select>

    <select id="roles" resultMap="roleMap">
        select r.role_id, r.name
        from sys_role r
        left join sys_users_roles sur on sur.role_id = r.role_id
        where sur.user_id = #{userId}
    </select>




</mapper>
