Rev 32436 | Rev 35460 | Go to most recent revision | 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.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();Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1),curDate.with(LocalTime.MAX), 0, false);Map<Integer, Double> lmsSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1), 0, false);Map<Integer, Double> todaytertiary = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate,curDate.with(LocalTime.MAX), 0, false);Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),curDate.minusDays(1).with(LocalTime.MAX), 0, false);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() <= 10).collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));LOGGER.info("investmentMaintainedDaysMap" + investmentMaintainedDaysMap);Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {try {return partnerInvestmentService.getInvestment(x.getId(), 0);} catch (Exception e) {LOGGER.info("Could not get invetment 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());for (FofoStore store : fofoStores) {int fofoId = store.getId();int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));int totalHygieneCount = hygieneCount + invalidHygieneCount;PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());Map<EscalationType, AuthUser> authuserEsclationTypeMap = csService.getAuthUserAndEsclationByPartnerId(fofoId);PartnerDetailModel pm = new PartnerDetailModel();pm.setFofoId(fofoId);pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId).intValue());pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId).intValue());pm.setLms(lmsSale.get(fofoId) == null ? 0 : lmsSale.get(fofoId).intValue());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(todaytertiary.get(fofoId) == null ? 0 : todaytertiary.get(fofoId).intValue());pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));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;}}