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