Subversion Repositories SmartDukaan

Rev

Rev 36181 | Rev 36212 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36181 Rev 36210
Line 613... Line 613...
613
 
613
 
614
        // Get all RBM positions (L1 and L2)
614
        // Get all RBM positions (L1 and L2)
615
        long start = System.currentTimeMillis();
615
        long start = System.currentTimeMillis();
616
        List<Position> allRbmPositions = positionRepository
616
        List<Position> allRbmPositions = positionRepository
617
                .selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM).stream()
617
                .selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM).stream()
618
                .filter(x -> Arrays.asList(EscalationType.L1, EscalationType.L2).contains(x.getEscalationType()))
618
                .filter(x -> Arrays.asList(EscalationType.L1, EscalationType.L2, EscalationType.L3).contains(x.getEscalationType()))
619
                .collect(Collectors.toList());
619
                .collect(Collectors.toList());
620
 
620
 
621
        // Separate L1 and L2 auth IDs
621
        // Separate L1, L2 and L3 auth IDs
622
        List<Integer> l1AuthIds = allRbmPositions.stream()
622
        List<Integer> l1AuthIds = allRbmPositions.stream()
623
                .filter(p -> EscalationType.L1.equals(p.getEscalationType()))
623
                .filter(p -> EscalationType.L1.equals(p.getEscalationType()))
624
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
624
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
625
        List<Integer> l2AuthIds = allRbmPositions.stream()
625
        List<Integer> l2AuthIds = allRbmPositions.stream()
626
                .filter(p -> EscalationType.L2.equals(p.getEscalationType()))
626
                .filter(p -> EscalationType.L2.equals(p.getEscalationType()))
627
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
627
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
-
 
628
        List<Integer> l3AuthIds = allRbmPositions.stream()
-
 
629
                .filter(p -> EscalationType.L3.equals(p.getEscalationType()))
-
 
630
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
628
 
631
 
629
        // Union of all auth IDs for batch fetching
632
        // Union of all auth IDs for batch fetching
630
        List<Integer> rbmPositionsAuthIds = allRbmPositions.stream()
633
        List<Integer> rbmPositionsAuthIds = allRbmPositions.stream()
631
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
634
                .map(Position::getAuthUserId).distinct().collect(Collectors.toList());
632
        LOGGER.info("RBM Call Target - RBM positions fetch: {}ms, L1: {}, L2: {}", System.currentTimeMillis() - start, l1AuthIds.size(), l2AuthIds.size());
635
        LOGGER.info("RBM Call Target - RBM positions fetch: {}ms, L1: {}, L2: {}, L3: {}", System.currentTimeMillis() - start, l1AuthIds.size(), l2AuthIds.size(), l3AuthIds.size());
633
 
636
 
634
        start = System.currentTimeMillis();
637
        start = System.currentTimeMillis();
635
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
638
        Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
636
        LOGGER.info("RBM Call Target - StoreGuyMap fetch: {}ms", System.currentTimeMillis() - start);
639
        LOGGER.info("RBM Call Target - StoreGuyMap fetch: {}ms", System.currentTimeMillis() - start);
637
 
640
 
Line 665... Line 668...
665
        // Initialize L2 calling list map - will be populated after fetching remarks
668
        // Initialize L2 calling list map - will be populated after fetching remarks
666
        Map<Integer, List<Integer>> l2AuthIdToFofoIds = new HashMap<>();
669
        Map<Integer, List<Integer>> l2AuthIdToFofoIds = new HashMap<>();
667
        for (int l2AuthId : l2AuthIds) {
670
        for (int l2AuthId : l2AuthIds) {
668
            l2AuthIdToFofoIds.put(l2AuthId, new ArrayList<>());
671
            l2AuthIdToFofoIds.put(l2AuthId, new ArrayList<>());
669
        }
672
        }
-
 
673
        // Initialize L3 calling list map - will be populated after fetching remarks
-
 
674
        Map<Integer, List<Integer>> l3AuthIdToFofoIds = new HashMap<>();
-
 
675
        for (int l3AuthId : l3AuthIds) {
-
 
676
            l3AuthIdToFofoIds.put(l3AuthId, new ArrayList<>());
-
 
677
        }
670
        LOGGER.info("RBM Call Target - Total fofo IDs to process: {}", allFofoIds.size());
678
        LOGGER.info("RBM Call Target - Total fofo IDs to process: {}", allFofoIds.size());
671
 
679
 
672
        // Get only needed fofo stores (OPTIMIZED - was fetching ALL stores before)
680
        // Get only needed fofo stores (OPTIMIZED - was fetching ALL stores before)
673
        start = System.currentTimeMillis();
681
        start = System.currentTimeMillis();
674
        Map<Integer, FofoStore> fofoStoresMap = new HashMap<>();
682
        Map<Integer, FofoStore> fofoStoresMap = new HashMap<>();
Line 716... Line 724...
716
                }
724
                }
717
            }
725
            }
718
        }
726
        }
719
        LOGGER.info("RBM Call Target - L2 calling lists populated from RBM_L2_ESCALATION remarks");
727
        LOGGER.info("RBM Call Target - L2 calling lists populated from RBM_L2_ESCALATION remarks");
720
 
728
 
-
 
729
        // Populate L3 calling list based on partners whose latest remark is RBM_L3_ESCALATION
-
 
730
        // Find the L1 who originally has the partner, then traverse: L1 -> L2 (manager) -> L3 (manager's manager)
-
 
731
        for (Map.Entry<Integer, PartnerCollectionRemark> entry : allPartnerCollectionRemarks.entrySet()) {
-
 
732
            Integer fofoId = entry.getKey();
-
 
733
            PartnerCollectionRemark remark = entry.getValue();
-
 
734
 
-
 
735
            if (CollectionRemark.RBM_L3_ESCALATION.equals(remark.getRemark())) {
-
 
736
                // Find which L1 RBM originally has this partner assigned
-
 
737
                for (int l1AuthId : l1AuthIds) {
-
 
738
                    List<Integer> l1FofoIds = rbmToFofoIdsMap.getOrDefault(l1AuthId, Collections.emptyList());
-
 
739
                    if (l1FofoIds.contains(fofoId)) {
-
 
740
                        // Get L1's manager (L2)
-
 
741
                        AuthUser l1User = authUserMap.get(l1AuthId);
-
 
742
                        if (l1User != null) {
-
 
743
                            int l2ManagerId = l1User.getManagerId();
-
 
744
                            AuthUser l2User = authUserMap.get(l2ManagerId);
-
 
745
                            // Get L2's manager (L3)
-
 
746
                            if (l2User != null && l3AuthIdToFofoIds.containsKey(l2User.getManagerId())) {
-
 
747
                                int l3ManagerId = l2User.getManagerId();
-
 
748
                                l3AuthIdToFofoIds.get(l3ManagerId).add(fofoId);
-
 
749
                                LOGGER.debug("L3 Calling List: fofoId={} -> L1={} -> L2={} -> L3={}",
-
 
750
                                        fofoId, l1AuthId, l2ManagerId, l3ManagerId);
-
 
751
                            }
-
 
752
                        }
-
 
753
                        break; // Found the L1 for this fofoId
-
 
754
                    }
-
 
755
                }
-
 
756
            }
-
 
757
        }
-
 
758
        LOGGER.info("RBM Call Target - L3 calling lists populated from RBM_L3_ESCALATION remarks");
-
 
759
 
721
        // Batch fetch collection RANK map for all fofoIds (OPTIMIZED - only fetches rank, not full model)
760
        // Batch fetch collection RANK map for all fofoIds (OPTIMIZED - only fetches rank, not full model)
722
        start = System.currentTimeMillis();
761
        start = System.currentTimeMillis();
723
        Map<Integer, Integer> allCollectionRankMap = new HashMap<>();
762
        Map<Integer, Integer> allCollectionRankMap = new HashMap<>();
724
        if (!allFofoIds.isEmpty()) {
763
        if (!allFofoIds.isEmpty()) {
725
            try {
764
            try {
Line 938... Line 977...
938
 
977
 
939
            l2Model.setOutOfSequenceCount(outOfSequenceCountByAuthId.getOrDefault(l2AuthId, 0L));
978
            l2Model.setOutOfSequenceCount(outOfSequenceCountByAuthId.getOrDefault(l2AuthId, 0L));
940
            rbmCallTargetModels.add(l2Model);
979
            rbmCallTargetModels.add(l2Model);
941
        }
980
        }
942
 
981
 
-
 
982
        // Process L3 RBMs (escalated ticket logic with categorization)
-
 
983
        for (int l3AuthId : l3AuthIds) {
-
 
984
            AuthUser authUser = authUserMap.get(l3AuthId);
-
 
985
            if (authUser == null) {
-
 
986
                continue;
-
 
987
            }
-
 
988
 
-
 
989
            List<Integer> l3FofoIdList = l3AuthIdToFofoIds.getOrDefault(l3AuthId, Collections.emptyList());
-
 
990
 
-
 
991
            // For L3, use unique fofoIds with RBM_L3_ESCALATION remark as target
-
 
992
            Set<Integer> l3TargetFofoIds = new HashSet<>(l3FofoIdList);
-
 
993
 
-
 
994
            RbmCallTargetModel l3Model = new RbmCallTargetModel();
-
 
995
            l3Model.setAuthId(l3AuthId);
-
 
996
            l3Model.setRbmName(authUser.getFullName() + " (L3)");
-
 
997
            l3Model.setL3Position(true);
-
 
998
            l3Model.setL3CallingList(l3TargetFofoIds.size());
-
 
999
            // Partner count = total assigned partners (same as L2 source)
-
 
1000
            List<Integer> l3AssignedFofoIds = rbmToFofoIdsMap.getOrDefault(l3AuthId, Collections.emptyList());
-
 
1001
            l3Model.setPartnerCount(l3AssignedFofoIds.size());
-
 
1002
 
-
 
1003
            // L3 Target = partners with RBM_L3_ESCALATION as latest remark
-
 
1004
            l3Model.setTodayTargetOfCall(l3TargetFofoIds.size());
-
 
1005
 
-
 
1006
            // Value Achieved = All distinct partners called today (from call logs)
-
 
1007
            long l3ValueAchieved = getCalledCountFromCallLogs(l3AuthId);
-
 
1008
            l3Model.setValueTargetAchieved(l3ValueAchieved);
-
 
1009
 
-
 
1010
            l3Model.setOutOfSequenceCount(outOfSequenceCountByAuthId.getOrDefault(l3AuthId, 0L));
-
 
1011
            rbmCallTargetModels.add(l3Model);
-
 
1012
        }
-
 
1013
 
943
        // Group L1 under their L2 manager using authUser.managerId
1014
        // Group L1 under their L2 manager, and L2 under their L3 manager using authUser.managerId
-
 
1015
        Map<Integer, RbmCallTargetModel> l3ModelsByAuthId = new HashMap<>();
944
        Map<Integer, RbmCallTargetModel> l2ModelsByAuthId = new HashMap<>();
1016
        Map<Integer, RbmCallTargetModel> l2ModelsByAuthId = new HashMap<>();
945
        Map<Integer, RbmCallTargetModel> l1ModelsByAuthId = new HashMap<>();
1017
        Map<Integer, RbmCallTargetModel> l1ModelsByAuthId = new HashMap<>();
946
        for (RbmCallTargetModel m : rbmCallTargetModels) {
1018
        for (RbmCallTargetModel m : rbmCallTargetModels) {
947
            if (m.isL2Position()) {
1019
            if (m.isL3Position()) {
-
 
1020
                l3ModelsByAuthId.put(m.getAuthId(), m);
-
 
1021
            } else if (m.isL2Position()) {
948
                l2ModelsByAuthId.put(m.getAuthId(), m);
1022
                l2ModelsByAuthId.put(m.getAuthId(), m);
949
            } else {
1023
            } else {
950
                l1ModelsByAuthId.put(m.getAuthId(), m);
1024
                l1ModelsByAuthId.put(m.getAuthId(), m);
951
            }
1025
            }
952
        }
1026
        }
953
 
1027
 
-
 
1028
        // Build L3 -> L2 team map using managerId from AuthUser
-
 
1029
        Map<Integer, List<RbmCallTargetModel>> l3TeamMap = new LinkedHashMap<>();
-
 
1030
        for (RbmCallTargetModel l3Model : l3ModelsByAuthId.values()) {
-
 
1031
            l3TeamMap.put(l3Model.getAuthId(), new ArrayList<>());
-
 
1032
        }
-
 
1033
 
-
 
1034
        Set<Integer> addedL2AuthIds = new HashSet<>();
-
 
1035
        for (RbmCallTargetModel l2Model : l2ModelsByAuthId.values()) {
-
 
1036
            AuthUser l2User = authUserMap.get(l2Model.getAuthId());
-
 
1037
            if (l2User != null && l3TeamMap.containsKey(l2User.getManagerId())) {
-
 
1038
                l3TeamMap.get(l2User.getManagerId()).add(l2Model);
-
 
1039
                addedL2AuthIds.add(l2Model.getAuthId());
-
 
1040
            }
-
 
1041
        }
-
 
1042
 
954
        // Build L2 -> L1 team map using managerId from AuthUser
1043
        // Build L2 -> L1 team map using managerId from AuthUser
955
        Map<Integer, List<RbmCallTargetModel>> l2TeamMap = new LinkedHashMap<>();
1044
        Map<Integer, List<RbmCallTargetModel>> l2TeamMap = new LinkedHashMap<>();
956
        for (RbmCallTargetModel l2Model : l2ModelsByAuthId.values()) {
1045
        for (RbmCallTargetModel l2Model : l2ModelsByAuthId.values()) {
957
            l2TeamMap.put(l2Model.getAuthId(), new ArrayList<>());
1046
            l2TeamMap.put(l2Model.getAuthId(), new ArrayList<>());
958
        }
1047
        }
Line 964... Line 1053...
964
                l2TeamMap.get(l1User.getManagerId()).add(l1Model);
1053
                l2TeamMap.get(l1User.getManagerId()).add(l1Model);
965
                addedL1AuthIds.add(l1Model.getAuthId());
1054
                addedL1AuthIds.add(l1Model.getAuthId());
966
            }
1055
            }
967
        }
1056
        }
968
 
1057
 
969
        // Build sorted result: L2 row, then its L1 team (sorted by name)
1058
        // Build sorted result: L3 row, then its L2 team with their L1 teams (sorted by name)
970
        List<RbmCallTargetModel> sortedModels = new ArrayList<>();
1059
        List<RbmCallTargetModel> sortedModels = new ArrayList<>();
971
 
1060
 
-
 
1061
        // First add L3 with their L2 teams
972
        List<RbmCallTargetModel> l2Sorted = new ArrayList<>(l2ModelsByAuthId.values());
1062
        List<RbmCallTargetModel> l3Sorted = new ArrayList<>(l3ModelsByAuthId.values());
973
        l2Sorted.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
1063
        l3Sorted.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
-
 
1064
 
-
 
1065
        for (RbmCallTargetModel l3Model : l3Sorted) {
-
 
1066
            sortedModels.add(l3Model);
-
 
1067
            List<RbmCallTargetModel> l2Team = l3TeamMap.getOrDefault(l3Model.getAuthId(), Collections.emptyList());
-
 
1068
            l2Team.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
-
 
1069
            for (RbmCallTargetModel l2Model : l2Team) {
-
 
1070
                sortedModels.add(l2Model);
-
 
1071
                List<RbmCallTargetModel> l1Team = l2TeamMap.getOrDefault(l2Model.getAuthId(), Collections.emptyList());
-
 
1072
                l1Team.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
-
 
1073
                sortedModels.addAll(l1Team);
-
 
1074
            }
-
 
1075
        }
-
 
1076
 
-
 
1077
        // Add L2s not mapped to any L3 with their L1 teams
-
 
1078
        List<RbmCallTargetModel> unmappedL2 = new ArrayList<>();
-
 
1079
        for (RbmCallTargetModel l2Model : l2ModelsByAuthId.values()) {
-
 
1080
            if (!addedL2AuthIds.contains(l2Model.getAuthId())) {
-
 
1081
                unmappedL2.add(l2Model);
-
 
1082
            }
-
 
1083
        }
-
 
1084
        unmappedL2.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
974
 
1085
 
975
        for (RbmCallTargetModel l2Model : l2Sorted) {
1086
        for (RbmCallTargetModel l2Model : unmappedL2) {
976
            sortedModels.add(l2Model);
1087
            sortedModels.add(l2Model);
977
            List<RbmCallTargetModel> team = l2TeamMap.getOrDefault(l2Model.getAuthId(), Collections.emptyList());
1088
            List<RbmCallTargetModel> team = l2TeamMap.getOrDefault(l2Model.getAuthId(), Collections.emptyList());
978
            team.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
1089
            team.sort(Comparator.comparing(RbmCallTargetModel::getRbmName));
979
            sortedModels.addAll(team);
1090
            sortedModels.addAll(team);
980
        }
1091
        }