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

    <resultMap id="BaseResultMap" type="com.zhentao.entity.UserVip">
            <id property="id" column="id" />
            <result property="userId" column="user_id" />
            <result property="packageId" column="package_id" />
            <result property="startTime" column="start_time" />
            <result property="endTime" column="end_time" />
            <result property="durationDays" column="duration_days" />
            <result property="paymentAmount" column="payment_amount" />
            <result property="paymentMethod" column="payment_method" />
            <result property="orderNo" column="order_no" />
            <result property="status" column="status" />
            <result property="source" column="source" />
            <result property="createTime" column="create_time" />
            <result property="paymentTime" column="payment_time" />
    </resultMap>

    <resultMap id="UserVipResultMap" type="com.zhentao.entity.UserVip">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="package_id" property="packageId"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="duration_days" property="durationDays"/>
        <result column="payment_amount" property="paymentAmount"/>
        <result column="payment_method" property="paymentMethod"/>
        <result column="order_no" property="orderNo"/>
        <result column="status" property="status"/>
        <result column="source" property="source"/>
        <result column="create_time" property="createTime"/>
        <result column="payment_time" property="paymentTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,user_id,package_id,start_time,end_time,duration_days,
        payment_amount,payment_method,order_no,status,source,
        create_time,payment_time
    </sql>
    <!-- 查询用户最近的指定套餐订单 -->
    <select id="selectLatestByUserIdAndPackageId" resultMap="UserVipResultMap">
        SELECT * FROM user_vip
        WHERE user_id = #{userId}
          AND package_id = #{packageId}
        ORDER BY create_time DESC
            LIMIT 1
    </select>

    <!-- 按用户ID查询VIP信息 -->
    <select id="selectByUserId" resultMap="UserVipResultMap">
        SELECT * FROM user_vip WHERE user_id = #{userId}
        ORDER BY end_time DESC
            LIMIT 1
    </select>
</mapper>
