/* Date: 03/16/2015
   By: Veena Veeravalli
   This SQL will import the consolidated transactions for each member with purge eligible transactions into EIM_LOY_TXN table */

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

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

   TYPE T_SNP IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.SUMMARY_NUM_POINTS%TYPE;

   TYPE T_TN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.TXN_NUM%TYPE;

   ARR_MN               T_MN;
   ARR_RM               T_RM;
   ARR_SNP              T_SNP;
   ARR_TN               T_TN;

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

   CURSOR C1
   IS
      SELECT A.SUMMARY_NUM_POINTS,
             ROWNUM,
             A.TXN_NUM,
             A.MEM_NUM
        FROM SIEBEL_PURGE.PURGE_MEM_TXN A
       WHERE A.PURGE_TXN_COUNT > 0 AND A.TXN_NUM 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_LOY_TXN', SYSDATE FROM DUAL;

   COMMIT;

   SELECT PURGE_DATE, PRODUCT_NAME
     INTO V_TXN_DATE, V_PRODUCT_NAME
     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_SNP,
              ARR_RM,
              ARR_TN,
              ARR_MN
         --LIMIT 10000;
		 LIMIT 50;

      FORALL ROW IN ARR_TN.FIRST .. ARR_TN.LAST
         INSERT INTO SIEBEL.EIM_LOY_TXN (ROW_ID,
                                         CREATED_BY,
                                         LAST_UPD_BY,
                                         IF_ROW_BATCH_NUM,
                                         IF_ROW_STAT,
                                         TXN_NUM,
                                         PROD_NAME,
                                         MEMBER_MEM_NUM,
                                         CURCY_EXCH_DT,
                                         NUM_POINTS,
                                         TXN_DT,
                                         COMMENTS,
                                         X_ORIG_MBR_NBR)
              VALUES (V_ROWID_DATE || LPAD (TO_CHAR (ARR_RM (ROW)), 8, 0),
                      '0-1',
                      '0-1',
                      V_IF_ROW_BATCH_NUM,
                      'FOR_IMPORT',
                      ARR_TN (ROW),
                      V_PRODUCT_NAME,
                      ARR_MN (ROW),
                      V_TXN_DATE,
                      ARR_SNP (ROW),
                      V_TXN_DATE,
                      V_PRODUCT_NAME,
                      ARR_MN (ROW));

      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;

   EXECUTE IMMEDIATE 'CREATE INDEX SIEBEL.EIM_LOY_TXN_150819C
   ON SIEBEL.EIM_LOY_TXN (TXN_NUM)
   LOGGING
   TABLESPACE SIEBEL_INDEX
   PARALLEL 5';

   EXECUTE IMMEDIATE 'ALTER INDEX SIEBEL.EIM_LOY_TXN_150819C
   NOPARALLEL';

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

   COMMIT;
END;
/

QUIT