| 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;
|
|
|
6 |
import java.util.HashMap;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
| 27545 |
tejbeer |
9 |
import java.util.Set;
|
| 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;
|
| 26479 |
amit.gupta |
19 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 26460 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
|
| 27545 |
tejbeer |
21 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
| 26460 |
amit.gupta |
22 |
import com.spice.profitmandi.dao.model.PartnerDetailModel;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
24 |
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
|
| 26479 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 26460 |
amit.gupta |
26 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
|
| 27545 |
tejbeer |
28 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 26460 |
amit.gupta |
29 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
30 |
|
| 26461 |
amit.gupta |
31 |
@Component
|
| 26460 |
amit.gupta |
32 |
public class PartnerStatsServiceImpl implements PartnerStatsService {
|
|
|
33 |
|
| 26462 |
amit.gupta |
34 |
private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);
|
| 27545 |
tejbeer |
35 |
|
| 26460 |
amit.gupta |
36 |
@Autowired
|
|
|
37 |
RetailerService retailerService;
|
|
|
38 |
|
|
|
39 |
@Autowired
|
| 26479 |
amit.gupta |
40 |
FofoStoreRepository fofoStoreRepository;
|
|
|
41 |
|
|
|
42 |
@Autowired
|
| 26460 |
amit.gupta |
43 |
FofoOrderItemRepository fofoOrderItemRepository;
|
|
|
44 |
|
|
|
45 |
@Autowired
|
|
|
46 |
CsService csService;
|
|
|
47 |
|
|
|
48 |
@Autowired
|
|
|
49 |
TicketRepository ticketRepository;
|
|
|
50 |
|
|
|
51 |
@Autowired
|
|
|
52 |
PartnerInvestmentService partnerInvestmentService;
|
|
|
53 |
|
|
|
54 |
@Autowired
|
|
|
55 |
HygieneDataRepository hygieneDataRepository;
|
|
|
56 |
|
| 27545 |
tejbeer |
57 |
@Autowired
|
|
|
58 |
UserWalletRepository userWalletRepository;
|
|
|
59 |
|
| 26460 |
amit.gupta |
60 |
@Override
|
| 27016 |
amit.gupta |
61 |
@Cacheable(value = "partnerStats", cacheManager = "thirtyMinsTimeOutCacheManager")
|
| 26460 |
amit.gupta |
62 |
public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {
|
|
|
63 |
LocalDateTime curDate = LocalDate.now().atStartOfDay();
|
| 27124 |
amit.gupta |
64 |
Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
| 26460 |
amit.gupta |
65 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);
|
| 27124 |
amit.gupta |
66 |
Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1),
|
| 26460 |
amit.gupta |
67 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
68 |
|
| 27545 |
tejbeer |
69 |
Map<Integer, Long> last1daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate,
|
|
|
70 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
71 |
|
|
|
72 |
Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),
|
|
|
73 |
curDate.minusDays(1).with(LocalTime.MAX), 0, false);
|
|
|
74 |
|
| 26460 |
amit.gupta |
75 |
Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
|
|
|
76 |
|
| 26479 |
amit.gupta |
77 |
List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
|
| 27545 |
tejbeer |
78 |
Set<Integer> fofoIds = fofoStores.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
|
|
79 |
Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream()
|
|
|
80 |
.collect(Collectors.toMap(x -> x.getUserId(), x -> x));
|
|
|
81 |
|
| 26460 |
amit.gupta |
82 |
Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
|
|
|
83 |
|
| 27545 |
tejbeer |
84 |
Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {
|
|
|
85 |
try {
|
|
|
86 |
return partnerInvestmentService.getInvestment(x.getId(), 0);
|
|
|
87 |
} catch (Exception e) {
|
|
|
88 |
LOGGER.info("Could not get invetment summary for {}", x);
|
|
|
89 |
return new PartnerDailyInvestment();
|
|
|
90 |
}
|
|
|
91 |
}).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
| 26460 |
amit.gupta |
92 |
|
| 27545 |
tejbeer |
93 |
for (FofoStore store : fofoStores) {
|
| 26479 |
amit.gupta |
94 |
int fofoId = store.getId();
|
| 26460 |
amit.gupta |
95 |
int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
96 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
97 |
int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
98 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
99 |
int totalHygieneCount = hygieneCount + invalidHygieneCount;
|
|
|
100 |
PartnerDetailModel pm = new PartnerDetailModel();
|
|
|
101 |
pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId));
|
|
|
102 |
pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId));
|
| 27545 |
tejbeer |
103 |
pm.setLastOneDayTertiary(last1daystertiary.get(fofoId) == null ? 0 : last1daystertiary.get(fofoId));
|
|
|
104 |
pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));
|
|
|
105 |
pm.setWalletAmount(userWallet.get(fofoId).getAmount() == 0 ? 0 : userWallet.get(fofoId).getAmount());
|
| 26460 |
amit.gupta |
106 |
pm.setInvestment(investmentMap.get(fofoId));
|
|
|
107 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
108 |
pm.setHygiene(hygieneCount);
|
| 27545 |
tejbeer |
109 |
|
|
|
110 |
pm.setAuthUser(csService.getAuthUserByPartnerId(fofoId).get(0) == null ? "not available"
|
|
|
111 |
: csService.getAuthUserByPartnerId(fofoId).get(0));
|
| 26460 |
amit.gupta |
112 |
pm.setTotalHygiene(totalHygieneCount);
|
|
|
113 |
|
|
|
114 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
115 |
allPartnerStats.put(fofoId, pm);
|
| 26462 |
amit.gupta |
116 |
LOGGER.info("pm {}", pm);
|
| 27545 |
tejbeer |
117 |
|
| 26460 |
amit.gupta |
118 |
}
|
|
|
119 |
return allPartnerStats;
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
@Override
|
| 27545 |
tejbeer |
123 |
// @Cacheable(value = "partnerAggregateStats", cacheManager =
|
|
|
124 |
// "oneDayCacheManager")
|
|
|
125 |
public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)
|
|
|
126 |
throws ProfitMandiBusinessException {
|
| 26460 |
amit.gupta |
127 |
PartnerDetailModel pdm = new PartnerDetailModel();
|
| 27545 |
tejbeer |
128 |
PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();
|
| 26460 |
amit.gupta |
129 |
pdm.setInvestment(aggregateInvestment);
|
|
|
130 |
double totallmtdAmount = 0;
|
|
|
131 |
double totalmtdAmount = 0;
|
|
|
132 |
int totalTicketCount = 0;
|
|
|
133 |
|
|
|
134 |
int currentHygieneCount = 0;
|
|
|
135 |
int currentTotalHygieneCount = 0;
|
|
|
136 |
for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
|
|
|
137 |
PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
|
|
|
138 |
totallmtdAmount += partnerDetailModel.getLmtd();
|
|
|
139 |
totalmtdAmount += partnerDetailModel.getMtd();
|
|
|
140 |
totalTicketCount += partnerDetailModel.getTicket();
|
|
|
141 |
currentHygieneCount += partnerDetailModel.getHygiene();
|
|
|
142 |
currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
|
| 27545 |
tejbeer |
143 |
aggregateInvestment.setActivatedStockAmount(
|
|
|
144 |
aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
|
|
|
145 |
aggregateInvestment
|
|
|
146 |
.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
|
| 26460 |
amit.gupta |
147 |
aggregateInvestment.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
|
| 27545 |
tejbeer |
148 |
aggregateInvestment.setReturnInTransitAmount(
|
|
|
149 |
aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
|
| 26460 |
amit.gupta |
150 |
aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
|
|
|
151 |
aggregateInvestment.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
|
|
|
152 |
aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
|
|
|
153 |
}
|
|
|
154 |
pdm.setHygiene(currentHygieneCount);
|
|
|
155 |
pdm.setTotalHygiene(currentTotalHygieneCount);
|
|
|
156 |
pdm.setLmtd(totallmtdAmount);
|
|
|
157 |
pdm.setMtd(totalmtdAmount);
|
|
|
158 |
pdm.setTicket(totalTicketCount);
|
| 26470 |
amit.gupta |
159 |
pdm.setCount(partnerDetailModels.size());
|
| 26460 |
amit.gupta |
160 |
return pdm;
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
}
|