-- INSERT
CREATE OR ALTER trigger tbavaliacao_des_fator_b02i for tbavaliacao_des_fator
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.id > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Reg. incluido';
    END

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

-- INSERT ITENS
CREATE OR ALTER trigger tbavaliacao_des_fator_nivel_b02 for tbavaliacao_des_fator_nivel
active before insert position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'Reg. Nível: '||NEW.ID||' INCLUIDO.';
    END

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

-- UPDATE NÍVEL
CREATE OR ALTER trigger ttbavaliacao_des_fator_nvl_b1u for tbavaliacao_des_fator_nivel
active before update position 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF (OLD.AVALIACAO_DES_FATOR_ID <> NEW.AVALIACAO_DES_FATOR_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'REG. '||OLD.ID|| ' -'||' FATOR:'||OLD.AVALIACAO_DES_FATOR_ID||'->'||NEW.AVALIACAO_DES_FATOR_ID;
    END

    IF (OLD.AVALIACAO_DES_NIVEL_ID <> NEW.AVALIACAO_DES_NIVEL_ID) THEN
    BEGIN
        HISTORICO = HISTORICO||'REG. '||OLD.ID|| ' -'||' NIVEL:'||OLD.AVALIACAO_DES_NIVEL_ID||'->'||NEW.AVALIACAO_DES_NIVEL_ID;
    END

    IF (OLD.FAIXA_INICIAL <> NEW.FAIXA_INICIAL) THEN
    BEGIN
        HISTORICO = HISTORICO||'REG. '||OLD.ID|| ' -'||' FX INI:'||OLD.FAIXA_INICIAL||'->'||NEW.FAIXA_INICIAL;
    END

    IF (OLD.FAIXA_FINAL <> NEW.FAIXA_FINAL) THEN
    BEGIN
        HISTORICO = HISTORICO||'REG. '||OLD.ID|| ' -'||' FX FIN:'||OLD.FAIXA_FINAL||'->'||NEW.FAIXA_FINAL;
    END

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

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBAVALIACAO_DES_FATOR', OLD.avaliacao_des_fator_id,
      (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
      (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
       :HISTORICO);
    END
END;

-- UPDATE STATUSEXCLUSAO 
CREATE OR ALTER trigger ttbavaliacao_des_fator_nvl_b0u for tbavaliacao_des_fator_nivel
active before update position 0
AS
/**************************************************
   ATUALIZA OS CAMPOS DATAHORA UPDATE E DELETE E
   REGISTRA HISTORICO DE EXCLUSAO/RECUPERACAO
**************************************************/
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    /* UPDATE */
    IF ( NEW.STATUSEXCLUSAO = '0' AND OLD.STATUSEXCLUSAO = '0' ) THEN
    BEGIN
        NEW.DATAHORA_UPDATE = CURRENT_TIMESTAMP;
    END
    /* DELETE */
    ELSE IF ( NEW.STATUSEXCLUSAO = '1' AND OLD.STATUSEXCLUSAO = '0' ) THEN
    BEGIN
        NEW.DATAHORA_DELETE = CURRENT_TIMESTAMP;
        HISTORICO = 'REG. '||OLD.ID|| ' EXCLUIDO';
    END
    /* RECUPERAR */
    ELSE IF ( NEW.STATUSEXCLUSAO = '0' AND OLD.STATUSEXCLUSAO = '1' ) THEN
    BEGIN
        NEW.DATAHORA_UPDATE = CURRENT_TIMESTAMP;
        HISTORICO = 'REG. RECUPERADO.';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        INSERT INTO TBHISTORICO (
            TABELA,
            TABELA_ID,
            USUARIO,
            IP,
            HISTORICO
        ) VALUES (
            'TBAVALIACAO_DES_FATOR',
            OLD.avaliacao_des_fator_id,
            (SELECT FIRST 1 MON$USER FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
            (SELECT FIRST 1 MON$REMOTE_ADDRESS FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID = CURRENT_CONNECTION),
            :HISTORICO
        );
    END
END;