| 34474 |
aman.kumar |
1 |
package com.spice.profitmandi.dao.service;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.common.model.CustomPaymentOption;
|
|
|
5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.dtr.ScratchOffer;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
|
|
8 |
import com.spice.profitmandi.dao.entity.scratch.GiftEntity;
|
|
|
9 |
import com.spice.profitmandi.dao.entity.scratch.OfferEntity;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.scratch.ScratchRequest;
|
|
|
11 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
12 |
import com.spice.profitmandi.dao.repository.dtr.ScratchOfferRepository;
|
|
|
13 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderItemRepository;
|
|
|
14 |
import com.spice.profitmandi.dao.repository.fofo.FofoOrderRepository;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.scratch.GiftRepository;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.scratch.SeasonalOfferRepository;
|
|
|
17 |
import org.apache.logging.log4j.LogManager;
|
|
|
18 |
import org.apache.logging.log4j.Logger;
|
|
|
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
20 |
import org.springframework.stereotype.Service;
|
| 35095 |
aman |
21 |
import org.springframework.transaction.annotation.Propagation;
|
| 34474 |
aman.kumar |
22 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
23 |
|
|
|
24 |
import java.time.LocalDate;
|
|
|
25 |
import java.time.LocalDateTime;
|
|
|
26 |
import java.time.LocalTime;
|
|
|
27 |
import java.util.*;
|
|
|
28 |
import java.util.function.Function;
|
|
|
29 |
import java.util.stream.Collectors;
|
|
|
30 |
|
|
|
31 |
@Service
|
|
|
32 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
33 |
public class ScratchService {
|
|
|
34 |
private static final Logger logger = LogManager.getLogger(ScratchService.class);
|
|
|
35 |
@Autowired
|
|
|
36 |
SeasonalOfferRepository seasonalOfferRepository;
|
|
|
37 |
@Autowired
|
|
|
38 |
GiftRepository giftRepository;
|
|
|
39 |
@Autowired
|
|
|
40 |
ScratchOfferRepository scratchOfferRepository;
|
|
|
41 |
@Autowired
|
|
|
42 |
FofoOrderItemRepository fofoOrderItemRepository;
|
|
|
43 |
@Autowired
|
|
|
44 |
ItemRepository itemRepository;
|
|
|
45 |
@Autowired
|
|
|
46 |
FofoOrderRepository fofoOrderRepository;
|
| 35095 |
aman |
47 |
|
|
|
48 |
|
|
|
49 |
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
| 34804 |
aman.kumar |
50 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<Integer> fofoOrderItemIds) throws ProfitMandiBusinessException {
|
| 34474 |
aman.kumar |
51 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
|
|
52 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
|
|
53 |
LocalDate today = LocalDate.now();
|
| 34804 |
aman.kumar |
54 |
for (Integer orderItemId : fofoOrderItemIds) {
|
|
|
55 |
logger.info("Processing order item: {}", orderItemId);
|
|
|
56 |
Item item = this.getItemById(orderItemId);
|
| 34474 |
aman.kumar |
57 |
Integer itemCategoryId = item.getCategoryId();
|
|
|
58 |
logger.info("Item category ID: {}", itemCategoryId);
|
|
|
59 |
// Find active offer
|
|
|
60 |
List<OfferEntity> activeOffers = seasonalOfferRepository.getOffersByDate(today);
|
| 34804 |
aman.kumar |
61 |
logger.info("Found Active offer: {}", activeOffers);
|
| 34474 |
aman.kumar |
62 |
OfferEntity eligibleOffer = FindEligibleOffer(activeOffers, fofoOrder);
|
| 34804 |
aman.kumar |
63 |
// Set<String> allowedPaymentMethods = Arrays.stream(eligibleOffer.getPaymentMethod().split(","))
|
|
|
64 |
// .collect(Collectors.toSet());
|
|
|
65 |
//
|
|
|
66 |
// boolean isOtherPaymentUsed = paymentOptions.stream()
|
|
|
67 |
// .map(option -> String.valueOf(option.getPaymentOptionId())) // Replace with the correct getter
|
|
|
68 |
// .anyMatch(paymentId -> !allowedPaymentMethods.contains(paymentId));
|
|
|
69 |
// // proceed with default because user payment method doest not match
|
|
|
70 |
// if (isOtherPaymentUsed) {
|
|
|
71 |
// logger.info("Customer {} has chosen a different mode of payment.", fofoOrder.getCustomerId());
|
|
|
72 |
// return;
|
|
|
73 |
// }
|
| 34474 |
aman.kumar |
74 |
if (eligibleOffer != null) {
|
|
|
75 |
logger.info("Found active offer: {}", eligibleOffer.getId());
|
|
|
76 |
Integer activeOfferId = eligibleOffer.getId();
|
|
|
77 |
logger.info
|
|
|
78 |
("Active offer ID: {}, Type: {}", activeOfferId, eligibleOffer.getName());
|
|
|
79 |
ScratchOffer availedOffer = scratchOfferRepository.getScractchOfferByCustomerIdAndOfferId(fofoOrder.getCustomerId(), activeOfferId);
|
|
|
80 |
|
|
|
81 |
if (availedOffer != null) {
|
|
|
82 |
logger.info("Customer {} has already scratched offer ID {}. No new gift will be allotted.", fofoOrder.getCustomerId(), activeOfferId);
|
| 35095 |
aman |
83 |
return;
|
| 34474 |
aman.kumar |
84 |
}
|
|
|
85 |
|
|
|
86 |
if (this.processOffer(eligibleOffer, fofoOrder, itemCategoryId)) {
|
| 34804 |
aman.kumar |
87 |
logger.info("Successfully processed offer for item: {}", orderItemId);
|
| 35095 |
aman |
88 |
break;
|
| 34474 |
aman.kumar |
89 |
} else {
|
| 34804 |
aman.kumar |
90 |
logger.info("Failed to process offer for item: {}", orderItemId);
|
| 34474 |
aman.kumar |
91 |
}
|
|
|
92 |
} else {
|
|
|
93 |
logger.info("No active offers found for today: {}", today);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
| 35095 |
aman |
97 |
return;
|
| 34474 |
aman.kumar |
98 |
}
|
|
|
99 |
|
|
|
100 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private OfferEntity FindEligibleOffer(List<OfferEntity> activeOffers, FofoOrder fofoOrder) {
|
| 34804 |
aman.kumar |
104 |
int customerFofoId = fofoOrder.getFofoId();
|
| 35095 |
aman |
105 |
logger.info("Looking for eligible offer for customer fofoId: {}", customerFofoId);
|
|
|
106 |
logger.info("Total active offers to check: {}", activeOffers.size());
|
|
|
107 |
|
| 34474 |
aman.kumar |
108 |
for (OfferEntity activeOffer : activeOffers) {
|
| 35095 |
aman |
109 |
logger.info("Checking offer ID: {} with classification: {}", activeOffer.getId(), activeOffer.getClassification());
|
|
|
110 |
|
| 34474 |
aman.kumar |
111 |
if ("PartnerWise".equals(activeOffer.getClassification())) {
|
|
|
112 |
List<GiftEntity> gifts = giftRepository.findByOfferId(activeOffer.getId());
|
| 35095 |
aman |
113 |
logger.info("Found {} gifts for PartnerWise offer {}", gifts.size(), activeOffer.getId());
|
| 34474 |
aman.kumar |
114 |
|
| 35095 |
aman |
115 |
if (gifts.isEmpty()) {
|
|
|
116 |
logger.warn("No gifts found for PartnerWise offer {}", activeOffer.getId());
|
|
|
117 |
continue;
|
|
|
118 |
}
|
|
|
119 |
|
| 34474 |
aman.kumar |
120 |
for (GiftEntity gift : gifts) {
|
| 35095 |
aman |
121 |
logger.info("Checking gift ID: {} with fofoStore: '{}'", gift.getId(), gift.getFofoStore());
|
|
|
122 |
|
|
|
123 |
if (gift.getFofoStore() == null || gift.getFofoStore().trim().isEmpty()) {
|
|
|
124 |
logger.warn("Gift {} has null/empty fofoStore - skipping", gift.getId());
|
|
|
125 |
continue;
|
|
|
126 |
}
|
|
|
127 |
|
| 34474 |
aman.kumar |
128 |
String[] fofoStores = gift.getFofoStore().split(",");
|
| 35095 |
aman |
129 |
logger.info("Gift {} fofoStores array: {}", gift.getId(), Arrays.toString(fofoStores));
|
|
|
130 |
|
| 34474 |
aman.kumar |
131 |
boolean isEligible = Arrays.stream(fofoStores)
|
| 35095 |
aman |
132 |
.filter(store -> !store.trim().isEmpty())
|
|
|
133 |
.map(store -> {
|
|
|
134 |
try {
|
|
|
135 |
int storeId = Integer.parseInt(store.trim());
|
|
|
136 |
logger.info("Parsed fofoStore: {} (comparing with customer fofoId: {})", storeId, customerFofoId);
|
|
|
137 |
return storeId;
|
|
|
138 |
} catch (NumberFormatException e) {
|
|
|
139 |
logger.warn("Invalid fofoId format in gift {}: '{}' - skipping", gift.getId(), store);
|
|
|
140 |
return -1;
|
|
|
141 |
}
|
|
|
142 |
})
|
|
|
143 |
.anyMatch(fofoStore -> {
|
|
|
144 |
boolean matches = fofoStore == customerFofoId;
|
|
|
145 |
logger.info("FofoStore {} matches customer fofoId {}: {}", fofoStore, customerFofoId, matches);
|
|
|
146 |
return matches;
|
|
|
147 |
});
|
|
|
148 |
|
|
|
149 |
logger.info("Gift {} is eligible for customer fofoId {}: {}", gift.getId(), customerFofoId, isEligible);
|
|
|
150 |
|
| 34474 |
aman.kumar |
151 |
if (isEligible) {
|
| 35095 |
aman |
152 |
logger.info("Found matching PartnerWise offer: {} for customer fofoId: {}", activeOffer.getId(), customerFofoId);
|
|
|
153 |
return activeOffer;
|
| 34474 |
aman.kumar |
154 |
}
|
|
|
155 |
}
|
| 35095 |
aman |
156 |
logger.info("No matching gifts found in PartnerWise offer {} for customer fofoId: {}", activeOffer.getId(), customerFofoId);
|
|
|
157 |
} else {
|
|
|
158 |
logger.info("Offer {} is not PartnerWise (classification: {})", activeOffer.getId(), activeOffer.getClassification());
|
| 34474 |
aman.kumar |
159 |
}
|
|
|
160 |
}
|
| 35095 |
aman |
161 |
|
|
|
162 |
logger.info("No PartnerWise offers matched. Looking for non-PartnerWise offers...");
|
|
|
163 |
|
|
|
164 |
List<OfferEntity> nonPartnerWiseOffers = activeOffers.stream()
|
| 34474 |
aman.kumar |
165 |
.filter(offer -> !"PartnerWise".equals(offer.getClassification()))
|
| 35095 |
aman |
166 |
.collect(Collectors.toList());
|
|
|
167 |
|
|
|
168 |
logger.info("Found {} non-PartnerWise offers", nonPartnerWiseOffers.size());
|
|
|
169 |
|
|
|
170 |
OfferEntity selectedOffer = nonPartnerWiseOffers.stream().findFirst().orElse(null);
|
|
|
171 |
|
|
|
172 |
if (selectedOffer != null) {
|
|
|
173 |
logger.info("Selected non-PartnerWise offer: {}", selectedOffer.getId());
|
|
|
174 |
} else {
|
|
|
175 |
logger.info("No eligible offers found for customer fofoId: {}", customerFofoId);
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
return selectedOffer;
|
| 34474 |
aman.kumar |
179 |
}
|
|
|
180 |
|
| 35095 |
aman |
181 |
|
| 34474 |
aman.kumar |
182 |
private Item getItemById(Integer itemId) throws ProfitMandiBusinessException {
|
|
|
183 |
logger.info("Getting item by ID: {}", itemId);
|
|
|
184 |
Item item = itemRepository.selectById(itemId);
|
|
|
185 |
if (item == null) {
|
|
|
186 |
logger.warn("Item not found with ID: {}", itemId);
|
|
|
187 |
}
|
|
|
188 |
return item;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
private boolean checkGiftAvailability(LocalDate date, int perDayMaxQty) {
|
|
|
192 |
Integer giftCounts = scratchOfferRepository.countTodaysGiftDistribution(date.atStartOfDay(), date.atTime(LocalTime.MAX));
|
|
|
193 |
return giftCounts < perDayMaxQty;
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private boolean processOffer(OfferEntity offer, FofoOrder fofoOrder, Integer itemCategoryId) {
|
|
|
197 |
List<GiftEntity> allGifts = giftRepository.findByOfferId(offer.getId());
|
|
|
198 |
logger.info("Processing offer {} for order {}", offer.getId(), fofoOrder.getId());
|
|
|
199 |
//Is more gift available
|
|
|
200 |
int perDayMaxQty = offer.getUserLimit();
|
|
|
201 |
LocalDate today = LocalDate.now();
|
|
|
202 |
boolean isMoreGiftAvailable = checkGiftAvailability(today, perDayMaxQty);
|
|
|
203 |
logger.info("Gift availability result: {}", isMoreGiftAvailable);
|
|
|
204 |
|
| 35095 |
aman |
205 |
if (allGifts.isEmpty()) {
|
|
|
206 |
logger.warn("Offer {} has no gifts - cannot process scratch offer", offer.getId());
|
|
|
207 |
return false;
|
|
|
208 |
}
|
| 34474 |
aman.kumar |
209 |
if (!isMoreGiftAvailable) {
|
|
|
210 |
logger.info("Daily limit reached - assigning default gift");
|
|
|
211 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
|
|
212 |
return true;
|
|
|
213 |
}
|
|
|
214 |
// Get eligible gifts
|
|
|
215 |
List<GiftEntity> eligibleGifts = findEligibleGifts(offer, fofoOrder, itemCategoryId);
|
|
|
216 |
if (eligibleGifts.isEmpty()) {
|
|
|
217 |
logger.info("No eligible gifts found for offer: {}", offer.getId());
|
|
|
218 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
|
|
219 |
return true;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
logger.info("Found {} eligible gifts for offer {}", eligibleGifts.size(), offer.getId());
|
|
|
223 |
|
|
|
224 |
// Select gift based on classification
|
|
|
225 |
GiftEntity selectedGift = selectGiftByClassification(offer, eligibleGifts, fofoOrder);
|
|
|
226 |
|
|
|
227 |
if (selectedGift == null || selectedGift.getMaxRedemptions() <= 0) {
|
|
|
228 |
logger.info("No valid gift selected for offer: {}", offer.getId());
|
|
|
229 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
| 34804 |
aman.kumar |
230 |
return true;
|
| 34474 |
aman.kumar |
231 |
}
|
|
|
232 |
|
|
|
233 |
logger.info("Selected gift: {} for order: {}", selectedGift.getId(), fofoOrder.getId());
|
|
|
234 |
|
|
|
235 |
// Create scratch offer
|
|
|
236 |
createScratchOffer(fofoOrder, offer, selectedGift);
|
|
|
237 |
logger.info("Created scratch offer for order: {}", fofoOrder.getId());
|
|
|
238 |
return true;
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
private List<GiftEntity> findEligibleGifts(OfferEntity offer, FofoOrder fofoOrder, Integer itemCategoryId) {
|
|
|
243 |
logger.info("Finding eligible gifts for offer: {} and category: {}", offer.getId(), itemCategoryId);
|
|
|
244 |
List<GiftEntity> activeOfferGifts = giftRepository.findByOfferId(offer.getId());
|
|
|
245 |
logger.info("Total gifts for offer {}: {}", offer.getId(), activeOfferGifts.size());
|
|
|
246 |
|
|
|
247 |
float totalAmount = fofoOrder.getTotalAmount();
|
|
|
248 |
logger.info("Order total amount: {}", totalAmount);
|
|
|
249 |
|
|
|
250 |
// Filter eligible gifts
|
|
|
251 |
List<GiftEntity> eligibleGifts = activeOfferGifts.stream().filter(gift -> isGiftEligible(gift, totalAmount, itemCategoryId)).collect(Collectors.toList());
|
|
|
252 |
|
|
|
253 |
logger.info("Filtered to {} eligible gifts", eligibleGifts.size());
|
|
|
254 |
|
|
|
255 |
for (GiftEntity gift : activeOfferGifts) {
|
|
|
256 |
boolean isEligible = isGiftEligible(gift, totalAmount, itemCategoryId);
|
|
|
257 |
logger.info("Gift ID: {}, Name: {}, MinCart: {}, MaxCart: {}, Categories: {}, MaxRedemptions: {}, Eligible: {}",
|
|
|
258 |
gift.getId(), gift.getName(), gift.getMinCartValue(), gift.getMaxCartValue(),
|
|
|
259 |
gift.getProductCategory(), gift.getMaxRedemptions(), isEligible);
|
|
|
260 |
}
|
|
|
261 |
return eligibleGifts;
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
private boolean isGiftEligible(GiftEntity gift, float totalAmount, Integer itemCategoryId) {
|
|
|
265 |
logger.info("Checking eligibility for gift: {}", gift.getId());
|
|
|
266 |
// Check cart value condition
|
|
|
267 |
boolean cartValueCondition = totalAmount >= gift.getMinCartValue() && totalAmount <= gift.getMaxCartValue() && gift.getMaxRedemptions() > 0;
|
|
|
268 |
logger.info(
|
|
|
269 |
"Gift {} cart value condition: {} (order amount: {}, min: {}, max: {}, max redemptions: {})",
|
|
|
270 |
gift.getId(), cartValueCondition, totalAmount,
|
|
|
271 |
gift.getMinCartValue(), gift.getMaxCartValue(),
|
|
|
272 |
gift.getMaxRedemptions());
|
|
|
273 |
// Check category condition
|
|
|
274 |
boolean categoryCondition = isItemCategoryEligible(gift.getProductCategory(), itemCategoryId);
|
|
|
275 |
logger.info
|
|
|
276 |
("Gift {} category condition: {} (item category: {}, gift categories: {})", gift.getId(), categoryCondition, itemCategoryId, gift.getProductCategory());
|
|
|
277 |
boolean isEligible = cartValueCondition && categoryCondition;
|
|
|
278 |
logger.info
|
|
|
279 |
("Gift {} is eligible: {}", gift.getId(), isEligible);
|
|
|
280 |
|
|
|
281 |
return isEligible;
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
private boolean isItemCategoryEligible(String giftCategories, Integer itemCategoryId) {
|
|
|
285 |
logger.info
|
|
|
286 |
("Checking if item category {} is in gift categories: {}", itemCategoryId, giftCategories);
|
|
|
287 |
|
| 35095 |
aman |
288 |
|
| 34474 |
aman.kumar |
289 |
if (giftCategories == null || giftCategories.trim().isEmpty()) {
|
|
|
290 |
return false;
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
return Arrays.stream(giftCategories.split(","))
|
|
|
294 |
.filter(category -> !category.trim().isEmpty()) // Filter out empty strings
|
|
|
295 |
.map(category -> {
|
|
|
296 |
try {
|
|
|
297 |
return Integer.parseInt(category.trim());
|
|
|
298 |
} catch (NumberFormatException e) {
|
|
|
299 |
logger.warn("Invalid category format in gift categories: {}", category);
|
| 35095 |
aman |
300 |
return -1;
|
| 34474 |
aman.kumar |
301 |
}
|
|
|
302 |
})
|
|
|
303 |
.anyMatch(itemCategoryId::equals);
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
private GiftEntity selectGiftByClassification(OfferEntity offer, List<GiftEntity> eligibleGifts, FofoOrder fofoOrder) {
|
|
|
307 |
String classification = offer.getClassification();
|
|
|
308 |
logger.info("Selecting gift by classification: {}", classification);
|
|
|
309 |
|
|
|
310 |
GiftEntity selectedGift;
|
|
|
311 |
switch (classification) {
|
|
|
312 |
case "Serial":
|
|
|
313 |
logger.info("Using Serial gift selection strategy");
|
|
|
314 |
selectedGift = handleSerialGifts(eligibleGifts);
|
|
|
315 |
break;
|
|
|
316 |
case "Random":
|
|
|
317 |
logger.info("Using Random gift selection strategy");
|
|
|
318 |
selectedGift = handleRandomGifts(eligibleGifts);
|
|
|
319 |
break;
|
|
|
320 |
case "PartnerWise":
|
|
|
321 |
logger.info("Using PartnerWise gift selection strategy");
|
|
|
322 |
selectedGift = handlePartnerWiseGifts(eligibleGifts, fofoOrder);
|
|
|
323 |
break;
|
|
|
324 |
case "BrandSpecific":
|
|
|
325 |
logger.info("Using BrandSpecific gift selection strategy");
|
|
|
326 |
selectedGift = handleBrandSpecificGifts(eligibleGifts, fofoOrder);
|
|
|
327 |
break;
|
|
|
328 |
default:
|
|
|
329 |
logger.warn("Unknown classification: {}", classification);
|
|
|
330 |
selectedGift = null;
|
|
|
331 |
}
|
|
|
332 |
|
|
|
333 |
if (selectedGift != null) {
|
|
|
334 |
logger.info("Selected gift: {}", selectedGift.getId());
|
|
|
335 |
} else {
|
|
|
336 |
logger.warn("No gift selected for classification: {}", classification);
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
return selectedGift;
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
private GiftEntity handleSerialGifts(List<GiftEntity> gifts) {
|
|
|
343 |
logger.info("Handling Serial gifts selection from {} gifts", gifts.size());
|
|
|
344 |
return gifts.get(0);
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
private GiftEntity handleRandomGifts(List<GiftEntity> gifts) {
|
|
|
348 |
logger.info("Handling Random gifts selection from {} gifts", gifts.size());
|
|
|
349 |
List<GiftEntity> giftPool = new ArrayList<>(gifts.subList(0, gifts.size()));
|
|
|
350 |
Collections.shuffle(giftPool);
|
|
|
351 |
return giftPool.get(0);
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
private GiftEntity handlePartnerWiseGifts(List<GiftEntity> gifts, FofoOrder order) {
|
|
|
355 |
int fofoId = order.getFofoId();
|
|
|
356 |
logger.info("Handling PartnerWise gifts selection from {} gifts for fofoId {}", gifts.size(), fofoId);
|
|
|
357 |
return gifts.stream()
|
|
|
358 |
.filter(gift -> {
|
|
|
359 |
String[] fofoStores = gift.getFofoStore().split(",");
|
|
|
360 |
return Arrays.stream(fofoStores)
|
|
|
361 |
.map(storeId -> {
|
|
|
362 |
try {
|
|
|
363 |
return Integer.parseInt(storeId.trim());
|
|
|
364 |
} catch (NumberFormatException e) {
|
|
|
365 |
logger.warn("Invalid fofoId format in gift {}: {}", gift.getId(), storeId);
|
| 35095 |
aman |
366 |
return -1;
|
| 34474 |
aman.kumar |
367 |
}
|
|
|
368 |
})
|
|
|
369 |
.anyMatch(storeId -> storeId == fofoId);
|
|
|
370 |
})
|
|
|
371 |
.findFirst()
|
|
|
372 |
.orElse(null);
|
|
|
373 |
}
|
|
|
374 |
|
|
|
375 |
private GiftEntity handleBrandSpecificGifts(List<GiftEntity> gifts, FofoOrder order) {
|
|
|
376 |
logger.info("Handling BrandSpecific gifts selection from {} gifts for order {}", gifts.size(), order.getId());
|
|
|
377 |
// TODO: Add brand-specific logic
|
|
|
378 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
|
|
379 |
return null;
|
|
|
380 |
}
|
|
|
381 |
|
| 35095 |
aman |
382 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
|
|
383 |
public void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
|
|
384 |
try {
|
|
|
385 |
GiftEntity currentGift = giftRepository.findById(gift.getId());
|
| 34474 |
aman.kumar |
386 |
|
| 35095 |
aman |
387 |
if (currentGift == null || currentGift.getMaxRedemptions() <= 0) {
|
|
|
388 |
logger.warn("Gift {} has no remaining redemptions", gift.getId());
|
|
|
389 |
return;
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
// Decrement and save
|
|
|
393 |
currentGift.setMaxRedemptions(currentGift.getMaxRedemptions() - 1);
|
|
|
394 |
giftRepository.save(currentGift);
|
|
|
395 |
|
|
|
396 |
ScratchOffer so = new ScratchOffer();
|
|
|
397 |
so.setCustomerId(order.getCustomerId());
|
|
|
398 |
so.setOfferId(offer.getId());
|
|
|
399 |
so.setGiftId(gift.getId());
|
|
|
400 |
so.setInvoiceNumber(order.getInvoiceNumber());
|
|
|
401 |
so.setCreatedTimestamp(LocalDateTime.now());
|
|
|
402 |
so.setOfferName(gift.getName());
|
|
|
403 |
|
|
|
404 |
scratchOfferRepository.persist(so);
|
|
|
405 |
logger.info("Created scratch offer: {}", so);
|
|
|
406 |
|
|
|
407 |
} catch (Exception e) {
|
|
|
408 |
logger.error("Error creating scratch offer: {}", e.getMessage());
|
|
|
409 |
|
|
|
410 |
}
|
| 34474 |
aman.kumar |
411 |
}
|
|
|
412 |
|
| 35095 |
aman |
413 |
|
|
|
414 |
|
| 34474 |
aman.kumar |
415 |
public boolean createOffer(ScratchRequest request) {
|
|
|
416 |
OfferEntity offer = mapToOfferEntity(request);
|
|
|
417 |
seasonalOfferRepository.save(offer);
|
|
|
418 |
List<GiftEntity> gifts = mapToGiftEntities(request.getGifts(), offer.getId());
|
|
|
419 |
gifts.forEach(giftRepository::save);
|
|
|
420 |
return offer.getId() > 0 && giftRepository.findByOfferId(offer.getId()).size() == request.getGifts().size();
|
|
|
421 |
}
|
|
|
422 |
|
|
|
423 |
public boolean updateOffer(Integer offerId, ScratchRequest request) {
|
|
|
424 |
OfferEntity existingOffer = seasonalOfferRepository.findById(offerId);
|
|
|
425 |
boolean res = false;
|
|
|
426 |
if (existingOffer != null) {
|
|
|
427 |
updateOfferEntity(existingOffer, request);
|
|
|
428 |
updateGifts(existingOffer.getId(), request.getGifts());
|
|
|
429 |
res = true;
|
|
|
430 |
}
|
|
|
431 |
return res;
|
|
|
432 |
}
|
|
|
433 |
|
|
|
434 |
//update offer
|
|
|
435 |
private void updateOfferEntity(OfferEntity existingOffer, ScratchRequest request) {
|
|
|
436 |
existingOffer.setName(request.getName());
|
|
|
437 |
existingOffer.setDescription(request.getDescription());
|
|
|
438 |
existingOffer.setStartDate(request.getStartDate());
|
|
|
439 |
existingOffer.setEndDate(request.getEndDate());
|
|
|
440 |
existingOffer.setTermsCondition(request.getTermsCondition());
|
|
|
441 |
existingOffer.setUserLimit(request.getUserLimit());
|
|
|
442 |
existingOffer.setScratchValidity(request.getScratchValidity());
|
|
|
443 |
existingOffer.setProductCategory(request.getProductCategory());
|
|
|
444 |
existingOffer.setPaymentMethod(request.getPaymentMethod());
|
|
|
445 |
existingOffer.setClassification(request.getClassification());
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
//update gift
|
|
|
449 |
private void updateGifts(Integer offerId, List<ScratchRequest.GiftData> giftsData) {
|
|
|
450 |
Map<Integer, GiftEntity> existingGifts = giftRepository.findByOfferId(offerId).stream().collect(Collectors.toMap(GiftEntity::getId, Function.identity()));
|
|
|
451 |
|
|
|
452 |
for (ScratchRequest.GiftData giftData : giftsData) {
|
|
|
453 |
if (giftData.getId() != null && existingGifts.containsKey(giftData.getId())) {
|
|
|
454 |
GiftEntity existingGift = existingGifts.get(giftData.getId());
|
|
|
455 |
updateGiftEntity(existingGift, giftData);
|
|
|
456 |
existingGifts.remove(giftData.getId());
|
|
|
457 |
} else {
|
|
|
458 |
GiftEntity gift = new GiftEntity();
|
|
|
459 |
gift.setName(giftData.getName());
|
|
|
460 |
gift.setThumbnailUrl(giftData.getThumbnailUrl());
|
|
|
461 |
gift.setMinCartValue(giftData.getMinCartValue());
|
|
|
462 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
|
|
463 |
gift.setOfferId(offerId);
|
|
|
464 |
gift.setMaxCartValue(giftData.getMaxCartValue());
|
|
|
465 |
gift.setFofoStore(giftData.getFofoStore());
|
|
|
466 |
gift.setProductCategory(giftData.getProductCategory());
|
|
|
467 |
giftRepository.save(gift);
|
|
|
468 |
}
|
|
|
469 |
}
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
private void updateGiftEntity(GiftEntity gift, ScratchRequest.GiftData data) {
|
|
|
473 |
gift.setName(data.getName());
|
|
|
474 |
gift.setThumbnailUrl(data.getThumbnailUrl());
|
|
|
475 |
gift.setMinCartValue(data.getMinCartValue());
|
|
|
476 |
gift.setMaxRedemptions(data.getMaxRedemptions());
|
|
|
477 |
gift.setMaxCartValue(data.getMaxCartValue());
|
|
|
478 |
gift.setFofoStore(data.getFofoStore());
|
|
|
479 |
gift.setProductCategory(data.getProductCategory());
|
|
|
480 |
}
|
|
|
481 |
|
|
|
482 |
//offer
|
|
|
483 |
private OfferEntity mapToOfferEntity(ScratchRequest request) {
|
|
|
484 |
OfferEntity offer = new OfferEntity();
|
|
|
485 |
offer.setName(request.getName());
|
|
|
486 |
offer.setDescription(request.getDescription());
|
|
|
487 |
offer.setOfferImage(request.getOfferImage());
|
|
|
488 |
offer.setOfferType(request.getOfferType());
|
|
|
489 |
offer.setStartDate(request.getStartDate());
|
|
|
490 |
offer.setEndDate(request.getEndDate());
|
|
|
491 |
offer.setTermsCondition(request.getTermsCondition());
|
|
|
492 |
offer.setUserLimit(request.getUserLimit());
|
|
|
493 |
offer.setScratchValidity(request.getScratchValidity());
|
|
|
494 |
offer.setProductCategory(request.getProductCategory());
|
|
|
495 |
offer.setPaymentMethod(request.getPaymentMethod());
|
|
|
496 |
offer.setClassification(request.getClassification());
|
| 34804 |
aman.kumar |
497 |
logger.info("active flag - " + request.isActive());
|
| 34474 |
aman.kumar |
498 |
offer.setActive(request.isActive());
|
|
|
499 |
return offer;
|
|
|
500 |
}
|
|
|
501 |
|
|
|
502 |
//gift
|
|
|
503 |
private List<GiftEntity> mapToGiftEntities(List<ScratchRequest.GiftData> giftsData, Integer offerId) {
|
|
|
504 |
return giftsData.stream().map(giftData -> {
|
| 34804 |
aman.kumar |
505 |
logger.info("giftData: {}", giftData);
|
| 34474 |
aman.kumar |
506 |
GiftEntity gift = new GiftEntity();
|
|
|
507 |
gift.setName(giftData.getName());
|
|
|
508 |
gift.setThumbnailUrl(giftData.getThumbnailUrl());
|
|
|
509 |
gift.setMinCartValue(giftData.getMinCartValue());
|
|
|
510 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
|
|
511 |
gift.setOfferId(offerId);
|
|
|
512 |
gift.setMaxCartValue(giftData.getMaxCartValue());
|
|
|
513 |
gift.setFofoStore(giftData.getFofoStore());
|
|
|
514 |
gift.setProductCategory(giftData.getProductCategory());
|
|
|
515 |
return gift;
|
|
|
516 |
}).collect(Collectors.toList());
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
}
|