ALTER TABLE TBCASO_PAINEL
    ADD UPDATE_TEG VARCHAR(50) CHARACTER SET UTF8 COLLATE UTF8;

/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/

CREATE GENERATOR GTBCASO_WEB_SERV;

CREATE TABLE TBCASO_WEB_SERV (
    ID           INTEGER NOT NULL,
    PAINEL_ID    INTEGER,
    CODIGO       INTEGER,
    SERVIDOR_ID  INTEGER,
    REMOVER      INTEGER
);




/******************************************************************************/
/****                             Primary keys                             ****/
/******************************************************************************/

ALTER TABLE TBCASO_WEB_SERV ADD CONSTRAINT PK_TBCASO_WEB_SERV PRIMARY KEY (ID);


/******************************************************************************/
/****                               Indices                                ****/
/******************************************************************************/

CREATE INDEX ITBCASO_WEB_SERV_01 ON TBCASO_WEB_SERV (PAINEL_ID);
CREATE INDEX ITBCASO_WEB_SERV_02 ON TBCASO_WEB_SERV (CODIGO);
CREATE INDEX ITBCASO_WEB_SERV_03 ON TBCASO_WEB_SERV (SERVIDOR_ID);
CREATE INDEX ITBCASO_WEB_SERV_04 ON TBCASO_WEB_SERV (REMOVER);


/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/



SET TERM ^ ;



/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: TTBCASO_WEB_SERV_B01D */
CREATE OR ALTER TRIGGER TTBCASO_WEB_SERV_B01D FOR TBCASO_WEB_SERV
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE PAINEL VARCHAR(500);
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    PAINEL = COALESCE((SELECT FIRST 1 R.DESCRICAO FROM TBCASO_PAINEL R WHERE R.ID = OLD.PAINEL_ID),'');

    IF (OLD.SERVIDOR_ID > 0 AND OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' PAINEL DE CASOS EXCLUIDO: '||OLD.PAINEL_ID||' - '||PAINEL;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBSERVIDOR', OLD.SERVIDOR_ID, HISTORICO);
    END
END
^


/* Trigger: TTBCASO_WEB_SERV_B01I */
CREATE OR ALTER TRIGGER TTBCASO_WEB_SERV_B01I FOR TBCASO_WEB_SERV
ACTIVE BEFORE INSERT POSITION 0
as
begin
    if (new.id is null) then
        new.id = gen_id(gtbcaso_web_serv, 1);
end
^


/* Trigger: TTBCASO_WEB_SERV_B02I */
CREATE OR ALTER TRIGGER TTBCASO_WEB_SERV_B02I FOR TBCASO_WEB_SERV
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE PAINEL VARCHAR(500);
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    PAINEL = COALESCE((SELECT FIRST 1 R.DESCRICAO FROM TBCASO_PAINEL R WHERE R.ID = NEW.PAINEL_ID),'');

    IF (NEW.SERVIDOR_ID > 0 AND NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' PAINEL DE CASOS INCLUIDO: '||NEW.PAINEL_ID||' - '||PAINEL;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBSERVIDOR', NEW.SERVIDOR_ID, HISTORICO);
    END
END
^

SET TERM ; ^

/*****************************************************************************************************/
/*****************************************************************************************************/
/*****************************************************************************************************/

ALTER SEQUENCE GTBCASO_PAINEL RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_SQL RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_VISUALIZACAO_CONF RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_PARAMETROS_USUARIO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_STATUS RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_MOTIVO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_TIPO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_ORIGEM RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_CAMPOS_AGRUPAMENTO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_PAINEL_CONF RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_CAMPO_CONSULTA RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_VISUALIZACAO_CAMPO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_PAINEL_CONF_ITENS RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_CAMPO_DEPENDENCIA RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_VALIDACAO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_WORKFLOWS RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBWORKFLOW RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBWORKFLOW_TAREFA RESTART WITH 99990 INCREMENT BY 1 ;
ALTER SEQUENCE GTBWORKFLOW_CAMPO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBWORKFLOW_CAMPO_SQL RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBWORKFLOW_CAMPO_ENCERRAMENTO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBWORKFLOW_DEPENDENCIA RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_ACAO_ENCERRAMENTO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_PAINEL_PROCEDIMENTO RESTART WITH 99990 INCREMENT BY 1;
ALTER SEQUENCE GTBCASO_WEB_SERV RESTART WITH 99990 INCREMENT BY 1;