<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: zhuxianfei
    $Date: 2018/6/25 13:13
    $Revision: 1.0 
-->
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
               Select t1.*
                  From (Select tc.assets_id,
                               tc.assets_number,
                               tc.assets_name,
                               (Select Sum(ccc.due_amount)
                                  From con_contract_cashflow  ccc,
                                       con_contract           cc,
                                       tre_assets_contract_ln a
                                 Where ccc.contract_id = cc.contract_id
                                   And cc.contract_id = a.contract_id
                                   And a.assets_id = tc.assets_id
                                   And ccc.cf_item = 1
                                   And ccc.cf_status = 'RELEASE'
                                   And ccc.cf_direction = 'INFLOW') total_amount, --金额总和
                               (Select Count(1)
                                  From tre_assets_contract_ln t1
                                 Where t1.assets_id = tc.assets_id) total_count, --合同个数
                               (Select t2.loan_contract_number
                                  From tre_loan_contract t2
                                 Where t2.loan_contract_id = tc.loan_contract_id) loan_contract_number,
                               tc.loan_institution, --融资机构
                               (Select bp.bp_name
                                  From hls_bp_master bp
                                 Where bp.bp_id = tc.loan_institution) loan_institution_n,
                               tc.status,
                               (Select v.code_value_name
                                  From sys_code_values_v v
                                 Where v.code = 'TRE_ASSETS_STATUS'
                                   And v.code_value = tc.status) As status_n,
                               tc.creation_date
                          From tre_assets_contract tc) t1
              #WHERE_CLAUSE#
    ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                begin

                 update tre_assets_contract t
                    set t.status = 'NEW'
                  where t.assets_id = ${@assets_id};

                end;]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="execute">
            <bm:update-sql><![CDATA[
                BEGIN
                ${@v_message} := tre_assets_import_pkg.tre_assets_contract_import(p_header_id => ${@header_id},p_assets_id => ${@assets_id}, p_user_id => ${/session/@user_id});
                END;
                ]]></bm:update-sql>
            <bm:parameters>
                <bm:parameter name="v_message" output="true" outputPath="@v_message"/>
            </bm:parameters>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="assets_id"/>
        <bm:field name="assets_number"/>
        <bm:field name="assets_name"/>
        <bm:field name="total_amount"/>
        <bm:field name="total_count"/>
        <bm:field name="loan_contract_number"/>
        <bm:field name="loan_institution"/>
        <bm:field name="loan_institution_n"/>
        <bm:field name="status"/>
        <bm:field name="status_n"/>
        <bm:field name="creation_date"/>
    </bm:fields>
    <bm:query-fields>
        <bm:query-field name="assets_number" queryExpression="t1.assets_number = ${@assets_number}"/>
        <bm:query-field name="assets_name" queryExpression="t1.assets_name like ${@assets_name}"/>
        <bm:query-field name="status" queryExpression="t1.status =  ${@status}"/>
        <bm:query-field name="loan_contract_number" queryExpression="t1.loan_contract_number =  ${@loan_contract_number}"/>
    </bm:query-fields>
</bm:model>