<?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">
        `user`.user_id,
		`user`.username,
		`user`.`password`,
		`user`.salt,
		`user`.phone,
		`user`.avatar,
		`user`.wx_openid,
		`user`.qq_openid,
		`user`.dept_id,
		`user`.create_time AS ucreate_time,
		`user`.update_time AS uupdate_time,
		`user`.del_flag AS udel_flag,
		`user`.lock_flag AS lock_flag,
		`user`.dept_id AS deptId,
		`user`.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">
        `user`.user_id,
		`user`.username,
		`user`.`password`,
		`user`.salt,
		`user`.phone,
		`user`.avatar,
		`user`.wx_openid,
		`user`.qq_openid,
		`user`.create_time AS ucreate_time,
		`user`.update_time AS uupdate_time,
		`user`.del_flag AS udel_flag,
		`user`.lock_flag AS lock_flag,
		`user`.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 `user`
        LEFT JOIN sys_user_role AS ur ON ur.user_id = `user`.user_id
        LEFT JOIN sys_role AS r ON r.role_id = ur.role_id
        WHERE `user`.username = #{username}
    </select>

    <select id="getUserVoById" resultMap="userVoResultMap">
        SELECT
        <include refid="userRoleDeptSql"/>
        FROM
        sys_user AS `user`
        LEFT JOIN sys_user_role AS ur ON ur.user_id = `user`.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 = `user`.dept_id
        WHERE
        `user`.user_id = #{id}
    </select>

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