<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: zhangxing5129  
    $Date: 2014-11-24 下午06:07:14  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:o="leaf.database.local.oracle" xmlns:f="leaf.database.features" xmlns:bm="http://www.leaf-framework.org/schema/bm" alias="t1" baseTable="YONDA_DOCUMENT_LEND_RECORD_LN" needAccessControl="false">
    <bm:fields>
        <bm:field name="lend_line_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="LEND_LINE_ID" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.LEND_LINE_ID"/>
        <bm:field name="lend_header_id" databaseType="NUMBER" datatype="java.lang.Long" insertExpression="${../../@lend_header_id}" physicalName="LEND_HEADER_ID" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.LEND_HEADER_ID"/>
        <bm:field name="contract_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_ID" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.CONTRACT_ID"/>
        <bm:field name="cdd_item_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CDD_ITEM_ID" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.CDD_ITEM_ID"/>
        <bm:field name="cdd_item_desc" expression="(select ci.description from prj_cdd_item ci where ci.cdd_item_id=t1.cdd_item_id )" forInsert="false" forUpdate="false"/>
        <bm:field name="return_date" databaseType="DATE" datatype="java.util.Date" physicalName="RETURN_DATE"/>
        <bm:field name="lend_flag" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="LEND_FLAG"/>
        <bm:field name="return_flag" expression="(case when t1.return_date is null then &apos;N&apos; else &apos;Y&apos; end)" forInsert="false" forUpdate="false"/>
        <bm:field name="ref_v01" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V01" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_V01"/>
        <bm:field name="ref_v02" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V02" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_V02"/>
        <bm:field name="ref_v03" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V03" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_V03"/>
        <bm:field name="ref_v04" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V04" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_V04"/>
        <bm:field name="ref_v05" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="REF_V05" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_V05"/>
        <bm:field name="ref_n01" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N01" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_N01"/>
        <bm:field name="ref_n02" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N02" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_N02"/>
        <bm:field name="ref_n03" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N03" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_N03"/>
        <bm:field name="ref_n04" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N04" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_N04"/>
        <bm:field name="ref_n05" databaseType="NUMBER" datatype="java.lang.Long" physicalName="REF_N05" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_N05"/>
        <bm:field name="ref_d01" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D01" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_D01"/>
        <bm:field name="ref_d02" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D02" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_D02"/>
        <bm:field name="ref_d03" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D03" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_D03"/>
        <bm:field name="ref_d04" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D04" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_D04"/>
        <bm:field name="ref_d05" databaseType="DATE" datatype="java.util.Date" physicalName="REF_D05" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.REF_D05"/>
        <bm:field name="doc_type" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="DOC_TYPE" prompt="YONDA_DOCUMENT_LEND_RECORD_LN.DOC_TYPE"/>
        <bm:field name="chest_number" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false" forUpdate="false" physicalName="CHEST_NUMBER"/>
        <bm:field name="docu_number" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false" forUpdate="false" physicalName="DOCU_NUMBER"/>
        <bm:field name="doc_type_n" databaseType="VARCHAR2" datatype="java.lang.String" forInsert="false" forUpdate="false" physicalName="DOC_TYPE_N"/>
    </bm:fields>
    <bm:operations>
        <bm:operation name="insert">
            <bm:update-sql><![CDATA[
                BEGIN
                    INSERT
                    INTO
                        YONDA_DOCUMENT_LEND_RECORD_LN
                        (
                            LEND_LINE_ID,
                            LEND_HEADER_ID,
                            CONTRACT_ID,
                            CDD_ITEM_ID,
                            RETURN_DATE,
                            LEND_FLAG,
                            REF_V01,
                            REF_V02,
                            REF_V03,
                            REF_V04,
                            REF_V05,
                            REF_N01,
                            REF_N02,
                            REF_N03,
                            REF_N04,
                            REF_N05,
                            REF_D01,
                            REF_D02,
                            REF_D03,
                            REF_D04,
                            REF_D05,
                            DOC_TYPE,
                            CREATED_BY,
                            CREATION_DATE,
                            LAST_UPDATED_BY,
                            LAST_UPDATE_DATE
                        )
                        VALUES
                        (
                            yonda_document_lend_ln_s.NEXTVAL,
                            ${../../@lend_header_id},
                            ${@contract_id},
                            ${@cdd_item_id},
                            to_date(${@return_date},'yyyy-mm-dd'),
                            ${@lend_flag},
                            ${@ref_v01},
                            ${@ref_v02},
                            ${@ref_v03},
                            ${@ref_v04},
                            ${@ref_v05},
                            ${@ref_n01},
                            ${@ref_n02},
                            ${@ref_n03},
                            ${@ref_n04},
                            ${@ref_n05},
                            ${@ref_d01},
                            ${@ref_d02},
                            ${@ref_d03},
                            ${@ref_d04},
                            ${@ref_d05},
                            ${@doc_type},
                            ${/session/@user_id},
                            sysdate,
                            ${/session/@user_id},
                            sysdate
                        )
                        RETURNING LEND_LINE_ID
                    INTO
                        ${@lend_line_id};
                   
                END;
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
                BEGIN
                    UPDATE
                        YONDA_DOCUMENT_LEND_RECORD_LN t1
                    SET
                        t1.LEND_HEADER_ID  =${@lend_header_id},
                        t1.CONTRACT_ID     =${@contract_id},
                        t1.CDD_ITEM_ID     =${@cdd_item_id},
                        t1.RETURN_DATE     =to_date(${@return_date},'yyyy-mm-dd'),
                        t1.LEND_FLAG       =${@lend_flag},
                        t1.REF_V01         =${@ref_v01},
                        t1.REF_V02         =${@ref_v02},
                        t1.REF_V03         =${@ref_v03},
                        t1.REF_V04         =${@ref_v04},
                        t1.REF_V05         =${@ref_v05},
                        t1.REF_N01         =${@ref_n01},
                        t1.REF_N02         =${@ref_n02},
                        t1.REF_N03         =${@ref_n03},
                        t1.REF_N04         =${@ref_n04},
                        t1.REF_N05         =${@ref_n05},
                        t1.REF_D01         =${@ref_d01},
                        t1.REF_D02         =${@ref_d02},
                        t1.REF_D03         =${@ref_d03},
                        t1.REF_D04         =${@ref_d04},
                        t1.REF_D05         =${@ref_d05},
                        t1.DOC_TYPE        =${@doc_type},
                        t1.LAST_UPDATED_BY =${/session/@user_id},
                        t1.LAST_UPDATE_DATE=sysdate
                    WHERE
                        t1.LEND_LINE_ID = ${@lend_line_id};
             
                END;
            ]]></bm:update-sql>
        </bm:operation>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
               SELECT
                    *
                FROM
                    (SELECT
                        (SELECT
                            v.code_value_name AS value_name
                        FROM
                            sys_code_values_v v
                        WHERE
                            v.code      = 'DOC_TYPE' AND
                            v.code_value=ccll.doc_type
                        ) doc_type_n ,
                        ccll.chest_number,
                        ccll.docu_number,
                        ydlr.LEND_LINE_ID,
                        ydlr.LEND_HEADER_ID,
                        ydlr.CONTRACT_ID,
                        ydlr.CDD_ITEM_ID,
                       -- (SELECT
                        --    ci.description
                       -- FROM
                       --     prj_cdd_item ci
                      --  WHERE
                      --      ci.cdd_item_id=ydlr.cdd_item_id
                      --  ) AS cdd_item_desc,
                      pci.description as cdd_item_desc,
                        ydlr.RETURN_DATE,
                        'Y' AS lend_flag,
                        (
                            CASE
                                WHEN ydlr.return_date IS NULL
                                THEN 'N'
                                ELSE 'Y'
                            END) AS return_flag,
                        ydlr.REF_V01,
                        ydlr.REF_V02,
                        ydlr.REF_V03,
                        ydlr.REF_V04,
                        ydlr.REF_V05,
                        ydlr.REF_N01,
                        ydlr.REF_N02,
                        ydlr.REF_N03,
                        ydlr.REF_N04,
                        ydlr.REF_N05,
                        ydlr.REF_D01,
                        ydlr.REF_D02,
                        ydlr.REF_D03,
                        ydlr.REF_D04,
                        ydlr.REF_D05,
                        ydlr.DOC_TYPE
                    FROM
                       YONDA_DOCUMENT_LEND_RECORD_LN ydlr,
                           LEND_CONTENT_LIST             lcl,
                           CON_CONTRACT_LOCATION_LN      ccll,
                           prj_cdd_item    pci
                   where lcl.lend_header_id = ydlr.lend_header_id
                       and ydlr.contract_id = lcl.contract_id
                       and ccll.contract_id = lcl.contract_id
                      and pci.doc_type = ccll.doc_type
                      and pci.cdd_item_id=lcl.cdd_item_id

                    ) t1 #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
    </bm:operations>
    <bm:features>
        <f:standard-who/>
        <o:sequence-pk sequenceName="yonda_document_lend_ln_s"/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="lend_line_id"/>
    </bm:primary-key>
    <bm:query-fields>
        <bm:query-field field="contract_id" queryOperator="="/>
        <bm:query-field field="lend_header_id" queryOperator="="/>
    </bm:query-fields>
</bm:model>
