| 27877 |
amit.gupta |
1 |
package com.spice.profitmandi.service.offers;
|
|
|
2 |
|
| 29783 |
amit.gupta |
3 |
import com.google.common.collect.Lists;
|
| 27877 |
amit.gupta |
4 |
import com.google.gson.Gson;
|
|
|
5 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 29683 |
amit.gupta |
6 |
import com.spice.profitmandi.common.model.PartnerTargetModel;
|
|
|
7 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 27877 |
amit.gupta |
8 |
import com.spice.profitmandi.dao.entity.catalog.ItemCriteria;
|
|
|
9 |
import com.spice.profitmandi.dao.entity.catalog.Offer;
|
| 29783 |
amit.gupta |
10 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 27877 |
amit.gupta |
11 |
import com.spice.profitmandi.dao.entity.catalog.TargetSlab;
|
| 31205 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
|
|
13 |
import com.spice.profitmandi.dao.entity.fofo.OfferPayout;
|
| 29783 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.enumuration.catalog.AmountType;
|
| 29499 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.enumuration.catalog.OfferSchemeType;
|
| 31205 |
amit.gupta |
16 |
import com.spice.profitmandi.dao.enumuration.transaction.SchemePayoutStatus;
|
| 27877 |
amit.gupta |
17 |
import com.spice.profitmandi.dao.model.CreateOfferRequest;
|
|
|
18 |
import com.spice.profitmandi.dao.model.ItemCriteriaPayout;
|
| 30066 |
amit.gupta |
19 |
import com.spice.profitmandi.dao.repository.catalog.*;
|
| 31205 |
amit.gupta |
20 |
import com.spice.profitmandi.dao.repository.fofo.OfferPayoutRepository;
|
| 27877 |
amit.gupta |
21 |
import com.spice.profitmandi.service.user.RetailerService;
|
| 31205 |
amit.gupta |
22 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
23 |
import in.shop2020.model.v1.order.WalletReferenceType;
|
| 30066 |
amit.gupta |
24 |
import org.apache.logging.log4j.LogManager;
|
|
|
25 |
import org.apache.logging.log4j.Logger;
|
|
|
26 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
27 |
import org.apache.poi.ss.usermodel.CellType;
|
|
|
28 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
29 |
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
|
|
|
30 |
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
31 |
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
32 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
33 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
34 |
import org.springframework.cache.CacheManager;
|
|
|
35 |
import org.springframework.cache.annotation.Cacheable;
|
|
|
36 |
import org.springframework.stereotype.Component;
|
| 27877 |
amit.gupta |
37 |
|
| 30066 |
amit.gupta |
38 |
import java.io.IOException;
|
|
|
39 |
import java.io.InputStream;
|
|
|
40 |
import java.time.LocalDate;
|
| 31205 |
amit.gupta |
41 |
import java.time.LocalDateTime;
|
| 30066 |
amit.gupta |
42 |
import java.time.YearMonth;
|
|
|
43 |
import java.util.*;
|
|
|
44 |
import java.util.stream.Collectors;
|
|
|
45 |
|
| 27877 |
amit.gupta |
46 |
@Component
|
|
|
47 |
public class OfferServiceImpl implements OfferService {
|
|
|
48 |
|
| 29499 |
amit.gupta |
49 |
private static final List<String> targets = Arrays.asList("Target 1", "Target 2", "Target 3", "Target 4",
|
|
|
50 |
"Target 5");
|
|
|
51 |
|
| 27877 |
amit.gupta |
52 |
@Autowired
|
|
|
53 |
private Gson gson;
|
|
|
54 |
|
|
|
55 |
@Autowired
|
|
|
56 |
private OfferRepository offerRepository;
|
|
|
57 |
|
|
|
58 |
@Autowired
|
|
|
59 |
private RetailerService retailerService;
|
|
|
60 |
|
|
|
61 |
@Autowired
|
| 31205 |
amit.gupta |
62 |
OfferPayoutRepository offerPayoutRepository;
|
|
|
63 |
|
|
|
64 |
@Autowired
|
| 27877 |
amit.gupta |
65 |
private ItemCriteriaRepository itemCriteriaRepository;
|
|
|
66 |
|
|
|
67 |
@Autowired
|
|
|
68 |
private ItemRepository itemRepository;
|
|
|
69 |
|
|
|
70 |
@Autowired
|
| 29783 |
amit.gupta |
71 |
private TagListingRepository tagListingRepository;
|
|
|
72 |
|
|
|
73 |
@Autowired
|
| 30066 |
amit.gupta |
74 |
private OfferTargetSlabRepository offerTargetSlabRepository;
|
| 30684 |
amit.gupta |
75 |
|
| 29806 |
amit.gupta |
76 |
@Autowired
|
| 29810 |
amit.gupta |
77 |
CacheManager thirtyMinsTimeOutCacheManager;
|
| 27877 |
amit.gupta |
78 |
|
| 29806 |
amit.gupta |
79 |
|
| 27877 |
amit.gupta |
80 |
private static final Logger LOGGER = LogManager.getLogger(OfferServiceImpl.class);
|
|
|
81 |
|
|
|
82 |
@Override
|
|
|
83 |
public void addOfferService(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
| 29686 |
amit.gupta |
84 |
LOGGER.info("createOfferRequest -- {}", createOfferRequest);
|
| 27877 |
amit.gupta |
85 |
this.createOffer(createOfferRequest);
|
|
|
86 |
this.createTargetSlabs(createOfferRequest);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
private void createOffer(CreateOfferRequest createOfferRequest) {
|
|
|
90 |
Offer offer = new Offer();
|
|
|
91 |
offer.setName(createOfferRequest.getName());
|
|
|
92 |
offer.setTargetType(createOfferRequest.getTargetType());
|
| 31190 |
amit.gupta |
93 |
offer.setBooster(createOfferRequest.isBooster());
|
| 27918 |
amit.gupta |
94 |
offer.setPayoutType(createOfferRequest.getPayoutType());
|
| 27877 |
amit.gupta |
95 |
offer.setSchemeType(createOfferRequest.getSchemeType());
|
| 29795 |
amit.gupta |
96 |
offer.setBaseCriteia(createOfferRequest.isBaseCriteria());
|
| 27877 |
amit.gupta |
97 |
offer.setEndDate(createOfferRequest.getEndDate());
|
|
|
98 |
offer.setStartDate(createOfferRequest.getStartDate());
|
|
|
99 |
offer.setBrandSharePercentage(createOfferRequest.getBrandShareTerms());
|
| 27918 |
amit.gupta |
100 |
offer.setSellinPercentage(createOfferRequest.getSellinPercentage());
|
| 27877 |
amit.gupta |
101 |
offer.setOfferNotes(createOfferRequest.getOfferNotes());
|
|
|
102 |
offer.setActivationBrands(createOfferRequest.getActivationBrands());
|
|
|
103 |
offer.setTerms(createOfferRequest.getTerms());
|
|
|
104 |
offer.setItemCriteria(gson.toJson(createOfferRequest.getItemCriteria()));
|
|
|
105 |
offer.setPartnerCriteria(gson.toJson(createOfferRequest.getPartnerCriteria()));
|
|
|
106 |
offerRepository.persist(offer);
|
|
|
107 |
createOfferRequest.setId(offer.getId());
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
@Override
|
|
|
111 |
public List<CreateOfferRequest> getPublishedOffers(int fofoId, YearMonth yearMonth) {
|
| 29783 |
amit.gupta |
112 |
List<Offer> publishedOffers = offerRepository.selectAllPublishedMapByPartner(yearMonth).get(fofoId);
|
| 27877 |
amit.gupta |
113 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
114 |
if (publishedOffers != null) {
|
|
|
115 |
for (Offer offer : publishedOffers) {
|
|
|
116 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
117 |
createOfferRequests.add(createOfferRequest);
|
| 29783 |
amit.gupta |
118 |
setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
| 27877 |
amit.gupta |
119 |
|
|
|
120 |
}
|
| 29783 |
amit.gupta |
121 |
}
|
|
|
122 |
return createOfferRequests;
|
|
|
123 |
}
|
| 27877 |
amit.gupta |
124 |
|
| 29783 |
amit.gupta |
125 |
private void setCriteriaPayoutAchieved(int fofoId, CreateOfferRequest createOfferRequest) {
|
|
|
126 |
Map<Integer, Long> criteriaTransactionMap = null;
|
|
|
127 |
if (createOfferRequest.getSchemeType().equals(OfferSchemeType.SELLIN)) {
|
|
|
128 |
criteriaTransactionMap = offerRepository.getPartnerWisePurchaseSum(createOfferRequest).get(fofoId);
|
|
|
129 |
} else {
|
|
|
130 |
criteriaTransactionMap = offerRepository.getPartnerWiseSalesSum(createOfferRequest).get(fofoId);
|
|
|
131 |
}
|
|
|
132 |
LOGGER.info("I am here ------- {}", createOfferRequest.getId());
|
|
|
133 |
List<com.spice.profitmandi.dao.model.ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest
|
|
|
134 |
.getTargetSlabs().get(0).getItemCriteriaPayouts();
|
| 27877 |
amit.gupta |
135 |
|
| 29783 |
amit.gupta |
136 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
|
|
137 |
itemCriteriaPayout.setNextSlab(null);
|
|
|
138 |
LOGGER.info("I am here --- {}", itemCriteriaPayout.getPayoutSlabs());
|
|
|
139 |
long saleAmount = 0;
|
|
|
140 |
if (criteriaTransactionMap != null
|
|
|
141 |
&& criteriaTransactionMap.containsKey(itemCriteriaPayout.getItemCriteria().getId())) {
|
|
|
142 |
saleAmount = criteriaTransactionMap.get(itemCriteriaPayout.getItemCriteria().getId());
|
|
|
143 |
}
|
|
|
144 |
itemCriteriaPayout.setAchievedValue(saleAmount);
|
|
|
145 |
List<com.spice.profitmandi.service.offers.PayoutSlab> payoutSlabs = itemCriteriaPayout.getPayoutSlabs();
|
|
|
146 |
for (int i = 0; i < payoutSlabs.size(); i++) {
|
|
|
147 |
com.spice.profitmandi.service.offers.PayoutSlab beginSlab = payoutSlabs.get(i);
|
|
|
148 |
com.spice.profitmandi.service.offers.PayoutSlab endSlab = null;
|
|
|
149 |
if (payoutSlabs.size() - 1 > i) {
|
|
|
150 |
endSlab = payoutSlabs.get(i + 1);
|
|
|
151 |
}
|
|
|
152 |
if (beginSlab.getOnwardsAmount() > saleAmount) {
|
|
|
153 |
itemCriteriaPayout.setShortValue(beginSlab.getOnwardsAmount() - saleAmount);
|
|
|
154 |
itemCriteriaPayout.setNextSlab(beginSlab);
|
|
|
155 |
LOGGER.info("I am here");
|
|
|
156 |
break;
|
|
|
157 |
}
|
|
|
158 |
if (endSlab != null) {
|
|
|
159 |
if (beginSlab.getOnwardsAmount() <= saleAmount && endSlab.getOnwardsAmount() > saleAmount) {
|
|
|
160 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
161 |
beginSlab.setSelected(true);
|
|
|
162 |
itemCriteriaPayout.setNextSlab(endSlab);
|
|
|
163 |
itemCriteriaPayout.setShortValue(endSlab.getOnwardsAmount() - saleAmount);
|
|
|
164 |
LOGGER.info("I am here");
|
|
|
165 |
break;
|
| 29499 |
amit.gupta |
166 |
}
|
| 29783 |
amit.gupta |
167 |
// Last loop
|
|
|
168 |
} else {
|
|
|
169 |
beginSlab.setSelected(true);
|
|
|
170 |
itemCriteriaPayout.setCurrentSlab(beginSlab);
|
|
|
171 |
LOGGER.info("I am here");
|
| 28152 |
amit.gupta |
172 |
}
|
| 27877 |
amit.gupta |
173 |
}
|
| 29783 |
amit.gupta |
174 |
LOGGER.info("I am here --- {}", itemCriteriaPayout);
|
| 27877 |
amit.gupta |
175 |
}
|
| 29783 |
amit.gupta |
176 |
|
| 27877 |
amit.gupta |
177 |
}
|
|
|
178 |
|
|
|
179 |
@Override
|
| 29783 |
amit.gupta |
180 |
@Cacheable(value = "allOffers", cacheManager = "thirtyMinsTimeOutCacheManager")
|
|
|
181 |
public Map<Integer, CreateOfferRequest> getAllOffers(YearMonth yearMonth) {
|
| 27877 |
amit.gupta |
182 |
List<Offer> allOffers = offerRepository.selectAll(yearMonth, false);
|
|
|
183 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
184 |
for (Offer offer : allOffers) {
|
|
|
185 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
186 |
createOfferRequests.add(createOfferRequest);
|
|
|
187 |
}
|
| 29783 |
amit.gupta |
188 |
return createOfferRequests.stream().collect(Collectors.toMap(CreateOfferRequest::getId, x -> x));
|
| 27877 |
amit.gupta |
189 |
}
|
|
|
190 |
|
|
|
191 |
@Override
|
| 29783 |
amit.gupta |
192 |
@Cacheable(value = "partnerOffers", cacheManager = "thirtyMinsTimeOutCacheManager")
|
|
|
193 |
public CreateOfferRequest getOffer(int fofoId, int offerId) {
|
| 27877 |
amit.gupta |
194 |
Offer offer = offerRepository.selectById(offerId);
|
|
|
195 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
| 29783 |
amit.gupta |
196 |
if (fofoId > 0) {
|
|
|
197 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
|
|
198 |
}
|
|
|
199 |
|
| 27877 |
amit.gupta |
200 |
return createOfferRequest;
|
|
|
201 |
}
|
|
|
202 |
|
| 29902 |
amit.gupta |
203 |
@Override
|
|
|
204 |
public CreateOfferRequest getCreateOfferRequest(Offer fromOffer) {
|
| 27877 |
amit.gupta |
205 |
CreateOfferRequest createOfferRequest = new CreateOfferRequest();
|
|
|
206 |
createOfferRequest.setName(fromOffer.getName());
|
|
|
207 |
createOfferRequest.setTargetType(fromOffer.getTargetType());
|
| 27918 |
amit.gupta |
208 |
createOfferRequest.setPayoutType(fromOffer.getPayoutType());
|
| 27877 |
amit.gupta |
209 |
createOfferRequest.setSchemeType(fromOffer.getSchemeType());
|
|
|
210 |
createOfferRequest.setActivationBrands(fromOffer.getActivationBrands());
|
|
|
211 |
createOfferRequest.setEndDate(fromOffer.getEndDate());
|
| 31608 |
amit.gupta |
212 |
createOfferRequest.setProcessTimestamp(fromOffer.getProcessedTimestamp());
|
| 27877 |
amit.gupta |
213 |
createOfferRequest.setStartDate(fromOffer.getStartDate());
|
| 31190 |
amit.gupta |
214 |
createOfferRequest.setBooster(fromOffer.isBooster());
|
| 27877 |
amit.gupta |
215 |
createOfferRequest.setActive(fromOffer.isActive());
|
|
|
216 |
createOfferRequest.setSellinPercentage(fromOffer.getSellinPercentage());
|
|
|
217 |
createOfferRequest.setBrandShareTerms(fromOffer.getBrandSharePercentage());
|
|
|
218 |
createOfferRequest.setOfferNotes(fromOffer.getOfferNotes());
|
|
|
219 |
createOfferRequest.setTerms(fromOffer.getTerms());
|
| 29795 |
amit.gupta |
220 |
createOfferRequest.setBaseCriteria(fromOffer.isBaseCriteia());
|
| 27877 |
amit.gupta |
221 |
createOfferRequest.setItemCriteria(
|
|
|
222 |
gson.fromJson(fromOffer.getItemCriteria(), com.spice.profitmandi.service.offers.ItemCriteria.class));
|
|
|
223 |
createOfferRequest.setPartnerCriteria(gson.fromJson(fromOffer.getPartnerCriteria(), PartnerCriteria.class));
|
|
|
224 |
createOfferRequest.setId(fromOffer.getId());
|
|
|
225 |
createOfferRequest.setCreatedOn(fromOffer.getCreatedTimestamp());
|
|
|
226 |
createOfferRequest.setItemCriteriaString(
|
|
|
227 |
itemCriteriaRepository.getItemCriteriaDescription(createOfferRequest.getItemCriteria()));
|
| 30066 |
amit.gupta |
228 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
| 27877 |
amit.gupta |
229 |
.getByOfferId(fromOffer.getId());
|
|
|
230 |
createOfferRequest.setPartnerCriteriaString(
|
|
|
231 |
retailerService.getPartnerCriteriaString(createOfferRequest.getPartnerCriteria()));
|
|
|
232 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
| 29783 |
amit.gupta |
233 |
for (com.spice.profitmandi.dao.model.ItemCriteriaPayout itemCriteriaPayout : createOfferRequest.getTargetSlabs()
|
|
|
234 |
.get(0).getItemCriteriaPayouts()) {
|
|
|
235 |
itemCriteriaPayout.setNextSlab(itemCriteriaPayout.getPayoutSlabs().get(0));
|
|
|
236 |
}
|
| 27877 |
amit.gupta |
237 |
return createOfferRequest;
|
|
|
238 |
|
|
|
239 |
}
|
|
|
240 |
|
| 31205 |
amit.gupta |
241 |
@Autowired
|
|
|
242 |
private WalletService walletService;
|
|
|
243 |
|
|
|
244 |
@Override
|
|
|
245 |
public void reverseAdditionalSelloutSchemes(int fofoId, List<InventoryItem> inventoryItems) throws ProfitMandiBusinessException {
|
|
|
246 |
List<OfferPayout> offerPayouts = new ArrayList<>();
|
| 31593 |
amit.gupta |
247 |
//As of now we are not handling non-serialised items
|
|
|
248 |
inventoryItems = inventoryItems.stream().filter(x -> x.getSerialNumber() != null).collect(Collectors.toList());
|
| 31205 |
amit.gupta |
249 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
250 |
offerPayouts.addAll(offerPayoutRepository.selectAllBySerialNumber(inventoryItem.getFofoId(), inventoryItem.getSerialNumber())
|
|
|
251 |
.stream()
|
|
|
252 |
.filter(x -> x.getRejectTimestamp() != null)
|
|
|
253 |
.collect(Collectors.toList()));
|
|
|
254 |
}
|
|
|
255 |
for (OfferPayout offerPayout : offerPayouts) {
|
|
|
256 |
offerPayout.setStatus(SchemePayoutStatus.REJECTED);
|
|
|
257 |
offerPayout.setRejectTimestamp(LocalDateTime.now());
|
|
|
258 |
}
|
|
|
259 |
Map<Long, Double> offerPayoutAmountMap = offerPayouts.stream().collect(Collectors.groupingBy(x -> x.getOfferId(), Collectors.summingDouble(x -> x.getAmount())));
|
|
|
260 |
for (Map.Entry<Long, Double> offerPayoutEntry : offerPayoutAmountMap.entrySet()) {
|
|
|
261 |
walletService.rollbackAmountFromWallet(fofoId, offerPayoutEntry.getValue().floatValue(), offerPayoutEntry.getKey().intValue(),
|
|
|
262 |
WalletReferenceType.ADDITIONAL_SCHEME, "Imeis for corresponding schemes are returned", LocalDateTime.now());
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
|
| 27877 |
amit.gupta |
266 |
private void createTargetSlabs(CreateOfferRequest createOfferRequest) throws ProfitMandiBusinessException {
|
|
|
267 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = createOfferRequest.getTargetSlabs().stream()
|
|
|
268 |
.filter(x -> x.validate()).collect(Collectors.toList());
|
|
|
269 |
if (targetSlabs.size() >= 0) {
|
|
|
270 |
Collections.sort(targetSlabs);
|
|
|
271 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
272 |
if (targetSlab.validate()) {
|
|
|
273 |
List<ItemCriteriaPayout> itemCriteriaPayouts = targetSlab.getItemCriteriaPayouts();
|
|
|
274 |
for (ItemCriteriaPayout itemCriteriaPayout : itemCriteriaPayouts) {
|
| 30684 |
amit.gupta |
275 |
ItemCriteria itemCriteria = itemCriteriaRepository.selectById(itemCriteriaPayout.getItemCriteria().getId());
|
|
|
276 |
if (itemCriteriaPayout.getItemCriteria().getId() > 0) {
|
|
|
277 |
itemCriteriaPayout.getItemCriteria().setId(0);
|
|
|
278 |
} else {
|
|
|
279 |
|
|
|
280 |
String itemCriteriaJson = gson.toJson(itemCriteriaPayout.getItemCriteria());
|
|
|
281 |
itemCriteria = itemCriteriaRepository.selectByCriteria(itemCriteriaJson);
|
|
|
282 |
// ItemCriteria Only once
|
|
|
283 |
if (itemCriteria == null) {
|
|
|
284 |
itemCriteria = new ItemCriteria();
|
|
|
285 |
itemCriteria.setCriteria(gson.toJson(itemCriteriaPayout.getItemCriteria()));
|
|
|
286 |
itemCriteriaRepository.persist(itemCriteria);
|
|
|
287 |
}
|
| 27877 |
amit.gupta |
288 |
}
|
|
|
289 |
for (PayoutSlab payoutSlab : itemCriteriaPayout.getPayoutSlabs()) {
|
|
|
290 |
TargetSlab targetSlab1 = new TargetSlab();
|
|
|
291 |
targetSlab1.setOfferId(createOfferRequest.getId());
|
|
|
292 |
targetSlab1.setOnwardsTarget(targetSlab.getOnwardsAmount());
|
|
|
293 |
targetSlab1.setTargetDescription(targetSlab.getTargetDescription());
|
|
|
294 |
targetSlab1.setPayoutTarget(payoutSlab.getOnwardsAmount());
|
|
|
295 |
targetSlab1.setPayoutValue(payoutSlab.getPayoutAmount());
|
|
|
296 |
targetSlab1.setItemCriteriaId(itemCriteria.getId());
|
|
|
297 |
targetSlab1.setAmountType(itemCriteriaPayout.getAmountType());
|
| 30066 |
amit.gupta |
298 |
offerTargetSlabRepository.persist(targetSlab1);
|
| 27877 |
amit.gupta |
299 |
}
|
|
|
300 |
}
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
} else {
|
|
|
305 |
throw new ProfitMandiBusinessException("Invalid Target Slabs", "Invalid Target", "");
|
|
|
306 |
}
|
|
|
307 |
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
@Override
|
| 30274 |
amit.gupta |
311 |
public List<CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, int catalogId) {
|
| 27877 |
amit.gupta |
312 |
List<Offer> publishedOffers = offerRepository.selectAllPublishedMapByPartner(YearMonth.from(date)).get(fofoId);
|
|
|
313 |
List<CreateOfferRequest> createOfferRequests = new ArrayList<>();
|
|
|
314 |
if (publishedOffers != null) {
|
|
|
315 |
for (Offer offer : publishedOffers) {
|
| 28007 |
amit.gupta |
316 |
boolean found = false;
|
| 30066 |
amit.gupta |
317 |
List<com.spice.profitmandi.dao.model.TargetSlab> targetSlabs = offerTargetSlabRepository
|
| 27877 |
amit.gupta |
318 |
.getByOfferId(offer.getId());
|
|
|
319 |
CreateOfferRequest createOfferRequest = this.getCreateOfferRequest(offer);
|
|
|
320 |
createOfferRequest.setTargetSlabs(targetSlabs);
|
| 29883 |
amit.gupta |
321 |
this.setCriteriaPayoutAchieved(fofoId, createOfferRequest);
|
| 27877 |
amit.gupta |
322 |
Set<com.spice.profitmandi.service.offers.ItemCriteria> itemCriteriaSet = targetSlabs.stream()
|
|
|
323 |
.flatMap(x -> x.getItemCriteriaPayouts().stream()).map(x -> x.getItemCriteria()).distinct()
|
| 30274 |
amit.gupta |
324 |
.filter(x -> itemRepository.containsModel(x, catalogId)).collect(Collectors.toSet());
|
| 27877 |
amit.gupta |
325 |
|
|
|
326 |
for (com.spice.profitmandi.dao.model.TargetSlab targetSlab : targetSlabs) {
|
|
|
327 |
targetSlab.setItemCriteriaPayouts(targetSlab.getItemCriteriaPayouts().stream()
|
|
|
328 |
.filter(x -> itemCriteriaSet.contains(x.getItemCriteria())).collect(Collectors.toList()));
|
| 29499 |
amit.gupta |
329 |
if (targetSlab.getItemCriteriaPayouts().size() > 0) {
|
| 28007 |
amit.gupta |
330 |
found = true;
|
|
|
331 |
}
|
| 27877 |
amit.gupta |
332 |
}
|
| 29499 |
amit.gupta |
333 |
if (found) {
|
| 28007 |
amit.gupta |
334 |
createOfferRequests.add(createOfferRequest);
|
|
|
335 |
}
|
| 27877 |
amit.gupta |
336 |
}
|
|
|
337 |
}
|
| 29499 |
amit.gupta |
338 |
|
| 27877 |
amit.gupta |
339 |
return createOfferRequests;
|
|
|
340 |
}
|
| 29783 |
amit.gupta |
341 |
|
| 29683 |
amit.gupta |
342 |
@Override
|
|
|
343 |
public void createOffers(InputStream fileInputStream) throws Exception {
|
|
|
344 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = this.parseFromExcel(fileInputStream);
|
| 29783 |
amit.gupta |
345 |
for (Map.Entry<CreateOfferRequest, List<PartnerTargetModel>> partnerTargetEntry : offerPartnerTargetMap
|
|
|
346 |
.entrySet()) {
|
| 29683 |
amit.gupta |
347 |
CreateOfferRequest createOfferRequest = partnerTargetEntry.getKey();
|
| 29783 |
amit.gupta |
348 |
Map<List<Integer>, List<Integer>> targetPartnerMap = partnerTargetEntry.getValue().stream()
|
|
|
349 |
.collect(Collectors.groupingBy(PartnerTargetModel::getTargets,
|
|
|
350 |
Collectors.mapping(PartnerTargetModel::getFofoId, Collectors.toList())));
|
|
|
351 |
for (Map.Entry<List<Integer>, List<Integer>> targetPartnersEntry : targetPartnerMap.entrySet()) {
|
|
|
352 |
List<Integer> fofoIds = createOfferRequest.getPartnerCriteria().getFofoIds();
|
|
|
353 |
List<Integer> filterFofoIds = targetPartnersEntry.getValue().stream().filter(x -> !fofoIds.contains(x))
|
|
|
354 |
.collect(Collectors.toList());
|
| 29796 |
amit.gupta |
355 |
LOGGER.info("FofoIds {}", filterFofoIds);
|
| 29783 |
amit.gupta |
356 |
List<Integer> targets = targetPartnersEntry.getKey();
|
| 29796 |
amit.gupta |
357 |
LOGGER.info("Targets {}", targets);
|
| 29783 |
amit.gupta |
358 |
|
|
|
359 |
createOfferRequest.getPartnerCriteria().setFofoIds(filterFofoIds);
|
|
|
360 |
int counter = 0;
|
| 29806 |
amit.gupta |
361 |
List<ItemCriteriaPayout> itemCriteriaPayouts = createOfferRequest.getTargetSlabs().get(0)
|
|
|
362 |
.getItemCriteriaPayouts();
|
| 30684 |
amit.gupta |
363 |
List<PayoutSlab> payoutSlabs = itemCriteriaPayouts.stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.toList());
|
| 29806 |
amit.gupta |
364 |
for (PayoutSlab payoutSlab : payoutSlabs) {
|
| 29783 |
amit.gupta |
365 |
payoutSlab.setOnwardsAmount(targets.get(counter));
|
|
|
366 |
counter++;
|
|
|
367 |
}
|
| 29808 |
amit.gupta |
368 |
this.addOfferService(createOfferRequest);
|
| 29783 |
amit.gupta |
369 |
}
|
| 29683 |
amit.gupta |
370 |
}
|
| 29810 |
amit.gupta |
371 |
thirtyMinsTimeOutCacheManager.getCache("allOffers").evict(YearMonth.now());
|
| 29806 |
amit.gupta |
372 |
|
| 29683 |
amit.gupta |
373 |
}
|
| 27877 |
amit.gupta |
374 |
|
| 29783 |
amit.gupta |
375 |
private Map<CreateOfferRequest, List<PartnerTargetModel>> parseFromExcel(InputStream inputStream)
|
|
|
376 |
throws ProfitMandiBusinessException {
|
|
|
377 |
|
|
|
378 |
Map<CreateOfferRequest, List<PartnerTargetModel>> offerPartnerTargetMap = new HashMap<>();
|
| 29683 |
amit.gupta |
379 |
XSSFWorkbook myWorkBook = null;
|
|
|
380 |
try {
|
|
|
381 |
myWorkBook = new XSSFWorkbook(inputStream);
|
|
|
382 |
myWorkBook.setMissingCellPolicy(MissingCellPolicy.RETURN_BLANK_AS_NULL);
|
| 29783 |
amit.gupta |
383 |
for (int i = 0; i < myWorkBook.getNumberOfSheets(); i++) {
|
|
|
384 |
|
| 29683 |
amit.gupta |
385 |
XSSFSheet mySheet = myWorkBook.getSheetAt(i);
|
|
|
386 |
int offerId = Integer.parseInt(mySheet.getSheetName());
|
| 29783 |
amit.gupta |
387 |
CreateOfferRequest existingOffer = this.getOffer(0, offerId);
|
| 29683 |
amit.gupta |
388 |
List<PartnerTargetModel> partnerTargetModels = new ArrayList<>();
|
|
|
389 |
offerPartnerTargetMap.put(existingOffer, partnerTargetModels);
|
|
|
390 |
this.printIterator(mySheet);
|
|
|
391 |
LOGGER.info("rowCellNum {}", mySheet.getLastRowNum());
|
|
|
392 |
for (int rowNumber = 1; rowNumber <= mySheet.getLastRowNum(); rowNumber++) {
|
|
|
393 |
XSSFRow row = mySheet.getRow(rowNumber);
|
|
|
394 |
LOGGER.info("row {}", row);
|
|
|
395 |
PartnerTargetModel partnerTargetModel = new PartnerTargetModel();
|
|
|
396 |
|
|
|
397 |
if (row.getCell(0) != null && row.getCell(0).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
398 |
partnerTargetModel.setFofoId((int) row.getCell(0).getNumericCellValue());
|
|
|
399 |
} else {
|
|
|
400 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
401 |
"FOFO ID", row.getCell(0).toString(), "TGLSTNG_VE_1010");
|
|
|
402 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
403 |
throw profitMandiBusinessException;
|
|
|
404 |
}
|
|
|
405 |
List<Integer> targets = new ArrayList<>();
|
| 30684 |
amit.gupta |
406 |
long targetsSize = existingOffer.getTargetSlabs().get(0).getItemCriteriaPayouts().stream().flatMap(x -> x.getPayoutSlabs().stream()).collect(Collectors.counting());
|
| 29797 |
amit.gupta |
407 |
for (int cellNumber = 1; cellNumber <= targetsSize; cellNumber++) {
|
| 29683 |
amit.gupta |
408 |
if (row.getCell(cellNumber) != null
|
|
|
409 |
&& row.getCell(cellNumber).getCellTypeEnum() == CellType.NUMERIC) {
|
|
|
410 |
targets.add((int) row.getCell(cellNumber).getNumericCellValue());
|
|
|
411 |
} else {
|
|
|
412 |
ProfitMandiBusinessException profitMandiBusinessException = new ProfitMandiBusinessException(
|
|
|
413 |
"Target Column issue at " + rowNumber + ", " + cellNumber, row.getCell(cellNumber),
|
|
|
414 |
"Invalid target");
|
|
|
415 |
LOGGER.error("Excel file parse error : ", profitMandiBusinessException);
|
|
|
416 |
throw profitMandiBusinessException;
|
|
|
417 |
}
|
|
|
418 |
}
|
|
|
419 |
partnerTargetModel.setTargets(targets);
|
|
|
420 |
partnerTargetModels.add(partnerTargetModel);
|
|
|
421 |
}
|
|
|
422 |
}
|
|
|
423 |
myWorkBook.close();
|
| 29783 |
amit.gupta |
424 |
} catch (ProfitMandiBusinessException pbse) {
|
| 29683 |
amit.gupta |
425 |
throw pbse;
|
|
|
426 |
} catch (IOException ioException) {
|
|
|
427 |
ioException.printStackTrace();
|
|
|
428 |
throw new ProfitMandiBusinessException(ProfitMandiConstants.EXCEL_FILE, ioException.getMessage(),
|
|
|
429 |
"EXL_VE_1000");
|
|
|
430 |
} finally {
|
|
|
431 |
if (myWorkBook != null) {
|
|
|
432 |
try {
|
|
|
433 |
myWorkBook.close();
|
|
|
434 |
} catch (IOException e) {
|
|
|
435 |
// TODO Auto-generated catch block
|
|
|
436 |
e.printStackTrace();
|
|
|
437 |
}
|
|
|
438 |
}
|
|
|
439 |
}
|
|
|
440 |
LOGGER.info("offerPartnerTargetMap {}", offerPartnerTargetMap);
|
|
|
441 |
return offerPartnerTargetMap;
|
|
|
442 |
}
|
| 29783 |
amit.gupta |
443 |
|
|
|
444 |
private void printIterator(XSSFSheet sheet) {
|
|
|
445 |
Iterator<Row> rowIterator = sheet.iterator();
|
|
|
446 |
while (rowIterator.hasNext()) {
|
|
|
447 |
Row row = rowIterator.next();
|
|
|
448 |
// For each row, iterate through all the columns
|
|
|
449 |
Iterator<Cell> cellIterator = row.cellIterator();
|
|
|
450 |
|
|
|
451 |
while (cellIterator.hasNext()) {
|
|
|
452 |
Cell cell = cellIterator.next();
|
|
|
453 |
// Check the cell type and format accordingly
|
|
|
454 |
switch (cell.getCellType()) {
|
| 30684 |
amit.gupta |
455 |
case Cell.CELL_TYPE_NUMERIC:
|
|
|
456 |
System.out.print(cell.getNumericCellValue() + "t");
|
|
|
457 |
break;
|
|
|
458 |
case Cell.CELL_TYPE_STRING:
|
|
|
459 |
System.out.print(cell.getStringCellValue() + "t");
|
|
|
460 |
break;
|
| 29783 |
amit.gupta |
461 |
}
|
|
|
462 |
}
|
|
|
463 |
System.out.println("");
|
|
|
464 |
}
|
| 29683 |
amit.gupta |
465 |
}
|
|
|
466 |
|
| 29783 |
amit.gupta |
467 |
@Override
|
|
|
468 |
public Map<Integer, CreateOfferRequest> getPublishedOffers(LocalDate date, int fofoId, String brand) {
|
|
|
469 |
// TODO Auto-generated method stub
|
|
|
470 |
return null;
|
|
|
471 |
}
|
| 29683 |
amit.gupta |
472 |
|
| 29783 |
amit.gupta |
473 |
@Override
|
| 30215 |
amit.gupta |
474 |
@Cacheable(value = "slabPayoutMap", cacheManager = "oneDayCacheManager")
|
| 29783 |
amit.gupta |
475 |
public Map<Integer, Map<Integer, Long>> getSlabPayoutMap(CreateOfferRequest createOfferRequest) {
|
| 30121 |
amit.gupta |
476 |
Map<Integer, Map<Integer, Long>> catalogSlabPayoutMap = new HashMap<>();
|
| 29785 |
amit.gupta |
477 |
com.spice.profitmandi.dao.model.TargetSlab targetSlab = createOfferRequest.getTargetSlabs().get(0);
|
| 29783 |
amit.gupta |
478 |
List<ItemCriteriaPayout> payouts = targetSlab.getItemCriteriaPayouts();
|
| 30684 |
amit.gupta |
479 |
|
| 29783 |
amit.gupta |
480 |
for (ItemCriteriaPayout itemCriteriaPayout : payouts) {
|
|
|
481 |
com.spice.profitmandi.service.offers.ItemCriteria itemCriteria = itemCriteriaPayout.getItemCriteria();
|
| 30121 |
amit.gupta |
482 |
List<Integer> catalogIds = itemRepository.getCatalogIds(itemCriteria);
|
| 29783 |
amit.gupta |
483 |
for (PayoutSlab payoutSlab : Lists.reverse(itemCriteriaPayout.getPayoutSlabs())) {
|
|
|
484 |
if (itemCriteriaPayout.getAmountType().equals(AmountType.FIXED)) {
|
| 30121 |
amit.gupta |
485 |
for (Integer catalogId : catalogIds) {
|
|
|
486 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
487 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
| 29783 |
amit.gupta |
488 |
}
|
| 30121 |
amit.gupta |
489 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
| 29783 |
amit.gupta |
490 |
(long) payoutSlab.getPayoutAmount());
|
|
|
491 |
}
|
|
|
492 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.SLAB_FIXED)) {
|
| 30121 |
amit.gupta |
493 |
for (Integer catalogId : catalogIds) {
|
|
|
494 |
if (!catalogSlabPayoutMap.containsKey(catalogId)) {
|
|
|
495 |
catalogSlabPayoutMap.put(catalogId, new LinkedHashMap<>());
|
| 29783 |
amit.gupta |
496 |
}
|
| 30121 |
amit.gupta |
497 |
catalogSlabPayoutMap.get(catalogId).put(payoutSlab.getOnwardsAmount(),
|
| 29783 |
amit.gupta |
498 |
(long) (payoutSlab.getPayoutAmount() / payoutSlab.getOnwardsAmount()));
|
|
|
499 |
}
|
|
|
500 |
} else if (itemCriteriaPayout.getAmountType().equals(AmountType.PERCENTAGE)) {
|
| 30139 |
amit.gupta |
501 |
Map<Integer, TagListing> tagListingsMap = tagListingRepository.selectAllByCatalogIds(catalogIds);
|
|
|
502 |
for (Map.Entry<Integer, TagListing> tagListingEntry : tagListingsMap.entrySet()) {
|
|
|
503 |
TagListing tagListing = tagListingEntry.getValue();
|
|
|
504 |
if (!catalogSlabPayoutMap.containsKey(tagListingEntry.getKey())) {
|
|
|
505 |
catalogSlabPayoutMap.put(tagListingEntry.getKey(), new LinkedHashMap<>());
|
| 29783 |
amit.gupta |
506 |
}
|
| 30139 |
amit.gupta |
507 |
catalogSlabPayoutMap.get(tagListingEntry.getKey()).put(payoutSlab.getOnwardsAmount(),
|
| 30957 |
amit.gupta |
508 |
(long) (tagListing.getSellingPrice() * payoutSlab.getPayoutAmount() / 100));
|
| 29783 |
amit.gupta |
509 |
}
|
|
|
510 |
}
|
|
|
511 |
}
|
|
|
512 |
}
|
| 30121 |
amit.gupta |
513 |
return catalogSlabPayoutMap;
|
| 29783 |
amit.gupta |
514 |
}
|
|
|
515 |
|
| 27877 |
amit.gupta |
516 |
}
|