<?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.iamberry.wechat.service.mapper.ExchangeMapper">
    <resultMap  id="BaseResultMap" type="Exchange" >
        <result    column="exchange_id"    property="exchangeId" />
        <result    column="coupon_id"    property="couponId" />
        <result    column="exchange_create_time"    property="exchangeCreateTime" />
        <result    column="exchange_update_time"    property="exchangeUpdateTime" />
        <collection property="exchangeItemList" column="exchange_id" ofType="ExchangeItem" select="getExchangeItemList"/>
    </resultMap>

    <resultMap  id="ExchangeItemMap" type="ExchangeItem" >
        <result    column="exchange_item_id"    property="exchangeItemId" />
        <result    column="exchange_id"    property="exchangeId" />
        <result    column="color_id"    property="colorId" />
        <result    column="exchange_item_num"    property="exchangeItemNum" />
    </resultMap>

    <sql    id="Base_List" >
        t.exchange_id,
        t.coupon_id,
        t.exchange_name,
        t.exchange_desc,
        t.exchange_create_time,
        t.exchange_update_time
    </sql>
    <select id="getExchangeList" resultMap="BaseResultMap" parameterType="Exchange" >
        select
        <include refid="Base_List" />
        from tb_iamberry_coupon_exchange t
        <where>
            <if test="exchangeId != null ">
                AND t.exchange_id = #{exchangeId}
            </if >
            <if test="couponId != null ">
                AND t.coupon_id = #{couponId}
            </if >
        </where>
    </select>


    <select id="getExchangeItemList" resultMap="ExchangeItemMap" parameterType="Integer" >
        select
        t.exchange_item_id,
        t.exchange_id,
        t.color_id,
        t.exchange_item_num
        from tb_iamberry_coupon_exchange_item t
        WHERE  t.exchange_id = #{exchangeId}
    </select>

    <select id="getExchangeById" resultMap="BaseResultMap" parameterType="Integer" >
        select
        <include refid="Base_List" />
        from tb_iamberry_coupon_exchange t
        where t.exchange_id= #{exchangeId}
    </select>
    <insert id="save" parameterType="Exchange" >
        insert into
        tb_iamberry_coupon_exchange
        (
        coupon_id,
        exchange_name,
        exchange_desc
        )
        values
        (
        #{couponId},
        #{exchangeName},
        #{exchangeDesc}
        )
    </insert>
    <update id="update" parameterType="Exchange" >
        update
        tb_iamberry_coupon_exchange
        <set >
            <if test="couponId != null ">
                coupon_id = #{couponId},
            </if >
        </set >
        where exchange_id= #{exchangeId}
    </update>
</mapper>