Rev 26464 | Rev 26466 | 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 java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.stream.Collectors;import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.Cacheable;import org.springframework.stereotype.Component;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;import com.spice.profitmandi.dao.model.PartnerDetailModel;import com.spice.profitmandi.dao.repository.cs.CsService;import com.spice.profitmandi.dao.repository.cs.TicketRepository;import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;import com.spice.profitmandi.service.user.RetailerService;@Componentpublic class PartnerStatsServiceImpl implements PartnerStatsService {private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);@AutowiredRetailerService retailerService;@AutowiredFofoOrderItemRepository fofoOrderItemRepository;@AutowiredCsService csService;@AutowiredTicketRepository ticketRepository;@AutowiredPartnerInvestmentService partnerInvestmentService;@AutowiredHygieneDataRepository hygieneDataRepository;@Override@Cacheable(value = "partnerStats", cacheManager = "oneDayCacheManager")public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {LocalDateTime curDate = LocalDate.now().atStartOfDay();Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(curDate.withDayOfMonth(1),curDate.with(LocalTime.MAX), 0, false);Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();Map<Integer, String> fofoRetailerMap = retailerService.getAllFofoRetailerIdNameMap();Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();Map<Integer, PartnerDailyInvestment> investmentMap = fofoRetailerMap.keySet().stream().map(x-> {PartnerDailyInvestment pdi = new PartnerDailyInvestment();pdi.setFofoId(x);return pdi;})/*.map(x->{try {return partnerInvestmentService.getInvestment(x,0);}catch(Exception e) {LOGGER.info("Could not get invetment summary for {}", x);return new PartnerDailyInvestment();}})*/.collect(Collectors.toMap(x->x.getFofoId(), x->x));for (int fofoId : fofoRetailerMap.keySet()) {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;PartnerDetailModel pm = new PartnerDetailModel();pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId));pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId));pm.setInvestment(investmentMap.get(fofoId));pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());pm.setHygiene(hygieneCount);pm.setTotalHygiene(totalHygieneCount);pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());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 totallmtdAmount = 0;double totalmtdAmount = 0;int totalTicketCount = 0;int currentHygieneCount = 0;int currentTotalHygieneCount = 0;for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();totallmtdAmount += partnerDetailModel.getLmtd();totalmtdAmount += partnerDetailModel.getMtd();totalTicketCount += partnerDetailModel.getTicket();currentHygieneCount += partnerDetailModel.getHygiene();currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();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.setLmtd(totallmtdAmount);pdm.setMtd(totalmtdAmount);pdm.setTicket(totalTicketCount);return pdm;}}