| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.dao.service;
|
1 |
package com.spice.profitmandi.dao.service;
|
| 2 |
|
2 |
|
| 3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 4 |
import com.spice.profitmandi.common.model.CustomFofoOrderItem;
|
- |
|
| 5 |
import com.spice.profitmandi.common.model.CustomPaymentOption;
|
4 |
import com.spice.profitmandi.common.model.CustomPaymentOption;
|
| 6 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 7 |
import com.spice.profitmandi.dao.entity.dtr.ScratchOffer;
|
6 |
import com.spice.profitmandi.dao.entity.dtr.ScratchOffer;
|
| 8 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
7 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 9 |
import com.spice.profitmandi.dao.entity.scratch.GiftEntity;
|
8 |
import com.spice.profitmandi.dao.entity.scratch.GiftEntity;
|
| Line 29... |
Line 28... |
| 29 |
import java.util.stream.Collectors;
|
28 |
import java.util.stream.Collectors;
|
| 30 |
|
29 |
|
| 31 |
@Service
|
30 |
@Service
|
| 32 |
@Transactional(rollbackFor = Throwable.class)
|
31 |
@Transactional(rollbackFor = Throwable.class)
|
| 33 |
public class ScratchService {
|
32 |
public class ScratchService {
|
| 34 |
private static final Logger LOGGER = LogManager.getLogger(ScratchService.class);
|
- |
|
| 35 |
private static final Logger logger = LogManager.getLogger(ScratchService.class);
|
33 |
private static final Logger logger = LogManager.getLogger(ScratchService.class);
|
| 36 |
@Autowired
|
34 |
@Autowired
|
| 37 |
SeasonalOfferRepository seasonalOfferRepository;
|
35 |
SeasonalOfferRepository seasonalOfferRepository;
|
| 38 |
@Autowired
|
36 |
@Autowired
|
| 39 |
GiftRepository giftRepository;
|
37 |
GiftRepository giftRepository;
|
| Line 43... |
Line 41... |
| 43 |
FofoOrderItemRepository fofoOrderItemRepository;
|
41 |
FofoOrderItemRepository fofoOrderItemRepository;
|
| 44 |
@Autowired
|
42 |
@Autowired
|
| 45 |
ItemRepository itemRepository;
|
43 |
ItemRepository itemRepository;
|
| 46 |
@Autowired
|
44 |
@Autowired
|
| 47 |
FofoOrderRepository fofoOrderRepository;
|
45 |
FofoOrderRepository fofoOrderRepository;
|
| 48 |
|
- |
|
| - |
|
46 |
// @Transactional(propagation = Propagation.REQUIRES_NEW)
|
| 49 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<CustomFofoOrderItem> fofoOrderItems) throws ProfitMandiBusinessException {
|
47 |
public void processScratchOffer(int fofoOrderId, Set<CustomPaymentOption> paymentOptions, Set<Integer> fofoOrderItemIds) throws ProfitMandiBusinessException {
|
| 50 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
48 |
FofoOrder fofoOrder = fofoOrderRepository.selectByOrderId(fofoOrderId);
|
| 51 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
49 |
logger.info("Starting scratchOffer for order: {}", fofoOrder);
|
| 52 |
LocalDate today = LocalDate.now();
|
50 |
LocalDate today = LocalDate.now();
|
| 53 |
for (CustomFofoOrderItem orderItem : fofoOrderItems) {
|
51 |
for (Integer orderItemId : fofoOrderItemIds) {
|
| 54 |
logger.info("Processing order item: {}", orderItem.getItemId());
|
52 |
logger.info("Processing order item: {}", orderItemId);
|
| 55 |
Item item = this.getItemById(orderItem.getItemId());
|
53 |
Item item = this.getItemById(orderItemId);
|
| 56 |
Integer itemCategoryId = item.getCategoryId();
|
54 |
Integer itemCategoryId = item.getCategoryId();
|
| 57 |
logger.info("Item category ID: {}", itemCategoryId);
|
55 |
logger.info("Item category ID: {}", itemCategoryId);
|
| 58 |
// Find active offer
|
56 |
// Find active offer
|
| 59 |
List<OfferEntity> activeOffers = seasonalOfferRepository.getOffersByDate(today);
|
57 |
List<OfferEntity> activeOffers = seasonalOfferRepository.getOffersByDate(today);
|
| - |
|
58 |
logger.info("Found Active offer: {}", activeOffers);
|
| 60 |
OfferEntity eligibleOffer = FindEligibleOffer(activeOffers, fofoOrder);
|
59 |
OfferEntity eligibleOffer = FindEligibleOffer(activeOffers, fofoOrder);
|
| - |
|
60 |
// Set<String> allowedPaymentMethods = Arrays.stream(eligibleOffer.getPaymentMethod().split(","))
|
| - |
|
61 |
// .collect(Collectors.toSet());
|
| - |
|
62 |
//
|
| - |
|
63 |
// boolean isOtherPaymentUsed = paymentOptions.stream()
|
| - |
|
64 |
// .map(option -> String.valueOf(option.getPaymentOptionId())) // Replace with the correct getter
|
| - |
|
65 |
// .anyMatch(paymentId -> !allowedPaymentMethods.contains(paymentId));
|
| - |
|
66 |
// // proceed with default because user payment method doest not match
|
| - |
|
67 |
// if (isOtherPaymentUsed) {
|
| - |
|
68 |
// logger.info("Customer {} has chosen a different mode of payment.", fofoOrder.getCustomerId());
|
| - |
|
69 |
// return;
|
| - |
|
70 |
// }
|
| 61 |
if (eligibleOffer != null) {
|
71 |
if (eligibleOffer != null) {
|
| 62 |
logger.info("Found active offer: {}", eligibleOffer.getId());
|
72 |
logger.info("Found active offer: {}", eligibleOffer.getId());
|
| 63 |
// Get the customer's scratch list and check if this offer is already there
|
73 |
// Get the customer's scratch list and check if this offer is already there
|
| 64 |
Integer activeOfferId = eligibleOffer.getId();
|
74 |
Integer activeOfferId = eligibleOffer.getId();
|
| 65 |
logger.info
|
75 |
logger.info
|
| Line 71... |
Line 81... |
| 71 |
return; // Customer already has this offer
|
81 |
return; // Customer already has this offer
|
| 72 |
}
|
82 |
}
|
| 73 |
|
83 |
|
| 74 |
// Proceed with processing the offer since customer hasn't scratched it yet
|
84 |
// Proceed with processing the offer since customer hasn't scratched it yet
|
| 75 |
if (this.processOffer(eligibleOffer, fofoOrder, itemCategoryId)) {
|
85 |
if (this.processOffer(eligibleOffer, fofoOrder, itemCategoryId)) {
|
| 76 |
logger.info("Successfully processed offer for item: {}", orderItem.getItemId());
|
86 |
logger.info("Successfully processed offer for item: {}", orderItemId);
|
| 77 |
return; // Successfully processed an offer
|
87 |
return; // Successfully processed an offer
|
| 78 |
} else {
|
88 |
} else {
|
| 79 |
logger.info("Failed to process offer for item: {}", orderItem.getItemId());
|
89 |
logger.info("Failed to process offer for item: {}", orderItemId);
|
| 80 |
}
|
90 |
}
|
| 81 |
} else {
|
91 |
} else {
|
| 82 |
logger.info("No active offers found for today: {}", today);
|
92 |
logger.info("No active offers found for today: {}", today);
|
| 83 |
}
|
93 |
}
|
| 84 |
|
94 |
|
| Line 88... |
Line 98... |
| 88 |
|
98 |
|
| 89 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
99 |
logger.info("No eligible gifts found for any items in order: {}", fofoOrder.getId());
|
| 90 |
}
|
100 |
}
|
| 91 |
|
101 |
|
| 92 |
private OfferEntity FindEligibleOffer(List<OfferEntity> activeOffers, FofoOrder fofoOrder) {
|
102 |
private OfferEntity FindEligibleOffer(List<OfferEntity> activeOffers, FofoOrder fofoOrder) {
|
| 93 |
int customerFofoId = fofoOrder.getFofoId(); // Changed to int
|
103 |
int customerFofoId = fofoOrder.getFofoId();
|
| 94 |
// First try to find a PARTNERWISE offer that matches the customer's FofoId
|
- |
|
| 95 |
for (OfferEntity activeOffer : activeOffers) {
|
104 |
for (OfferEntity activeOffer : activeOffers) {
|
| 96 |
if ("PartnerWise".equals(activeOffer.getClassification())) {
|
105 |
if ("PartnerWise".equals(activeOffer.getClassification())) {
|
| 97 |
List<GiftEntity> gifts = giftRepository.findByOfferId(activeOffer.getId());
|
106 |
List<GiftEntity> gifts = giftRepository.findByOfferId(activeOffer.getId());
|
| 98 |
|
107 |
|
| 99 |
for (GiftEntity gift : gifts) {
|
108 |
for (GiftEntity gift : gifts) {
|
| 100 |
// Check if the customer's FofoId is in the gift's FofoStore list
|
- |
|
| 101 |
String[] fofoStores = gift.getFofoStore().split(",");
|
109 |
String[] fofoStores = gift.getFofoStore().split(",");
|
| 102 |
boolean isEligible = Arrays.stream(fofoStores)
|
110 |
boolean isEligible = Arrays.stream(fofoStores)
|
| 103 |
.map(Integer::parseInt) // Convert string to int
|
111 |
.map(Integer::parseInt) // Convert string to int
|
| 104 |
.anyMatch(fofoStore -> fofoStore == customerFofoId); // Use == for int comparison
|
112 |
.anyMatch(fofoStore -> fofoStore == customerFofoId);
|
| 105 |
if (isEligible) {
|
113 |
if (isEligible) {
|
| 106 |
return activeOffer; // Return the matching PARTNERWISE offer
|
114 |
return activeOffer; // Return the matching PARTNERWISE offer
|
| 107 |
}
|
115 |
}
|
| 108 |
}
|
116 |
}
|
| 109 |
}
|
117 |
}
|
| Line 157... |
Line 165... |
| 157 |
GiftEntity selectedGift = selectGiftByClassification(offer, eligibleGifts, fofoOrder);
|
165 |
GiftEntity selectedGift = selectGiftByClassification(offer, eligibleGifts, fofoOrder);
|
| 158 |
|
166 |
|
| 159 |
if (selectedGift == null || selectedGift.getMaxRedemptions() <= 0) {
|
167 |
if (selectedGift == null || selectedGift.getMaxRedemptions() <= 0) {
|
| 160 |
logger.info("No valid gift selected for offer: {}", offer.getId());
|
168 |
logger.info("No valid gift selected for offer: {}", offer.getId());
|
| 161 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
169 |
createScratchOffer(fofoOrder, offer, allGifts.get(0));
|
| 162 |
return false;
|
170 |
return true;
|
| 163 |
}
|
171 |
}
|
| 164 |
|
172 |
|
| 165 |
logger.info("Selected gift: {} for order: {}", selectedGift.getId(), fofoOrder.getId());
|
173 |
logger.info("Selected gift: {} for order: {}", selectedGift.getId(), fofoOrder.getId());
|
| 166 |
|
174 |
|
| 167 |
// Create scratch offer
|
175 |
// Create scratch offer
|
| Line 324... |
Line 332... |
| 324 |
so.setCreatedTimestamp(LocalDateTime.now());
|
332 |
so.setCreatedTimestamp(LocalDateTime.now());
|
| 325 |
so.setOfferName(gift.getName());
|
333 |
so.setOfferName(gift.getName());
|
| 326 |
|
334 |
|
| 327 |
gift.setMaxRedemptions(gift.getMaxRedemptions() - 1);
|
335 |
gift.setMaxRedemptions(gift.getMaxRedemptions() - 1);
|
| 328 |
scratchOfferRepository.persist(so);
|
336 |
scratchOfferRepository.persist(so);
|
| 329 |
LOGGER.info("Created scratch offer: {}", so);
|
337 |
logger.info("Created scratch offer: {}", so);
|
| 330 |
}
|
338 |
}
|
| 331 |
|
339 |
|
| 332 |
public boolean createOffer(ScratchRequest request) {
|
340 |
public boolean createOffer(ScratchRequest request) {
|
| 333 |
OfferEntity offer = mapToOfferEntity(request);
|
341 |
OfferEntity offer = mapToOfferEntity(request);
|
| 334 |
seasonalOfferRepository.save(offer);
|
342 |
seasonalOfferRepository.save(offer);
|
| Line 409... |
Line 417... |
| 409 |
offer.setUserLimit(request.getUserLimit());
|
417 |
offer.setUserLimit(request.getUserLimit());
|
| 410 |
offer.setScratchValidity(request.getScratchValidity());
|
418 |
offer.setScratchValidity(request.getScratchValidity());
|
| 411 |
offer.setProductCategory(request.getProductCategory());
|
419 |
offer.setProductCategory(request.getProductCategory());
|
| 412 |
offer.setPaymentMethod(request.getPaymentMethod());
|
420 |
offer.setPaymentMethod(request.getPaymentMethod());
|
| 413 |
offer.setClassification(request.getClassification());
|
421 |
offer.setClassification(request.getClassification());
|
| 414 |
LOGGER.info("active flag - " + request.isActive());
|
422 |
logger.info("active flag - " + request.isActive());
|
| 415 |
offer.setActive(request.isActive());
|
423 |
offer.setActive(request.isActive());
|
| 416 |
return offer;
|
424 |
return offer;
|
| 417 |
}
|
425 |
}
|
| 418 |
|
426 |
|
| 419 |
//gift
|
427 |
//gift
|
| 420 |
private List<GiftEntity> mapToGiftEntities(List<ScratchRequest.GiftData> giftsData, Integer offerId) {
|
428 |
private List<GiftEntity> mapToGiftEntities(List<ScratchRequest.GiftData> giftsData, Integer offerId) {
|
| 421 |
return giftsData.stream().map(giftData -> {
|
429 |
return giftsData.stream().map(giftData -> {
|
| 422 |
LOGGER.info("giftData: {}", giftData);
|
430 |
logger.info("giftData: {}", giftData);
|
| 423 |
GiftEntity gift = new GiftEntity();
|
431 |
GiftEntity gift = new GiftEntity();
|
| 424 |
gift.setName(giftData.getName());
|
432 |
gift.setName(giftData.getName());
|
| 425 |
gift.setThumbnailUrl(giftData.getThumbnailUrl());
|
433 |
gift.setThumbnailUrl(giftData.getThumbnailUrl());
|
| 426 |
gift.setMinCartValue(giftData.getMinCartValue());
|
434 |
gift.setMinCartValue(giftData.getMinCartValue());
|
| 427 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|
435 |
gift.setMaxRedemptions(giftData.getMaxRedemptions());
|