<?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.czg.service.account.mapper.ShopActivateCouponRecordMapper">

    <select id="queryReceive" resultType="com.czg.account.vo.CouponReceiveVo">
        SELECT
        record.id,
        vip.id as userId ,
        vip.`nick_name`,
        vip.phone as phone,
        record.create_time as receiveTime,
        record.update_time as useTime,
        record.source as source
        FROM
        tb_shop_activate_coupon_record record
        LEFT JOIN tb_shop_user vip
        ON record.shop_user_id = vip.id AND vip.shop_id = #{param.shopId}
        WHERE
        record.coupon_id = #{param.couponId}
        and record.shop_id = #{param.shopId}
        <if test="param.value != null and param.value != '' ">
            and (vip.name like concat('%', #{param.value}, '%') or vip.telephone like concat('%', #{param.value}, '%'))
        </if>
        <if test="param.status != null">
            and record.status = #{param.status}
        </if>
        <if test="param.startTime != null ">
            and record.create_time &gt; #{param.startTime}
        </if>
        <if test="param.endTime != null ">
            and record.create_time &lt; #{param.endTime}
        </if>
        order by record.create_time desc
    </select>
    <select id="findByUser" resultType="com.czg.account.entity.ShopActivateCouponRecord">
        select tb_shop_activate_coupon_record.*,tb_shop_info.shop_name
        from tb_shop_activate_coupon_record
        left join tb_shop_info on tb_shop_activate_coupon_record.shop_id = tb_shop_info.id
        where tb_shop_activate_coupon_record.shop_user_id in
        <foreach collection="shopUserIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        <if test="status != null">
            and tb_shop_activate_coupon_record.status = #{status}
        </if>
        order by tb_shop_activate_coupon_record.status , tb_shop_activate_coupon_record.create_time desc
    </select>


    <select id="queryByVipIdAndShopId" resultType="com.czg.account.vo.UserCouponVo">
        SELECT
        inRecord.id as id,
        inRecord.shop_id as shopId,
        shop.shop_name as shopName,
        inRecord.full_amount as fullAmount,
        inRecord.discount_amount as discountAmount,
        inRecord.coupon_id as couponId,
        pro.id as proId,
        pro.name as productName,
        inRecord.name as name,
        inRecord.type,
        inRecord.use_end_time as endTime
        FROM
        tb_shop_activate_coupon_record inRecord
        LEFT JOIN tb_shop_info shop ON inRecord.shop_id = shop.id
        LEFT JOIN tb_product pro ON inRecord.pro_id = pro.id and pro.shop_id = #{shopId}
        WHERE
        inRecord.shop_user_id = #{shopUserId}
        <if test="shopId != null and shopId != ''">
            and inRecord.shop_id = #{shopId}
        </if>
        <if test="type != null ">
            and inRecord.type = #{type}
        </if>
        and inRecord.status = 0
        and inRecord.use_start_time &lt; now()
        and inRecord.use_end_time &gt; now()
        order by inRecord.use_end_time
    </select>
</mapper>
