CREATE trigger ttbacesso_ip_b02i for tbacesso_ip
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('TBACESSO_IP',NEW.ID, HISTORICO);
    END
END;


CREATE trigger ttbacesso_ip_b01d for tbacesso_ip
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('TBACESSO_IP',OLD.ID, HISTORICO);
    END
END;


CREATE trigger ttbacesso_ip_b02d for tbacesso_ip
active before delete position 1
AS
BEGIN
   IF(OLD.ID > 0) THEN
   BEGIN
      DELETE FROM TBACESSO_IP_USUARIO WHERE ACESSO_IP_ID = OLD.ID;
   END
END;


CREATE trigger ttbacesso_ip_b01u for tbacesso_ip
active before update position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN
    HISTORICO = '';

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

    IF ( NEW.STATUS IS DISTINCT FROM OLD.STATUS) THEN
    BEGIN
        HISTORICO = HISTORICO||' OBS:'||COALESCE(OLD.STATUS, 0)||'->'||COALESCE(NEW.STATUS, 0);
    END

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

    IF ( NEW.HORA_INICIO <> OLD.HORA_INICIO) THEN
    BEGIN
        HISTORICO = HISTORICO||' HR.INI:'||OLD.HORA_INICIO||'->'||NEW.HORA_INICIO;
    END

    IF ( NEW.HORA_FIM <> OLD.HORA_FIM) THEN
    BEGIN
        HISTORICO = HISTORICO||' HR.FIM:'||OLD.HORA_FIM||'->'||NEW.HORA_FIM;
    END

    IF ( NEW.BLOQUEAR_HORA IS DISTINCT FROM OLD.BLOQUEAR_HORA) THEN
    BEGIN
        HISTORICO = HISTORICO||' BLOQ.HR:'||COALESCE(OLD.BLOQUEAR_HORA, 0)||'->'||COALESCE(NEW.BLOQUEAR_HORA, 0);
    END

    IF ( NEW.BLOQUEAR IS DISTINCT FROM OLD.BLOQUEAR) THEN
    BEGIN
        HISTORICO = HISTORICO||' BLOQ.ACESSO:'||COALESCE(OLD.BLOQUEAR, 0)||'->'||COALESCE(NEW.BLOQUEAR, 0);
    END

    IF ( NEW.SEG IS DISTINCT FROM OLD.SEG) THEN
    BEGIN
        HISTORICO = HISTORICO||' SEG:'||COALESCE(OLD.SEG, 0)||'->'||COALESCE(NEW.SEG, 0);
    END

    IF ( NEW.TER IS DISTINCT FROM OLD.TER) THEN
    BEGIN
        HISTORICO = HISTORICO||' TER:'||COALESCE(OLD.TER, 0)||'->'||COALESCE(NEW.TER, 0);
    END

    IF ( NEW.QUA IS DISTINCT FROM OLD.QUA) THEN
    BEGIN
        HISTORICO = HISTORICO||' QUA:'||COALESCE(OLD.QUA, 0)||'->'||COALESCE(NEW.QUA, 0);
    END

    IF ( NEW.QUI IS DISTINCT FROM OLD.QUI) THEN
    BEGIN
        HISTORICO = HISTORICO||' QUI:'||COALESCE(OLD.QUI, 0)||'->'||COALESCE(NEW.QUI, 0);
    END

    IF ( NEW.SEX IS DISTINCT FROM OLD.SEX) THEN
    BEGIN
        HISTORICO = HISTORICO||' SEG:'||COALESCE(OLD.SEX, 0)||'->'||COALESCE(NEW.SEX, 0);
    END

    IF ( NEW.SAB IS DISTINCT FROM OLD.SAB) THEN
    BEGIN
        HISTORICO = HISTORICO||' SAB:'||COALESCE(OLD.SAB, 0)||'->'||COALESCE(NEW.SAB, 0);
    END

    IF ( NEW.DOM IS DISTINCT FROM OLD.DOM) THEN
    BEGIN
        HISTORICO = HISTORICO||' SEG:'||COALESCE(OLD.DOM, 0)||'->'||COALESCE(NEW.DOM, 0);
    END

    IF ( NEW.TODOS IS DISTINCT FROM OLD.TODOS) THEN
    BEGIN
        HISTORICO = HISTORICO||' TODOS:'||COALESCE(OLD.TODOS, 0)||'->'||COALESCE(NEW.TODOS, 0);
    END


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

