<?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.MyResourceMapper">

    <resultMap id="BaseResultMap" type="com.zhentao.entity.MyResource">
            <id property="resourceId" column="resource_id" jdbcType="INTEGER"/>
            <result property="matchmakerId" column="matchmaker_id" jdbcType="INTEGER"/>
            <result property="name" column="name" jdbcType="VARCHAR"/>
            <result property="age" column="age" jdbcType="INTEGER"/>
            <result property="gender" column="gender" jdbcType="INTEGER"/>
            <result property="constellation" column="constellation" jdbcType="VARCHAR"/>
            <result property="height" column="height" jdbcType="INTEGER"/>
            <result property="weight" column="weight" jdbcType="INTEGER"/>
            <result property="marrStatus" column="marr_status" jdbcType="INTEGER"/>
            <result property="diploma" column="diploma" jdbcType="VARCHAR"/>
            <result property="income" column="income" jdbcType="VARCHAR"/>
            <result property="address" column="address" jdbcType="VARCHAR"/>
            <result property="domicile" column="domicile" jdbcType="VARCHAR"/>
            <result property="occupation" column="occupation" jdbcType="VARCHAR"/>
            <result property="house" column="house" jdbcType="INTEGER"/>
            <result property="phone" column="phone" jdbcType="VARCHAR"/>
            <result property="backupPhone" column="backup_phone" jdbcType="VARCHAR"/>
            <result property="car" column="car" jdbcType="INTEGER"/>
            <result property="mateSelectionCriteria" column="mate_selection_criteria" jdbcType="VARCHAR"/>
            <result property="isUser" column="is_user" jdbcType="INTEGER"/>
            <result property="userId" column="user_id" jdbcType="INTEGER"/>
            <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
            <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        resource_id,matchmaker_id,name,
        age,gender,constellation,
        height,weight,marr_status,
        diploma,income,address,
        domicile,occupation,house,
        phone,backup_phone,car,
        mate_selection_criteria,
        is_user,user_id,
        create_time,update_time
    </sql>

    <!-- 自定义插入方法，确保所有字段（包括null值）都被插入 -->
    <insert id="insertWithNulls" parameterType="com.zhentao.entity.MyResource" useGeneratedKeys="true" keyProperty="resourceId">
        INSERT INTO my_resource (
            matchmaker_id, name, age, gender, constellation,
            height, weight, marr_status, diploma, income,
            address, domicile, occupation, house, phone,
            backup_phone, car, mate_selection_criteria,
            is_user, user_id, create_time, update_time
        ) VALUES (
            #{matchmakerId}, #{name}, #{age}, #{gender}, #{constellation},
            #{height}, #{weight}, #{marrStatus}, #{diploma}, #{income},
            #{address}, #{domicile}, #{occupation}, #{house}, #{phone},
            #{backupPhone}, #{car}, #{mateSelectionCriteria},
            #{isUser}, #{userId}, #{createTime}, #{updateTime}
        )
    </insert>

    <!-- MyResourceVO结果映射 -->
    <resultMap id="MyResourceVOMap" type="com.zhentao.vo.MyResourceVO">
        <id property="resourceId" column="resource_id" jdbcType="INTEGER"/>
        <result property="matchmakerId" column="matchmaker_id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="age" column="age" jdbcType="INTEGER"/>
        <result property="gender" column="gender" jdbcType="INTEGER"/>
        <result property="constellation" column="constellation" jdbcType="VARCHAR"/>
        <result property="height" column="height" jdbcType="INTEGER"/>
        <result property="weight" column="weight" jdbcType="INTEGER"/>
        <result property="marrStatus" column="marr_status" jdbcType="INTEGER"/>
        <result property="diploma" column="diploma" jdbcType="VARCHAR"/>
        <result property="income" column="income" jdbcType="VARCHAR"/>
        <result property="address" column="address" jdbcType="VARCHAR"/>
        <result property="domicile" column="domicile" jdbcType="VARCHAR"/>
        <result property="occupation" column="occupation" jdbcType="VARCHAR"/>
        <result property="house" column="house" jdbcType="INTEGER"/>
        <result property="phone" column="phone" jdbcType="VARCHAR"/>
        <result property="backupPhone" column="backup_phone" jdbcType="VARCHAR"/>
        <result property="car" column="car" jdbcType="INTEGER"/>
        <result property="mateSelectionCriteria" column="mate_selection_criteria" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="avatarUrl" column="avatar_url" jdbcType="VARCHAR"/>
        <result property="isUser" column="is_user" jdbcType="INTEGER"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="rejectReason" column="reject_reason" jdbcType="VARCHAR"/>
        <result property="rewardPoints" column="reward_points" jdbcType="INTEGER"/>
        <result property="isMatch" column="is_match" jdbcType="INTEGER"/>
        <result property="isInvitation" column="is_invitation" jdbcType="INTEGER"/>
        <result property="invitationTime" column="invitation_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <!-- 分页查询资源列表（关联users表获取头像） -->
    <select id="selectResourceListWithAvatar" resultMap="MyResourceVOMap">
        SELECT DISTINCT
            r.resource_id,
            r.matchmaker_id,
            r.name,
            r.age,
            r.gender,
            r.constellation,
            r.height,
            r.weight,
            r.marr_status,
            r.diploma,
            r.income,
            r.address,
            r.domicile,
            r.occupation,
            r.house,
            r.phone,
            r.backup_phone,
            r.car,
            r.mate_selection_criteria,
            r.is_user,
            r.user_id,
            r.status,
            r.reject_reason,
            r.reward_points,
            r.create_time,
            r.update_time,
            u.avatar_url,
            COALESCE(f.is_match, 0) as is_match,
            COALESCE(f.is_invitation, 0) as is_invitation,
            f.invitation_time
        FROM my_resource r
        LEFT JOIN users u ON r.user_id = u.user_id
        LEFT JOIN (
            SELECT f1.resource_id, f1.matchmaker_id, f1.is_match, f1.is_invitation, f1.invitation_time
            FROM user_follow_up f1
            INNER JOIN (
                SELECT resource_id, matchmaker_id, MAX(update_time) as max_update_time
                FROM user_follow_up
                GROUP BY resource_id, matchmaker_id
            ) f2 ON f1.resource_id = f2.resource_id 
                AND f1.matchmaker_id = f2.matchmaker_id 
                AND f1.update_time = f2.max_update_time
        ) f ON r.resource_id = f.resource_id AND r.matchmaker_id = f.matchmaker_id
        <if test="keyword != null and keyword != ''">
            LEFT JOIN my_resource_tag mrt ON r.resource_id = mrt.resource_id
            LEFT JOIN tag t ON mrt.tag_id = t.tag_id AND t.status = 1
        </if>
        WHERE 1=1
        <if test="matchmakerId != null">
            AND r.matchmaker_id = #{matchmakerId}
        </if>
        <if test="isUser != null">
            AND r.is_user = #{isUser}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (r.name LIKE CONCAT('%', #{keyword}, '%') 
                 OR r.phone LIKE CONCAT('%', #{keyword}, '%')
                 OR t.tag_name LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        ORDER BY r.create_time DESC
    </select>
    
    <!-- 根据标签名称分页查询资源列表（关联users表获取头像） -->
    <select id="selectResourceListByTagName" resultMap="MyResourceVOMap">
        SELECT 
            r.resource_id,
            r.matchmaker_id,
            r.name,
            r.age,
            r.gender,
            r.constellation,
            r.height,
            r.weight,
            r.marr_status,
            r.diploma,
            r.income,
            r.address,
            r.domicile,
            r.occupation,
            r.house,
            r.phone,
            r.backup_phone,
            r.car,
            r.mate_selection_criteria,
            r.is_user,
            r.user_id,
            r.status,
            r.reject_reason,
            r.reward_points,
            r.create_time,
            r.update_time,
            u.avatar_url,
            COALESCE(f.is_match, 0) as is_match,
            COALESCE(f.is_invitation, 0) as is_invitation,
            f.invitation_time
        FROM my_resource r
        LEFT JOIN users u ON r.user_id = u.user_id
        LEFT JOIN (
            SELECT f1.resource_id, f1.matchmaker_id, f1.is_match, f1.is_invitation, f1.invitation_time
            FROM user_follow_up f1
            INNER JOIN (
                SELECT resource_id, matchmaker_id, MAX(update_time) as max_update_time
                FROM user_follow_up
                GROUP BY resource_id, matchmaker_id
            ) f2 ON f1.resource_id = f2.resource_id 
                AND f1.matchmaker_id = f2.matchmaker_id 
                AND f1.update_time = f2.max_update_time
        ) f ON r.resource_id = f.resource_id AND r.matchmaker_id = f.matchmaker_id
        INNER JOIN my_resource_tag mrt ON r.resource_id = mrt.resource_id
        INNER JOIN tag t ON mrt.tag_id = t.tag_id
        WHERE t.tag_name = #{tagName} AND t.status = 1
        AND r.is_user = 1
        <if test="excludeMatchmakerId != null">
            AND NOT EXISTS (
                SELECT 1 
                FROM my_resource mr 
                WHERE mr.phone = r.phone 
                AND mr.matchmaker_id = #{excludeMatchmakerId}
            )
        </if>
        <if test="keyword != null and keyword != ''">
            AND (r.name LIKE CONCAT('%', #{keyword}, '%') 
                 OR r.phone LIKE CONCAT('%', #{keyword}, '%'))
        </if>
        ORDER BY r.create_time DESC
    </select>
</mapper>
