<?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.yijia.market.mapper.MarketCouponMapper">
    
    <resultMap type="MarketCoupon" id="MarketCouponResult">
        <result property="couponId"    column="coupon_id"    />
        <result property="stationNo"    column="station_no"    />
        <result property="stationName"    column="station_name"    />
        <result property="couponName"    column="coupon_name"    />
        <result property="couponRule"    column="coupon_rule"    />
        <result property="couponType"    column="coupon_type"    />
        <result property="couponAmt"    column="coupon_amt"    />
        <result property="couponReulAmt"    column="coupon_reul_amt"    />
        <result property="couponOil"    column="coupon_oil"    />
        <result property="validityDate"    column="validity_date"    />
    </resultMap>

    <sql id="selectMarketCouponVo">
        select coupon_id, station_no, station_name, coupon_name, coupon_rule, coupon_type, coupon_amt, coupon_reul_amt, coupon_oil, validity_date from market_coupon
    </sql>

    <select id="selectMarketCouponList" parameterType="MarketCoupon" resultMap="MarketCouponResult">
        <include refid="selectMarketCouponVo"/>
        <where>  
            <if test="stationNo != null  and stationNo != ''"> and station_no = #{stationNo}</if>
            <if test="stationName != null  and stationName != ''"> and station_name like concat('%', #{stationName}, '%')</if>
            <if test="couponName != null  and couponName != ''"> and coupon_name like concat('%', #{couponName}, '%')</if>
            <if test="couponRule != null  and couponRule != ''"> and coupon_rule = #{couponRule}</if>
            <if test="couponType != null  and couponType != ''"> and coupon_type = #{couponType}</if>
            <if test="couponAmt != null "> and coupon_amt = #{couponAmt}</if>
            <if test="couponReulAmt != null "> and coupon_reul_amt = #{couponReulAmt}</if>
            <if test="couponOil != null  and couponOil != ''"> and coupon_oil = #{couponOil}</if>
            <if test="validityDate != null "> and validity_date = #{validityDate}</if>
        </where>
    </select>
    
    <select id="selectMarketCouponById" parameterType="Long" resultMap="MarketCouponResult">
        <include refid="selectMarketCouponVo"/>
        where coupon_id = #{couponId}
    </select>
        
    <insert id="insertMarketCoupon" parameterType="MarketCoupon" useGeneratedKeys="true" keyProperty="couponId">
        insert into market_coupon
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationNo != null">station_no,</if>
            <if test="stationName != null">station_name,</if>
            <if test="couponName != null">coupon_name,</if>
            <if test="couponRule != null">coupon_rule,</if>
            <if test="couponType != null">coupon_type,</if>
            <if test="couponAmt != null">coupon_amt,</if>
            <if test="couponReulAmt != null">coupon_reul_amt,</if>
            <if test="couponOil != null">coupon_oil,</if>
            <if test="validityDate != null">validity_date,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationNo != null">#{stationNo},</if>
            <if test="stationName != null">#{stationName},</if>
            <if test="couponName != null">#{couponName},</if>
            <if test="couponRule != null">#{couponRule},</if>
            <if test="couponType != null">#{couponType},</if>
            <if test="couponAmt != null">#{couponAmt},</if>
            <if test="couponReulAmt != null">#{couponReulAmt},</if>
            <if test="couponOil != null">#{couponOil},</if>
            <if test="validityDate != null">#{validityDate},</if>
         </trim>
    </insert>

    <update id="updateMarketCoupon" parameterType="MarketCoupon">
        update market_coupon
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationNo != null">station_no = #{stationNo},</if>
            <if test="stationName != null">station_name = #{stationName},</if>
            <if test="couponName != null">coupon_name = #{couponName},</if>
            <if test="couponRule != null">coupon_rule = #{couponRule},</if>
            <if test="couponType != null">coupon_type = #{couponType},</if>
            <if test="couponAmt != null">coupon_amt = #{couponAmt},</if>
            <if test="couponReulAmt != null">coupon_reul_amt = #{couponReulAmt},</if>
            <if test="couponOil != null">coupon_oil = #{couponOil},</if>
            <if test="validityDate != null">validity_date = #{validityDate},</if>
        </trim>
        where coupon_id = #{couponId}
    </update>

    <delete id="deleteMarketCouponById" parameterType="Long">
        delete from market_coupon where coupon_id = #{couponId}
    </delete>

    <delete id="deleteMarketCouponByIds" parameterType="String">
        delete from market_coupon where coupon_id in 
        <foreach item="couponId" collection="array" open="(" separator="," close=")">
            #{couponId}
        </foreach>
    </delete>
    
</mapper>