| Line 604... |
Line 604... |
| 604 |
|
604 |
|
| 605 |
@Override
|
605 |
@Override
|
| 606 |
public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) throws ProfitMandiBusinessException {
|
606 |
public Map<Integer, List<CustomRetailer>> getPositionCustomRetailerMap(List<Position> positions) throws ProfitMandiBusinessException {
|
| 607 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
|
607 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
|
| 608 |
Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
|
608 |
Map<Integer, List<CustomRetailer>> positionRetailerMap = new HashMap<>();
|
| - |
|
609 |
|
| - |
|
610 |
// Batch fetch all PartnerPositions upfront to avoid N+1 queries
|
| - |
|
611 |
List<Integer> positionIds = positions.stream().map(Position::getId).collect(Collectors.toList());
|
| - |
|
612 |
Map<Integer, List<Integer>> positionIdToFofoIdsMap = partnerPositionRepository.selectByPositionIds(positionIds)
|
| - |
|
613 |
.stream()
|
| - |
|
614 |
.collect(Collectors.groupingBy(
|
| - |
|
615 |
pp -> pp.getPositionId(),
|
| - |
|
616 |
Collectors.mapping(pp -> pp.getFofoId(), Collectors.toList())
|
| - |
|
617 |
));
|
| - |
|
618 |
|
| - |
|
619 |
// Collect all region IDs that might need lookup (positions with fofoId=0)
|
| - |
|
620 |
Set<Integer> regionIdsNeedingLookup = new HashSet<>();
|
| 609 |
for (Position position : positions) {
|
621 |
for (Position position : positions) {
|
| - |
|
622 |
List<Integer> fofoIds = positionIdToFofoIdsMap.get(position.getId());
|
| - |
|
623 |
if (fofoIds != null && fofoIds.contains(0)) {
|
| - |
|
624 |
regionIdsNeedingLookup.add(position.getRegionId());
|
| - |
|
625 |
}
|
| - |
|
626 |
}
|
| - |
|
627 |
|
| - |
|
628 |
// Batch fetch all PartnerRegions for regions needing lookup
|
| - |
|
629 |
Map<Integer, List<Integer>> regionIdToFofoIdsMap = new HashMap<>();
|
| - |
|
630 |
if (!regionIdsNeedingLookup.isEmpty()) {
|
| 610 |
List<Integer> fofoIds = partnerPositionRepository.selectByPositionId(position.getId()).stream().map(x -> x.getFofoId()).collect(Collectors.toList());
|
631 |
regionIdToFofoIdsMap = partnerRegionRepository.selectAllByRegionIds(new ArrayList<>(regionIdsNeedingLookup))
|
| - |
|
632 |
.stream()
|
| - |
|
633 |
.collect(Collectors.groupingBy(
|
| - |
|
634 |
pr -> pr.getRegionId(),
|
| - |
|
635 |
Collectors.mapping(pr -> pr.getFofoId(), Collectors.toList())
|
| - |
|
636 |
));
|
| - |
|
637 |
}
|
| 611 |
|
638 |
|
| 612 |
//LOGGER.info("fofoIds - {}", fofoIds);
|
639 |
// Build the result map using pre-fetched data
|
| 613 |
if (!fofoIds.isEmpty()) {
|
640 |
for (Position position : positions) {
|
| 614 |
if (fofoIds.contains(0)) {
|
641 |
List<Integer> fofoIds = positionIdToFofoIdsMap.get(position.getId());
|
| 615 |
fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream().map(x -> x.getFofoId()).collect(Collectors.toList());
|
- |
|
| 616 |
if (fofoIds.contains(0)) {
|
642 |
if (fofoIds == null || fofoIds.isEmpty()) {
|
| 617 |
fofoIds = new ArrayList<>(customRetailerMap.keySet());
|
643 |
continue;
|
| 618 |
}
|
644 |
}
|
| 619 |
|
645 |
|
| - |
|
646 |
if (fofoIds.contains(0)) {
|
| - |
|
647 |
// Need region-based lookup
|
| - |
|
648 |
fofoIds = regionIdToFofoIdsMap.get(position.getRegionId());
|
| - |
|
649 |
if (fofoIds == null || fofoIds.contains(0)) {
|
| - |
|
650 |
fofoIds = new ArrayList<>(customRetailerMap.keySet());
|
| 620 |
}
|
651 |
}
|
| 621 |
//LOGGER.info("fofoIds - {}", fofoIds);
|
- |
|
| 622 |
positionRetailerMap.put(position.getId(), fofoIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null).collect(Collectors.toList()));
|
- |
|
| 623 |
|
- |
|
| 624 |
}
|
652 |
}
|
| - |
|
653 |
|
| - |
|
654 |
positionRetailerMap.put(position.getId(),
|
| - |
|
655 |
fofoIds.stream()
|
| - |
|
656 |
.map(customRetailerMap::get)
|
| - |
|
657 |
.filter(x -> x != null)
|
| - |
|
658 |
.collect(Collectors.toList()));
|
| 625 |
}
|
659 |
}
|
| 626 |
return positionRetailerMap;
|
660 |
return positionRetailerMap;
|
| 627 |
}
|
661 |
}
|
| 628 |
|
662 |
|
| 629 |
@Override
|
663 |
@Override
|
| 630 |
public Map<Integer, List<CustomRetailer>> getRegionPartners(List<Position> positions) throws ProfitMandiBusinessException {
|
664 |
public Map<Integer, List<CustomRetailer>> getRegionPartners(List<Position> positions) throws ProfitMandiBusinessException {
|
| 631 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
|
665 |
Map<Integer, CustomRetailer> customRetailerMap = retailerService.getAllFofoRetailers();
|
| 632 |
Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
|
666 |
Map<Integer, List<CustomRetailer>> positionIdAndpartnerRegionMap = new HashedMap<>();
|
| 633 |
for (Position position : positions) {
|
- |
|
| 634 |
List<Integer> fofoIds = partnerRegionRepository.selectByRegionId(position.getRegionId()).stream().map(x -> x.getFofoId()).collect(Collectors.toList());
|
- |
|
| 635 |
|
667 |
|
| - |
|
668 |
// Batch fetch all PartnerRegions upfront to avoid N+1 queries
|
| - |
|
669 |
List<Integer> regionIds = positions.stream().map(Position::getRegionId).distinct().collect(Collectors.toList());
|
| - |
|
670 |
Map<Integer, List<Integer>> regionIdToFofoIdsMap = partnerRegionRepository.selectAllByRegionIds(regionIds)
|
| 636 |
if (!fofoIds.isEmpty()) {
|
671 |
.stream()
|
| - |
|
672 |
.collect(Collectors.groupingBy(
|
| 637 |
if (fofoIds.contains(0)) {
|
673 |
pr -> pr.getRegionId(),
|
| 638 |
fofoIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
|
674 |
Collectors.mapping(pr -> pr.getFofoId(), Collectors.toList())
|
| - |
|
675 |
));
|
| 639 |
|
676 |
|
| - |
|
677 |
// Fetch all store IDs once in case any region has fofoId=0
|
| - |
|
678 |
List<Integer> allStoreIds = null;
|
| - |
|
679 |
|
| - |
|
680 |
for (Position position : positions) {
|
| - |
|
681 |
List<Integer> fofoIds = regionIdToFofoIdsMap.get(position.getRegionId());
|
| - |
|
682 |
if (fofoIds == null || fofoIds.isEmpty()) {
|
| - |
|
683 |
continue;
|
| 640 |
}
|
684 |
}
|
| 641 |
|
685 |
|
| - |
|
686 |
if (fofoIds.contains(0)) {
|
| - |
|
687 |
if (allStoreIds == null) {
|
| 642 |
Map<Integer, CustomRetailer> fofoRetailers = fofoIds.stream().map(x -> customRetailerMap.get(x)).filter(x -> x != null).collect(Collectors.toList()).stream().collect(Collectors.toMap(x -> x.getPartnerId(), x -> x));
|
688 |
allStoreIds = fofoStoreRepository.selectAll().stream().map(x -> x.getId()).collect(Collectors.toList());
|
| - |
|
689 |
}
|
| 643 |
positionIdAndpartnerRegionMap.put(position.getRegionId(), new ArrayList<CustomRetailer>(fofoRetailers.values()));
|
690 |
fofoIds = allStoreIds;
|
| 644 |
}
|
691 |
}
|
| 645 |
|
692 |
|
| - |
|
693 |
Map<Integer, CustomRetailer> fofoRetailers = fofoIds.stream()
|
| - |
|
694 |
.map(customRetailerMap::get)
|
| - |
|
695 |
.filter(x -> x != null)
|
| - |
|
696 |
.collect(Collectors.toMap(x -> x.getPartnerId(), x -> x, (a, b) -> a));
|
| - |
|
697 |
positionIdAndpartnerRegionMap.put(position.getRegionId(), new ArrayList<>(fofoRetailers.values()));
|
| 646 |
}
|
698 |
}
|
| 647 |
|
699 |
|
| 648 |
return positionIdAndpartnerRegionMap;
|
700 |
return positionIdAndpartnerRegionMap;
|
| 649 |
}
|
701 |
}
|
| 650 |
|
702 |
|
| 651 |
@Override
|
703 |
@Override
|
| 652 |
@Cacheable(value = "authUserEmailMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
|
704 |
@Cacheable(value = "authUserEmailMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
|
| 653 |
public Map<String, Set<String>> getAuthUserPartnerEmailMapping() throws ProfitMandiBusinessException {
|
705 |
public Map<String, Set<String>> getAuthUserPartnerEmailMapping() throws ProfitMandiBusinessException {
|
| 654 |
Map<String, Set<String>> storeGuyMap = new HashMap<>();
|
706 |
Map<String, Set<String>> storeGuyMap = new HashMap<>();
|
| 655 |
Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toSet());
|
707 |
Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toSet());
|
| - |
|
708 |
|
| - |
|
709 |
// Batch fetch positions by category IDs instead of multiple calls
|
| 656 |
List<Position> categoryPositions = positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
|
710 |
List<Position> categoryPositions = positionRepository.selectPositionByCategoryIds(Arrays.asList(
|
| - |
|
711 |
ProfitMandiConstants.TICKET_CATEGORY_SALES,
|
| 657 |
categoryPositions.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
|
712 |
ProfitMandiConstants.TICKET_CATEGORY_RBM,
|
| 658 |
categoryPositions.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_ABM));
|
713 |
ProfitMandiConstants.TICKET_CATEGORY_ABM));
|
| 659 |
Map<Integer, Position> positionsMap = categoryPositions.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
714 |
Map<Integer, Position> positionsMap = categoryPositions.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 660 |
|
715 |
|
| - |
|
716 |
// Batch fetch all AuthUsers upfront to avoid N+1 queries
|
| - |
|
717 |
Set<Integer> authUserIds = categoryPositions.stream().map(Position::getAuthUserId).collect(Collectors.toSet());
|
| - |
|
718 |
Map<Integer, AuthUser> authUserMap = authRepository.selectByIds(new ArrayList<>(authUserIds)).stream()
|
| - |
|
719 |
.collect(Collectors.toMap(AuthUser::getId, x -> x));
|
| - |
|
720 |
|
| 661 |
Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
|
721 |
Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
|
| 662 |
for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
|
722 |
for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
|
| 663 |
int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
|
723 |
int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
|
| 664 |
Set<String> partnerEmails = positionPartnerEntry.getValue().stream().filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail()).collect(Collectors.toSet());
|
724 |
Set<String> partnerEmails = positionPartnerEntry.getValue().stream().filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getEmail()).collect(Collectors.toSet());
|
| 665 |
AuthUser authUser = authRepository.selectById(authUserId);
|
725 |
AuthUser authUser = authUserMap.get(authUserId);
|
| 666 |
if (authUser.isActive()) {
|
726 |
if (authUser != null && authUser.isActive()) {
|
| 667 |
if (!storeGuyMap.containsKey(authUser.getEmailId())) {
|
727 |
if (!storeGuyMap.containsKey(authUser.getEmailId())) {
|
| 668 |
storeGuyMap.put(authUser.getEmailId(), partnerEmails);
|
728 |
storeGuyMap.put(authUser.getEmailId(), partnerEmails);
|
| 669 |
} else {
|
729 |
} else {
|
| 670 |
storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
|
730 |
storeGuyMap.get(authUser.getEmailId()).addAll(partnerEmails);
|
| 671 |
}
|
731 |
}
|
| Line 677... |
Line 737... |
| 677 |
@Override
|
737 |
@Override
|
| 678 |
@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
|
738 |
@Cacheable(value = "authUserEmailPartnerIdMapping", cacheManager = "thirtyMinsTimeOutCacheManager")
|
| 679 |
public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() throws ProfitMandiBusinessException {
|
739 |
public Map<String, Set<Integer>> getAuthUserPartnerIdMapping() throws ProfitMandiBusinessException {
|
| 680 |
Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
|
740 |
Map<String, Set<Integer>> storeGuyMap = new HashMap<>();
|
| 681 |
Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toSet());
|
741 |
Set<Integer> activeFofoIds = fofoStoreRepository.selectActiveStores().stream().map(x -> x.getId()).collect(Collectors.toSet());
|
| - |
|
742 |
|
| - |
|
743 |
// Batch fetch positions by category IDs instead of multiple calls
|
| 682 |
List<Position> categoryPositions = positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_SALES);
|
744 |
List<Position> categoryPositions = positionRepository.selectPositionByCategoryIds(Arrays.asList(
|
| - |
|
745 |
ProfitMandiConstants.TICKET_CATEGORY_SALES,
|
| 683 |
categoryPositions.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_RBM));
|
746 |
ProfitMandiConstants.TICKET_CATEGORY_RBM,
|
| 684 |
categoryPositions.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_ABM));
|
747 |
ProfitMandiConstants.TICKET_CATEGORY_ABM,
|
| 685 |
categoryPositions.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_BUSINESSINTELLIGENT));
|
748 |
ProfitMandiConstants.TICKET_CATEGORY_BUSINESSINTELLIGENT,
|
| 686 |
categoryPositions.addAll(positionRepository.selectPositionByCategoryId(ProfitMandiConstants.TICKET_CATEGORY_TRAINING));
|
749 |
ProfitMandiConstants.TICKET_CATEGORY_TRAINING));
|
| 687 |
Map<Integer, Position> positionsMap = categoryPositions.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
750 |
Map<Integer, Position> positionsMap = categoryPositions.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));
|
| 688 |
|
751 |
|
| - |
|
752 |
// Batch fetch all AuthUsers upfront to avoid N+1 queries
|
| - |
|
753 |
Set<Integer> authUserIds = categoryPositions.stream().map(Position::getAuthUserId).collect(Collectors.toSet());
|
| - |
|
754 |
Map<Integer, AuthUser> authUserMap = authRepository.selectByIds(new ArrayList<>(authUserIds)).stream()
|
| - |
|
755 |
.collect(Collectors.toMap(AuthUser::getId, x -> x));
|
| - |
|
756 |
|
| 689 |
Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
|
757 |
Map<Integer, List<CustomRetailer>> positonPartnerMap = this.getPositionCustomRetailerMap(categoryPositions);
|
| 690 |
for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
|
758 |
for (Map.Entry<Integer, List<CustomRetailer>> positionPartnerEntry : positonPartnerMap.entrySet()) {
|
| 691 |
int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
|
759 |
int authUserId = positionsMap.get(positionPartnerEntry.getKey()).getAuthUserId();
|
| 692 |
Set<Integer> partnerIds = positionPartnerEntry.getValue().stream().filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId()).collect(Collectors.toSet());
|
760 |
Set<Integer> partnerIds = positionPartnerEntry.getValue().stream().filter(x -> activeFofoIds.contains(x.getPartnerId())).map(x -> x.getPartnerId()).collect(Collectors.toSet());
|
| 693 |
AuthUser authUser = authRepository.selectById(authUserId);
|
761 |
AuthUser authUser = authUserMap.get(authUserId);
|
| 694 |
if (authUser.isActive()) {
|
762 |
if (authUser != null && authUser.isActive()) {
|
| 695 |
if (!storeGuyMap.containsKey(authUser.getEmailId())) {
|
763 |
if (!storeGuyMap.containsKey(authUser.getEmailId())) {
|
| 696 |
storeGuyMap.put(authUser.getEmailId(), partnerIds);
|
764 |
storeGuyMap.put(authUser.getEmailId(), partnerIds);
|
| 697 |
} else {
|
765 |
} else {
|
| 698 |
storeGuyMap.get(authUser.getEmailId()).addAll(partnerIds);
|
766 |
storeGuyMap.get(authUser.getEmailId()).addAll(partnerIds);
|
| 699 |
}
|
767 |
}
|