| Line 16... |
Line 16... |
| 16 |
import com.spice.profitmandi.dao.repository.scratch.SeasonalOfferRepository;
|
16 |
import com.spice.profitmandi.dao.repository.scratch.SeasonalOfferRepository;
|
| 17 |
import org.apache.logging.log4j.LogManager;
|
17 |
import org.apache.logging.log4j.LogManager;
|
| 18 |
import org.apache.logging.log4j.Logger;
|
18 |
import org.apache.logging.log4j.Logger;
|
| 19 |
import org.springframework.beans.factory.annotation.Autowired;
|
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 20 |
import org.springframework.stereotype.Service;
|
20 |
import org.springframework.stereotype.Service;
|
| - |
|
21 |
import org.springframework.transaction.annotation.Propagation;
|
| 21 |
import org.springframework.transaction.annotation.Transactional;
|
22 |
import org.springframework.transaction.annotation.Transactional;
|
| 22 |
|
23 |
|
| 23 |
import java.time.LocalDate;
|
24 |
import java.time.LocalDate;
|
| 24 |
import java.time.LocalDateTime;
|
25 |
import java.time.LocalDateTime;
|
| 25 |
import java.time.LocalTime;
|
26 |
import java.time.LocalTime;
|
| Line 41... |
Line 42... |
| 41 |
FofoOrderItemRepository fofoOrderItemRepository;
|
42 |
FofoOrderItemRepository fofoOrderItemRepository;
|
| 42 |
@Autowired
|
43 |
@Autowired
|
| 43 |
ItemRepository itemRepository;
|
44 |
ItemRepository itemRepository;
|
| 44 |
@Autowired
|
45 |
@Autowired
|
| 45 |
FofoOrderRepository fofoOrderRepository;
|
46 |
FofoOrderRepository fofoOrderRepository;
|
| - |
|
47 |
|
| - |
|
48 |
|
| 46 |
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
49 |
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
| 47 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<Integer> fofoOrderItemIds) throws ProfitMandiBusinessException {
|
50 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<Integer> fofoOrderItemIds) throws ProfitMandiBusinessException {
|
| 48 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
51 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
| 49 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
52 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
| 50 |
LocalDate today = LocalDate.now();
|
53 |
LocalDate today = LocalDate.now();
|
| 51 |
for (Integer orderItemId : fofoOrderItemIds) {
|
54 |
for (Integer orderItemId : fofoOrderItemIds) {
|
| Line 68... |
Line 71... |
| 68 |
// logger.info("Customer {} has chosen a different mode of payment.", fofoOrder.getCustomerId());
|
71 |
// logger.info("Customer {} has chosen a different mode of payment.", fofoOrder.getCustomerId());
|
| 69 |
// return;
|
72 |
// return;
|
| 70 |
// }
|
73 |
// }
|
| 71 |
if (eligibleOffer != null) {
|
74 |
if (eligibleOffer != null) {
|
| 72 |
logger.info("Found active offer: {}", eligibleOffer.getId());
|
75 |
logger.info("Found active offer: {}", eligibleOffer.getId());
|
| 73 |
// Get the customer's scratch list and check if this offer is already there
|
- |
|
| 74 |
Integer activeOfferId = eligibleOffer.getId();
|
76 |
Integer activeOfferId = eligibleOffer.getId();
|
| 75 |
logger.info
|
77 |
logger.info
|
| 76 |
("Active offer ID: {}, Type: {}", activeOfferId, eligibleOffer.getName());
|
78 |
("Active offer ID: {}, Type: {}", activeOfferId, eligibleOffer.getName());
|
| 77 |
ScratchOffer availedOffer = scratchOfferRepository.getScractchOfferByCustomerIdAndOfferId(fofoOrder.getCustomerId(), activeOfferId);
|
79 |
ScratchOffer availedOffer = scratchOfferRepository.getScractchOfferByCustomerIdAndOfferId(fofoOrder.getCustomerId(), activeOfferId);
|
| 78 |
|
80 |
|
| 79 |
if (availedOffer != null) {
|
81 |
if (availedOffer != null) {
|
| 80 |
logger.info("Customer {} has already scratched offer ID {}. No new gift will be allotted.", fofoOrder.getCustomerId(), activeOfferId);
|
82 |
logger.info("Customer {} has already scratched offer ID {}. No new gift will be allotted.", fofoOrder.getCustomerId(), activeOfferId);
|
| 81 |
return; // Customer already has this offer
|
83 |
return;
|
| 82 |
}
|
84 |
}
|
| 83 |
|
85 |
|
| 84 |
// Proceed with processing the offer since customer hasn't scratched it yet
|
- |
|
| 85 |
if (this.processOffer(eligibleOffer, fofoOrder, itemCategoryId)) {
|
86 |
if (this.processOffer(eligibleOffer, fofoOrder, itemCategoryId)) {
|
| 86 |
logger.info("Successfully processed offer for item: {}", orderItemId);
|
87 |
logger.info("Successfully processed offer for item: {}", orderItemId);
|
| 87 |
return; // Successfully processed an offer
|
88 |
break;
|
| 88 |
} else {
|
89 |
} else {
|
| 89 |
logger.info("Failed to process offer for item: {}", orderItemId);
|
90 |
logger.info("Failed to process offer for item: {}", orderItemId);
|
| 90 |
}
|
91 |
}
|
| 91 |
} else {
|
92 |
} else {
|
| 92 |
logger.info("No active offers found for today: {}", today);
|
93 |
logger.info("No active offers found for today: {}", today);
|
| 93 |
}
|
94 |
}
|
| 94 |
|
95 |
|
| 95 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
96 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
| 96 |
return; // No eligible gifts found for any items
|
97 |
return;
|
| 97 |
}
|
98 |
}
|
| 98 |
|
99 |
|
| 99 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
100 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
| 100 |
}
|
101 |
}
|
| 101 |
|
102 |
|
| 102 |
private OfferEntity FindEligibleOffer(List<OfferEntity> activeOffers, FofoOrder fofoOrder) {
|
103 |
private OfferEntity FindEligibleOffer(List<OfferEntity> activeOffers, FofoOrder fofoOrder) {
|
| 103 |
int customerFofoId = fofoOrder.getFofoId();
|
104 |
int customerFofoId = fofoOrder.getFofoId();
|
| - |
|
105 |
logger.info("Looking for eligible offer for customer fofoId: {}", customerFofoId);
|
| - |
|
106 |
logger.info("Total active offers to check: {}", activeOffers.size());
|
| - |
|
107 |
|
| 104 |
for (OfferEntity activeOffer : activeOffers) {
|
108 |
for (OfferEntity activeOffer : activeOffers) {
|
| - |
|
109 |
logger.info("Checking offer ID: {} with classification: {}", activeOffer.getId(), activeOffer.getClassification());
|
| - |
|
110 |
|
| 105 |
if ("PartnerWise".equals(activeOffer.getClassification())) {
|
111 |
if ("PartnerWise".equals(activeOffer.getClassification())) {
|
| 106 |
List<GiftEntity> gifts = giftRepository.findByOfferId(activeOffer.getId());
|
112 |
List<GiftEntity> gifts = giftRepository.findByOfferId(activeOffer.getId());
|
| - |
|
113 |
logger.info("Found {} gifts for PartnerWise offer {}", gifts.size(), activeOffer.getId());
|
| - |
|
114 |
|
| - |
|
115 |
if (gifts.isEmpty()) {
|
| - |
|
116 |
logger.warn("No gifts found for PartnerWise offer {}", activeOffer.getId());
|
| - |
|
117 |
continue;
|
| - |
|
118 |
}
|
| 107 |
|
119 |
|
| 108 |
for (GiftEntity gift : gifts) {
|
120 |
for (GiftEntity gift : gifts) {
|
| - |
|
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 |
|
| 109 |
String[] fofoStores = gift.getFofoStore().split(",");
|
128 |
String[] fofoStores = gift.getFofoStore().split(",");
|
| - |
|
129 |
logger.info("Gift {} fofoStores array: {}", gift.getId(), Arrays.toString(fofoStores));
|
| - |
|
130 |
|
| 110 |
boolean isEligible = Arrays.stream(fofoStores)
|
131 |
boolean isEligible = Arrays.stream(fofoStores)
|
| 111 |
.map(Integer::parseInt) // Convert string to int
|
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 -> {
|
| 112 |
.anyMatch(fofoStore -> fofoStore == customerFofoId);
|
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 |
|
| 113 |
if (isEligible) {
|
151 |
if (isEligible) {
|
| - |
|
152 |
logger.info("Found matching PartnerWise offer: {} for customer fofoId: {}", activeOffer.getId(), customerFofoId);
|
| 114 |
return activeOffer; // Return the matching PARTNERWISE offer
|
153 |
return activeOffer;
|
| 115 |
}
|
154 |
}
|
| 116 |
}
|
155 |
}
|
| - |
|
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());
|
| 117 |
}
|
159 |
}
|
| 118 |
}
|
160 |
}
|
| - |
|
161 |
|
| 119 |
// If no PARTNERWISE offer matches, return the first non-PARTNERWISE offer (if any)
|
162 |
logger.info("No PartnerWise offers matched. Looking for non-PartnerWise offers...");
|
| - |
|
163 |
|
| 120 |
return activeOffers.stream()
|
164 |
List<OfferEntity> nonPartnerWiseOffers = activeOffers.stream()
|
| 121 |
.filter(offer -> !"PartnerWise".equals(offer.getClassification()))
|
165 |
.filter(offer -> !"PartnerWise".equals(offer.getClassification()))
|
| 122 |
.findFirst()
|
166 |
.collect(Collectors.toList());
|
| - |
|
167 |
|
| - |
|
168 |
logger.info("Found {} non-PartnerWise offers", nonPartnerWiseOffers.size());
|
| - |
|
169 |
|
| - |
|
170 |
OfferEntity selectedOffer = nonPartnerWiseOffers.stream().findFirst().orElse(null);
|
| - |
|
171 |
|
| 123 |
.orElse(null);
|
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;
|
| 124 |
}
|
179 |
}
|
| 125 |
|
180 |
|
| - |
|
181 |
|
| 126 |
private Item getItemById(Integer itemId) throws ProfitMandiBusinessException {
|
182 |
private Item getItemById(Integer itemId) throws ProfitMandiBusinessException {
|
| 127 |
logger.info("Getting item by ID: {}", itemId);
|
183 |
logger.info("Getting item by ID: {}", itemId);
|
| 128 |
Item item = itemRepository.selectById(itemId);
|
184 |
Item item = itemRepository.selectById(itemId);
|
| 129 |
if (item == null) {
|
185 |
if (item == null) {
|
| 130 |
logger.warn("Item not found with ID: {}", itemId);
|
186 |
logger.warn("Item not found with ID: {}", itemId);
|
| Line 144... |
Line 200... |
| 144 |
int perDayMaxQty = offer.getUserLimit();
|
200 |
int perDayMaxQty = offer.getUserLimit();
|
| 145 |
LocalDate today = LocalDate.now();
|
201 |
LocalDate today = LocalDate.now();
|
| 146 |
boolean isMoreGiftAvailable = checkGiftAvailability(today, perDayMaxQty);
|
202 |
boolean isMoreGiftAvailable = checkGiftAvailability(today, perDayMaxQty);
|
| 147 |
logger.info("Gift availability result: {}", isMoreGiftAvailable);
|
203 |
logger.info("Gift availability result: {}", isMoreGiftAvailable);
|
| 148 |
|
204 |
|
| - |
|
205 |
if (allGifts.isEmpty()) {
|
| - |
|
206 |
logger.warn("Offer {} has no gifts - cannot process scratch offer", offer.getId());
|
| - |
|
207 |
return false;
|
| - |
|
208 |
}
|
| 149 |
if (!isMoreGiftAvailable) {
|
209 |
if (!isMoreGiftAvailable) {
|
| 150 |
logger.info("Daily limit reached - assigning default gift");
|
210 |
logger.info("Daily limit reached - assigning default gift");
|
| 151 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
211 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
| 152 |
return true;
|
212 |
return true;
|
| 153 |
}
|
213 |
}
|
| Line 223... |
Line 283... |
| 223 |
|
283 |
|
| 224 |
private boolean isItemCategoryEligible(String giftCategories, Integer itemCategoryId) {
|
284 |
private boolean isItemCategoryEligible(String giftCategories, Integer itemCategoryId) {
|
| 225 |
logger.info
|
285 |
logger.info
|
| 226 |
("Checking if item category {} is in gift categories: {}", itemCategoryId, giftCategories);
|
286 |
("Checking if item category {} is in gift categories: {}", itemCategoryId, giftCategories);
|
| 227 |
|
287 |
|
| 228 |
// Handle null or empty categories
|
- |
|
| - |
|
288 |
|
| 229 |
if (giftCategories == null || giftCategories.trim().isEmpty()) {
|
289 |
if (giftCategories == null || giftCategories.trim().isEmpty()) {
|
| 230 |
return false;
|
290 |
return false;
|
| 231 |
}
|
291 |
}
|
| 232 |
|
292 |
|
| 233 |
return Arrays.stream(giftCategories.split(","))
|
293 |
return Arrays.stream(giftCategories.split(","))
|
| Line 235... |
Line 295... |
| 235 |
.map(category -> {
|
295 |
.map(category -> {
|
| 236 |
try {
|
296 |
try {
|
| 237 |
return Integer.parseInt(category.trim());
|
297 |
return Integer.parseInt(category.trim());
|
| 238 |
} catch (NumberFormatException e) {
|
298 |
} catch (NumberFormatException e) {
|
| 239 |
logger.warn("Invalid category format in gift categories: {}", category);
|
299 |
logger.warn("Invalid category format in gift categories: {}", category);
|
| 240 |
return -1; // Will not match any real category ID
|
300 |
return -1;
|
| 241 |
}
|
301 |
}
|
| 242 |
})
|
302 |
})
|
| 243 |
.anyMatch(itemCategoryId::equals);
|
303 |
.anyMatch(itemCategoryId::equals);
|
| 244 |
}
|
304 |
}
|
| 245 |
|
305 |
|
| Line 284... |
Line 344... |
| 284 |
return gifts.get(0);
|
344 |
return gifts.get(0);
|
| 285 |
}
|
345 |
}
|
| 286 |
|
346 |
|
| 287 |
private GiftEntity handleRandomGifts(List<GiftEntity> gifts) {
|
347 |
private GiftEntity handleRandomGifts(List<GiftEntity> gifts) {
|
| 288 |
logger.info("Handling Random gifts selection from {} gifts", gifts.size());
|
348 |
logger.info("Handling Random gifts selection from {} gifts", gifts.size());
|
| 289 |
// Create a new list for remaining gifts to avoid modifying the original list
|
- |
|
| 290 |
List<GiftEntity> giftPool = new ArrayList<>(gifts.subList(0, gifts.size()));
|
349 |
List<GiftEntity> giftPool = new ArrayList<>(gifts.subList(0, gifts.size()));
|
| 291 |
// Shuffle the gift pool to increase randomness
|
- |
|
| 292 |
Collections.shuffle(giftPool);
|
350 |
Collections.shuffle(giftPool);
|
| 293 |
// Find a valid gift from the shuffled pool
|
- |
|
| 294 |
return giftPool.get(0);
|
351 |
return giftPool.get(0);
|
| 295 |
}
|
352 |
}
|
| 296 |
|
353 |
|
| 297 |
private GiftEntity handlePartnerWiseGifts(List<GiftEntity> gifts, FofoOrder order) {
|
354 |
private GiftEntity handlePartnerWiseGifts(List<GiftEntity> gifts, FofoOrder order) {
|
| 298 |
int fofoId = order.getFofoId();
|
355 |
int fofoId = order.getFofoId();
|
| 299 |
logger.info("Handling PartnerWise gifts selection from {} gifts for fofoId {}", gifts.size(), fofoId);
|
356 |
logger.info("Handling PartnerWise gifts selection from {} gifts for fofoId {}", gifts.size(), fofoId);
|
| 300 |
// Stream approach - consistent with FindEligibleOffer method
|
- |
|
| 301 |
return gifts.stream()
|
357 |
return gifts.stream()
|
| 302 |
.filter(gift -> {
|
358 |
.filter(gift -> {
|
| 303 |
String[] fofoStores = gift.getFofoStore().split(",");
|
359 |
String[] fofoStores = gift.getFofoStore().split(",");
|
| 304 |
return Arrays.stream(fofoStores)
|
360 |
return Arrays.stream(fofoStores)
|
| 305 |
.map(storeId -> {
|
361 |
.map(storeId -> {
|
| 306 |
try {
|
362 |
try {
|
| 307 |
return Integer.parseInt(storeId.trim());
|
363 |
return Integer.parseInt(storeId.trim());
|
| 308 |
} catch (NumberFormatException e) {
|
364 |
} catch (NumberFormatException e) {
|
| 309 |
logger.warn("Invalid fofoId format in gift {}: {}", gift.getId(), storeId);
|
365 |
logger.warn("Invalid fofoId format in gift {}: {}", gift.getId(), storeId);
|
| 310 |
return -1; // Invalid ID that won't match
|
366 |
return -1;
|
| 311 |
}
|
367 |
}
|
| 312 |
})
|
368 |
})
|
| 313 |
.anyMatch(storeId -> storeId == fofoId);
|
369 |
.anyMatch(storeId -> storeId == fofoId);
|
| 314 |
})
|
370 |
})
|
| 315 |
.findFirst()
|
371 |
.findFirst()
|
| Line 321... |
Line 377... |
| 321 |
// TODO: Add brand-specific logic
|
377 |
// TODO: Add brand-specific logic
|
| 322 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
378 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
| 323 |
return null;
|
379 |
return null;
|
| 324 |
}
|
380 |
}
|
| 325 |
|
381 |
|
| - |
|
382 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
| 326 |
private void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
383 |
public void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
| - |
|
384 |
try {
|
| - |
|
385 |
GiftEntity currentGift = giftRepository.findById(gift.getId());
|
| - |
|
386 |
|
| - |
|
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 |
|
| 327 |
ScratchOffer so = new ScratchOffer();
|
396 |
ScratchOffer so = new ScratchOffer();
|
| 328 |
so.setCustomerId(order.getCustomerId());
|
397 |
so.setCustomerId(order.getCustomerId());
|
| 329 |
so.setOfferId(offer.getId());
|
398 |
so.setOfferId(offer.getId());
|
| 330 |
so.setGiftId(gift.getId());
|
399 |
so.setGiftId(gift.getId());
|
| 331 |
so.setInvoiceNumber(order.getInvoiceNumber());
|
400 |
so.setInvoiceNumber(order.getInvoiceNumber());
|
| 332 |
so.setCreatedTimestamp(LocalDateTime.now());
|
401 |
so.setCreatedTimestamp(LocalDateTime.now());
|
| 333 |
so.setOfferName(gift.getName());
|
402 |
so.setOfferName(gift.getName());
|
| 334 |
|
403 |
|
| 335 |
gift.setMaxRedemptions(gift.getMaxRedemptions() - 1);
|
- |
|
| 336 |
scratchOfferRepository.persist(so);
|
404 |
scratchOfferRepository.persist(so);
|
| 337 |
logger.info("Created scratch offer: {}", so);
|
405 |
logger.info("Created scratch offer: {}", so);
|
| - |
|
406 |
|
| - |
|
407 |
} catch (Exception e) {
|
| - |
|
408 |
logger.error("Error creating scratch offer: {}", e.getMessage());
|
| - |
|
409 |
|
| - |
|
410 |
}
|
| 338 |
}
|
411 |
}
|
| 339 |
|
412 |
|
| - |
|
413 |
|
| - |
|
414 |
|
| 340 |
public boolean createOffer(ScratchRequest request) {
|
415 |
public boolean createOffer(ScratchRequest request) {
|
| 341 |
OfferEntity offer = mapToOfferEntity(request);
|
416 |
OfferEntity offer = mapToOfferEntity(request);
|
| 342 |
seasonalOfferRepository.save(offer);
|
417 |
seasonalOfferRepository.save(offer);
|
| 343 |
List<GiftEntity> gifts = mapToGiftEntities(request.getGifts(), offer.getId());
|
418 |
List<GiftEntity> gifts = mapToGiftEntities(request.getGifts(), offer.getId());
|
| 344 |
gifts.forEach(giftRepository::save);
|
419 |
gifts.forEach(giftRepository::save);
|