| 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;
|
| 34545 |
vikas.jang |
5 |
import com.spice.profitmandi.common.util.StringUtils;
|
| 34443 |
vikas.jang |
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;
|
| 34545 |
vikas.jang |
9 |
import com.spice.profitmandi.dao.entity.catalog.TagListing;
|
| 34443 |
vikas.jang |
10 |
import com.spice.profitmandi.dao.entity.inventory.SaholicCISTable;
|
|
|
11 |
import com.spice.profitmandi.dao.repository.catalog.BidRepository;
|
|
|
12 |
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
|
|
|
13 |
import com.spice.profitmandi.dao.repository.catalog.LiquidationRepository;
|
| 34545 |
vikas.jang |
14 |
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
|
| 34443 |
vikas.jang |
15 |
import com.spice.profitmandi.dao.repository.inventory.SaholicCISTableRepository;
|
| 34545 |
vikas.jang |
16 |
import com.spice.profitmandi.service.NotificationService;
|
| 34443 |
vikas.jang |
17 |
import com.spice.profitmandi.service.order.BulkOrderService;
|
| 34545 |
vikas.jang |
18 |
import com.spice.profitmandi.service.whatsapp.WhatsappMessageType;
|
| 34443 |
vikas.jang |
19 |
import org.apache.logging.log4j.LogManager;
|
|
|
20 |
import org.apache.logging.log4j.Logger;
|
|
|
21 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
22 |
import org.springframework.stereotype.Component;
|
|
|
23 |
|
|
|
24 |
import java.time.LocalDateTime;
|
| 34545 |
vikas.jang |
25 |
import java.util.*;
|
| 34443 |
vikas.jang |
26 |
|
|
|
27 |
@Component
|
|
|
28 |
public class LiquidationService {
|
|
|
29 |
private static final Logger LOGGER = LogManager.getLogger(LiquidationService.class);
|
|
|
30 |
|
|
|
31 |
@Autowired
|
|
|
32 |
BidRepository bidRepository;
|
|
|
33 |
|
|
|
34 |
@Autowired
|
|
|
35 |
LiquidationRepository liquidationRepository;
|
|
|
36 |
|
|
|
37 |
@Autowired
|
|
|
38 |
private ItemRepository itemRepository;
|
|
|
39 |
|
|
|
40 |
@Autowired
|
| 34468 |
vikas.jang |
41 |
private BidService bidService;
|
| 34443 |
vikas.jang |
42 |
|
|
|
43 |
@Autowired
|
|
|
44 |
SaholicCISTableRepository saholicCISTableRepository;
|
|
|
45 |
|
|
|
46 |
@Autowired
|
|
|
47 |
private BulkOrderService bulkOrderService;
|
|
|
48 |
|
| 34545 |
vikas.jang |
49 |
@Autowired
|
|
|
50 |
private TagListingRepository tagListingRepository;
|
|
|
51 |
|
|
|
52 |
@Autowired
|
|
|
53 |
private NotificationService notificationService;
|
|
|
54 |
|
| 34443 |
vikas.jang |
55 |
public void processBids(ProfitMandiConstants.BID_CRON_ENUM type) throws Exception {
|
|
|
56 |
List<Liquidation> liquidations;
|
| 34637 |
vikas.jang |
57 |
boolean canRefund = true;
|
| 34586 |
vikas.jang |
58 |
List<Map<String, Object>> liquidationList = new ArrayList<>();
|
| 34443 |
vikas.jang |
59 |
if (type.equals(ProfitMandiConstants.BID_CRON_ENUM.YESTERDAY)){
|
|
|
60 |
liquidations = liquidationRepository.selectAllByEndDate(LocalDateTime.now().minusDays(1));
|
| 34637 |
vikas.jang |
61 |
canRefund = false;
|
| 34443 |
vikas.jang |
62 |
} else {
|
|
|
63 |
liquidations = liquidationRepository.selectAllByEndDate(LocalDateTime.now());
|
|
|
64 |
}
|
| 34586 |
vikas.jang |
65 |
LOGGER.info("liquidations for {}: {}",type,liquidations);
|
| 34443 |
vikas.jang |
66 |
|
|
|
67 |
if (liquidations.size() > 0) {
|
|
|
68 |
for (Liquidation liquidation : liquidations) {
|
| 34586 |
vikas.jang |
69 |
Map<String, Object> liquidationsMap = new HashMap<>();
|
| 34576 |
vikas.jang |
70 |
try {
|
|
|
71 |
List<Item> items = itemRepository.selectAllByCatalogItemId(liquidation.getCatalogId());
|
|
|
72 |
if (items.size() > 0) {
|
|
|
73 |
long remainingQty = 0;
|
|
|
74 |
for (Item item : items) {
|
|
|
75 |
List<SaholicCISTable> saholicCISTable = saholicCISTableRepository.selectAllByItemId(item.getId());
|
|
|
76 |
remainingQty += saholicCISTable.stream().mapToInt(SaholicCISTable::getAvailability).sum();
|
|
|
77 |
}
|
|
|
78 |
List<Bid> bids = bidRepository.selectAllBidByLiquidationIdAndStatus(liquidation.getId(), Arrays.asList(ProfitMandiConstants.BID_ENUM.PENDING, ProfitMandiConstants.BID_ENUM.PROCESSING));
|
| 34586 |
vikas.jang |
79 |
liquidationsMap.put("id",liquidation.getId());
|
|
|
80 |
liquidationsMap.put("catalog",items.get(0).getItemDescription());
|
|
|
81 |
liquidationsMap.put("basePrice",liquidation.getPrice());
|
|
|
82 |
liquidationsMap.put("openingStock",liquidation.getQuantity());
|
|
|
83 |
liquidationsMap.put("endDate",StringUtils.toLocalDateTime(String.valueOf(liquidation.getEndDate())));
|
|
|
84 |
List<Bid> bidsList = new ArrayList<>();
|
|
|
85 |
if (bids.size() > 0) {
|
| 34592 |
vikas.jang |
86 |
ProfitMandiConstants.BID_ENUM bidStatus = ProfitMandiConstants.BID_ENUM.PENDING;
|
| 34586 |
vikas.jang |
87 |
for (Bid bid : bids) {
|
|
|
88 |
LOGGER.info("remainingQty: {}", remainingQty);
|
|
|
89 |
if (remainingQty >= bid.getQuantity()) {
|
|
|
90 |
try {
|
|
|
91 |
List<BulkOrderModel> bulkOrderModels = new ArrayList<>();
|
|
|
92 |
BulkOrderModel bulkOrderModel = new BulkOrderModel();
|
|
|
93 |
bulkOrderModel.setRowIndex(bid.getId());
|
|
|
94 |
bulkOrderModel.setFofoId(bid.getFofoId());
|
|
|
95 |
bulkOrderModel.setItemId(items.get(0).getId());
|
|
|
96 |
bulkOrderModel.setQuantity(bid.getQuantity());
|
|
|
97 |
bulkOrderModel.setDescription(items.get(0).getItemDescription());
|
|
|
98 |
bulkOrderModel.setItemPrice(bid.getBiddingAmount());
|
| 34443 |
vikas.jang |
99 |
|
| 34586 |
vikas.jang |
100 |
bulkOrderModels.add(bulkOrderModel);
|
| 34443 |
vikas.jang |
101 |
|
| 34592 |
vikas.jang |
102 |
bidStatus = bulkOrderService.generatePurchaseOrder(bulkOrderModels, bid.getFofoId(), ProfitMandiConstants.PO_TYPE.AUTO, type);
|
| 34586 |
vikas.jang |
103 |
remainingQty -= bid.getQuantity();
|
|
|
104 |
} catch (Exception exception) {
|
|
|
105 |
LOGGER.info("generatePurchaseOrder if exception: ", exception);
|
|
|
106 |
}
|
|
|
107 |
} else {
|
|
|
108 |
try {
|
| 34592 |
vikas.jang |
109 |
bidStatus = bidService.cancelYesterdayProcessBid(bid);
|
| 34637 |
vikas.jang |
110 |
if (canRefund || remainingQty < 1) {
|
|
|
111 |
bidService.refundBidAmountToWallet(bid, bidStatus);
|
|
|
112 |
}
|
| 34586 |
vikas.jang |
113 |
} catch (Exception exception) {
|
|
|
114 |
LOGGER.info("generatePurchaseOrder else exception: ", exception);
|
|
|
115 |
}
|
| 34576 |
vikas.jang |
116 |
}
|
| 34592 |
vikas.jang |
117 |
bid.setStatus(bidStatus);
|
| 34586 |
vikas.jang |
118 |
bidsList.add(bid);
|
| 34576 |
vikas.jang |
119 |
}
|
| 34443 |
vikas.jang |
120 |
}
|
| 34586 |
vikas.jang |
121 |
liquidationsMap.put("bids",bidsList);
|
| 34576 |
vikas.jang |
122 |
liquidation.setQuantity((int) remainingQty);
|
|
|
123 |
liquidation.setStatus(ProfitMandiConstants.LIQUIDATION_ENUM.CLOSED);
|
| 34586 |
vikas.jang |
124 |
LOGGER.info("generatePurchaseOrder qty final: {} = {}",remainingQty, liquidation.getStatus());
|
|
|
125 |
liquidationsMap.put("closingStock",liquidation.getQuantity());
|
| 34443 |
vikas.jang |
126 |
}
|
| 34576 |
vikas.jang |
127 |
} catch (Exception exception){
|
| 34586 |
vikas.jang |
128 |
LOGGER.info("generatePurchaseOrder loop: ",exception);
|
| 34443 |
vikas.jang |
129 |
}
|
| 34586 |
vikas.jang |
130 |
liquidationList.add(liquidationsMap);
|
| 34443 |
vikas.jang |
131 |
}
|
| 34586 |
vikas.jang |
132 |
LOGGER.info("liquidationList: {}",liquidationList);
|
|
|
133 |
bidService.sendLiquidationReport(liquidationList);
|
| 34443 |
vikas.jang |
134 |
}
|
|
|
135 |
}
|
| 34545 |
vikas.jang |
136 |
|
|
|
137 |
public void consolidatedBiddingMessage() throws Exception {
|
|
|
138 |
List<Liquidation> liquidations = liquidationRepository.selectAllByStatus(ProfitMandiConstants.LIQUIDATION_ENUM.PUBLISHED);
|
|
|
139 |
LOGGER.info("consolidatedBiddingMessage liquidations: {}",liquidations);
|
|
|
140 |
|
|
|
141 |
if (liquidations.size() > 0) {
|
| 34568 |
vikas.jang |
142 |
StringBuilder message = new StringBuilder("*Bidding Update!*\n" +
|
|
|
143 |
"Place your best bids and unlock unbeatable price on smartphones!\n");
|
| 34545 |
vikas.jang |
144 |
//StringBuilder templateMessage = new StringBuilder("bidd_live");
|
|
|
145 |
for (Liquidation liquidation : liquidations) {
|
|
|
146 |
List<Item> items = itemRepository.selectAllByCatalogItemId(liquidation.getCatalogId());
|
| 34568 |
vikas.jang |
147 |
List<Bid> bids = bidRepository.selectBidByLiquidationId(liquidation.getId());
|
| 34545 |
vikas.jang |
148 |
if (items.size() > 0) {
|
|
|
149 |
long remainingQty = 0;
|
|
|
150 |
for (Item item : items) {
|
|
|
151 |
List<SaholicCISTable> saholicCISTable = saholicCISTableRepository.selectAllByItemId(item.getId());
|
|
|
152 |
remainingQty += saholicCISTable.stream().mapToInt(SaholicCISTable::getAvailability).sum();
|
|
|
153 |
}
|
|
|
154 |
TagListing tagListing = tagListingRepository.selectByItemId(items.get(0).getId());
|
|
|
155 |
liquidation.setQuantity((int) remainingQty);
|
|
|
156 |
if (liquidation.getQuantity() > 0){
|
|
|
157 |
double mrp = tagListing.getMrp();
|
|
|
158 |
double discountPercentage = ((mrp - liquidation.getPrice()) / mrp) * 100;
|
|
|
159 |
message.append("\n")
|
|
|
160 |
.append("*").append(items.get(0).getItemDescription())
|
|
|
161 |
.append(" at ").append(Math.round(discountPercentage)).append("% Off*\n")
|
| 34568 |
vikas.jang |
162 |
.append("Starting price: *").append(liquidation.getPrice()).append("*\n");
|
|
|
163 |
if (bids.size() > 0) {
|
|
|
164 |
message.append("Top Bid: *").append(bids.get(0).getBiddingAmount()).append("*\n");
|
|
|
165 |
}
|
|
|
166 |
message.append("Ending on: *").append(StringUtils.toLocalDateTime(liquidation.getEndDate().toString())).append("*\n");
|
| 34545 |
vikas.jang |
167 |
//templateMessage.append("|").append(items.get(0).getItemDescription()).append("|").append(liquidation.getPrice()).append("|").append(StringUtils.toLocalDateTime(liquidation.getEndDate().toString()));
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
message.append("\nDon't miss out. Submit your bid now and grab the deal before it's gone!");
|
|
|
173 |
|
|
|
174 |
LOGGER.info("templateMessage : {}",message.toString());
|
|
|
175 |
|
|
|
176 |
bidService.biddingLiveNotification(message.toString(), "https://images.smartdukaan.com/uploads/whatsapp.jpeg");
|
|
|
177 |
//notificationService.sendWhatsappMessage(message, "Bidding Live Now!", "8529003611");
|
|
|
178 |
//notificationService.sendWhatsappMediaMessage(message.toString(), "8529003611", "https://images.smartdukaan.com/uploads/whatsapp.jpeg","whatsapp.jpeg", WhatsappMessageType.IMAGE);
|
|
|
179 |
}
|
|
|
180 |
}
|
| 34443 |
vikas.jang |
181 |
}
|