CREATE trigger ttbpower_bi_painel_b02i for tbpower_bi_painel
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPOWER_BI_PAINEL',NEW.ID, HISTORICO);
    END
END;


CREATE OR ALTER trigger ttbpower_bi_painel_b01u for tbpower_bi_painel
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( NEW.DESCRICAO <> OLD.DESCRICAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DESC:'||OLD.DESCRICAO||'->'||NEW.DESCRICAO;
    END

    IF ( NEW.URL <> OLD.URL) THEN
    BEGIN
        HISTORICO = HISTORICO||' URL ALTERADA';
    END
    
    IF ( NEW.STATUS <> OLD.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||' STATUS:'||OLD.STATUS||'->'||NEW.STATUS;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' REGISTRO ALTERADO: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPOWER_BI_PAINEL',NEW.ID, HISTORICO);
    END
END;


CREATE trigger ttbpower_bi_painel_b01d for tbpower_bi_painel
active before delete position 0
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBPOWER_BI_PAINEL_USUARIO WHERE POWER_BI_PAINEL_ID = OLD.ID;
   END
END;


CREATE trigger ttbpower_bi_painel_b02d for tbpower_bi_painel
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||' REGISTRO EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPOWER_BI_PAINEL',OLD.ID, HISTORICO);
    END
END;






