<?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.czg.service.order.mapper.CreditBuyerOrderMapper">

    <sql id="view_credit_buyer_order">
        (SELECT t1.id                                               AS id,
                t1.credit_buyer_id                                  AS credit_buyer_id,
                t1.order_id                                         AS order_id,
                t2.pay_amount - t2.refund_amount                    AS pay_amount,
                t1.paid_amount                                      AS paid_amount,
                (t2.pay_amount - t2.refund_amount) - t1.paid_amount AS unpaid_amount,
                t1.status                                           AS status,
                t2.create_time                                      AS create_time,
                t1.last_payment_time                                AS last_payment_time,
                t1.last_payment_method                              AS last_payment_method,
                t1.remark                                           AS remark
         FROM tb_credit_buyer_order t1
                  LEFT JOIN tb_order_info t2 ON t1.order_id = t2.id)
    </sql>

    <sql id="commonWhere">
        <where>
            and x1.credit_buyer_id = #{creditBuyerId}
            <if test="orderId != null and orderId != ''">
                and x1.order_id = #{orderId}
            </if>
            <if test="beginDate != null and beginDate != ''">
                and x1.create_time >= str_to_date(concat(#{beginDate},' 00:00:00'), '%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endDate != null and endDate != ''">
                <![CDATA[
                and x1.create_time <= str_to_date(concat(#{endDate},' 23:59:59'), '%Y-%m-%d %H:%i:%s')
                ]]>
            </if>
            <if test="status != null and status != ''">
                and x1.status = #{status}
            </if>
            <if test="statusList != null">
                and x1.status in
                <foreach item="status" collection="statusList" open="(" separator="," close=")">
                    #{status}
                </foreach>
            </if>
            <if test="id != null">
                and x1.id = #{id}
            </if>
        </where>
    </sql>

    <select id="findCreditBuyerOrderList" resultType="com.czg.order.dto.CreditBuyerOrderDTO">
        select * from
        <include refid="view_credit_buyer_order"/>
        x1
        <include refid="commonWhere"/>
        order by x1.order_id desc
    </select>
    <select id="getCreditBuyerOrderCount" resultType="java.lang.Long">
        select count(*) from
        <include refid="view_credit_buyer_order"/>
        x1
        <include refid="commonWhere"/>
        order by x1.order_id desc
    </select>
    <select id="getSumPayAmount" resultType="java.math.BigDecimal">
        select ifnull(sum(x1.pay_amount),0) from
        <include refid="view_credit_buyer_order"/>
        x1
        <include refid="commonWhere"/>
        order by x1.order_id desc
    </select>
    <select id="getSumPaidAmount" resultType="java.math.BigDecimal">
        select ifnull(sum(x1.paid_amount),0) from
        <include refid="view_credit_buyer_order"/>
        x1
        <include refid="commonWhere"/>
        order by x1.order_id desc
    </select>
    <select id="getSumUnpaidAmount" resultType="java.math.BigDecimal">
        select ifnull(sum(x1.unpaid_amount),0) from
        <include refid="view_credit_buyer_order"/>
        x1
        <include refid="commonWhere"/>
        order by x1.order_id desc
    </select>
    <select id="getOne" resultType="com.czg.order.dto.CreditBuyerOrderDTO">
        select * from
        <include refid="view_credit_buyer_order"/>
        x1
        <include refid="commonWhere"/>
        order by x1.order_id desc
        limit 1
    </select>
</mapper>