<?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.elephant.framework.galaxy.aroad.module.system.mapper.UserMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.elephant.framework.galaxy.aroad.module.system.vo.UserVO">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="salt" property="salt"/>
        <result column="phone" property="phone"/>
        <result column="avatar" property="avatar"/>
        <result column="wx_openid" property="wxOpenid"/>
        <result column="qq_openid" property="qqOpenid"/>
        <result column="ucreate_time" property="createTime"/>
        <result column="uupdate_time" property="updateTime"/>
        <result column="lock_flag" property="lockFlag"/>
        <result column="udel_flag" property="delFlag"/>
        <result column="deptId" property="deptId"/>
        <result column="tenantId" property="tenantId"/>
        <result column="deptName" property="deptName"/>
        <collection property="roleList" ofType="com.elephant.framework.galaxy.aroad.module.system.entity.Role"
                    select="com.elephant.framework.galaxy.aroad.module.system.mapper.RoleMapper.listRolesByUserId" column="user_id">
        </collection>
    </resultMap>

    <resultMap id="userDeptResultMap" type="com.elephant.framework.galaxy.aroad.module.system.vo.UserVO">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
        <result column="phone" property="phone"/>
        <result column="dept_id" property="deptId"/>
        <result column="name" property="deptName"/>
    </resultMap>

    <!-- userVo结果集 -->
    <resultMap id="userVoResultMap" type="com.elephant.framework.galaxy.aroad.module.system.vo.UserVO">
        <id column="user_id" property="userId"/>
        <result column="username" property="username"/>
        <result column="password" property="password"/>
        <result column="salt" property="salt"/>
        <result column="phone" property="phone"/>
        <result column="avatar" property="avatar"/>
        <result column="wx_openid" property="wxOpenid"/>
        <result column="qq_openid" property="qqOpenid"/>
        <result column="ucreate_time" property="createTime"/>
        <result column="uupdate_time" property="updateTime"/>
        <result column="lock_flag" property="lockFlag"/>
        <result column="udel_flag" property="delFlag"/>
        <result column="deptId" property="deptId"/>
        <result column="tenantId" property="tenantId"/>
        <result column="deptName" property="deptName"/>
        <collection property="roleList" ofType="com.elephant.framework.galaxy.aroad.module.system.entity.Role">
            <id column="role_id" property="roleId"/>
            <result column="role_name" property="roleName"/>
            <result column="role_code" property="roleCode"/>
            <result column="role_desc" property="roleDesc"/>
            <result column="rcreate_time" property="createTime"/>
            <result column="rupdate_time" property="updateTime"/>
        </collection>
    </resultMap>

    <resultMap id="memberDtoMap" type="com.elephant.framework.galaxy.aroad.module.attendance.dto.MemberDto">
        <result property="id" column="user_id"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
    </resultMap>

    <sql id="userRoleSql">
        u.user_id,
		u.username,
		u.password,
		u.salt,
		u.phone,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.dept_id,
		u.create_time AS ucreate_time,
		u.update_time AS uupdate_time,
		u.del_flag AS udel_flag,
		u.lock_flag AS lock_flag,
		u.dept_id AS deptId,
		u.tenant_id AS tenantId,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time AS rcreate_time,
		r.update_time AS rupdate_time
    </sql>

    <sql id="userRoleDeptSql">
        u.user_id,
		u.username,
		u.password,
		u.salt,
		u.phone,
		u.avatar,
		u.wx_openid,
		u.qq_openid,
		u.create_time AS ucreate_time,
		u.update_time AS uupdate_time,
		u.del_flag AS udel_flag,
		u.lock_flag AS lock_flag,
		u.tenant_id AS tenantId,
		r.role_id,
		r.role_name,
		r.role_code,
		r.role_desc,
		r.create_time AS rcreate_time,
		r.update_time AS rupdate_time,
		d.name AS deptName,
		d.dept_id AS deptId
    </sql>

    <select id="getUserVoByUsername" resultMap="userVoResultMap">
        SELECT
        <include refid="userRoleSql"/>
        FROM
        sys_user AS u
        LEFT JOIN sys_user_role AS ur ON ur.user_id = u.user_id
        LEFT JOIN sys_role AS r ON r.role_id = ur.role_id
        WHERE u.username = #{username}
    </select>

    <select id="getUserVoById" resultMap="userVoResultMap">
        SELECT
        <include refid="userRoleDeptSql"/>
        FROM
        sys_user AS u
        LEFT JOIN sys_user_role AS ur ON ur.user_id = u.user_id
        LEFT JOIN sys_role AS r ON r.role_id = ur.role_id
        LEFT JOIN sys_dept AS d ON d.dept_id = u.dept_id
        WHERE
        u.user_id = #{id}
    </select>

    <select id="getUserVosPage" resultMap="BaseResultMap">
        SELECT
        u.user_id,
        u.username,
        u.password,
        u.salt,
        u.phone,
        u.avatar,
        u.wx_openid,
        u.qq_openid,
        u.dept_id,
        u.create_time AS ucreate_time,
        u.update_time AS uupdate_time,
        u.del_flag AS udel_flag,
        u.lock_flag AS lock_flag,
        u.dept_id AS deptId,
        u.tenant_id AS tenantId
        FROM
        sys_user AS u
        LEFT JOIN sys_dept AS d ON d.dept_id = u.dept_id
        <where>
            <if test="query.username != null and query.username != ''">
                and u.username LIKE CONCAT('%',#{query.username},'%')
            </if>
            <if test="query.deptId != null and query.deptId != ''">
                and u.dept_id = #{query.deptId}
            </if>
        </where>
        ORDER BY u.create_time DESC
    </select>

    <select id="getSelfAndChildUsers" resultMap="userDeptResultMap">
      select
        d.dept_id,d.name,
        u.user_id,u.username,u.phone
      from sys_dept d
      inner join sys_user u
      on d.dept_id = u.dept_id
      where d.dept_id
      in
      <foreach collection="deptIds" item="id" index="index" open="(" close=")" separator=",">
        #{id}
      </foreach>
    </select>

    <select id="getSelfUsers" resultMap="userDeptResultMap">
        select
        u.user_id,u.username
        from sys_dept d
        inner join sys_user u
        on d.dept_id = u.dept_id
        where d.dept_id = #{deptId}
    </select>

    <select id="getUsers" resultMap="memberDtoMap">
        select
          user_id,username as name,phone
        from
          sys_user
        where user_id
        in
        <foreach collection="userIds" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </select>
</mapper>
