﻿execute block (
    IN_ID_APP_EVENT D_BIGINT = :IN_ID_APP_EVENT)
returns (
    output_type D_SMALLINT)
as
declare variable CUR_ID_APP_EVENT type of column APP_EVENT.ID_APP_EVENT;
declare variable CUR_APP_EVENT_ID_SOTR type of column APP_EVENT.APP_EVENT_ID_SOTR;
declare variable CUR_APP_EVENT_ID_REC type of column APP_EVENT.APP_EVENT_ID_REC;
declare variable CUR_APP_EVENT_VID type of column APP_EVENT.APP_EVENT_VID;
declare variable CUR_APP_EVENT_DATA type of column APP_EVENT.APP_EVENT_DATA;

-- APP_NOTE_VARS
DECLARE VARIABLE NEW_ID_APP_NOTE TYPE OF D_BIGINT;
DECLARE VARIABLE NEW_APP_NOTE_ID_SOTR TYPE OF D_BIGINT;
DECLARE VARIABLE NEW_APP_NOTE_ID_APP_TASK TYPE OF D_BIGINT;
DECLARE VARIABLE NEW_APP_NOTE_STATUS TYPE OF D_SMALLINT;
DECLARE VARIABLE NEW_APP_NOTE_TIP TYPE OF D_SMALLINT;
DECLARE VARIABLE NEW_APP_NOTE_TEXT TYPE OF D_STR1024;
DECLARE VARIABLE NEW_APP_NOTE_DEL TYPE OF D_SMALLINT;
DECLARE VARIABLE NEW_APP_NOTE_CR TYPE OF D_DATETIME;
DECLARE VARIABLE NEW_APP_NOTE_WCR TYPE OF D_BIGINT;
DECLARE VARIABLE NEW_APP_NOTE_CH TYPE OF D_DATETIME;
DECLARE VARIABLE NEW_APP_NOTE_WCH TYPE OF D_BIGINT;


DECLARE VARIABLE CUR_ID_APP_NOTE TYPE OF D_BIGINT;
DECLARE VARIABLE CUR_APP_NOTE_ID_SOTR TYPE OF D_BIGINT;
DECLARE VARIABLE CUR_APP_NOTE_ID_APP_TASK TYPE OF D_BIGINT;
DECLARE VARIABLE CUR_APP_NOTE_STATUS TYPE OF D_SMALLINT;
DECLARE VARIABLE CUR_APP_NOTE_TIP TYPE OF D_SMALLINT;
DECLARE VARIABLE CUR_APP_NOTE_TEXT TYPE OF D_STR1024;
DECLARE VARIABLE CUR_APP_NOTE_DEL TYPE OF D_SMALLINT;
DECLARE VARIABLE CUR_APP_NOTE_CR TYPE OF D_DATETIME;
DECLARE VARIABLE CUR_APP_NOTE_WCR TYPE OF D_BIGINT;
DECLARE VARIABLE CUR_APP_NOTE_CH TYPE OF D_DATETIME;
DECLARE VARIABLE CUR_APP_NOTE_WCH TYPE OF D_BIGINT;

declare variable F_OPER type of D_SMALLINT;

begin
  output_type = 0;/* 0- Не выполнено, -1 Ошибка*/
  --   Находим событие с переданным идентификатором

  --0. ДЛЯ ПОЧТОВОГО СЕРВЕРА НИЧЕГО НЕ ДЕЛАЕМ
  if (exists(select 1
             from PARSRV A
             where A.PARSRV_VID = 1)) then
    exit;

  select first 1 E.ID_APP_EVENT, E.APP_EVENT_ID_SOTR, E.APP_EVENT_ID_REC, E.APP_EVENT_VID, E.APP_EVENT_DATA
  from APP_EVENT E
  where E.ID_APP_EVENT = :IN_ID_APP_EVENT
  into :CUR_ID_APP_EVENT, :CUR_APP_EVENT_ID_SOTR, :CUR_APP_EVENT_ID_REC, :CUR_APP_EVENT_VID, :CUR_APP_EVENT_DATA;

  if (:CUR_APP_EVENT_VID = 8795 /*Создание уведомления*/ ) then
    F_OPER := 1;
  if (exists(select 1
             from APP_EVENT E
             where E.ID_APP_EVENT = :CUR_ID_APP_EVENT)) then
  begin
    output = -5;/*Уведомление уже было создано*/
    suspend;
    exit;
    end

  else
  begin
    output = -2; /*Неизвестная операция*/
    suspend;
    exit;
  end

  if (:F_OPER = 1) then
  begin

    


    /*Для начала обновим ID_REC в таблице APP_EVENT*/



    insert into APP_NOTE (ID_APP_NOTE, APP_NOTE_ID_SOTR, APP_NOTE_ID_APP_TASK, APP_NOTE_STATUS, APP_NOTE_TIP,
                        APP_NOTE_TEXT, APP_NOTE_DEL, APP_NOTE_CR, APP_NOTE_WCR, APP_NOTE_CH, APP_NOTE_WCH)
                        values (
                        :cur_id_app_event, :cur_app_event_id_sotr,
                        )
  end

  /*TODO определить операцию и на ее основе делать FOPER */

)
  values ();

  suspend;
  exit;
end
