<?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.cyksj.mapper.manage.coupon.CouponRecommendMapper">

    <select id="getRecommendCouponViewList" resultType="com.cyksj.model.views.CouponRecommendView">
        select * from (select cr.id,
        cr.coupon_id,
        c.name,
        c.goods_type,
        c.type,
        c.reduce_money,
        c.discount,
        c.goods_sku_ids,
        c.is_valid_time,
        c.valid_start_time,
        c.valid_end_time,
        cu.valid_start_time as userValidStartTime,
        cu.valid_end_time as userValidEndTime,
        c.valid_days,
        c.valid_hours,
        c.is_renew,
        c.scope,
        (case
        when cu.id is null then 'collecting'
        when cu.id is not null and cu.status != 'unused' then 'used'
        when cu.id is not null and cu.valid_end_time &lt; now() then 'expired'
        when cu.id is not null and cu.valid_start_time > now() then 'noEffective'
        when cu.id is not null and cu.status = 'unused' then 'received'
        end) as couponStatus,
        (case
        when cu.id is null then 0
        when cu.id is not null and cu.status = 'unused' then 1
        end) as `order`,
        if(c.is_valid_time is true and c.valid_end_time &lt; now(),false,true) as flag
        from coupon_recommend cr left join coupon c on c.id = cr.coupon_id
        left join coupon_user cu on cu.coupon_id = c.id and cu.user_id = #{userId}
        where cr.deleted is true and c.deleted is true and cr.status = 1
        and (cr.up_time is null or cr.up_time &lt;= now()) and (cr.down_time is null or cr.down_time > now())
        <if test="couponId != null">
            and c.id = #{couponId}
        </if>
        <if test='!isNewUser'>
            and c.scope !='newUser'
        </if>)s where couponStatus not in ('used','expired','noEffective') and flag is true order by `order`
    </select>

    <select id="getRecommendCouponList" resultType="com.cyksj.model.views.CouponAvailableRecommendView">
        select * from (select c.id as couponId,
                              c.scope,
                              cr.id as recommendId,
        if(c.is_valid_time is true and c.valid_end_time &lt; now(),false,true) as flag
        from coupon_recommend cr left join coupon c on c.id = cr.coupon_id
        left join coupon_user cu on cu.coupon_id = c.id and cu.user_id = #{userId}
        where cr.deleted is true and c.deleted is true and cr.status is true and cu.coupon_id is null
        and (cr.up_time is null or cr.up_time &lt;= now()) and (cr.down_time is null or cr.down_time > now())
        <if test='!isNewUser'>
            and c.scope !='newUser'
        </if>)s where flag is true
    </select>
</mapper>