Subversion Repositories SmartDukaan

Rev

Rev 26462 | Rev 26464 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26460 amit.gupta 1
package com.spice.profitmandi.service;
2
 
3
import java.time.LocalDate;
4
import java.time.LocalDateTime;
5
import java.time.LocalTime;
26463 amit.gupta 6
import java.util.ArrayList;
26460 amit.gupta 7
import java.util.HashMap;
8
import java.util.List;
9
import java.util.Map;
26463 amit.gupta 10
import java.util.stream.Collectors;
26460 amit.gupta 11
 
26462 amit.gupta 12
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.Logger;
26460 amit.gupta 14
import org.springframework.beans.factory.annotation.Autowired;
26461 amit.gupta 15
import org.springframework.cache.annotation.Cacheable;
16
import org.springframework.stereotype.Component;
26460 amit.gupta 17
 
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
20
import com.spice.profitmandi.dao.model.PartnerDetailModel;
21
import com.spice.profitmandi.dao.repository.cs.CsService;
22
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
23
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
24
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
25
import com.spice.profitmandi.service.user.RetailerService;
26
 
26461 amit.gupta 27
@Component
26460 amit.gupta 28
public class PartnerStatsServiceImpl implements PartnerStatsService {
29
 
26462 amit.gupta 30
	private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);
31
 
26460 amit.gupta 32
	@Autowired
33
	RetailerService retailerService;
34
 
35
	@Autowired
36
	FofoOrderItemRepository fofoOrderItemRepository;
37
 
38
	@Autowired
39
	CsService csService;
40
 
41
	@Autowired
42
	TicketRepository ticketRepository;
43
 
44
	@Autowired
45
	PartnerInvestmentService partnerInvestmentService;
46
 
47
	@Autowired
48
	HygieneDataRepository hygieneDataRepository;
49
 
50
	@Override
26461 amit.gupta 51
	@Cacheable(value = "partnerStats", cacheManager = "oneDayCacheManager")
26460 amit.gupta 52
	public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {
53
		LocalDateTime curDate = LocalDate.now().atStartOfDay();
54
		Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(
55
				curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);
56
		Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumAmountGroupByRetailer(curDate.withDayOfMonth(1),
57
				curDate.with(LocalTime.MAX), 0, false);
58
 
59
		Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
60
 
61
		Map<Integer, String> fofoRetailerMap = retailerService.getAllFofoRetailerIdNameMap();
62
		Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
63
 
64
		Map<Integer, PartnerDailyInvestment> investmentMap = partnerInvestmentService
65
				.getInvestment(new ArrayList<>(fofoRetailerMap.keySet()), 0).stream()
66
				.collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
67
 
68
		for (int fofoId : fofoRetailerMap.keySet()) {
69
			int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
70
					curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
71
			int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
72
					curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
73
			int totalHygieneCount = hygieneCount + invalidHygieneCount;
74
 
75
			PartnerDetailModel pm = new PartnerDetailModel();
76
			pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId));
77
			pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId));
78
 
79
			pm.setInvestment(investmentMap.get(fofoId));
80
			pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
81
			pm.setHygiene(hygieneCount);
82
			pm.setTotalHygiene(totalHygieneCount);
83
 
84
			pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
85
			allPartnerStats.put(fofoId, pm);
26462 amit.gupta 86
			LOGGER.info("pm {}", pm);
87
 
26460 amit.gupta 88
		}
89
		return allPartnerStats;
90
	}
91
 
92
	@Override
26461 amit.gupta 93
	@Cacheable(value = "partnerAggregateStats", cacheManager = "oneDayCacheManager")
26460 amit.gupta 94
	public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels) throws ProfitMandiBusinessException {
95
		PartnerDetailModel pdm = new PartnerDetailModel();
96
		PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment(); 
97
		pdm.setInvestment(aggregateInvestment);
98
		double totallmtdAmount = 0;
99
		double totalmtdAmount = 0;
100
		int totalTicketCount = 0;
101
 
102
		int currentHygieneCount = 0;
103
		int currentTotalHygieneCount = 0;
104
		for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
105
			PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
106
			totallmtdAmount += partnerDetailModel.getLmtd();
107
			totalmtdAmount += partnerDetailModel.getMtd();
108
			totalTicketCount += partnerDetailModel.getTicket();
109
			currentHygieneCount += partnerDetailModel.getHygiene();
110
			currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
111
			aggregateInvestment.setActivatedStockAmount(aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
112
			aggregateInvestment.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
113
			aggregateInvestment.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
114
			aggregateInvestment.setReturnInTransitAmount(aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
115
			aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
116
			aggregateInvestment.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
117
			aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
118
		}
119
		pdm.setHygiene(currentHygieneCount);
120
		pdm.setTotalHygiene(currentTotalHygieneCount);
121
		pdm.setLmtd(totallmtdAmount);
122
		pdm.setMtd(totalmtdAmount);
123
		pdm.setTicket(totalTicketCount);
124
		return pdm;
125
	}
126
 
127
 
128
}