<?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.UserBenefitsOrderRecordMapper">

    <select id="selectUseComboRecordByUid" resultType="java.lang.Integer">
        select count(1) from (select order_id from user_benefits_order_record where goods_id = #{goodsId}
        <if test="skuId != null">
            and sku_id = #{skuId}
        </if>
        and type = #{type}
        and user_id in
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="startTime != null">
            and created_time > #{startTime}
        </if>
        ) bu inner join order_don o on o.id = bu.order_id
        where o.status not in
        <foreach collection="orderStatus" item="status" open="(" separator="," close=")">
            #{status}
        </foreach>
        <if test="lastYear != null">
            and o.created_time >= #{lastYear}
        </if>
        <if test="relationId != null">
            and o.relation_id = #{relationId}
        </if>
    </select>

    <select id="selectNoPaymentRecord" resultType="java.lang.Integer">
        select count(1) from (select order_id from user_benefits_order_record where benefits_id = #{benefitsId}
        and user_id in
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>) bu inner join order_don o on o.id = bu.order_id
        where o.status = 'noPayment'
    </select>
</mapper>