| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.dao.cart;
|
1 |
package com.spice.profitmandi.dao.cart;
|
| 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.ProfitMandiConstants;
|
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| - |
|
5 |
import org.springframework.beans.BeanUtils;
|
| 5 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
6 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 6 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
7 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 7 |
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
|
8 |
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
|
| 8 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
9 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| 9 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
10 |
import com.spice.profitmandi.dao.entity.fofo.PartnerType;
|
| Line 46... |
Line 47... |
| 46 |
@Component
|
47 |
@Component
|
| 47 |
public class CartServiceImpl implements CartService {
|
48 |
public class CartServiceImpl implements CartService {
|
| 48 |
|
49 |
|
| 49 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
50 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
| 50 |
|
51 |
|
| - |
|
52 |
private static final float CARRY_BAG_MIN_PHONE_PRICE = 12000f;
|
| - |
|
53 |
private static final float CARRY_BAG_FLAT_PRICE = 1f;
|
| - |
|
54 |
|
| 51 |
private static final Logger logger = LogManager.getLogger(CartServiceImpl.class);
|
55 |
private static final Logger logger = LogManager.getLogger(CartServiceImpl.class);
|
| 52 |
|
56 |
|
| 53 |
private static final Map<Integer, List<Integer>> focusedModelVariantMap = new HashMap<>();
|
57 |
private static final Map<Integer, List<Integer>> focusedModelVariantMap = new HashMap<>();
|
| 54 |
|
58 |
|
| 55 |
@Autowired
|
59 |
@Autowired
|
| Line 249... |
Line 253... |
| 249 |
warehouseItemQtyMap.put(itemId, existingQty + intransitData.getQty());
|
253 |
warehouseItemQtyMap.put(itemId, existingQty + intransitData.getQty());
|
| 250 |
}
|
254 |
}
|
| 251 |
}
|
255 |
}
|
| 252 |
cart.setTotalPrice(0);
|
256 |
cart.setTotalPrice(0);
|
| 253 |
int nonAccessoryQuantity = 0;
|
257 |
int nonAccessoryQuantity = 0;
|
| - |
|
258 |
int carryBagAllowance = carryBagAllowance(cartLines.stream()
|
| - |
|
259 |
.collect(Collectors.toMap(CartLine::getItemId, CartLine::getQuantity, Integer::sum)), itemMap, tagListingsMap);
|
| - |
|
260 |
CarryBagQuote carryBagQuote = null;
|
| 254 |
for (CartLine cartLine : cartLines) {
|
261 |
for (CartLine cartLine : cartLines) {
|
| 255 |
CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();
|
262 |
CartItemResponseModel cartItemResponseModel = new CartItemResponseModel();
|
| 256 |
int oldEstimate = cartLine.getEstimate();
|
263 |
int oldEstimate = cartLine.getEstimate();
|
| 257 |
int itemId = cartLine.getItemId();
|
264 |
int itemId = cartLine.getItemId();
|
| 258 |
// N+1 fix: Use pre-fetched itemMap instead of querying per item
|
265 |
// N+1 fix: Use pre-fetched itemMap instead of querying per item
|
| Line 290... |
Line 297... |
| 290 |
|
297 |
|
| 291 |
if (item.getCategoryId() == 10006 || item.getCategoryId() == 10010) {
|
298 |
if (item.getCategoryId() == 10006 || item.getCategoryId() == 10010) {
|
| 292 |
nonAccessoryQuantity += cartItemResponseModel.getQuantity();
|
299 |
nonAccessoryQuantity += cartItemResponseModel.getQuantity();
|
| 293 |
cartItemResponseModel.setCategoryName("mobile");
|
300 |
cartItemResponseModel.setCategoryName("mobile");
|
| 294 |
}
|
301 |
}
|
| 295 |
if (cartItemResponseModel.getItemId() == ProfitMandiConstants.ITEM_CARRY_BAG) {
|
302 |
if (itemId == ProfitMandiConstants.ITEM_CARRY_BAG) {
|
| 296 |
if (partnerType.equals(PartnerType.BRONZE)) {
|
303 |
carryBagQuote = carryBagQuote(partnerType, carryBagAllowance, cartLine.getQuantity(), tagListingsMap.get(itemId));
|
| 297 |
cartLine.setActualPrice(tagListingsMap.get(itemId).getSellingPrice());
|
304 |
cartLine.setActualPrice(carryBagQuote.getUnitPrice());
|
| 298 |
cartItemResponseModel.setSellingPrice(cartLine.getActualPrice());
|
305 |
cartItemResponseModel.setSellingPrice(cartLine.getActualPrice());
|
| 299 |
} else {
|
- |
|
| 300 |
cartItemResponseModel.setSellingPrice(1);
|
- |
|
| 301 |
cartLine.setActualPrice(1);
|
- |
|
| 302 |
}
|
- |
|
| 303 |
} else {
|
306 |
} else {
|
| 304 |
// Check for price mismatch between cart and current TagListing
|
307 |
// Check for price mismatch between cart and current TagListing
|
| 305 |
TagListing currentTagListing = tagListingsMap.get(itemId);
|
308 |
TagListing currentTagListing = tagListingsMap.get(itemId);
|
| 306 |
if (currentTagListing != null) {
|
309 |
if (currentTagListing != null) {
|
| 307 |
float cartPrice = cartLine.getActualPrice();
|
310 |
float cartPrice = cartLine.getActualPrice();
|
| Line 350... |
Line 353... |
| 350 |
}
|
353 |
}
|
| 351 |
}
|
354 |
}
|
| 352 |
cartResponse.setTotalQty(totalQty);
|
355 |
cartResponse.setTotalQty(totalQty);
|
| 353 |
cartResponse.setTotalAmount(totalAmount);
|
356 |
cartResponse.setTotalAmount(totalAmount);
|
| 354 |
cartResponse.setNonAccessoryQuantity(nonAccessoryQuantity);
|
357 |
cartResponse.setNonAccessoryQuantity(nonAccessoryQuantity);
|
| - |
|
358 |
cartResponse.setCarryBag(carryBagQuote != null ? carryBagQuote
|
| - |
|
359 |
: carryBagQuote(partnerType, carryBagAllowance, 0, tagListingRepository.selectByItemId(ProfitMandiConstants.ITEM_CARRY_BAG)));
|
| 355 |
cartResponse.setShippingCharge(0);
|
360 |
cartResponse.setShippingCharge(0);
|
| 356 |
cartResponse.setCartMessageChanged(cartMessageChanged);
|
361 |
cartResponse.setCartMessageChanged(cartMessageChanged);
|
| 357 |
cartResponse.setCartMessageOOS(cartMessageOOS);
|
362 |
cartResponse.setCartMessageOOS(cartMessageOOS);
|
| 358 |
cartResponse.setCartMessageUndeliverable(cartMessageUndeliverable);
|
363 |
cartResponse.setCartMessageUndeliverable(cartMessageUndeliverable);
|
| 359 |
cartResponse.setCod(false);
|
364 |
cartResponse.setCod(false);
|
| Line 416... |
Line 421... |
| 416 |
cart.setAddressId((int) addressId);
|
421 |
cart.setAddressId((int) addressId);
|
| 417 |
|
422 |
|
| 418 |
}
|
423 |
}
|
| 419 |
|
424 |
|
| 420 |
@Override
|
425 |
@Override
|
| 421 |
public void addShoppingBag(int cartId, long qty) {
|
- |
|
| 422 |
// Cart row first, then lines -- see CartRepository.selectByIdForUpdate.
|
426 |
public CarryBagQuote getCarryBagQuote(int fofoId, Map<Integer, Integer> itemQty) throws ProfitMandiBusinessException {
|
| 423 |
cartRepository.selectByIdForUpdate(cartId);
|
- |
|
| 424 |
CartLine cl = new CartLine();
|
427 |
Set<Integer> itemIds = new HashSet<>(itemQty.keySet());
|
| 425 |
cl.setItemId(ProfitMandiConstants.ITEM_CARRY_BAG);
|
428 |
itemIds.add(ProfitMandiConstants.ITEM_CARRY_BAG);
|
| 426 |
cl.setQuantity((int) qty);
|
429 |
Map<Integer, Item> itemMap = itemRepository.selectByIds(itemIds).stream()
|
| 427 |
cl.setCartId(cartId);
|
430 |
.collect(Collectors.toMap(Item::getId, x -> x));
|
| 428 |
cl.setCreateTimestamp(LocalDateTime.now());
|
431 |
Map<Integer, TagListing> tagListingsMap = tagListingRepository.selectByItemIdsAndTagIds(itemIds, tagIds).stream()
|
| 429 |
cl.setDataProtectionAmount(0);
|
432 |
.collect(Collectors.toMap(TagListing::getItemId, x -> x, (a, b) -> a));
|
| 430 |
cl.setEstimate(0);
|
433 |
return carryBagQuote(partnerTypeChangeService.getTypeOnDate(fofoId, LocalDate.now()),
|
| 431 |
cl.setInsuranceAmount(0f);
|
434 |
carryBagAllowance(itemQty, itemMap, tagListingsMap),
|
| 432 |
cl.setDataProtectionInsurer(0);
|
435 |
itemQty.getOrDefault(ProfitMandiConstants.ITEM_CARRY_BAG, 0),
|
| 433 |
cl.setActualPrice(0.01f);
|
436 |
tagListingsMap.get(ProfitMandiConstants.ITEM_CARRY_BAG));
|
| 434 |
cartLineRepository.persist(cl);
|
437 |
}
|
| 435 |
|
438 |
|
| - |
|
439 |
@Override
|
| - |
|
440 |
public List<CartLine> splitCarryBagLine(int fofoId, List<CartLine> cartLines) throws ProfitMandiBusinessException {
|
| - |
|
441 |
CartLine bagLine = cartLines.stream()
|
| - |
|
442 |
.filter(x -> x.getItemId() == ProfitMandiConstants.ITEM_CARRY_BAG).findFirst().orElse(null);
|
| - |
|
443 |
if (bagLine == null) return cartLines;
|
| - |
|
444 |
CarryBagQuote quote = this.getCarryBagQuote(fofoId, cartLines.stream()
|
| - |
|
445 |
.collect(Collectors.toMap(CartLine::getItemId, CartLine::getQuantity, Integer::sum)));
|
| - |
|
446 |
List<CartLine> split = cartLines.stream().filter(x -> x != bagLine).collect(Collectors.toList());
|
| - |
|
447 |
addCarryBagPart(split, bagLine, quote.getFlatQty(), quote.getFlatPrice());
|
| - |
|
448 |
addCarryBagPart(split, bagLine, quote.getListingQty(), quote.getListingPrice());
|
| - |
|
449 |
return split;
|
| - |
|
450 |
}
|
| - |
|
451 |
|
| - |
|
452 |
// A detached copy of the bag line - never persisted, the cart keeps its single line
|
| - |
|
453 |
private static void addCarryBagPart(List<CartLine> lines, CartLine bagLine, int qty, float price) {
|
| - |
|
454 |
if (qty <= 0) return;
|
| - |
|
455 |
CartLine part = new CartLine();
|
| - |
|
456 |
BeanUtils.copyProperties(bagLine, part);
|
| - |
|
457 |
part.setQuantity(qty);
|
| - |
|
458 |
part.setActualPrice(price);
|
| - |
|
459 |
lines.add(part);
|
| - |
|
460 |
}
|
| - |
|
461 |
|
| - |
|
462 |
// Bronze partners pay the listing price for every bag
|
| - |
|
463 |
private static CarryBagQuote carryBagQuote(PartnerType partnerType, int allowance, int bags, TagListing bagListing) {
|
| - |
|
464 |
return new CarryBagQuote(allowance, bags, partnerType != PartnerType.BRONZE, CARRY_BAG_FLAT_PRICE, bagListing.getSellingPrice());
|
| - |
|
465 |
}
|
| - |
|
466 |
|
| - |
|
467 |
/** One carry bag per unit of a smartphone selling above Rs. 12,000. */
|
| - |
|
468 |
private static int carryBagAllowance(Map<Integer, Integer> itemQty, Map<Integer, Item> itemMap,
|
| - |
|
469 |
Map<Integer, TagListing> tagListingsMap) {
|
| - |
|
470 |
int allowance = 0;
|
| - |
|
471 |
for (Entry<Integer, Integer> e : itemQty.entrySet()) {
|
| - |
|
472 |
Item item = itemMap.get(e.getKey());
|
| - |
|
473 |
TagListing tagListing = tagListingsMap.get(e.getKey());
|
| - |
|
474 |
if (item != null && tagListing != null && item.getCategoryId() == ProfitMandiConstants.MOBILE_CATEGORY_ID
|
| - |
|
475 |
&& tagListing.getSellingPrice() > CARRY_BAG_MIN_PHONE_PRICE) {
|
| - |
|
476 |
allowance += e.getValue();
|
| - |
|
477 |
}
|
| - |
|
478 |
}
|
| - |
|
479 |
return allowance;
|
| 436 |
}
|
480 |
}
|
| 437 |
|
481 |
|
| 438 |
@Override
|
482 |
@Override
|
| 439 |
public CartModel removeFromCart(int cartId, int itemId) {
|
483 |
public CartModel removeFromCart(int cartId, int itemId) {
|
| 440 |
// TODO Auto-generated method stub
|
484 |
// TODO Auto-generated method stub
|