| Line 1... |
Line 1... |
| 1 |
package com.spice.profitmandi.dao.cart;
|
1 |
package com.spice.profitmandi.dao.cart;
|
| 2 |
|
2 |
|
| 3 |
import java.time.LocalDateTime;
|
3 |
import java.time.LocalDateTime;
|
| 4 |
import java.util.ArrayList;
|
4 |
import java.util.ArrayList;
|
| 5 |
import java.util.Arrays;
|
5 |
import java.util.Arrays;
|
| - |
|
6 |
import java.util.Collections;
|
| 6 |
import java.util.HashMap;
|
7 |
import java.util.HashMap;
|
| 7 |
import java.util.HashSet;
|
8 |
import java.util.HashSet;
|
| - |
|
9 |
import java.util.LinkedHashMap;
|
| 8 |
import java.util.List;
|
10 |
import java.util.List;
|
| 9 |
import java.util.Map;
|
11 |
import java.util.Map;
|
| 10 |
import java.util.Set;
|
12 |
import java.util.Set;
|
| - |
|
13 |
import java.util.Map.Entry;
|
| 11 |
import java.util.stream.Collectors;
|
14 |
import java.util.stream.Collectors;
|
| 12 |
|
15 |
|
| 13 |
import org.apache.logging.log4j.LogManager;
|
16 |
import org.apache.logging.log4j.LogManager;
|
| 14 |
import org.apache.logging.log4j.Logger;
|
17 |
import org.apache.logging.log4j.Logger;
|
| 15 |
import org.springframework.beans.factory.annotation.Autowired;
|
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
| Line 17... |
Line 20... |
| 17 |
|
20 |
|
| 18 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
21 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 19 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
22 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 20 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
23 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
| 21 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
24 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| - |
|
25 |
import com.spice.profitmandi.dao.entity.cs.PartnerRegion;
|
| - |
|
26 |
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
|
| - |
|
27 |
import com.spice.profitmandi.dao.entity.inventory.SaholicCIS;
|
| 22 |
import com.spice.profitmandi.dao.entity.user.Cart;
|
28 |
import com.spice.profitmandi.dao.entity.user.Cart;
|
| 23 |
import com.spice.profitmandi.dao.entity.user.CartLine;
|
29 |
import com.spice.profitmandi.dao.entity.user.CartLine;
|
| 24 |
import com.spice.profitmandi.dao.entity.user.User;
|
30 |
import com.spice.profitmandi.dao.entity.user.User;
|
| 25 |
import com.spice.profitmandi.dao.model.CartItem;
|
31 |
import com.spice.profitmandi.dao.model.CartItem;
|
| 26 |
import com.spice.profitmandi.dao.model.CartItemResponseModel;
|
32 |
import com.spice.profitmandi.dao.model.CartItemResponseModel;
|
| 27 |
import com.spice.profitmandi.dao.model.CartMessage;
|
33 |
import com.spice.profitmandi.dao.model.CartMessage;
|
| 28 |
import com.spice.profitmandi.dao.model.CartResponse;
|
34 |
import com.spice.profitmandi.dao.model.CartResponse;
|
| - |
|
35 |
import com.spice.profitmandi.dao.repository.catalog.FocusedModelRepository;
|
| 29 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
36 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
| 30 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
37 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| - |
|
38 |
import com.spice.profitmandi.dao.repository.cs.PartnerRegionRepository;
|
| 31 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
39 |
import com.spice.profitmandi.dao.repository.dtr.FofoStoreRepository;
|
| - |
|
40 |
import com.spice.profitmandi.dao.repository.dtr.RetailerBlockBrandsRepository;
|
| 32 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
41 |
import com.spice.profitmandi.dao.repository.fofo.CurrentInventorySnapshotRepository;
|
| - |
|
42 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 33 |
import com.spice.profitmandi.dao.repository.user.CartLineRepository;
|
43 |
import com.spice.profitmandi.dao.repository.user.CartLineRepository;
|
| 34 |
import com.spice.profitmandi.dao.repository.user.CartRepository;
|
44 |
import com.spice.profitmandi.dao.repository.user.CartRepository;
|
| 35 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
45 |
import com.spice.profitmandi.dao.repository.user.UserRepository;
|
| - |
|
46 |
import com.spice.profitmandi.service.NotificationService;
|
| - |
|
47 |
import com.spice.profitmandi.service.inventory.FocusedShortageModel;
|
| 36 |
import com.spice.profitmandi.service.inventory.SaholicInventoryService;
|
48 |
import com.spice.profitmandi.service.inventory.SaholicInventoryService;
|
| - |
|
49 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
| 37 |
|
50 |
|
| 38 |
@Component
|
51 |
@Component
|
| 39 |
public class CartServiceImpl implements CartService {
|
52 |
public class CartServiceImpl implements CartService {
|
| 40 |
|
53 |
|
| 41 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
54 |
private static final Set<Integer> tagIds = new HashSet<>(Arrays.asList(4));
|
| Line 64... |
Line 77... |
| 64 |
SaholicInventoryService saholicInventoryService;
|
77 |
SaholicInventoryService saholicInventoryService;
|
| 65 |
|
78 |
|
| 66 |
@Autowired
|
79 |
@Autowired
|
| 67 |
CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
80 |
CurrentInventorySnapshotRepository currentInventorySnapshotRepository;
|
| 68 |
|
81 |
|
| - |
|
82 |
@Autowired
|
| - |
|
83 |
private FocusedModelRepository focusedModelRepository;
|
| - |
|
84 |
|
| - |
|
85 |
@Autowired
|
| - |
|
86 |
private PartnerRegionRepository partnerRegionRepository;
|
| - |
|
87 |
|
| - |
|
88 |
@Autowired
|
| - |
|
89 |
private TransactionService transactionService;
|
| - |
|
90 |
|
| - |
|
91 |
@Autowired
|
| - |
|
92 |
private RetailerBlockBrandsRepository retailerBlockBrandRepository;
|
| - |
|
93 |
|
| - |
|
94 |
@Autowired
|
| - |
|
95 |
private OrderRepository orderRepository;
|
| - |
|
96 |
|
| - |
|
97 |
@Autowired
|
| - |
|
98 |
private NotificationService notificationService;
|
| - |
|
99 |
|
| 69 |
@Override
|
100 |
@Override
|
| 70 |
//Unused method
|
101 |
// Unused method
|
| 71 |
public CartModel addToCart(int cartId, int itemId, int quantity, float sellingPrice) {
|
102 |
public CartModel addToCart(int cartId, int itemId, int quantity, float sellingPrice) {
|
| 72 |
CartLine cartItem = cartLineRepository.selectByCartItem(cartId, itemId);
|
103 |
CartLine cartItem = cartLineRepository.selectByCartItem(cartId, itemId);
|
| 73 |
if (cartItem == null) {
|
104 |
if (cartItem == null) {
|
| 74 |
this.createCartItem(cartId, itemId, quantity, sellingPrice);
|
105 |
this.createCartItem(cartId, itemId, quantity, sellingPrice);
|
| 75 |
} else {
|
106 |
} else {
|
| 76 |
cartItem.setQuantity(quantity);
|
107 |
cartItem.setQuantity(quantity);
|
| 77 |
cartItem.setActualPrice(sellingPrice);
|
108 |
cartItem.setActualPrice(sellingPrice);
|
| 78 |
}
|
109 |
}
|
| 79 |
return null;
|
110 |
return null;
|
| 80 |
}
|
111 |
}
|
| 81 |
|
112 |
|
| 82 |
private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
|
113 |
private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
|
| 83 |
CartLine cartLine = new CartLine();
|
114 |
CartLine cartLine = new CartLine();
|
| 84 |
cartLine.setActualPrice(sellingPrice);
|
115 |
cartLine.setActualPrice(sellingPrice);
|
| 85 |
cartLine.setCartId(cartId);
|
116 |
cartLine.setCartId(cartId);
|
| 86 |
cartLine.setQuantity(quantity);
|
117 |
cartLine.setQuantity(quantity);
|
| Line 325... |
Line 356... |
| 325 |
public CartModel removeFromCart(int cartId, int itemId) {
|
356 |
public CartModel removeFromCart(int cartId, int itemId) {
|
| 326 |
// TODO Auto-generated method stub
|
357 |
// TODO Auto-generated method stub
|
| 327 |
return null;
|
358 |
return null;
|
| 328 |
}
|
359 |
}
|
| 329 |
|
360 |
|
| - |
|
361 |
@Override
|
| - |
|
362 |
public List<FocusedShortageModel> focusedModelShortageValidation(int fofoId,
|
| - |
|
363 |
Map<Integer, Integer> catalogCartQtyMap) throws ProfitMandiBusinessException {
|
| - |
|
364 |
|
| - |
|
365 |
logger.info("fofoId {}", fofoId);
|
| - |
|
366 |
|
| - |
|
367 |
Map<Integer, Long> inStockItemModel = new HashMap<>();
|
| - |
|
368 |
Map<Integer, Long> itempendingIndent = new HashMap<>();
|
| - |
|
369 |
Map<Integer, Long> grnItemPendingOrders = new HashMap<>();
|
| - |
|
370 |
|
| - |
|
371 |
Map<Integer, Integer> focusedModelMap = null;
|
| - |
|
372 |
|
| - |
|
373 |
inStockItemModel = currentInventorySnapshotRepository.selectSumInMobileStockGroupByCatalog(fofoId).stream()
|
| - |
|
374 |
.collect(Collectors.toMap(x -> x.getCatalogItemId(), x -> x.getQty()));
|
| - |
|
375 |
|
| - |
|
376 |
itempendingIndent = transactionService.getInTransitOrders(fofoId).stream()
|
| - |
|
377 |
.collect(Collectors.groupingBy(x -> x.getLineItem().getItem().getCatalogItemId(),
|
| - |
|
378 |
Collectors.summingLong(x -> (long) x.getLineItem().getQuantity())));
|
| - |
|
379 |
|
| - |
|
380 |
grnItemPendingOrders = orderRepository.selectPendingGrnOrders(fofoId).stream()
|
| - |
|
381 |
.collect(Collectors.groupingBy(x -> x.getLineItem().getItem().getCatalogItemId(),
|
| - |
|
382 |
Collectors.summingLong(x -> (long) x.getLineItem().getQuantity())));
|
| - |
|
383 |
List<PartnerRegion> partnerRegions = partnerRegionRepository.selectByfofoId(fofoId);
|
| - |
|
384 |
|
| - |
|
385 |
FofoStore fofoStore = fofoStoreRepository.selectByRetailerId(fofoId);
|
| - |
|
386 |
|
| - |
|
387 |
List<String> retailerBlockBrands = retailerBlockBrandRepository.selectAllByRetailer(fofoId).stream()
|
| - |
|
388 |
.map(x -> x.getBlockBrands()).collect(Collectors.toList());
|
| - |
|
389 |
|
| - |
|
390 |
if (partnerRegions.size() > 0) {
|
| - |
|
391 |
focusedModelMap = focusedModelRepository
|
| - |
|
392 |
.selectAllByRegionIds(
|
| - |
|
393 |
partnerRegions.stream().map(x -> x.getRegionId()).collect(Collectors.toList()))
|
| - |
|
394 |
.stream().collect(Collectors.toMap(x -> x.getCatalogId(), x -> x.getMinimumQty()));
|
| - |
|
395 |
} else {
|
| - |
|
396 |
focusedModelMap = new HashMap<>();
|
| - |
|
397 |
}
|
| - |
|
398 |
|
| - |
|
399 |
Map<Integer, List<SaholicCIS>> itemAvailabilityMap = saholicInventoryService.getSaholicStock()
|
| - |
|
400 |
.get(fofoStore.getWarehouseId());
|
| - |
|
401 |
|
| - |
|
402 |
Map<Integer, Integer> catalogItemAailabilityMap = new HashMap<>();
|
| - |
|
403 |
|
| - |
|
404 |
for (Entry<Integer, Integer> fm : focusedModelMap.entrySet()) {
|
| - |
|
405 |
|
| - |
|
406 |
List<Item> items = itemRepository.selectAllByCatalogItemId(fm.getKey());
|
| - |
|
407 |
|
| - |
|
408 |
int allColorNetAvailability = 0;
|
| - |
|
409 |
for (Item item : items) {
|
| - |
|
410 |
if (retailerBlockBrands.contains(item.getBrand()))
|
| - |
|
411 |
continue;
|
| - |
|
412 |
List<SaholicCIS> currentAvailability = itemAvailabilityMap.get(item.getId());
|
| - |
|
413 |
|
| - |
|
414 |
if (currentAvailability != null) {
|
| - |
|
415 |
allColorNetAvailability += currentAvailability.stream()
|
| - |
|
416 |
.collect(Collectors.summingInt(SaholicCIS::getNetavailability));
|
| - |
|
417 |
}
|
| - |
|
418 |
}
|
| - |
|
419 |
|
| - |
|
420 |
catalogItemAailabilityMap.put(fm.getKey(), allColorNetAvailability);
|
| - |
|
421 |
|
| - |
|
422 |
}
|
| - |
|
423 |
|
| - |
|
424 |
List<FocusedShortageModel> focusedModelShortageList = new ArrayList<>();
|
| - |
|
425 |
|
| - |
|
426 |
for (Entry<Integer, Integer> fm : focusedModelMap.entrySet()) {
|
| - |
|
427 |
|
| - |
|
428 |
logger.info("fm {}", fm);
|
| - |
|
429 |
|
| - |
|
430 |
long overallStock = (inStockItemModel.get(fm.getKey()) == null ? 0 : inStockItemModel.get(fm.getKey()))
|
| - |
|
431 |
+ (itempendingIndent.get(fm.getKey()) == null ? 0 : itempendingIndent.get(fm.getKey()))
|
| - |
|
432 |
+ (grnItemPendingOrders.get(fm.getKey()) == null ? 0 : grnItemPendingOrders.get(fm.getKey()));
|
| - |
|
433 |
logger.info("overallStock {}", overallStock);
|
| - |
|
434 |
|
| - |
|
435 |
// Blocked brands available quantity is discussed to zero
|
| - |
|
436 |
long warehouseStock = catalogItemAailabilityMap.get(fm.getKey()) == null ? 0
|
| - |
|
437 |
: catalogItemAailabilityMap.get(fm.getKey());
|
| - |
|
438 |
logger.info("warehouseStock {}", warehouseStock);
|
| - |
|
439 |
|
| - |
|
440 |
long cartValue = catalogCartQtyMap.get(fm.getKey()) == null ? 0 : catalogCartQtyMap.get(fm.getKey());
|
| - |
|
441 |
|
| - |
|
442 |
logger.info("cartValue" + cartValue);
|
| - |
|
443 |
long totalStock = overallStock + cartValue;
|
| - |
|
444 |
logger.info("totalStock" + totalStock);
|
| - |
|
445 |
|
| - |
|
446 |
long shortQty = Math.min(fm.getValue() - totalStock, warehouseStock);
|
| - |
|
447 |
logger.info("shortQty {}", shortQty);
|
| - |
|
448 |
|
| - |
|
449 |
if (shortQty > 0) {
|
| - |
|
450 |
|
| - |
|
451 |
Map<Integer, Integer> requiredItemQty = new HashMap<>();
|
| - |
|
452 |
|
| - |
|
453 |
List<Item> it = itemRepository.selectAllByCatalogItemId(fm.getKey());
|
| - |
|
454 |
FocusedShortageModel fms = new FocusedShortageModel();
|
| - |
|
455 |
fms.setShortageQty((int) shortQty);
|
| - |
|
456 |
fms.setCatalogId(fm.getKey());
|
| - |
|
457 |
fms.setAvailabitiy(fm.getValue());
|
| - |
|
458 |
fms.setInStock(overallStock);
|
| - |
|
459 |
|
| - |
|
460 |
fms.setItemName(it.get(0).getBrand() + it.get(0).getModelNumber() + it.get(0).getModelName());
|
| - |
|
461 |
|
| - |
|
462 |
Map<Integer, Integer> itemQtyMap = new HashMap<>();
|
| - |
|
463 |
|
| - |
|
464 |
for (Item i : it) {
|
| - |
|
465 |
|
| - |
|
466 |
List<SaholicCIS> ca = itemAvailabilityMap.get(i.getId());
|
| - |
|
467 |
|
| - |
|
468 |
if (ca != null) {
|
| - |
|
469 |
|
| - |
|
470 |
itemQtyMap.put(i.getId(),
|
| - |
|
471 |
ca.stream().collect(Collectors.summingInt(SaholicCIS::getNetavailability)));
|
| - |
|
472 |
}
|
| - |
|
473 |
}
|
| - |
|
474 |
|
| - |
|
475 |
itemQtyMap = itemQtyMap.entrySet().stream()
|
| - |
|
476 |
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue())).collect(Collectors
|
| - |
|
477 |
.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
|
| - |
|
478 |
|
| - |
|
479 |
for (long i = shortQty; i > 0; i--) {
|
| - |
|
480 |
if (shortQty == 0) {
|
| - |
|
481 |
break;
|
| - |
|
482 |
}
|
| - |
|
483 |
|
| - |
|
484 |
for (Entry<Integer, Integer> itemQty : itemQtyMap.entrySet()) {
|
| - |
|
485 |
|
| - |
|
486 |
int availbleQty = itemQty.getValue();
|
| - |
|
487 |
if (availbleQty == 0) {
|
| - |
|
488 |
break;
|
| - |
|
489 |
}
|
| - |
|
490 |
if (requiredItemQty.get(itemQty.getKey()) == null) {
|
| - |
|
491 |
requiredItemQty.put(itemQty.getKey(), 1);
|
| - |
|
492 |
availbleQty = availbleQty - 1;
|
| - |
|
493 |
if (availbleQty != 0) {
|
| - |
|
494 |
itemQtyMap.put(itemQty.getKey(), availbleQty);
|
| - |
|
495 |
}
|
| - |
|
496 |
shortQty -= 1;
|
| - |
|
497 |
} else {
|
| - |
|
498 |
|
| - |
|
499 |
int qty = requiredItemQty.get(itemQty.getKey());
|
| - |
|
500 |
|
| - |
|
501 |
requiredItemQty.put(itemQty.getKey(), qty + 1);
|
| - |
|
502 |
|
| - |
|
503 |
availbleQty = availbleQty - 1;
|
| - |
|
504 |
if (availbleQty != 0) {
|
| - |
|
505 |
itemQtyMap.put(itemQty.getKey(), availbleQty);
|
| - |
|
506 |
}
|
| - |
|
507 |
shortQty -= 1;
|
| - |
|
508 |
}
|
| - |
|
509 |
|
| - |
|
510 |
if (shortQty == 0) {
|
| - |
|
511 |
break;
|
| - |
|
512 |
}
|
| - |
|
513 |
|
| - |
|
514 |
}
|
| - |
|
515 |
|
| - |
|
516 |
}
|
| - |
|
517 |
|
| - |
|
518 |
Set<CartItem> cartItems = new HashSet<>();
|
| - |
|
519 |
for (Entry<Integer, Integer> itemQtySet : requiredItemQty.entrySet()) {
|
| - |
|
520 |
|
| - |
|
521 |
CartItem ci = new CartItem();
|
| - |
|
522 |
Item item = itemRepository.selectById(itemQtySet.getKey());
|
| - |
|
523 |
TagListing tl = tagListingRepository.selectByItemId(item.getId());
|
| - |
|
524 |
ci.setItemId(item.getId());
|
| - |
|
525 |
ci.setQuantity(itemQtySet.getValue());
|
| - |
|
526 |
ci.setSellingPrice(tl.getSellingPrice());
|
| - |
|
527 |
cartItems.add(ci);
|
| - |
|
528 |
|
| - |
|
529 |
}
|
| - |
|
530 |
fms.setCartItems(cartItems);
|
| - |
|
531 |
|
| - |
|
532 |
// fms.setItemQtyAvailability(requiredItemQty);
|
| - |
|
533 |
|
| - |
|
534 |
focusedModelShortageList.add(fms);
|
| - |
|
535 |
|
| - |
|
536 |
}
|
| - |
|
537 |
|
| - |
|
538 |
}
|
| - |
|
539 |
|
| - |
|
540 |
logger.info("focusedModelShortageList {}", focusedModelShortageList);
|
| - |
|
541 |
return focusedModelShortageList;
|
| - |
|
542 |
}
|
| - |
|
543 |
|
| 330 |
}
|
544 |
}
|