1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| PROCEDURE Abo_Churn_Regroupe_Login
IS
--Déclaration d'un nouveau type
TYPE type_tab_expuid IS TABLE OF ABO_CHURN_DROITS_CAS.EXPUID%TYPE;
TYPE type_tab_debut IS TABLE OF ABO_CHURN_DROITS_CAS.DEBUT%TYPE;
TYPE type_tab_fin IS TABLE OF ABO_CHURN_DROITS_CAS.FIN%TYPE;
TYPE type_tab_email IS TABLE OF ABO_CHURN_DROITS_CAS.EMAIL%TYPE;
TYPE type_tab_portail IS TABLE OF ABO_CHURN_DROITS_CAS.PORTAIL%TYPE;
--Déclaration variable du nouveau type
tab_expuid type_tab_expuid;
tab_debut type_tab_debut;
tab_fin type_tab_fin;
tab_email type_tab_email;
tab_portail type_tab_portail;
--Déclaration des curseurs
CURSOR c_abonnes IS
SELECT UNIQUE expuid
FROM ABO_CHURN_DROITS_CAS
ORDER BY expuid;
CURSOR c_ses_abonnements (un_expuid INTEGER) IS
SELECT a.debut, a.fin, a.email, a.portail
FROM ABO_CHURN_DROITS_CAS a
WHERE a.expuid = un_expuid
ORDER BY a.expuid, a.debut;
jour CONSTANT INTEGER := 1;
delai_regroupement CONSTANT INTEGER := 30 * jour;
BEGIN
--on vide la table
DELETE ABO_CHURN_LOGIN_CAS;
--Utilisation du premier curseur
OPEN c_abonnes;
FETCH c_abonnes BULK COLLECT INTO tab_expuid;
CLOSE c_abonnes;
IF tab_expuid.first IS NOT NULL THEN
FOR abonne IN tab_expuid.first..tab_expuid.last LOOP
DECLARE
nb_lignes_regroupées INTEGER;
premiere_ligne BOOLEAN := TRUE;
debut_regroupement_courant DATE := NULL;
fin_regroupement_courant DATE := NULL;
email_courant ABO_CHURN_DROITS_CAS.EMAIL%TYPE:=NULL;
portail_courant ABO_CHURN_DROITS_CAS.PORTAIL%TYPE:=NULL;
BEGIN
--Utilisation du deuxième curseur
OPEN c_ses_abonnements(tab_expuid(abonne));
FETCH c_ses_abonnements BULK COLLECT INTO tab_debut, tab_fin, tab_email, tab_portail;
CLOSE c_ses_abonnements;
nb_lignes_regroupées := 0;
IF tab_debut.first IS NOT NULL THEN
IF tab_debut.COUNT <> 0 THEN
FOR un_droit IN tab_debut.first..tab_debut.last LOOP
--DBMS_OUTPUT.PUT_LINE ('D : '||tab_debut(un_droit)||' F : '||tab_fin(un_droit)||' debut_regroupement_courant : '|| debut_regroupement_courant|| ' fin_regroupement_courant : '|| fin_regroupement_courant|| ' nb_regroupement : '|| nb_lignes_regroupées|| ' DEBUT');
IF premiere_ligne THEN
-- On teste si le curseur parcourt la 1e ligne d'un groupe
premiere_ligne := FALSE;
-- On initialise les variables de début et de fin du regroupement courant
debut_regroupement_courant := tab_debut(un_droit);
fin_regroupement_courant := tab_fin(un_droit);
nb_lignes_regroupées := 1;
-- DBMS_OUTPUT.PUT_LINE ('D : '||un_droit.debut||' F : '||un_droit.fin||' debut_regroupement_courant : '|| debut_regroupement_courant|| ' fin_regroupement_courant : '|| fin_regroupement_courant|| ' nb_regroupement : '|| nb_lignes_regroupées|| ' 1e ligne groupe');
ELSE
-- Ce n'est pas une première ligne
IF tab_debut(un_droit) - fin_regroupement_courant < delai_regroupement THEN
-- Pas de rupture => On regroupe
-- On prend le min de la date de début du regroupement courant et de la date de début lue (cas où les abonnements sont englobés)
debut_regroupement_courant := LEAST(debut_regroupement_courant, tab_debut(un_droit));
-- On prend le max de la date de fin du regroupement courant et de la date de fin lue (cas où les abonnements sont englobés)
fin_regroupement_courant := GREATEST(fin_regroupement_courant, tab_fin(un_droit));
-- Ne pas oublier de compter le regroupement
nb_lignes_regroupées := nb_lignes_regroupées + 1;
ELSE
-- Rupture, on enregistre le groupe courant.
--DBMS_OUTPUT.PUT_LINE ('tab_portail '||tab_portail(un_droit));
INSERT INTO ABO_CHURN_LOGIN_CAS VALUES (tab_expuid(abonne), debut_regroupement_courant, fin_regroupement_courant, nb_lignes_regroupées, 'MILIEU',tab_email(un_droit),tab_portail(un_droit));
-- Et on ouvre le groupe suivant à partir de la ligne de rupture
debut_regroupement_courant := tab_debut(un_droit);
fin_regroupement_courant := tab_fin(un_droit);
nb_lignes_regroupées := 1;
-- DBMS_OUTPUT.PUT_LINE ('D : '||un_droit.debut||' F : '||un_droit.fin||' debut_regroupement_courant : '|| debut_regroupement_courant|| ' fin_regroupement_courant : '|| fin_regroupement_courant|| ' nb_regroupement : '|| nb_lignes_regroupées|| ' APRES INSERT MILIEU');
END IF;
END IF;
-- Passage des variables locales à la sortie de la boucle
email_courant:=tab_email(un_droit);
portail_courant:=tab_portail(un_droit);
END LOOP;
END IF;
END IF;
-- DBMS_OUTPUT.PUT_LINE (' debut_regroupement_courant : '|| debut_regroupement_courant|| ' fin_regroupement_courant : '|| fin_regroupement_courant|| ' nb_regroupement : '|| nb_lignes_regroupées|| ' DERNIER');
INSERT INTO ABO_CHURN_LOGIN_CAS VALUES (tab_expuid(abonne), debut_regroupement_courant, fin_regroupement_courant, nb_lignes_regroupées, 'DERNIER', email_courant,portail_courant);
END;
END LOOP;
END IF;
COMMIT;
END Abo_Churn_Regroupe_Login; |
Partager