<?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.IntegralItemMapper">
    <resultMap  id="BaseResultMap" type="IntegralItem" >
        <result    column="integral_item_id"    property="integralItemId" />
        <result    column="integral_item_order_id"    property="integralItemOrderId" />
        <result    column="integral_product_id"    property="integralProductId" />
        <result    column="integral_item_num"    property="integralItemNum" />
        <result    column="integral_item_total"    property="integralItemTotal" />
        <result    column="integral_item_dfbi"    property="integralItemDfbi" />
        <result    column="integral_item_product_type"    property="integralItemProductType" />
        <result    column="integral_item_coupon_id"    property="integralItemCouponId" />
        <result    column="integral_item_coupon_item_id"    property="integralItemCouponItemId" />
        <result    column="integral_item_product_name"    property="integralItemProductName" />
        <result    column="integral_item_product_price"    property="integralItemProductPrice" />
        <result    column="integral_item_product_dfbi"    property="integralItemProductDfbi" />
        <result    column="integral_item_product_pic"    property="integralItemProductPic" />
        <result    column="integral_item_create_date"    property="integralItemCreateDate" />
    </resultMap>
    <sql    id="Base_List" >
        t.integral_item_id,
        t.integral_item_order_id,
        t.integral_product_id,
        t.integral_item_num,
        t.integral_item_total,
        t.integral_item_dfbi,
        t.integral_item_product_type,
        t.integral_item_coupon_id,
        t.integral_item_coupon_item_id,
        t.integral_item_product_name,
        t.integral_item_product_price,
        t.integral_item_product_dfbi,
        t.integral_item_product_pic,
        t.integral_item_create_date
    </sql>

    <select id="getIntegralItemList" resultType="IntegralItem" parameterType="IntegralItem" >
        select
            t.integral_item_id,
            t.integral_item_order_id,
            t.integral_product_id,
            t.integral_item_num,
            t.integral_item_total,
            t.integral_item_dfbi,
            t.integral_item_product_type,
            t.integral_item_coupon_id,
            t.integral_item_coupon_item_id,
            t.integral_item_product_name,
            t.integral_item_product_price,
            t.integral_item_product_dfbi,
            t.integral_item_product_pic,
            t.integral_item_create_date,
            ci.coupon_use_end_date AS  couponUseEndDate,
            ct.coupon_type AS  couponType
        from tb_iamberry_integral_item t
        LEFT JOIN tb_iamberry_coupon_item ci ON t. integral_item_coupon_item_id = ci.coupon_item_id
        LEFT JOIN tb_iamberry_coupon_type ct ON t.integral_item_coupon_id = ct.coupon_id
        <where>
            <if test="integralItemId != null ">
                AND t.integral_item_id = #{integralItemId}
            </if >
            <if test="integralItemOrderId != null and integralItemOrderId != ''">
                AND t.integral_item_order_id = #{integralItemOrderId}
            </if >
            <if test="integralProductId != null ">
                AND t.integral_product_id = #{integralProductId}
            </if >
            <if test="integralItemNum != null ">
                AND t.integral_item_num = #{integralItemNum}
            </if >
            <if test="integralItemTotal != null ">
                AND t.integral_item_total = #{integralItemTotal}
            </if >
            <if test="integralItemDfbi != null ">
                AND t.integral_item_dfbi = #{integralItemDfbi}
            </if >
            <if test="integralItemProductType != null ">
                AND t.integral_item_product_type = #{integralItemProductType}
            </if >
            <if test="integralItemCouponId != null ">
                AND t.integral_item_coupon_id = #{integralItemCouponId}
            </if >
            <if test="integralItemCouponItemId != null and integralItemCouponItemId != ''">
                AND t.integral_item_coupon_item_id = #{integralItemCouponItemId}
            </if >
            <if test="integralItemProductName != null and integralItemProductName != ''">
                AND t.integral_item_product_name = #{integralItemProductName}
            </if >
        </where>
    </select>

    <select id="getIntegralItemById" resultMap="BaseResultMap" parameterType="Integer" >
        select
        <include refid="Base_List" />
        from tb_iamberry_integral_item t
        where t.integral_item_id= #{integralItemId}
    </select>
    <insert id="save" parameterType="IntegralItem" >
        insert into
        tb_iamberry_integral_item
        (
        integral_item_id,
        integral_item_order_id,
        integral_product_id,
        integral_item_num,
        integral_item_total,
        integral_item_dfbi,
        integral_item_product_type,
        integral_item_coupon_id,
        integral_item_coupon_item_id,
        integral_item_product_name,
        integral_item_product_price,
        integral_item_product_dfbi,
        integral_item_product_pic
        )
        values
        (
        #{integralItemId},
        #{integralItemOrderId},
        #{integralProductId},
        #{integralItemNum},
        #{integralItemTotal},
        #{integralItemDfbi},
        #{integralItemProductType},
        #{integralItemCouponId},
        #{integralItemCouponItemId},
        #{integralItemProductName},
        #{integralItemProductPrice},
        #{integralItemProductDfbi},
        #{integralItemProductPic}
        )
    </insert>
    <update id="update" parameterType="IntegralItem" >
        update
        tb_iamberry_integral_item
        <set >
            <if test="integralItemId != null ">
                integral_item_id = #{integralItemId},
            </if >
            <if test="integralItemOrderId != null and integralItemOrderId != ''">
                integral_item_order_id = #{integralItemOrderId},
            </if >
            <if test="integralProductId != null ">
                integral_product_id = #{integralProductId},
            </if >
            <if test="integralItemNum != null ">
                integral_item_num = #{integralItemNum},
            </if >
            <if test="integralItemTotal != null ">
                integral_item_total = #{integralItemTotal},
            </if >
            <if test="integralItemDfbi != null ">
                integral_item_dfbi = #{integralItemDfbi},
            </if >
            <if test="integralItemProductType != null ">
                integral_item_product_type = #{integralItemProductType},
            </if >
            <if test="integralItemCouponId != null ">
                integral_item_coupon_id = #{integralItemCouponId},
            </if >
            <if test="integralItemCouponItemId != null and integralItemCouponItemId != ''">
                integral_item_coupon_item_id = #{integralItemCouponItemId},
            </if >
            <if test="integralItemProductName != null and integralItemProductName != ''">
                integral_item_product_name = #{integralItemProductName},
            </if >
            <if test="integralItemProductPrice != null ">
                integral_item_product_price = #{integralItemProductPrice},
            </if >
            <if test="integralItemProductDfbi != null ">
                integral_item_product_dfbi = #{integralItemProductDfbi},
            </if >
            <if test="integralItemProductPic != null and integralItemProductPic != ''">
                integral_item_product_pic = #{integralItemProductPic},
            </if >
        </set >
        where integral_item_id= #{integralItemId}
    </update>
    <delete id="delete" parameterType="Integer" >
        delete
        tb_iamberry_integral_item
        where integral_item_id=#{integralItemId}
    </delete>

    <insert id="saveList" parameterType="java.util.List" >
        insert into
        tb_iamberry_integral_item
        (
        integral_item_id,
        integral_item_order_id,
        integral_product_id,
        integral_item_num,
        integral_item_total,
        integral_item_dfbi,
        integral_item_product_type,
        integral_item_coupon_id,
        integral_item_coupon_item_id,
        integral_item_product_name,
        integral_item_product_price,
        integral_item_product_dfbi,
        integral_item_product_pic
        )
        values
        <foreach collection="list" item="node" index="index" separator="," open="" close="">
            (
            #{node.integralItemId},
            #{node.integralItemOrderId},
            #{node.integralProductId},
            #{node.integralItemNum},
            #{node.integralItemTotal},
            #{node.integralItemDfbi},
            #{node.integralItemProductType},
            #{node.integralItemCouponId},
            #{node.integralItemCouponItemId},
            #{node.integralItemProductName},
            #{node.integralItemProductPrice},
            #{node.integralItemProductDfbi},
            #{node.integralItemProductPic}
            )
        </foreach >
    </insert>

</mapper>