<?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.mapper.contract.ContractPayMapper">

    <select id="getSupplierInfoList" resultType="com.fjhx.params.contract.SupplierInfo">
        select c.supplier_id        supplierId,
               s.name               supplierName,
               sum(c.adjust_amount) totalAmount
        from contract c
                 left join supplier s on c.supplier_id = s.id
            ${ew.customSqlSegment}
    </select>

    <select id="getPage" resultType="java.util.LinkedHashMap">
        select c.id                                                                    contractId,
               c.code                                                                  contractCode,
               s.name                                                                  supplierName,
               c.complete_time                                                         completeTime,
               c.account_date                                                          accountDate,
               c.invoice_amount                                                        invoiceAmount,
               m.name                                                                  materialName,
               m.spec                                                                  materialSpec,
               cd.quantity,
               cd.price,
               cd.apply_purchase_id                                                    applyPurchaseId,
               cd.apply_purchase_details_id                                            applyPurchaseDetailsId,
               c.amount_money                                                          amountMoney,
               c.adjust_amount                                                         adjustAmount,
               c.had_pay_amount                                                        hadPayAmount,
               (c.adjust_amount - c.back_amount - c.had_pay_amount - c.express_amount) unpaidAmount,
               c.apply_pay_amount                                                      applyPayAmount,
               ifnull(t.batchCount, 0)                                                 batchCount,
               ifnull(t.qualityTestingCount, 0)                                        qualityTestingCount,
               ifnull(t.contractFileCount, 0)                                          contractFileCount
        from contract c
                 left join contract_details cd on cd.contract_id = c.id
                 left join material m on cd.material_id = m.id
                 left join supplier s on c.supplier_id = s.id
                 left join (
            select count(wb.id)               batchCount,
                   count(qt.id)               qualityTestingCount,
                   sum(if(ce.type = 3, 1, 0)) contractFileCount,
                   wb.contract_id
            from water_batch wb
                     left join quality_testing qt on wb.id = qt.water_batch_id
                     left join contract_enclosure ce on wb.contract_id = ce.contract_id
            group by wb.contract_id
        ) t on t.contract_id = c.id
            ${ew.customSqlSegment}
    </select>

    <select id="statusStatistics" resultType="java.util.Map">
        select ifnull(sum(if(c.apply_pay_amount = 0
                                 and t.batchCount = t.qualityTestingCount
                                 and t.contractFileCount > 0
                                 and c.invoice_amount >= c.adjust_amount,
                             1, 0)), 0) canLaunch,
               ifnull(sum(if(t.batchCount != t.qualityTestingCount
                                 or t.contractFileCount &lt;= 0
                                 or c.invoice_amount &lt; c.adjust_amount,
                             1, 0)), 0) needSupplemented,
               count(0)                 total
        from contract c
                 left join (
            select count(wb.id)               batchCount,
                   count(qt.id)               qualityTestingCount,
                   sum(if(ce.type = 3, 1, 0)) contractFileCount,
                   wb.contract_id
            from water_batch wb
                     left join quality_testing qt on wb.id = qt.water_batch_id
                     left join contract_enclosure ce on wb.contract_id = ce.contract_id
            group by wb.contract_id
        ) t on t.contract_id = c.id
            ${ew.customSqlSegment}
    </select>

    <select id="cycleStatistics" resultType="java.util.Map">
        select ifnull(sum(if(date_add(c.complete_time, interval c.account_date day) &lt; #{currentAccountBeginTime}, 1,
                             0)), 0)
                        historicalCycle,
               ifnull(sum(if(date_add(c.complete_time, interval c.account_date day) &gt; #{currentAccountEndTime}, 1,
                             0)), 0)
                        futureCycle,
               count(0) total,
               (count(0)
                   - ifnull(
                        sum(if(date_add(c.complete_time, interval c.account_date day) &lt; #{currentAccountBeginTime},
                               1, 0)), 0)
                   - ifnull(
                        sum(if(date_add(c.complete_time, interval c.account_date day) &gt; #{currentAccountEndTime}, 1,
                               0)), 0))
                        currentCycle
        from contract c
                 left join (
            select count(wb.id)               batchCount,
                   count(qt.id)               qualityTestingCount,
                   sum(if(ce.type = 3, 1, 0)) contractFileCount,
                   wb.contract_id
            from water_batch wb
                     left join quality_testing qt on wb.id = qt.water_batch_id
                     left join contract_enclosure ce on wb.contract_id = ce.contract_id
            group by wb.contract_id
        ) t on t.contract_id = c.id
            ${ew.customSqlSegment}
    </select>

</mapper>
