<?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.supply.mapper.DrpOrderGoodsRefundMapper">
  <select id="listRefundRecords" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsRefundListVO">
      SELECT
          *
      FROM
          drp_order_goods_refund
      WHERE
          order_goods_id = #{content.orderGoodsId}
          <if test="content.type != null">
              AND type = #{content.type}
          </if>
  </select>
  <select id="getOrderGoodsRefundList"
          resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsManageRefundVO">
      SELECT
          r.refund_money_id,
          r.refund_amount AS amount,
          r.refund_time AS time,
          s.supplier_name
      FROM drp_order_goods_refund r
               INNER JOIN drp_order_goods o ON r.order_goods_id = o.order_goods_id
               INNER JOIN drp_supplier s ON s.supplier_id = o.supplier_id
    <where>
        r.type = #{type}
       <if test="content.startTime != null and content.endTime != null">
           and (r.refund_time between #{content.startTime} and #{content.endTime})
       </if>
      <if test="content.supplierId != null">
          and s.supplier_id = #{content.supplierId}
      </if>
      <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
          and r.order_goods_id like #{content.orderGoodsId}
      </if>
      </where>
      order by r.crtime desc
  </select>
  <select id="getOrderGoodsRefundDetail"
          resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsManageDetailVO">
    SELECT
      r.refund_time payTime,
      r.refund_amount AS amount,
      r.pay_bank,
      r.pay_account,
      r.pay_account_name,
      r.collect_bank,
      r.collect_account,
      r.collect_account_name,
      r.order_goods_id,
      o.crtime AS order_time,
      r.crby,
      r.crtime
    FROM drp_order_goods_refund r
    INNER JOIN drp_order_goods o ON r.order_goods_id = o.order_goods_id
    WHERE r.refund_money_id = #{id};
  </select>
  <select id="getOrderGoodsRefundTotal"
          resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsManageTotalVO">
    SELECT
      count(*) AS total,
      COALESCE(SUM(r.refund_amount), '0.00') AS amount
    FROM drp_order_goods_refund r
           INNER JOIN drp_order_goods o ON r.order_goods_id = o.order_goods_id
           INNER JOIN drp_supplier s ON s.supplier_id = o.supplier_id
    <where>
        r.type = #{type}
        <if test="content.startTime != null and content.endTime != null">
            AND (r.refund_time between #{content.startTime} and #{content.endTime})
        </if>
        <if test="content.supplierId != null">
            AND o.supplier_id = #{content.supplierId}
        </if>
        <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
            AND r.order_goods_id like #{content.orderGoodsId}
        </if>
    </where>
  </select>
</mapper>
