<?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.yami.shop.coupon.comment.dao.CouponUserMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.coupon.comment.model.CouponUser">
        <!--
          WARNING - @mbg.generated
        -->
        <result column="coupon_user_id" jdbcType="BIGINT" property="couponUserId"/>
        <result column="coupon_id" jdbcType="BIGINT" property="couponId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="receive_time" jdbcType="TIMESTAMP" property="receiveTime"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="user_start_time" jdbcType="TIMESTAMP" property="userStartTime"/>
        <result column="user_end_time" jdbcType="TIMESTAMP" property="userEndTime"/>
        <result column="is_delete" jdbcType="TINYINT" property="isDelete"/>
    </resultMap>

    <resultMap id="CouponAndCouponUserMap" type="com.yami.shop.coupon.comment.model.CouponUser">
        <result column="coupon_user_id" jdbcType="BIGINT" property="couponUserId"/>
        <result column="coupon_id" jdbcType="BIGINT" property="couponId"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="receive_time" jdbcType="TIMESTAMP" property="receiveTime"/>
        <result column="status" jdbcType="TINYINT" property="status"/>
        <result column="user_start_time" jdbcType="TIMESTAMP" property="userStartTime"/>
        <result column="user_end_time" jdbcType="TIMESTAMP" property="userEndTime"/>
        <association property="coupon" javaType="com.yami.shop.coupon.comment.model.Coupon">
            <result column="coupon_id" jdbcType="BIGINT" property="couponId"/>
            <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
            <result column="coupon_name" jdbcType="VARCHAR" property="couponName"/>
            <result column="sub_title" jdbcType="VARCHAR" property="subTitle"/>
            <result column="coupon_type" jdbcType="TINYINT" property="couponType"/>
            <result column="cash_condition" jdbcType="DECIMAL" property="cashCondition"/>
            <result column="reduce_amount" jdbcType="DECIMAL" property="reduceAmount"/>
            <result column="coupon_discount" jdbcType="DECIMAL" property="couponDiscount"/>
            <result column="valid_time_type" jdbcType="TINYINT" property="validTimeType"/>
            <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
            <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
            <result column="after_receive_days" jdbcType="INTEGER" property="afterReceiveDays"/>
            <result column="valid_days" jdbcType="INTEGER" property="validDays"/>
            <result column="stocks" jdbcType="INTEGER" property="stocks"/>
            <result column="suitable_prod_type" jdbcType="TINYINT" property="suitableProdType"/>
            <result column="limit_num" jdbcType="INTEGER" property="limitNum"/>
            <result column="version" jdbcType="INTEGER" property="version"/>
            <result column="overdue_status" jdbcType="TINYINT" property="overdueStatus"/>
            <result column="puton_status" jdbcType="TINYINT" property="putonStatus"/>
            <result column="shop_name" jdbcType="VARCHAR" property="shopName"/>
        </association>
    </resultMap>

    <select id="getCouponAndCouponUserByUserId" resultMap="CouponAndCouponUserMap">
        select cu.*,c.*,sd.shop_name from tz_coupon_user cu
        left join tz_coupon c on cu.coupon_id = c.coupon_id
        left join tz_shop_detail sd on c.shop_id = sd.shop_id
        where cu.user_id = #{userId} and (cu.status = 0 or cu.status = 1)
   </select>

    <select id="getCouponAndCouponUserByCouponUserIds" resultMap="CouponAndCouponUserMap">
        select cu.*,c.* from tz_coupon_user cu
        left join tz_coupon c on cu.coupon_id = c.coupon_id
        where cu.coupon_user_id in
        <foreach collection="couponUserIds" item="couponUserId" separator="," open="(" close=")">
            #{couponUserId}
        </foreach>
        and cu.status = 1 and c.overdue_status = 1 and c.puton_status = 1
    </select>

    <delete id="deleteUnValidTimeCoupons">
    delete from tz_coupon_user where user_end_time &lt; #{date} and status = 0
     </delete>

    <update id="updateStatusByTime">
    update tz_coupon_user set status = 0 where user_end_time &lt; #{now}
   </update>

    <update id="deleteUserCoupon">
        UPDATE tz_coupon_user cu SET cu.is_delete =1 WHERE cu.user_id =#{userId} AND cu.coupon_id = #{couponId}
    </update>

    <update id="updateUseStatusByCouponUserId">
        UPDATE tz_coupon_user SET `status` = #{status} WHERE coupon_user_id = #{couponUserId}
    </update>

    <update id="batchUpdateUserCouponStatus">
        UPDATE tz_coupon_user SET `status` = #{status}
        WHERE coupon_user_id IN
        <foreach collection="couponUserIds" item="userCouponId" open="(" close=")" separator=",">
            #{userCouponId}
        </foreach>
    </update>

    <update id="updateStatusByOrderNumber">
        UPDATE tz_coupon_user SET `status` = #{status}
        WHERE order_number = #{orderNumber}
    </update>


</mapper>
