/* Date: 03/16/2015
   By: Veena Veeravalli
   This SQL will import notes for the primary contact of each purge eligible member  into EIM_CON_DTL table */

DECLARE
   TYPE T_PID IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.PERSON_UID%TYPE;

   TYPE T_RM IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.PURGE_TXN_COUNT%TYPE;

   TYPE T_AS IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.ACRL_SUM%TYPE;

   TYPE T_RS IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.RDM_SUM%TYPE;

   TYPE T_MN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.MEM_NUM%TYPE;

   ARR_PID              T_PID;
   ARR_RM               T_RM;
   ARR_AS               T_AS;
   ARR_RS               T_RS;
   ARR_MN               T_MN;

   V_ROWID_DATE         VARCHAR2 (7 CHAR);
   V_IF_ROW_BATCH_NUM   NUMBER := 7001;
   V_PRODUCT_NAME       VARCHAR2 (100 CHAR);
   LOG_ROW_ID           NUMBER := 0;
   ROW_COUNT            NUMBER := 0;

   CURSOR C1
   IS
      SELECT A.PERSON_UID,
             A.MEM_NUM,
             ROWNUM,
             A.ACRL_SUM,
             A.RDM_SUM
        FROM SIEBEL_PURGE.PURGE_MEM_TXN A
       WHERE     A.PURGE_TXN_COUNT > 0
             AND A.TXN_NUM IS NOT NULL
             AND A.PERSON_UID IS NOT NULL;
BEGIN
   SELECT CASE WHEN MAX (ROW_ID) IS NOT NULL THEN MAX (ROW_ID) + 1 ELSE 1 END
     INTO LOG_ROW_ID
     FROM SIEBEL_PURGE.PURGE_LOG;

   INSERT INTO SIEBEL_PURGE.PURGE_LOG (ROW_ID, PROCESS_NAME, START_TIME)
      SELECT LOG_ROW_ID, 'Load EIM_CON_DTL Notes2', SYSDATE FROM DUAL;

   COMMIT;

   SELECT TO_CHAR (SYSDATE, 'Y') || TO_CHAR (SYSDATE, 'HH24MISS')
     INTO V_ROWID_DATE
     FROM DUAL;

   SELECT PRODUCT_NAME
     INTO V_PRODUCT_NAME
     FROM SIEBEL_PURGE.PURGE_CONFIG
    WHERE ROW_ID = 1;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_PID,
              ARR_MN,
              ARR_RM,
              ARR_AS,
              ARR_RS
         --LIMIT 10000;
		 LIMIT 50;

      FORALL ROW IN ARR_PID.FIRST .. ARR_PID.LAST
         INSERT INTO SIEBEL.EIM_CON_DTL (ROW_ID,
                                         CREATED_BY,
                                         LAST_UPD_BY,
                                         IF_ROW_BATCH_NUM,
                                         IF_ROW_STAT,
                                         PARTY_TYPE_CD,
                                         PARTY_UID,
                                         CON_PERSON_UID,
                                         NOTE_NOTE_TYPE,
                                         NOTE_NOTE)
                 VALUES (
                              V_ROWID_DATE
                           || LPAD (TO_CHAR (ARR_RM (ROW)), 8, 0),
                           '0-1',
                           '0-1',
                           V_IF_ROW_BATCH_NUM,
                           'FOR_IMPORT',
                           'Person',
                           ARR_PID (ROW),
                           ARR_PID (ROW),
                           'Archiving',
                              'For Member#: '
                           || ARR_MN (ROW)
                           || ' '
                           || V_PRODUCT_NAME
                           || ' had '
                           || ARR_AS (ROW)
                           || ' accrued value and '
                           || ARR_RS (ROW)
                           || ' redemption value');

      ROW_COUNT := ROW_COUNT + SQL%ROWCOUNT;

      UPDATE SIEBEL_PURGE.PURGE_LOG
         SET PROCESSED_ROWS = ROW_COUNT
       WHERE ROW_ID = LOG_ROW_ID;

      COMMIT;

      IF V_IF_ROW_BATCH_NUM = 8000
      THEN
         V_IF_ROW_BATCH_NUM := 7001;
      ELSE
         V_IF_ROW_BATCH_NUM := V_IF_ROW_BATCH_NUM + 1;
      END IF;

      EXIT WHEN C1%NOTFOUND;
   END LOOP;

   COMMIT;

   CLOSE C1;

   EXECUTE IMMEDIATE 'CREATE INDEX SIEBEL.EIM_CON_DTL_150826A
   ON SIEBEL.EIM_CON_DTL (CON_PERSON_UID)
   TABLESPACE SIEBEL_INDEX
   PARALLEL 5';

   EXECUTE IMMEDIATE 'ALTER INDEX SIEBEL.EIM_CON_DTL_150826A
   NOPARALLEL';

   UPDATE SIEBEL_PURGE.PURGE_LOG
      SET END_TIME = SYSDATE, PROCESSED_ROWS = ROW_COUNT
    WHERE ROW_ID = LOG_ROW_ID;

   COMMIT;
END;
/

QUIT