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

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.platform.entity.CouponVo" id="couponMap">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="typeMoney" column="type_money"/>
        <result property="sendType" column="send_type"/>
        <result property="minAmount" column="min_amount"/>
        <result property="maxAmount" column="max_amount"/>
        <result property="sendStartDate" column="send_start_date"/>
        <result property="sendEndDate" column="send_end_date"/>
        <result property="useStartDate" column="use_start_date"/>
        <result property="useEndDate" column="use_end_date"/>
        <result property="useDesc" column="use_desc"/>
        <result property="minGoodsAmount" column="min_goods_amount"/>
        <result property="userId" column="user_id"/>
        <result property="couponNumber" column="coupon_number"/>
        <result property="minTransmitNum" column="min_transmit_num"/>
        <result property="userCouponId" column="user_coupon_id"/>
        <result property="couponStatus" column="coupon_status"/>
        <result property="enable" column="enable"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="useStatus" column="use_status"/>
    </resultMap>

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

    <select id="queryList" resultMap="couponMap">
        select * from nideshop_coupon a
        where 1 = 1
        <if test="send_types != null">
            and send_type in
            <foreach item="item" collection="send_types" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="enabled != null and enabled == true">
            and a.use_end_date >= now()
            and now() >= a.use_start_date
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
    </select>

    <select id="getCouponByBrandId" resultType="com.platform.entity.CouponVo">
          select * from nideshop_coupon
              ${ew.customSqlSegment}
    </select>


    <select id="selectForUpdateCoupon" resultType="com.platform.entity.CouponVo">
          select * from nideshop_coupon
          where id =  #{couponId}
         for update
    </select>

    <insert id="save" parameterType="com.platform.entity.CouponVo" useGeneratedKeys="true" keyProperty="id">
		insert into nideshop_coupon
		(
			`name`, 
			`type_money`, 
			`send_type`, 
			`min_amount`, 
			`max_amount`, 
			`send_start_date`, 
			`send_end_date`, 
			`use_start_date`, 
			`use_end_date`, 
			`min_goods_amount`,
			`min_transmit_num`,
			`coupon_status`,
			`brand_id`,
			`brand_name`,
			`enable`,
			`create_time`,
			`update_time`,
			`number`,
			`receive_number`,
			`used_number`
		)
		values
		(
			#{name}, 
			#{typeMoney},
			#{sendType},
			#{minAmount},
			#{maxAmount},
			#{sendStartDate},
			#{sendEndDate},
			#{useStartDate},
			#{useEndDate},
			#{minGoodsAmount},
			#{minTransmitNum},
			#{couponStatus},
			#{brandId},
			#{brandName},
			#{enable},
			#{createTime},
			#{updateTime},
			#{number},
			#{receiveNumber},
			#{usedNumber}
		)
	</insert>

    <update id="update" parameterType="com.platform.entity.CouponVo">
        update nideshop_coupon
        <set>
            <if test="name != null">`name` = #{name},</if>
            <if test="typeMoney != null">`type_money` = #{typeMoney},</if>
            <if test="sendType != null">`send_type` = #{sendType},</if>
            <if test="minAmount != null">`min_amount` = #{minAmount},</if>
            <if test="maxAmount != null">`max_amount` = #{maxAmount},</if>
            <if test="sendStartDate != null">`send_start_date` = #{sendStartDate},</if>
            <if test="sendEndDate != null">`send_end_date` = #{sendEndDate},</if>
            <if test="useStartDate != null">`use_start_date` = #{useStartDate},</if>
            <if test="useEndDate != null">`use_end_date` = #{useEndDate},</if>
            <if test="useDesc != null">`use_desc` = #{useDesc},</if>
            <if test="minGoodsAmount != null">`min_goods_amount` = #{minGoodsAmount},</if>
            <if test="minTransmitNum != null">`min_transmit_num` = #{minTransmitNum},</if>
            <if test="brandId != null">`brand_id` = #{brandId},</if>
            <if test="brandName != null">`brand_name` = #{brandName},</if>
            <if test="receiveNumber != null">`receive_number` = #{receiveNumber},</if>
            <if test="usedNumber != null">`used_number` = #{usedNumber},</if>
            <if test="number != null">`number` = #{number},</if>
            <if test="enable != null">`enable` = #{enable},</if>
            <if test="createTime != null">`create_time` = #{createTime},</if>
            <if test="updateTime != null">`update_time` = #{updateTime},</if>
            <if test="couponStatus != null">`coupon_status` = #{couponStatus}</if>

        </set>
        where id = #{id}
    </update>

    <update id="updateUserCoupon" parameterType="com.platform.entity.CouponVo">
        update nideshop_user_coupon
        <set>
            <if test="couponStatus != null">`coupon_status` = #{couponStatus}</if>
        </set>
        where id = #{userCouponId}
    </update>

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

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

    <select id="queryUserCoupons" resultMap="couponMap">
        select a.*,b.coupon_number,b.user_id,b.coupon_status as use_status,b.id as user_coupon_id
        from nideshop_coupon a
        left join nideshop_user_coupon b on a.id = b.coupon_id
        where 1 = 1
        <if test="user_id != null">
            and b.`user_id` = #{user_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>
        and b.coupon_status = 1
        and a.enable = 1
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by b.add_time desc
            </otherwise>
        </choose>
    </select>

    <select id="getInvalidCouponByYear" resultType="com.platform.entity.CouponVo">
        select a.*,b.coupon_number,b.user_id,b.coupon_status as use_status,b.id as user_coupon_id
        from nideshop_coupon a
        left join nideshop_user_coupon b on a.id = b.coupon_id
         ${ew.customSqlSegment}
    </select>
    <select id="queryMaxUserEnableCoupon" resultMap="couponMap">
        select a.`id`,
        a.`name`,
        a.`type_money` as type_money,
        a.`send_type`,
        a.`min_amount`,
        a.`max_amount`,
        a.`send_start_date`,
        a.`send_end_date`,
        a.`use_start_date`,
        a.`use_end_date`,
        a.`min_goods_amount`,
        a.min_transmit_num
        from nideshop_coupon a
        left join nideshop_coupon b on a.send_type = b.send_type and b.type_money > a.type_money
        where 1 = 1
        and a.use_end_date >= now()
        and now() >= a.use_start_date
        and b.id is null
        <if test="send_type != null">
            and a.send_type = #{send_type}
        </if>
        limit 0, 1
    </select>

    <select id="queryUserCouponList" resultMap="couponMap">
        select a.*,b.coupon_number,b.user_id
        from nideshop_coupon a
        left join nideshop_user_coupon b on a.id = b.coupon_id
        where 1 = 1
        and a.use_end_date >= now()
        and now() >= a.use_start_date
        and b.user_id = #{user_id} and b.order_id is null
    </select>

    <select id="getUserCoupon" resultMap="couponMap">
        select a.*,b.coupon_number,b.user_id,b.coupon_status,b.id user_coupon_id
        from nideshop_coupon a
        left join nideshop_user_coupon b on a.id = b.coupon_id
        where 1 = 1
        and b.id = #{id}
    </select>

</mapper>