<?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.bonus.canteen.core.order.common.mapper.OrderRefundMapper">

    <select id="getOrderRealRefundAmount" resultType="java.math.BigDecimal">
        SELECT SUM(real_refund_amount) FROM order_refund
        WHERE order_id = #{orderId} AND check_state = #{checkState};
    </select>
    <select id="listUnCheckCorrectRefundIds" resultType="java.lang.Long">
        SELECT orf.order_refund_id FROM order_refund orf
        WHERE orf.mac_order_id = #{macOrderId}
        AND orf.apply_type = 2
        AND orf.check_state = 1
        <if test="detailId != null">
            AND EXISTS(
                SELECT 1 FROM order_refund_detail ord
                WHERE ord.order_refund_id = orf.order_refund_id
                AND ord.detail_id = #{detailId}
            )
        </if>
    </select>

    <select id="getByRefundTradeId" resultType="com.bonus.canteen.core.order.common.model.OrderRefund">
        SELECT * FROM trade_order_recharge tor
                 LEFT JOIN order_refund orf ON tor.order_recharge_id = orf.order_refund_id
        WHERE tor.trade_id = #{refundTradeId}
    </select>

</mapper>
