| Line 880... |
Line 880... |
| 880 |
if (authUser == null) {
|
880 |
if (authUser == null) {
|
| 881 |
continue;
|
881 |
continue;
|
| 882 |
}
|
882 |
}
|
| 883 |
|
883 |
|
| 884 |
List<Integer> l2FofoIdList = l2AuthIdToFofoIds.getOrDefault(l2AuthId, Collections.emptyList());
|
884 |
List<Integer> l2FofoIdList = l2AuthIdToFofoIds.getOrDefault(l2AuthId, Collections.emptyList());
|
| 885 |
if (l2FofoIdList.isEmpty()) {
|
- |
|
| 886 |
continue;
|
- |
|
| 887 |
}
|
- |
|
| 888 |
|
885 |
|
| 889 |
// Filter to only external, ACTIVE stores with collection plan
|
886 |
// Filter to only external, ACTIVE stores with collection plan
|
| 890 |
Map<Integer, Integer> finalAllCollectionRankMap2 = allCollectionRankMap;
|
887 |
Map<Integer, Integer> finalAllCollectionRankMap2 = allCollectionRankMap;
|
| 891 |
Map<Integer, FofoStore> finalFofoStoresMap2 = fofoStoresMap;
|
888 |
Map<Integer, FofoStore> finalFofoStoresMap2 = fofoStoresMap;
|
| 892 |
List<Integer> validL2FofoIds = l2FofoIdList.stream()
|
889 |
List<Integer> validL2FofoIds = l2FofoIdList.stream()
|
| Line 1083... |
Line 1080... |
| 1083 |
}
|
1080 |
}
|
| 1084 |
|
1081 |
|
| 1085 |
return result;
|
1082 |
return result;
|
| 1086 |
}
|
1083 |
}
|
| 1087 |
|
1084 |
|
| - |
|
1085 |
@Override
|
| - |
|
1086 |
public List<List<String>> getRbmCallTargetRawDataByAuthId(int authId) throws Exception {
|
| - |
|
1087 |
List<List<String>> rows = new ArrayList<>();
|
| - |
|
1088 |
|
| - |
|
1089 |
// Get auth user
|
| - |
|
1090 |
List<AuthUser> authUsers = authRepository.selectByIds(Collections.singletonList(authId));
|
| - |
|
1091 |
if (authUsers.isEmpty()) {
|
| - |
|
1092 |
return rows;
|
| - |
|
1093 |
}
|
| - |
|
1094 |
AuthUser authUser = authUsers.get(0);
|
| - |
|
1095 |
|
| - |
|
1096 |
// Get positions to determine if L1 or L2
|
| - |
|
1097 |
List<Position> positions = positionRepository.selectPositionByAuthIds(Collections.singletonList(authId));
|
| - |
|
1098 |
boolean isL1 = positions.stream()
|
| - |
|
1099 |
.anyMatch(p -> ProfitMandiConstants.TICKET_CATEGORY_RBM == p.getCategoryId()
|
| - |
|
1100 |
&& EscalationType.L1.equals(p.getEscalationType()));
|
| - |
|
1101 |
boolean isL2 = positions.stream()
|
| - |
|
1102 |
.anyMatch(p -> ProfitMandiConstants.TICKET_CATEGORY_RBM == p.getCategoryId()
|
| - |
|
1103 |
&& EscalationType.L2.equals(p.getEscalationType()));
|
| - |
|
1104 |
|
| - |
|
1105 |
// Get fofo IDs for this RBM
|
| - |
|
1106 |
Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMapping();
|
| - |
|
1107 |
List<Integer> fofoIdList = new ArrayList<>();
|
| - |
|
1108 |
|
| - |
|
1109 |
if (isL2) {
|
| - |
|
1110 |
// L2: get fofo IDs from escalated tickets
|
| - |
|
1111 |
List<Ticket> escalatedTickets = ticketRepository.selectOpenEscalatedTicketsByAuthIds(Collections.singletonList(authId));
|
| - |
|
1112 |
fofoIdList = escalatedTickets.stream()
|
| - |
|
1113 |
.filter(t -> t.getL2AuthUser() == authId
|
| - |
|
1114 |
|| t.getL3AuthUser() == authId
|
| - |
|
1115 |
|| t.getL4AuthUser() == authId
|
| - |
|
1116 |
|| t.getL5AuthUser() == authId)
|
| - |
|
1117 |
.map(Ticket::getFofoId)
|
| - |
|
1118 |
.distinct()
|
| - |
|
1119 |
.collect(Collectors.toList());
|
| - |
|
1120 |
} else if (storeGuyMap.containsKey(authUser.getEmailId())) {
|
| - |
|
1121 |
fofoIdList = new ArrayList<>(storeGuyMap.get(authUser.getEmailId()));
|
| - |
|
1122 |
}
|
| - |
|
1123 |
|
| - |
|
1124 |
if (fofoIdList.isEmpty()) {
|
| - |
|
1125 |
return rows;
|
| - |
|
1126 |
}
|
| - |
|
1127 |
|
| - |
|
1128 |
LocalDateTime startDate = LocalDate.now().atStartOfDay();
|
| - |
|
1129 |
LocalDate firstOfMonth = LocalDate.now().withDayOfMonth(1);
|
| - |
|
1130 |
LocalDate endOfMonth = LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()).plusDays(1);
|
| - |
|
1131 |
|
| - |
|
1132 |
// Get fofo stores
|
| - |
|
1133 |
Map<Integer, FofoStore> fofoStoresMap = new HashMap<>();
|
| - |
|
1134 |
try {
|
| - |
|
1135 |
fofoStoresMap = fofoStoreRepository.selectByRetailerIds(fofoIdList).stream()
|
| - |
|
1136 |
.collect(Collectors.toMap(FofoStore::getId, x -> x, (a, b) -> a));
|
| - |
|
1137 |
} catch (ProfitMandiBusinessException e) {
|
| - |
|
1138 |
LOGGER.error("Error fetching fofo stores", e);
|
| - |
|
1139 |
}
|
| - |
|
1140 |
|
| - |
|
1141 |
// For L1 RBMs, filter escalated partners
|
| - |
|
1142 |
if (isL1) {
|
| - |
|
1143 |
List<Integer> allRemarkIds = partnerCollectionRemarkRepository.selectMaxRemarkId(fofoIdList);
|
| - |
|
1144 |
if (!allRemarkIds.isEmpty()) {
|
| - |
|
1145 |
Map<Integer, PartnerCollectionRemark> partnerCollectionRemarks = partnerCollectionRemarkRepository.selectByIds(allRemarkIds).stream()
|
| - |
|
1146 |
.collect(Collectors.toMap(PartnerCollectionRemark::getFofoId, x -> x, (a, b) -> a));
|
| - |
|
1147 |
fofoIdList = partnerCollectionRemarks.entrySet().stream()
|
| - |
|
1148 |
.filter(entry -> {
|
| - |
|
1149 |
PartnerCollectionRemark pcrMap = entry.getValue();
|
| - |
|
1150 |
return !(CollectionRemark.RBM_L2_ESCALATION.equals(pcrMap.getRemark())
|
| - |
|
1151 |
|| CollectionRemark.SALES_ESCALATION.equals(pcrMap.getRemark()));
|
| - |
|
1152 |
})
|
| - |
|
1153 |
.map(Map.Entry::getKey)
|
| - |
|
1154 |
.collect(Collectors.toList());
|
| - |
|
1155 |
}
|
| - |
|
1156 |
}
|
| - |
|
1157 |
|
| - |
|
1158 |
// Collection rank map
|
| - |
|
1159 |
Map<Integer, Integer> collectionRankMap = new HashMap<>();
|
| - |
|
1160 |
try {
|
| - |
|
1161 |
collectionRankMap = partnerCollectionService.getCollectionRankMap(fofoIdList, startDate);
|
| - |
|
1162 |
} catch (ProfitMandiBusinessException e) {
|
| - |
|
1163 |
LOGGER.error("Error fetching collection rank map", e);
|
| - |
|
1164 |
}
|
| - |
|
1165 |
|
| - |
|
1166 |
// MTD billing data
|
| - |
|
1167 |
List<RbmWeeklyBillingModel> mtdBillingData = getWeeklyBillingDataForMonth(firstOfMonth, endOfMonth);
|
| - |
|
1168 |
Set<Integer> mtdBilledFofoIds = mtdBillingData.stream()
|
| - |
|
1169 |
.filter(RbmWeeklyBillingModel::isMtdBilled)
|
| - |
|
1170 |
.map(RbmWeeklyBillingModel::getFofoId)
|
| - |
|
1171 |
.collect(Collectors.toSet());
|
| - |
|
1172 |
|
| - |
|
1173 |
// Filter to valid fofo IDs (external, ACTIVE, has collection plan)
|
| - |
|
1174 |
Map<Integer, Integer> finalCollectionRankMap = collectionRankMap;
|
| - |
|
1175 |
Map<Integer, FofoStore> finalFofoStoresMap = fofoStoresMap;
|
| - |
|
1176 |
List<Integer> validFofoIds = fofoIdList.stream()
|
| - |
|
1177 |
.filter(fofoId -> {
|
| - |
|
1178 |
FofoStore store = finalFofoStoresMap.get(fofoId);
|
| - |
|
1179 |
if (store == null || store.isInternal()) return false;
|
| - |
|
1180 |
if (!ActivationType.ACTIVE.equals(store.getActivationType())) return false;
|
| - |
|
1181 |
return finalCollectionRankMap.containsKey(fofoId);
|
| - |
|
1182 |
})
|
| - |
|
1183 |
.collect(Collectors.toList());
|
| - |
|
1184 |
|
| - |
|
1185 |
// Resolve partner names/codes
|
| - |
|
1186 |
Map<Integer, CustomRetailer> retailerMap = Collections.emptyMap();
|
| - |
|
1187 |
if (!validFofoIds.isEmpty()) {
|
| - |
|
1188 |
try {
|
| - |
|
1189 |
retailerMap = retailerService.getFofoRetailers(validFofoIds);
|
| - |
|
1190 |
} catch (ProfitMandiBusinessException e) {
|
| - |
|
1191 |
LOGGER.error("Error fetching fofo retailers for raw data", e);
|
| - |
|
1192 |
}
|
| - |
|
1193 |
}
|
| - |
|
1194 |
|
| - |
|
1195 |
String rbmName = authUser.getFullName() + (isL2 ? " (L2)" : "");
|
| - |
|
1196 |
|
| - |
|
1197 |
// Build rows
|
| - |
|
1198 |
for (Integer fofoId : validFofoIds) {
|
| - |
|
1199 |
int rank = collectionRankMap.getOrDefault(fofoId, 5);
|
| - |
|
1200 |
boolean hasZeroBilling = !mtdBilledFofoIds.contains(fofoId);
|
| - |
|
1201 |
|
| - |
|
1202 |
String status;
|
| - |
|
1203 |
if (rank == 1) {
|
| - |
|
1204 |
status = "Plan Today";
|
| - |
|
1205 |
} else if (rank == 2) {
|
| - |
|
1206 |
status = "Carry Forward";
|
| - |
|
1207 |
} else if (rank == 3) {
|
| - |
|
1208 |
status = "Untouched";
|
| - |
|
1209 |
} else if (hasZeroBilling) {
|
| - |
|
1210 |
status = "Zero Billing";
|
| - |
|
1211 |
} else if (rank == 4) {
|
| - |
|
1212 |
status = "Future Plan";
|
| - |
|
1213 |
} else {
|
| - |
|
1214 |
status = "Normal";
|
| - |
|
1215 |
}
|
| - |
|
1216 |
|
| - |
|
1217 |
CustomRetailer retailer = retailerMap.get(fofoId);
|
| - |
|
1218 |
String partnerName = retailer != null ? retailer.getBusinessName() : "Unknown (" + fofoId + ")";
|
| - |
|
1219 |
String partnerCode = retailer != null ? retailer.getCode() : "-";
|
| - |
|
1220 |
|
| - |
|
1221 |
rows.add(Arrays.asList(partnerName, partnerCode, status, rbmName));
|
| - |
|
1222 |
}
|
| - |
|
1223 |
|
| - |
|
1224 |
return rows;
|
| - |
|
1225 |
}
|
| 1088 |
|
1226 |
|
| 1089 |
}
|
1227 |
}
|