-- =============================================
-- Author:		Marat Zigangaraev
-- Create date: 2011/06/28
-- Description:	Обработка ENTRY_LOG
-- =============================================
drop procedure if exists sp_processentrylog;
CREATE procedure sp_processentrylog
(
	"Action" integer	-- 1-insert, 2-update, 3-delete
,	"EntryID" uuid
,	"OwnerID" uuid DEFAULT NULL::uuid
,	"ResourceID" uuid DEFAULT NULL::integer
,	"WorkDate" timestamp DEFAULT NULL::timestamp
,	"EntryKind" integer DEFAULT NULL::integer
,	"channel" integer DEFAULT NULL::integer
,	"PayType" integer DEFAULT NULL::integer
)
AS
$BODY$
Begin
	if "Action" = 1
	then
		INSERT INTO akuz.T_ENTRY_LOG(EntryLogID, WorkDate, OwnerID, Resource, EntryKind, Channel, PayType)
		Values ("EntryID", "WorkDate", "OwnerID", "ResourceID", "EntryKind", "channel", "PayType");
	elsif "Action" = 2
	then
		if "OwnerID" is not null
		then
			-- смена идентификатора
			Update akuz.T_ENTRY_LOG 
			Set EntryLogID = "OwnerID"
			Where EntryLogID = "EntryID";
		else
			-- перезапись
			Update AKUZ.T_ENTRY_LOG 
			Set WorkDate = "WorkDate"
			,	Resource = "ResourceID"
			Where EntryLogID = "EntryID";
		end if;
	elsif "Action" = 3
	then
		Delete From Akuz.T_ENTRY_LOG Where EntryLogID = "EntryID";
	end if;
	return;
End;
$BODY$
  LANGUAGE plpgsql;