| 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.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
|
|
|
72 |
PartnerDailyInvestmentRepository partnerDailyInvestmentRepository;
|
|
|
73 |
|
| 26460 |
amit.gupta |
74 |
@Override
|
| 27628 |
tejbeer |
75 |
// @Cacheable(value = "partnerStats", cacheManager =
|
|
|
76 |
// "thirtyMinsTimeOutCacheManager")
|
| 26460 |
amit.gupta |
77 |
public Map<Integer, PartnerDetailModel> getAllPartnerStats() throws ProfitMandiBusinessException {
|
|
|
78 |
LocalDateTime curDate = LocalDate.now().atStartOfDay();
|
| 27124 |
amit.gupta |
79 |
Map<Integer, Double> lmtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
| 26460 |
amit.gupta |
80 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1), 0, false);
|
| 27124 |
amit.gupta |
81 |
Map<Integer, Double> mtdSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate.withDayOfMonth(1),
|
| 26460 |
amit.gupta |
82 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
83 |
|
| 27660 |
tejbeer |
84 |
Map<Integer, Double> lmsSale = fofoOrderItemRepository.selectSumMopGroupByRetailer(
|
| 27749 |
tejbeer |
85 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1), 0, false);
|
| 27660 |
tejbeer |
86 |
|
| 27549 |
tejbeer |
87 |
Map<Integer, Double> todaytertiary = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate,
|
| 27545 |
tejbeer |
88 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
89 |
|
|
|
90 |
Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),
|
|
|
91 |
curDate.minusDays(1).with(LocalTime.MAX), 0, false);
|
|
|
92 |
|
| 26460 |
amit.gupta |
93 |
Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
|
|
|
94 |
|
| 27893 |
tejbeer |
95 |
Map<Integer, Double> secondaryMtd = orderRepository
|
|
|
96 |
.selectBillingDatesBetweenSumGroupByRetailerId(curDate.withDayOfMonth(1), curDate.with(LocalTime.MAX));
|
|
|
97 |
Map<Integer, Double> secondarylmtd = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(
|
|
|
98 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.with(LocalTime.MAX).minusMonths(1));
|
|
|
99 |
Map<Integer, Double> secondarylms = orderRepository.selectBillingDatesBetweenSumGroupByRetailerId(
|
|
|
100 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.withDayOfMonth(1));
|
|
|
101 |
|
|
|
102 |
LOGGER.info("secondarylmtd" + secondarylmtd);
|
|
|
103 |
LOGGER.info("secondarylms" + secondarylms);
|
| 26479 |
amit.gupta |
104 |
List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
|
| 27545 |
tejbeer |
105 |
Set<Integer> fofoIds = fofoStores.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
|
|
106 |
Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream()
|
|
|
107 |
.collect(Collectors.toMap(x -> x.getUserId(), x -> x));
|
|
|
108 |
|
| 26460 |
amit.gupta |
109 |
Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
|
|
|
110 |
|
| 27903 |
tejbeer |
111 |
List<PartnerDailyInvestment> partnerDailyInvestments = partnerDailyInvestmentRepository
|
|
|
112 |
.selectAll(curDate.withDayOfMonth(1).toLocalDate(), curDate.toLocalDate());
|
|
|
113 |
Map<Integer, Long> investmentMaintainedDaysMap = partnerDailyInvestments.stream()
|
|
|
114 |
.filter(x -> x.getShortPercentage() <= 10)
|
|
|
115 |
.collect(Collectors.groupingBy(x -> x.getFofoId(), Collectors.counting()));
|
|
|
116 |
|
|
|
117 |
LOGGER.info("investmentMaintainedDaysMap" + investmentMaintainedDaysMap);
|
|
|
118 |
|
| 27545 |
tejbeer |
119 |
Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {
|
|
|
120 |
try {
|
|
|
121 |
return partnerInvestmentService.getInvestment(x.getId(), 0);
|
|
|
122 |
} catch (Exception e) {
|
|
|
123 |
LOGGER.info("Could not get invetment summary for {}", x);
|
|
|
124 |
return new PartnerDailyInvestment();
|
|
|
125 |
}
|
|
|
126 |
}).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
| 26460 |
amit.gupta |
127 |
|
| 27545 |
tejbeer |
128 |
for (FofoStore store : fofoStores) {
|
| 26479 |
amit.gupta |
129 |
int fofoId = store.getId();
|
| 26460 |
amit.gupta |
130 |
int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
131 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
132 |
int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
133 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
134 |
int totalHygieneCount = hygieneCount + invalidHygieneCount;
|
| 27548 |
tejbeer |
135 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
|
|
|
136 |
|
|
|
137 |
Map<EscalationType, AuthUser> authuserEsclationTypeMap = csService
|
|
|
138 |
.getAuthUserAndEsclationByPartnerId(fofoId);
|
| 26460 |
amit.gupta |
139 |
PartnerDetailModel pm = new PartnerDetailModel();
|
| 27552 |
tejbeer |
140 |
pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId).intValue());
|
|
|
141 |
pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId).intValue());
|
| 27660 |
tejbeer |
142 |
pm.setLms(lmsSale.get(fofoId) == null ? 0 : lmsSale.get(fofoId).intValue());
|
| 27893 |
tejbeer |
143 |
pm.setSecondarymtd(secondaryMtd.get(fofoId) == null ? 0 : secondaryMtd.get(fofoId).intValue());
|
|
|
144 |
pm.setSecondarylmtd(secondarylmtd.get(fofoId) == null ? 0 : secondarylmtd.get(fofoId).intValue());
|
|
|
145 |
pm.setSecondarylms(secondarylms.get(fofoId) == null ? 0 : secondarylms.get(fofoId).intValue());
|
| 27552 |
tejbeer |
146 |
pm.setTodayTertiary(todaytertiary.get(fofoId) == null ? 0 : todaytertiary.get(fofoId).intValue());
|
| 27545 |
tejbeer |
147 |
pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));
|
| 27559 |
tejbeer |
148 |
pm.setWalletAmount(userWallet.get(fofoId) == null ? 0 : userWallet.get(fofoId).getAmount());
|
| 26460 |
amit.gupta |
149 |
pm.setInvestment(investmentMap.get(fofoId));
|
|
|
150 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
151 |
pm.setHygiene(hygieneCount);
|
| 27903 |
tejbeer |
152 |
pm.setInvestment_ok(
|
|
|
153 |
investmentMaintainedDaysMap.get(fofoId) == null ? 0 : investmentMaintainedDaysMap.get(fofoId));
|
| 27548 |
tejbeer |
154 |
pm.setPartnerType(partnerType);
|
| 27549 |
tejbeer |
155 |
if (authuserEsclationTypeMap.get(EscalationType.L1) == null) {
|
| 27545 |
tejbeer |
156 |
|
| 27549 |
tejbeer |
157 |
if (authuserEsclationTypeMap.get(EscalationType.L2) == null) {
|
|
|
158 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L3).getName());
|
|
|
159 |
} else {
|
|
|
160 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L2).getName());
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
} else {
|
|
|
164 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L1).getName());
|
|
|
165 |
}
|
| 26460 |
amit.gupta |
166 |
pm.setTotalHygiene(totalHygieneCount);
|
|
|
167 |
|
|
|
168 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
169 |
allPartnerStats.put(fofoId, pm);
|
| 26462 |
amit.gupta |
170 |
LOGGER.info("pm {}", pm);
|
| 27545 |
tejbeer |
171 |
|
| 26460 |
amit.gupta |
172 |
}
|
|
|
173 |
return allPartnerStats;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
@Override
|
| 27545 |
tejbeer |
177 |
// @Cacheable(value = "partnerAggregateStats", cacheManager =
|
|
|
178 |
// "oneDayCacheManager")
|
|
|
179 |
public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)
|
|
|
180 |
throws ProfitMandiBusinessException {
|
| 26460 |
amit.gupta |
181 |
PartnerDetailModel pdm = new PartnerDetailModel();
|
| 27545 |
tejbeer |
182 |
PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();
|
| 26460 |
amit.gupta |
183 |
pdm.setInvestment(aggregateInvestment);
|
| 27732 |
tejbeer |
184 |
double totallmsAmount = 0;
|
| 26460 |
amit.gupta |
185 |
double totallmtdAmount = 0;
|
|
|
186 |
double totalmtdAmount = 0;
|
| 27550 |
tejbeer |
187 |
double totalTodayTertiary = 0;
|
| 26460 |
amit.gupta |
188 |
int totalTicketCount = 0;
|
|
|
189 |
|
|
|
190 |
int currentHygieneCount = 0;
|
|
|
191 |
int currentTotalHygieneCount = 0;
|
| 27632 |
tejbeer |
192 |
if (partnerDetailModels != null && !partnerDetailModels.isEmpty()) {
|
|
|
193 |
for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
|
| 27635 |
tejbeer |
194 |
if (partnerDetailModel != null) {
|
|
|
195 |
PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
|
| 27732 |
tejbeer |
196 |
totallmsAmount += partnerDetailModel.getLms();
|
| 27635 |
tejbeer |
197 |
totallmtdAmount += partnerDetailModel.getLmtd();
|
|
|
198 |
totalmtdAmount += partnerDetailModel.getMtd();
|
|
|
199 |
totalTicketCount += partnerDetailModel.getTicket();
|
|
|
200 |
totalTodayTertiary += partnerDetailModel.getTodayTertiary();
|
|
|
201 |
currentHygieneCount += partnerDetailModel.getHygiene();
|
|
|
202 |
currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
|
|
|
203 |
aggregateInvestment.setActivatedStockAmount(
|
|
|
204 |
aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
|
|
|
205 |
aggregateInvestment
|
|
|
206 |
.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
|
|
|
207 |
aggregateInvestment
|
|
|
208 |
.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
|
|
|
209 |
aggregateInvestment.setReturnInTransitAmount(
|
|
|
210 |
aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
|
|
|
211 |
aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
|
|
|
212 |
aggregateInvestment
|
|
|
213 |
.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
|
|
|
214 |
aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
pdm.setHygiene(currentHygieneCount);
|
|
|
218 |
pdm.setTotalHygiene(currentTotalHygieneCount);
|
| 27733 |
tejbeer |
219 |
pdm.setLms((int) totallmsAmount);
|
| 27635 |
tejbeer |
220 |
pdm.setLmtd((int) totallmtdAmount);
|
|
|
221 |
pdm.setMtd((int) totalmtdAmount);
|
|
|
222 |
pdm.setTicket((int) totalTicketCount);
|
|
|
223 |
pdm.setTodayTertiary((int) totalTodayTertiary);
|
|
|
224 |
pdm.setCount(partnerDetailModels.size());
|
| 27632 |
tejbeer |
225 |
}
|
| 26460 |
amit.gupta |
226 |
}
|
|
|
227 |
return pdm;
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
}
|