<?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="coupon_id" column="coupon_id"/>
        <result property="coupon_name" column="coupon_name"/>
        <result property="coupon_number" column="coupon_number"/>
        <result property="user_id" column="user_id"/>
        <result property="used_time" column="used_time"/>
        <result property="add_time" column="add_time"/>
        <result property="order_id" column="order_id"/>
        <result property="source_key" column="source_key"/>
        <result property="referrer" column="referrer"/>
        <result property="show_state" column="show_state"/>
        <result property="end_time" column="end_time"/>
        <result property="type_money" column="type_money"/>
        <result property="send_type" 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"/>

    </resultMap>

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

    <select id="queryObjectByIdAndUsed" resultMap="userCouponMap">
        select * from mall_user_coupon where id = #{value} and is_used = 0
    </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.*,c.is_all,c.coupon_url
        from mall_user_coupon a  inner join mall_coupon c on a.coupon_id = c.id
        where 1 = 1
        <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
        where 1 = 1
        <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="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>
    </select>

    <insert id="save" parameterType="com.kmall.api.entity.UserCouponVo" useGeneratedKeys="true" keyProperty="id">
		insert into mall_user_coupon
		(
			`coupon_id`,
			`coupon_name`,
            `is_used`,
			`coupon_number`, 
			`user_id`, 
			`used_time`, 
			`order_id`,
			`add_time`,
			`source_key`,
			`referrer`,
			 show_state,
			 end_time,
			 type_money,
			 send_type,
			 min_goods_amount
		)
		values
		(
            #{coupon_id},
            #{coupon_name},
            #{isUsed},
			#{coupon_number},
			#{user_id},
			#{used_time},
			#{order_id},
			#{add_time},
			#{source_key},
			#{referrer},
			#{show_state},
			#{end_time},
			#{type_money},
			#{send_type},
			#{min_goods_amount}
		)
	</insert>

    <update id="update" parameterType="com.kmall.api.entity.UserCouponVo">
        update mall_user_coupon
        <set>
            <if test="coupon_id != null">`coupon_id` = #{coupon_id},</if>
            <if test="coupon_name != null">`coupon_name` = #{coupon_name},</if>
            <if test="isUsed != null">`is_used` = #{isUsed},</if>
            <if test="coupon_number != null">`coupon_number` = #{coupon_number},</if>
            <if test="user_id != null">`user_id` = #{user_id},</if>
            <if test="used_time != null">`used_time` = #{used_time},</if>
            <if test="order_id != null">`order_id` = #{order_id},</if>
            <if test="add_time != null">`add_time` = #{add_time},</if>
            <if test="source_key != null">`source_key` = #{source_key},</if>
            <if test="referrer != null">`referrer` = #{referrer},</if>
            <if test="show_state != null">`show_state` = #{show_state},</if>
            <if test="end_time != null">`end_time` = #{end_time},</if>
            <if test="type_money != null">`type_money` = #{type_money},</if>
            <if test="send_type != null">`send_type` = #{send_type},</if>
            <if test="min_goods_amount != null">`min_goods_amount` = #{min_goods_amount},</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
        </set>
        where id = #{id}
    </update>
</mapper>