<?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.zhentao.mapper.MatchmakerMapper">
    
    <!-- 红娘VO结果映射 -->
    <resultMap id="MatchmakerVOMap" type="com.zhentao.vo.MatchmakerVO">
        <id column="matchmaker_id" property="matchmakerId"/>
        <result column="real_name" property="realName"/>
        <result column="username" property="username"/>
        <result column="phone" property="phone"/>
        <result column="email" property="email"/>
        <result column="gender" property="gender"/>
        <result column="birth_date" property="birthDate"/>
        <result column="avatar_url" property="avatarUrl"/>
        <result column="matchmaker_type" property="matchmakerType"/>
        <result column="level" property="level"/>
        <result column="success_couples" property="successCouples"/>
        <result column="points" property="points"/>
        <result column="likes" property="likes"/>
        <result column="address_detail" property="addressDetail"/>
        <result column="profile" property="profile"/>
        <result column="status" property="status"/>
        <result column="created_at" property="createdAt"/>
        <result column="province_id" property="provinceId"/>
        <result column="province_name" property="provinceName"/>
        <result column="city_id" property="cityId"/>
        <result column="city_name" property="cityName"/>
        <result column="area_id" property="areaId"/>
        <result column="area_name" property="areaName"/>
    </resultMap>
    
    <!-- 分页查询红娘列表 -->
    <select id="selectMatchmakerPage" resultMap="MatchmakerVOMap">
        SELECT 
            m.matchmaker_id,
            m.real_name,
            m.username,
            m.phone,
            m.email,
            m.gender,
            m.birth_date,
            m.avatar_url,
            m.matchmaker_type,
            m.level,
            m.success_couples,
            m.points,
            m.likes,
            m.address_detail,
            m.profile,
            m.status,
            m.created_at,
            m.province_id,
            p.name AS province_name,
            m.city_id,
            c.name AS city_name,
            m.area_id,
            a.name AS area_name
        FROM matchmakers m
        LEFT JOIN province p ON m.province_id = p.id
        LEFT JOIN city c ON m.city_id = c.id
        LEFT JOIN area a ON m.area_id = a.id
        WHERE 1=1
        <if test="matchmakerType != null">
            AND m.matchmaker_type = #{matchmakerType}
        </if>
        <if test="level != null">
            AND m.level = #{level}
        </if>
        <if test="provinceId != null">
            AND m.province_id = #{provinceId}
        </if>
        <if test="cityId != null">
            AND m.city_id = #{cityId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (m.real_name LIKE CONCAT('%', #{keyword}, '%') 
                 OR m.phone LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        <choose>
            <when test="orderBy == 'level'">
                ORDER BY m.level ${orderType}, m.success_couples DESC
            </when>
            <otherwise>
                ORDER BY m.success_couples ${orderType}, m.level DESC
            </otherwise>
        </choose>
    </select>
    
    <!-- 查询红娘详情 -->
    <select id="selectMatchmakerDetail" resultMap="MatchmakerVOMap">
        SELECT 
            m.matchmaker_id,
            m.real_name,
            m.username,
            m.phone,
            m.email,
            m.gender,
            m.birth_date,
            m.avatar_url,
            m.matchmaker_type,
            m.level,
            m.success_couples,
            m.points,
            m.likes,
            m.address_detail,
            m.profile,
            m.status,
            m.created_at,
            m.province_id,
            p.name AS province_name,
            m.city_id,
            c.name AS city_name,
            m.area_id,
            a.name AS area_name
        FROM matchmakers m
        LEFT JOIN province p ON m.province_id = p.id
        LEFT JOIN city c ON m.city_id = c.id
        LEFT JOIN area a ON m.area_id = a.id
        WHERE m.matchmaker_id = #{matchmakerId}
    </select>
    
    <!-- 根据userId查询红娘详情 -->
    <select id="selectMatchmakerByUserId" resultMap="MatchmakerVOMap">
        SELECT 
            m.matchmaker_id,
            m.real_name,
            m.username,
            m.phone,
            m.email,
            m.gender,
            m.birth_date,
            m.avatar_url,
            m.matchmaker_type,
            m.level,
            m.success_couples,
            m.points,
            m.likes,
            m.address_detail,
            m.profile,
            m.status,
            m.created_at,
            m.province_id,
            p.name AS province_name,
            m.city_id,
            c.name AS city_name,
            m.area_id,
            a.name AS area_name
        FROM matchmakers m
        LEFT JOIN province p ON m.province_id = p.id
        LEFT JOIN city c ON m.city_id = c.id
        LEFT JOIN area a ON m.area_id = a.id
        WHERE m.user_id = #{userId}
    </select>
    
    <!-- 查询红娘排行榜 -->
    <select id="selectRankingList" resultMap="MatchmakerVOMap">
        SELECT 
            m.matchmaker_id,
            m.real_name,
            m.username,
            m.phone,
            m.email,
            m.gender,
            m.birth_date,
            m.avatar_url,
            m.matchmaker_type,
            m.level,
            m.success_couples,
            m.points,
            m.likes,
            m.address_detail,
            m.profile,
            m.status,
            m.created_at,
            m.province_id,
            p.name AS province_name,
            m.city_id,
            c.name AS city_name,
            m.area_id,
            a.name AS area_name
        FROM matchmakers m
        LEFT JOIN province p ON m.province_id = p.id
        LEFT JOIN city c ON m.city_id = c.id
        LEFT JOIN area a ON m.area_id = a.id
        WHERE m.status = 1
        ORDER BY m.success_couples DESC, m.level DESC
        LIMIT #{limit}
    </select>
    
    <!-- 批量查询红娘信息 -->
    <select id="selectBatchMatchmakers" resultMap="MatchmakerVOMap">
        SELECT 
            m.matchmaker_id,
            m.real_name,
            m.phone,
            m.email,
            m.gender,
            m.birth_date,
            m.avatar_url,
            m.matchmaker_type,
            m.level,
            m.success_couples,
            m.address_detail,
            m.profile,
            m.status,
            m.created_at,
            m.province_id,
            p.name AS province_name,
            m.city_id,
            c.name AS city_name,
            m.area_id,
            a.name AS area_name
        FROM matchmakers m
        LEFT JOIN province p ON m.province_id = p.id
        LEFT JOIN city c ON m.city_id = c.id
        LEFT JOIN area a ON m.area_id = a.id
        WHERE m.matchmaker_id IN
        <foreach collection="matchmakerIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>

