drop function if exists staging2.fn_PTO_MPM_block;
CREATE OR REPLACE FUNCTION staging2.fn_PTO_MPM_block(p_client_id int,p_function_id int, p_file_mnemonic text,p_file_sheet_mnemonic text, p_file_syspk int)
   RETURNS text AS $$
   declare __make text;
   declare __model text;
   
   declare __client_id int :=p_client_id;
   declare __function_id int :=p_function_id;
   declare __file_mnemonic text  :=p_file_mnemonic;
   declare __file_sheet_mnemonic text :=p_file_sheet_mnemonic;
   declare __file_syspk int :=p_file_syspk;
   declare err_state   text;
   declare err_msg     text;
   declare err_detail  text;
   declare err_hint    text;
   declare err_context text;   
   declare err_query int;
   declare err_block text;    
  
 begin

/************************************************************************************
Function Name:fn_PTOBEN_MPM_block
Function Desc: This function populates data into staging2 blocks
File Format: PTOBEN
Sheet Format: PTOBEN_MPMB
Creation Date: April 26 2021
Updation Date:
Author: compegence team
Function Call: select staging2.fn_PTO_MPM_block(1,1,'PTOBEN','PTOBEN_MPM',944)
***************************************************************************************/

SET search_path TO  staging2;

/* rerunnability - delete block tables and update config tables to null */
truncate table staging2.stg_specific_table_PTOBEN_MPM;
truncate table  staging2.stg_process_table_PTOBEN_MPM;
truncate table  staging2.PTOBEN_MPM_performance_boost_natural_block;


execute 'delete from fw_core.fw_jobctl_file_sheet_block_run_schedule where file_syspk='||p_file_syspk||' and file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';


execute 'update transactional.source_config a
set row_number_start=null,
row_previous_number=null,
row_read_end=null,
run_time=null
where   a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';

/* transfer data from generic to specific for PTOBST */
execute 'insert into  staging2.stg_specific_table_PTOBEN_MPM 
select * from staging1.staging_generic_table a
where   a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''
and a.file_syspk='||p_file_syspk||'';

select count(*) into err_query from staging2.stg_specific_table_PTOBEN_MPM;

if err_query=0 then
err_context := 'data not present';
	raise exception using 
	      message = 'No Data for pto',
          detail = 'No data in table stg_specific_table_PTOBEN_PRF',  
          errcode = 'PTO', 
          hint = 'check sheet mnemonic in generic table, if it is null update it';
end if;	

/* trimming data */
update staging2.stg_specific_table_PTOBEN_MPM set column3 =  TRIM (TRAILING FROM column3 );
update staging2.stg_specific_table_PTOBEN_MPM set column3 =  TRIM (LEADING FROM column3 );

update transactional.source_config set F1_source=F1_modified ;

/* keyword match in config table*/
execute 'update transactional.source_config a  
set row_number_start=(select min(b.row_number)
from staging2.stg_specific_table_PTOBEN_MPM  b
where trim(upper(F1_modified))=trim(upper(column2))
and b.is_rownumber_fetched is null)
where a.row_number_start is null
and a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';

/*  reverese update*/
execute 'update staging2.stg_specific_table_PTOBEN_MPM  a  
set is_rownumber_fetched=1
from transactional.source_config  b
where trim(upper(F1_modified))=trim(upper(column3))
and b.row_number_start=a.row_number
and is_rownumber_fetched is null
and a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';

execute 'update transactional.source_config a set row_previous_number=row_number_start-1
where a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';

execute 'update transactional.source_config a
set row_read_end= (select b.row_number_start
from transactional.source_config b 
where b.syspk=a.syspk+1 and file_mnemonic='''||p_file_mnemonic||''' and file_sheet_mnemonic='''||p_file_sheet_mnemonic||''' ) 
where a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';

/* config file last field update as null otherwise it picks up next format row number*/
execute 'update transactional.source_config a 
set row_read_end = null ,run_time=current_timestamp
where f1_modified =''Performance - Boost Mode @ Natural Ambient'' 
and a.file_mnemonic='''||p_file_mnemonic||''' and a.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';

/* tagging block_row_numbers*/

execute 'insert into staging2.stg_process_table_PTOBEN_MPM
select a.* , RANK () OVER ( ORDER BY row_number) as block_row_number,b.block_tag as block_tag
from  staging2.stg_specific_table_PTOBEN_MPM  a
join transactional.source_config b 
on a.row_number >=row_number_start 
and (a.row_number< row_read_end or row_read_end is null)
and f1_modified=''Data Inputs''
and  b.file_mnemonic='''||p_file_mnemonic||''' and b.file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';


select column5 into __make from staging2.stg_process_table_PTOBEN_PRF a
where  block_tag='PTOBEN_PRF_Key_Performance_Parameters' and block_row_number=7 ;

select column5  into __model from staging2.stg_process_table_PTOBEN_PRF a
where  block_tag='PTOBEN_PRF_Key_Performance_Parameters' and block_row_number=9;

/* block - PTOBEN_MPM_performance_boost_natural_block*/

perform fw_core.fn_jobctl_block_begin(__client_id,__function_id,__file_syspk,
'PTOBEN_MPM_performance_boost_natural_block',__file_mnemonic,__file_sheet_mnemonic,1); 
err_block :='PTOBEN_MPM_performance_boost_natural_block';
insert into staging2.PTOBEN_MPM_performance_boost_natural_block
(
c2,
c3,
c4,
c5,
block_row_number
)
select column2,column3,column4 ,column5,block_row_number from staging2.stg_process_table_PTOBEN_MPM
where block_tag='PTOBEN_MPM_data_inputs';


update staging2.PTOBEN_MPM_performance_boost_natural_block
set test_condition=c2
where block_row_number=2;

update staging2.PTOBEN_MPM_performance_boost_natural_block
set c2=null
where block_row_number=2;


update staging2.PTOBEN_MPM_performance_boost_natural_block a 
set test_condition= b.first_value from (SELECT
  block_row_number, test_condition, value_partition, first_value(test_condition) over (partition by value_partition order by block_row_number)
FROM (
  SELECT
    block_row_number,
    test_condition,
    sum(case when test_condition is null then 0 else 1 end) over (order by block_row_number) as value_partition  
    FROM staging2.PTOBEN_MPM_performance_boost_natural_block 
  ORDER BY block_row_number ASC
) as q) b where a.block_row_number = b.block_row_number ;


update staging2.PTOBEN_MPM_performance_boost_natural_block
set trx_record=0
where block_row_number in (1,2,3);

 execute 'update staging2.PTOBEN_MPM_performance_boost_natural_block set client_id='||p_client_id||'';
 execute 'update staging2.PTOBEN_MPM_performance_boost_natural_block set function_id='||p_function_id||'';
 execute 'update staging2.PTOBEN_MPM_performance_boost_natural_block set file_mnemonic='''||p_file_mnemonic||'''';
 execute 'update staging2.PTOBEN_MPM_performance_boost_natural_block set file_sheet_mnemonic='''||p_file_sheet_mnemonic||'''';
 execute 'update staging2.PTOBEN_MPM_performance_boost_natural_block set file_syspk='||p_file_syspk||'';
  
 update staging2.PTOBEN_MPM_performance_boost_natural_block set make=__make; 
 update staging2.PTOBEN_MPM_performance_boost_natural_block set model=__model;


perform fw_core.fn_jobctl_block_end(__file_syspk,'PTOBEN_MPM_performance_boost_natural_block'); 
 err_context := '';
perform fw_core.fn_insert_db_error ( __client_id, __function_id, 1001, 'Compegence', __file_syspk ,__file_mnemonic,__file_sheet_mnemonic ,null,'stg2', 'fn_PTO_MPM_block', err_state, err_msg, err_detail, err_hint, err_context,'success');
return err_context;	

EXCEPTION when OTHERS then 
	
GET STACKED DIAGNOSTICS 
       err_state   = returned_sqlstate,
       err_msg     = message_text,
       err_detail  = pg_exception_detail,
       err_hint    = pg_exception_hint,
       err_context = pg_exception_context;

   	   perform  fw_core.fn_insert_db_error ( __client_id, __function_id, 1001, 'Compegence', __file_syspk ,__file_mnemonic,__file_sheet_mnemonic ,err_block,'stg2', 'fn_PTO_MPM_block', err_state, err_msg, err_detail, err_hint, err_context,'error');
return err_context;
end
$$ LANGUAGE plpgsql;
