| 26460 |
amit.gupta |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
| 32429 |
amit.gupta |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 32421 |
amit.gupta |
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
5 |
import com.spice.profitmandi.common.util.Utils;
|
| 27548 |
tejbeer |
6 |
import com.spice.profitmandi.dao.entity.auth.AuthUser;
|
| 26479 |
amit.gupta |
7 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 26460 |
amit.gupta |
8 |
import com.spice.profitmandi.dao.entity.fofo.PartnerDailyInvestment;
|
| 27548 |
tejbeer |
9 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| 27545 |
tejbeer |
10 |
import com.spice.profitmandi.dao.entity.transaction.UserWallet;
|
| 27548 |
tejbeer |
11 |
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
|
| 26460 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.model.PartnerDetailModel;
|
| 32429 |
amit.gupta |
13 |
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
|
| 26460 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.repository.cs.CsService;
|
| 32430 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.repository.cs.PositionRepository;
|
| 26460 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.repository.cs.TicketRepository;
|
| 26479 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| 26460 |
amit.gupta |
18 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
|
|
19 |
import com.spice.profitmandi.dao.repository.fofo.HygieneDataRepository;
|
| 27903 |
tejbeer |
20 |
import com.spice.profitmandi.dao.repository.fofo.PartnerDailyInvestmentRepository;
|
| 27548 |
tejbeer |
21 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 27893 |
tejbeer |
22 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 27545 |
tejbeer |
23 |
import com.spice.profitmandi.dao.repository.transaction.UserWalletRepository;
|
| 26460 |
amit.gupta |
24 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 32429 |
amit.gupta |
25 |
import org.apache.logging.log4j.LogManager;
|
|
|
26 |
import org.apache.logging.log4j.Logger;
|
|
|
27 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
28 |
import org.springframework.stereotype.Component;
|
| 26460 |
amit.gupta |
29 |
|
| 32429 |
amit.gupta |
30 |
import java.time.LocalDate;
|
|
|
31 |
import java.time.LocalDateTime;
|
|
|
32 |
import java.time.LocalTime;
|
|
|
33 |
import java.util.*;
|
|
|
34 |
import java.util.stream.Collectors;
|
|
|
35 |
|
| 26461 |
amit.gupta |
36 |
@Component
|
| 26460 |
amit.gupta |
37 |
public class PartnerStatsServiceImpl implements PartnerStatsService {
|
|
|
38 |
|
| 26462 |
amit.gupta |
39 |
private static final Logger LOGGER = LogManager.getLogger(PartnerStatsServiceImpl.class);
|
| 27545 |
tejbeer |
40 |
|
| 26460 |
amit.gupta |
41 |
@Autowired
|
|
|
42 |
RetailerService retailerService;
|
|
|
43 |
|
|
|
44 |
@Autowired
|
| 26479 |
amit.gupta |
45 |
FofoStoreRepository fofoStoreRepository;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
| 26460 |
amit.gupta |
48 |
FofoOrderItemRepository fofoOrderItemRepository;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
CsService csService;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
|
|
54 |
TicketRepository ticketRepository;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
|
|
57 |
PartnerInvestmentService partnerInvestmentService;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
|
|
60 |
HygieneDataRepository hygieneDataRepository;
|
|
|
61 |
|
| 27545 |
tejbeer |
62 |
@Autowired
|
|
|
63 |
UserWalletRepository userWalletRepository;
|
|
|
64 |
|
| 27548 |
tejbeer |
65 |
@Autowired
|
|
|
66 |
PartnerTypeChangeService partnerTypeChangeService;
|
|
|
67 |
|
| 27893 |
tejbeer |
68 |
@Autowired
|
|
|
69 |
OrderRepository orderRepository;
|
|
|
70 |
|
| 27903 |
tejbeer |
71 |
@Autowired
|
| 32421 |
amit.gupta |
72 |
AuthRepository authRepository;
|
|
|
73 |
|
|
|
74 |
@Autowired
|
| 27903 |
tejbeer |
75 |
PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
|
|
|
76 |
|
| 32430 |
amit.gupta |
77 |
@Autowired
|
|
|
78 |
PositionRepository positionRepository;
|
|
|
79 |
|
| 26460 |
amit.gupta |
80 |
@Override
|
| 27628 |
tejbeer |
81 |
// @Cacheable(value = "partnerStats", cacheManager =
|
|
|
82 |
// "thirtyMinsTimeOutCacheManager")
|
| 26460 |
amit.gupta |
83 |
public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {
|
|
|
84 |
LocalDateTime curDate = LocalDate.now().atStartOfDay();
|
| 27124 |
amit.gupta |
85 |
Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
| 26460 |
amit.gupta |
86 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);
|
| 28617 |
tejbeer |
87 |
|
| 27124 |
amit.gupta |
88 |
Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1),
|
| 26460 |
amit.gupta |
89 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
90 |
|
| 27660 |
tejbeer |
91 |
Map<Integer, Double> lmsSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
| 27749 |
tejbeer |
92 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1), 0, false);
|
| 27660 |
tejbeer |
93 |
|
| 27549 |
tejbeer |
94 |
Map<Integer, Double> todaytertiary = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate,
|
| 27545 |
tejbeer |
95 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
96 |
|
|
|
97 |
Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),
|
|
|
98 |
curDate.minusDays(1).with(LocalTime.MAX), 0, false);
|
|
|
99 |
|
| 26460 |
amit.gupta |
100 |
Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
|
|
|
101 |
|
| 27893 |
tejbeer |
102 |
Map<Integer, Double> secondaryMtd = orderRepository
|
|
|
103 |
.selectBillingDatesBetweenSumGroupByRetailerId(curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX));
|
|
|
104 |
Map<Integer, Double> secondarylmtd = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(
|
|
|
105 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1));
|
|
|
106 |
Map<Integer, Double> secondarylms = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(
|
|
|
107 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1));
|
|
|
108 |
|
|
|
109 |
LOGGER.info("secondarylmtd" + secondarylmtd);
|
|
|
110 |
LOGGER.info("secondarylms" + secondarylms);
|
| 26479 |
amit.gupta |
111 |
List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
|
| 31569 |
amit.gupta |
112 |
Set<Integer> fofoIds = fofoStores.stream().filter(x->!x.isInternal()).map(x -> x.getId()).collect(Collectors.toSet());
|
| 27545 |
tejbeer |
113 |
Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream()
|
|
|
114 |
.collect(Collectors.toMap(x -> x.getUserId(), x -> x));
|
|
|
115 |
|
| 26460 |
amit.gupta |
116 |
Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
|
|
|
117 |
|
| 27903 |
tejbeer |
118 |
List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
|
|
|
119 |
.selectAll(curDate.withDayOfMonth(1).toLocalDate(), curDate.toLocalDate());
|
|
|
120 |
Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
|
|
|
121 |
.filter(x -> x.getShortPercentage() <= 10)
|
|
|
122 |
.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
|
|
|
123 |
|
|
|
124 |
LOGGER.info("investmentMaintainedDaysMap" + investmentMaintainedDaysMap);
|
|
|
125 |
|
| 27545 |
tejbeer |
126 |
Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {
|
|
|
127 |
try {
|
|
|
128 |
return partnerInvestmentService.getInvestment(x.getId(), 0);
|
|
|
129 |
} catch (Exception e) {
|
|
|
130 |
LOGGER.info("Could not get invetment summary for {}", x);
|
|
|
131 |
return new PartnerDailyInvestment();
|
|
|
132 |
}
|
|
|
133 |
}).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
| 26460 |
amit.gupta |
134 |
|
| 32421 |
amit.gupta |
135 |
Map<String, Set<Integer>> storeGuyMap = csService.getAuthUserPartnerIdMappingByCategoryIds(Arrays.asList(ProfitMandiConstants.TICKET_CATEGORY_RBM), true);
|
|
|
136 |
Map<Integer, AuthUser> authUserMap = authRepository.selectAllAuthUserByIds(storeGuyMap.values().stream().flatMap(x->x.stream()).filter(Utils.distinctByKey(x->x)).collect(Collectors.toList())).stream().collect(Collectors.toMap(x->x.getId(),x->x));
|
|
|
137 |
|
| 32430 |
amit.gupta |
138 |
Set<Integer> l1Rbms = positionRepository.selectPositionbyCategoryIdAndEscalationType(
|
|
|
139 |
ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1).stream().map(x -> x.getAuthUserId()).collect(Collectors.toSet());
|
|
|
140 |
|
| 27545 |
tejbeer |
141 |
for (FofoStore store : fofoStores) {
|
| 26479 |
amit.gupta |
142 |
int fofoId = store.getId();
|
| 32421 |
amit.gupta |
143 |
|
| 26460 |
amit.gupta |
144 |
int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
145 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
146 |
int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
147 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
148 |
int totalHygieneCount = hygieneCount + invalidHygieneCount;
|
| 27548 |
tejbeer |
149 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
|
|
|
150 |
|
|
|
151 |
Map<EscalationType, AuthUser> authuserEsclationTypeMap = csService
|
|
|
152 |
.getAuthUserAndEsclationByPartnerId(fofoId);
|
| 26460 |
amit.gupta |
153 |
PartnerDetailModel pm = new PartnerDetailModel();
|
| 31285 |
tejbeer |
154 |
pm.setFofoId(fofoId);
|
| 27552 |
tejbeer |
155 |
pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId).intValue());
|
|
|
156 |
pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId).intValue());
|
| 27660 |
tejbeer |
157 |
pm.setLms(lmsSale.get(fofoId) == null ? 0 : lmsSale.get(fofoId).intValue());
|
| 27893 |
tejbeer |
158 |
pm.setSecondarymtd(secondaryMtd.get(fofoId) == null ? 0 : secondaryMtd.get(fofoId).intValue());
|
|
|
159 |
pm.setSecondarylmtd(secondarylmtd.get(fofoId) == null ? 0 : secondarylmtd.get(fofoId).intValue());
|
|
|
160 |
pm.setSecondarylms(secondarylms.get(fofoId) == null ? 0 : secondarylms.get(fofoId).intValue());
|
| 27552 |
tejbeer |
161 |
pm.setTodayTertiary(todaytertiary.get(fofoId) == null ? 0 : todaytertiary.get(fofoId).intValue());
|
| 27545 |
tejbeer |
162 |
pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));
|
| 27559 |
tejbeer |
163 |
pm.setWalletAmount(userWallet.get(fofoId) == null ? 0 : userWallet.get(fofoId).getAmount());
|
| 26460 |
amit.gupta |
164 |
pm.setInvestment(investmentMap.get(fofoId));
|
|
|
165 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
166 |
pm.setHygiene(hygieneCount);
|
| 27903 |
tejbeer |
167 |
pm.setInvestment_ok(
|
|
|
168 |
investmentMaintainedDaysMap.get(fofoId) == null ? 0 : investmentMaintainedDaysMap.get(fofoId));
|
| 27548 |
tejbeer |
169 |
pm.setPartnerType(partnerType);
|
| 27907 |
amit.gupta |
170 |
if (authuserEsclationTypeMap.get(EscalationType.L1) != null) {
|
|
|
171 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L1).getName());
|
|
|
172 |
} else if (authuserEsclationTypeMap.get(EscalationType.L2) != null) {
|
| 28617 |
tejbeer |
173 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L2).getName());
|
| 27907 |
amit.gupta |
174 |
} else if (authuserEsclationTypeMap.get(EscalationType.L3) != null) {
|
|
|
175 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L3).getName());
|
|
|
176 |
} else if (authuserEsclationTypeMap.get(EscalationType.L4) != null) {
|
|
|
177 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L4).getName());
|
| 27549 |
tejbeer |
178 |
} else {
|
| 27907 |
amit.gupta |
179 |
pm.setAuthUser(" - ");
|
| 27549 |
tejbeer |
180 |
}
|
| 26460 |
amit.gupta |
181 |
pm.setTotalHygiene(totalHygieneCount);
|
|
|
182 |
|
|
|
183 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
| 32421 |
amit.gupta |
184 |
Set<Integer> rbmUserIds = storeGuyMap.get(fofoId);
|
| 32430 |
amit.gupta |
185 |
if (rbmUserIds == null || rbmUserIds.stream().noneMatch(x -> l1Rbms.contains(x))) {
|
| 32421 |
amit.gupta |
186 |
pm.setRbms("-");
|
|
|
187 |
} else {
|
| 32430 |
amit.gupta |
188 |
rbmUserIds = rbmUserIds.stream().filter(x -> l1Rbms.contains(x)).collect(Collectors.toSet());
|
| 32421 |
amit.gupta |
189 |
String commaSeperatedRbms = rbmUserIds.stream().map(x->authUserMap.get(x).getFullName()).collect(Collectors.joining(","));
|
|
|
190 |
pm.setRbms(commaSeperatedRbms);
|
|
|
191 |
}
|
| 26460 |
amit.gupta |
192 |
allPartnerStats.put(fofoId, pm);
|
| 26462 |
amit.gupta |
193 |
LOGGER.info("pm {}", pm);
|
| 27545 |
tejbeer |
194 |
|
| 26460 |
amit.gupta |
195 |
}
|
|
|
196 |
return allPartnerStats;
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
@Override
|
| 27545 |
tejbeer |
200 |
// @Cacheable(value = "partnerAggregateStats", cacheManager =
|
|
|
201 |
// "oneDayCacheManager")
|
|
|
202 |
public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)
|
|
|
203 |
throws ProfitMandiBusinessException {
|
| 26460 |
amit.gupta |
204 |
PartnerDetailModel pdm = new PartnerDetailModel();
|
| 27545 |
tejbeer |
205 |
PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();
|
| 26460 |
amit.gupta |
206 |
pdm.setInvestment(aggregateInvestment);
|
| 27732 |
tejbeer |
207 |
double totallmsAmount = 0;
|
| 26460 |
amit.gupta |
208 |
double totallmtdAmount = 0;
|
|
|
209 |
double totalmtdAmount = 0;
|
| 27550 |
tejbeer |
210 |
double totalTodayTertiary = 0;
|
| 26460 |
amit.gupta |
211 |
int totalTicketCount = 0;
|
|
|
212 |
|
|
|
213 |
int currentHygieneCount = 0;
|
|
|
214 |
int currentTotalHygieneCount = 0;
|
| 27632 |
tejbeer |
215 |
if (partnerDetailModels != null && !partnerDetailModels.isEmpty()) {
|
|
|
216 |
for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
|
| 27635 |
tejbeer |
217 |
if (partnerDetailModel != null) {
|
|
|
218 |
PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
|
| 27732 |
tejbeer |
219 |
totallmsAmount += partnerDetailModel.getLms();
|
| 27635 |
tejbeer |
220 |
totallmtdAmount += partnerDetailModel.getLmtd();
|
|
|
221 |
totalmtdAmount += partnerDetailModel.getMtd();
|
|
|
222 |
totalTicketCount += partnerDetailModel.getTicket();
|
|
|
223 |
totalTodayTertiary += partnerDetailModel.getTodayTertiary();
|
|
|
224 |
currentHygieneCount += partnerDetailModel.getHygiene();
|
|
|
225 |
currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
|
| 28617 |
tejbeer |
226 |
if (pdi != null) {
|
|
|
227 |
aggregateInvestment.setActivatedStockAmount(
|
|
|
228 |
aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
|
|
|
229 |
aggregateInvestment.setGrnPendingAmount(
|
|
|
230 |
aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
|
|
|
231 |
aggregateInvestment
|
|
|
232 |
.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
|
|
|
233 |
aggregateInvestment.setReturnInTransitAmount(
|
|
|
234 |
aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
|
|
|
235 |
aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
|
|
|
236 |
aggregateInvestment
|
|
|
237 |
.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
|
|
|
238 |
aggregateInvestment
|
|
|
239 |
.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
|
|
|
240 |
}
|
| 27635 |
tejbeer |
241 |
}
|
|
|
242 |
|
|
|
243 |
pdm.setHygiene(currentHygieneCount);
|
|
|
244 |
pdm.setTotalHygiene(currentTotalHygieneCount);
|
| 27733 |
tejbeer |
245 |
pdm.setLms((int) totallmsAmount);
|
| 27635 |
tejbeer |
246 |
pdm.setLmtd((int) totallmtdAmount);
|
|
|
247 |
pdm.setMtd((int) totalmtdAmount);
|
|
|
248 |
pdm.setTicket((int) totalTicketCount);
|
|
|
249 |
pdm.setTodayTertiary((int) totalTodayTertiary);
|
|
|
250 |
pdm.setCount(partnerDetailModels.size());
|
| 27632 |
tejbeer |
251 |
}
|
| 26460 |
amit.gupta |
252 |
}
|
|
|
253 |
return pdm;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
}
|