<?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.kmall.schedule.dao.QzStoreTicketDiscountMapper">
    <select id="queryStoreTicketDiscountListBy00" resultType="map">
        select
        `tick_disc_id` 'ticketDistId',cc.id 'userCouponId',a.store_topic_id 'storeTopicId'
        from mk_store_ticket_discount a LEFT JOIN mall_user_coupon cc on a.store_topic_id = cc.store_topic_id AND cc.enabled = 1
        where 1=1 AND a.fix_end_time <![CDATA[ < ]]> now() AND a.effect_time_type = '00' AND a.is_valid = 0
    </select>

    <select id="queryStoreTicketDiscountListBy01" resultType="map">
        select
        a.`tick_disc_id` 'ticketDistId',a.postpone_num 'postponeNum',a.valid_day_num 'validDayNum',cc.add_time 'addTime',cc.id 'userCouponId',
        a.ticket_num 'ticketNum',a.limit_num 'limitNum',cc.end_time 'endTime',a.store_topic_id 'storeTopicId'
        from mk_store_ticket_discount a INNER JOIN mall_user_coupon cc on a.store_topic_id = cc.store_topic_id
        where 1=1 AND a.effect_time_type = '01' AND a.is_valid = 0 and cc.enabled = 1 and cc.end_time <![CDATA[ < ]]> now()
    </select>

    <select id="queryStoreTicketDiscountListByLimit" resultType="map">
        select
        a.`tick_disc_id` 'ticketDistId',a.store_topic_id 'storeTopicId'
        from mk_store_ticket_discount a
        where 1=1 AND a.ticket_num <![CDATA[ <= ]]> 0
    </select>

    <update id="updateStoreTicketDiscount" parameterType="map">
        update mk_store_ticket_discount set
        <if test="isValid != null and isValid != ''">
            is_valid = 1,
        </if>
         <if test="isPast != null and isPast != ''">
             is_past = #{isPast},
         </if>
         mod_time = now() where tick_disc_id = #{ticketDistId}
    </update>

    <update id="updateUserCoupon" parameterType="map">
        update mall_user_coupon set enabled = 0,mod_time = now() where id = #{userCouponId}
    </update>

    <select id="selectAdIdByDiscId" resultType="map">
        select
        id 'adId'
        from mall_ad
        where tick_disc_id = #{tickDiscId}
    </select>

    <update id="updateAd" parameterType="map">
        update mall_ad set enabled = #{enabled} where id = #{adId}
    </update>
    <update id="updateStoreTopic" parameterType="map">
        update mall_store_topic set is_valid = 1,mod_time = now() where id = #{storeTopicId}
    </update>
</mapper>