/*******Main Section**************************************************************************/
\set ON_ERROR_STOP on
\set AUTOCOMMIT on
\timing on
 
DELETE FROM data_api.scrm_contact;

insert into  data_api.scrm_contact ( 
	 img 
	, not_acceptsms 
	, gender 
	, create_method 
	, mobile_verified 
	, language 
	, is_member 
	, source 
	, is_referrer 
	, create_from 
	, last_updated 
	, date_created 
	, acceptsms 
	, id 
	, subscribe_scene 
	, id_str 
	, is_employee 
	, version 
	, not_accept_email 
	, date_join 
	, content_name 
	, update_method 
	, email_verified 
	, is_anonymous 
	, accept_email 
	, create_from_name 
	, status 
	,etl_tx_dt
)
select 
	 case when trim(both from img)='' then null else  img::text  end  img 
	, case when trim(both from not_acceptsms)='' then null else  not_acceptsms::text  end  not_acceptsms 
	, case when trim(both from gender)='' then null else  gender::text  end  gender 
	, case when trim(both from create_method)='' then null else  create_method::text  end  create_method 
	, case when trim(both from mobile_verified)='' then null else  mobile_verified::text  end  mobile_verified 
	, case when trim(both from language)='' then null else  language::text  end  language 
	, case when trim(both from is_member)='' then null else  is_member::text  end  is_member 
	, case when trim(both from source)='' then null else  source::text  end  source 
	, case when trim(both from is_referrer)='' then null else  is_referrer::text  end  is_referrer 
	, case when trim(both from create_from)='' then null else  create_from::text  end  create_from 
	, case when trim(both from last_updated)='' then null else  last_updated::text  end  last_updated 
	, case when trim(both from date_created)='' then null else  date_created::text  end  date_created 
	, case when trim(both from acceptsms)='' then null else  acceptsms::text  end  acceptsms 
	, case when trim(both from id)='' then null else  id::text  end  id 
	, case when trim(both from subscribe_scene)='' then null else  subscribe_scene::text  end  subscribe_scene 
	, case when trim(both from id_str)='' then null else  id_str::text  end  id_str 
	, case when trim(both from is_employee)='' then null else  is_employee::text  end  is_employee 
	, case when trim(both from version)='' then null else  version::text  end  version 
	, case when trim(both from not_accept_email)='' then null else  not_accept_email::text  end  not_accept_email 
	, case when trim(both from date_join)='' then null else  date_join::text  end  date_join 
	, case when trim(both from content_name)='' then null else  content_name::text  end  content_name 
	, case when trim(both from update_method)='' then null else  update_method::text  end  update_method 
	, case when trim(both from email_verified)='' then null else  email_verified::text  end  email_verified 
	, case when trim(both from is_anonymous)='' then null else  is_anonymous::text  end  is_anonymous 
	, case when trim(both from accept_email)='' then null else  accept_email::text  end  accept_email 
	, case when trim(both from create_from_name)='' then null else  create_from_name::text  end  create_from_name 
	, case when trim(both from status)='' then null else  status::text  end  status 
,etl_tx_dt
from (
select 
	 (json_array_elements(data::json)::json->>'img')  img 
	, (json_array_elements(data::json)::json->>'notAcceptsms')  not_acceptsms 
	, (json_array_elements(data::json)::json->>'gender')  gender 
	, (json_array_elements(data::json)::json->>'createMethod')  create_method 
	, (json_array_elements(data::json)::json->>'mobileVerified')  mobile_verified 
	, (json_array_elements(data::json)::json->>'language')  language 
	, (json_array_elements(data::json)::json->>'isMember')  is_member 
	, (json_array_elements(data::json)::json->>'source')  source 
	, (json_array_elements(data::json)::json->>'isReferrer')  is_referrer 
	, (json_array_elements(data::json)::json->>'createFrom')  create_from 
	, (json_array_elements(data::json)::json->>'lastUpdated')  last_updated 
	, (json_array_elements(data::json)::json->>'dateCreated')  date_created 
	, (json_array_elements(data::json)::json->>'acceptsms')  acceptsms 
	, (json_array_elements(data::json)::json->>'id')  id 
	, (json_array_elements(data::json)::json->>'subscribeScene')  subscribe_scene 
	, (json_array_elements(data::json)::json->>'idStr')  id_str 
	, (json_array_elements(data::json)::json->>'isEmployee')  is_employee 
	, (json_array_elements(data::json)::json->>'version')  version 
	, (json_array_elements(data::json)::json->>'notAcceptEmail')  not_accept_email 
	, (json_array_elements(data::json)::json->>'dateJoin')  date_join 
	, (json_array_elements(data::json)::json->>'contentName')  content_name 
	, (json_array_elements(data::json)::json->>'updateMethod')  update_method 
	, (json_array_elements(data::json)::json->>'emailVerified')  email_verified 
	, (json_array_elements(data::json)::json->>'isAnonymous')  is_anonymous 
	, (json_array_elements(data::json)::json->>'acceptEmail')  accept_email 
	, (json_array_elements(data::json)::json->>'createFromName')  create_from_name 
	, (json_array_elements(data::json)::json->>'status')  status 
	,CURRENT_TIMESTAMP(0)  etl_tx_dt
from (select * from data_api.api_data 
WHERE api_id='cab1a761-b8e5-4db4-a769-88db3ec1' and is_loaded = '0' order by request_tm desc limit 1) p )p;

update  data_api.api_data 
set is_loaded = '1' ,
	status = '1',
	request_tm = current_timestamp(0)
where api_id='cab1a761-b8e5-4db4-a769-88db3ec1';
\q