<?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.fjhx.purchase.mapper.purchase.PurchaseMapper">
    <select id="getPage" resultType="com.fjhx.purchase.entity.purchase.vo.PurchaseVo">
        select p.*,
               (SELECT IFNULL(SUM(IFNULL(money, 0) * IFNULL(arw.rate, 0)), 0)
                FROM pay_detail t1
                         LEFT JOIN pay t2 ON t1.pay_id = t2.id
                         JOIN bytesailing_account.account_payment ap ON ap.business_id = t2.id
                         JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id
                WHERE t2.`status` &gt;= 30
                  AND t2.`status` &lt; 88
                  AND t1.purchase_id = p.id)                                                  AS paySumAmount,
               (SELECT `code` FROM `bytesailing_sale`.contract WHERE id = p.data_resource_id) AS contractCode
        from purchase p
            ${ew.customSqlSegment}
    </select>

    <select id="paymentBill" resultType="com.fjhx.purchase.entity.purchase.dto.PurchaseDto">
        SELECT pu.code,
               pu.supply_id,
               bs.`name`                supplyName,
               pu.amount,
               IFNULL(SUM(nd.money), 0) invoiceAmount,
               pu.pay_status            payStatus,
               pu.currency,
               pu.id
        FROM purchase pu
                 LEFT JOIN bytesailing_supply.supplier_info bs ON pu.supply_id = bs.id
                 LEFT JOIN invoice_details nd on pu.id = nd.purchase_id
            ${ew.customSqlSegment}
    </select>

    <select id="arrivalAmount" resultType="com.fjhx.purchase.entity.purchase.dto.PurchaseDto">
        SELECT SUM(wd.receipt_quantity * pd.price) amount,
               pd.purchase_id                      id
        FROM bytesailing_wms.stock_wait sw
                 JOIN bytesailing_wms.stock_wait_details wd on sw.id = wd.stock_wait_id
                 JOIN arrival_detail ad on wd.business_details_id = ad.id
                 JOIN purchase_detail pd on ad.purchase_detail_id = pd.id
            ${ew.customSqlSegment}
    </select>

    <select id="paidAmount" resultType="com.fjhx.purchase.entity.purchase.dto.PurchaseDto">
        SELECT
            sum(pd.money) amount,
            p.currency,
            pd.purchase_id id
        FROM
            pay_detail pd
                LEFT JOIN pay p ON pd.pay_id = p.id
            ${ew.customSqlSegment}
    </select>

</mapper>
