<?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
			sys_menu.permission
		FROM
			( SELECT GET_V_AUTH_MODEL_ID_P_USE ( #{userId}, 'menu' ) cids ) t,
			sys_menu
		WHERE
			FIND_IN_SET( sys_menu.menu_id, cids ) UNION ALL
		SELECT
			plugin_sys_menu.permission
		FROM
			( SELECT GET_V_AUTH_MODEL_ID_P_USE (  #{userId}, 'menu' ) cids ) t,
			plugin_sys_menu
		WHERE
			FIND_IN_SET( plugin_sys_menu.menu_id, cids )
    </select>

    <select id="userMenuIds" resultType="Long">
        select srm.menu_id
        from sys_roles_menus srm
        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>


    <select id="permissionsAll" resultType="String">
		SELECT
			sys_menu.permission
		FROM
			sys_menu UNION ALL
		SELECT
			plugin_sys_menu.permission
		FROM
			plugin_sys_menu
    </select>


</mapper>
