﻿execute block (
    IN_ID_APP_EVENT D_BIGINT = :IN_ID_APP_EVENT)
returns (
    output D_SMALLINT)
as

-- APP_EVENT VARIABLES
declare variable F_ID_APP_EVENT type of column APP_EVENT.ID_APP_EVENT;
declare variable F_APP_EVENT_ID_SOTR type of column APP_EVENT.APP_EVENT_ID_SOTR;
declare variable F_APP_EVENT_ID_REC type of column APP_EVENT.APP_EVENT_ID_REC;
declare variable F_APP_EVENT_VID type of column APP_EVENT.APP_EVENT_VID;
declare variable F_APP_EVENT_TIP type of column APP_EVENT.APP_EVENT_TIP;
declare variable F_APP_EVENT_DT type of column APP_EVENT.APP_EVENT_DT;
declare variable F_APP_EVENT_TEXT type of column APP_EVENT.APP_EVENT_TEXT;
declare variable F_APP_EVENT_PARAM type of column APP_EVENT.APP_EVENT_PARAM;
declare variable F_APP_EVENT_DATA type of column APP_EVENT.APP_EVENT_DATA;
declare variable F_APP_EVENT_DEL type of column APP_EVENT.APP_EVENT_DEL;
declare variable F_APP_EVENT_IMP type of column APP_EVENT.APP_EVENT_IMP;
declare variable F_APP_EVENT_CR type of column APP_EVENT.APP_EVENT_CR;
declare variable F_APP_EVENT_WCR type of column APP_EVENT.APP_EVENT_WCR;
declare variable F_APP_EVENT_CH type of column APP_EVENT.APP_EVENT_CH;
declare variable F_APP_EVENT_WCH type of column APP_EVENT.APP_EVENT_WCH;
declare variable F_APP_EVENT_SEANS type of column APP_EVENT.APP_EVENT_SEANS;
declare variable F_APP_EVENT_OWNERMST type of column APP_EVENT.APP_EVENT_OWNERMST;
declare variable F_APP_EVENT_FIMP type of column APP_EVENT.APP_EVENT_FIMP;
declare variable F_APP_EVENT_MPOST type of column APP_EVENT.APP_EVENT_MPOST;

-- APP_NOTE VARIABLES
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;

-- Execute blocks
declare variable EXEC_BLOCK_TEXT_32_NEG_1 type of column EXEC_BLOCKS.EXEC_BLOCKS_TEXT;
declare variable EXEC_BLOCK_32_NEG_1_OUT_KEY type of D_STR1024;
declare variable EXEC_BLOCK_32_NEG_1_OUT_VAL type of D_STR1024;

begin
  output = 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_TIP,
                 E.APP_EVENT_DT, E.APP_EVENT_TEXT, E.APP_EVENT_PARAM, E.APP_EVENT_DATA, E.APP_EVENT_DEL,
                 E.APP_EVENT_IMP, E.APP_EVENT_CR, E.APP_EVENT_WCR, E.APP_EVENT_CH, E.APP_EVENT_WCH, E.APP_EVENT_SEANS,
                 E.APP_EVENT_OWNERMST, E.APP_EVENT_FIMP, E.APP_EVENT_MPOST
  from APP_EVENT E
  where E.ID_APP_EVENT = :IN_ID_APP_EVENT
  into :F_ID_APP_EVENT, :F_APP_EVENT_ID_SOTR, :F_APP_EVENT_ID_REC, :F_APP_EVENT_VID, :F_APP_EVENT_TIP, :F_APP_EVENT_DT,
       :F_APP_EVENT_TEXT, :F_APP_EVENT_PARAM, :F_APP_EVENT_DATA, :F_APP_EVENT_DEL, :F_APP_EVENT_IMP, :F_APP_EVENT_CR,
       :F_APP_EVENT_WCR, :F_APP_EVENT_CH, :F_APP_EVENT_WCH, :F_APP_EVENT_SEANS, :F_APP_EVENT_OWNERMST,
       :F_APP_EVENT_FIMP, :F_APP_EVENT_MPOST;

  -- Получаем текст EXEC 32 -2
  select E.EXEC_BLOCKS_TEXT
  from EXEC_BLOCKS E
  where E.EXEC_BLOCKS_VID = 32 and
        E.EXEC_BLOCKS_TIP = -2 and
        E.EXEC_BLOCKS_DEL = 0 rows 1 to 1
  into :EXEC_BLOCK_TEXT_32_NEG_1;
  if (:EXEC_BLOCK_TEXT_32_NEG_1 is null) then
  begin

    output = -1;/*Не найден EXEC BLOCK 32 -2 */
    suspend;
    exit;
  end

  if (:F_APP_EVENT_VID = 8795 /*Создание уведомления*/ ) then
    F_OPER = 1;
  else
  if (:F_APP_EVENT_VID = 8797) /*Изменение уведомения*/ then
    F_OPER = 2;
  else
  begin
    output = -2;/*Неизвестная операция*/
    suspend;
    exit;
  end

  if (:F_APP_EVENT_ID_REC is null or :F_APP_EVENT_ID_REC = 0) then
  begin

    :F_APP_EVENT_ID_REC = :F_ID_APP_EVENT;
    update APP_EVENT
    set APP_EVENT_ID_REC = :F_APP_EVENT_ID_REC
    where ID_APP_EVENT = :F_ID_APP_EVENT;

  end

  /*Получаем все события от начала до конца*/

  for select E.ID_APP_EVENT, E.APP_EVENT_ID_SOTR, E.APP_EVENT_ID_REC, E.APP_EVENT_VID, E.APP_EVENT_TIP, E.APP_EVENT_DT,
             E.APP_EVENT_TEXT, E.APP_EVENT_PARAM, E.APP_EVENT_DATA, E.APP_EVENT_DEL, E.APP_EVENT_IMP, E.APP_EVENT_CR,
             E.APP_EVENT_WCR, E.APP_EVENT_CH, E.APP_EVENT_WCH, E.APP_EVENT_SEANS, E.APP_EVENT_OWNERMST,
             E.APP_EVENT_FIMP, E.APP_EVENT_MPOST
      from APP_EVENT E
      where E.APP_EVENT_ID_REC = :F_APP_EVENT_ID_REC and
            E.APP_EVENT_DEL = 0
      order by E.APP_EVENT_DT
      into :F_ID_APP_EVENT, :F_APP_EVENT_ID_SOTR, :F_APP_EVENT_ID_REC, :F_APP_EVENT_VID, :F_APP_EVENT_TIP,
           :F_APP_EVENT_DT, :F_APP_EVENT_TEXT, :F_APP_EVENT_PARAM, :F_APP_EVENT_DATA, :F_APP_EVENT_DEL,
           :F_APP_EVENT_IMP, :F_APP_EVENT_CR, :F_APP_EVENT_WCR, :F_APP_EVENT_CH, :F_APP_EVENT_WCH, :F_APP_EVENT_SEANS,
           :F_APP_EVENT_OWNERMST, :F_APP_EVENT_FIMP, :F_APP_EVENT_MPOST
  do
  begin

    if (:F_APP_EVENT_VID = 8795) then
    begin
      NEW_ID_APP_NOTE = :F_APP_EVENT_ID_REC;
      NEW_APP_NOTE_ID_SOTR = :F_APP_EVENT_ID_SOTR;
      NEW_APP_NOTE_ID_APP_TASK = 0;
      NEW_APP_NOTE_STATUS = 0;
      NEW_APP_NOTE_TIP = F_APP_EVENT_TIP;
      NEW_APP_NOTE_TEXT = F_APP_EVENT_TEXT;
      NEW_APP_NOTE_DEL = F_APP_EVENT_DEL;
      NEW_APP_NOTE_CR = F_APP_EVENT_CR;
      NEW_APP_NOTE_WCR = F_APP_EVENT_WCR;
      NEW_APP_NOTE_CH = F_APP_EVENT_CH;
      NEW_APP_NOTE_WCH = F_APP_EVENT_WCH;

      /*Парсим параметры JSON-a APP_EVENT_DATA*/
      for execute statement(:EXEC_BLOCK_TEXT_32_NEG_1)(IN_PARAM_JSON := :F_APP_EVENT_DATA)
              into :EXEC_BLOCK_32_NEG_1_OUT_VAL, :EXEC_BLOCK_32_NEG_1_OUT_KEY
      do
      begin
        if (:EXEC_BLOCK_32_NEG_1_OUT_KEY = '8794') then
          NEW_APP_NOTE_STATUS = cast(:EXEC_BLOCK_32_NEG_1_OUT_VAL as D_INTEGER);
        else
        if (:EXEC_BLOCK_32_NEG_1_OUT_KEY = 'ID_APP_TASK') then
        begin
          if (exists(select 1
                     from APP_TASK T
                     where T.ID_APP_TASK = cast(:EXEC_BLOCK_32_NEG_1_OUT_VAL as D_BIGINT))) then
            NEW_APP_NOTE_ID_APP_TASK = cast(:EXEC_BLOCK_32_NEG_1_OUT_VAL as D_BIGINT);
          else
          begin
            output = -3;/*Не найден APP_TASK*/
            suspend;
            exit;
          end

        end

      end

    end
    else
    if (:F_APP_EVENT_VID = 8797) then
    begin
      /*Парсим параметры JSON-a APP_EVENT_DATA*/
      for execute statement(:EXEC_BLOCK_TEXT_32_NEG_1)(IN_PARAM_JSON := :F_APP_EVENT_DATA)
              into :EXEC_BLOCK_32_NEG_1_OUT_VAL, :EXEC_BLOCK_32_NEG_1_OUT_KEY
      do
      begin
        if (:EXEC_BLOCK_32_NEG_1_OUT_KEY = '8794') then
        begin
          NEW_APP_NOTE_STATUS = cast(:EXEC_BLOCK_32_NEG_1_OUT_VAL as D_INTEGER);
        end
      end
    end
  end

  if (NEW_ID_APP_NOTE is null) then
  begin
    output = -5;/*Не было создано событие о создании Уведомсления (8795) */
    suspend;
    exit;
  end

  update or 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 (:NEW_ID_APP_NOTE, :NEW_APP_NOTE_ID_SOTR, :NEW_APP_NOTE_ID_APP_TASK, :NEW_APP_NOTE_STATUS, :NEW_APP_NOTE_TIP,
          :NEW_APP_NOTE_TEXT, :NEW_APP_NOTE_DEL, :NEW_APP_NOTE_CR, :NEW_APP_NOTE_WCR, :NEW_APP_NOTE_CH,
          :NEW_APP_NOTE_WCH)
  matching (ID_APP_NOTE);

  suspend;
end