<?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.xinelu.manage.mapper.coupon.CouponMapper">

    <resultMap type="Coupon" id="CouponResult">
        <result property="id" column="id"/>
        <result property="couponTitle" column="coupon_title"/>
        <result property="couponCode" column="coupon_code"/>
        <result property="couponType" column="coupon_type"/>
        <result property="couponReductionDays" column="coupon_reduction_days"/>
        <result property="couponPrice" column="coupon_price"/>
        <result property="couponConsumePrice" column="coupon_consume_price"/>
        <result property="couponDescription" column="coupon_description"/>
        <result property="receiveType" column="receive_type"/>
        <result property="receiveCount" column="receive_count"/>
        <result property="receiveLimitCount" column="receive_limit_count"/>
        <result property="useScope" column="use_scope"/>
        <result property="showSort" column="show_sort"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectCouponVo">
        select id,
               coupon_title,
               coupon_code,
               coupon_type,
               coupon_reduction_days,
               coupon_price,
               coupon_consume_price,
               coupon_description,
               receive_type,
               receive_count,
               receive_limit_count,
               use_scope,
               show_sort,
               create_by,
               create_time,
               update_by,
               update_time
        from coupon
    </sql>

    <select id="selectCouponList" parameterType="Coupon" resultMap="CouponResult">
        <include refid="selectCouponVo"/>
        <where>
            <if test="couponTitle != null  and couponTitle != ''">
                and coupon_title like concat( #{couponTitle}, '%')
            </if>
            <if test="couponCode != null  and couponCode != ''">
                and coupon_code = #{couponCode}
            </if>
            <if test="couponType != null  and couponType != ''">
                and coupon_type = #{couponType}
            </if>
            <if test="couponReductionDays != null ">
                and coupon_reduction_days = #{couponReductionDays}
            </if>
            <if test="couponPrice != null ">
                and coupon_price = #{couponPrice}
            </if>
            <if test="couponConsumePrice != null ">
                and coupon_consume_price = #{couponConsumePrice}
            </if>
            <if test="couponDescription != null  and couponDescription != ''">
                and coupon_description = #{couponDescription}
            </if>
            <if test="receiveType != null  and receiveType != ''">
                and receive_type = #{receiveType}
            </if>
            <if test="receiveCount != null ">
                and receive_count = #{receiveCount}
            </if>
            <if test="receiveLimitCount != null ">
                and receive_limit_count = #{receiveLimitCount}
            </if>
            <if test="useScope != null  and useScope != ''">
                and use_scope = #{useScope}
            </if>
            <if test="showSort != null ">
                and show_sort = #{showSort}
            </if>
        </where>
        ORDER BY create_time desc, show_sort asc
    </select>

    <select id="selectCouponById" parameterType="Long"
            resultMap="CouponResult">
        <include refid="selectCouponVo"/>
        where id = #{id}
    </select>

    <insert id="insertCoupon" parameterType="Coupon" useGeneratedKeys="true"
            keyProperty="id">
        insert into coupon
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="couponTitle != null">coupon_title,
            </if>
            <if test="couponCode != null">coupon_code,
            </if>
            <if test="couponType != null">coupon_type,
            </if>
            <if test="couponReductionDays != null">coupon_reduction_days,
            </if>
            <if test="couponPrice != null">coupon_price,
            </if>
            <if test="couponConsumePrice != null">coupon_consume_price,
            </if>
            <if test="couponDescription != null">coupon_description,
            </if>
            <if test="receiveType != null">receive_type,
            </if>
            <if test="receiveCount != null">receive_count,
            </if>
            <if test="receiveLimitCount != null">receive_limit_count,
            </if>
            <if test="useScope != null">use_scope,
            </if>
            <if test="showSort != null">show_sort,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="couponTitle != null">#{couponTitle},
            </if>
            <if test="couponCode != null">#{couponCode},
            </if>
            <if test="couponType != null">#{couponType},
            </if>
            <if test="couponReductionDays != null">#{couponReductionDays},
            </if>
            <if test="couponPrice != null">#{couponPrice},
            </if>
            <if test="couponConsumePrice != null">#{couponConsumePrice},
            </if>
            <if test="couponDescription != null">#{couponDescription},
            </if>
            <if test="receiveType != null">#{receiveType},
            </if>
            <if test="receiveCount != null">#{receiveCount},
            </if>
            <if test="receiveLimitCount != null">#{receiveLimitCount},
            </if>
            <if test="useScope != null">#{useScope},
            </if>
            <if test="showSort != null">#{showSort},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateCoupon" parameterType="Coupon">
        update coupon
        <trim prefix="SET" suffixOverrides=",">
            <if test="couponTitle != null">coupon_title =
                #{couponTitle},
            </if>
            <if test="couponCode != null">coupon_code =
                #{couponCode},
            </if>
            <if test="couponType != null">coupon_type =
                #{couponType},
            </if>
            <if test="couponReductionDays != null">coupon_reduction_days =
                #{couponReductionDays},
            </if>
            <if test="couponPrice != null">coupon_price =
                #{couponPrice},
            </if>
            <if test="couponConsumePrice != null">coupon_consume_price =
                #{couponConsumePrice},
            </if>
            <if test="couponDescription != null">coupon_description =
                #{couponDescription},
            </if>
            <if test="receiveType != null">receive_type =
                #{receiveType},
            </if>
            <if test="receiveCount != null">receive_count =
                #{receiveCount},
            </if>
            <if test="receiveLimitCount != null">receive_limit_count =
                #{receiveLimitCount},
            </if>
            <if test="useScope != null">use_scope =
                #{useScope},
            </if>
            <if test="showSort != null">show_sort =
                #{showSort},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCouponById" parameterType="Long">
        delete
        from coupon
        where id = #{id}
    </delete>

    <delete id="deleteCouponByIds" parameterType="String">
        delete from coupon where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectCouponByTitle" resultType="java.lang.Integer">
        select count(1)
        from coupon
        where coupon_title = #{couponTitle}
    </select>
</mapper>
