<?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.fs.live.mapper.LiveOrderItemMapper">

    <resultMap type="LiveOrderItem" id="LiveOrderItemResult">
        <result property="itemId"    column="item_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderCode"    column="order_code"    />
        <result property="cartId"    column="cart_id"    />
        <result property="goodsId"    column="goods_id"    />
        <result property="productId"    column="product_id"    />
        <result property="productAttrValueId"    column="product_attr_value_id"    />
        <result property="jsonInfo"    column="json_info"    />
        <result property="num"    column="num"    />
        <result property="isAfterSales"    column="is_after_sales"    />
        <result property="isPrescribe"    column="is_prescribe"    />
        <result property="storeId"    column="store_id"    />
        <result property="isGift"    column="is_gift"    />
    </resultMap>
    <resultMap id="liveOrderItemVoMap" type="com.fs.live.vo.LiveOrderItemVo">
        <result property="itemId" column="item_id"/>
        <result property="orderId" column="orderId"/>
        <result property="orderCode" column="order_code"/>
        <result property="cartId" column="cart_id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="productId" column="product_id"/>
        <result property="productAttrValueId" column="product_attr_value_id"/>
        <result property="jsonInfo" column="json_info"/>
        <result property="num" column="num" javaType="java.lang.Long"/>
        <result property="isAfterSales" column="is_after_sales"/>
        <result property="isPrescribe" column="is_prescribe"/>
        <result property="storeId" column="store_id"/>
        <result property="isGift" column="is_gift"/>
        <result property="productName" column="product_name"/>
        <result property="imgUrl" column="img_url"/>
        <result property="price" column="price"/>
        <result property="otPrice" column="ot_price"/>
        <result property="sales" column="sales" javaType="java.lang.Integer"/>
        <result property="productIntroduce" column="product_introduce"/>
    </resultMap>


    <sql id="selectLiveOrderItemVo">
        select item_id, order_id, order_code, cart_id, goods_id, product_id, product_attr_value_id, json_info, num, is_after_sales, is_prescribe, store_id, is_gift from live_order_item
    </sql>

    <select id="selectLiveOrderItemList" parameterType="LiveOrderItem" resultMap="LiveOrderItemResult">
        <include refid="selectLiveOrderItemVo"/>
        <where>
            <if test="orderId != null  and orderId != ''"> and order_id = #{orderId}</if>
            <if test="orderCode != null  and orderCode != ''"> and order_code = #{orderCode}</if>
            <if test="cartId != null  and cartId != ''"> and cart_id = #{cartId}</if>
            <if test="goodsId != null "> and goods_id = #{goodsId}</if>
            <if test="productId != null  and productId != ''"> and product_id = #{productId}</if>
            <if test="productAttrValueId != null "> and product_attr_value_id = #{productAttrValueId}</if>
            <if test="jsonInfo != null  and jsonInfo != ''"> and json_info = #{jsonInfo}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="isAfterSales != null "> and is_after_sales = #{isAfterSales}</if>
            <if test="isPrescribe != null "> and is_prescribe = #{isPrescribe}</if>
            <if test="storeId != null "> and store_id = #{storeId}</if>
            <if test="isGift != null "> and is_gift = #{isGift}</if>
        </where>
    </select>


    <select id="selectCheckedByUserId" parameterType="java.util.List" resultType="com.fs.live.vo.LiveOrderItemVo">
        select a.item_id, a.order_id, a.order_code, a.cart_id, a.goods_id, a.product_id,a. product_attr_value_id, a.json_info, a.num, a.is_after_sales, a.is_prescribe, a.store_id, a.is_gift
        , b.product_name, b.image as img_url, b.price, b.ot_price, b.sales, b.product_info as product_introduce
        from live_order_item a
        left join fs_store_product b on a.product_id = b.product_id
        where a.user_id=#{userId}
    </select>

    <select id="selectLiveOrderItemByItemId" parameterType="String" resultMap="LiveOrderItemResult">
        <include refid="selectLiveOrderItemVo"/>
        where item_id = #{itemId}
    </select>

    <insert id="insertLiveOrderItem" parameterType="LiveOrderItem" useGeneratedKeys="true" keyProperty="itemId">
        insert into live_order_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderId != null and orderId != ''">order_id,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="cartId != null and cartId != ''">cart_id,</if>
            <if test="goodsId != null">goods_id,</if>
            <if test="productId != null and productId != ''">product_id,</if>
            <if test="productAttrValueId != null">product_attr_value_id,</if>
            <if test="jsonInfo != null">json_info,</if>
            <if test="num != null">num,</if>
            <if test="isAfterSales != null">is_after_sales,</if>
            <if test="isPrescribe != null">is_prescribe,</if>
            <if test="storeId != null">store_id,</if>
            <if test="isGift != null">is_gift,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderId != null and orderId != ''">#{orderId},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="cartId != null and cartId != ''">#{cartId},</if>
            <if test="goodsId != null">#{goodsId},</if>
            <if test="productId != null and productId != ''">#{productId},</if>
            <if test="productAttrValueId != null">#{productAttrValueId},</if>
            <if test="jsonInfo != null">#{jsonInfo},</if>
            <if test="num != null">#{num},</if>
            <if test="isAfterSales != null">#{isAfterSales},</if>
            <if test="isPrescribe != null">#{isPrescribe},</if>
            <if test="storeId != null">#{storeId},</if>
            <if test="isGift != null">#{isGift},</if>
         </trim>
    </insert>

    <insert id="insertBatchList" parameterType="java.util.List">
        insert into live_order_item
        ( order_id, order_code, cart_id, goods_id, product_id, product_attr_value_id, json_info, num, is_after_sales, is_prescribe, store_id, is_gift)
        values
        <foreach collection="liveOrderItemList" item="item" separator=",">
            ( #{item.orderId}, #{item.orderCode}, #{item.cartId}, #{item.goodsId}, #{item.productId}, #{item.productAttrValueId}, #{item.jsonInfo}, #{item.num}, #{item.isAfterSales}, #{item.isPrescribe}, #{item.storeId}, #{item.isGift})
        </foreach>
    </insert>

    <update id="updateLiveOrderItem" parameterType="LiveOrderItem">
        update live_order_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null and orderId != ''">order_id = #{orderId},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="cartId != null and cartId != ''">cart_id = #{cartId},</if>
            <if test="goodsId != null">goods_id = #{goodsId},</if>
            <if test="productId != null and productId != ''">product_id = #{productId},</if>
            <if test="productAttrValueId != null">product_attr_value_id = #{productAttrValueId},</if>
            <if test="jsonInfo != null">json_info = #{jsonInfo},</if>
            <if test="num != null">num = #{num},</if>
            <if test="isAfterSales != null">is_after_sales = #{isAfterSales},</if>
            <if test="isPrescribe != null">is_prescribe = #{isPrescribe},</if>
            <if test="storeId != null">store_id = #{storeId},</if>
            <if test="isGift != null">is_gift = #{isGift},</if>
        </trim>
        where item_id = #{itemId}
    </update>

    <delete id="deleteLiveOrderItemByItemId" parameterType="String">
        delete from live_order_item where item_id = #{itemId}
    </delete>

    <delete id="deleteLiveOrderItemByItemIds" parameterType="String">
        delete from live_order_item where item_id in
        <foreach item="itemId" collection="array" open="(" separator="," close=")">
            #{itemId}
        </foreach>
    </delete>
</mapper>
