<?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="co.yixiang.yshop.module.product.dal.mysql.productcouponrelation.ProductCouponRelationMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.yixiang.co/MyBatis/x-plugins/
     -->

    <select id="searchUserCoupon"
            resultType="co.yixiang.yshop.module.product.controller.app.couponrelation.vo.UserCouponVO">
        select yspc.*,yspcr.receive_time,yspcr.id as detailId
        from yshop_store_product_coupon_relation as yspcr
        inner join yshop_store_product_coupon as yspc
        on yspcr.coupon_id = yspc.id
        where yspcr.uid = #{uid}
        and yspc.deleted = 0
        <if test="id != null and id != 0">
            and yspcr.id = #{id}
        </if>
        <if test="type == 1">
            and yspcr.is_used = 0
            and ((yspc.taking_effect_time &lt; now() and yspc.expiration_time &gt; now())
            or (DATE_ADD(yspcr.receive_time, INTERVAL yspc.expiration_day DAY) &gt; now()))
        </if>
        <if test="type == 2">
            and yspcr.is_used = 1
        </if>
        <if test="type == 3">
            and yspcr.is_used = 0
            and (yspc.expiration_time &lt; now()
            or (DATE_ADD(yspcr.receive_time, INTERVAL yspc.expiration_day DAY) &lt; now()))
        </if>

    </select>

    <select id="getCouponNumber" resultType="java.lang.Integer">
        select count(0)
        from yshop_store_product_coupon_relation as yspcr
        inner join yshop_store_product_coupon as yspc
        on yspcr.coupon_id = yspc.id
        where yspcr.uid = #{userId}
        and yspc.deleted = 0
        and yspcr.is_used = 0
        and ((yspc.taking_effect_time &lt; now() and yspc.expiration_time &gt; now())
        or (DATE_ADD(yspcr.receive_time, INTERVAL yspc.expiration_day DAY) &gt; now()))

    </select>

</mapper>
