<?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.admin.dao.UserCouponDao">

    <resultMap type="com.kmall.admin.entity.UserCouponEntity" 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="orderId" column="order_id"/>
        <result property="addTime" column="add_time"/>
        <result property="isUsed" column="is_used"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.UserCouponEntity">
		select
			`id`,
			`coupon_id`,
			`coupon_number`,
			`user_id`,
			`used_time`,
			`add_time`,
			`order_id`,
			`is_used`
		from mall_user_coupon
		where id = #{id}
	</select>

    <select id="queryByOrderId" resultType="com.kmall.admin.entity.UserCouponEntity">
        select
        `id`,
        `coupon_id`,
        `coupon_number`,
        `user_id`,
        `used_time`,
        `add_time`,
        `order_id`,
        `is_used`
        from mall_user_coupon
        where order_id = #{orderId}
    </select>

    <select id="queryList" resultType="com.kmall.admin.entity.UserCouponEntity">
        select
        mall_user_coupon.id,
        mall_user_coupon.coupon_id,
        mall_user_coupon.coupon_number,
        mall_user_coupon.user_id,
        mall_user_coupon.used_time,
        mall_user_coupon.add_time,
        mall_user_coupon.order_id,
        mall_user_coupon.is_used,
        mall_user.username user_name,
        mall_coupon.name coupon_name
        from mall_user_coupon
        LEFT JOIN mall_user ON mall_user_coupon.user_id = mall_user.id
        LEFT JOIN mall_coupon ON mall_user_coupon.coupon_id = mall_coupon.id
        WHERE 1=1
        <if test="userName != null and userName.trim() != ''">
            AND mall_user.username LIKE concat('%',#{userName},'%')
        </if>
        <if test="couponName != null and couponName.trim() != ''">
            AND mall_coupon.name LIKE concat('%',#{couponName},'%')
        </if>
        <if test="userId != null">
            AND mall_user_coupon.user_id = #{userId}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by mall_user_coupon.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 LEFT JOIN mall_user ON mall_user_coupon.user_id = mall_user.id
        LEFT JOIN mall_coupon ON mall_user_coupon.coupon_id = mall_coupon.id
        WHERE 1=1
        <if test="userName != null and userName.trim() != ''">
            AND mall_user.username LIKE concat('%',#{userName},'%')
        </if>
        <if test="couponName != null and couponName.trim() != ''">
            AND mall_coupon.name LIKE concat('%',#{couponName},'%')
        </if>
        <if test="userId != null">
            AND mall_user_coupon.user_id = #{userId}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.UserCouponEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_user_coupon(
			`coupon_id`,
			`coupon_number`,
			`user_id`,
			`used_time`,
			`add_time`,
			`order_id`,
			`is_used`)
		values(
			#{couponId},
			#{couponNumber},
			#{userId},
			#{usedTime},
			#{addTime},
			#{orderId},
			#{isUsed})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.UserCouponEntity">
        update mall_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="addTime != null">`add_time` = #{addTime},</if>
            <if test="orderId != null">`order_id` = #{orderId}</if>
            <if test="isUsed != null">`is_used` = #{isUsed}</if>
        </set>
        where id = #{id}
    </update>

    <update id="cancelOrder" parameterType="com.kmall.admin.entity.UserCouponEntity">
        update mall_user_coupon
        <set>
            `used_time` = null,
            `order_id` = 0
        </set>
        where id = #{id}
    </update>

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

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

</mapper>