<?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.backendsys.modules.system.dao.SysUserInfoDao">

    <sql id="includeUser">
        u.id id,
        COALESCE(u.username, '') username,
        COALESCE(u.phone, '') phone,
        COALESCE(u.phone_area_code, '') phone_area_code,
        uf.user_id user_id,
        COALESCE(uf.nickname, '') nickname,
        COALESCE(uf.email, '') email,
        COALESCE(uf.gender, '') gender,
        COALESCE(uf.avatar, '') AS avatar,
        COALESCE(uf.last_login_ip, '') last_login_ip,
        COALESCE(uf.last_login_uuid, '') last_login_uuid,
        COALESCE(uf.last_login_time, '') last_login_time,
        uf.is_super is_super,
        uf.point_balance point_balance,
        uf.invite_code invite_code,
        uf.status status,
        uf.audit_status audit_status,
        uf.audit_note audit_note,
        uf.create_time create_time,
        uf.update_time update_time,
        uf.del_flag del_flag
    </sql>

    <resultMap id="resultMapUserList" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="user_id" column="user_id" javaType="java.lang.Long" />
        <result property="username" column="username" />
        <result property="phone" column="phone" />
        <result property="phone_area_code" column="phone_area_code" />
        <result property="nickname" column="nickname" />
        <result property="email" column="email" />
        <result property="gender" column="gender" javaType="java.lang.Integer" />
        <result property="avatar" column="avatar" />
        <result property="last_login_ip" column="last_login_ip" />
        <result property="last_login_uuid" column="last_login_uuid" />
        <result property="last_login_time" column="last_login_time" />
        <result property="is_super" column="is_super" javaType="java.lang.Integer" />
        <result property="integral" column="integral" javaType="java.lang.Integer" />
        <result property="point_balance" column="point_balance" javaType="java.lang.Float" />
        <result property="invite_code" column="invite_code" />
        <result property="status" column="status" javaType="java.lang.Integer"/>
        <result property="audit_status" column="audit_status" javaType="java.lang.Integer" />
        <result property="audit_note" column="audit_note" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
        <result property="del_flag" column="del_flag" javaType="java.lang.Integer" />
        <collection property="role_ids" javaType="java.util.List" ofType="java.lang.Long" column="role_id">
            <id property="role_id" column="role_id" />
        </collection>
    </resultMap>

    <select id="selectUserList" resultMap="resultMapUserList">
        SELECT
            <include refid="includeUser" />,
            urr.role_id role_id,
            COALESCE(ui.integral, 0) integral
        FROM sys_user_info uf
        LEFT JOIN sys_user u ON u.id = uf.user_id
        LEFT JOIN sys_user_role_relation urr ON uf.user_id = urr.user_id
        LEFT JOIN sys_user_integral ui ON uf.user_id = ui.user_id
        <where>
            AND uf.del_flag != 1
            <if test="username != null and username != ''">AND u.username LIKE CONCAT('%', #{username}, '%')</if>
            <if test="phone != null and phone != ''">AND u.phone LIKE CONCAT('%', #{phone}, '%')</if>
            <if test="phone_area_code != null and phone_area_code != ''">AND u.phone_area_code = #{phone_area_code}</if>
            <if test="nickname != null and nickname != ''">AND uf.nickname LIKE CONCAT('%', #{nickname}, '%')</if>
            <if test="email != null and email != ''">AND uf.email LIKE CONCAT('%', #{email}, '%')</if>
            <if test="status != null and status != ''">AND uf.status = #{status}</if>
            <if test="audit_status != null and audit_status != ''">AND uf.audit_status = #{audit_status}</if>
            <if test="role_id != null and role_id != ''">
                AND urr.role_id IN
                <foreach collection="role_id" item="role" open="(" separator="," close=")">#{role}</foreach>
            </if>
            <if test="is_super != null and is_super != ''">AND uf.is_super = #{is_super}</if>
        </where>
        ORDER BY uf.create_time DESC
    </select>

<!--    <resultMap id="resultMapUserDetail" type="java.util.LinkedHashMap">-->
<!--    <resultMap id="resultMapUserDetail" type="com.backendsys.modules.system.entity.SysUserDTO">-->
<!--        <id property="id" column="id" jdbcType="BIGINT" />-->
<!--        <result property="user_id" column="user_id" />-->
<!--        <result property="username" column="username" />-->
<!--        <result property="phone" column="phone" />-->
<!--        <result property="phone_area_code" column="phone_area_code" />-->
<!--        <result property="nickname" column="nickname" />-->
<!--        <result property="email" column="email" />-->
<!--        <result property="gender" column="gender" javaType="java.lang.Integer"/>-->
<!--        <result property="avatar" column="avatar" />-->
<!--        <result property="last_login_ip" column="last_login_ip" />-->
<!--        <result property="last_login_uuid" column="last_login_uuid" />-->
<!--        <result property="last_login_time" column="last_login_time" />-->
<!--        <result property="is_super" column="is_super" javaType="java.lang.Integer"/>-->
<!--        <result property="point_balance" column="point_balance" javaType="java.lang.Float"/>-->
<!--        <result property="invite_code" column="invite_code" />-->
<!--        <result property="status" column="status" javaType="java.lang.Integer"/>-->
<!--        <result property="audit_status" column="audit_status" javaType="java.lang.Integer"/>-->
<!--        <result property="audit_note" column="audit_note" />-->
<!--        <result property="create_time" column="create_time" />-->
<!--        <result property="update_time" column="update_time" />-->
<!--        <result property="del_flag" column="del_flag" javaType="java.lang.Integer"/>-->
<!--        <collection property="roles" javaType="java.util.List" ofType="java.util.LinkedHashMap"-->
<!--            select="queryRoleById" column="id">-->
<!--            <id property="id" column="id" />-->
<!--            <result property="role_name" column="role_name" />-->
<!--            <result property="sort" column="sort" />-->
<!--        </collection>-->
<!--    </resultMap>-->

    <!-- [多对多，使用子查询] -->
    <!-- [嵌套查询] 用户角色 / 角色权限 -->
<!--    <select id="queryRoleById" resultType="java.util.LinkedHashMap">-->
<!--        SELECT ur.id, ur.role_name,-->
<!--               GROUP_CONCAT(m.id) AS module_ids-->
<!--        FROM sys_user_role_relation urr-->
<!--                 LEFT JOIN sys_user_role ur ON urr.role_id = ur.id-->
<!--                 LEFT JOIN sys_user_role_module_relation mr ON urr.role_id = mr.role_id-->
<!--                 LEFT JOIN sys_user_role_module m ON mr.module_id = m.id-->
<!--        WHERE urr.user_id = #{userId}-->
<!--        GROUP BY ur.id-->
<!--        ORDER BY ur.sort ASC-->
<!--    </select>-->

    <!-- 查询 用户详情 -->
<!--    <select id="selectUserDetail" resultMap="resultMapUserDetail">-->
<!--        SELECT-->
<!--        <include refid="includeUser" />-->
<!--        FROM sys_user u-->
<!--        LEFT JOIN sys_user_info uf ON u.id = uf.user_id-->
<!--        WHERE u.id = #{user_id}-->
<!--        AND uf.del_flag = -1-->
<!--    </select>-->

    <resultMap id="resultMapUserByLastLoginUuids" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="username" column="username" />
        <result property="phone" column="phone" />
        <result property="phone_area_code" column="phone_area_code" />
        <result property="nickname" column="nickname" />
        <result property="email" column="email" />
        <result property="avatar" column="avatar" />
        <result property="is_super" column="is_super" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
        <result property="audit_status" column="audit_status" javaType="java.lang.Integer" />
    </resultMap>

    <!-- 查询当前在线用户的信息 -->
    <!--  resultType="java.util.LinkedHashMap" -->
    <select id="selectUserByLastLoginUuids" resultMap="resultMapUserByLastLoginUuids">
        SELECT
        u.id id,
        COALESCE(u.username, '') username,
        COALESCE(u.phone, '') phone,
        COALESCE(u.phone_area_code, '') phone_area_code,

        COALESCE(uf.nickname, '') nickname,
        COALESCE(uf.email, '') email,
        COALESCE(uf.avatar, '') avatar,
        uf.is_super is_super,
        uf.status status,
        uf.audit_status audit_status
        FROM sys_user u
        LEFT JOIN sys_user_info uf ON u.id = uf.user_id
        WHERE uf.last_login_uuid IN
        <foreach collection="last_login_uuids" item="last_login_uuid" open="(" separator="," close=")">
            #{last_login_uuid}
        </foreach>
    </select>

</mapper>