<?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.api.dao.mk.ApiMkStoreTicketDiscountMapper">

    <resultMap type="com.kmall.api.entity.mk.MkStoreTicketDiscountVo" id="mkStoreTicketDiscountMap">
        <result property="tickDiscId" column="tick_disc_id"/>
        <result property="tickDiscType" column="tick_disc_type"/>
        <result property="tickDiscSubTitle" column="tick_disc_sub_title"/>
        <result property="merchSn" column="merch_sn"/>
        <result property="thirdMerchSn" column="third_merch_sn"/>
        <result property="storeId" column="store_id"/>
        <result property="storeTopicId" column="store_topic_id"/>
        <result property="name" column="name"/>
        <result property="voucherMoney" column="voucher_money"/>
        <result property="voucherCond" column="voucher_cond"/>
        <result property="discRatio" column="disc_ratio"/>
        <result property="discCond" column="disc_cond"/>
        <result property="exchCond" column="exch_cond"/>
        <result property="effectTimeType" column="effect_time_type"/>
        <result property="fixBegTime" column="fix_beg_time"/>
        <result property="fixEndTime" column="fix_end_time"/>
        <result property="postponeNum" column="postpone_num"/>
        <result property="validDayNum" column="valid_day_num"/>
        <result property="ticketNum" column="ticket_num"/>
        <result property="applyType" column="apply_type"/>
        <result property="getEnter" column="get_enter"/>
        <result property="canShareGet" column="can_share_get"/>
        <result property="limitNum" column="limit_num"/>
        <result property="advImgUrl" column="adv_img_url"/>
        <result property="storeLogoUrl" column="store_logo_url"/>
        <result property="storeName" column="store_name"/>
        <result property="ticketColor" column="ticket_color"/>
        <result property="optDecl" column="opt_decl"/>
        <result property="discDecl" column="disc_decl"/>
        <result property="useNoti" column="use_noti"/>
        <result property="svrTel" column="svr_tel"/>
        <result property="isValid" column="is_valid"/>
        <result property="note" column="note"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
        <result property="sendType" column="send_type"/>
		<result property="discSellCount" column="disc_sell_count"/>
		<result property="discSellDoller" column="disc_sell_doller"/>
		<result property="guid" column="guid"/>
		<result property="userId" column="user_id"/>
		<result property="referrer" column="referrer"/>
		<result property="isUsed" column="is_used"/>
		<result property="isPast" column="is_past"/>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		select
			`tick_disc_id`,
			`tick_disc_type`,
			`tick_disc_sub_title`,
			`merch_sn`,
			`third_merch_sn`,
			`store_id`,goods_biz_type,
			`store_topic_id`,
			`name`,
			`voucher_money`,
			`voucher_cond`,
			`disc_ratio`,
			`disc_cond`,
			`exch_cond`,
			`effect_time_type`,
			`fix_beg_time`,
			`fix_end_time`,
			`postpone_num`,
			`valid_day_num`,
			`ticket_num`,
			`apply_type`,
			`get_enter`,
			`can_share_get`,
			`limit_num`,
			`adv_img_url`,
			`store_logo_url`,
			`store_name`,
			`ticket_color`,
			`opt_decl`,
			`disc_decl`,
			`use_noti`,
			`svr_tel`,
			`is_valid`,
			disc_sell_count,
			disc_sell_doller,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`,
			`send_type`,guid
		from mk_store_ticket_discount d
		where tick_disc_id = #{id}
	</select>

	<!--根据用户id、优惠券id查询优惠券详情信息-->
	<select id="queryDiscObjectByUserId" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		select
		`tick_disc_id`,
		`tick_disc_type`,
		`tick_disc_sub_title`,
		`merch_sn`,
		`third_merch_sn`,
		`store_id`,goods_biz_type,
		`store_topic_id`,
		`name`,
		`voucher_money`,
		`voucher_cond`,
		`disc_ratio`,
		`disc_cond`,
		`exch_cond`,
		`effect_time_type`,
		`fix_beg_time`,
		`fix_end_time`,
		`postpone_num`,
		`valid_day_num`,
		`ticket_num`,
		`apply_type`,
		`get_enter`,
		`can_share_get`,
		`limit_num`,
		`adv_img_url`,
		`store_logo_url`,
		`store_name`,
		`ticket_color`,
		`opt_decl`,
		`disc_decl`,
		`use_noti`,
		`svr_tel`,
		`is_valid`,
		disc_sell_count,
		disc_sell_doller,
		`note`,
		`creater_sn`,
		`create_time`,
		`moder_sn`,
		`mod_time`,
		`tstm`,
		`send_type`,
		guid,
		is_past,
		( SELECT count( * ) FROM mall_user_coupon WHERE store_topic_id = d.store_topic_id AND user_id = #{userId} ) 'receiveCount'
		from mk_store_ticket_discount d
		where tick_disc_id = #{id}
	</select>

	<!--根据用户优惠券id、门店id、用户id查询优惠券详情信息-->
	<select id="queryTickObjectByIdAndStoreIdAndUserId" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		select
		d.`tick_disc_id`,
		d.`tick_disc_type`,
		d.`tick_disc_sub_title`,
		d.`merch_sn`,
		d.`third_merch_sn`,
		d.`store_id`,
		d.goods_biz_type,
		d.`store_topic_id`,
		d.`name`,
		d.`voucher_money`,
		d.`voucher_cond`,
		d.`disc_ratio`,
		d.`disc_cond`,
		d.`exch_cond`,
		d.`effect_time_type`,
		d.`fix_beg_time`,
		d.`fix_end_time`,
		d.`postpone_num`,
		d.`valid_day_num`,
		d.`ticket_num`,
		d.`apply_type`,
		d.`get_enter`,
		d.`can_share_get`,
		d.`limit_num`,
		d.`adv_img_url`,
		d.`store_logo_url`,
		d.`store_name`,
		d.`ticket_color`,
		d.`opt_decl`,
		d.`disc_decl`,
		d.`use_noti`,
		d.`svr_tel`,
		d.`is_valid`,
		d.disc_sell_count,
		d.disc_sell_doller,
		d.`note`,
		d.`creater_sn`,
		d.`create_time`,
		d.`moder_sn`,
		d.`mod_time`,
		d.`tstm`,
		d.`send_type`,d.guid,cc.add_time addTime,cc.valid_time validTime,cc.end_time endTime,cc.id 'couponId',d.is_past,
		( SELECT count( * ) FROM mall_user_coupon WHERE store_topic_id = d.store_topic_id AND user_id = #{userId} ) 'receiveCount'
		from mk_store_ticket_discount d LEFT JOIN mall_user_coupon cc on d.store_topic_id = cc.store_topic_id
		where cc.id = #{couponId} and store_id = #{storeId}
	</select>

	<select id="queryList" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		select
    		`tick_disc_id`,
    		`tick_disc_type`,
    		`tick_disc_sub_title`,
    		`merch_sn`,
    		`third_merch_sn`,
    		`store_id`,goods_biz_type,
    		`store_topic_id`,
    		`name`,
    		`voucher_money`,
    		`voucher_cond`,
    		`disc_ratio`,
    		`disc_cond`,
    		`exch_cond`,
    		`effect_time_type`,
    		`fix_beg_time`,
    		`fix_end_time`,
    		`postpone_num`,
    		`valid_day_num`,
    		`ticket_num`,
    		`apply_type`,
    		`get_enter`,
    		`can_share_get`,
    		`limit_num`,
    		`adv_img_url`,
    		`store_logo_url`,
    		`store_name`,
    		`ticket_color`,
    		`opt_decl`,
    		`disc_decl`,
    		`use_noti`,
    		`svr_tel`,
    		`is_valid`,
			disc_sell_count,
			disc_sell_doller,
    		`note`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`,
    		`send_type`,guid
		from mk_store_ticket_discount
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
		<if test="storeId != null and storeId != ''">
			AND store_id = #{storeId}
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by tick_disc_id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from mk_store_ticket_discount
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
		<if test="storeId != null and storeId != ''">
			AND store_id = #{storeId}
		</if>
	</select>

	<!--查询用户已领取的优惠券信息-->
	<select id="queryStoreDiscountByUserId" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		SELECT
			d.*,
			a.user_id,
			a.referrer,
			a.is_used,a.add_time 'addTime',
			a.valid_time 'validTime',a.id 'couponId'
		FROM
			mk_store_ticket_discount d
			INNER JOIN mall_user_coupon a ON a.store_topic_id = d.store_topic_id
			INNER JOIN third_merchant_biz b on d.third_merch_sn = b.third_merch_sn
		WHERE
			1 = 1 and d.is_valid = 0
		<if test="merchSn != null">
			and b.merch_sn = #{merchSn}
		</if>
		<if test="couponNumber != null">
			and a.coupon_number = #{couponNumber}
		</if>
		<if test="userId != null">
			and a.user_id = #{userId}
		</if>
		<if test="storeId != null">
			and d.store_id = #{storeId}
		</if>
		<if test="storeTopicId != null">
			and d.store_topic_id = #{storeTopicId}
		</if>
		<if test="tickDiscId != null">
			and d.tick_disc_id = #{tickDiscId}
		</if>
		<if test="unUsed != null and unUsed == true">
			and (a.order_id is null or a.order_id =0)
		</if>
		<if test="referrer != null">
			and a.referrer = #{referrer}
		</if>
		<if test="sourceKey != null">
			and a.source_key = #{sourceKey}
		</if>
		<if test="showState != null">
			and a.show_state = #{showState}
		</if>
		<if test="isUsed != null and isUsed == true">
			and a.is_used = 0
		</if>
		<if test="isUsed != null and isUsed == false">
			and a.is_used = 1
		</if>
		<if test="enabled != null and enabled == true">
			and a.enabled = 1 and a.end_time > NOW()
		</if>
		<if test="enabled != null and enabled == false">
			and a.enabled = 0 and a.end_time <![CDATA[ < ]]> NOW()
		</if>
	</select>

	<!--详情页根据门店id、用户id、业务类型获取当前门店所有的优惠券-->
	<!--优惠券页查询门店id可领取的优惠券信息-->
	<select id="getDiscountByStoreIdList" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		SELECT
		d.*,
		( SELECT count( * ) FROM mall_user_coupon WHERE store_topic_id = d.store_topic_id AND user_id = #{userId} ) 'receiveCount'
		FROM
		mk_store_ticket_discount d
		WHERE
		tick_disc_id IN (
		SELECT
		d.tick_disc_id
		FROM
		mk_store_ticket_discount d
		LEFT JOIN mall_user_coupon c ON d.store_topic_id = c.store_topic_id
		AND c.user_id = #{userId}
		WHERE
		1 = 1
		AND d.ticket_num >= 0
		AND d.is_valid = 0
		<if test="storeId != null">
			and d.store_id = #{storeId}
		</if>
		<if test="storeTopicId != null">
			and d.store_topic_id = #{storeTopicId}
		</if>
		<if test="tickDiscId != null">
			and d.tick_disc_id = #{tickDiscId}
		</if>
		<if test="isUsed != null">
			and c.is_used = #{isUsed}
		</if>
		<if test="goodsBizType != null">
			and d.goods_biz_type = #{goodsBizType}
		</if>
		<if test="isPast != null">
			AND d.is_past = #{isPast}
		</if>
		<if test="notIsPast != null">
			AND d.is_past <![CDATA[ <> ]]> #{notIsPast}
		</if>
		)
	</select>
	<select id="getTotalByDiscountByStoreId" resultType="int">
		SELECT count(*)
		FROM
		mk_store_ticket_discount d
		WHERE
		tick_disc_id IN (
		SELECT
		d.tick_disc_id
		FROM
		mk_store_ticket_discount d
		LEFT JOIN mall_user_coupon c ON d.store_topic_id = c.store_topic_id
		AND c.user_id = #{userId}
		WHERE
		1 = 1
		AND d.ticket_num >= 0
		AND d.is_valid = 0
		<if test="storeId != null">
			and d.store_id = #{storeId}
		</if>
		<if test="storeTopicId != null">
			and d.store_topic_id = #{storeTopicId}
		</if>
		<if test="tickDiscId != null">
			and d.tick_disc_id = #{tickDiscId}
		</if>
		<if test="isUsed != null">
			and c.is_used = #{isUsed}
		</if>
		<if test="goodsBizType != null">
			and d.goods_biz_type = #{goodsBizType}
		</if>
		<if test="isPast != null">
			AND d.is_past = #{isPast}
		</if>
		)
	</select>

	<update id="update" parameterType="com.kmall.admin.entity.mk.store.MkStoreTicketDiscountEntity">
		update mk_store_ticket_discount
		<set>
			<if test="ticketNum != null">`ticket_num` = #{ticketNum}</if>
		</set>
		where tick_disc_id = #{tickDiscId}
	</update>
	<!--获取购物车中根据门店id、用户id、业务类型，获取is_used已领取、is_valid有效、enabled可用、ticket_num有库存、isPast未过期、且失效时间没超时的优惠券-->
	<select id="getDiscountByCartGoodsIdList" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		SELECT
		d.*,
		c.user_id,
		c.referrer,
		c.is_used,
		c.enabled,c.add_time 'addTime',
		(select count(*) from mall_user_coupon where store_topic_id = d.store_topic_id AND user_id = #{userId} ) 'receiveCount'
		FROM
		mk_store_ticket_discount d
		LEFT JOIN mall_user_coupon c ON d.store_topic_id = c.store_topic_id AND c.user_id = #{userId}
		WHERE
		1 = 1 AND d.ticket_num >= 0 AND d.is_valid = 0 AND c.enabled = 1 AND c.valid_time <![CDATA[ < ]]> now()
		<if test="storeId != null">
			AND d.store_id = #{storeId}
		</if>
		<if test="storeTopicId != null">
			AND d.store_topic_id = #{storeTopicId}
		</if>
		<if test="tickDiscId != null">
			AND d.tick_disc_id = #{tickDiscId}
		</if>
		<if test="isUsed != null">
			AND c.is_used = #{isUsed}
		</if>
		<if test="goodsBizType != null">
			AND d.goods_biz_type = #{goodsBizType}
		</if>
		<if test="isPast != null">
			AND d.is_past = #{isPast}
		</if>
	</select>

	<select id="getDiscountByUserCouponId" resultType="com.kmall.api.entity.mk.MkStoreTicketDiscountVo">
		select
		d.`tick_disc_id`,
		d.`tick_disc_type`,
		d.`tick_disc_sub_title`,
		d.`merch_sn`,
		d.`third_merch_sn`,
		d.`store_id`,
		d.goods_biz_type,
		d.`store_topic_id`,
		d.`name`,
		d.`voucher_money`,
		d.`voucher_cond`,
		d.`disc_ratio`,
		d.`disc_cond`,
		d.`exch_cond`,
		d.`effect_time_type`,
		d.`fix_beg_time`,
		d.`fix_end_time`,
		d.`postpone_num`,
		d.`valid_day_num`,
		d.`ticket_num`,
		d.`apply_type`,
		d.`get_enter`,
		d.`can_share_get`,
		d.`limit_num`,
		d.`adv_img_url`,
		d.`store_logo_url`,
		d.`store_name`,
		d.`ticket_color`,
		d.`opt_decl`,
		d.`disc_decl`,
		d.`use_noti`,
		d.`svr_tel`,
		d.`is_valid`,
		d.disc_sell_count,
		d.disc_sell_doller,
		d.`note`,
		d.`send_type`,
		d.guid
		from mk_store_ticket_discount d
		LEFT JOIN mall_user_coupon c ON d.store_topic_id = c.store_topic_id
		WHERE
			c.id = #{userCouponId}
	</select>
</mapper>