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