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

    <resultMap type="com.kmall.admin.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="useStartDate" column="use_start_date"/>
        <result property="useEndDate" column="use_end_date"/>
        <result property="minGoodsAmount" column="min_goods_amount"/>
        <result property="isAll" column="is_all"/>
    </resultMap>

    <sql id="conditions">
        <where>
            <if test="sendType != null">and a.`send_type` = #{sendType}</if>
            <if test="name != null and name.trim() != ''">
                AND a.name LIKE concat('%',#{name},'%')
            </if>
        </where>
    </sql>

    <select id="queryObject" resultType="com.kmall.admin.entity.CouponEntity">
		select
			`id`,
			`name`,
			`type_money`,
			`send_type`,
			`min_amount`,
			`max_amount`,
			`send_start_date`,
			`send_end_date`,
			`use_start_date`,
			`use_end_date`,
			`min_goods_amount`,
			`is_all`
		from mall_coupon
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.kmall.admin.entity.CouponEntity">
        select
        `id`,
        `name`,
        `type_money`,
        `send_type`,
        `min_amount`,
        `max_amount`,
        `send_start_date`,
        `send_end_date`,
        `use_start_date`,
        `use_end_date`,
        `min_goods_amount`,
        `is_all`
        from mall_coupon a
        <include refid="conditions"/>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.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_coupon a
        <include refid="conditions"/>
	</select>

    <insert id="save" parameterType="com.kmall.admin.entity.CouponEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_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`,
			`is_all`)
		values(
			#{name},
			#{typeMoney},
			#{sendType},
			#{minAmount},
			#{maxAmount},
			#{sendStartDate},
			#{sendEndDate},
			#{useStartDate},
			#{useEndDate},
			#{minGoodsAmount},
			#{isAll})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.CouponEntity">
        update mall_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="minGoodsAmount != null">`min_goods_amount` = #{minGoodsAmount},</if>
            <if test="isAll != null">`is_all` = #{isAll},</if>
        </set>
        where id = #{id}
    </update>

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

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

</mapper>