<?xml version="1.0" encoding="UTF-8"?>
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" needAccessControl="false">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                    t1.*
                FROM
                    (SELECT DISTINCT
                        cci.contract_lease_item_id,
                        cc.contract_id,
                        NVL(cci.price, 0) sin_car_finance_amount,
                        NVL(cc.finance_amount, 0) con_finance_amount,
                        NVL(cc.deposit, 0) deposit, --保证金
                        (SELECT
                            SUM(NVL(cf.principal, 0)) - SUM(NVL(cf.received_principal, 0))
                        FROM
                            con_contract_cashflow cf
                        WHERE
                            cf.contract_id  = cc.contract_id AND
                            cf.cf_item      = 1 AND
                            cf.cf_direction = 'INFLOW'
                        ) over_principal, -- 剩余本金
                        (SELECT
                            SUM(NVL(cf.due_amount, 0)) - SUM(NVL(cf.received_amount, 0))
                        FROM
                            con_contract_cashflow cf
                        WHERE
                            cf.contract_id  = cc.contract_id AND
                            cf.cf_item      = 1 AND
                            cf.cf_direction = 'INFLOW'
                        ) over_due_amount, -- 剩余租金
                        (SELECT
                            NVL(SUM(NVL(cf.due_amount, 0)), 0)
                        FROM
                            con_contract_cashflow cf
                        WHERE
                            cf.contract_id  = cc.contract_id AND
                            cf.cf_item      = 9 AND
                            cf.cf_direction = 'INFLOW'
                        ) interest -- 支付表罚息
                    FROM
                        con_contract_lease_item cci,
                        con_contract cc,
                        con_contract_cashflow ccc
                    WHERE
                        cci.contract_id = cc.contract_id AND
                        cc.contract_id  = ccc.contract_id and 
                        cci.contract_lease_item_id = ${@contract_lease_item_id}
                    ) t1
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
</bm:model>
