Rev 35537 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed
package com.spice.profitmandi.service;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.auth.AuthUser;import com.spice.profitmandi.dao.entity.fofo.FofoStore;import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;import com.spice.profitmandi.dao.entity.fofo.PartnerType;import com.spice.profitmandi.dao.entity.transaction.UserWallet;import com.spice.profitmandi.dao.enumuration.cs.EscalationType;import com.spice.profitmandi.dao.model.PartnerDetailModel;import com.spice.profitmandi.dao.repository.auth.AuthRepository;import com.spice.profitmandi.dao.repository.cs.CsService;import com.spice.profitmandi.dao.repository.cs.PositionRepository;import com.spice.profitmandi.dao.repository.cs.TicketRepository;import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;import com.spice.profitmandi.dao.repository.transaction.OrderRepository;import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;import com.spice.profitmandi.dao.model.PartnerTertiarySalesModel;import com.spice.profitmandi.service.user.RetailerService;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.util.*;import java.util.stream.Collectors;@Componentpublic class PartnerStatsServiceImpl implements PartnerStatsService {private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);@AutowiredRetailerService retailerService;@AutowiredFofoStoreRepository fofoStoreRepository;@AutowiredFofoOrderItemRepository fofoOrderItemRepository;@AutowiredCsService csService;@AutowiredTicketRepository ticketRepository;@AutowiredPartnerInvestmentService partnerInvestmentService;@AutowiredHygieneDataRepository hygieneDataRepository;@AutowiredUserWalletRepository userWalletRepository;@AutowiredPartnerTypeChangeService partnerTypeChangeService;@AutowiredOrderRepository orderRepository;@AutowiredAuthRepository authRepository;@AutowiredPartnerDailyInvestmentRepository partnerDailyInvestmentRepository;@AutowiredPositionRepository positionRepository;@Override// @Cacheable(value = "partnerStats", cacheManager =// "thirtyMinsTimeOutCacheManager")public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {LocalDateTime curDate = LocalDate.now().atStartOfDay();// Batch fetch all tertiary sales data in a single query (performance optimization)// Combines 5 separate queries: lmtdSale, mtdSale, lmsSale, todaytertiary, last3daystertiaryMap<Integer, PartnerTertiarySalesModel> tertiarySalesMap = fofoOrderItemRepository.selectPartnerTertiarySales(curDate.withDayOfMonth(1).minusMonths(1), // lmtdStartDatecurDate.with(LocalTime.MAX).minusMonths(1), // lmtdEndDatecurDate.withDayOfMonth(1), // mtdStartDatecurDate.with(LocalTime.MAX), // mtdEndDatecurDate.withDayOfMonth(1).minusMonths(1), // lmsStartDatecurDate.withDayOfMonth(1), // lmsEndDatecurDate, // todayStartDatecurDate.with(LocalTime.MAX), // todayEndDatecurDate.minusDays(4), // last3daysStartDatecurDate.minusDays(1).with(LocalTime.MAX) // last3daysEndDate);Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();Map<Integer, Double> secondaryMtd = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX));Map<Integer, Double> secondarylmtd = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1));Map<Integer, Double> secondarylms = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1));LOGGER.info("secondarylmtd" + secondarylmtd);LOGGER.info("secondarylms" + secondarylms);List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();Set<Integer> fofoIds = fofoStores.stream().filter(x -> !x.isInternal()).map(x -> x.getId()).collect(Collectors.toSet());Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream().collect(Collectors.toMap(x -> x.getUserId(), x -> x));Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository.selectAll(curDate.withDayOfMonth(1).toLocalDate(), curDate.toLocalDate());Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream().filter(x -> x.getShortPercentage() <= ProfitMandiConstants.OK_INVESTMENT_SHORT_PERCENTAGE).collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));LOGGER.info("investmentMaintainedDaysMap" + investmentMaintainedDaysMap);// Batch fetch investments for all fofoStores at once (performance optimization)Map<Integer, PartnerDailyInvestment> investmentMap;try {investmentMap = partnerInvestmentService.getInvestmentsForFofoStores(fofoStores);} catch (Exception e) {LOGGER.error("Error fetching investments in batch, falling back to individual calls", e);investmentMap = fofoStores.stream().map(x -> {try {return partnerInvestmentService.getInvestment(x.getId(), 0);} catch (Exception ex) {LOGGER.info("Could not get investment summary for {}", x);return new PartnerDailyInvestment();}}).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));}Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMappingByCategoryIds(Arrays.asList(ProfitMandiConstants.TICKET_CATEGORY_RBM), true);Map<String, AuthUser> authUserMap = authRepository.selectAll().stream().filter(x -> storeGuyMap.keySet().contains(x.getEmailId())).collect(Collectors.toMap(x -> x.getEmailId(), x -> x));Map<Integer, Set<AuthUser>> partnerRbmsMap = new HashMap<>();for (Map.Entry<String, Set<Integer>> storeGuySetEntry : storeGuyMap.entrySet()) {//storefor (Integer storeId : storeGuySetEntry.getValue()) {if (!partnerRbmsMap.containsKey(storeId)) {partnerRbmsMap.put(storeId, new HashSet<>());}partnerRbmsMap.get(storeId).add(authUserMap.get(storeGuySetEntry.getKey()));}}Set<Integer> l1Rbms = positionRepository.selectPositionbyCategoryIdAndEscalationType(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1).stream().map(x -> x.getAuthUserId()).collect(Collectors.toSet());Set<Integer> l2Rbms = positionRepository.selectPositionbyCategoryIdAndEscalationType(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L2).stream().map(x -> x.getAuthUserId()).collect(Collectors.toSet());// Batch fetch hygiene counts for all fofoIds at once (performance optimization)LocalDateTime hygieneStartDate = curDate.withDayOfMonth(1).minusMonths(1);LocalDateTime hygieneEndDate = curDate.plusMonths(1).withDayOfMonth(1);Map<Integer, Long> validHygieneCountMap = hygieneDataRepository.selectHygieneCountByFofoIds(fofoIds, true, hygieneStartDate, hygieneEndDate);Map<Integer, Long> invalidHygieneCountMap = hygieneDataRepository.selectHygieneCountByFofoIds(fofoIds, false, hygieneStartDate, hygieneEndDate);// Bulk fetch partner types to avoid N+1 queriesMap<Integer, PartnerType> partnerTypeMap = partnerTypeChangeService.getTypesForFofoIds(new ArrayList<>(fofoIds), LocalDate.now());// Batch fetch auth user escalations for all fofoIds at once (performance optimization)Map<Integer, Map<EscalationType, AuthUser>> authUserEscalationMap = csService.getAuthUserAndEsclationByPartnerIds(fofoIds);for (FofoStore store : fofoStores) {int fofoId = store.getId();// Use batch-fetched hygiene counts instead of N+1 queriesint hygieneCount = validHygieneCountMap.getOrDefault(fofoId, 0L).intValue();int invalidHygieneCount = invalidHygieneCountMap.getOrDefault(fofoId, 0L).intValue();int totalHygieneCount = hygieneCount + invalidHygieneCount;PartnerType partnerType = partnerTypeMap.get(fofoId);// Use batch-fetched auth user escalations instead of N+1 queriesMap<EscalationType, AuthUser> authuserEsclationTypeMap = authUserEscalationMap.getOrDefault(fofoId, new HashMap<>());PartnerDetailModel pm = new PartnerDetailModel();pm.setFofoId(fofoId);// Use batch-fetched tertiary sales data instead of 5 separate queriesPartnerTertiarySalesModel tertiarySales = tertiarySalesMap.get(fofoId);pm.setLmtd(tertiarySales == null ? 0 : (int) tertiarySales.getLmtdSale());pm.setMtd(tertiarySales == null ? 0 : (int) tertiarySales.getMtdSale());pm.setLms(tertiarySales == null ? 0 : (int) tertiarySales.getLmsSale());pm.setSecondarymtd(secondaryMtd.get(fofoId) == null ? 0 : secondaryMtd.get(fofoId).intValue());pm.setSecondarylmtd(secondarylmtd.get(fofoId) == null ? 0 : secondarylmtd.get(fofoId).intValue());pm.setSecondarylms(secondarylms.get(fofoId) == null ? 0 : secondarylms.get(fofoId).intValue());pm.setTodayTertiary(tertiarySales == null ? 0 : (int) tertiarySales.getTodaySale());pm.setLastThreeDaytertiary(tertiarySales == null ? 0 : tertiarySales.getLast3daysQty());pm.setWalletAmount(userWallet.get(fofoId) == null ? 0 : userWallet.get(fofoId).getAmount());pm.setInvestment(investmentMap.get(fofoId));pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());pm.setHygiene(hygieneCount);pm.setInvestment_ok(investmentMaintainedDaysMap.get(fofoId) == null ? 0 : investmentMaintainedDaysMap.get(fofoId));pm.setPartnerType(partnerType);if (authuserEsclationTypeMap.get(EscalationType.L1) != null) {pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L1).getName());} else if (authuserEsclationTypeMap.get(EscalationType.L2) != null) {pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L2).getName());} else if (authuserEsclationTypeMap.get(EscalationType.L3) != null) {pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L3).getName());} else if (authuserEsclationTypeMap.get(EscalationType.L4) != null) {pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L4).getName());} else {pm.setAuthUser(" - ");}pm.setTotalHygiene(totalHygieneCount);pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());Set<AuthUser> rbmAuths = partnerRbmsMap.get(fofoId);if (rbmAuths == null) {pm.setRbms("-");} else {pm.setRbms(rbmAuths.stream().filter(x -> l1Rbms.contains(x.getId())).map(x -> x.getFullName()).collect(Collectors.joining(",")));if (pm.getRbms().equals("")) {pm.setRbms(rbmAuths.stream().filter(x -> l2Rbms.contains(x.getId())).map(x -> x.getFullName()).collect(Collectors.joining(",")));}if (pm.getRbms().equals("")) {pm.setRbms("-");}}allPartnerStats.put(fofoId, pm);//LOGGER.info("pm {}", pm);}return allPartnerStats;}@Override// @Cacheable(value = "partnerAggregateStats", cacheManager =// "oneDayCacheManager")public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)throws ProfitMandiBusinessException {PartnerDetailModel pdm = new PartnerDetailModel();PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();pdm.setInvestment(aggregateInvestment);double totallmsAmount = 0;double totallmtdAmount = 0;double totalmtdAmount = 0;double totalTodayTertiary = 0;int totalTicketCount = 0;int currentHygieneCount = 0;int currentTotalHygieneCount = 0;if (partnerDetailModels != null && !partnerDetailModels.isEmpty()) {for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {if (partnerDetailModel != null) {PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();totallmsAmount += partnerDetailModel.getLms();totallmtdAmount += partnerDetailModel.getLmtd();totalmtdAmount += partnerDetailModel.getMtd();totalTicketCount += partnerDetailModel.getTicket();totalTodayTertiary += partnerDetailModel.getTodayTertiary();currentHygieneCount += partnerDetailModel.getHygiene();currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();if (pdi != null) {aggregateInvestment.setActivatedStockAmount(aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());aggregateInvestment.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());aggregateInvestment.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());aggregateInvestment.setReturnInTransitAmount(aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());aggregateInvestment.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());}}pdm.setHygiene(currentHygieneCount);pdm.setTotalHygiene(currentTotalHygieneCount);pdm.setLms((int) totallmsAmount);pdm.setLmtd((int) totallmtdAmount);pdm.setMtd((int) totalmtdAmount);pdm.setTicket((int) totalTicketCount);pdm.setTodayTertiary((int) totalTodayTertiary);pdm.setCount(partnerDetailModels.size());}}return pdm;}}