<?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.pangu.member.mapper.MemberMapper">

    <resultMap id="MemberVOResult" type="com.pangu.member.domain.vo.MemberVO">
        <id property="memberId" column="member_id"/>
        <result property="memberCode" column="member_code"/>
        <result property="phone" column="phone"/>
        <result property="phoneFull" column="phone_full"/>
        <result property="nickname" column="nickname"/>
        <result property="avatar" column="avatar"/>
        <result property="gender" column="gender"/>
        <result property="birthday" column="birthday"/>
        <result property="identityType" column="identity_type"/>
        <result property="openId" column="open_id"/>
        <result property="regionId" column="region_id"/>
        <result property="regionPath" column="region_path"/>
        <result property="schoolId" column="school_id"/>
        <result property="schoolName" column="school_name"/>
        <result property="schoolGradeId" column="school_grade_id"/>
        <result property="gradeName" column="grade_name"/>
        <result property="schoolClassId" column="school_class_id"/>
        <result property="className" column="class_name"/>
        <result property="registerSource" column="register_source"/>
        <result property="registerTime" column="register_time"/>
        <result property="status" column="status"/>
    </resultMap>

    <sql id="selectMemberVOColumns">
        m.member_id,
        m.member_code,
        CONCAT(LEFT(m.phone, 3), '****', RIGHT(m.phone, 4)) AS phone,
        m.phone AS phone_full,
        m.nickname,
        m.avatar,
        m.gender,
        m.birthday,
        m.identity_type,
        m.open_id,
        m.region_id,
        m.school_id,
        m.school_grade_id,
        m.school_class_id,
        m.register_source,
        m.register_time,
        m.status,
        s.school_name,
        s.region_path,
        g.grade_name,
        c.class_name
    </sql>

    <!-- 查询会员列表 -->
    <select id="selectMemberVOList" resultMap="MemberVOResult">
        SELECT <include refid="selectMemberVOColumns"/>
        FROM pg_member m
        LEFT JOIN pg_school s ON m.school_id = s.school_id AND s.del_flag = '0'
        LEFT JOIN pg_school_grade sg ON m.school_grade_id = sg.id AND sg.del_flag = '0'
        LEFT JOIN pg_grade g ON sg.grade_id = g.grade_id AND g.del_flag = '0'
        LEFT JOIN pg_school_class sc ON m.school_class_id = sc.id AND sc.del_flag = '0'
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        WHERE m.del_flag = '0'
        <if test="dto.phone != null and dto.phone != ''">
            AND m.phone LIKE CONCAT('%', #{dto.phone}, '%')
        </if>
        <if test="dto.nickname != null and dto.nickname != ''">
            AND m.nickname LIKE CONCAT('%', #{dto.nickname}, '%')
        </if>
        <if test="dto.identityType != null and dto.identityType != ''">
            AND m.identity_type = #{dto.identityType}
        </if>
        <if test="dto.status != null and dto.status != ''">
            AND m.status = #{dto.status}
        </if>
        <if test="dto.beginTime != null and dto.beginTime != ''">
            AND m.register_time &gt;= CONCAT(#{dto.beginTime}, ' 00:00:00')
        </if>
        <if test="dto.endTime != null and dto.endTime != ''">
            AND m.register_time &lt;= CONCAT(#{dto.endTime}, ' 23:59:59')
        </if>
        <if test="dto.regionId != null">
            AND m.region_id = #{dto.regionId}
        </if>
        <if test="dto.schoolId != null">
            AND m.school_id = #{dto.schoolId}
        </if>
        ORDER BY m.register_time DESC
    </select>

    <!-- 根据ID查询会员详情 -->
    <select id="selectMemberVOById" resultMap="MemberVOResult">
        SELECT <include refid="selectMemberVOColumns"/>
        FROM pg_member m
        LEFT JOIN pg_school s ON m.school_id = s.school_id AND s.del_flag = '0'
        LEFT JOIN pg_school_grade sg ON m.school_grade_id = sg.id AND sg.del_flag = '0'
        LEFT JOIN pg_grade g ON sg.grade_id = g.grade_id AND g.del_flag = '0'
        LEFT JOIN pg_school_class sc ON m.school_class_id = sc.id AND sc.del_flag = '0'
        LEFT JOIN pg_class c ON sc.class_id = c.class_id AND c.del_flag = '0'
        WHERE m.member_id = #{memberId}
        AND m.del_flag = '0'
    </select>

    <!-- 根据手机号查询数量 -->
    <select id="countByPhone" resultType="int">
        SELECT COUNT(1)
        FROM pg_member
        WHERE phone = #{phone}
        AND del_flag = '0'
        <if test="memberId != null">
            AND member_id != #{memberId}
        </if>
    </select>

    <!-- 根据手机号查询会员 -->
    <select id="selectByPhone" resultType="com.pangu.member.domain.entity.Member">
        SELECT * FROM pg_member
        WHERE phone = #{phone}
        AND del_flag = '0'
        LIMIT 1
    </select>

    <!-- 根据OpenID查询会员 -->
    <select id="selectByOpenId" resultType="com.pangu.member.domain.entity.Member">
        SELECT * FROM pg_member
        WHERE open_id = #{openId}
        AND del_flag = '0'
        LIMIT 1
    </select>

</mapper>
