| 490 |
rajveer |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
| 5959 |
mandeep.dh |
3 |
import in.shop2020.model.v1.catalog.Item;
|
| 5945 |
mandeep.dh |
4 |
import in.shop2020.model.v1.inventory.InventoryService.Client;
|
|
|
5 |
import in.shop2020.model.v1.inventory.InventoryServiceException;
|
|
|
6 |
import in.shop2020.model.v1.inventory.ItemInventory;
|
|
|
7 |
import in.shop2020.model.v1.inventory.VendorItemMapping;
|
|
|
8 |
import in.shop2020.model.v1.inventory.Warehouse;
|
| 4585 |
rajveer |
9 |
import in.shop2020.support.models.BillingUpdate;
|
| 490 |
rajveer |
10 |
import in.shop2020.support.models.InventoryUpdate;
|
| 4987 |
mandeep.dh |
11 |
import in.shop2020.support.models.PLBDetails;
|
| 490 |
rajveer |
12 |
import in.shop2020.support.models.Update;
|
| 3125 |
rajveer |
13 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 5945 |
mandeep.dh |
14 |
import in.shop2020.thrift.clients.InventoryClient;
|
| 4585 |
rajveer |
15 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 4990 |
mandeep.dh |
16 |
import in.shop2020.utils.GmailUtils;
|
| 490 |
rajveer |
17 |
|
| 5684 |
mandeep.dh |
18 |
import java.io.BufferedWriter;
|
| 4990 |
mandeep.dh |
19 |
import java.io.File;
|
| 5684 |
mandeep.dh |
20 |
import java.io.FileWriter;
|
|
|
21 |
import java.io.IOException;
|
| 4990 |
mandeep.dh |
22 |
import java.util.ArrayList;
|
| 4987 |
mandeep.dh |
23 |
import java.util.HashMap;
|
|
|
24 |
import java.util.List;
|
|
|
25 |
import java.util.Map;
|
|
|
26 |
|
| 5212 |
mandeep.dh |
27 |
import javax.mail.MessagingException;
|
|
|
28 |
|
| 5684 |
mandeep.dh |
29 |
import org.apache.commons.lang.StringUtils;
|
| 490 |
rajveer |
30 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
31 |
import org.apache.struts2.rest.HttpHeaders;
|
| 5212 |
mandeep.dh |
32 |
import org.apache.thrift.TException;
|
| 5690 |
mandeep.dh |
33 |
import org.apache.thrift.transport.TTransportException;
|
| 3104 |
chandransh |
34 |
import org.slf4j.Logger;
|
|
|
35 |
import org.slf4j.LoggerFactory;
|
|
|
36 |
|
| 490 |
rajveer |
37 |
import com.opensymphony.xwork2.ModelDriven;
|
|
|
38 |
|
| 1367 |
chandransh |
39 |
public class UpdatesController implements ModelDriven<InventoryUpdate>{
|
| 490 |
rajveer |
40 |
|
| 4987 |
mandeep.dh |
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
*/
|
| 5945 |
mandeep.dh |
44 |
private static final int HOTSPOT_VENDOR_ID = 1;
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
*
|
|
|
48 |
*/
|
| 4987 |
mandeep.dh |
49 |
private static final String FIELD_DELIMITER = "~#~#~#";
|
|
|
50 |
|
| 3104 |
chandransh |
51 |
private static Logger logger = LoggerFactory.getLogger(UpdatesController.class);
|
|
|
52 |
|
| 490 |
rajveer |
53 |
private InventoryUpdate inventoryUpdate = new InventoryUpdate();
|
| 4987 |
mandeep.dh |
54 |
private String timestampAndItemIds;
|
| 490 |
rajveer |
55 |
|
| 5945 |
mandeep.dh |
56 |
private Map<Long, List<String>> hotspotMappings = new HashMap<Long, List<String>>();
|
| 5237 |
mandeep.dh |
57 |
|
| 490 |
rajveer |
58 |
private int errorCode = 0;
|
|
|
59 |
private String errorMessage;
|
| 5210 |
mandeep.dh |
60 |
private boolean fullPlbSync = false;
|
| 490 |
rajveer |
61 |
private String id;
|
| 5690 |
mandeep.dh |
62 |
|
|
|
63 |
private Client snapshotClient;
|
| 490 |
rajveer |
64 |
|
|
|
65 |
|
|
|
66 |
public UpdatesController(){
|
|
|
67 |
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
@Override
|
| 1367 |
chandransh |
71 |
public InventoryUpdate getModel() {
|
| 490 |
rajveer |
72 |
return this.inventoryUpdate;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
public HttpHeaders show(){
|
| 3104 |
chandransh |
76 |
logger.info("Status requested for warehouse id: " + getId());
|
| 490 |
rajveer |
77 |
Client client;
|
|
|
78 |
|
|
|
79 |
try {
|
| 5945 |
mandeep.dh |
80 |
client = new InventoryClient().getClient();
|
| 490 |
rajveer |
81 |
long warehouseId = Long.parseLong(getId());
|
|
|
82 |
Warehouse warehouse = client.getWarehouse(warehouseId);
|
| 4987 |
mandeep.dh |
83 |
timestampAndItemIds = warehouse.getVendorString();
|
| 5210 |
mandeep.dh |
84 |
|
| 5395 |
mandeep.dh |
85 |
List<String> itemKeys = new ArrayList<String>();
|
| 5210 |
mandeep.dh |
86 |
|
| 5395 |
mandeep.dh |
87 |
if (!fullPlbSync) {
|
| 5945 |
mandeep.dh |
88 |
client = new InventoryClient().getClient();
|
| 5395 |
mandeep.dh |
89 |
itemKeys = client.getItemKeysToBeProcessed(warehouseId);
|
| 5210 |
mandeep.dh |
90 |
}
|
|
|
91 |
|
| 5395 |
mandeep.dh |
92 |
for (String itemKey : itemKeys) {
|
|
|
93 |
timestampAndItemIds += FIELD_DELIMITER + itemKey;
|
| 4987 |
mandeep.dh |
94 |
}
|
| 490 |
rajveer |
95 |
} catch (Exception e) {
|
| 3104 |
chandransh |
96 |
logger.error("Error getting the warehouse or setting the timestamp", e);
|
| 490 |
rajveer |
97 |
}
|
|
|
98 |
return new DefaultHttpHeaders("lsuccess");
|
|
|
99 |
}
|
|
|
100 |
|
| 3326 |
chandransh |
101 |
/**
|
| 5285 |
mandeep.dh |
102 |
* @return
|
|
|
103 |
*/
|
|
|
104 |
private List<Item> fetchItems() {
|
|
|
105 |
try {
|
| 5945 |
mandeep.dh |
106 |
in.shop2020.model.v1.catalog.CatalogService.Client snapshotClient = new CatalogClient().getClient();
|
| 5285 |
mandeep.dh |
107 |
return snapshotClient.getAllItems(true);
|
|
|
108 |
} catch (Exception e) {
|
|
|
109 |
logger.error("Could not fetch all items. Retrying.", e);
|
| 5597 |
mandeep.dh |
110 |
try {
|
|
|
111 |
Thread.sleep(30000 * Long.parseLong(inventoryUpdate.getwarehouseId()));
|
|
|
112 |
} catch (Exception e1) {
|
|
|
113 |
logger.error("Could not sleep.", e1);
|
|
|
114 |
}
|
|
|
115 |
|
| 5395 |
mandeep.dh |
116 |
return fetchItems();
|
| 5285 |
mandeep.dh |
117 |
}
|
|
|
118 |
}
|
|
|
119 |
|
| 5306 |
rajveer |
120 |
/**
|
|
|
121 |
* @return
|
|
|
122 |
*/
|
|
|
123 |
private ItemInventory fetchInventory(long itemId) {
|
|
|
124 |
try {
|
|
|
125 |
return snapshotClient.getItemInventoryByItemId(itemId);
|
|
|
126 |
} catch (Exception e) {
|
|
|
127 |
logger.error("Could not fetch all items. Retrying.", e);
|
| 5690 |
mandeep.dh |
128 |
try {
|
| 5945 |
mandeep.dh |
129 |
snapshotClient = new InventoryClient().getClient();
|
| 5690 |
mandeep.dh |
130 |
return fetchInventory(itemId);
|
|
|
131 |
} catch (TTransportException e1) {
|
|
|
132 |
logger.error("Could not create client", e);
|
|
|
133 |
return null;
|
|
|
134 |
}
|
| 5306 |
rajveer |
135 |
}
|
|
|
136 |
}
|
| 5448 |
mandeep.dh |
137 |
|
| 5285 |
mandeep.dh |
138 |
/**
|
| 5237 |
mandeep.dh |
139 |
*
|
|
|
140 |
*/
|
|
|
141 |
private void loadHotspotMappings() {
|
|
|
142 |
try {
|
| 5284 |
mandeep.dh |
143 |
for (VendorItemMapping mapping : snapshotClient.getAllVendorItemMappings()) {
|
| 5945 |
mandeep.dh |
144 |
if (mapping.getVendorId() == HOTSPOT_VENDOR_ID) {
|
|
|
145 |
long itemId = mapping.getItemId();
|
|
|
146 |
if (!hotspotMappings.containsKey(itemId)) {
|
|
|
147 |
hotspotMappings.put(itemId, new ArrayList<String>());
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
hotspotMappings.get(itemId).add(mapping.getItemKey().trim().toLowerCase());
|
| 5237 |
mandeep.dh |
151 |
}
|
|
|
152 |
}
|
|
|
153 |
} catch (Exception e) {
|
| 5284 |
mandeep.dh |
154 |
logger.error("Could not load vendor item mappings. Trying to reload.", e);
|
| 5597 |
mandeep.dh |
155 |
|
|
|
156 |
try {
|
|
|
157 |
Thread.sleep(30000 * Long.parseLong(inventoryUpdate.getwarehouseId()));
|
|
|
158 |
} catch (Exception e1) {
|
|
|
159 |
logger.error("Could not sleep.", e1);
|
|
|
160 |
}
|
|
|
161 |
|
| 5690 |
mandeep.dh |
162 |
try {
|
| 5945 |
mandeep.dh |
163 |
snapshotClient = new InventoryClient().getClient();
|
| 5690 |
mandeep.dh |
164 |
loadHotspotMappings();
|
|
|
165 |
} catch (TTransportException e1) {
|
|
|
166 |
logger.error("Could not create client", e);
|
|
|
167 |
}
|
| 5237 |
mandeep.dh |
168 |
}
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
| 3326 |
chandransh |
172 |
* Stores the inventory updates on the history and the production server.
|
|
|
173 |
* @return
|
|
|
174 |
*/
|
| 490 |
rajveer |
175 |
public HttpHeaders create(){
|
| 3104 |
chandransh |
176 |
logger.info(inventoryUpdate.toString());
|
| 490 |
rajveer |
177 |
try {
|
| 3326 |
chandransh |
178 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
| 5945 |
mandeep.dh |
179 |
InventoryClient snapshotCatalogServiceClient = new InventoryClient();
|
| 3326 |
chandransh |
180 |
Client snapshotClient = snapshotCatalogServiceClient.getClient();
|
|
|
181 |
|
| 4585 |
rajveer |
182 |
//The snapshot client is used to update the inventory snapshot for production systems.
|
|
|
183 |
TransactionClient transactionClient = new TransactionClient();
|
|
|
184 |
in.shop2020.model.v1.order.TransactionService.Client tClient = transactionClient.getClient();
|
|
|
185 |
|
| 490 |
rajveer |
186 |
Long warehouseId = Long.parseLong(inventoryUpdate.getwarehouseId());
|
|
|
187 |
String timestamp = inventoryUpdate.getCurrentTimestamp();
|
|
|
188 |
Map<String, Long> availability = new HashMap<String, Long>();
|
|
|
189 |
List<Update> updates = inventoryUpdate.getUpdates();
|
| 5230 |
mandeep.dh |
190 |
if (updates != null) {
|
|
|
191 |
for(Update update: updates){
|
|
|
192 |
String key = update.getKey();
|
|
|
193 |
long quantity = (long) Double.parseDouble(update.getQuantity());
|
|
|
194 |
if(availability.containsKey(key)){
|
|
|
195 |
quantity = quantity + availability.get(key);
|
|
|
196 |
}
|
|
|
197 |
availability.put(key, quantity);
|
|
|
198 |
}
|
| 490 |
rajveer |
199 |
}
|
| 4585 |
rajveer |
200 |
|
| 3326 |
chandransh |
201 |
//Update the snapshot before the history since that is more important.
|
|
|
202 |
snapshotClient.updateInventory(warehouseId, timestamp, availability);
|
| 5959 |
mandeep.dh |
203 |
snapshotClient.updateInventoryHistory(warehouseId, timestamp, availability);
|
| 4585 |
rajveer |
204 |
|
|
|
205 |
List<BillingUpdate> billingUpdates = inventoryUpdate.getBillingUpdates();
|
|
|
206 |
if(billingUpdates!=null){
|
|
|
207 |
for(BillingUpdate billingUpdate: billingUpdates){
|
| 6747 |
amar.kumar |
208 |
tClient.addInvoiceNumber(Long.parseLong(billingUpdate.getOrderId()), billingUpdate.getInvoiceNumber(), billingUpdate.getColor(), billingUpdate.getSerialNumber(), billingUpdate.getItemNumber());
|
| 4585 |
rajveer |
209 |
}
|
|
|
210 |
}
|
| 4987 |
mandeep.dh |
211 |
|
|
|
212 |
List<PLBDetails> plbDetails = inventoryUpdate.getPlbDetails();
|
|
|
213 |
if (plbDetails != null) {
|
| 5212 |
mandeep.dh |
214 |
Map<String, PLBDetails> currentInventory = new HashMap<String, PLBDetails>();
|
| 4987 |
mandeep.dh |
215 |
for (PLBDetails plbDetail : plbDetails) {
|
|
|
216 |
try {
|
| 5212 |
mandeep.dh |
217 |
if (fullPlbSync) {
|
| 5451 |
mandeep.dh |
218 |
currentInventory.put(plbDetail.getItemKey().toLowerCase(), plbDetail);
|
| 5212 |
mandeep.dh |
219 |
}
|
|
|
220 |
else {
|
|
|
221 |
snapshotClient.resetAvailability(
|
|
|
222 |
plbDetail.getItemKey(), 1,
|
|
|
223 |
plbDetail.getQuantity().longValue(), warehouseId);
|
|
|
224 |
snapshotClient.markMissedInventoryUpdatesAsProcessed(
|
| 5702 |
mandeep.dh |
225 |
plbDetail.getItemKey(), warehouseId);
|
| 5212 |
mandeep.dh |
226 |
}
|
| 4987 |
mandeep.dh |
227 |
} catch (Exception e) {
|
|
|
228 |
logger.error("Could not reset availability of: " + plbDetail.getItemKey(), e);
|
| 4990 |
mandeep.dh |
229 |
GmailUtils g = new GmailUtils();
|
| 10253 |
manish.sha |
230 |
g.sendSSLMessage(new String[]{ "amit.gupta@shop2020.in","manish.sharma@shop2020.in" }, "Error resetting availability for " + plbDetail.getItemKey() + " in warehouseId: " + warehouseId + " to " + plbDetail.getQuantity(), "", "cnc.center@shop2020.in", "5hop2o2o", new ArrayList<File>());
|
| 4987 |
mandeep.dh |
231 |
}
|
|
|
232 |
}
|
| 5212 |
mandeep.dh |
233 |
|
| 5218 |
mandeep.dh |
234 |
sendMailForPLBMismatches(warehouseId, currentInventory);
|
| 4987 |
mandeep.dh |
235 |
}
|
| 490 |
rajveer |
236 |
} catch (Exception e) {
|
| 3104 |
chandransh |
237 |
logger.error("Unable to update inventory", e);
|
| 490 |
rajveer |
238 |
}
|
| 5220 |
mandeep.dh |
239 |
|
| 490 |
rajveer |
240 |
return new DefaultHttpHeaders("psuccess");
|
|
|
241 |
}
|
|
|
242 |
|
| 5218 |
mandeep.dh |
243 |
private void sendMailForPLBMismatches(Long warehouseId, Map<String, PLBDetails> currentInventory)
|
| 5212 |
mandeep.dh |
244 |
throws InventoryServiceException, TException, MessagingException {
|
|
|
245 |
if (fullPlbSync) {
|
| 5945 |
mandeep.dh |
246 |
snapshotClient = new InventoryClient().getClient();
|
| 5237 |
mandeep.dh |
247 |
loadHotspotMappings();
|
| 5212 |
mandeep.dh |
248 |
Map<Item, PLBDetails> mismatches = new HashMap<Item, PLBDetails>();
|
| 5290 |
mandeep.dh |
249 |
List<Item> items = fetchItems();
|
| 5212 |
mandeep.dh |
250 |
for (Item item : items) {
|
| 5237 |
mandeep.dh |
251 |
if (hotspotMappings.containsKey(item.getId())) {
|
| 5945 |
mandeep.dh |
252 |
PLBDetails plbDetails = null;
|
|
|
253 |
for (String s : hotspotMappings.get(item.getId())) {
|
|
|
254 |
if (currentInventory.containsKey(s.trim().toLowerCase())) {
|
|
|
255 |
plbDetails = currentInventory.get(s.trim().toLowerCase());
|
|
|
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
| 5423 |
rajveer |
259 |
Long currentSnapshotAvailability = fetchInventory(item.getId()).getAvailability().get(warehouseId);
|
|
|
260 |
if ((currentSnapshotAvailability == null || currentSnapshotAvailability.equals(0l)) &&
|
| 5241 |
mandeep.dh |
261 |
(plbDetails == null || plbDetails.getQuantity().longValue() == 0))
|
| 5237 |
mandeep.dh |
262 |
{
|
|
|
263 |
continue;
|
|
|
264 |
}
|
| 5223 |
mandeep.dh |
265 |
|
| 5423 |
rajveer |
266 |
if (currentSnapshotAvailability == null || plbDetails == null ||
|
|
|
267 |
!currentSnapshotAvailability.equals(plbDetails.getQuantity().longValue()))
|
| 5237 |
mandeep.dh |
268 |
{
|
|
|
269 |
mismatches.put(item, plbDetails);
|
| 5212 |
mandeep.dh |
270 |
}
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
String subject = mismatches.size() + " mismatches with PLB sync for warehouse id: " + warehouseId;
|
| 5684 |
mandeep.dh |
275 |
File file = new File("mismatches.xls");
|
|
|
276 |
|
|
|
277 |
try {
|
|
|
278 |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
|
|
|
279 |
bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
|
|
|
280 |
"Brand", "Model Name", "Model Number",
|
|
|
281 |
"Color", "SpiceOnlineRetailStock", "HotspotStock" }, '\t'));
|
|
|
282 |
for (Item item : mismatches.keySet()) {
|
|
|
283 |
Long currentSnapshotAvailability = fetchInventory(item.getId()).getAvailability().get(warehouseId);
|
|
|
284 |
String plbQuantity = mismatches.get(item) == null ? "0" : mismatches.get(item).getQuantity().toString();
|
|
|
285 |
String currentSnapshotQuantity = currentSnapshotAvailability == null ? "0" : currentSnapshotAvailability.toString();
|
|
|
286 |
bufferedWriter.newLine();
|
|
|
287 |
bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(item.getId()), item.getBrand(), item.getModelName(),
|
|
|
288 |
item.getModelNumber(), item.getColor(),currentSnapshotQuantity, plbQuantity }, "\t"));
|
|
|
289 |
}
|
|
|
290 |
|
|
|
291 |
bufferedWriter.close();
|
|
|
292 |
} catch (IOException e) {
|
|
|
293 |
logger.error("Could not write mismatches to file", e);
|
| 5212 |
mandeep.dh |
294 |
}
|
| 5220 |
mandeep.dh |
295 |
|
| 5212 |
mandeep.dh |
296 |
GmailUtils g = new GmailUtils();
|
| 10253 |
manish.sha |
297 |
g.sendSSLMessage(new String[]{ "sandeep.sachdeva@shop2020.in", "khushal.bhatia@shop2020.in", "rajneesh.arora@shop2020.in", "amit.gupta@shop2020.in", "manish.sharma@shop2020.in", "rajveer.singh@shop2020.in" }, subject,
|
| 5684 |
mandeep.dh |
298 |
"", "cnc.center@shop2020.in", "5h0p2o2o", file.getAbsolutePath());
|
| 5212 |
mandeep.dh |
299 |
}
|
|
|
300 |
}
|
|
|
301 |
|
| 490 |
rajveer |
302 |
public int getErrorCode() {
|
|
|
303 |
return errorCode;
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
public String getErrorMessage() {
|
|
|
307 |
return errorMessage;
|
|
|
308 |
}
|
|
|
309 |
|
|
|
310 |
public String getId(){
|
|
|
311 |
return id;
|
|
|
312 |
}
|
|
|
313 |
|
|
|
314 |
public void setId(String id){
|
|
|
315 |
this.id = id;
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
public InventoryUpdate getInventoryUpdate() {
|
|
|
319 |
return inventoryUpdate;
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
public void setInventoryUpdate(InventoryUpdate inventoryUpdate) {
|
|
|
323 |
this.inventoryUpdate = inventoryUpdate;
|
|
|
324 |
}
|
|
|
325 |
|
| 4987 |
mandeep.dh |
326 |
public void setTimestampAndItemIds(String timestampAndItemIds) {
|
|
|
327 |
this.timestampAndItemIds = timestampAndItemIds;
|
| 490 |
rajveer |
328 |
}
|
|
|
329 |
|
| 4987 |
mandeep.dh |
330 |
public String getTimestampAndItemIds() {
|
|
|
331 |
return timestampAndItemIds;
|
| 5210 |
mandeep.dh |
332 |
}
|
|
|
333 |
|
|
|
334 |
public boolean isFullPlbSync() {
|
|
|
335 |
return fullPlbSync;
|
|
|
336 |
}
|
|
|
337 |
|
|
|
338 |
public void setFullPlbSync(boolean fullPlbSync) {
|
|
|
339 |
this.fullPlbSync = fullPlbSync;
|
| 5684 |
mandeep.dh |
340 |
}
|
| 490 |
rajveer |
341 |
}
|