| 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;
|
| 27660 |
tejbeer |
6 |
import java.time.YearMonth;
|
| 26460 |
amit.gupta |
7 |
import java.util.HashMap;
|
|
|
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
| 27545 |
tejbeer |
10 |
import java.util.Set;
|
| 26463 |
amit.gupta |
11 |
import java.util.stream.Collectors;
|
| 26460 |
amit.gupta |
12 |
|
| 26462 |
amit.gupta |
13 |
import org.apache.logging.log4j.LogManager;
|
|
|
14 |
import org.apache.logging.log4j.Logger;
|
| 26460 |
amit.gupta |
15 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 26461 |
amit.gupta |
16 |
import org.springframework.stereotype.Component;
|
| 26460 |
amit.gupta |
17 |
|
|
|
18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 27548 |
tejbeer |
19 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 26479 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 26460 |
amit.gupta |
21 |
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
|
| 27548 |
tejbeer |
22 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 27545 |
tejbeer |
23 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
| 27548 |
tejbeer |
24 |
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
|
| 26460 |
amit.gupta |
25 |
import com.spice.profitmandi.dao.model.PartnerDetailModel;
|
|
|
26 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
|
|
27 |
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
|
| 26479 |
amit.gupta |
28 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 26460 |
amit.gupta |
29 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
|
|
30 |
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
|
| 27548 |
tejbeer |
31 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 27545 |
tejbeer |
32 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 26460 |
amit.gupta |
33 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
34 |
|
| 26461 |
amit.gupta |
35 |
@Component
|
| 26460 |
amit.gupta |
36 |
public class PartnerStatsServiceImpl implements PartnerStatsService {
|
|
|
37 |
|
| 26462 |
amit.gupta |
38 |
private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);
|
| 27545 |
tejbeer |
39 |
|
| 26460 |
amit.gupta |
40 |
@Autowired
|
|
|
41 |
RetailerService retailerService;
|
|
|
42 |
|
|
|
43 |
@Autowired
|
| 26479 |
amit.gupta |
44 |
FofoStoreRepository fofoStoreRepository;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
| 26460 |
amit.gupta |
47 |
FofoOrderItemRepository fofoOrderItemRepository;
|
|
|
48 |
|
|
|
49 |
@Autowired
|
|
|
50 |
CsService csService;
|
|
|
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
TicketRepository ticketRepository;
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
PartnerInvestmentService partnerInvestmentService;
|
|
|
57 |
|
|
|
58 |
@Autowired
|
|
|
59 |
HygieneDataRepository hygieneDataRepository;
|
|
|
60 |
|
| 27545 |
tejbeer |
61 |
@Autowired
|
|
|
62 |
UserWalletRepository userWalletRepository;
|
|
|
63 |
|
| 27548 |
tejbeer |
64 |
@Autowired
|
|
|
65 |
PartnerTypeChangeService partnerTypeChangeService;
|
|
|
66 |
|
| 26460 |
amit.gupta |
67 |
@Override
|
| 27628 |
tejbeer |
68 |
// @Cacheable(value = "partnerStats", cacheManager =
|
|
|
69 |
// "thirtyMinsTimeOutCacheManager")
|
| 26460 |
amit.gupta |
70 |
public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {
|
|
|
71 |
LocalDateTime curDate = LocalDate.now().atStartOfDay();
|
| 27124 |
amit.gupta |
72 |
Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
| 26460 |
amit.gupta |
73 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);
|
| 27124 |
amit.gupta |
74 |
Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1),
|
| 26460 |
amit.gupta |
75 |
curDate.with(LocalTime.MAX), 0, false);
|
| 27660 |
tejbeer |
76 |
int lengthOfMonth = YearMonth.from(curDate.minusMonths(1)).lengthOfMonth();
|
| 26460 |
amit.gupta |
77 |
|
| 27660 |
tejbeer |
78 |
Map<Integer, Double> lmsSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
|
|
79 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1).withDayOfMonth(lengthOfMonth), 0,
|
|
|
80 |
false);
|
|
|
81 |
|
| 27549 |
tejbeer |
82 |
Map<Integer, Double> todaytertiary = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate,
|
| 27545 |
tejbeer |
83 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
84 |
|
|
|
85 |
Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),
|
|
|
86 |
curDate.minusDays(1).with(LocalTime.MAX), 0, false);
|
|
|
87 |
|
| 26460 |
amit.gupta |
88 |
Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
|
|
|
89 |
|
| 26479 |
amit.gupta |
90 |
List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
|
| 27545 |
tejbeer |
91 |
Set<Integer> fofoIds = fofoStores.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
|
|
92 |
Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream()
|
|
|
93 |
.collect(Collectors.toMap(x -> x.getUserId(), x -> x));
|
|
|
94 |
|
| 26460 |
amit.gupta |
95 |
Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
|
|
|
96 |
|
| 27545 |
tejbeer |
97 |
Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {
|
|
|
98 |
try {
|
|
|
99 |
return partnerInvestmentService.getInvestment(x.getId(), 0);
|
|
|
100 |
} catch (Exception e) {
|
|
|
101 |
LOGGER.info("Could not get invetment summary for {}", x);
|
|
|
102 |
return new PartnerDailyInvestment();
|
|
|
103 |
}
|
|
|
104 |
}).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
| 26460 |
amit.gupta |
105 |
|
| 27545 |
tejbeer |
106 |
for (FofoStore store : fofoStores) {
|
| 26479 |
amit.gupta |
107 |
int fofoId = store.getId();
|
| 26460 |
amit.gupta |
108 |
int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
109 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
110 |
int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
111 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
112 |
int totalHygieneCount = hygieneCount + invalidHygieneCount;
|
| 27548 |
tejbeer |
113 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
|
|
|
114 |
|
|
|
115 |
Map<EscalationType, AuthUser> authuserEsclationTypeMap = csService
|
|
|
116 |
.getAuthUserAndEsclationByPartnerId(fofoId);
|
| 26460 |
amit.gupta |
117 |
PartnerDetailModel pm = new PartnerDetailModel();
|
| 27552 |
tejbeer |
118 |
pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId).intValue());
|
|
|
119 |
pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId).intValue());
|
| 27660 |
tejbeer |
120 |
pm.setLms(lmsSale.get(fofoId) == null ? 0 : lmsSale.get(fofoId).intValue());
|
| 27552 |
tejbeer |
121 |
pm.setTodayTertiary(todaytertiary.get(fofoId) == null ? 0 : todaytertiary.get(fofoId).intValue());
|
| 27545 |
tejbeer |
122 |
pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));
|
| 27559 |
tejbeer |
123 |
pm.setWalletAmount(userWallet.get(fofoId) == null ? 0 : userWallet.get(fofoId).getAmount());
|
| 26460 |
amit.gupta |
124 |
pm.setInvestment(investmentMap.get(fofoId));
|
|
|
125 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
126 |
pm.setHygiene(hygieneCount);
|
| 27548 |
tejbeer |
127 |
pm.setPartnerType(partnerType);
|
| 27549 |
tejbeer |
128 |
if (authuserEsclationTypeMap.get(EscalationType.L1) == null) {
|
| 27545 |
tejbeer |
129 |
|
| 27549 |
tejbeer |
130 |
if (authuserEsclationTypeMap.get(EscalationType.L2) == null) {
|
|
|
131 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L3).getName());
|
|
|
132 |
} else {
|
|
|
133 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L2).getName());
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
} else {
|
|
|
137 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L1).getName());
|
|
|
138 |
}
|
| 26460 |
amit.gupta |
139 |
pm.setTotalHygiene(totalHygieneCount);
|
|
|
140 |
|
|
|
141 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
142 |
allPartnerStats.put(fofoId, pm);
|
| 26462 |
amit.gupta |
143 |
LOGGER.info("pm {}", pm);
|
| 27545 |
tejbeer |
144 |
|
| 26460 |
amit.gupta |
145 |
}
|
|
|
146 |
return allPartnerStats;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
@Override
|
| 27545 |
tejbeer |
150 |
// @Cacheable(value = "partnerAggregateStats", cacheManager =
|
|
|
151 |
// "oneDayCacheManager")
|
|
|
152 |
public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)
|
|
|
153 |
throws ProfitMandiBusinessException {
|
| 26460 |
amit.gupta |
154 |
PartnerDetailModel pdm = new PartnerDetailModel();
|
| 27545 |
tejbeer |
155 |
PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();
|
| 26460 |
amit.gupta |
156 |
pdm.setInvestment(aggregateInvestment);
|
|
|
157 |
double totallmtdAmount = 0;
|
|
|
158 |
double totalmtdAmount = 0;
|
| 27550 |
tejbeer |
159 |
double totalTodayTertiary = 0;
|
| 26460 |
amit.gupta |
160 |
int totalTicketCount = 0;
|
|
|
161 |
|
|
|
162 |
int currentHygieneCount = 0;
|
|
|
163 |
int currentTotalHygieneCount = 0;
|
| 27632 |
tejbeer |
164 |
if (partnerDetailModels != null && !partnerDetailModels.isEmpty()) {
|
|
|
165 |
for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
|
| 27635 |
tejbeer |
166 |
if (partnerDetailModel != null) {
|
|
|
167 |
PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
|
|
|
168 |
totallmtdAmount += partnerDetailModel.getLmtd();
|
|
|
169 |
totalmtdAmount += partnerDetailModel.getMtd();
|
|
|
170 |
totalTicketCount += partnerDetailModel.getTicket();
|
|
|
171 |
totalTodayTertiary += partnerDetailModel.getTodayTertiary();
|
|
|
172 |
currentHygieneCount += partnerDetailModel.getHygiene();
|
|
|
173 |
currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
|
|
|
174 |
aggregateInvestment.setActivatedStockAmount(
|
|
|
175 |
aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
|
|
|
176 |
aggregateInvestment
|
|
|
177 |
.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
|
|
|
178 |
aggregateInvestment
|
|
|
179 |
.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
|
|
|
180 |
aggregateInvestment.setReturnInTransitAmount(
|
|
|
181 |
aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
|
|
|
182 |
aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
|
|
|
183 |
aggregateInvestment
|
|
|
184 |
.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
|
|
|
185 |
aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
pdm.setHygiene(currentHygieneCount);
|
|
|
189 |
pdm.setTotalHygiene(currentTotalHygieneCount);
|
|
|
190 |
pdm.setLmtd((int) totallmtdAmount);
|
|
|
191 |
pdm.setMtd((int) totalmtdAmount);
|
|
|
192 |
pdm.setTicket((int) totalTicketCount);
|
|
|
193 |
pdm.setTodayTertiary((int) totalTodayTertiary);
|
|
|
194 |
pdm.setCount(partnerDetailModels.size());
|
| 27632 |
tejbeer |
195 |
}
|
| 26460 |
amit.gupta |
196 |
}
|
|
|
197 |
return pdm;
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
}
|