<?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.tmerclub.cloud.order.mapper.OrderItemMapper">
    <resultMap id="orderItemMap" type="com.tmerclub.cloud.order.model.OrderItem">
        <id column="order_item_id" property="orderItemId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="order_id" property="orderId"/>
        <result column="spu_id" property="spuId"/>
        <result column="sku_id" property="skuId"/>
        <result column="user_id" property="userId"/>
        <result column="final_refund_id" property="finalRefundId"/>
        <result column="distribution_user_id" property="distributionUserId"/>
        <result column="stock_point_id" property="stockPointId"/>
        <result column="count" property="count"/>
        <result column="pic" property="pic"/>
        <result column="is_comm" property="isComm"/>
        <result column="refund_status" property="refundStatus"/>
        <result column="be_delivered_num" property="beDeliveredNum"/>
        <result column="delivery_type" property="deliveryType"/>
        <result column="shop_cart_time" property="shopCartTime"/>
        <result column="price" property="price"/>
        <result column="spu_total_amount" property="spuTotalAmount"/>
        <result column="actual_total" property="actualTotal"/>
        <result column="share_reduce" property="shareReduce"/>
        <result column="platform_share_reduce" property="platformShareReduce"/>
        <result column="distribution_amount" property="distributionAmount"/>
        <result column="distribution_parent_amount" property="distributionParentAmount"/>
        <result column="use_score" property="useScore"/>
        <result column="gain_score" property="gainScore"/>
        <result column="supplier_rate" property="supplierRate"/>
        <result column="item_purchase_amount" property="itemPurchaseAmount"/>
        <result column="purchase_platform_commission" property="purchasePlatformCommission"/>
        <result column="giveaway_amount" property="giveawayAmount"/>
        <result column="supplier_spu_id" property="supplierSpuId"/>
        <result column="supplier_sku_id" property="supplierSkuId"/>
        <result column="spu_mold" property="spuMold"/>
        <result column="is_refund" property="isRefund"/>
        <result column="virtual_remark" property="virtualRemark"/>
    </resultMap>
    <resultMap id="orderItemVOMap" type="com.tmerclub.cloud.common.order.vo.OrderItemVO">
        <id column="order_item_id" property="orderItemId"/>
        <result column="shop_id" property="shopId"/>
        <result column="order_id" property="orderId"/>
        <result column="spu_id" property="spuId"/>
        <result column="sku_id" property="skuId"/>
        <result column="supplier_sku_id" property="supplierSkuId"/>
        <result column="user_id" property="userId"/>
        <result column="final_refund_id" property="finalRefundId"/>
        <result column="distribution_user_id" property="distributionUserId"/>
        <result column="stock_point_id" property="stockPointId"/>
        <result column="count" property="count"/>
        <result column="pic" property="pic"/>
        <result column="is_comm" property="isComm"/>
        <result column="refund_status" property="refundStatus"/>
        <result column="be_delivered_num" property="beDeliveredNum"/>
        <result column="delivery_type" property="deliveryType"/>
        <result column="shop_cart_time" property="shopCartTime"/>
        <result column="price" property="price"/>
        <result column="spu_total_amount" property="spuTotalAmount"/>
        <result column="actual_total" property="actualTotal"/>
        <result column="share_reduce" property="shareReduce"/>
        <result column="platform_share_reduce" property="platformShareReduce"/>
        <result column="distribution_amount" property="distributionAmount"/>
        <result column="distribution_parent_amount" property="distributionParentAmount"/>
        <result column="score_fee" property="scoreFee"/>
        <result column="use_score" property="useScore"/>
        <result column="gain_score" property="gainScore"/>
        <result column="spu_mold" property="spuMold"/>
        <result column="is_refund" property="isRefund"/>
        <result column="virtual_remark" property="virtualRemark"/>
        <result column="giveaway_amount" property="giveawayAmount"/>
        <result column="main_giveaway" property="mainGiveaway"/>
<!--        <result column="main_combo_item_id" property="mainComboItemId"/>-->
        <result column="platform_commission" property="platformCommission"/>
        <result column="shop_change_free_amount" property="shopChangeFreeAmount"/>
        <result column="activity_id" property="activityId"/>
        <result column="activity_type" property="activityType"/>
        <result column="supplier_spu_id" property="supplierSpuId"/>
        <result column="party_code" property="partyCode"/>
        <collection property="orderItemLangList" ofType="com.tmerclub.cloud.common.order.vo.OrderItemLangVO">
            <id column="lang" property="lang"/>
            <result column="spu_name" property="spuName"/>
            <result column="sku_name" property="skuName"/>
        </collection>
    </resultMap>
    <sql id="Vo_Column_List">
        `order_item_id`,
        `create_time`,
        `update_time`,
        `shop_id`,
        `order_id`,
        `category_id`,
        `spu_id`,
        `sku_id`,
        `user_id`,
        `final_refund_id`,
        `distribution_user_id`,
        `stock_point_id`,
        `count`,
        `spu_name`,
        `sku_name`,
        `spu_code`,
        `party_code`,
        `pic`,
        `is_comm`,
        `comm_time`,
        `refund_status`,
        `be_delivered_num`,
        `delivery_type`,
        `shop_cart_time`,
        `price`,
        `spu_total_amount`,
        `actual_total`,
        `share_reduce`,
        `platform_share_reduce`,
        `distribution_amount`,
        `distribution_parent_amount`,
        `use_score`,
        `gain_score`,
        `rate`,
        `platform_commission`,
        `score_amount`,
        `member_amount`,
        `platform_coupon_amount`,
        `shop_coupon_amount`,
        `discount_amount`,
        `platform_free_freight_amount`,
        `free_freight_amount`,
        `shop_change_free_amount`,
        `score_fee`,
        `item_purchase_amount`,
        `purchase_platform_commission`,
        `supplier_rate`,
        `supplier_spu_id`,
        `supplier_sku_id`,
        `gain_score`,
        `giveaway_amount`,
        `spu_name`,
        `sku_name`,
        `activity_id`,
        `activity_type`,
        `spu_mold`,
        main_giveaway
    </sql>

    <sql id="Join_Column_List">
        oi.`order_item_id`,
        oi.`create_time`,
        oi.`update_time`,
        oi.`shop_id`,
        oi.`order_id`,
        oi.`category_id`,
        oi.`spu_id`,
        oi.`sku_id`,
        oi.`user_id`,
        oi.`final_refund_id`,
        oi.`distribution_user_id`,
        oi.`stock_point_id`,
        oi.`count`,
        oi.`spu_code`,
        oi.`party_code`,
        oi.`pic`,
        oi.`is_comm`,
        oi.`comm_time`,
        oi.`refund_status`,
        oi.`be_delivered_num`,
        oi.`delivery_type`,
        oi.`shop_cart_time`,
        oi.`price`,
        oi.`spu_total_amount`,
        oi.`actual_total`,
        oi.`share_reduce`,
        oi.`platform_share_reduce`,
        oi.`distribution_amount`,
        oi.`distribution_parent_amount`,
        oi.`use_score`,
        oi.`gain_score`,
        oi.`rate`,
        oi.`platform_commission`,
        oi.`score_amount`,
        oi.`member_amount`,
        oi.`platform_coupon_amount`,
        oi.`shop_coupon_amount`,
        oi.`discount_amount`,
        oi.`platform_free_freight_amount`,
        oi.`free_freight_amount`,
        oi.`shop_change_free_amount`,
        oi.`score_fee`,
        oi.`item_purchase_amount`,
        oi.`purchase_platform_commission`,
        oi.`supplier_rate`,
        oi.`supplier_spu_id`,
        oi.`supplier_sku_id`,
        oi.`activity_id`,
        oi.`activity_type`,
        oi.`giveaway_amount`,
        oi.`main_giveaway`,
        oi.`is_refund`,
        oi.`spu_mold`,
        oi.`virtual_remark`
    </sql>
    <select id="list" resultType="com.tmerclub.cloud.common.order.vo.OrderItemVO">
        select
        <include refid="Vo_Column_List"/>
        from order_item
        order by order_item_id desc
    </select>
    <select id="getByOrderItemId" resultType="com.tmerclub.cloud.common.order.vo.OrderItemVO">
        select
            oi.`order_item_id`, oi.`create_time`, oi.`update_time`, oi.`shop_id`, oi.`order_id`, oi.`spu_id`, oi.`sku_id`,
            oi.`user_id`, oi.`final_refund_id`, oi.`distribution_user_id`, oi.`count`, oi.`pic`, oi.`is_comm`, oi.`comm_time`, oi.`refund_status`,
            oi.`be_delivered_num`, oi.`delivery_type`, oi.`shop_cart_time`, oi.`price`, oi.`spu_total_amount`, oi.`actual_total`,
            oi.`share_reduce`, oi.`platform_share_reduce`, oi.`distribution_amount`, oi.`distribution_parent_amount`, oi.`use_score`,
            oi.`platform_commission`, oi.`rate`, oi.`gain_score`,shop_change_free_amount,oi.item_purchase_amount,oi.purchase_platform_commission,
            oi.supplier_spu_id, oi.supplier_sku_id, oi.giveaway_amount, oi.activity_id, oi.activity_type, oil.spu_name, oil.sku_name,oi.main_giveaway
        from order_item oi
        left join order_item_lang oil on oi.order_item_id = oil.order_item_id and oil.lang = #{lang}
        where oi.order_item_id = #{orderItemId}
    </select>
    <insert id="save">
        insert into order_item (`order_item_id`, `shop_id`, `order_id`,`category_id`, `spu_id`, `sku_id`, `user_id`, `final_refund_id`,
        `distribution_user_id`, `count`, `pic`, `is_comm`,
        `refund_status`, `be_delivered_num`, `delivery_type`, `shop_cart_time`, `price`,
        `spu_total_amount`, `actual_total`, `share_reduce`, `platform_share_reduce`,
        `distribution_amount`, `distribution_parent_amount`, `score_fee`, `use_score`, `gain_score`,`rate`, `platform_commission`,
        `score_amount`, `member_amount`, `platform_coupon_amount`, `shop_coupon_amount`, `discount_amount`,
        `platform_free_freight_amount`, `free_freight_amount`,`shop_change_free_amount`,`item_purchase_amount`,`purchase_platform_commission`,
        `supplier_rate`,`spu_code`,`party_code`,`supplier_spu_id`,`supplier_sku_id`,`spu_mold`,`is_refund`,`virtual_remark`,
        `giveaway_amount`,`main_giveaway`,`combo_amount`,`activity_id`,`activity_type`,`supplier_pic`,`create_time`)
        values (#{orderItem.orderItemId}, #{orderItem.shopId}, #{orderItem.orderId}, #{orderItem.categoryId},  #{orderItem.spuId}, #{orderItem.skuId}, #{orderItem.userId},
        #{orderItem.finalRefundId}, #{orderItem.distributionUserId}, #{orderItem.count}, #{orderItem.pic},
        #{orderItem.isComm}, #{orderItem.refundStatus}, #{orderItem.beDeliveredNum}, #{orderItem.deliveryType},
        #{orderItem.shopCartTime}, #{orderItem.price}, #{orderItem.spuTotalAmount}, #{orderItem.actualTotal},
        #{orderItem.shareReduce}, #{orderItem.platformShareReduce}, #{orderItem.distributionAmount},
        #{orderItem.distributionParentAmount}, #{orderItem.scoreFee},#{orderItem.useScore}, #{orderItem.gainScore}, #{orderItem.rate}, #{orderItem.platformCommission},
        #{orderItem.scoreAmount}, #{orderItem.memberAmount}, #{orderItem.platformCouponAmount}, #{orderItem.shopCouponAmount}, #{orderItem.discountAmount},
        #{orderItem.platformFreeFreightAmount}, #{orderItem.freeFreightAmount}, #{orderItem.shopChangeFreeAmount},
        #{orderItem.itemPurchaseAmount}, #{orderItem.purchasePlatformCommission}, #{orderItem.supplierRate}, #{orderItem.spuCode},
        #{orderItem.partyCode}, #{orderItem.supplierSpuId}, #{orderItem.supplierSkuId},#{orderItem.spuMold}, #{orderItem.isRefund}, #{orderItem.virtualRemark},
        #{orderItem.giveawayAmount}, #{orderItem.mainGiveaway}, #{orderItem.comboAmount}, #{orderItem.activityId}, #{orderItem.activityType}, #{orderItem.supplierPic}, #{orderItem.createTime})
    </insert>
    <insert id="saveBatch">
        insert into order_item (`order_item_id`, `shop_id`, `order_id`,`category_id`, `spu_id`, `sku_id`, `user_id`, `final_refund_id`,
        `distribution_user_id`, `count`, `pic`, `is_comm`,
        `refund_status`, `be_delivered_num`, `delivery_type`, `shop_cart_time`, `price`,
        `spu_total_amount`, `actual_total`, `share_reduce`, `platform_share_reduce`,
        `distribution_amount`, `distribution_parent_amount`, `score_fee`, `use_score`, `gain_score`,`rate`, `platform_commission`,
        `score_amount`, `member_amount`, `platform_coupon_amount`, `shop_coupon_amount`, `discount_amount`,
        `platform_free_freight_amount`, `free_freight_amount`,`shop_change_free_amount`,`item_purchase_amount`,`purchase_platform_commission`,
        `supplier_rate`,`spu_code`,`party_code`,`supplier_spu_id`,`supplier_sku_id`,`spu_mold`,`is_refund`,`virtual_remark`,
        `giveaway_amount`,`main_giveaway`,`combo_amount`,`activity_id`,`activity_type`,`supplier_pic`,`create_time`,
        `write_off_num`,`write_off_multiple_count`,`write_off_start`,`write_off_end`,`stock_point_id`)
        values
        <foreach collection="orderItems" item="orderItem" separator=",">
            (#{orderItem.orderItemId}, #{orderItem.shopId}, #{orderItem.orderId}, #{orderItem.categoryId},  #{orderItem.spuId}, #{orderItem.skuId}, #{orderItem.userId},
            #{orderItem.finalRefundId}, #{orderItem.distributionUserId}, #{orderItem.count}, #{orderItem.pic},
            #{orderItem.isComm}, #{orderItem.refundStatus}, #{orderItem.beDeliveredNum}, #{orderItem.deliveryType},
            #{orderItem.shopCartTime}, #{orderItem.price}, #{orderItem.spuTotalAmount}, #{orderItem.actualTotal},
            #{orderItem.shareReduce}, #{orderItem.platformShareReduce}, #{orderItem.distributionAmount},
            #{orderItem.distributionParentAmount}, #{orderItem.scoreFee},#{orderItem.useScore}, #{orderItem.gainScore}, #{orderItem.rate}, #{orderItem.platformCommission},
            #{orderItem.scoreAmount}, #{orderItem.memberAmount}, #{orderItem.platformCouponAmount}, #{orderItem.shopCouponAmount}, #{orderItem.discountAmount},
            #{orderItem.platformFreeFreightAmount}, #{orderItem.freeFreightAmount}, #{orderItem.shopChangeFreeAmount},
            #{orderItem.itemPurchaseAmount}, #{orderItem.purchasePlatformCommission}, #{orderItem.supplierRate}, #{orderItem.spuCode},
            #{orderItem.partyCode}, #{orderItem.supplierSpuId}, #{orderItem.supplierSkuId},#{orderItem.spuMold}, #{orderItem.isRefund}, #{orderItem.virtualRemark},
            #{orderItem.giveawayAmount}, #{orderItem.mainGiveaway}, #{orderItem.comboAmount}, #{orderItem.activityId}, #{orderItem.activityType}, #{orderItem.supplierPic}, #{orderItem.createTime},
            #{orderItem.writeOffNum}, #{orderItem.writeOffMultipleCount}, #{orderItem.writeOffStart}, #{orderItem.writeOffEnd}, #{orderItem.stockPointId})
        </foreach>
    </insert>
    <insert id="saveCueOrderItem">
        insert into order_item (
            `order_item_id`,
            `create_time`,
            `shop_id`,
            `order_id`,
            `spu_id`,
            `sku_id`,
            `user_id`,
            `count`,
            `pic`,
            `price`,
            `spu_total_amount`
        ) values (
            #{orderItem.orderItemId},
            #{orderItem.createTime},
            #{orderItem.shopId},
            #{orderItem.orderId},
            #{orderItem.spuId},
            #{orderItem.skuId},
            #{orderItem.userId},
            #{orderItem.count},
            #{orderItem.pic},
            #{orderItem.price},
            #{orderItem.spuTotalAmount}
        )
    </insert>
    <update id="update">
        update order_item
        <set>
            <if test="orderItem.finalRefundId != null">
                `final_refund_id` = #{orderItem.finalRefundId},
            </if>
            <if test="orderItem.distributionUserId != null">
                `distribution_user_id` = #{orderItem.distributionUserId},
            </if>
            <if test="orderItem.count != null">
                `count` = #{orderItem.count},
            </if>
            <if test="orderItem.pic != null">
                `pic` = #{orderItem.pic},
            </if>
            <if test="orderItem.isComm != null">
                `is_comm` = #{orderItem.isComm},
            </if>
            <if test="orderItem.refundStatus != null">
                `refund_status` = #{orderItem.refundStatus},
            </if>
            <if test="orderItem.beDeliveredNum != null">
                `be_delivered_num` = #{orderItem.beDeliveredNum},
            </if>
            <if test="orderItem.deliveryType != null">
                `delivery_type` = #{orderItem.deliveryType},
            </if>
            <if test="orderItem.shopCartTime != null">
                `shop_cart_time` = #{orderItem.shopCartTime},
            </if>
            <if test="orderItem.price != null">
                `price` = #{orderItem.price},
            </if>
            <if test="orderItem.spuTotalAmount != null">
                `spu_total_amount` = #{orderItem.spuTotalAmount},
            </if>
            <if test="orderItem.actualTotal != null">
                `actual_total` = #{orderItem.actualTotal},
            </if>
            <if test="orderItem.shareReduce != null">
                `share_reduce` = #{orderItem.shareReduce},
            </if>
            <if test="orderItem.platformShareReduce != null">
                `platform_share_reduce` = #{orderItem.platformShareReduce},
            </if>
            <if test="orderItem.distributionAmount != null">
                `distribution_amount` = #{orderItem.distributionAmount},
            </if>
            <if test="orderItem.platformCommission != null">
                `platform_commission` = #{orderItem.platformCommission},
            </if>
            <if test="orderItem.distributionParentAmount != null">
                `distribution_parent_amount` = #{orderItem.distributionParentAmount},
            </if>
            <if test="orderItem.shopChangeFreeAmount != null">
                `shop_change_free_amount` = #{orderItem.shopChangeFreeAmount},
            </if>
            <if test="orderItem.useScore != null">
                `use_score` = #{orderItem.useScore},
            </if>
            <if test="orderItem.gainScore != null">
                `gain_score` = #{orderItem.gainScore},
            </if>
            <if test="orderItem.updateTime != null">
                `update_time` = #{orderItem.updateTime},
            </if>
            <if test="orderItem.commTime != null">
                `comm_time` = #{orderItem.commTime},
            </if>
        </set>
        where order_item_id = #{orderItem.orderItemId}
    </update>
    <delete id="deleteById">
        delete
        from order_item
        where order_item_id = #{orderItemId}
    </delete>
    <delete id="deleteBatchByIds">
        delete
        from order_item
        where order_item_id in
        <foreach collection="orderItemIds" item="orderItemId" separator="," open="(" close=")">
            #{orderItemId}
        </foreach>
    </delete>

    <select id="listOrderItemsByOrderId" resultMap="orderItemMap">
        select <include refid="Vo_Column_List" />
        from order_item
        where order_id = #{orderId}
    </select>

    <select id="listByIds" resultMap="orderItemMap">
        select <include refid="Vo_Column_List" />
        from order_item
        where order_item_id in
        <foreach collection="itemIds" item="orderItemId" open="(" separator="," close=")">
            #{orderItemId}
        </foreach>
    </select>

    <select id="listOrderItemsByOrderIds" resultMap="orderItemMap">
        select toi.*
        from order_item toi
        where toi.order_id = #{orderId}
        AND (ISNULL(toi.activity_type) OR  (toi.activity_type != 5 AND toi.activity_type != 6))
    </select>

    <select id="listOrderItemAndLangByOrderIds" resultMap="orderItemVOMap">
        select <include refid="Join_Column_List"/>, oil.lang,oil.sku_name,oil.spu_name
        from order_item oi
        join order_item_lang oil on oi.order_item_id = oil.order_item_id
        where oi.order_id in
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator="OR">
            #{orderId}
        </foreach>
    </select>

    <select id="sumTotalDistributionAmountByOrderIds"
            resultType="com.tmerclub.cloud.api.order.bo.DistributionAmountWithOrderIdBO">
        select order_id,ifnull(sum(distribution_amount), 0) as distributionAmount,ifnull(sum(distribution_parent_amount), 0) as distributionParentAmount from order_item
        where (refund_status IS NULL OR refund_status != 5) and
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator="OR">
            order_id = #{orderId}
        </foreach>
    </select>

    <select id="countByOrderId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM order_item
        WHERE order_id = #{orderId}
    </select>

    <select id="sumNumByOrderId" resultType="java.lang.Integer">
        SELECT ifnull(sum(count),0)
        FROM order_item
        WHERE order_id = #{orderId}
        and (ISNULL(activity_type) OR  (activity_type != 5 AND activity_type != 6))
    </select>

    <update id="updateByDelivery" >
        update order_item
        set be_delivered_num = be_delivered_num - #{orderItem.changeNum}, delivery_type = #{deliveryType}
        where order_item_id = #{orderItem.orderItemId}
    </update>

    <select id="countUnDeliveryNumByOrderId" resultType="int">
        select sum(be_delivered_num) from order_item where be_delivered_num != -1 and order_id = #{orderId}
    </select>

    <select id="listDetailByOrderId" resultType="com.tmerclub.cloud.order.vo.OrderItemDetailVO">
        SELECT o.shop_name,oil.spu_name,oil.sku_name,oil.supplier_spu_name,oil.supplier_sku_name,o.shop_name,toi.spu_total_amount,toi.actual_total,toi.category_id,toi.count,toi.order_item_id,
               (IFNULL(toi.share_reduce,0) - IFNULL(toi.platform_share_reduce,0)) AS multishop_reduce,toi.platform_share_reduce,
               (IFNULL(toi.distribution_amount,0) + IFNULL(toi.distribution_parent_amount,0)) AS distribution_amount,
               toi.use_score,toi.rate,toi.platform_commission,toi.score_amount,toi.member_amount,toi.platform_coupon_amount,
               toi.purchase_platform_commission,toi.supplier_rate,toi.shop_coupon_amount,toi.discount_amount,toi.platform_free_freight_amount,
               toi.free_freight_amount,toi.shop_change_free_amount,toi.item_purchase_amount, toi.activity_type
        FROM `order_item` toi
        JOIN `order` o ON o.order_id = toi.order_id
        JOIN `order_item_lang` oil ON toi.order_item_id = oil.order_item_id and oil.lang = #{lang}
        WHERE toi.order_id = #{orderId}
    </select>
    <update id="reduceUnDeliveryNumByOrderItemId">
        UPDATE order_item set be_delivered_num=be_delivered_num - #{count} WHERE order_item_id = #{orderItemId}
    </update>

    <update id="updateRefundStatusByOrderId">
        UPDATE order_item set refund_status=#{refundStatus} WHERE order_id = #{orderId}
    </update>
    <select id="getSpuInfoByOrderItemId" resultType="com.tmerclub.cloud.common.order.vo.OrderItemVO">
        SELECT oil.spu_name, oil.sku_name, oi.create_time FROM order_item oi
        LEFT JOIN order_item_lang oil ON oi.order_item_id = oil.order_item_id
        WHERE oi.order_item_id = #{orderItemId} AND oil.lang = #{lang}
    </select>
    <select id="getDevTypeByOrderItemId" resultType="int">
        SELECT ifnull(delivery_type,-1) FROM `order_item` WHERE order_item_id = #{orderItemId}
    </select>
    <select id="getDevTypeByOrderId" resultType="java.lang.Integer">
        SELECT delivery_type FROM `order_item` WHERE order_id = #{orderId}
    </select>

    <update id="updateBatchDistributionAmount">
        UPDATE order_item set distribution_amount = #{orderItem.distributionAmount}
        <if test="orderItem.distributionParentAmount != null and orderItem.distributionParentAmount >= 0">
            ,distribution_parent_amount = #{orderItem.distributionParentAmount}
        </if>
        WHERE order_item_id = #{orderItem.orderItemId} and distribution_amount = 0
    </update>

    <select id="listOrderItemByOrderIds" resultMap="orderItemVOMap">
        SELECT order_id,spu_id,activity_id,activity_type,count FROM order_item WHERE
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator="OR">
            order_id = #{orderId}
        </foreach>
    </select>

    <select id="getUserScoreLogDetail" resultType="com.tmerclub.cloud.api.order.bo.UserScoreLogOrderBO">
        SELECT order_id, spu_total_amount as actual_total, `count` as all_count, price as score_fee
        FROM `order_item`
        WHERE
        <foreach collection="orderIds" item="orderId" open="(" close=")" separator="OR">
            order_id = #{orderId}
        </foreach>
    </select>

    <select id="getOrderItemByGiveawayOrderItemId" resultMap="orderItemVOMap">
        select toi.order_item_id,toi.shop_id,toi.order_id,toi.spu_id,toi.sku_id,toi.user_id,toi.final_refund_id,toi.distribution_user_id,toi.platform_commission,toi.shop_change_free_amount,
               toi.pic,toi.is_comm,toi.refund_status,toi.be_delivered_num,toi.delivery_type,toi.shop_cart_time,toi.price,toi.spu_total_amount,
               toi.actual_total,toi.share_reduce,toi.platform_share_reduce,toi.distribution_amount,toi.distribution_parent_amount,toi.score_fee,
               toi.use_score,toi.gain_score,toi.count,oil.lang,oil.sku_name,oil.spu_name,toi.giveaway_amount,toi.activity_type, toi.activity_id
        from order_item toi
        join order_item_lang oil on toi.order_item_id = oil.order_item_id
        where toi.user_id = #{userId} and toi.activity_type = 5 and toi.activity_id = #{giveawayOrderItemId}
        order by order_item_id desc
    </select>

    <select id="getOrderItemByGiveawayOrderItemIds" resultMap="orderItemVOMap">
        select toi.order_item_id,toi.shop_id,toi.order_id,toi.spu_id,toi.sku_id,toi.user_id,toi.final_refund_id,toi.distribution_user_id,
        toi.pic,toi.is_comm,toi.refund_status,toi.be_delivered_num,toi.delivery_type,toi.shop_cart_time,toi.price,toi.spu_total_amount,
        toi.actual_total,toi.share_reduce,toi.platform_share_reduce,toi.distribution_amount,toi.distribution_parent_amount,toi.score_fee,
        toi.use_score,toi.gain_score,toi.count,oil.lang,oil.sku_name,oil.spu_name,toi.giveaway_amount,toi.activity_id, toi.activity_type
        from order_item toi
        join order_item_lang oil on toi.order_item_id = oil.order_item_id
        WHERE toi.user_id = #{userId}
        <if test="sysType != null" >
            and toi.activity_type in (5,6)
        </if>
        and
        <foreach collection="giveawayOrderItemIds" open="(" item="giveawayOrderItemId" separator="OR" close=")">
            <if test="sysType != null" >
                toi.activity_id =
            </if>
            <if test="sysType == null" >
                toi.order_item_id =
            </if>
            #{giveawayOrderItemId}
        </foreach>
        ORDER BY
        toi.order_item_id DESC
    </select>

    <select id="listOrderItemsByUserIdAndOrderIds" resultMap="orderItemVOMap">
        select <include refid="Join_Column_List"/>,oil.lang,oil.sku_name,oil.spu_name
        from order_item oi
        join order_item_lang oil on oi.order_item_id = oil.order_item_id
        WHERE
        <foreach collection="orderIds" item="orderId" separator="OR" open="(" close=")">
            oi.order_id = #{orderId}
        </foreach>
    </select>

    <select id="listOrderItemAndLangByOrderItemIds" resultMap="orderItemVOMap">
        select <include refid="Join_Column_List"/>, oil.lang,oil.sku_name,oil.spu_name
        from order_item oi
        join order_item_lang oil on oi.order_item_id = oil.order_item_id
        where oi.order_item_id in
        <foreach collection="orderItemIds" item="orderItemId" open="(" close=")" separator=",">
            #{orderItemId}
        </foreach>
    </select>
</mapper>
