CREATE OR ALTER trigger ttbpower_bi_painel_usuario_b02b for tbpower_bi_painel_usuario
active before insert position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'USUARIO INCLUIDO:'||NEW.USUARIO_ID||' - '||COALESCE((SELECT FIRST 1 IIF( CHAR_LENGTH(COALESCE(A.NOME,'')) > 0, A.NOME, A.USUARIO) AS DESCRICAO FROM TBUSUARIO A WHERE A.CODIGO = NEW.USUARIO_ID), '');
    END

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


CREATE trigger ttbpower_bi_painel_usuario_b01d for tbpower_bi_painel_usuario
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'USUARIO EXCLUIDO:'||OLD.USUARIO_ID||' - '||COALESCE((SELECT FIRST 1 IIF( CHAR_LENGTH(COALESCE(A.NOME,'')) > 0, A.NOME, A.USUARIO) AS DESCRICAO FROM TBUSUARIO A WHERE A.CODIGO = OLD.USUARIO_ID), '');
    END

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

