Загрузка данных


CREATE OR REPLACE FUNCTION integration.fn_rabbitmq_sender(message text)
 RETURNS text
 LANGUAGE plpgsql
AS $function$
	declare
		p_pattern_id int = message::jsonb->>'pattern_id';
		p_ind_id int = message::jsonb->>'person_code';
		v_uuid uuid = gen_random_uuid();
		add_params jsonb = message::JSONB->'add_params';
		v_final_message text;
		v_delay int;
		v_log_id int;
	BEGIN
		raise notice '1 % % % %',p_pattern_id, p_ind_id, message::JSONB->'add_params'->>'reg_number', message::JSONB->'add_params'->>'appId';

		if p_pattern_id NOT IN (13, 14) then
	
			select jsonb_build_object(
					'type',x.type_code,
					'caption',x.title,
					'text',x.body,
					'link',x.link,
					'duration_days',x.duration_days,
					'person_code',(select code_1c from ourpension.individual where id = p_ind_id),
					'uuid',v_uuid)::text, 
				x.delay
			from integration.dict_360_to_lkk_notification_patterns x
			where x.id = p_pattern_id
			into v_final_message, v_delay;

		elsif p_pattern_id = 13 then
	
			select jsonb_build_object(
					'type',x.type_code,
					'caption',x.title,
			        'text', replace(x.body, '%',
			          COALESCE(
			            CASE
			              WHEN message::JSONB->'add_params'->>'reg_number' <> '' THEN message::JSONB->'add_params'->>'reg_number'
			              ELSE (SELECT number FROM integration.get_all_client_applications(p_ind_id) WHERE id = (message::JSONB->'add_params'->>'appId')::BIGINT)
			            END, '(номер заявления не указан)')
			        ),
					'link',x.link,
					'duration_days',x.duration_days,
					'person_code',(select code_1c from ourpension.individual where id = p_ind_id),
					'add_params', jsonb_build_object ('ukep_sign',add_params->>'ukep_sign'),
					'uuid',v_uuid)::text, 
				x.delay
			from integration.dict_360_to_lkk_notification_patterns x
			where x.id = p_pattern_id
			into v_final_message, v_delay;

		elsif p_pattern_id = 14 then
	
			SELECT jsonb_build_object(
				'type', x.type_code,
				'caption', x.title,
				'text', replace(x.body, 'contract_number', COALESCE(message::JSONB->>'contract_number', '(контракт не указан)')),
				'link', x.link,
				'duration_days', x.duration_days,
				'person_code', (SELECT code_1c FROM ourpension.individual WHERE id = p_ind_id LIMIT 1),
				'individual_code', p_ind_id,
				'uuid', v_uuid
			)::TEXT, 
			x.delay

			FROM integration.dict_360_to_lkk_notification_patterns x
			WHERE x.id = p_pattern_id
			into v_final_message, v_delay;						
			
		end if;

		raise notice '2 % %',v_final_message, v_delay;

		INSERT INTO integration.npf360_to_lkk_notification_logs (guid, body, delay)
		select v_uuid,v_final_message,v_delay
		returning id into v_log_id;
		
		if (select * from utils.send_message_rmq_pyt(v_final_message)) = 'DONE' then
			update integration.npf360_to_lkk_notification_logs 
			set sended = true
			where id = v_log_id;
			return 'DONE';
		else return 'ERR';
		end if;
	END;
$function$