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


    <select id="listPayRecords" resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsPayListVO">
        SELECT
            *
        FROM
            drp_order_goods_pay
        WHERE
            order_goods_id = #{content.orderGoodsId}
            <if test="content.type != null">
                AND type = #{content.type}
            </if>

    </select>
  <select id="getOrderGoodsPayList"
          resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsManagePayVO">
      SELECT
          p.pay_id,
          p.pay_amount AS amount,
          p.pay_time AS time,
          s.supplier_name
      FROM drp_order_goods_pay p
        INNER JOIN drp_order_goods o ON p.order_goods_id = o.order_goods_id
        INNER JOIN drp_supplier s ON s.supplier_id = o.supplier_id
      WHERE
        p.type = #{type}
      <if test="content.startTime != null and content.endTime != null">
        AND (p.pay_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 p.order_goods_id like #{content.orderGoodsId}
      </if>
      order by p.crtime desc
  </select>
  <select id="getOrderGoodsPayDetail"
          resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsManageDetailVO">
    SELECT
      p.pay_time,
      p.pay_amount AS amount,
      p.pay_bank,
      p.pay_account,
      p.pay_account_name,
      p.collect_bank,
      p.collect_account,
      p.collect_account_name,
      p.order_goods_id,
      o.crtime AS order_time,
      p.crby,
      p.crtime
    FROM drp_order_goods_pay p
           INNER JOIN drp_order_goods o ON p.order_goods_id = o.order_goods_id
    WHERE p.pay_id = #{id};
  </select>
  <select id="getOrderGoodsPayTotal"
          resultType="com.bonus.canteen.core.supply.vo.DrpOrderGoodsManageTotalVO">
    SELECT
    count(*) AS total,
    COALESCE(SUM(pay_amount), '0.00') AS amount
    FROM drp_order_goods_pay p
      INNER JOIN drp_order_goods o ON p.order_goods_id = o.order_goods_id
      INNER JOIN drp_supplier s ON s.supplier_id = o.supplier_id
    <where>
        p.type = #{type}
        <if test="content.startTime != null and content.endTime != null">
            AND (pay_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 p.order_goods_id like #{content.orderGoodsId}
        </if>
    </where>
  </select>
</mapper>
