CREATE TABLE TBTIPO_VISITA (
    ID INTEGER NOT NULL,
    DESCRICAO VARCHAR(50),
    STATUSEXCLUSAO CHAR(1) DEFAULT '0',
    DATAHORA_INSERT TIMESTAMP DEFAULT 'NOW',
    DATAHORA_UPDATE TIMESTAMP,
    DATAHORA_DELETE TIMESTAMP
);

ALTER TABLE TBTIPO_VISITA
ADD CONSTRAINT PK_TBTIPO_VISITA
PRIMARY KEY (ID);


CREATE SEQUENCE GTBTIPO_VISITA;


CREATE INDEX ITBTIPO_VISITA_01
ON TBTIPO_VISITA (STATUSEXCLUSAO);



CREATE OR ALTER TRIGGER TTBTIPO_VISITA_B0I FOR TBTIPO_VISITA
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL OR NEW.ID = 0) THEN
      NEW.ID = GEN_ID(GTBTIPO_VISITA, 1);
END;

COMMENT ON TRIGGER TTBTIPO_VISITA_B0I IS 'Gerar ID automaticamente.';


CREATE TRIGGER TTBTIPO_VISITA_B0U FOR TBTIPO_VISITA
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. 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 (
            'TBTIPO_VISITA',
            NEW.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;

COMMENT ON TRIGGER TTBTIPO_VISITA_B0U IS 'Atualiza os campos DATAHORA update e delete e registra historico de exclusao/recuperacao.';


CREATE TRIGGER TTBTIPO_VISITA_B1U FOR TBTIPO_VISITA
ACTIVE BEFORE UPDATE POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

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

    IF (HISTORICO <> '') THEN
    BEGIN
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
       'TBTIPO_VISITA', OLD.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;

COMMENT ON TRIGGER TTBTIPO_VISITA_B1U IS 'Registra historico de alteracoes.';