drop procedure if exists sp_updateticketinfo;
-- =============================================
-- Author:		Marat Zigangarayev
-- Create date: 2023/05/31
-- Description:	установка бита "Явка по посещению" в атрибуте TICKET.TicketInfo
-- =============================================
create procedure sp_updateticketinfo("AmbulanceEvent" uuid, "Post" uuid, "VisitDate" timestamp without time zone, "Action" integer)
    language plpgsql
as
$$
Begin
	if "Action" = 2 then -- DELETE VISIT
		update akuz.t_ticket t
		set TicketInfo = (t.TicketInfo & (~4))
		from akuz.t_entry_base eb
		where t.EntryBase = eb.EntryBaseID
			and eb.AmbulanceEvent = "AmbulanceEvent"
			and eb.Resource = "Post"
			and eb.EntryDate = "VisitDate";
	else
		update akuz.t_ticket t
		set TicketInfo = (t.TicketInfo | 4)
		from akuz.t_entry_base eb
		where t.EntryBase = eb.EntryBaseID
			and eb.AmbulanceEvent = "AmbulanceEvent"
			and eb.Resource = "Post"
			and eb.EntryDate = "VisitDate";
	end if;

End;
$$;

--alter procedure sp_updateticketinfo(uuid, uuid, timestamp, integer) owner to postgres;

