| Line 892... |
Line 892... |
| 892 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSchemeSummaryMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(retailerId,
|
892 |
Map<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSchemeSummaryMap = tagListingRepository.getModelSchemesByCatalogIdsAndType(retailerId,
|
| 893 |
partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), fofoOrder.getCreateTimestamp());
|
893 |
partnerType, new ArrayList<>(catalogInventoryItemMap.keySet()), fofoOrder.getCreateTimestamp());
|
| 894 |
|
894 |
|
| 895 |
LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
|
895 |
LOGGER.info("catalogSchemeSummaryMap - {}", catalogSchemeSummaryMap);
|
| 896 |
|
896 |
|
| - |
|
897 |
// N+1 fix: Batch fetch all SchemeInOut records for all inventoryItems at once
|
| - |
|
898 |
Set<Integer> allInventoryItemIds = inventoryItems.stream().map(InventoryItem::getId).collect(Collectors.toSet());
|
| - |
|
899 |
List<SchemeInOut> allSchemeInOuts = schemeInOutRepository.selectByInventoryItemIds(allInventoryItemIds);
|
| - |
|
900 |
Map<Integer, List<SchemeInOut>> schemeInOutByInventoryItemId = allSchemeInOuts.stream()
|
| - |
|
901 |
.collect(Collectors.groupingBy(SchemeInOut::getInventoryItemId));
|
| 897 |
|
902 |
|
| 898 |
int count = 0;
|
903 |
int count = 0;
|
| 899 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
|
904 |
for (Map.Entry<CatalogSummaryModel, List<SchemeSummaryModel>> catalogSummaryModelListEntry : catalogSchemeSummaryMap.entrySet()) {
|
| 900 |
CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
|
905 |
CatalogSummaryModel catalogSummaryModel = catalogSummaryModelListEntry.getKey();
|
| 901 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(x -> x != null).collect(Collectors.toList());
|
906 |
List<SchemeSummaryModel> schemeSummaryModels = catalogSummaryModelListEntry.getValue().stream().filter(x -> x != null).collect(Collectors.toList());
|
| Line 906... |
Line 911... |
| 906 |
// Create map once per catalog instead of per inventoryItem
|
911 |
// Create map once per catalog instead of per inventoryItem
|
| 907 |
Map<Integer, SchemeSummaryModel> schemeSummaryModelMap = schemeSummaryModels.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
912 |
Map<Integer, SchemeSummaryModel> schemeSummaryModelMap = schemeSummaryModels.stream().collect(Collectors.toMap(x -> x.getSchemeId(), x -> x));
|
| 908 |
|
913 |
|
| 909 |
List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
|
914 |
List<InventoryItem> modelInventoryItems = catalogInventoryItemMap.get(catalogSummaryModel.getCatalogId());
|
| 910 |
for (InventoryItem inventoryItem : modelInventoryItems) {
|
915 |
for (InventoryItem inventoryItem : modelInventoryItems) {
|
| - |
|
916 |
// N+1 fix: Use pre-fetched schemeInOut map instead of querying per inventoryItem
|
| 911 |
List<SchemeInOut> sios = schemeInOutRepository.selectAll(inventoryItem.getId());
|
917 |
List<SchemeInOut> sios = schemeInOutByInventoryItemId.getOrDefault(inventoryItem.getId(), Collections.emptyList());
|
| 912 |
|
918 |
|
| 913 |
List<Integer> creditedSchemeIds = sios.stream()
|
919 |
List<Integer> creditedSchemeIds = sios.stream()
|
| 914 |
.filter(x -> x.getStatus().equals(SchemePayoutStatus.CREDITED))
|
920 |
.filter(x -> x.getStatus().equals(SchemePayoutStatus.CREDITED))
|
| 915 |
.map(x -> x.getSchemeId()).collect(Collectors.toList());
|
921 |
.map(x -> x.getSchemeId()).collect(Collectors.toList());
|
| 916 |
|
922 |
|