<?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.yami.shop.dao.OrderItemMapper">
    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.OrderItem">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
        <result column="shop_id" jdbcType="BIGINT" property="shopId"/>
        <result column="order_number" jdbcType="VARCHAR" property="orderNumber"/>
        <result column="prod_id" jdbcType="BIGINT" property="prodId"/>
        <result column="sku_id" jdbcType="BIGINT" property="skuId"/>
        <result column="prod_count" jdbcType="INTEGER" property="prodCount"/>
        <result column="gain_score" jdbcType="INTEGER" property="gainScore"/>
        <result column="use_score" jdbcType="INTEGER" property="useScore"/>
        <result column="prod_name" jdbcType="VARCHAR" property="prodName"/>
        <result column="pic" jdbcType="VARCHAR" property="pic"/>
        <result column="price" jdbcType="DECIMAL" property="price"/>
        <result column="user_id" jdbcType="VARCHAR" property="userId"/>
        <result column="product_total_amount" jdbcType="DECIMAL" property="productTotalAmount"/>
        <result column="subsidy_amount" jdbcType="DECIMAL" property="subsidyAmount"/>
        <result column="rec_time" jdbcType="TIMESTAMP" property="recTime"/>
        <result column="comm_sts" jdbcType="INTEGER" property="commSts"/>
        <result column="actual_total" jdbcType="DECIMAL" property="actualTotal"/>
        <result column="basket_date" property="basketDate"/>
        <result column="distribution_card_no" property="distributionCardNo"/>
        <result column="share_reduce" jdbcType="DECIMAL" property="shareReduce"/>
        <result column="dvy_type" jdbcType="INTEGER" property="dvyType"/>
    </resultMap>


    <select id="listByOrderNumber" resultType="com.yami.shop.bean.model.OrderItem">
    	select sku.spec,toi.*, tor.return_money_sts, tor.refund_sn from tz_order_item toi
        left JOIN tz_order_refund tor ON tor.`order_item_id` = toi.`order_item_id` AND tor.`return_money_sts` not in (30, 40)
        LEFT JOIN tz_sku sku on toi.sku_id=sku.sku_id
        where toi.order_number = #{orderNumber}
    </select>


    <update id="updateByDeliveries">
        <foreach collection="orderItems" item="orderItem" separator=";">
            UPDATE tz_order_item oi  set status=#{orderItem.prodCount} - #{orderItem.changeNum}, dvy_type = #{orderItem.dvyType}  WHERE order_item_id = #{orderItem.orderItemId}
        </foreach>
    </update>
    <select id="getListByOrderNumber" resultType="com.yami.shop.bean.model.OrderItem">
        SELECT r.return_money_sts,sku.spec,sku.hb_sku_id,sku.sku_code,oi.* FROM tz_order_item oi
        JOIN tz_order o ON o.order_number = oi.order_number  AND o.order_number = #{orderNumber}
        LEFT JOIN tz_order_refund r ON r.order_item_id = oi.order_item_id
        LEFT JOIN tz_sku sku on oi.sku_id=sku.sku_id
        WHERE  r.return_money_sts != 5 OR r.return_money_sts IS NULL
    </select>

    <select id="getRefundTypeByOrderItem2" resultType="com.yami.shop.bean.model.OrderItem">
        SELECT a.product_count,b.*,(a.product_count * b.price) goods_total FROM tz_order_refund_sku a
        LEFT JOIN tz_order_item b on a.order_item_id=b.order_item_id
        where a.order_refund_id=#{refundId}
    </select>

    <!--<select id="listByOrderNumber" resultType="com.yami.shop.bean.model.OrderItem">-->
        <!--SELECT toi.*, tor.return_money_sts, tor.refund_sn-->
        <!--FROM tz_order_item toi-->
        <!--LEFT JOIN tz_order_refund tor ON tor.`order_item_id` = toi.`order_item_id` AND tor.return_money_sts NOT IN (6,7, -1)-->
        <!--WHERE tor.order_item_id  <![CDATA[ <> ]]> 0 AND toi.order_number = #{orderNumber}-->
        <!--UNION-->
        <!--SELECT toi.*, tor.return_money_sts, tor.refund_sn FROM tz_order_item toi ,tz_order_refund tor, tz_order tzor-->
        <!--WHERE tor.order_item_id  = 0 AND tor.order_id = tzor.order_id AND tzor.`order_number`=toi.`order_number` AND tor.return_money_sts NOT IN (6,7, -1)-->
        <!--AND toi.`order_number` = #{orderNumber}-->
    <!--</select>-->

    <select id="refundIngCount" resultType="integer">
        SELECT IFNULL(SUM(product_count), 0) c
        FROM `tz_order_refund_sku` a
                 LEFT JOIN tz_order_refund b on a.order_refund_id = b.refund_id
        WHERE a.order_item_id = #{orderItemId}
          and b.return_money_sts not in(30,40,70);
    </select>

    <select id="refundSuccessCount" resultType="integer">
        SELECT IFNULL(SUM(product_count), 0) c
        FROM `tz_order_refund_sku` a
                 LEFT JOIN tz_order_refund b on a.order_refund_id = b.refund_id
        WHERE a.order_item_id = #{orderItemId}
          and b.return_money_sts =70;
    </select>

    <select id="refundCount" resultType="integer">
        SELECT IFNULL(SUM(product_count), 0) c
        FROM `tz_order_refund_sku` a
                 LEFT JOIN tz_order_refund b on a.order_refund_id = b.refund_id
        WHERE a.order_item_id = #{orderItemId}
          and b.return_money_sts not in (30,40)
    </select>
    <select id="findBySkuIdAndOrderNumber" resultType="com.yami.shop.bean.model.OrderItem">
        select * from tz_order_item where order_number = #{orderNumber} and sku_id =#{skuId}
    </select>

</mapper>
