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

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.UserCouponVo" id="userCouponMap">
        <result property="id" column="id"/>
        <result property="storeTopicId" column="store_topic_id"/>
        <result property="couponName" column="coupon_name"/>
        <result property="couponNumber" column="coupon_number"/>
        <result property="userId" column="user_id"/>
        <result property="usedTime" column="used_time"/>
        <result property="addTime" column="add_time"/>
        <result property="orderId" column="order_id"/>
        <result property="sourceKey" column="source_key"/>
        <result property="referrer" column="referrer"/>
        <result property="showState" column="show_state"/>
        <result property="endTime" column="end_time"/>
        <result property="typeMoney" column="type_money"/>
        <result property="sendType" column="send_type"/>
        <!--<result property="min_goods_amount" column="min_goods_amount"/>-->
        <result property="isAll" column="is_all"/>
        <result property="couponUrl" column="coupon_url"/>
        <result property="isUsed" column="is_used"/>
        <result property="merchSn" column="merch_sn"/>
        <result property="advImgUrl" column="adv_img_url"/>
        <result property="dicountName" column="dicountName"/>
        <result property="applyType" column="applyType"/>
        <result property="validTime" column="valid_time"/>
    </resultMap>

    <select id="queryObject" resultMap="userCouponMap">
		select * from mall_user_coupon where id = #{value}
	</select>

    <select id="queryObjectByStoreTopicIdAndUserId" resultMap="userCouponMap">
        select * from mall_user_coupon where user_id = #{userId} and store_topic_id = #{storeTopicId}
    </select>

    <select id="queryObjectByDiscIdAndUserId" resultMap="userCouponMap">
        SELECT
            c.*
        FROM
            mall_user_coupon c
            INNER JOIN mk_store_ticket_discount d ON c.store_topic_id = d.store_topic_id
        WHERE user_id = #{userId} and d.tick_disc_id = #{ticketDiscId}
    </select>
    <!--查询未使用且未过期，生效时间小于当前时间且失效时间大于当前时间的优惠券-->
    <select id="queryObjectByIdAndUsed" resultMap="userCouponMap">
        SELECT
            c.*,d.apply_type applyType
        FROM
            mall_user_coupon c
            INNER JOIN mk_store_ticket_discount d ON c.store_topic_id = d.store_topic_id
        where user_id = #{userId} AND d.tick_disc_id = #{ticketDiscId} AND is_used = 0 and c.enabled = 1
            AND valid_time <![CDATA[ < ]]> now() AND end_time <![CDATA[ > ]]> now() AND d.is_past = 0 and d.is_valid = 0
            ORDER BY end_time ASC
    </select>

    <select id="queryByCouponNumber" resultMap="userCouponMap">
        select * from mall_user_coupon where coupon_number = #{coupon_number}
    </select>

    <select id="queryByOrderId" resultMap="userCouponMap">
        select * from mall_user_coupon where order_id = #{order_id} limit 1
    </select>

    <select id="queryList" resultMap="userCouponMap">
        select a.*,d.adv_img_url
        from mall_user_coupon a  inner join mk_store_ticket_discount d 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
        <if test="merchSn != null">
            and b.merch_sn = #{merchSn}
        </if>
        <if test="coupon_number != null">
            and a.coupon_number = #{coupon_number}
        </if>
        <if test="user_id != null">
            and a.user_id = #{user_id}
        </if>
        <if test="send_type != null">
            and a.send_type = #{send_type}
        </if>
        <if test="send_types != null">
            and a.send_type in
            <foreach item="item" collection="send_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </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="source_key != null">
            and a.source_key = #{source_key}
        </if>
        <if test="show_state != null">
            and a.show_state = #{show_state}
        </if>
        <if test="enabled != null and enabled == true">
            and a.end_time > now()
        </if>
        <if test="isUsed != null and isUsed != ''">
            and a.is_used = 0
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_user_coupon a  inner join mk_store_ticket_discount d 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
        <if test="merchSn != null">
            and b.merch_sn = #{merchSn}
        </if>
        <if test="coupon_number != null">
            and a.coupon_number = #{coupon_number}
        </if>
        <if test="user_id != null">
            and a.user_id = #{user_id}
        </if>
        <if test="send_type != null">
            and a.send_type = #{send_type}
        </if>
        <if test="send_types != null">
            and a.send_type in
            <foreach item="item" collection="send_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </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="source_key != null">
            and a.source_key = #{source_key}
        </if>
        <if test="show_state != null">
            and a.show_state = #{show_state}
        </if>
        <if test="enabled != null and enabled == true">
            and a.end_time > now()
        </if>
        <if test="isUsed != null and isUsed != ''">
            and a.is_used = 0
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.api.entity.UserCouponVo" useGeneratedKeys="true" keyProperty="id">
		insert into mall_user_coupon
		(
			`store_topic_id`,
			`coupon_name`,
            `is_used`,
			`coupon_number`,
			`user_id`,
			`used_time`,
			`order_id`,
			`add_time`,
			valid_time,
			`source_key`,
			`referrer`,
			 show_state,
			 end_time,
			 type_money,
			 send_type,
			 create_time,
			 mod_time,
			 enabled
		)
		values
		(
            #{storeTopicId},
            #{couponName},
            #{isUsed},
			#{couponNumber},
			#{userId},
			#{usedTime},
			#{orderId},
			#{addTime},
			#{validTime},
			#{sourceKey},
			#{referrer},
			#{showState},
			#{endTime},
			#{typeMoney},
			#{sendType},
			#{createTime},
			#{modTime},
			#{enabled}
		)
	</insert>

    <update id="update" parameterType="com.kmall.api.entity.UserCouponVo">
        update mall_user_coupon
        <set>
            <if test="storeTopicId != null">`store_topic_id` = #{storeTopicId},</if>
            <if test="couponName != null">`coupon_name` = #{couponName},</if>
            <if test="isUsed != null">`is_used` = #{isUsed},</if>
            <if test="couponNumber != null">`coupon_number` = #{couponNumber},</if>
            <if test="userId != null">`user_id` = #{userId},</if>
            <if test="usedTime != null">`used_time` = #{usedTime},</if>
            <if test="orderId != null">`order_id` = #{orderId},</if>
            <if test="addTime != null">`add_time` = #{addTime},</if>
            <if test="validTime != null">`valid_time` = #{validTime},</if>
            <if test="sourceKey != null">`source_key` = #{sourceKey},</if>
            <if test="referrer != null">`referrer` = #{referrer},</if>
            <if test="showState != null">`show_state` = #{showState},</if>
            <if test="endTime != null">`end_time` = #{endTime},</if>
            <if test="typeMoney != null">`type_money` = #{typeMoney},</if>
            <if test="enabled != null">`enabled` = #{enabled},</if>
            <if test="sendType != null">`send_type` = #{sendType}</if>
        </set>
        where id = #{id}
    </update>

    <update id="cancelOrder" parameterType="com.kmall.api.entity.UserCouponVo">
        update mall_user_coupon
        <set>
            `used_time` = null,
            `order_id` = 0,
            is_used= 0
        </set>
        where id = #{id}
    </update>
    <!--根据优惠券id查询门店活动信息-->
    <select id="queryStoreTopicByDictId" resultType="map">
        SELECT
         t.id 'storeTopicId'
        FROM
        mall_store_topic t
        INNER JOIN mk_store_ticket_discount d ON t.id = d.store_topic_id
        WHERE d.tick_disc_id = #{ticketDiscId}
    </select>

    <select id="queryReceiveNumByStoreTopicId" resultType="java.lang.Integer">
        SELECT
        count(*)
        FROM
        mall_user_coupon cc
        LEFT JOIN mall_store_topic s ON cc.store_topic_id = s.id
        WHERE s.id = #{storeTopicId}
    </select>

    <select id="queryUserCoupons" resultMap="userCouponMap">
        select b.*
        from mall_store_topic a
        LEFT JOIN mall_user_coupon b ON a.id = b.store_topic_id
        where 1 = 1
        <if test="user_id != null">
            and b.`user_id` = #{user_id}
        </if>
        <if test="store_id != null">
            and a.`store_id` = #{store_id}
        </if>
        <!--        <if test="send_type != null">-->
        <!--            and a.send_type = #{send_type}-->
        <!--        </if>-->
        <if test="coupon_number != null">
            and b.coupon_number = #{coupon_number}
        </if>
        <if test="source_key != null and source_key != ''">
            and b.source_key = #{source_key}
        </if>
        <if test="unUsed != null and unUsed == true">
            and b.used_time is null and (b.order_id is null or b.order_id =0)
        </if>
        <if test="unUsed != null and unUsed == true">
            and (b.order_id is null or b.order_id =0)
        </if>
        <if test="unObtain != null and unObtain == true">
            and b.id is null
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>
</mapper>
