| Line 3... |
Line 3... |
| 3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 4 |
import com.spice.profitmandi.common.model.CustomPaymentOption;
|
4 |
import com.spice.profitmandi.common.model.CustomPaymentOption;
|
| 5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 6 |
import com.spice.profitmandi.dao.entity.dtr.ScratchOffer;
|
6 |
import com.spice.profitmandi.dao.entity.dtr.ScratchOffer;
|
| 7 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
7 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| - |
|
8 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 8 |
import com.spice.profitmandi.dao.entity.scratch.GiftEntity;
|
9 |
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.OfferEntity;
|
| 10 |
import com.spice.profitmandi.dao.entity.scratch.ScratchRequest;
|
11 |
import com.spice.profitmandi.dao.entity.scratch.ScratchRequest;
|
| 11 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
12 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 12 |
import com.spice.profitmandi.dao.repository.dtr.ScratchOfferRepository;
|
13 |
import com.spice.profitmandi.dao.repository.dtr.ScratchOfferRepository;
|
| Line 191... |
Line 192... |
| 191 |
private boolean checkGiftAvailability(LocalDate date, int perDayMaxQty) {
|
192 |
private boolean checkGiftAvailability(LocalDate date, int perDayMaxQty) {
|
| 192 |
Integer giftCounts = scratchOfferRepository.countTodaysGiftDistribution(date.atStartOfDay(), date.atTime(LocalTime.MAX));
|
193 |
Integer giftCounts = scratchOfferRepository.countTodaysGiftDistribution(date.atStartOfDay(), date.atTime(LocalTime.MAX));
|
| 193 |
return giftCounts < perDayMaxQty;
|
194 |
return giftCounts < perDayMaxQty;
|
| 194 |
}
|
195 |
}
|
| 195 |
|
196 |
|
| - |
|
197 |
private boolean isAppleItem(FofoOrder fofoOrder) {
|
| - |
|
198 |
List<FofoOrderItem> fofoOrderItems = fofoOrderItemRepository.selectByOrderId(fofoOrder.getId());
|
| - |
|
199 |
if (fofoOrderItems == null || fofoOrderItems.isEmpty()) {
|
| - |
|
200 |
return false;
|
| - |
|
201 |
}
|
| - |
|
202 |
for (FofoOrderItem item : fofoOrderItems) {
|
| - |
|
203 |
if (item != null && item.getBrand() != null && item.getBrand().equalsIgnoreCase("apple")) {
|
| - |
|
204 |
return true;
|
| - |
|
205 |
}
|
| - |
|
206 |
}
|
| - |
|
207 |
return false;
|
| - |
|
208 |
}
|
| 196 |
private boolean processOffer(OfferEntity offer, FofoOrder fofoOrder, Integer itemCategoryId) {
|
209 |
private boolean processOffer(OfferEntity offer, FofoOrder fofoOrder, Integer itemCategoryId) {
|
| 197 |
List<GiftEntity> allGifts = giftRepository.findByOfferId(offer.getId());
|
210 |
List<GiftEntity> allGifts = giftRepository.findByOfferId(offer.getId());
|
| 198 |
logger.info("Processing offer {} for order {}", offer.getId(), fofoOrder.getId());
|
211 |
logger.info("Processing offer {} for order {}", offer.getId(), fofoOrder.getId());
|
| - |
|
212 |
if (isAppleItem(fofoOrder)) {
|
| - |
|
213 |
GiftEntity lastGift = allGifts.get(allGifts.size() - 1);
|
| - |
|
214 |
if (lastGift.getMaxRedemptions() > 0) {
|
| - |
|
215 |
createScratchOffer(fofoOrder, offer, lastGift);
|
| - |
|
216 |
logger.info("Apple item detected, assigned last gift: {}", lastGift.getName());
|
| - |
|
217 |
} else {
|
| - |
|
218 |
logger.info("Apple last gift exhausted, assigning Better Luck Next Time");
|
| - |
|
219 |
createBetterLuckNextTimeScratch(fofoOrder, offer);
|
| - |
|
220 |
}
|
| 199 |
//Is more gift available
|
221 |
return true;
|
| - |
|
222 |
}
|
| 200 |
int perDayMaxQty = offer.getUserLimit();
|
223 |
int perDayMaxQty = offer.getUserLimit();
|
| 201 |
LocalDate today = LocalDate.now();
|
224 |
LocalDate today = LocalDate.now();
|
| 202 |
boolean isMoreGiftAvailable = checkGiftAvailability(today, perDayMaxQty);
|
225 |
boolean isMoreGiftAvailable = checkGiftAvailability(today, perDayMaxQty);
|
| 203 |
logger.info("Gift availability result: {}", isMoreGiftAvailable);
|
226 |
logger.info("Gift availability result: {}", isMoreGiftAvailable);
|
| 204 |
|
227 |
|
| Line 378... |
Line 401... |
| 378 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
401 |
logger.warn("BrandSpecific gift selection not implemented yet");
|
| 379 |
return null;
|
402 |
return null;
|
| 380 |
}
|
403 |
}
|
| 381 |
|
404 |
|
| 382 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
405 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
| - |
|
406 |
public void createBetterLuckNextTimeScratch(FofoOrder order, OfferEntity offer) {
|
| - |
|
407 |
try {
|
| - |
|
408 |
ScratchOffer so = new ScratchOffer();
|
| - |
|
409 |
so.setCustomerId(order.getCustomerId());
|
| - |
|
410 |
so.setOfferId(offer.getId());
|
| - |
|
411 |
so.setGiftId(1);
|
| - |
|
412 |
so.setInvoiceNumber(order.getInvoiceNumber());
|
| - |
|
413 |
so.setCreatedTimestamp(LocalDateTime.now());
|
| - |
|
414 |
so.setOfferName(offer.getName());
|
| - |
|
415 |
|
| - |
|
416 |
scratchOfferRepository.persist(so);
|
| - |
|
417 |
logger.info("Created Better Luck Next Time scratch offer: {}", so);
|
| - |
|
418 |
} catch (Exception e) {
|
| - |
|
419 |
logger.error("Error creating Better Luck Next Time scratch offer: {}", e.getMessage());
|
| - |
|
420 |
}
|
| - |
|
421 |
}
|
| - |
|
422 |
|
| - |
|
423 |
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
| 383 |
public void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
424 |
public void createScratchOffer(FofoOrder order, OfferEntity offer, GiftEntity gift) {
|
| 384 |
try {
|
425 |
try {
|
| 385 |
GiftEntity currentGift = giftRepository.findById(gift.getId());
|
426 |
GiftEntity currentGift = giftRepository.findById(gift.getId());
|
| 386 |
|
427 |
|
| 387 |
if (currentGift == null || currentGift.getMaxRedemptions() <= 0) {
|
428 |
if (currentGift == null || currentGift.getMaxRedemptions() <= 0) {
|