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

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.platform.entity.UserCouponVo" id="userCouponMap">
        <result property="id" column="id"/>
        <result property="couponId" column="coupon_id"/>
        <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="couponStatus" column="coupon_status"/>
    </resultMap>

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

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

    <select id="getByUserIdAndCouponId" resultType="com.platform.entity.UserCouponVo">
        select * from nideshop_user_coupon
        where  user_id = #{userId}
         and coupon_id = #{couponId} limit 1
    </select>


    <select id="getByUserIdAndCouponIdForUpdate" resultType="com.platform.entity.UserCouponVo">
        select * from nideshop_user_coupon
        where  user_id = #{userId}
         and coupon_id = #{couponId} limit 1 for update
    </select>

    <select id="queryList" resultMap="userCouponMap">
        select a.* from nideshop_user_coupon a
        left join nideshop_coupon b on b.id = a.coupon_id
        where 1 = 1
        <if test="coupon_number != null">
            and a.coupon_number = #{couponNumber}
        </if>
        <if test="user_id != null">
            and a.user_id = #{userId}
        </if>
        <if test="send_type != null">
            and b.send_type = #{sendType}
        </if>
        <if test="referrer != null">
            and a.referrer = #{referrer}
        </if>
        <if test="source_key != null">
            and a.source_key = #{sourceKey}
        </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 nideshop_user_coupon 
	</select>

    <insert id="save" parameterType="com.platform.entity.UserCouponVo" useGeneratedKeys="true" keyProperty="id">
		insert into nideshop_user_coupon
		(
			`coupon_id`, 
			`coupon_number`, 
			`user_id`, 
			`used_time`, 
			`order_id`,
			`add_time`,
			`source_key`,
			`referrer`,
			`coupon_status`
		)
		values
		(
            #{couponId},
			#{couponNumber},
			#{userId},
			#{usedTime},
			#{orderId},
			#{addTime},
			#{sourceKey},
			#{referrer},
			#{couponStatus}
		)
	</insert>

    <update id="update" parameterType="com.platform.entity.UserCouponVo">
        update nideshop_user_coupon
        <set>
            <if test="couponId != null">
                `coupon_id` = #{couponId},
            </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="sourceKey != null">`source_key` = #{sourceKey},</if>
            <if test="referrer != null">`referrer` = #{referrer},</if>
            <if test="couponStatus != null">`coupon_status` = #{couponStatus}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from nideshop_user_coupon where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from nideshop_user_coupon where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>