<?xml version="1.0" encoding="UTF-8"?>
<!--
    $Author: ZLF  
    $Date: 2014-11-7 上午9:34:20  
    $Revision: 1.0  
    $Purpose: 
-->
<bm:model xmlns:bm="http://www.leaf-framework.org/schema/bm" xmlns:f="leaf.database.features" alias="t1" baseTable="CON_CONTRACT_ITEM_DETAIL">
    <bm:fields>
        <bm:field name="item_detail_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="ITEM_DETAIL_ID" prompt="CON_CONTRACT_ITEM_DETAIL.ITEM_DETAIL_ID"/>
        <bm:field name="contract_lease_item_id" databaseType="NUMBER" datatype="java.lang.Long" physicalName="CONTRACT_LEASE_ITEM_ID"/>
        <bm:field name="item_frame_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="ITEM_FRAME_NUMBER" prompt="CON_CONTRACT_ITEM_DETAIL.ITEM_FRAME_NUMBER"/>
        <bm:field name="item_engine_number" databaseType="VARCHAR2" datatype="java.lang.String" physicalName="ITEM_ENGINE_NUMBER" prompt="CON_CONTRACT_ITEM_DETAIL.ITEM_ENGINE_NUMBER"/>
    </bm:fields>
    <bm:features>
        <f:standard-who/>
    </bm:features>
    <bm:primary-key>
        <bm:pk-field name="item_detail_id"/>
    </bm:primary-key>
    <bm:operations>
        <bm:operation name="query">
            <bm:query-sql><![CDATA[
                SELECT
                	c3.item_detail_id,
                    c3.contract_lease_item_id,
                    c3.item_frame_number,
                    c3.item_engine_number
                FROM
                    con_contract_item_detail c3 #WHERE_CLAUSE#
            ]]></bm:query-sql>
        </bm:operation>
        <bm:operation name="update">
            <bm:update-sql><![CDATA[
            	declare
            		v_length number;
            		e_frame_err exception;
            		v_con_contract con_contract%rowtype;
            		v_standard_history_id number;
            	begin
            		v_length:=length(${@item_frame_number});
            		if v_length != 17 then
            			raise e_frame_err;
            		end if;
            		select t.* into v_con_contract from con_contract t,con_contract_item_detail d where t.contract_id=d.contract_id
            		and d.ITEM_DETAIL_ID = ${@item_detail_id};
            		yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => v_con_contract.contract_id,
                                                  p_document_type     => v_con_contract.document_type,
                                                  p_document_category => v_con_contract.document_category,
                                                  p_doc_status        => yonda_doc_history_pkg.yonda_con_frame_maintain,
                                                  p_instance_id       => null,
                                                  p_user_id           => ${/session/@user_id});
                    if nvl(${@item_frame_number_bak},'')='' then
                      ${@item_frame_number_bak}:='此记录为新增纪录';
                    end if;
                    
                    if nvl(${@item_engine_number_bak},'')='' then
                      ${@item_engine_number_bak}:='此记录为新增纪录';
                    end if;
                   
                    if ${@item_frame_number_bak}!=${@item_frame_number} then                             
						hls_standard_history_pkg.insert_std_history(
	        	 		 p_standard_history_id=>v_standard_history_id,
			             p_table_name=>'CON_CONTRACT_ITEM_DETAIL',
			             p_table_pk_value=>${@item_detail_id},
			             p_parent_table => 'CON_CONTRACT_LEASE_ITEM',
			             p_parent_table_pk_value=>${@contract_lease_item_id},
			             p_source_table=>'CON_CONTRACT',
			             p_source_table_pk_value=>v_con_contract.contract_id,
			             p_layout_code=>'STD',
			             p_tab_code=>'STD',
			             p_column_name=>'ITEM_FRAME_NUMBER',
			             p_from_column_value=>${@item_frame_number_bak},
			             p_to_column_value=>${@item_frame_number},
			             p_operation_date=>sysdate,
			             p_confirm_flag =>'N',
			             p_user_id=>${/session/@user_id}
		             );
	                end if;
	             
	                if ${@item_engine_number_bak}!=${@item_engine_number} then
		             	hls_standard_history_pkg.insert_std_history(
	        	 		 p_standard_history_id=>v_standard_history_id,
			             p_table_name=>'CON_CONTRACT_ITEM_DETAIL',
			             p_table_pk_value=>${@item_detail_id},
			             p_parent_table => 'CON_CONTRACT_LEASE_ITEM',
			             p_parent_table_pk_value=>${@contract_lease_item_id},
			             p_source_table=>'CON_CONTRACT',
			             p_source_table_pk_value=>v_con_contract.contract_id,
			             p_layout_code=>'STD',
			             p_tab_code=>'STD',
			             p_column_name=>'ITEM_ENGINE_NUMBER',
			             p_from_column_value=>${@item_engine_number_bak},
			             p_to_column_value=>${@item_engine_number},
			             p_operation_date=>sysdate,
			             p_confirm_flag =>'N',
			             p_user_id=>${/session/@user_id}
		             );
	                end if;
					                                                  
	        		UPDATE
					    CON_CONTRACT_ITEM_DETAIL t1
					SET
					    t1.ITEM_FRAME_NUMBER =${@item_frame_number},
					    t1.ITEM_ENGINE_NUMBER=${@item_engine_number},
					    t1.key_flag		     =${@key_flag},
					    t1.registration_flag =${@registration_flag},
					    t1.purchase_flag     =${@purchase_flag},
					    t1.insurance_flag    =${@insurance_flag},
					    t1.LAST_UPDATED_BY   =${/session/@user_id},
					    t1.LAST_UPDATE_DATE  =sysdate					    
					WHERE
					    t1.ITEM_DETAIL_ID = ${@item_detail_id};
					    
					exception
						when e_frame_err then 
							sys_raise_app_error_pkg.raise_user_define_error('CON_CONTRACT_ITEM_DETAIL_PKG.FRAME_NUMBER_ERROR',
                                                      ${/session/@user_id},
                                                      '',
                                                      '');
      						raise_application_error(sys_raise_app_error_pkg.c_error_number,sys_raise_app_error_pkg.g_err_line_id);
				end;
        	]]></bm:update-sql>
        </bm:operation>
    </bm:operations>
    <bm:query-fields>
        <bm:query-field name="contract_lease_item_id" queryExpression="c3.contract_lease_item_id = ${@contract_lease_item_id}"/>
        <bm:query-field field="item_frame_number" queryOperator="like"/>
        <bm:query-field field="item_engine_number" queryOperator="like"/>
    </bm:query-fields>
</bm:model>
