Subversion Repositories SmartDukaan

Rev

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