Subversion Repositories SmartDukaan

Rev

Rev 26463 | Rev 26465 | 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
 
26464 amit.gupta 64
		Map<Integer, PartnerDailyInvestment> investmentMap = fofoRetailerMap.keySet().stream()
65
				.map(x->{
66
					try {
67
						return partnerInvestmentService.getInvestment(x,0);
68
					}catch(Exception e) {
69
						LOGGER.info("Could not get invetment summary for {}", x);
70
						return new PartnerDailyInvestment();
71
					}
72
					})
73
				.collect(Collectors.toMap(x->x.getFofoId(), x->x));
26460 amit.gupta 74
 
75
		for (int fofoId : fofoRetailerMap.keySet()) {
76
			int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
77
					curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
78
			int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
79
					curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
80
			int totalHygieneCount = hygieneCount + invalidHygieneCount;
81
 
82
			PartnerDetailModel pm = new PartnerDetailModel();
83
			pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId));
84
			pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId));
85
 
86
			pm.setInvestment(investmentMap.get(fofoId));
87
			pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
88
			pm.setHygiene(hygieneCount);
89
			pm.setTotalHygiene(totalHygieneCount);
90
 
91
			pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
92
			allPartnerStats.put(fofoId, pm);
26462 amit.gupta 93
			LOGGER.info("pm {}", pm);
94
 
26460 amit.gupta 95
		}
96
		return allPartnerStats;
97
	}
98
 
99
	@Override
26461 amit.gupta 100
	@Cacheable(value = "partnerAggregateStats", cacheManager = "oneDayCacheManager")
26460 amit.gupta 101
	public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels) throws ProfitMandiBusinessException {
102
		PartnerDetailModel pdm = new PartnerDetailModel();
103
		PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment(); 
104
		pdm.setInvestment(aggregateInvestment);
105
		double totallmtdAmount = 0;
106
		double totalmtdAmount = 0;
107
		int totalTicketCount = 0;
108
 
109
		int currentHygieneCount = 0;
110
		int currentTotalHygieneCount = 0;
111
		for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
112
			PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
113
			totallmtdAmount += partnerDetailModel.getLmtd();
114
			totalmtdAmount += partnerDetailModel.getMtd();
115
			totalTicketCount += partnerDetailModel.getTicket();
116
			currentHygieneCount += partnerDetailModel.getHygiene();
117
			currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
118
			aggregateInvestment.setActivatedStockAmount(aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
119
			aggregateInvestment.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
120
			aggregateInvestment.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
121
			aggregateInvestment.setReturnInTransitAmount(aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
122
			aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
123
			aggregateInvestment.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
124
			aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
125
		}
126
		pdm.setHygiene(currentHygieneCount);
127
		pdm.setTotalHygiene(currentTotalHygieneCount);
128
		pdm.setLmtd(totallmtdAmount);
129
		pdm.setMtd(totalmtdAmount);
130
		pdm.setTicket(totalTicketCount);
131
		return pdm;
132
	}
133
 
134
 
135
}