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

    <resultMap id="sysUserDept" type="io.dataease.controller.sys.response.SysUserDept" >
        <id column="dept_id" property="deptId"></id>
        <result column="pid" property="pid"></result>
        <result column="dept_name" property="deptName"></result>
    </resultMap>

    <resultMap id="sysUserRole" type="io.dataease.controller.sys.response.SysUserRole" >
        <result column="role_id" property="roleId"></result>
        <result column="role_name" property="roleName"></result>
    </resultMap>

    <resultMap id="BaseResultMap" type="io.dataease.controller.sys.response.SysUserGridResponse" extends="io.dataease.base.mapper.SysUserMapper.BaseResultMap">
        <result property="id" column="id"></result>
        <association property="dept" javaType="io.dataease.controller.sys.response.SysUserDept">
            <id column="dept_id" property="deptId"/>
            <result column="pid" property="pid" />
            <result column="dept_name" property="deptName" />
        </association>
        <association property="dept" column="dept_id" javaType="io.dataease.controller.sys.response.SysUserDept" resultMap="sysUserDept"/>

        <collection property="roles"
                    javaType="java.util.ArrayList"
                    ofType="io.dataease.controller.sys.response.SysUserRole"
                    column="user_id"
                    select="queryRole">
        </collection>
    </resultMap>




    <select id="query" parameterType="io.dataease.base.mapper.ext.query.GridExample" resultMap="BaseResultMap">
        select u.*,u.user_id as id, d.pid, d.name as dept_name
        from sys_user u
        left join sys_dept d on d.dept_id = u.dept_id

        <if test="_parameter != null">
            <include refid="io.dataease.base.mapper.ext.query.GridSql.gridCondition" />
        </if>
        <if test="orderByClause != null">
            order by ${orderByClause}
        </if>
        <if test="orderByClause == null">
            order by u.update_time desc
        </if>
    </select>

    <select id="queryRole" resultMap="sysUserRole">
        select r.role_id, r.name as role_name
        from sys_users_roles sur
        left join sys_role r on r.role_id = sur.role_id
        where sur.user_id = #{user_id}
    </select>
</mapper>
