--Author: Venkata Potluri
--Date: Oct 23, 2015

DECLARE
   TYPE T_ID IS TABLE OF SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM.RDM_ID%TYPE;

   TYPE T_MEM_ID IS TABLE OF SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM.MEM_ID%TYPE;

   TYPE T_ACRL_ID
      IS TABLE OF SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM.PRV_ACRL_ID%TYPE;

   TYPE T_ON IS TABLE OF SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM.ORDER_NUM%TYPE;

   ARR_ID        T_ID;
   ARR_MEM_ID    T_MEM_ID;
   ARR_ACRL_ID   T_ACRL_ID;
   ARR_ON        T_ON;
   LOG_ROW_ID    NUMBER := 0;
   ROW_COUNT     NUMBER := 0;
   V_TASK_NUM    NUMBER := 1;

   CURSOR C1
   IS
        SELECT /*+ PARALLEL(R,10) FULL(R) INDEX(A,S_LOY_ACRL_ITM_P1) */
              R.ROW_ID,
               R.MEMBER_ID,
               R.ACCRUAL_ITEM_ID,
               ROW_NUMBER () OVER (ORDER BY R.ROWID) AS SEQ_NUM
          FROM SIEBEL.S_LOY_RDM_ITM R
               INNER JOIN SIEBEL.S_LOY_ACRL_ITM A
                  ON R.ACCRUAL_ITEM_ID = A.ROW_ID
         WHERE     R.X_PURGE_FLG IS NULL
               AND A.X_PURGE_FLG = 'Y'
               AND R.CREATED >
                      (SELECT MAX (START_TIME)
                         FROM SIEBEL_PURGE.PURGE_LOG
                        WHERE PROCESS_NAME LIKE
                                 'Populate P_INT_S_LOY_RDM_ITM Before Purge')
      ORDER BY R.ROWID;
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, 'Populate P_INT_S_LOY_RDM_ITM Before Purge', SYSDATE
        FROM DUAL;

   COMMIT;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_ID,
              ARR_MEM_ID,
              ARR_ACRL_ID,
              ARR_ON
         --LIMIT 10000;
         LIMIT 50;

      FORALL ROW IN ARR_ID.FIRST .. ARR_ID.LAST
         INSERT INTO SIEBEL_PURGE.P_INT_S_LOY_RDM_ITM (MEM_ID,
                                                       RDM_ID,
                                                       PRV_ACRL_ID,
                                                       ORDER_NUM,
                                                       TASK_NUM)
              VALUES (ARR_MEM_ID (ROW),
                      ARR_ID (ROW),
                      ARR_ACRL_ID (ROW),
                      ARR_ON (ROW),
                      V_TASK_NUM);

      ROW_COUNT := ROW_COUNT + SQL%ROWCOUNT;

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

      COMMIT;

      IF ROW_COUNT < 10500000
      THEN
         V_TASK_NUM := 1;
      ELSE
         IF ROW_COUNT < 21000000
         THEN
            V_TASK_NUM := 2;
         ELSE
            IF ROW_COUNT < 31500000
            THEN
               V_TASK_NUM := 3;
            ELSE
               V_TASK_NUM := 4;
            END IF;
         END IF;
      END IF;


      EXIT WHEN C1%NOTFOUND;
   END LOOP;

   CLOSE C1;

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

   COMMIT;
END;
/

QUIT