<?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.kmall.api.dao.ApiCouponMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.CouponVo" id="couponMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="type_money" column="type_money"/>
        <result property="send_type" column="send_type"/>
        <result property="min_amount" column="min_amount"/>
        <result property="max_amount" column="max_amount"/>
        <result property="send_start_date" column="send_start_date"/>
        <result property="send_end_date" column="send_end_date"/>
        <result property="use_start_date" column="use_start_date"/>
        <result property="use_end_date" column="use_end_date"/>
        <result property="min_goods_amount" column="min_goods_amount"/>
        <result property="coupon_txt" column="coupon_txt"/>
        <result property="user_id" column="user_id"/>
        <result property="coupon_number" column="coupon_number"/>
        <result property="min_transmit_num" column="min_transmit_num"/>
        <result property="show_state" column="show_state"/>
        <result property="pic_url" column="pic_url"/>
        <result property="isAll" column="is_all"/>
        <result property="couponUrl" column="coupon_url"/>
    </resultMap>

    <select id="queryObject" resultMap="couponMap">
		select * from mall_coupon where id = #{value}
	</select>

    <select id="queryList" resultMap="couponMap">
        select * from mall_coupon a
        where 1 = 1
        <if test="store_id != null">
            and a.`store_id` = #{store_id}
        </if>
        <if test="send_types != null">
            and send_type in
            <foreach item="item" collection="send_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="enabled != null and enabled == true">
            and a.use_end_date >= now()
            and now() >= a.use_start_date
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_coupon a
        where 1 = 1
        <if test="send_types != null">
            and send_type in
            <foreach item="item" collection="send_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="store_id != null">
            and a.`store_id` = #{store_id}
        </if>
        <if test="enabled != null and enabled == true">
            and a.use_end_date >= now()
            and now() >= a.use_start_date
        </if>
    </select>

    <select id="queryUserCoupons" resultMap="couponMap">
        select a.*,b.coupon_number,b.user_id
        from mall_coupon a
        left join mall_user_coupon b on a.id = b.coupon_id
        where 1 = 1
        <if test="user_id != null">
            and b.`user_id` = #{user_id}
        </if>
        <if test="store_id != null">
            and a.`store_id` = #{store_id}
        </if>
        <if test="send_type != null">
            and a.send_type = #{send_type}
        </if>
        <if test="coupon_number != null">
            and b.coupon_number = #{coupon_number}
        </if>
        <if test="source_key != null and source_key != ''">
            and b.source_key = #{source_key}
        </if>
        <if test="unUsed != null and unUsed == true">
            and b.used_time is null and (b.order_id is null or b.order_id =0)
        </if>
        <if test="unUsed != null and unUsed == true">
            and (b.order_id is null or b.order_id =0)
        </if>
        <if test="unObtain != null and unObtain == true">
            and b.id is null
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryMaxUserEnableCoupon" resultMap="couponMap">
        select a.*
        from mall_coupon a
        where 1 = 1
        and a.use_end_date >= now()
        and now() >= a.use_start_date
        <if test="send_type != null">
            and a.send_type = #{send_type}
        </if>
        <if test="send_types != null">
            and a.send_type in
            <foreach item="item" collection="send_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="filterCouponIds != null">
            and a.`id` not in
            <foreach item="item" collection="filterCouponIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        order by type_money desc
        limit 1
    </select>
</mapper>