| 34443 |
vikas.jang |
1 |
package com.spice.profitmandi.dao.service;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.model.BulkOrderModel;
|
|
|
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
|
|
5 |
import com.spice.profitmandi.dao.cart.CartService;
|
|
|
6 |
import com.spice.profitmandi.dao.entity.catalog.Bid;
|
|
|
7 |
import com.spice.profitmandi.dao.entity.catalog.Item;
|
|
|
8 |
import com.spice.profitmandi.dao.entity.catalog.Liquidation;
|
|
|
9 |
import com.spice.profitmandi.dao.entity.inventory.SaholicCISTable;
|
|
|
10 |
import com.spice.profitmandi.dao.model.CartItem;
|
|
|
11 |
import com.spice.profitmandi.dao.model.UserCart;
|
|
|
12 |
import com.spice.profitmandi.dao.repository.catalog.BidRepository;
|
|
|
13 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
14 |
import com.spice.profitmandi.dao.repository.catalog.LiquidationRepository;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
|
|
16 |
import com.spice.profitmandi.dao.repository.inventory.SaholicCISTableRepository;
|
|
|
17 |
import com.spice.profitmandi.service.order.BulkOrderService;
|
|
|
18 |
import com.spice.profitmandi.service.transaction.TransactionService;
|
|
|
19 |
import com.spice.profitmandi.service.wallet.CommonPaymentService;
|
|
|
20 |
import com.spice.profitmandi.service.wallet.WalletService;
|
|
|
21 |
import org.apache.logging.log4j.LogManager;
|
|
|
22 |
import org.apache.logging.log4j.Logger;
|
|
|
23 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
24 |
import org.springframework.stereotype.Component;
|
|
|
25 |
|
|
|
26 |
import java.time.LocalDateTime;
|
|
|
27 |
import java.util.ArrayList;
|
|
|
28 |
import java.util.Arrays;
|
|
|
29 |
import java.util.List;
|
|
|
30 |
|
|
|
31 |
@Component
|
|
|
32 |
public class LiquidationService {
|
|
|
33 |
private static final Logger LOGGER = LogManager.getLogger(LiquidationService.class);
|
|
|
34 |
|
|
|
35 |
@Autowired
|
|
|
36 |
BidRepository bidRepository;
|
|
|
37 |
|
|
|
38 |
@Autowired
|
|
|
39 |
LiquidationRepository liquidationRepository;
|
|
|
40 |
|
|
|
41 |
@Autowired
|
|
|
42 |
private ItemRepository itemRepository;
|
|
|
43 |
|
|
|
44 |
@Autowired
|
|
|
45 |
private UserAccountRepository userAccountRepository;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
private TransactionService transactionService;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
private CommonPaymentService commonPaymentService;
|
|
|
52 |
|
|
|
53 |
@Autowired
|
|
|
54 |
private CartService cartService;
|
|
|
55 |
|
|
|
56 |
@Autowired
|
|
|
57 |
SaholicCISTableRepository saholicCISTableRepository;
|
|
|
58 |
|
|
|
59 |
@Autowired
|
|
|
60 |
WalletService walletService;
|
|
|
61 |
|
|
|
62 |
@Autowired
|
|
|
63 |
private BulkOrderService bulkOrderService;
|
|
|
64 |
|
|
|
65 |
public void processBids(ProfitMandiConstants.BID_CRON_ENUM type) throws Exception {
|
|
|
66 |
List<Liquidation> liquidations;
|
|
|
67 |
if (type.equals(ProfitMandiConstants.BID_CRON_ENUM.YESTERDAY)){
|
|
|
68 |
liquidations = liquidationRepository.selectAllByEndDate(LocalDateTime.now().minusDays(1));
|
|
|
69 |
} else {
|
|
|
70 |
liquidations = liquidationRepository.selectAllByEndDate(LocalDateTime.now());
|
|
|
71 |
}
|
|
|
72 |
LOGGER.info("liquidations: {}",liquidations);
|
|
|
73 |
|
|
|
74 |
if (liquidations.size() > 0) {
|
|
|
75 |
for (Liquidation liquidation : liquidations) {
|
|
|
76 |
List<Item> items = itemRepository.selectAllByCatalogItemId(liquidation.getCatalogId());
|
|
|
77 |
if (items.size() > 0) {
|
|
|
78 |
long remainingQty = 0;
|
|
|
79 |
for (Item item : items) {
|
|
|
80 |
List<SaholicCISTable> saholicCISTable = saholicCISTableRepository.selectAllByItemId(item.getId());
|
|
|
81 |
remainingQty += saholicCISTable.stream().filter(x -> x.getNetAvailability() > 0).count();
|
|
|
82 |
}
|
|
|
83 |
List<Bid> bids = bidRepository.selectAllBidByLiquidationIdAndStatus(liquidation.getId(), Arrays.asList(ProfitMandiConstants.BID_ENUM.PENDING, ProfitMandiConstants.BID_ENUM.PROCESSING));
|
|
|
84 |
|
|
|
85 |
for (Bid bid : bids) {
|
|
|
86 |
if (remainingQty >= bid.getQuantity()) {
|
|
|
87 |
List<BulkOrderModel> bulkOrderModels = new ArrayList<>();
|
|
|
88 |
BulkOrderModel bulkOrderModel = new BulkOrderModel();
|
|
|
89 |
bulkOrderModel.setFofoId(bid.getFofoId());
|
|
|
90 |
bulkOrderModel.setItemId(items.get(0).getId());
|
|
|
91 |
bulkOrderModel.setQuantity(bid.getQuantity());
|
|
|
92 |
bulkOrderModel.setDescription(items.get(0).getItemDescription());
|
|
|
93 |
bulkOrderModel.setItemPrice(bid.getBiddingAmount());
|
|
|
94 |
|
|
|
95 |
bulkOrderModels.add(bulkOrderModel);
|
|
|
96 |
|
|
|
97 |
bulkOrderService.generatePurchaseOrder(bulkOrderModels, bid.getFofoId(), ProfitMandiConstants.PO_TYPE.AUTO);
|
|
|
98 |
remainingQty -= bid.getQuantity();
|
|
|
99 |
//this.generatePurchaseOrder(liquidation, bid, item, remainingQty, type);
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
liquidation.setQuantity((int) remainingQty);
|
|
|
103 |
liquidation.setStatus(ProfitMandiConstants.LIQUIDATION_ENUM.CLOSED);
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
public void generatePurchaseOrder(Liquidation liquidation, Bid bid, Item item, long remainingQty, ProfitMandiConstants.BID_CRON_ENUM type) throws Exception {
|
|
|
110 |
UserCart uc = userAccountRepository.getUserCart(bid.getFofoId());
|
|
|
111 |
double walletAmount = walletService.getWalletAmount(uc.getUserId());
|
|
|
112 |
double orderAmount = (bid.getBiddingAmount() * bid.getQuantity());
|
|
|
113 |
|
|
|
114 |
if (walletAmount >= orderAmount) {
|
|
|
115 |
List<CartItem> cartItems = new ArrayList<>();
|
|
|
116 |
CartItem cartItem = new CartItem();
|
|
|
117 |
cartItem.setItemId(item.getId());
|
|
|
118 |
cartItem.setQuantity(bid.getQuantity());
|
|
|
119 |
cartItem.setSellingPrice(bid.getBiddingAmount());
|
|
|
120 |
cartItems.add(cartItem);
|
|
|
121 |
cartService.addItemsToCart(uc.getCartId(), cartItems);
|
|
|
122 |
liquidation.setQuantity((int) remainingQty - bid.getQuantity());
|
|
|
123 |
bid.setStatus(ProfitMandiConstants.BID_ENUM.CLOSED);
|
|
|
124 |
int transactionId = transactionService.createTransaction(uc, orderAmount, 0);
|
|
|
125 |
commonPaymentService.payThroughWallet(transactionId);
|
|
|
126 |
} else {
|
|
|
127 |
if (remainingQty <= bid.getQuantity() || (type.equals(ProfitMandiConstants.BID_CRON_ENUM.YESTERDAY))){
|
|
|
128 |
bid.setStatus(ProfitMandiConstants.BID_ENUM.CANCELLED);
|
|
|
129 |
} else {
|
|
|
130 |
bid.setStatus(ProfitMandiConstants.BID_ENUM.PROCESSING);
|
|
|
131 |
liquidation.setQuantity((int) remainingQty - bid.getQuantity());
|
|
|
132 |
bulkOrderService.notifyPartnerRBM(walletAmount, orderAmount, bid.getFofoId());
|
|
|
133 |
}
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
|
|
|
138 |
}
|