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

    <resultMap type="com.platform.entity.CouponEntity" 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="useDesc" column="use_desc"/>
        <result property="useStartDate" column="use_start_date"/>
        <result property="useEndDate" column="use_end_date"/>
        <result property="minGoodsAmount" column="min_goods_amount"/>
        <result property="minTransmitNum" column="min_transmit_num"/>
        <result property="brandId" column="brand_id"/>
        <result property="brandName" column="brand_name"/>
        <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="number" column="number"/>
        <result property="receiveNumber" column="receive_number"/>
        <result property="usedNumber" column="used_number"/>
        <result property="orderSn" column="order_sn"/>
        <result property="couponSn" column="coupon_sn"/>
    </resultMap>

    <select id="queryObject" resultType="com.platform.entity.CouponEntity">
		select * from nideshop_coupon
		where id = #{id} and enable = 1
	</select>


    <select id="queryTotal" resultType="java.lang.Integer">
        select count(*) from nideshop_coupon
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <if test="brandName != null and brandName.trim() != ''">
            AND brand_name LIKE concat('%',#{brandName},'%')
        </if>
        <if test="couponStatus != null  and couponStatus.trim() != ''">
            AND coupon_status = #{couponStatus}
        </if>

        AND enable = 1
    </select>


    <select id="queryList" resultType="com.platform.entity.CouponEntity">
        select * from nideshop_coupon
        WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
        <if test="brandName != null and brandName.trim() != ''">
            AND brand_name LIKE concat('%',#{brandName},'%')
        </if>
        <if test="couponStatus != null  and couponStatus.trim() != ''">
            AND coupon_status = #{couponStatus}
        </if>
        <if test="deptIdList != null and deptIdList.size() > 0">
            AND create_user_dept_id in
            <foreach item="id" collection="deptIdList" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>

        AND enable = 1

        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
    </select>


    <select id="queryUseDetailTotal" resultType="java.lang.Integer">
        select count(*)
        from  nideshop_user_coupon relation
        inner join nideshop_coupon coupon on (relation.coupon_id = coupon.id  and relation.user_id is not null)
        left join nideshop_user user on user.id = relation.user_id
        left join nideshop_order sorder on relation.order_id = sorder.id
        WHERE 1=1
        <if test="orderId != null and orderId.trim() != ''">
            AND sorder.order_sn LIKE concat(#{orderSn},'%')
        </if>
        <if test="userName != null and userName.trim() != ''">
            AND user.username LIKE concat(#{userName},'%')
        </if>
        <if test="couponStatus != null and couponStatus.trim() != ''">
            AND relation.coupon_status = #{couponStatus}
        </if>
        AND coupon.id = #{couponId}
        AND enable = 1
        order by relation.used_time desc

    </select>

    <select id="queryUseDetailList" resultType="com.platform.entity.CouponEntity">
        select
        coupon.*,
        sorder.order_sn as order_sn,
        relation.coupon_status as use_status ,
        relation.used_time as use_time ,
        user.username as user_name ,
        user.id as user_id
        from  nideshop_user_coupon relation
        inner join nideshop_coupon coupon on (relation.coupon_id = coupon.id  and relation.user_id is not null)
        left join nideshop_user user on user.id = relation.user_id
        left join nideshop_order sorder on relation.order_id = sorder.id
        WHERE 1=1
        <if test="orderId != null and orderId.trim() != ''">
            AND sorder.order_sn LIKE concat(#{orderSn},'%')
        </if>
        <if test="userName != null and userName.trim() != ''">
            AND user.username LIKE concat(#{userName},'%')
        </if>
        <if test="couponStatus != null and couponStatus.trim() != ''">
            AND relation.coupon_status = #{couponStatus}
        </if>
        AND coupon.id = #{couponId}
        AND enable = 1
        order by relation.used_time desc

    </select>

    <insert id="addCoupon" parameterType="com.platform.entity.CouponEntity">
		insert into nideshop_coupon(
			`id`,
			`name`,
			`type_money`,
			`send_type`,
			`min_amount`,
			`max_amount`,
			`send_start_date`,
			`send_end_date`,
			`use_start_date`,
			`use_end_date`,
			`use_desc`,
			`min_goods_amount`,
			`min_transmit_num`,
			`coupon_status`,
			`brand_id`,
			`brand_name`,
			`enable`,
			`create_time`,
			`update_time`,
			`number`,
			`receive_number`,
			`coupon_sn`,
			`used_number`,
			`create_user_id`,
			`update_user_id`,
			`create_user_dept_id`
			)
		values(#{id},
			#{name},
			#{typeMoney},
			#{sendType},
			#{minAmount},
			#{maxAmount},
			#{sendStartDate},
			#{sendEndDate},
			#{useStartDate},
			#{useEndDate},
			#{useDesc},
			#{minGoodsAmount},
			#{minTransmitNum},
			#{couponStatus},
			#{brandId},
			#{brandName},
			#{enable},
			#{createTime},
			#{updateTime},
			#{number},
			#{receiveNumber},
			#{couponSn},
			#{usedNumber},
			#{createUserId},
			#{updateUserId},
			#{createUserDeptId}
			)
	</insert>

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

    <update id="update" parameterType="com.platform.entity.CouponEntity">
        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="couponStatus != null">`coupon_status` = #{couponStatus},</if>
            <if test="brandId != null">`brand_id` = #{brandId},</if>
            <if test="brandName != null">`brand_name` = #{brandName},</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="number != null">`number` = #{number},</if>
            <if test="receiveNumber != null">`receive_number` = #{receiveNumber},</if>
            <if test="usedNumber != null">`used_number` = #{usedNumber},</if>
            <if test="couponSn != null">`coupon_sn` = #{couponSn},</if>
            <if test="createUserId != null">`create_user_id` = #{createUserId},</if>
            <if test="updateUserId != null">`update_user_id` = #{updateUserId},</if>
            <if test="createUserDeptId != null">`create_user_dept_id` = #{createUserDeptId}</if>
        </set>
        where id = #{id}
    </update>

    <update id="updateStatus">
        update nideshop_coupon
        set coupon_status = #{couponStatus} , `update_time` = now()
        where id = #{id}
    </update>

    <update id="deleteCouponBatch">
        update nideshop_coupon
        set enable = 0 , `update_time` = now()
        where id in
        <foreach item="id" collection="idList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>


</mapper>