| 6484 |
amar.kumar |
1 |
package in.shop2020.warehouse.util;
|
|
|
2 |
|
|
|
3 |
import java.io.BufferedWriter;
|
|
|
4 |
import java.io.File;
|
|
|
5 |
import java.io.FileWriter;
|
|
|
6 |
import java.io.IOException;
|
|
|
7 |
import java.math.BigDecimal;
|
|
|
8 |
import java.util.ArrayList;
|
|
|
9 |
import java.util.HashMap;
|
|
|
10 |
import java.util.List;
|
|
|
11 |
import java.util.Map;
|
|
|
12 |
|
|
|
13 |
import javax.mail.MessagingException;
|
|
|
14 |
|
|
|
15 |
import org.apache.commons.lang.StringUtils;
|
|
|
16 |
import org.apache.thrift.TException;
|
|
|
17 |
import org.apache.thrift.transport.TTransportException;
|
|
|
18 |
import org.springframework.context.ApplicationContext;
|
|
|
19 |
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
|
20 |
|
|
|
21 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
|
|
22 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
23 |
import in.shop2020.model.v1.inventory.InventoryService.Client;
|
|
|
24 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
25 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
26 |
import in.shop2020.utils.GmailUtils;
|
|
|
27 |
import in.shop2020.warehouse.handler.InventoryItemHandler;
|
|
|
28 |
|
|
|
29 |
public class InventoryMismatchGenerator {
|
|
|
30 |
|
|
|
31 |
private InventoryItemHandler inventoryItemhandler;
|
|
|
32 |
/**
|
|
|
33 |
* @param args
|
|
|
34 |
*/
|
|
|
35 |
|
|
|
36 |
public InventoryMismatchGenerator() {
|
|
|
37 |
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
|
|
|
38 |
inventoryItemhandler = context.getBean(InventoryItemHandler.class);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public static void main(String[] args) {
|
|
|
42 |
InventoryMismatchGenerator mismatchGenerator = new InventoryMismatchGenerator();
|
|
|
43 |
try {
|
|
|
44 |
mismatchGenerator.sendMailForMismatches();
|
|
|
45 |
} catch (MessagingException e) {
|
|
|
46 |
// TODO Auto-generated catch block
|
|
|
47 |
e.printStackTrace();
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public void sendMailForMismatches() throws MessagingException {
|
|
|
52 |
Map<Long, Long[]> availabilityMismatchMap = new HashMap<Long, Long[]>();
|
|
|
53 |
|
|
|
54 |
List<Map<String, Integer>> serializedInventorybyScansinList = inventoryItemhandler.getCurrentSerializedInventory();
|
|
|
55 |
List<Map<String, Integer>> unSerializedInventorybyScansinList = inventoryItemhandler.getCurrentNonSerializedInventory();
|
|
|
56 |
|
|
|
57 |
Map<Long, Long> itemAvailabilityByScans = new HashMap<Long, Long>();
|
|
|
58 |
|
|
|
59 |
for (Map<String,Integer> inventoryForItem : serializedInventorybyScansinList) {
|
|
|
60 |
Object sumObj = inventoryForItem.get("sum");
|
|
|
61 |
String sumString = sumObj.toString();
|
|
|
62 |
Long availability = Long.parseLong(sumString);
|
|
|
63 |
itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
for (Map<String,Integer> inventoryForItem : unSerializedInventorybyScansinList) {
|
|
|
67 |
Object sumObj = inventoryForItem.get("sum");
|
|
|
68 |
String sumString = sumObj.toString();
|
|
|
69 |
Long availability = Long.parseLong(sumString);
|
|
|
70 |
itemAvailabilityByScans.put(Long.parseLong(inventoryForItem.get("itemId").toString()), availability);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
|
75 |
Map<Long, Long> itemAvailabilitiesOnSite = null;
|
|
|
76 |
|
|
|
77 |
try {
|
|
|
78 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
|
| 6493 |
amar.kumar |
79 |
List<Item> activeItems = catalogClient.getAllItems(false);
|
| 6484 |
amar.kumar |
80 |
List<Long> itemIds = new ArrayList<Long>();
|
|
|
81 |
for(Item item : activeItems) {
|
|
|
82 |
itemIds.add(item.getId());
|
|
|
83 |
}
|
|
|
84 |
Client inventoryClient = new InventoryClient().getClient();
|
|
|
85 |
itemAvailabilitiesOnSite = inventoryClient.getItemAvailabilitiesAtOurWarehouses(itemIds);
|
|
|
86 |
} catch (TException e) {
|
|
|
87 |
e.printStackTrace();
|
|
|
88 |
return;
|
|
|
89 |
} catch (CatalogServiceException e) {
|
|
|
90 |
// TODO Auto-generated catch block
|
|
|
91 |
e.printStackTrace();
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
for( Long itemId : itemAvailabilitiesOnSite.keySet()) {
|
|
|
96 |
if(itemAvailabilityByScans.containsKey(itemId)) {
|
|
|
97 |
if(itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId)) {
|
|
|
98 |
Long[] mismatch = new Long[2];
|
|
|
99 |
mismatch[0] = itemAvailabilityByScans.get(itemId);
|
|
|
100 |
mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
|
|
|
101 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
102 |
}
|
|
|
103 |
} else {
|
|
|
104 |
if(itemAvailabilitiesOnSite.get(itemId)!=0) {
|
|
|
105 |
Long[] mismatch = new Long[2];
|
|
|
106 |
mismatch[0] = 0L;
|
|
|
107 |
mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
|
|
|
108 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
for( Long itemId : itemAvailabilityByScans.keySet()) {
|
|
|
114 |
if(!availabilityMismatchMap.containsKey(itemId)) {
|
|
|
115 |
if(itemAvailabilitiesOnSite.containsKey(itemId)) {
|
|
|
116 |
if(itemAvailabilitiesOnSite.get(itemId)!=0 && (itemAvailabilitiesOnSite.get(itemId)!=itemAvailabilityByScans.get(itemId))) {
|
|
|
117 |
Long[] mismatch = new Long[2];
|
|
|
118 |
mismatch[0] = itemAvailabilityByScans.get(itemId);
|
|
|
119 |
mismatch[1] = itemAvailabilitiesOnSite.get(itemId);
|
|
|
120 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
121 |
}
|
|
|
122 |
} else {
|
|
|
123 |
if(itemAvailabilityByScans.get(itemId)!=0) {
|
|
|
124 |
Long[] mismatch = new Long[2];
|
|
|
125 |
mismatch[0] = itemAvailabilityByScans.get(itemId);
|
|
|
126 |
mismatch[1] = 0L;
|
|
|
127 |
availabilityMismatchMap.put(itemId, mismatch);
|
|
|
128 |
}
|
|
|
129 |
}
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
String subject = availabilityMismatchMap.size() + " mismatches in inventory compared to scan Records: ";
|
|
|
134 |
File file = new File("inv_mismatches.xls");
|
|
|
135 |
|
|
|
136 |
try {
|
|
|
137 |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
|
|
|
138 |
bufferedWriter.write(StringUtils.join(new String[] { "Item Id",
|
|
|
139 |
"Brand", "Model Name", "Model Number",
|
|
|
140 |
"Color", "Inventory shown", "Inventory through Scans" }, '\t'));
|
|
|
141 |
for (Long itemId : availabilityMismatchMap.keySet()) {
|
| 9217 |
amar.kumar |
142 |
if(availabilityMismatchMap.get(itemId)[0] == availabilityMismatchMap.get(itemId)[1]){
|
|
|
143 |
continue;
|
|
|
144 |
}
|
| 6484 |
amar.kumar |
145 |
Item item = null;
|
|
|
146 |
try {
|
|
|
147 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
|
|
|
148 |
item = catalogClient.getItem(itemId);
|
|
|
149 |
} catch (CatalogServiceException e) {
|
|
|
150 |
// TODO Auto-generated catch block
|
|
|
151 |
e.printStackTrace();
|
|
|
152 |
} catch (TException e) {
|
|
|
153 |
// TODO Auto-generated catch block
|
|
|
154 |
e.printStackTrace();
|
|
|
155 |
}
|
|
|
156 |
bufferedWriter.newLine();
|
|
|
157 |
bufferedWriter.write(StringUtils.join(new String[] { String.valueOf(item.getId()), item.getBrand(), item.getModelName(),
|
| 6492 |
amar.kumar |
158 |
item.getModelNumber(), item.getColor(),availabilityMismatchMap.get(itemId)[1].toString(), availabilityMismatchMap.get(itemId)[0].toString() }, "\t"));
|
| 6484 |
amar.kumar |
159 |
}
|
|
|
160 |
bufferedWriter.close();
|
|
|
161 |
} catch (IOException e) {
|
|
|
162 |
//TODO logger.error("Could not write mismatches to file", e);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
GmailUtils g = new GmailUtils();
|
| 6634 |
amar.kumar |
166 |
g.sendSSLMessage(new String[]{ "amar.kumar@shop2020.in","sandeep.sachdeva@shop2020.in","rajveer.singh@shop2020.in" }, subject,
|
| 6484 |
amar.kumar |
167 |
"", "cnc.center@shop2020.in", "5h0p2o2o", file.getAbsolutePath());
|
|
|
168 |
}
|
|
|
169 |
}
|