/* Date: 03/16/2015
   By: Veena Veeravalli
   This SQL will import notes (TYPE 1) 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_MN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.MEM_NUM%TYPE;

   ARR_PID              T_PID;
   ARR_RM               T_RM;
   ARR_MN               T_MN;

   V_ROWID_DATE         VARCHAR2 (7 CHAR);
   V_IF_ROW_BATCH_NUM   NUMBER := 6001;
   V_PURGE_DATE         VARCHAR2 (10 CHAR);
   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
        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 Notes1', SYSDATE FROM DUAL;

   COMMIT;

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

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

   OPEN C1;

   LOOP
      --FETCH C1 BULK COLLECT INTO ARR_PID, ARR_MN, ARR_RM LIMIT 10000;
	  FETCH C1 BULK COLLECT INTO ARR_PID, ARR_MN, ARR_RM 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)
                           || ' Transactions older than '
                           || V_PURGE_DATE
                           || ' have been consolidated into a Special Services transaction using product code "'
                           || V_PRODUCT_NAME
                           || '"');

      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 = 7000
      THEN
         V_IF_ROW_BATCH_NUM := 6001;
      ELSE
         V_IF_ROW_BATCH_NUM := V_IF_ROW_BATCH_NUM + 1;
      END IF;

      EXIT WHEN C1%NOTFOUND;
   END LOOP;

   COMMIT;

   CLOSE C1;

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

   COMMIT;
END;
/

QUIT