<?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.OrderRefundMapper">
    <resultMap id="orderRefundMap" type="com.tmerclub.cloud.order.model.OrderRefund">
        <id column="refund_id" property="refundId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="user_id" property="userId"/>
        <result column="order_id" property="orderId"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="refund_count" property="refundCount"/>
        <result column="refund_score" property="refundScore"/>
        <result column="refund_amount" property="refundAmount"/>
        <result column="platform_refund_amount" property="platformRefundAmount"/>
        <result column="platform_refund_commission" property="platformRefundCommission"/>
        <result column="distribution_total_amount" property="distributionTotalAmount"/>
        <result column="refund_type" property="refundType"/>
        <result column="apply_type" property="applyType"/>
        <result column="is_received" property="isReceived"/>
        <result column="close_type" property="closeType"/>
        <result column="return_money_sts" property="returnMoneySts"/>
        <result column="buyer_reason" property="buyerReason"/>
        <result column="buyer_desc" property="buyerDesc"/>
        <result column="buyer_mobile" property="buyerMobile"/>
        <result column="img_urls" property="imgUrls"/>
        <result column="over_time" property="overTime"/>
        <result column="reject_message" property="rejectMessage"/>
        <result column="seller_msg" property="sellerMsg"/>
        <result column="handel_time" property="handelTime"/>
        <result column="delivery_time" property="deliveryTime"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="shop_img_urls" property="shopImgUrls"/>
        <result column="close_time" property="closeTime"/>
        <result column="supplier_time" property="supplierTime"/>
        <result column="decision_time" property="decisionTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="supplier_id" property="supplierId"/>
        <result column="supplier_handle_status" property="supplierHandleStatus"/>
        <result column="purchase_refund_amount" property="purchaseRefundAmount"/>
        <result column="pur_platform_refund_commission" property="purPlatformRefundCommission"/>
        <result column="return_giveaway_ids" property="returnGiveawayIds"/>
        <result column="refund_actual_total" property="refundActualTotal"/>
        <result column="shop_seller_msg" property="shopSellerMsg"/>
        <result column="platform_intervention_status" property="platformInterventionStatus"/>
        <result column="apply_intervention_img_urls" property="applyInterventionImgUrls"/>
        <result column="apply_intervention_reason" property="applyInterventionReason"/>
        <result column="apply_intervention_time" property="applyInterventionTime"/>
        <result column="platform_message" property="platformMessage"/>
        <result column="intervention_finish_time" property="interventionFinishTime"/>
        <result column="intervention_refund_type" property="interventionRefundType"/>
        <result column="after_intervention_refund_amount" property="afterInterventionRefundAmount"/>
    </resultMap>

    <resultMap type="com.tmerclub.cloud.order.vo.OrderRefundVO" id="orderRefundWithItemMap" extends="orderRefundMap">
        <result column="o_pay_time" property="payTime"/>
        <result column="o_actual_total" property="actualTotal"/>
        <result column="o_status" property="orderStatus"/>
        <result column="o_refund_status" property="refundStatus"/>
        <result column="o_shop_name" property="shopName"/>
        <result column="o_order_score" property="orderScore"/>
        <collection property="orderItems" columnPrefix="oi_" ofType="com.tmerclub.cloud.common.order.vo.RefundOrderItemVO">
            <id column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
            <result column="count" property="count"/>
            <result column="pic" property="pic"/>
            <result column="spu_id" property="spuId"/>
            <result column="price" property="price"/>
            <result column="party_code" property="partyCode"/>
            <result column="spu_total_amount" property="spuTotalAmount"/>
            <result column="actual_total" property="actualTotal"/>
            <result column="platform_share_reduce" property="platformShareReduce"/>
            <result column="refund_score" property="refundScore"/>
            <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>
        </collection>
    </resultMap>

    <resultMap type="com.tmerclub.cloud.order.bo.mongo.MongoOrderRefundItemBO" id="mongoOrderRefundItemMap" extends="orderRefundMap">
        <id column="order_item_id" property="orderItemId"/>
        <result column="spu_id" property="spuId"/>
        <result column="sku_id" property="skuId"/>
        <result column="pic" property="pic"/>
        <result column="party_code" property="partyCode"/>
        <result column="count" property="count"/>
        <result column="price" property="price"/>
        <result column="spu_total_amount" property="spuTotalAmount"/>
        <result column="actual_total" property="actualTotal"/>
        <result column="refund_amount" property="refundAmount"/>
        <result column="platform_share_reduce" property="platformShareReduce"/>
        <result column="refund_score" property="refundScore"/>
        <result column="activity_id" property="activityId"/>
        <result column="activity_type" property="activityType"/>
        <result column="supplier_pic" property="supplierPic"/>
        <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"/>
            <result column="supplier_spu_name" property="supplierSpuName"/>
            <result column="supplier_sku_name" property="supplierSkuName"/>
        </collection>
    </resultMap>

    <sql id="Vo_Column_List">
        `refund_id`,
        `create_time`,
        `update_time`,
        `shop_id`,
        `user_id`,
        `order_id`,
        `order_item_id`,
        `refund_count`,
        `refund_amount`,
        `platform_refund_commission`,
        `platform_refund_amount`,
        `distribution_total_amount`,
        `refund_type`,
        `apply_type`,
        `is_received`,
        `close_type`,
        `return_money_sts`,
        `buyer_reason`,
        `buyer_desc`,
        `buyer_mobile`,
        `refund_actual_total`,
        `img_urls`,
        `over_time`,
        `reject_message`,
        `seller_msg`,
        `handel_time`,
        `supplier_time`,
        `delivery_time`,
        `receive_time`,
        `close_time`,
        `decision_time`,
        `supplier_handle_status`,
        `purchase_refund_amount`,
        `supplier_id`,
        `pur_platform_refund_commission`,
        `refund_time`,
        `shop_img_urls`,
        `return_giveaway_ids`,
        `platform_intervention_status`,
        `apply_intervention_reason`,
        `apply_intervention_img_urls`,
        `apply_intervention_time`,
        `intervention_refund_type`,
        `after_intervention_refund_amount`,
        `platform_message`,
        `intervention_finish_time`,
        `shop_seller_msg`
    </sql>


    <sql id="Join_Column_List">
        r.`refund_id`,
        r.`create_time`,
        r.`update_time`,
        r.`shop_id`,
        r.`user_id`,
        r.`order_id`,
        r.`order_item_id`,
        r.`refund_count`,
        r.`refund_amount`,
        r.`platform_refund_commission`,
        r.`platform_refund_amount`,
        r.`distribution_total_amount`,
        r.`refund_type`,
        r.`apply_type`,
        r.`Refund_actual_total`,
        r.`is_received`,
        r.`close_type`,
        r.`return_money_sts`,
        r.`buyer_reason`,
        r.`buyer_desc`,
        r.`buyer_mobile`,
        r.`img_urls`,
        r.`over_time`,
        r.`reject_message`,
        r.`seller_msg`,
        r.`handel_time`,
        r.`delivery_time`,
        r.`receive_time`,
        r.`close_time`,
        r.`decision_time`,
        r.`supplier_handle_status`,
        r.`purchase_refund_amount`,
        r.`supplier_id`,
        r.`pur_platform_refund_commission`,
        r.`refund_time`,
        r.`platform_intervention_status`,
        r.`apply_intervention_reason`,
        r.`apply_intervention_img_urls`,
        r.`apply_intervention_time`,
        r.`intervention_refund_type`,
        r.`after_intervention_refund_amount`,
        r.`platform_message`,
        r.`intervention_finish_time`
    </sql>

    <select id="getByRefundId" resultMap="orderRefundWithItemMap">
        select
        <include refid="Vo_Column_List"/>
        from order_refund
        where refund_id = #{refundId}
    </select>

    <insert id="save">
        insert into order_refund (`refund_id`, `shop_id`, `user_id`, `pay_sys_type`, `order_id`, `order_item_id`, `refund_count`,
                                  `refund_amount`,`platform_refund_commission`,
                                  `platform_refund_amount`, `distribution_total_amount`, `refund_type`, `apply_type`,
                                  `is_received`, `close_type`, `return_money_sts`, `buyer_reason`, `buyer_desc`,
                                  `buyer_mobile`, `img_urls`, `over_time`, `reject_message`, `seller_msg`,
                                  `handel_time`, `delivery_time`, `receive_time`, `close_time`, `decision_time`,
                                  `refund_time`,`supplier_id`,`purchase_refund_amount`,`pur_platform_refund_commission`,
                                  `return_giveaway_ids`,`refund_actual_total`)
        values (#{orderRefund.refundId}, #{orderRefund.shopId}, #{orderRefund.userId}, #{orderRefund.paySysType}, #{orderRefund.orderId},
                #{orderRefund.orderItemId},#{orderRefund.refundCount}, #{orderRefund.refundAmount},
                #{orderRefund.platformRefundCommission},#{orderRefund.platformRefundAmount},
                #{orderRefund.distributionTotalAmount}, #{orderRefund.refundType}, #{orderRefund.applyType},
                #{orderRefund.isReceived}, #{orderRefund.closeType}, #{orderRefund.returnMoneySts},
                #{orderRefund.buyerReason}, #{orderRefund.buyerDesc}, #{orderRefund.buyerMobile},
                #{orderRefund.imgUrls}, #{orderRefund.overTime}, #{orderRefund.rejectMessage}, #{orderRefund.sellerMsg},
                #{orderRefund.handelTime}, #{orderRefund.deliveryTime}, #{orderRefund.receiveTime},
                #{orderRefund.closeTime}, #{orderRefund.decisionTime}, #{orderRefund.refundTime},
                #{orderRefund.supplierId}, #{orderRefund.purchaseRefundAmount}, #{orderRefund.purPlatformRefundCommission},
                #{orderRefund.returnGiveawayIds},#{orderRefund.refundActualTotal});
    </insert>
    <update id="update">
        update order_refund
        <set>
            <if test="orderRefund.refundCount != null">
                `refund_count` = #{orderRefund.refundCount},
            </if>
            <if test="orderRefund.refundAmount != null">
                `refund_amount` = #{orderRefund.refundAmount},
            </if>
            <if test="orderRefund.platformRefundAmount != null">
                `platform_refund_amount` = #{orderRefund.platformRefundAmount},
            </if>
            <if test="orderRefund.distributionTotalAmount != null">
                `distribution_total_amount` = #{orderRefund.distributionTotalAmount},
            </if>
            <if test="orderRefund.refundType != null">
                `refund_type` = #{orderRefund.refundType},
            </if>
            <if test="orderRefund.applyType != null">
                `apply_type` = #{orderRefund.applyType},
            </if>
            <if test="orderRefund.isReceived != null">
                `is_received` = #{orderRefund.isReceived},
            </if>
            <if test="orderRefund.closeType != null">
                `close_type` = #{orderRefund.closeType},
            </if>
            <if test="orderRefund.returnMoneySts != null">
                `return_money_sts` = #{orderRefund.returnMoneySts},
            </if>
            <if test="orderRefund.buyerReason != null">
                `buyer_reason` = #{orderRefund.buyerReason},
            </if>
            <if test="orderRefund.buyerDesc != null">
                `buyer_desc` = #{orderRefund.buyerDesc},
            </if>
            <if test="orderRefund.buyerMobile != null">
                `buyer_mobile` = #{orderRefund.buyerMobile},
            </if>
            <if test="orderRefund.imgUrls != null">
                `img_urls` = #{orderRefund.imgUrls},
            </if>
            <if test="orderRefund.overTime != null">
                `over_time` = #{orderRefund.overTime},
            </if>
            <if test="orderRefund.rejectMessage != null">
                `reject_message` = #{orderRefund.rejectMessage},
            </if>
            <if test="orderRefund.sellerMsg != null">
                `seller_msg` = #{orderRefund.sellerMsg},
            </if>
            <if test="orderRefund.handelTime != null">
                `handel_time` = #{orderRefund.handelTime},
            </if>
            <if test="orderRefund.deliveryTime != null">
                `delivery_time` = #{orderRefund.deliveryTime},
            </if>
            <if test="orderRefund.receiveTime != null">
                `receive_time` = #{orderRefund.receiveTime},
            </if>
            <if test="orderRefund.closeTime != null">
                `close_time` = #{orderRefund.closeTime},
            </if>
            <if test="orderRefund.decisionTime != null">
                `decision_time` = #{orderRefund.decisionTime},
            </if>
            <if test="orderRefund.refundTime != null">
                `refund_time` = #{orderRefund.refundTime},
            </if>
            <if test="orderRefund.updateTime != null">
                `update_time` = #{orderRefund.updateTime},
            </if>
            <if test="orderRefund.supplierHandleStatus != null">
                `supplier_handle_status` = #{orderRefund.supplierHandleStatus},
            </if>
            <if test="orderRefund.shopSellerMsg != null">
                `shop_seller_msg` = #{orderRefund.shopSellerMsg},
            </if>
            <if test="orderRefund.platformInterventionStatus != null">
                `platform_intervention_status` = #{orderRefund.platformInterventionStatus},
            </if>
            <if test="orderRefund.platformInterventionStatus == 1">
                `intervention_finish_time` = null,
            </if>
            <if test="orderRefund.applyInterventionReason != null">
                `apply_intervention_reason` = #{orderRefund.applyInterventionReason},
            </if>
            <if test="orderRefund.applyInterventionImgUrls != null and orderRefund.applyInterventionImgUrls != ''">
                `apply_intervention_img_urls` = #{orderRefund.applyInterventionImgUrls},
            </if>
            <if test="orderRefund.applyInterventionTime != null">
                `apply_intervention_time` = #{orderRefund.applyInterventionTime},
            </if>
            <if test="orderRefund.refundActualTotal != null">
                `refund_actual_total` = #{orderRefund.refundActualTotal},
            </if>
            <if test="orderRefund.interventionFinishTime != null">
                `intervention_finish_time` = #{orderRefund.interventionFinishTime}
            </if>
        </set>
        where refund_id = #{orderRefund.refundId}
    </update>
    <delete id="deleteById">
        delete
        from order_refund
        where refund_id = #{refundId}
    </delete>
    <select id="getProcessOrderRefundByOrderId" resultMap="orderRefundMap">
        SELECT <include refid="Vo_Column_List" />
        FROM order_refund
        WHERE order_id = #{orderId}
          AND (return_money_sts != -1 or platform_intervention_status in (1,2,5))
    </select>
    <select id="listSuccessOrderRefundByOrderId" resultMap="orderRefundMap">
        SELECT <include refid="Vo_Column_List" />
        FROM order_refund
        WHERE order_id = #{orderId}
          AND (return_money_sts = 5 or platform_intervention_status = 5)
    </select>
    <select id="getProcessOrderRefundByOrderIds" resultType="com.tmerclub.cloud.order.vo.OrderRefundVO">
        SELECT r.*
        FROM order_refund r
        WHERE r.order_item_id <![CDATA[ <> ]]> 0 and
        <foreach collection="orderIds" item="orderId" open="(" separator="OR" close=")">
            r.order_id = #{orderId}
        </foreach>
        AND (return_money_sts != -1 or platform_intervention_status in (1,2,5))
    </select>

    <select id="getOrderRefundByOrderIdAndRefundStatus" resultType="com.tmerclub.cloud.api.order.vo.OrderRefundVO">
        SELECT *
        FROM order_refund
        WHERE order_id = #{orderId}
          AND return_money_sts = #{returnMoneySts}
    </select>

    <select id="countByReturnMoneyStsAndOrderId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM order_refund
        WHERE return_money_sts &gt; #{returnMoneySts}
          AND return_money_sts &lt; #{returnMoneySts2}
          AND order_id = #{orderId}
    </select>

    <select id="countReturnProcessingItemByOrderId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM order_refund
        WHERE order_id = #{orderId} and (return_money_sts = 5 or platform_intervention_status = 5)
    </select>

    <select id="countRefundSuccessRefundCountByOrderId" resultType="java.lang.Integer">
        SELECT ifnull(SUM(refund_count),0)
        FROM order_refund
        WHERE order_id = #{orderId}
          and (return_money_sts = 5 or platform_intervention_status = 5)
    </select>

    <select id="countSuccessItemNum" resultType="java.lang.Integer">
        SELECT ifnull(count(*),0)
        FROM order_refund
        WHERE order_id = #{orderId}
        and (return_money_sts = 5 or platform_intervention_status = 5)
    </select>

    <update id="agreeReturns">
        update order_refund
        set return_money_sts = 2,
            seller_msg       = #{sellerMsg},
            handel_time      = NOW()
        where refund_id = #{refundId}
          and return_money_sts = 1
    </update>

    <update id="disagreeRefund">
        update order_refund
        set return_money_sts = -1,
        reject_message   = #{rejectMessage},
        seller_msg       = #{sellerMsg},
        close_type       = 2,
        close_time       = NOW(),
        <if test="receiver == 1">
            receive_time = NOW(),
        </if>
        <if test="returnMoneySts == 1">
            handel_time  = NOW(),
        </if>
        <if test="supplierHandleStatus != null">
            supplier_handle_status = #{supplierHandleStatus},
        </if>
        <if test="imgUrls != null and imgUrls != ''">
            shop_img_urls = #{imgUrls},
        </if>
        is_received      = #{receiver}
        where refund_id = #{refundId}
        and return_money_sts = #{returnMoneySts}
    </update>

    <update id="disagreeIntervention">
        update order_refund
        set platform_message      = #{platformMessage},
        intervention_finish_time    = NOW(),
        platform_intervention_status = #{applyInterventionStatus}
        where refund_id = #{refundId}
        and platform_intervention_status = 1
    </update>

    <update id="agreeRefund">
        update order_refund
        set return_money_sts = 2,
        <if test="receiver == 1">
            receive_time = NOW(),
        </if>
        <if test="applyType == 1">
            handel_time  = NOW(),
        </if>
        decision_time = NOW(),
        <if test="sellerMsg != null and sellerMsg != ''">
            seller_msg = #{sellerMsg},
        </if>
        is_received      = #{receiver},
        refund_amount    = #{refundAmount},
        purchase_refund_amount    = #{purchaseRefundAmount}

        where refund_id = #{refundId}
        and return_money_sts = #{returnMoneySts}
    </update>

    <update id="platformAgreeRefund">
        update order_refund
        set
            refund_amount = #{orderRefundVO.refundAmount},
            platform_refund_amount = #{orderRefundVO.platformRefundAmount},
            platform_refund_commission = #{orderRefundVO.platformRefundCommission},
            purchase_refund_amount = #{orderRefundVO.purchaseRefundAmount},
            pur_platform_refund_commission = #{orderRefundVO.purPlatformRefundCommission},
            after_intervention_refund_amount = #{orderRefundVO.afterInterventionRefundAmount},
            intervention_refund_type = #{orderRefundVO.interventionRefundType},
            platform_message = #{orderRefundVO.platformMessage},
            decision_time = NOW(),
            platform_intervention_status    = 2
        where
            refund_id = #{orderRefundVO.refundId}
            and platform_intervention_status = 1
    </update>

    <select id="getRefundStatus" resultType="java.lang.Integer">
        select return_money_sts  from order_refund where refund_id = #{refundId}
    </select>

    <select id="getProcessingOrderRefundByOrderId" resultType="com.tmerclub.cloud.order.vo.OrderRefundVO">
        SELECT <include refid="Join_Column_List" />
        FROM order_refund r
        WHERE r.`order_id` = #{orderId}
          AND (
                (
                    --         买家撤回申请
                    r.`return_money_sts` != 6
                    --         商家拒绝
                    AND r.`return_money_sts` != 7
                    --         退款关闭
                    AND r.`return_money_sts` != - 1
                )
                or
                -- 平台介入状态
                platform_intervention_status in (1,2,5)
          )
    </select>

    <select id="getProcessingRefundByOrderId" resultType="com.tmerclub.cloud.order.vo.OrderRefundVO">
        SELECT <include refid="Join_Column_List" />
        FROM order_refund r
        WHERE r.`order_id` = #{orderId}
          AND (
                (
                    --         买家撤回申请
                    r.`return_money_sts` != 6
                    --         商家拒绝
                    AND r.`return_money_sts` != 7
                    --         退款关闭
                    AND r.`return_money_sts` != - 1
                    --         退款成功
                    AND r.`return_money_sts` != - 1
                )
                or
                -- 平台介入状态
                platform_intervention_status in (1,2)
          )
    </select>

    <select id="listOrderRefundByOrderId" resultType="com.tmerclub.cloud.order.vo.OrderRefundVO">
        SELECT <include refid="Join_Column_List" />
        FROM order_refund r
        WHERE r.`order_id` = #{orderId}
          AND (
                (
                    --         买家撤回申请
                    r.`return_money_sts` != 6
                    --         商家拒绝
                    AND r.`return_money_sts` != 7
                    --         退款关闭
                    AND r.`return_money_sts` != - 1
                )
                or
                (
                    platform_intervention_status  != -1
                     and platform_intervention_status  != 3
                )

          )
    </select>

    <select id="getSupplierHandleStatusByOrderId" resultType="int">
        SELECT IFNULL(MAX(r.supplier_handle_status),-1)
        FROM order_refund r
        WHERE r.`order_id` = #{orderId}
    </select>

    <select id="listOrderIdsByRefundIds" resultType="com.tmerclub.cloud.api.order.vo.OrderRefundSimpleVO">
        select order_id, refund_id
        from `order_refund`
        where refund_id in
        <foreach collection="refundIds" item="refundId" separator="," open="(" close=")">
            #{refundId}
        </foreach>
    </select>

    <select id="listOrderRefundByOrderIds" resultType="long">
        select distinct(order_id)
        from `order_refund`
        where platform_intervention_status in (-1,3,5)order_id in
        <foreach collection="orderIds" item="orderId" separator="," open="(" close=")">
            #{orderId}
        </foreach>
    </select>

    <update id="transferRefundOrderToSupplier">
        UPDATE
            order_refund
        SET
            supplier_handle_status = #{supplierHandleStatus},
            supplier_time = now()
        WHERE refund_id = #{refundId}
          AND return_money_sts = #{returnMoneySts}
          AND supplier_handle_status = #{oldSupplierHandleStatus}
    </update>

    <select id="mongoAndEsOrderRefundInfo" resultType="com.tmerclub.cloud.order.bo.mongo.MongoOrderRefundBO">
        SELECT *
        FROM order_refund
        WHERE refund_id = #{refundId}
    </select>

    <select id="mongoEsOrder" resultType="com.tmerclub.cloud.order.bo.mongo.MongoOrderRefundBO">
        SELECT o.order_type,o.shop_name,o.freight_amount,o.wait_purchase,o.status as orderStatus, o.pay_type, ifnull(os.pay_id, os.pay_ids) as payId
        FROM `order` o
        left join order_settlement os on os.order_id = o.order_id
        WHERE o.order_id = #{orderId}
    </select>

    <select id="mongoAndEsOrderRefundItems" resultMap="mongoOrderRefundItemMap">
        SELECT oi.order_item_id,oi.spu_id,oi.sku_id,oi.pic,oi.count,oi.price,oi.spu_total_amount,oi.actual_total,oi.platform_share_reduce,IFNULL(oi.use_score, 0) refund_score,
            oil.lang,oil.spu_name ,oil.sku_name,oi.activity_id,oi.activity_type,oi.actual_total refund_amount, oil.supplier_spu_name, oil.supplier_sku_name, oi.supplier_pic
        FROM order_item oi
        LEFT JOIN order_item_lang oil ON oi.order_item_id = oil.order_item_id
        <where>
            <if test="orderItemId != null and orderItemId != 0">
                (oi.order_item_id = #{orderItemId}
                <if test="giveawayList !=null and giveawayList.size() != 0">
                    or oi.order_item_id in
                    <foreach collection="giveawayList" item="giveaway" separator="," open="(" close=")">
                        #{giveaway}
                    </foreach>
                </if>
                )
            </if>
            <if test="orderItemId == null or orderItemId == 0">
                AND oi.order_id = #{orderId}
            </if>
        </where>
    </select>

    <select id="listUserOrderRefund" resultMap="orderRefundWithItemMap">
        SELECT <include refid="Vo_Column_List" />
        FROM order_refund
        WHERE user_id = #{orderRefundSearchDTO.userId}
        <if test="orderRefundSearchDTO.returnMoneySts != null">
            AND return_money_sts = #{orderRefundSearchDTO.returnMoneySts}
        </if>
        <if test="orderRefundSearchDTO.applyType != null">
            AND apply_type = #{orderRefundSearchDTO.applyType}
        </if>
        <if test="orderRefundSearchDTO.startTime != null ">
            AND create_time &gt;= #{orderRefundSearchDTO.startTime}
        </if>
        <if test="orderRefundSearchDTO.endTime != null ">
            AND create_time &lt;= #{orderRefundSearchDTO.endTime}
        </if>
        <if test="orderRefundSearchDTO.refundId != null">
            AND refund_id LIKE CONCAT('%', #{orderRefundSearchDTO.refundId}, '%')
        </if>
        ORDER BY create_time DESC
    </select>
</mapper>
