<?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.mapper.B2c.B2cMemberMapper">

    <sql id="includeMember">
        id,
        id member_id,
        open_id,
        nickname,
        phone,
        email,
        gender,
        COALESCE(avatar, '') avatar,
        COALESCE(last_login_ip, '') last_login_ip,
        COALESCE(last_login_uuid, '') last_login_uuid,
        COALESCE(last_login_time, '') last_login_time,
        status,
        create_time,
        update_time
    </sql>

    <resultMap id="resultMapMember" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="member_id" column="member_id" javaType="java.lang.Long"/>
        <result property="open_id" column="open_id"/>
        <result property="nickname" column="nickname"/>
        <result property="phone" column="phone"/>
        <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="status" column="status" javaType="java.lang.Integer"/>
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
    </resultMap>

    <select id="queryMemberList" resultMap="resultMapMember">
        SELECT
        <include refid="includeMember" />
        FROM b2c_member
        <where>
            <if test="open_id != null and open_id != ''">
                AND open_id = #{open_id}
            </if>
            <if test="nickname != null and nickname != ''">
                AND nickname LIKE CONCAT('%', #{nickname}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND phone LIKE CONCAT('%', #{phone}, '%')
            </if>
            <if test="email != null and email != ''">
                AND email LIKE CONCAT('%', #{email}, '%')
            </if>
            <if test="create_time_begin != null and create_time_begin != '' and create_time_end != null and create_time_end != ''">
                AND create_time BETWEEN #{create_time_begin} AND #{create_time_end}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <!-- 根据 ID 或 账号(手机) 查询是否存在 (用于校验密码是否正确) -->
    <select id="queryMemberByIdOrName" resultType="java.util.LinkedHashMap">
        SELECT id, phone, password FROM b2c_member WHERE (phone = #{phone} OR id = #{id})
    </select>

    <!-- 查询 详情 -->
    <select id="queryMemberDetail" resultMap="resultMapMember">
        SELECT
        <include refid="includeMember" />
        FROM b2c_member
        WHERE id = #{member_id}
        AND del_flag = -1
    </select>

    <!-- 编辑 用户信息 -->
    <update id="updateMember" parameterType="com.backendsys.entity.B2c.B2cMemberDTO">
        UPDATE b2c_member SET
        <trim suffixOverrides="," suffix=" ">
            <if test="open_id != null and open_id != ''">open_id = #{open_id},</if>
            <if test="nickname != null and nickname != ''">nickname = #{nickname},</if>
            <if test="phone != null and phone != ''">phone = #{phone},</if>
            <if test="email != null and email != ''">email = #{email},</if>
            <if test="gender != null and gender != ''">gender = #{gender},</if>
            <if test="avatar != null and avatar != ''">avatar = #{avatar},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="last_login_ip != null and last_login_ip != ''">last_login_ip = #{last_login_ip},</if>
            <if test="last_login_uuid != null and last_login_uuid != ''">last_login_uuid = #{last_login_uuid},</if>
            <if test="last_login_time != null">last_login_time = #{last_login_time},</if>
        </trim>
        WHERE id = #{member_id};
    </update>

    <!-- 查询当前在线用户的信息 -->
    <select id="queryMemberWithLogined" resultType="java.util.LinkedHashMap">
        SELECT
        id,
        open_id,
        COALESCE(nickname, '') nickname,
        COALESCE(phone, '') phone,
        COALESCE(email, '') email,
        COALESCE(gender, '') gender,
        status
        FROM b2c_member
        WHERE last_login_uuid IN
        <foreach collection="last_login_uuids" item="last_login_uuid" open="(" separator="," close=")">
            #{last_login_uuid}
        </foreach>
    </select>

</mapper>