| 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;
|
| 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.PartnerTypeChangeRepository;
|
|
|
32 |
import com.spice.profitmandi.dao.repository.fofo.PartnerTypeChangeService;
|
| 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 |
|
| 26460 |
amit.gupta |
68 |
@Override
|
| 27016 |
amit.gupta |
69 |
@Cacheable(value = "partnerStats", cacheManager = "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);
|
|
|
76 |
|
| 27549 |
tejbeer |
77 |
Map<Integer, Double> todaytertiary = fofoOrderItemRepository.selectSumMopGroupByRetailer(curDate,
|
| 27545 |
tejbeer |
78 |
curDate.with(LocalTime.MAX), 0, false);
|
|
|
79 |
|
|
|
80 |
Map<Integer, Long> last3daystertiary = fofoOrderItemRepository.selectQtyGroupByRetailer(curDate.minusDays(4),
|
|
|
81 |
curDate.minusDays(1).with(LocalTime.MAX), 0, false);
|
|
|
82 |
|
| 26460 |
amit.gupta |
83 |
Map<Integer, Long> ticketMap = ticketRepository.selectAllOpenTicketsGroupByRetailer();
|
|
|
84 |
|
| 26479 |
amit.gupta |
85 |
List<FofoStore> fofoStores = fofoStoreRepository.selectActiveStores();
|
| 27545 |
tejbeer |
86 |
Set<Integer> fofoIds = fofoStores.stream().map(x -> x.getId()).collect(Collectors.toSet());
|
|
|
87 |
Map<Integer, UserWallet> userWallet = userWalletRepository.selectByRetailerIds(fofoIds).stream()
|
|
|
88 |
.collect(Collectors.toMap(x -> x.getUserId(), x -> x));
|
|
|
89 |
|
| 26460 |
amit.gupta |
90 |
Map<Integer, PartnerDetailModel> allPartnerStats = new HashMap<>();
|
|
|
91 |
|
| 27545 |
tejbeer |
92 |
Map<Integer, PartnerDailyInvestment> investmentMap = fofoStores.stream().map(x -> {
|
|
|
93 |
try {
|
|
|
94 |
return partnerInvestmentService.getInvestment(x.getId(), 0);
|
|
|
95 |
} catch (Exception e) {
|
|
|
96 |
LOGGER.info("Could not get invetment summary for {}", x);
|
|
|
97 |
return new PartnerDailyInvestment();
|
|
|
98 |
}
|
|
|
99 |
}).collect(Collectors.toMap(x -> x.getFofoId(), x -> x));
|
| 26460 |
amit.gupta |
100 |
|
| 27545 |
tejbeer |
101 |
for (FofoStore store : fofoStores) {
|
| 26479 |
amit.gupta |
102 |
int fofoId = store.getId();
|
| 26460 |
amit.gupta |
103 |
int hygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
104 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
105 |
int invalidHygieneCount = (int) hygieneDataRepository.selectHygieneCount(fofoId, true,
|
|
|
106 |
curDate.withDayOfMonth(1).minusMonths(1), curDate.plusMonths(1).withDayOfMonth(1));
|
|
|
107 |
int totalHygieneCount = hygieneCount + invalidHygieneCount;
|
| 27548 |
tejbeer |
108 |
PartnerType partnerType = partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now());
|
|
|
109 |
|
|
|
110 |
Map<EscalationType, AuthUser> authuserEsclationTypeMap = csService
|
|
|
111 |
.getAuthUserAndEsclationByPartnerId(fofoId);
|
| 26460 |
amit.gupta |
112 |
PartnerDetailModel pm = new PartnerDetailModel();
|
| 27552 |
tejbeer |
113 |
pm.setLmtd(lmtdSale.get(fofoId) == null ? 0 : lmtdSale.get(fofoId).intValue());
|
|
|
114 |
pm.setMtd(mtdSale.get(fofoId) == null ? 0 : mtdSale.get(fofoId).intValue());
|
|
|
115 |
pm.setTodayTertiary(todaytertiary.get(fofoId) == null ? 0 : todaytertiary.get(fofoId).intValue());
|
| 27545 |
tejbeer |
116 |
pm.setLastThreeDaytertiary(last3daystertiary.get(fofoId) == null ? 0 : last3daystertiary.get(fofoId));
|
|
|
117 |
pm.setWalletAmount(userWallet.get(fofoId).getAmount() == 0 ? 0 : userWallet.get(fofoId).getAmount());
|
| 26460 |
amit.gupta |
118 |
pm.setInvestment(investmentMap.get(fofoId));
|
|
|
119 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
120 |
pm.setHygiene(hygieneCount);
|
| 27548 |
tejbeer |
121 |
pm.setPartnerType(partnerType);
|
| 27549 |
tejbeer |
122 |
if (authuserEsclationTypeMap.get(EscalationType.L1) == null) {
|
| 27545 |
tejbeer |
123 |
|
| 27549 |
tejbeer |
124 |
if (authuserEsclationTypeMap.get(EscalationType.L2) == null) {
|
|
|
125 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L3).getName());
|
|
|
126 |
} else {
|
|
|
127 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L2).getName());
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
} else {
|
|
|
131 |
pm.setAuthUser(authuserEsclationTypeMap.get(EscalationType.L1).getName());
|
|
|
132 |
}
|
| 26460 |
amit.gupta |
133 |
pm.setTotalHygiene(totalHygieneCount);
|
|
|
134 |
|
|
|
135 |
pm.setTicket(ticketMap.get(fofoId) == null ? 0 : ticketMap.get(fofoId).intValue());
|
|
|
136 |
allPartnerStats.put(fofoId, pm);
|
| 26462 |
amit.gupta |
137 |
LOGGER.info("pm {}", pm);
|
| 27545 |
tejbeer |
138 |
|
| 26460 |
amit.gupta |
139 |
}
|
|
|
140 |
return allPartnerStats;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
@Override
|
| 27545 |
tejbeer |
144 |
// @Cacheable(value = "partnerAggregateStats", cacheManager =
|
|
|
145 |
// "oneDayCacheManager")
|
|
|
146 |
public PartnerDetailModel getAggregateStats(List<PartnerDetailModel> partnerDetailModels)
|
|
|
147 |
throws ProfitMandiBusinessException {
|
| 26460 |
amit.gupta |
148 |
PartnerDetailModel pdm = new PartnerDetailModel();
|
| 27545 |
tejbeer |
149 |
PartnerDailyInvestment aggregateInvestment = new PartnerDailyInvestment();
|
| 26460 |
amit.gupta |
150 |
pdm.setInvestment(aggregateInvestment);
|
|
|
151 |
double totallmtdAmount = 0;
|
|
|
152 |
double totalmtdAmount = 0;
|
| 27550 |
tejbeer |
153 |
double totalTodayTertiary = 0;
|
| 26460 |
amit.gupta |
154 |
int totalTicketCount = 0;
|
|
|
155 |
|
|
|
156 |
int currentHygieneCount = 0;
|
|
|
157 |
int currentTotalHygieneCount = 0;
|
|
|
158 |
for (PartnerDetailModel partnerDetailModel : partnerDetailModels) {
|
|
|
159 |
PartnerDailyInvestment pdi = partnerDetailModel.getInvestment();
|
|
|
160 |
totallmtdAmount += partnerDetailModel.getLmtd();
|
|
|
161 |
totalmtdAmount += partnerDetailModel.getMtd();
|
|
|
162 |
totalTicketCount += partnerDetailModel.getTicket();
|
| 27550 |
tejbeer |
163 |
totalTodayTertiary += partnerDetailModel.getTodayTertiary();
|
| 26460 |
amit.gupta |
164 |
currentHygieneCount += partnerDetailModel.getHygiene();
|
|
|
165 |
currentTotalHygieneCount += partnerDetailModel.getTotalHygiene();
|
| 27545 |
tejbeer |
166 |
aggregateInvestment.setActivatedStockAmount(
|
|
|
167 |
aggregateInvestment.getActivatedStockAmount() + pdi.getActivatedStockAmount());
|
|
|
168 |
aggregateInvestment
|
|
|
169 |
.setGrnPendingAmount(aggregateInvestment.getGrnPendingAmount() + pdi.getGrnPendingAmount());
|
| 26460 |
amit.gupta |
170 |
aggregateInvestment.setInStockAmount(aggregateInvestment.getInStockAmount() + pdi.getInStockAmount());
|
| 27545 |
tejbeer |
171 |
aggregateInvestment.setReturnInTransitAmount(
|
|
|
172 |
aggregateInvestment.getReturnInTransitAmount() + pdi.getReturnInTransitAmount());
|
| 26460 |
amit.gupta |
173 |
aggregateInvestment.setSalesAmount(aggregateInvestment.getSalesAmount() + pdi.getSalesAmount());
|
|
|
174 |
aggregateInvestment.setUnbilledAmount(aggregateInvestment.getUnbilledAmount() + pdi.getUnbilledAmount());
|
|
|
175 |
aggregateInvestment.setWalletAmount(aggregateInvestment.getWalletAmount() + pdi.getWalletAmount());
|
|
|
176 |
}
|
| 27549 |
tejbeer |
177 |
|
| 26460 |
amit.gupta |
178 |
pdm.setHygiene(currentHygieneCount);
|
|
|
179 |
pdm.setTotalHygiene(currentTotalHygieneCount);
|
| 27552 |
tejbeer |
180 |
pdm.setLmtd((int) totallmtdAmount);
|
|
|
181 |
pdm.setMtd((int) totalmtdAmount);
|
|
|
182 |
pdm.setTicket((int) totalTicketCount);
|
|
|
183 |
pdm.setTodayTertiary((int) totalTodayTertiary);
|
| 26470 |
amit.gupta |
184 |
pdm.setCount(partnerDetailModels.size());
|
| 26460 |
amit.gupta |
185 |
return pdm;
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
}
|