CREATE OR REPLACE TRIGGER LOG_GER_ESTADO
AFTER DELETE OR INSERT OR UPDATE
ON ESTADO FOR EACH ROW
DECLARE
  V_USUARIO      VARCHAR2(40);
  V_TIPO         VARCHAR2(1);
  V_ITPESQ       VARCHAR2(40);
  V_DESC         VARCHAR2(300);
  V_CDFILLOGOPER VARCHAR2(4);
  V_DTOPER       DATE;
  V_DSREMOTEADDR VARCHAR2(15);
  V_DSREMOTEHOST VARCHAR2(15);
  V_DSSERVERRDM  VARCHAR2(20);
  V_DSSERVMETHOD VARCHAR2(30);
  V_DSREMOTEOPER VARCHAR2(12);
BEGIN
   V_TIPO         := '0';
   V_DESC         := '';
   V_CDFILLOGOPER := '';
   IF DELETING THEN
      IF :OLD.CDPAIS IS NOT NULL THEN
         V_DESC := V_DESC || 'Pais=' || :OLD.CDPAIS;
      END IF;
      IF :OLD.SGESTADO IS NOT NULL THEN
         V_DESC := V_DESC || ' SiglaEst=' || :OLD.SGESTADO;
      END IF;
      IF :OLD.NMESTADO IS NOT NULL THEN
         V_DESC := V_DESC || ' NomeEstado=' || :OLD.NMESTADO;
      END IF;
      IF :OLD.CDESTAEXPO IS NOT NULL THEN
         V_DESC := V_DESC || ' CodExport=' || :OLD.CDESTAEXPO;
      END IF;
      IF :OLD.CDESTADOIBGE IS NOT NULL THEN
         V_DESC := V_DESC || ' CodIBGE=' || :OLD.CDESTADOIBGE;
      END IF;
      IF :OLD.IDUTHORVER IS NOT NULL THEN
         V_DESC := V_DESC || ' HorVerao=' || :OLD.IDUTHORVER;
      END IF;
      IF :OLD.HRDIFUTCFUSO IS NOT NULL THEN
         V_DESC := V_DESC || ' FusoHor=' || :OLD.HRDIFUTCFUSO;
      END IF;
      IF :OLD.VRPERVALOPER IS NOT NULL THEN
         V_DESC := V_DESC || ' PercCompValOper(DFS47000)=' || :OLD.VRPERVALOPER;
      END IF;
      V_TIPO         := '3';
      V_ITPESQ       := :OLD.CDPAIS||:OLD.SGESTADO;
   ELSE
      IF UPDATING THEN
         IF (NVL(:OLD.CDPAIS, '')       <> NVL(:NEW.CDPAIS, ''))       OR
            (NVL(:OLD.SGESTADO, '')     <> NVL(:NEW.SGESTADO, ''))     OR
            (NVL(:OLD.NMESTADO, '')     <> NVL(:NEW.NMESTADO, ''))     OR
            (NVL(:OLD.CDESTAEXPO, '')   <> NVL(:NEW.CDESTAEXPO, ''))   OR
            (NVL(:OLD.CDESTADOIBGE, '') <> NVL(:NEW.CDESTADOIBGE, '')) OR
            (NVL(:OLD.IDUTHORVER, '')   <> NVL(:NEW.IDUTHORVER, ''))   OR
            (NVL(:OLD.HRDIFUTCFUSO, '') <> NVL(:NEW.HRDIFUTCFUSO, '')) OR
            (NVL(:OLD.VRPERVALOPER, 0)   <> NVL(:NEW.VRPERVALOPER, 0))   THEN
            V_TIPO   := '2';
            V_ITPESQ := :OLD.CDPAIS||:OLD.SGESTADO;
         END IF;
      ELSE
         V_TIPO   := '1';
         V_ITPESQ := :NEW.CDPAIS||:NEW.SGESTADO;
      END IF;
      IF NVL(:OLD.CDPAIS, '') <> NVL(:NEW.CDPAIS, '') THEN
         V_DESC := V_DESC || 'Pais=' || :NEW.CDPAIS;
      END IF;
      IF NVL(:OLD.SGESTADO, '') <> NVL(:NEW.SGESTADO, '') THEN
         V_DESC := V_DESC || ' SiglaEst=' || :NEW.SGESTADO;
      END IF;
      IF NVL(:OLD.NMESTADO, '') <> NVL(:NEW.NMESTADO, '') THEN
         V_DESC := V_DESC || ' NomeEstado=' || :NEW.NMESTADO;
      END IF;
      IF NVL(:OLD.CDESTAEXPO, '') <> NVL(:NEW.CDESTAEXPO, '') THEN
         V_DESC := V_DESC || ' CodExport=' || :NEW.CDESTAEXPO;
      END IF;
      IF NVL(:OLD.CDESTADOIBGE, '') <> NVL(:NEW.CDESTADOIBGE, '') THEN
         V_DESC := V_DESC || ' CodIBGE=' || :NEW.CDESTADOIBGE;
      END IF;
      IF NVL(:OLD.IDUTHORVER, '') <> NVL(:NEW.IDUTHORVER, '') THEN
         V_DESC := V_DESC || ' HorVerao=' || :NEW.IDUTHORVER;
      END IF;
      IF NVL(:OLD.HRDIFUTCFUSO, '') <> NVL(:NEW.HRDIFUTCFUSO, '') THEN
         V_DESC := V_DESC || ' FusoHor=' || :NEW.HRDIFUTCFUSO;
      END IF;
      IF NVL(:OLD.VRPERVALOPER, 0) <> NVL(:NEW.VRPERVALOPER, 0) THEN
         V_DESC := V_DESC || ' PercCompValOper(DFS47000)=' || :NEW.VRPERVALOPER;
      END IF;
   END IF;
   IF V_TIPO <> '0' THEN
      NMUSUARIO(V_USUARIO);
      WEBTUNNING(V_DSREMOTEADDR,
                 V_DSREMOTEHOST,
                 V_DSSERVERRDM,
                 V_DSSERVMETHOD,
                 V_DSREMOTEOPER);
      IF NOT V_DESC IS NULL THEN
         IF NOT V_USUARIO IS NULL THEN
            V_DTOPER := SYSDATE;
            INSERT INTO LOGOPERA
               (NMTABELA, DTOPERACAO, NMRESPOPER, IDTIPOOPER, CDPESQITEM, 
                DSOPERACAO, CDFILLOGOPER, DSRDMUSUOPER, DSENDUSUOPER, DSNMUSUOPER, 
                DSMETUSUOPER, CDORESPOPER)
            VALUES
               ('ESTADO', V_DTOPER, V_USUARIO, V_TIPO, V_ITPESQ, 
                V_DESC, V_CDFILLOGOPER, V_DSSERVERRDM, V_DSREMOTEADDR, V_DSREMOTEHOST, 
                V_DSSERVMETHOD, V_DSREMOTEOPER);
         END IF;
      END IF;
   END IF;
END;
/