<?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.xxfc.platform.activity.mapper.UserCouponMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.xxfc.platform.activity.vo.UserCouponVo" id="userCouponMap">
        <result property="couponId" column="coupon_id"/>
        <result property="userId" column="user_id"/>
        <result property="tickerNo" column="ticker_no"/>
        <result property="isUse" column="is_use"/>
        <result property="orderNo" column="order_no"/>
        <result property="crtTime" column="crt_time"/>
        <result property="startTime" column="start_time"/>
        <result property="expireTime" column="expire_time"/>
        <result property="useTime" column="use_time"/>
        <result property="title" column="title"/>
        <result property="icon" column="icon"/>
        <result property="used" column="used"/>
        <result property="channel" column="channel"/>
        <result property="userType" column="user_type"/>
        <result property="type" column="type"/>
        <result property="withSpecial" column="with_special"/>
        <result property="withSn" column="with_sn"/>
        <result property="withAmount" column="with_amount"/>
        <result property="usedAmount" column="used_amount"/>
        <result property="status" column="status"/>
        <result property="url" column="url"/>
    </resultMap>


    <select id="getUserCouponsByType" resultMap="userCouponMap">
         SELECT
        *
    FROM
        user_coupon u
    LEFT JOIN coupon c ON u.coupon_id = c.id
    WHERE
        u.is_del = 0  and u.user_id=#{userId}
        <if test="type==0">
           and u.is_use=0 and  u.expire_time >=#{time}
        </if>
        <if test="type==1">
            and u.is_use=1
        </if>
        <if test="type==2">
            and u.is_use=0 and  u.expire_time &lt;#{time}
        </if>
        <choose>
            <when test="type==1">
                ORDER BY u.use_time DESC
            </when>
            <otherwise>
                ORDER BY c.used_amount DESC
            </otherwise>

        </choose>

    </select>

    <select id="getUserCoupon" resultMap="userCouponMap">
        SELECT
        *
        FROM
        user_coupon u
        LEFT JOIN coupon c ON u.coupon_id = c.id
        WHERE
        u.is_del = 0  and u.ticker_no=#{tickerNo} limit 1
    </select>

</mapper>