<?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.gree.mall.miniapp.commonmapper.CustomCoupouMapper">
    <select id="listCoupou" resultType="com.gree.mall.miniapp.bean.coupon.CustomCoupouBean">
        Select t.*
        from ((SELECT uc.*,
        (uc.order_amount &lt;= #{orderAmount} and (uc.transfer_type = 0 or (uc.transfer_type = 1 and uc.left_share_times>0))) as useable_flag
        FROM user_coupon uc
        WHERE uc.user_id = #{userId}
        and uc.STATUS = 0
        and uc.coupon_type = 'SATISFY'
        AND uc.state = 1
        and now() BETWEEN uc.active_start_time and uc.active_end_time)
        union
        (
        select uc.*,if((uc.transfer_type = 0 or (uc.transfer_type = 1 and uc.left_share_times>0))
        <if test="goodsIds!=null and goodsIds.size>0">
            and count(if(cg.goods_id in
            <foreach collection="goodsIds" item="item" open="(" separator="," close=")">#{item}</foreach>
            ,1,null) )>0
        </if>
        <if test="goodsIds==null or goodsIds.size&lt;1">
            and 1=0
        </if>
        ,1,0) as useable_flag
        FROM user_coupon uc
        left join coupon_goods cg on cg.coupon_id = uc.coupon_id
        WHERE uc.user_id = #{userId}
        and uc.STATUS = 0
        and uc.coupon_type = 'GOODS'
        AND uc.state = 1
        and now() BETWEEN uc.active_start_time and uc.active_end_time
        group by uc.id
        )) t
        ORDER BY t.order_amount desc
    </select>

<!--    if(-->
<!--    (select count(1) from coupon_goods where goods_id in ('1442782181161480194','1457557166946021377') and coupon_id=uc.coupon_id)>0,1,0-->
<!--    ),-->
<!--    uc.*-->
<!--    from user_coupon uc-->
<!--    where user_id = '1460858516945534977' and uc.coupon_type = 'GOODS'-->
<!--    and uc.STATUS = 0-->
<!--    AND uc.state = 1 and now() BETWEEN uc.active_start_time and uc.active_end_time-->

    <!--0/1/2  未到领取时间/可领/已领取可用(分享券剩余0时不可用)-->
    <select id="listObtainCoupou" resultType="com.gree.mall.miniapp.bean.coupon.CouponObtainBean">
        (SELECT cp.*, 2 as obtainType
         from coupon cp
                  inner join user_coupon uc on uc.coupon_id = cp.coupon_id
         where uc.status = 0
           and uc.state = 1
           and uc.user_id = #{userId}
           and (now() BETWEEN uc.active_start_time AND uc.active_end_time)
           and now() > cp.display_time
           and now() &lt; cp.obtain_end_time
           and if(uc.transfer_type = 1 and uc.left_share_times&lt;1,0,1)
           and cp.company_wechat_id = #{companyWechatId}
         group by cp.coupon_id
         ORDER BY cp.create_time Desc)
        UNION
        (SELECT cp.*, 0 as obtainType
         from coupon cp
                  LEFT JOIN coupon_user cu ON cp.coupon_id = cu.coupon_id
                  left JOIN user ur on ur.user_id = cu.user_id
         WHERE (cp.receive_crowd = 0 or (cu.user_id = #{userId} and cp.receive_crowd &lt;> 0))
           and cp.left_amount > 0
           and cp.flag &lt;> 'CANCEL'
           and (now() &lt; cp.obtain_start_time OR now() > cp.obtain_end_time)
           and now() > cp.display_time
           and now() &lt; cp.obtain_end_time
           and cp.company_wechat_id = #{companyWechatId}
         group by cp.coupon_id)
        UNION
        (SELECT cp.*,
                1 as obtainType
         from coupon cp
                  LEFT JOIN coupon_user cu on cp.coupon_id = cu.coupon_id
                  left join user_coupon s on s.coupon_id = cp.coupon_id
         WHERE (cp.receive_crowd = 0 or (cu.user_id = #{userId} and cp.receive_crowd &lt;> 0))
           and cp.left_amount > 0
           and cp.flag &lt;> 'CANCEL'
           and now() BETWEEN cp.obtain_start_time AND cp.obtain_end_time
           AND now() > cp.display_time
           and now() &lt; cp.obtain_end_time
           and cp.company_wechat_id = #{companyWechatId}
         group by cp.coupon_id
         having count(if(s.user_id = #{userId}, s.user_id, null)) &lt; cp.receive_limit_count
             or cp.receive_limit_count = 0)
    </select>


</mapper>