<?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.OrderRefundMapper">

    <resultMap id="BaseResultMap" type="com.yami.shop.bean.model.OrderRefund">
        <id column="refund_id" property="refundId"/>
        <result column="refund_sn" property="refundSn"/>
        <result column="order_number" property="orderNumber"/>
        <result column="shop_id" property="shopId"/>
        <result column="user_id" property="userId"/>
        <result column="order_id" property="orderId"/>
        <result column="refund_type" property="refundType"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="goods_num" property="goodsNum"/>
        <result column="refund_amount" property="refundAmount"/>
        <result column="apply_type" property="applyType"/>
        <result column="is_receiver" property="isReceiver"/>
        <result column="buyer_reason" property="buyerReason"/>
        <result column="buyer_desc" property="buyerDesc"/>
        <result column="buyer_mobile" property="buyerMobile"/>
        <result column="photo_files" property="photoFiles"/>
        <result column="return_money_sts" property="returnMoneySts"/>
        <result column="reject_message" property="rejectMessage"/>
        <result column="seller_msg" property="sellerMsg"/>
        <result column="apply_time" property="applyTime"/>
        <result column="handel_time" property="handelTime"/>
        <result column="ship_time" property="shipTime"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="cancel_time" property="cancelTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="decision_time" property="decisionTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="reject_time" property="rejectTime"/>
    </resultMap>


    <resultMap id="OrderRefundVoMap" type="com.yami.shop.bean.vo.OrderRefundVo">
        <id column="refund_id" property="refundId"/>
        <result column="refund_sn" property="refundSn"/>
        <result column="order_number" property="orderNumber"/>
        <result column="shop_id" property="shopId"/>
        <result column="user_id" property="userId"/>
        <result column="o_dvy_type" property="dvyType"/>
        <result column="order_id" property="orderId"/>
        <result column="refund_type" property="refundType"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="goods_num" property="goodsNum"/>
        <result column="refund_amount" property="refundAmount"/>
        <result column="apply_type" property="applyType"/>
        <result column="is_receiver" property="isReceiver"/>
        <result column="buyer_reason" property="buyerReason"/>
        <result column="buyer_desc" property="buyerDesc"/>
        <result column="buyer_mobile" property="buyerMobile"/>
        <result column="photo_files" property="photoFiles"/>
        <result column="return_money_sts" property="returnMoneySts"/>
        <result column="reject_message" property="rejectMessage"/>
        <result column="seller_msg" property="sellerMsg"/>
        <result column="apply_time" property="applyTime"/>
        <result column="handel_time" property="handelTime"/>
        <result column="ship_time" property="shipTime"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="cancel_time" property="cancelTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="decision_time" property="decisionTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="reject_time" property="rejectTime"/>
        <result column="channel_name" property="channelName"/>
        <result column="real_name" property="receiver"/>
        <result column="refund_total_money" property="refundTotalMoney"/>
    </resultMap>
    <resultMap id="tagResultMap" type="com.yami.shop.bean.dto.OrderRefundDto">
        <id column="refund_id" property="refundId"/>
        <result column="refund_sn" property="refundSn"/>
        <result column="shop_id" property="shopId"/>
        <result column="user_id" property="userId"/>
        <result column="order_id" property="orderId"/>
        <result column="refund_type" property="refundType"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="r_goods_num" property="goodsNum"/>
        <result column="refund_amount" property="refundAmount"/>
        <result column="apply_type" property="applyType"/>
        <result column="is_receiver" property="isReceiver"/>
        <result column="buyer_reason" property="buyerReason"/>
        <result column="buyer_mobile" property="buyerMobile"/>
        <result column="photo_files" property="photoFiles"/>
        <result column="return_money_sts" property="returnMoneySts"/>
        <result column="reject_message" property="rejectMessage"/>
        <result column="seller_msg" property="sellerMsg"/>
        <result column="apply_time" property="applyTime"/>
        <result column="handel_time" property="handelTime"/>
        <result column="ship_time" property="shipTime"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="cancel_time" property="cancelTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="decision_time" property="decisionTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="reject_time" property="rejectTime"/>
        <result column="shop_name" property="shopName"/>
        <result column="o_dvy_type" property="dvyType"/>
        <!-- 扩展字段 -->
        <result column="o_order_number" property="orderNumber"/>
        <result column="o_actual_total" property="orderAmount"/>
        <result column="s_order_pay_no" property="orderPayNo"/>
        <result column="s_settlement_id" property="settlementId"/>
        <result column="o_pay_time" property="orderPayTime"/>
        <result column="o_status" property="status"/>
        <result column="o_refund_status" property="refundStatus"/>
        <result column="freight_amount" property="freightAmount"/>
        <result column="offset_points" property="offsetPoints"/>

        <!-- 退货地址信息 -->
        <association property="refundDelivery" javaType="com.yami.shop.bean.model.RefundDelivery">
            <id column="refund_delivery_id" property="refundDeliveryId"/>
            <result column="shop_id" property="shopId"/>
            <result column="refund_sn" property="refundSn"/>
            <result column="user_id" property="userId"/>
            <result column="payer" property="payer"/>
            <result column="price" property="price"/>
            <result column="dey_id" property="deyId"/>
            <result column="dey_name" property="deyName"/>
            <result column="dey_nu" property="deyNu"/>
            <result column="receiver_name" property="receiverName"/>
            <result column="receiver_mobile" property="receiverMobile"/>
            <result column="receiver_telephone" property="receiverTelephone"/>
            <result column="receiver_post_code" property="receiverPostCode"/>
            <result column="receiver_addr" property="receiverAddr"/>
            <result column="sender_mobile" property="senderMobile"/>
            <result column="sender_remarks" property="senderRemarks"/>
            <result column="imgs" property="imgs"/>
            <result column="create_time" property="createTime"/>
        </association>
    </resultMap>


    <resultMap id="InfoResultMap" type="com.yami.shop.bean.dto.OrderRefundDto">
        <id column="refund_id" property="refundId"/>
        <result column="refund_sn" property="refundSn"/>
        <result column="shop_id" property="shopId"/>
        <result column="user_id" property="userId"/>
        <result column="order_id" property="orderId"/>
        <result column="refund_type" property="refundType"/>
        <result column="order_item_id" property="orderItemId"/>
        <result column="goods_num" property="goodsNum"/>
        <result column="refund_amount" property="refundAmount"/>
        <result column="apply_type" property="applyType"/>
        <result column="is_receiver" property="isReceiver"/>
        <result column="is_return_logistics" property="isReturnLogistics"/>
        <result column="buyer_desc" property="buyerDesc"/>
        <result column="platform_refund_amount" property="platformRefundAmount"/>
        <result column="buyer_reason" property="buyerReason"/>
        <result column="buyer_mobile" property="buyerMobile"/>
        <result column="photo_files" property="photoFiles"/>
        <result column="return_money_sts" property="returnMoneySts"/>
        <result column="reject_message" property="rejectMessage"/>
        <result column="seller_msg" property="sellerMsg"/>
        <result column="apply_time" property="applyTime"/>
        <result column="handel_time" property="handelTime"/>
        <result column="ship_time" property="shipTime"/>
        <result column="receive_time" property="receiveTime"/>
        <result column="cancel_time" property="cancelTime"/>
        <result column="refund_time" property="refundTime"/>
        <result column="decision_time" property="decisionTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="reject_time" property="rejectTime"/>
        <result column="shop_name" property="shopName"/>
        <result column="distribution_total_amount" property="distributionTotalAmount"/>
        <!-- 扩展字段 -->
        <result column="o_order_number" property="orderNumber"/>
        <result column="o_actual_total" property="orderAmount"/>
        <result column="o_status" property="status"/>
        <result column="s_order_pay_no" property="orderPayNo"/>
        <result column="s_settlement_id" property="settlementId"/>
        <result column="o_pay_time" property="orderPayTime"/>
        <result column="o_dvy_type" property="dvyType"/>

        <result column="freight_amount" property="freightAmount"/>
        <result column="offset_points" property="offsetPoints"/>

        <!-- 退货地址信息 -->
        <association property="refundDelivery" javaType="com.yami.shop.bean.model.RefundDelivery">
            <id column="refund_delivery_id" property="refundDeliveryId"/>
            <result column="shop_id" property="shopId"/>
            <result column="refund_sn" property="refundSn"/>
            <result column="user_id" property="userId"/>
            <result column="dey_id" property="deyId"/>
            <result column="dey_name" property="deyName"/>
            <result column="dey_nu" property="deyNu"/>
            <result column="receiver_name" property="receiverName"/>
            <result column="receiver_mobile" property="receiverMobile"/>
            <result column="receiver_telephone" property="receiverTelephone"/>
            <result column="receiver_post_code" property="receiverPostCode"/>
            <result column="receiver_addr" property="receiverAddr"/>
            <result column="sender_mobile" property="senderMobile"/>
            <result column="sender_remarks" property="senderRemarks"/>
            <result column="imgs" property="imgs"/>
            <result column="create_time" property="createTime"/>
            <result column="pay" property="pay"/>
            <result column="payer" property="payer"/>
            <result column="price" property="price"/>
            <result column="touch_time" property="touchTime"/>
            <result column="delivery_time" property="deliveryTime"/>
        </association>
    </resultMap>

    <sql id="selectJoinAll">
        SELECT r.*,
               o.dvy_type        as o_dvy_type,
               o.pay_time        as o_pay_time,
               o.order_number    AS o_order_number,
               o.actual_total    AS o_actual_total,
               o.hb_order_status AS o_status,
               s.pay_no          AS s_order_pay_no,
               sp.shop_name      AS shop_name,
               s.settlement_id   AS s_settlement_id,
               d.*
        FROM tz_order_refund r
                 LEFT JOIN tz_order o ON r.order_id = o.order_id
                 LEFT JOIN tz_shop_detail sp ON sp.shop_id = o.shop_id
                 LEFT JOIN tz_refund_delivery d ON r.refund_sn = d.refund_sn
                 LEFT JOIN tz_order_settlement s ON o.order_number = s.order_number
    </sql>

    <!-- 分页查询列表 -->
    <select id="getPage" parameterType="com.yami.shop.bean.dto.OrderRefundDto" resultMap="tagResultMap">
        SELECT
        r.*,
        rd.payer,
        rd.price,
        IF(r.goods_num = 0,o.product_nums,r.goods_num) AS r_goods_num,
        o.order_number AS o_order_number,
        o.pay_time as o_pay_time,
        o.actual_total AS o_actual_total,
        o.product_nums AS product_nums,
        o.hb_order_status AS o_status,
        o.dvy_type        as o_dvy_type,
        o.refund_status AS o_refund_status,
        sd.shop_name AS shop_name
        FROM tz_order_refund r
        LEFT JOIN tz_order o on r.order_id = o.order_id
        LEFT JOIN tz_shop_detail sd on sd.shop_id = r.shop_id
        LEFT JOIN tz_refund_delivery rd on rd.refund_sn=r.refund_sn
        <where>
            <if test="orderRefundDto.shopId != null and orderRefundDto.shopId != ''">
                AND r.shop_id = #{orderRefundDto.shopId}
            </if>
            <if test="orderRefundDto.userId != null and orderRefundDto.userId != ''">
                AND r.user_id = #{orderRefundDto.userId}
            </if>
            <if test="orderRefundDto.refundSn != null and orderRefundDto.refundSn != ''">
                AND r.refund_sn = #{orderRefundDto.refundSn}
            </if>
            <if test="orderRefundDto.orderNumber != null and orderRefundDto.orderNumber != ''">
                AND o.order_number = #{orderRefundDto.orderNumber}
            </if>
            <if test="orderRefundDto.applyType != null and orderRefundDto.applyType != ''">
                AND r.apply_type = #{orderRefundDto.applyType}
            </if>
            <if test="beginTime != null and beginTime != ''">
                AND r.apply_time &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND r.apply_time &lt;= #{endTime}
            </if>
            <if test="orderRefundDto.returnMoneySts != null and orderRefundDto.returnMoneySts != ''">
                AND r.return_money_sts = #{orderRefundDto.returnMoneySts}
            </if>
            <if test="orderRefundDto.shopName != null">
                AND sd.shop_name LIKE concat("%",#{orderRefundDto.shopName},"%")
            </if>
            <if test="orderRefundDto.orderType != null and orderRefundDto.orderType != ''">
                AND o.order_type = #{orderRefundDto.orderType}
            </if>
            <if test="orderRefundDto.refundType != null and orderRefundDto.refundType != ''">
                AND r.refund_type = #{orderRefundDto.refundType}
            </if>
        </where>
        ORDER BY apply_time DESC
    </select>

    <!-- 查询详情信息 -->
    <select id="getById" resultMap="InfoResultMap">
        <include refid="selectJoinAll"/>
        WHERE r.refund_id = #{id}
        <if test="shopId != null">
            and r.shop_id = #{shopId}
        </if>
    </select>
    <!-- 获取退款单数量 -->
    <select id="getRefundCount" resultType="int">
        SELECT count(*)
        FROM tz_order_refund r
                 LEFT JOIN tz_order o on r.order_id = o.order_id
                 LEFT JOIN tz_shop_detail sd on sd.shop_id = r.shop_id
        where r.user_id = #{userId}
    </select>

    <select id="getOrderRefundByRefundSn" resultMap="InfoResultMap">
        <include refid="selectJoinAll"/>
        WHERE r.refund_sn = #{refundSn}
    </select>

    <select id="getRefundByShopId" resultType="com.yami.shop.bean.param.StatisticsRefundParam">
        SELECT SUM(refund_amount) as payActualTotal,count(*) as refundCount FROM tz_order_refund
        <where>
            <if test="shopId != null">
                and shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and refund_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and refund_time &lt;= #{endTime}
            </if>
            AND return_money_sts =5
        </where>
    </select>

    <select id="getRefundRankingByProd" resultType="com.yami.shop.bean.param.StatisticsRefundParam">
        SELECT MAX(buyer_reason) as buyerReason,prod_name as refundPordName,COUNT(buyer_reason) as
        refundCount,SUM(refund_amount) as payActualTotal
        FROM `tz_order_refund` a LEFT JOIN tz_order b
        ON a.order_id = b.order_id
        <where>
            <if test="shopId != null">
                and a.shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and refund_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and refund_time &lt;= #{endTime}
            </if>
            AND return_money_sts =5
        </where>
        GROUP BY prod_name ORDER BY SUM(refund_amount) DESC LIMIT 0,5
    </select>


    <select id="getRefundRankingByReason" resultType="com.yami.shop.bean.param.StatisticsRefundParam">
        SELECT buyer_reason as buyerReason,MAX(prod_name) as refundPordName,COUNT(buyer_reason) as
        refundCount,SUM(refund_amount) as payActualTotal
        FROM `tz_order_refund` a LEFT JOIN tz_order b
        ON a.order_id = b.order_id
        <where>
            <if test="shopId != null">
                and a.shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and refund_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and refund_time &lt;= #{endTime}
            </if>
            AND return_money_sts =5
        </where>
        GROUP BY buyer_reason ORDER BY SUM(refund_amount) DESC LIMIT 0,5
    </select>


    <select id="getRefundByShopIdAndDay" resultType="com.yami.shop.bean.param.StatisticsRefundParam">
        SELECT dates as refundDate,MAX(payActualTotal) as payActualTotal,MAX(refundCount) as refundCount FROM(
        SELECT @cdate := DATE_ADD(@cdate,INTERVAL - 1 DAY) dates ,0 AS payActualTotal,0 AS 'refundCount'
        FROM (SELECT @cdate :=DATE_ADD(CURDATE(),INTERVAL + 1 DAY) FROM tz_user) t1
        <where>
            <if test="startTime != null">
                and @cdate >= #{startTime}
            </if>
        </where>
        UNION ALL
        SELECT DATE_FORMAT(refund_time, '%Y-%m-%d') AS dates,SUM(refund_amount) AS payActualTotal,COUNT(refund_id)
        refundCount FROM tz_order_refund
        <where>
            <if test="shopId != null">
                and shop_id = #{shopId}
            </if>
            <if test="startTime != null">
                and refund_time >= #{startTime}
            </if>
            <if test="endTime != null">
                and refund_time &lt;= #{endTime}
            </if>
            AND return_money_sts =5
        </where>
        GROUP BY dates)_tmpAllTable GROUP BY dates
        ORDER BY dates
    </select>

    <update id="updateReturnMoneySts">
        UPDATE tz_order_refund
        <set>
            return_money_sts = #{status},update_time = now(),
            <if test="status == 2">
                handel_time = now()
            </if>
        </set>
        WHERE refund_id IN (
        <foreach collection="refundList" separator="," item="item">
            #{item.refundId}
        </foreach>
        )
    </update>
    <select id="sumRefundSuccessAmountByOrderId" resultType="double">
        select ifnull(sum(refund_amount), 0)
        from tz_order_refund
        where order_id = #{orderId}
          and return_money_sts = 5
    </select>

    <select id="getProcessingOrderRefundByOrderId" resultType="com.yami.shop.bean.model.OrderRefund">
        SELECT r.*
        FROM tz_order_refund r
        WHERE r.`order_id` = #{orderId}
          --         买家撤回申请
          AND r.`return_money_sts` != 40
          --         商家拒绝
          AND r.`return_money_sts` != 30
    </select>
    <select id="sumRefundSuccessPlatformAmountByOrderId" resultType="double">
        select ifnull(sum(platform_refund_amount), 0)
        from tz_order_refund
        where order_id = #{orderId}
          and return_money_sts = 5
    </select>
    <select id="getOrderRefundCountByRefundSn" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM tz_order_refund
        WHERE order_id = (SELECT order_id
                          FROM tz_order_refund
                          WHERE refund_sn = #{refundSn }
                            AND return_money_sts IN (1, 2, 3, 4, 5))
    </select>
    <select id="countRefundByOrderItem" resultType="java.lang.Integer">
        SELECT COUNT(refund_id)
        FROM `tz_order_refund`
        WHERE order_item_id = #{orderItemId}
          AND (return_money_sts &gt;= #{returnMoneySts0} AND return_money_sts &lt;= #{returnMoneySts1})
    </select>


    <select id="orderRefundCount" resultType="com.yami.shop.bean.param.OrderRefundCountParam">
        SELECT
        COUNT(re.return_money_sts) AS allCount,
        COUNT(CASE WHEN re.return_money_sts = 10 THEN 1 END) AS sellerApplyCount,
        COUNT(CASE WHEN re.return_money_sts = 30 THEN 1 END) AS sellerRejectCount,
        COUNT(CASE WHEN re.return_money_sts = 40 THEN 1 END) AS withdrawApplyCount,
        COUNT(CASE WHEN re.return_money_sts = 60 THEN 1 END) AS sellerAcceptCount,
        COUNT(CASE WHEN re.return_money_sts = 65 THEN 1 END) AS buyerDeliveryCount,
        COUNT(CASE WHEN re.return_money_sts = 70 THEN 1 END) AS refundCompleteCount
        FROM tz_order_refund re
        left join tz_order o on re.order_id = o.order_id
        <where>
            <if test="orderRefund.orderNumber != null">
                and re.order_number = #{orderRefund.orderNumber}
            </if>
            <if test="orderRefund.channelIdList != null and  !orderRefund.channelIdList.isEmpty()">
                and o.channel_id  in
                <foreach collection="orderRefund.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderRefund.dvyType != null and orderRefund.dvyType != ''">
                and o.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderRefund.refundSn != null">
                and re.refund_sn = #{orderRefund.refundSn}
            </if>
            <if test="orderRefund.buyerMobile != null">
                and re.buyer_mobile = #{orderRefund.buyerMobile}
            </if>
            <if test="orderRefund.applyType != null">
                and re.apply_type = #{orderRefund.applyType}
            </if>
            <if test="orderRefund.returnMoneySts != null and orderRefund.returnMoneySts != 0">
                and re.return_money_sts = #{orderRefund.returnMoneySts}
            </if>
            <if test="orderRefund.startTime != null">
                AND re.create_time &gt;= #{orderRefund.startTime}
            </if>
            <if test="orderRefund.endTime != null">
                AND re.create_time &lt;= #{orderRefund.endTime}
            </if>
        </where>

    </select>
    <select id="orderRefundPage" resultMap="OrderRefundVoMap">
        select refund.*, o.actual_total as actualTotal,o.dvy_type as o_dvy_type, o.product_nums as productNums, o.hb_order_status as hbOrderStatus,
        tsd.shop_name as shopName
        from tz_order_refund refund
        join tz_shop_detail tsd on refund.shop_id = tsd.shop_id
        join tz_order o on refund.order_id = o.order_id
        <where>
            <if test="orderRefund.orderNumber != null">
                and refund.order_number = #{orderRefund.orderNumber}
            </if>
            <if test="orderRefund.channelIdList != null and  !orderRefund.channelIdList.isEmpty()">
                and o.channel_id  in
                <foreach collection="orderRefund.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderRefund.dvyType != null and orderRefund.dvyType != ''">
                and o.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderRefund.refundSn != null">
                and refund.refund_sn = #{orderRefund.refundSn}
            </if>
            <if test="orderRefund.buyerMobile != null">
                and refund.buyer_mobile = #{orderRefund.buyerMobile}
            </if>
            <if test="orderRefund.applyType != null">
                and refund.apply_type = #{orderRefund.applyType}
            </if>
            <if test="orderRefund.returnMoneySts != null and orderRefund.returnMoneySts != 0">
                and refund.return_money_sts = #{orderRefund.returnMoneySts}
            </if>
            <if test="orderRefund.startTime != null">
                AND refund.create_time &gt;= #{orderRefund.startTime}
            </if>
            <if test="orderRefund.endTime != null">
                AND refund.create_time &lt;= #{orderRefund.endTime}
            </if>
        </where>
        order by refund.apply_time desc
    </select>

    <select id="selectInfoById" resultMap="OrderRefundVoMap">
        select refund.*,
               o.actual_total    as actualTotal,
               o.product_nums    as productNums,
               o.hb_order_status as hbOrderStatus,
               o.offset_points as OrderOffsetPoints,
               o.dvy_type        as o_dvy_type,
               o.actual_total as OrderActualTotal,
               tsd.shop_name     as shopName,
               uao.receiver    as receiver
        from tz_order_refund refund
                 join tz_shop_detail tsd on refund.shop_id = tsd.shop_id
                 join tz_order o on refund.order_id = o.order_id
        left join tz_user_addr_order uao on o.addr_order_id = uao.addr_order_id
        where refund.refund_id = #{refundId}
    </select>
    <select id="findByOrderNumber" resultType="com.yami.shop.bean.model.OrderRefund">
        select * from tz_order_refund where return_money_sts=#{returnMoneySts} and order_number =#{orderNumber}
    </select>
    <select id="findOrderRefund" resultMap="OrderRefundVoMap">
        select refund.*, o.actual_total as actualTotal,o.dvy_type as o_dvy_type, o.product_nums as productNums, o.hb_order_status as hbOrderStatus,u.real_name,
        tsd.shop_name as shopName,d.channel_name
        from tz_order_refund refund
        join tz_shop_detail tsd on refund.shop_id = tsd.shop_id
        join tz_order o on refund.order_id = o.order_id
        join tz_channel d on o.channel_id = d.id
        join tz_user u on refund.user_id = u.user_id
        <where>
            <if test="orderRefund.orderNumber != null">
                and refund.order_number = #{orderRefund.orderNumber}
            </if>
            <if test="orderRefund.channelIdList != null and !orderRefund.channelIdList.isEmpty()">
                and o.channel_id  in
                <foreach collection="orderRefund.channelIdList" item="channelId" open="(" close=")" separator=",">
                    #{channelId}
                </foreach>
            </if>
            <if test="orderRefund.dvyType != null and orderRefund.dvyType != ''">
                and o.dvy_type = #{orderParam.dvyType}
            </if>
            <if test="orderRefund.refundSn != null">
                and refund.refund_sn = #{orderRefund.refundSn}
            </if>
            <if test="orderRefund.buyerMobile != null">
                and refund.buyer_mobile = #{orderRefund.buyerMobile}
            </if>
            <if test="orderRefund.applyType != null">
                and refund.apply_type = #{orderRefund.applyType}
            </if>
            <if test="orderRefund.returnMoneySts != null and orderRefund.returnMoneySts != 0">
                and refund.return_money_sts = #{orderRefund.returnMoneySts}
            </if>
            <if test="orderRefund.startTime != null">
                AND refund.create_time &gt;= #{orderRefund.startTime}
            </if>
            <if test="orderRefund.endTime != null">
                AND refund.create_time &lt;= #{orderRefund.endTime}
            </if>
        </where>
        order by refund.apply_time desc
    </select>

    <select id="queryRemainGoodsNumber" resultType="integer">
        SELECT (SELECT IFNULL(SUM(prod_count), 0) FROM tz_order_item WHERE order_number = #{orderNumber}) -
               (SELECT COALESCE(SUM(product_count), 0)FROM tz_order_refund_sku a
                LEFT JOIN tz_order_refund b ON a.order_refund_id = b.refund_id
                WHERE b.order_number = #{orderNumber} AND b.return_money_sts NOT IN (30, 40)) AS result
    </select>

</mapper>
