Subversion Repositories SmartDukaan

Rev

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

Rev 35670 Rev 35672
Line 1020... Line 1020...
1020
 
1020
 
1021
        return result;
1021
        return result;
1022
    }
1022
    }
1023
 
1023
 
1024
    @Override
1024
    @Override
1025
    public List<CalledPartnerDetailModel> getCalledPartnerDetails(int authId) {
1025
    public List<CalledPartnerDetailModel> getCalledPartnerDetails(int authId) throws ProfitMandiBusinessException {
1026
        LocalDate today = LocalDate.now();
1026
        LocalDate today = LocalDate.now();
-
 
1027
        LocalDateTime startDate = today.atStartOfDay();
-
 
1028
        LocalDate firstOfMonth = today.withDayOfMonth(1);
-
 
1029
        LocalDate endOfMonth = today.withDayOfMonth(today.lengthOfMonth()).plusDays(1);
-
 
1030
 
-
 
1031
        // Get auth user
-
 
1032
        List<AuthUser> authUsers = authRepository.selectByIds(Collections.singletonList(authId));
-
 
1033
        if (authUsers.isEmpty()) {
-
 
1034
            return Collections.emptyList();
-
 
1035
        }
-
 
1036
        AuthUser authUser = authUsers.get(0);
1027
 
1037
 
-
 
1038
        // Check if L1 or L2
-
 
1039
        List<Position> positions = positionRepository.selectPositionByAuthIds(Collections.singletonList(authId));
-
 
1040
        boolean isL2 = positions.stream()
-
 
1041
                .anyMatch(p -> ProfitMandiConstants.TICKET_CATEGORY_RBM == p.getCategoryId()
-
 
1042
                        && EscalationType.L2.equals(p.getEscalationType()));
-
 
1043
 
1028
        // Get today's remarks for this RBM
1044
        // Get fofo IDs for this RBM
-
 
1045
        List<Integer> fofoIdList;
-
 
1046
        if (isL2) {
-
 
1047
            // L2: get fofo IDs from escalated tickets (all escalated tickets are target)
-
 
1048
            List<Ticket> escalatedTickets = ticketRepository.selectOpenEscalatedTicketsByAuthIds(Collections.singletonList(authId));
-
 
1049
            fofoIdList = escalatedTickets.stream()
-
 
1050
                    .filter(t -> t.getL2AuthUser() == authId
-
 
1051
                            || t.getL3AuthUser() == authId
-
 
1052
                            || t.getL4AuthUser() == authId
-
 
1053
                            || t.getL5AuthUser() == authId)
-
 
1054
                    .map(Ticket::getFofoId)
-
 
1055
                    .distinct()
-
 
1056
                    .collect(Collectors.toList());
-
 
1057
 
-
 
1058
            // For L2, all escalated tickets are target
-
 
1059
            Set<Integer> targetFofoIds = new HashSet<>(fofoIdList);
-
 
1060
 
-
 
1061
            // Get today's remarks
1029
        List<PartnerCollectionRemark> todayRemarks = partnerCollectionRemarkRepository
1062
            List<PartnerCollectionRemark> todayRemarks = partnerCollectionRemarkRepository
1030
                .selectAllByAuthIdsOnDate(Collections.singletonList(authId), today);
1063
                    .selectAllByAuthIdsOnDate(Collections.singletonList(authId), today);
-
 
1064
 
-
 
1065
            // Filter to only target partners and deduplicate
-
 
1066
            Map<Integer, PartnerCollectionRemark> uniqueRemarksByFofoId = new LinkedHashMap<>();
-
 
1067
            for (PartnerCollectionRemark remark : todayRemarks) {
-
 
1068
                if (targetFofoIds.contains(remark.getFofoId())) {
-
 
1069
                    uniqueRemarksByFofoId.putIfAbsent(remark.getFofoId(), remark);
-
 
1070
                }
-
 
1071
            }
-
 
1072
 
-
 
1073
            return buildCalledPartnerResult(uniqueRemarksByFofoId);
-
 
1074
        }
-
 
1075
 
-
 
1076
        // L1 Logic
-
 
1077
        Map<String, Set<Integer>> storeGuyMap;
-
 
1078
        try {
-
 
1079
            storeGuyMap = csService.getAuthUserPartnerIdMapping();
-
 
1080
        } catch (ProfitMandiBusinessException e) {
-
 
1081
            LOGGER.error("Error fetching store guy map", e);
-
 
1082
            return Collections.emptyList();
-
 
1083
        }
-
 
1084
 
-
 
1085
        if (!storeGuyMap.containsKey(authUser.getEmailId())) {
-
 
1086
            return Collections.emptyList();
-
 
1087
        }
-
 
1088
        fofoIdList = new ArrayList<>(storeGuyMap.get(authUser.getEmailId()));
-
 
1089
 
-
 
1090
        if (fofoIdList.isEmpty()) {
-
 
1091
            return Collections.emptyList();
-
 
1092
        }
-
 
1093
 
-
 
1094
        // Get fofo stores for filtering
-
 
1095
        Map<Integer, FofoStore> fofoStoresMap;
-
 
1096
        try {
-
 
1097
            fofoStoresMap = fofoStoreRepository.selectByRetailerIds(fofoIdList).stream()
-
 
1098
                    .collect(Collectors.toMap(FofoStore::getId, x -> x, (a, b) -> a));
-
 
1099
        } catch (ProfitMandiBusinessException e) {
-
 
1100
            LOGGER.error("Error fetching fofo stores", e);
-
 
1101
            return Collections.emptyList();
-
 
1102
        }
-
 
1103
 
-
 
1104
        // Filter escalated partners for L1
-
 
1105
        List<Integer> allRemarkIds = partnerCollectionRemarkRepository.selectMaxRemarkId(fofoIdList);
-
 
1106
        if (!allRemarkIds.isEmpty()) {
-
 
1107
            Map<Integer, PartnerCollectionRemark> partnerCollectionRemarks = partnerCollectionRemarkRepository.selectByIds(allRemarkIds).stream()
-
 
1108
                    .collect(Collectors.toMap(PartnerCollectionRemark::getFofoId, x -> x, (a, b) -> a));
-
 
1109
            fofoIdList = partnerCollectionRemarks.entrySet().stream()
-
 
1110
                    .filter(entry -> {
-
 
1111
                        PartnerCollectionRemark pcrMap = entry.getValue();
-
 
1112
                        return !(CollectionRemark.RBM_L2_ESCALATION.equals(pcrMap.getRemark())
-
 
1113
                                || CollectionRemark.SALES_ESCALATION.equals(pcrMap.getRemark()));
-
 
1114
                    })
-
 
1115
                    .map(Map.Entry::getKey)
-
 
1116
                    .collect(Collectors.toList());
-
 
1117
        }
-
 
1118
 
-
 
1119
        // Filter to only external, ACTIVE stores
-
 
1120
        List<Integer> validFofoIds = fofoIdList.stream()
-
 
1121
                .filter(fofoId -> {
-
 
1122
                    FofoStore store = fofoStoresMap.get(fofoId);
-
 
1123
                    if (store == null || store.isInternal()) {
-
 
1124
                        return false;
-
 
1125
                    }
-
 
1126
                    return ActivationType.ACTIVE.equals(store.getActivationType());
-
 
1127
                })
-
 
1128
                .collect(Collectors.toList());
1031
 
1129
 
1032
        if (todayRemarks.isEmpty()) {
1130
        if (validFofoIds.isEmpty()) {
1033
            return Collections.emptyList();
1131
            return Collections.emptyList();
1034
        }
1132
        }
1035
 
1133
 
-
 
1134
        // Get collection rank map
-
 
1135
        Map<Integer, Integer> collectionRankMap;
-
 
1136
        try {
-
 
1137
            collectionRankMap = partnerCollectionService.getCollectionRankMap(validFofoIds, startDate);
-
 
1138
        } catch (ProfitMandiBusinessException e) {
-
 
1139
            LOGGER.error("Error fetching collection rank map", e);
-
 
1140
            collectionRankMap = new HashMap<>();
-
 
1141
        }
-
 
1142
 
-
 
1143
        // Get MTD billing data for zero billing check
-
 
1144
        List<RbmWeeklyBillingModel> mtdBillingData = getWeeklyBillingDataForMonth(firstOfMonth, endOfMonth);
-
 
1145
        Set<Integer> mtdBilledFofoIds = mtdBillingData.stream()
-
 
1146
                .filter(RbmWeeklyBillingModel::isMtdBilled)
-
 
1147
                .map(RbmWeeklyBillingModel::getFofoId)
-
 
1148
                .collect(Collectors.toSet());
-
 
1149
 
-
 
1150
        // Build target partner set (PlanToday + CarryForward + ZeroBilling + Untouched)
-
 
1151
        Set<Integer> targetFofoIds = new HashSet<>();
-
 
1152
        for (Integer fofoId : validFofoIds) {
-
 
1153
            int rank = collectionRankMap.getOrDefault(fofoId, 5);
-
 
1154
            boolean hasZeroBilling = !mtdBilledFofoIds.contains(fofoId);
-
 
1155
 
-
 
1156
            // Same priority logic as in getRbmCallTargetModels
-
 
1157
            if (rank == 1 || rank == 2 || hasZeroBilling || rank == 3) {
-
 
1158
                targetFofoIds.add(fofoId);
-
 
1159
            }
-
 
1160
            // rank 4 (FuturePlan) and rank 5 (Normal) are NOT in target
-
 
1161
        }
-
 
1162
 
-
 
1163
        // Get today's remarks
-
 
1164
        List<PartnerCollectionRemark> todayRemarks = partnerCollectionRemarkRepository
-
 
1165
                .selectAllByAuthIdsOnDate(Collections.singletonList(authId), today);
-
 
1166
 
1036
        // Get unique fofoIds (keep first/latest remark per fofoId)
1167
        // Filter to only target partners and deduplicate
1037
        Map<Integer, PartnerCollectionRemark> uniqueRemarksByFofoId = new LinkedHashMap<>();
1168
        Map<Integer, PartnerCollectionRemark> uniqueRemarksByFofoId = new LinkedHashMap<>();
1038
        for (PartnerCollectionRemark remark : todayRemarks) {
1169
        for (PartnerCollectionRemark remark : todayRemarks) {
-
 
1170
            if (targetFofoIds.contains(remark.getFofoId())) {
1039
            uniqueRemarksByFofoId.putIfAbsent(remark.getFofoId(), remark);
1171
                uniqueRemarksByFofoId.putIfAbsent(remark.getFofoId(), remark);
-
 
1172
            }
-
 
1173
        }
-
 
1174
 
-
 
1175
        return buildCalledPartnerResult(uniqueRemarksByFofoId);
-
 
1176
    }
-
 
1177
 
-
 
1178
    private List<CalledPartnerDetailModel> buildCalledPartnerResult(Map<Integer, PartnerCollectionRemark> uniqueRemarksByFofoId) {
-
 
1179
        if (uniqueRemarksByFofoId.isEmpty()) {
-
 
1180
            return Collections.emptyList();
1040
        }
1181
        }
1041
 
1182
 
1042
        Set<Integer> fofoIds = uniqueRemarksByFofoId.keySet();
1183
        Set<Integer> fofoIds = uniqueRemarksByFofoId.keySet();
1043
        Map<Integer, CustomRetailer> retailerMap = Collections.emptyMap();
1184
        Map<Integer, CustomRetailer> retailerMap = Collections.emptyMap();
1044
        try {
1185
        try {