<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: zhangxing5129  
    $Date: 2014-11-24 下午06:06:45  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:o="leaf.database.local.oracle" xmlns:s="leaf.plugin.script" xmlns:f="leaf.database.features"
          xmlns:bm="http://www.leaf-framework.org/schema/bm" baseTable="YONDA_DOCUMENT_LEND_RECORD_HD">
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
               Select t1.*
  From (Select hd.lend_header_id,
               hd.lend_number,
               hd.borrower_name,
               hd.borrower_phone,
               hd.borrower_date,
               hd.lender_name,
               hd.creation_date,
               hd.borrow_to,
               (Select v.code_value_name
                  From sys_code_values_v v
                 Where v.code = 'BORROW_TO'
                   And v.code_value = hd.borrow_to) borrow_to_des,
               hd.unit_id,
               hd.apply_date,
               ln.contract_id,
               hd.reader_name,
               hd.back_date,
               hd.back_number,
               hd.promise_date,
               hd.real_date,
               hd.status,
               (Select t.code_value_name
                  From sys_code_values_v t
                 Where t.code = 'LEND_WFL_STATUS'
                   And t.code_value = hd.status) status_desc,
               hd.borrow_reason,
               hd.send_flag,
               (Select t.code_value_name
                  From sys_code_values_v t
                 Where t.code = 'YES_NO'
                   And t.code_value = hd.send_flag) send_flag_n,
               hd.track_number,
               hd.recipient
          From document_lend_record_ln ln, document_lend_record_hd hd
         Where hd.lend_header_id = ln.lend_header_id(+)
         Group By hd.lend_header_id,
                  hd.borrower_name,
                  hd.lend_number,
                  hd.borrower_phone,
                  hd.borrower_date,
                  hd.lender_name,
                  hd.creation_date,
                  hd.borrow_to,
                  hd.unit_id,
                  hd.apply_date,
                  ln.contract_id,
                  hd.reader_name,
                  hd.back_date,
                  hd.back_number,
                  hd.promise_date,
                  hd.real_date,
                  hd.status,
                  hd.borrow_reason,
                  hd.send_flag,
                  hd.track_number,
                  hd.recipient) t1 #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
                DECLARE
                    v_count NUMBER;
                BEGIN
                    SELECT
                        COUNT(1)
                    INTO
                        v_count
                    FROM
                        document_lend_record_hd h
                    WHERE
                        h.lend_header_id = ${@lend_header_id};
                    IF v_count           = 0 THEN
                        INSERT
                        INTO
                            document_lend_record_hd
                            (
                                lend_header_id,
                                borrower_name,
                                borrower_phone,
                                lender_name,
                                created_by,
                                creation_date,
                                last_updated_by,
                                last_update_date,
                                borrow_to,
                                unit_id,
                                apply_date,
                                reader_name,
                                status,
                                promise_date
                            )
                            VALUES
                            (
                                ${@lend_header_id},
                                ${@borrower_name},
                                ${@borrower_phone},
                                ${@lender_name},
                                ${/session/@user_id},
                                sysdate,
                                ${/session/@user_id},
                                sysdate,
                                ${@borrow_to},
                                ${@unit_id},
                                to_date(${@apply_date},'yyyy-mm-dd'),
                                ${@reader_name},
                                'NEW',
                                to_date(${@promise_date},'yyyy-mm-dd')
                            );
                    ELSE
                        UPDATE
                            document_lend_record_hd h
                        SET
                            h.borrower_name    = ${@borrower_name},
                            h.borrower_phone   = ${@borrower_phone},
                            h.lender_name      = ${@lender_name},
                            h.last_updated_by  = ${/session/@user_id},
                            h.last_update_date = sysdate,
                            h.borrow_to        = ${@borrow_to},
                            h.unit_id          = ${@unit_id},
                            h.apply_date       = to_date(${@apply_date},'yyyy-mm-dd'),
                            h.reader_name      = ${@reader_name},
                            h.status           = ${@status},
                            h.promise_date     = to_date(${@promise_date},'yyyy-mm-dd')
                        WHERE
                            h.lend_header_id = ${@lend_header_id};
                    END IF;
                END;
            ]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:fields>
        <bm:field name="lend_header_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="LEND_HEADER_ID"
                  prompt="YONDA_DOCUMENT_LEND_RECORD_HD.LEND_HEADER_ID"/>
        <bm:field name="borrower_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BORROWER_NAME"
                  prompt="YONDA_DOCUMENT_LEND_RECORD_HD.BORROWER_NAME"/>
        <bm:field name="borrower_phone" databaseType="VARCHAR2" datatype="java.lang.String"
                  physicalName="BORROWER_PHONE" prompt="YONDA_DOCUMENT_LEND_RECORD_HD.BORROWER_PHONE"/>
        <bm:field name="borrower_date" databaseType="DATE" datatype="java.util.Date" physicalName="BORROWER_DATE"
                  prompt="YONDA_DOCUMENT_LEND_RECORD_HD.BORROWER_DATE"/>
        <bm:field name="lender_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="LENDER_NAME"
                  prompt="YONDA_DOCUMENT_LEND_RECORD_HD.LENDER_NAME"/>
        <bm:field name="lend_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="LEND_NUMBER"
                  prompt="YONDA_DOCUMENT_LEND_RECORD_HD.LEND_NUMBER"/>
        <bm:field name="borrow_to" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BORROW_TO"/>
        <bm:field name="borrow_to_des" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false"
                  forUpdate="false"/>
        <bm:field name="unit_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="UNIT_ID"/>
        <bm:field name="apply_date" databaseType="DATE" datatype="java.util.Date" physicalName="APPLY_DATE"/>

        <bm:field name="back_date" databaseType="DATE" datatype="java.util.Date" physicalName="BACK_DATE"/>
        <bm:field name="back_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="BACK_NUMBER"/>

        <bm:field name="reader_name" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="READER_NAME"/>
        <bm:field name="status" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="status"/>
        <bm:field name="status_desc" forInsert="false" forUpdate="false"/>

        <bm:field name="promise_date" databaseType="DATE" datatype="java.util.Date" physicalName="PROMISE_DATE"/>
        <bm:field name="real_date" databaseType="DATE" datatype="java.util.Date" physicalName="REAL_DATE"/>
        <bm:field name="borrow_reason" />
        <bm:field name="send_flag" />
        <bm:field name="send_flag_n" />
        <bm:field name="track_number" />
        <bm:field name="recipient" />
    </bm:fields>
    <bm:features>
        <f:standard-who/>
        <o:sequence-pk sequenceName="yonda_document_lend_hd_s"/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="lend_header_id"/>
    </bm:primary-key>
    <bm:query-fields>
        <bm:query-field field="lend_header_id" queryOperator="="/>
        <!--<bm:query-field field="contract_number" queryOperator="like"/>-->
        <!--<bm:query-field field="contract_name" queryOperator="like"/>-->
        <bm:query-field field="borrower_name" queryOperator="like"/>
        <!--<bm:query-field field="bp_name" queryOperator="like"/>-->
        <bm:query-field name="apply_date_from"
                        queryExpression="t1.apply_date &gt;= to_date(${@apply_date_from},&apos;yyyy-mm-dd&apos;) "/>
        <bm:query-field name="apply_date_to"
                        queryExpression="t1.apply_date &lt;= to_date(${@apply_date_to},&apos;yyyy-mm-dd&apos;)"/>
        <bm:query-field name="return_all_flag" queryExpression="t1.return_all_flag=${@return_all_flag}"/>
    </bm:query-fields>
</bm:model>
