| 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;
|
| 34586 |
vikas.jang |
57 |
List<Map<String, Object>> liquidationList = new ArrayList<>();
|
| 34443 |
vikas.jang |
58 |
if (type.equals(ProfitMandiConstants.BID_CRON_ENUM.YESTERDAY)){
|
|
|
59 |
liquidations = liquidationRepository.selectAllByEndDate(LocalDateTime.now().minusDays(1));
|
|
|
60 |
} else {
|
|
|
61 |
liquidations = liquidationRepository.selectAllByEndDate(LocalDateTime.now());
|
|
|
62 |
}
|
| 34586 |
vikas.jang |
63 |
LOGGER.info("liquidations for {}: {}",type,liquidations);
|
| 34443 |
vikas.jang |
64 |
|
|
|
65 |
if (liquidations.size() > 0) {
|
|
|
66 |
for (Liquidation liquidation : liquidations) {
|
| 34586 |
vikas.jang |
67 |
Map<String, Object> liquidationsMap = new HashMap<>();
|
| 34576 |
vikas.jang |
68 |
try {
|
|
|
69 |
List<Item> items = itemRepository.selectAllByCatalogItemId(liquidation.getCatalogId());
|
|
|
70 |
if (items.size() > 0) {
|
|
|
71 |
long remainingQty = 0;
|
|
|
72 |
for (Item item : items) {
|
|
|
73 |
List<SaholicCISTable> saholicCISTable = saholicCISTableRepository.selectAllByItemId(item.getId());
|
|
|
74 |
remainingQty += saholicCISTable.stream().mapToInt(SaholicCISTable::getAvailability).sum();
|
|
|
75 |
}
|
|
|
76 |
List<Bid> bids = bidRepository.selectAllBidByLiquidationIdAndStatus(liquidation.getId(), Arrays.asList(ProfitMandiConstants.BID_ENUM.PENDING, ProfitMandiConstants.BID_ENUM.PROCESSING));
|
| 34586 |
vikas.jang |
77 |
liquidationsMap.put("id",liquidation.getId());
|
|
|
78 |
liquidationsMap.put("catalog",items.get(0).getItemDescription());
|
|
|
79 |
liquidationsMap.put("basePrice",liquidation.getPrice());
|
|
|
80 |
liquidationsMap.put("openingStock",liquidation.getQuantity());
|
|
|
81 |
liquidationsMap.put("endDate",StringUtils.toLocalDateTime(String.valueOf(liquidation.getEndDate())));
|
|
|
82 |
List<Bid> bidsList = new ArrayList<>();
|
|
|
83 |
if (bids.size() > 0) {
|
| 34592 |
vikas.jang |
84 |
ProfitMandiConstants.BID_ENUM bidStatus = ProfitMandiConstants.BID_ENUM.PENDING;
|
| 34586 |
vikas.jang |
85 |
for (Bid bid : bids) {
|
|
|
86 |
LOGGER.info("remainingQty: {}", remainingQty);
|
|
|
87 |
if (remainingQty >= bid.getQuantity()) {
|
|
|
88 |
try {
|
|
|
89 |
List<BulkOrderModel> bulkOrderModels = new ArrayList<>();
|
|
|
90 |
BulkOrderModel bulkOrderModel = new BulkOrderModel();
|
|
|
91 |
bulkOrderModel.setRowIndex(bid.getId());
|
|
|
92 |
bulkOrderModel.setFofoId(bid.getFofoId());
|
|
|
93 |
bulkOrderModel.setItemId(items.get(0).getId());
|
|
|
94 |
bulkOrderModel.setQuantity(bid.getQuantity());
|
|
|
95 |
bulkOrderModel.setDescription(items.get(0).getItemDescription());
|
|
|
96 |
bulkOrderModel.setItemPrice(bid.getBiddingAmount());
|
| 34443 |
vikas.jang |
97 |
|
| 34586 |
vikas.jang |
98 |
bulkOrderModels.add(bulkOrderModel);
|
| 34443 |
vikas.jang |
99 |
|
| 34592 |
vikas.jang |
100 |
bidStatus = bulkOrderService.generatePurchaseOrder(bulkOrderModels, bid.getFofoId(), ProfitMandiConstants.PO_TYPE.AUTO, type);
|
| 34586 |
vikas.jang |
101 |
remainingQty -= bid.getQuantity();
|
|
|
102 |
} catch (Exception exception) {
|
|
|
103 |
LOGGER.info("generatePurchaseOrder if exception: ", exception);
|
|
|
104 |
}
|
|
|
105 |
} else {
|
|
|
106 |
try {
|
| 34592 |
vikas.jang |
107 |
bidStatus = bidService.cancelYesterdayProcessBid(bid);
|
| 34586 |
vikas.jang |
108 |
} catch (Exception exception) {
|
|
|
109 |
LOGGER.info("generatePurchaseOrder else exception: ", exception);
|
|
|
110 |
}
|
| 34576 |
vikas.jang |
111 |
}
|
| 34592 |
vikas.jang |
112 |
bid.setStatus(bidStatus);
|
| 34586 |
vikas.jang |
113 |
bidsList.add(bid);
|
| 34576 |
vikas.jang |
114 |
}
|
| 34443 |
vikas.jang |
115 |
}
|
| 34586 |
vikas.jang |
116 |
liquidationsMap.put("bids",bidsList);
|
| 34576 |
vikas.jang |
117 |
liquidation.setQuantity((int) remainingQty);
|
|
|
118 |
liquidation.setStatus(ProfitMandiConstants.LIQUIDATION_ENUM.CLOSED);
|
| 34586 |
vikas.jang |
119 |
LOGGER.info("generatePurchaseOrder qty final: {} = {}",remainingQty, liquidation.getStatus());
|
|
|
120 |
liquidationsMap.put("closingStock",liquidation.getQuantity());
|
| 34443 |
vikas.jang |
121 |
}
|
| 34576 |
vikas.jang |
122 |
} catch (Exception exception){
|
| 34586 |
vikas.jang |
123 |
LOGGER.info("generatePurchaseOrder loop: ",exception);
|
| 34443 |
vikas.jang |
124 |
}
|
| 34586 |
vikas.jang |
125 |
liquidationList.add(liquidationsMap);
|
| 34443 |
vikas.jang |
126 |
}
|
| 34586 |
vikas.jang |
127 |
LOGGER.info("liquidationList: {}",liquidationList);
|
|
|
128 |
bidService.sendLiquidationReport(liquidationList);
|
| 34443 |
vikas.jang |
129 |
}
|
|
|
130 |
}
|
| 34545 |
vikas.jang |
131 |
|
|
|
132 |
public void consolidatedBiddingMessage() throws Exception {
|
|
|
133 |
List<Liquidation> liquidations = liquidationRepository.selectAllByStatus(ProfitMandiConstants.LIQUIDATION_ENUM.PUBLISHED);
|
|
|
134 |
LOGGER.info("consolidatedBiddingMessage liquidations: {}",liquidations);
|
|
|
135 |
|
|
|
136 |
if (liquidations.size() > 0) {
|
| 34568 |
vikas.jang |
137 |
StringBuilder message = new StringBuilder("*Bidding Update!*\n" +
|
|
|
138 |
"Place your best bids and unlock unbeatable price on smartphones!\n");
|
| 34545 |
vikas.jang |
139 |
//StringBuilder templateMessage = new StringBuilder("bidd_live");
|
|
|
140 |
for (Liquidation liquidation : liquidations) {
|
|
|
141 |
List<Item> items = itemRepository.selectAllByCatalogItemId(liquidation.getCatalogId());
|
| 34568 |
vikas.jang |
142 |
List<Bid> bids = bidRepository.selectBidByLiquidationId(liquidation.getId());
|
| 34545 |
vikas.jang |
143 |
if (items.size() > 0) {
|
|
|
144 |
long remainingQty = 0;
|
|
|
145 |
for (Item item : items) {
|
|
|
146 |
List<SaholicCISTable> saholicCISTable = saholicCISTableRepository.selectAllByItemId(item.getId());
|
|
|
147 |
remainingQty += saholicCISTable.stream().mapToInt(SaholicCISTable::getAvailability).sum();
|
|
|
148 |
}
|
|
|
149 |
TagListing tagListing = tagListingRepository.selectByItemId(items.get(0).getId());
|
|
|
150 |
liquidation.setQuantity((int) remainingQty);
|
|
|
151 |
if (liquidation.getQuantity() > 0){
|
|
|
152 |
double mrp = tagListing.getMrp();
|
|
|
153 |
double discountPercentage = ((mrp - liquidation.getPrice()) / mrp) * 100;
|
|
|
154 |
message.append("\n")
|
|
|
155 |
.append("*").append(items.get(0).getItemDescription())
|
|
|
156 |
.append(" at ").append(Math.round(discountPercentage)).append("% Off*\n")
|
| 34568 |
vikas.jang |
157 |
.append("Starting price: *").append(liquidation.getPrice()).append("*\n");
|
|
|
158 |
if (bids.size() > 0) {
|
|
|
159 |
message.append("Top Bid: *").append(bids.get(0).getBiddingAmount()).append("*\n");
|
|
|
160 |
}
|
|
|
161 |
message.append("Ending on: *").append(StringUtils.toLocalDateTime(liquidation.getEndDate().toString())).append("*\n");
|
| 34545 |
vikas.jang |
162 |
//templateMessage.append("|").append(items.get(0).getItemDescription()).append("|").append(liquidation.getPrice()).append("|").append(StringUtils.toLocalDateTime(liquidation.getEndDate().toString()));
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
message.append("\nDon't miss out. Submit your bid now and grab the deal before it's gone!");
|
|
|
168 |
|
|
|
169 |
LOGGER.info("templateMessage : {}",message.toString());
|
|
|
170 |
|
|
|
171 |
bidService.biddingLiveNotification(message.toString(), "https://images.smartdukaan.com/uploads/whatsapp.jpeg");
|
|
|
172 |
//notificationService.sendWhatsappMessage(message, "Bidding Live Now!", "8529003611");
|
|
|
173 |
//notificationService.sendWhatsappMediaMessage(message.toString(), "8529003611", "https://images.smartdukaan.com/uploads/whatsapp.jpeg","whatsapp.jpeg", WhatsappMessageType.IMAGE);
|
|
|
174 |
}
|
|
|
175 |
}
|
| 34443 |
vikas.jang |
176 |
}
|