Subversion Repositories SmartDukaan

Rev

Rev 31620 | Rev 31623 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
31604 tejbeer 1
package com.spice.profitmandi.dao.service.solr;
2
 
3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.net.HttpURLConnection;
8
import java.net.URL;
9
import java.net.URLConnection;
10
import java.time.ZoneId;
11
import java.util.ArrayList;
12
import java.util.Arrays;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Map.Entry;
17
import java.util.stream.Collectors;
18
import java.util.zip.GZIPInputStream;
19
 
20
import org.apache.logging.log4j.LogManager;
21
import org.apache.logging.log4j.Logger;
22
import org.apache.solr.client.solrj.SolrClient;
23
import org.apache.solr.client.solrj.impl.HttpSolrClient;
24
import org.apache.solr.common.SolrInputDocument;
25
import org.json.JSONArray;
26
import org.json.JSONException;
27
import org.json.JSONObject;
28
import org.springframework.beans.factory.annotation.Autowired;
31613 tejbeer 29
import org.springframework.beans.factory.annotation.Value;
31604 tejbeer 30
import org.springframework.stereotype.Component;
31
 
32
import com.google.gson.Gson;
33
import com.spice.profitmandi.dao.entity.catalog.TagListing;
34
import com.spice.profitmandi.dao.entity.catalog.TagRanking;
31612 tejbeer 35
import com.spice.profitmandi.dao.entity.dtr.WebProductListing;
31604 tejbeer 36
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
37
import com.spice.profitmandi.dao.repository.catalog.TagRankingRepository;
38
import com.spice.profitmandi.dao.repository.dtr.Mongo;
31612 tejbeer 39
import com.spice.profitmandi.dao.repository.dtr.WebListingRepository;
40
import com.spice.profitmandi.dao.repository.dtr.WebProductListingRepository;
31604 tejbeer 41
import com.spice.profitmandi.service.tag.ItemTagModel;
42
 
43
import in.shop2020.model.v1.catalog.status;
44
 
45
@Component
46
public class FofoSolr {
47
 
48
	@Autowired
49
	private Gson gson;
50
 
51
	@Autowired
52
	private TagListingRepository tagListingRepository;
53
 
54
	@Autowired
55
	TagRankingRepository tagRankingRepository;
56
 
57
	@Autowired
58
	private Mongo contentMongoClient;
59
 
31612 tejbeer 60
	@Autowired
61
	private WebListingRepository webListingRepository;
62
 
63
	@Autowired
64
	private WebProductListingRepository webProductListingRepository;
65
 
31613 tejbeer 66
	@Value("${new.solr.url}")
67
	private String solrUrl;
68
 
31604 tejbeer 69
	private static final Logger logger = LogManager.getLogger(FofoSolr.class);
70
 
71
	private static final List<Integer> CATEGORY_MASTER = Arrays.asList(10006, 10010, 14202, 14203);
72
 
31622 tejbeer 73
	String solrPath = "http://" + "50.116.10.120" + "8984/solr/demo";
31604 tejbeer 74
	String mongoHost = "localhost";
75
	SolrClient solr = new HttpSolrClient.Builder(solrPath).build();
76
 
77
	private String getAvailabilityJSON() {
78
		String url = "http://50.116.10.120/reports/run.php?execute_mode=EXECUTE&xmlin=warehousecisnew.xml&project=FOCOR&project_password=focor&target_format=JSON";
79
		return getUrlContent(url);
80
	}
81
 
82
	private String getPendingPOJSON() {
83
		String url = "http://50.116.10.120/reports/run.php?execute_mode=EXECUTE&xmlin=UnfulfilledPOItemsNew.xml&project=FOCOR&project_password=focor&target_format=JSON";
84
		return getUrlContent(url);
85
	}
86
 
87
	private static String getUrlContent(String urlString) {
88
		BufferedReader reader = null;
89
		try {
90
			URL url = new URL(urlString);
91
			URLConnection conn = url.openConnection();
92
			conn.setConnectTimeout(5000);
93
			conn.setReadTimeout(5000);
94
			reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
95
			return reader.lines().collect(Collectors.joining(System.lineSeparator()));
96
		} catch (IOException e) {
97
			e.printStackTrace();
98
		} finally {
99
			if (reader != null) {
100
				try {
101
					reader.close();
102
				} catch (IOException e) {
103
					e.printStackTrace();
104
				}
105
			}
106
		}
107
		return null;
108
	}
109
 
110
	public JSONArray getAvailability() throws IOException, JSONException {
111
		String url = "http://50.116.10.120/reports/run.php?execute_mode=EXECUTE&xmlin=warehousecisnew.xml&project=FOCOR&project_password=focor&target_format=JSON";
112
		HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
113
		connection.setRequestProperty("Accept-Encoding", "gzip");
114
		BufferedReader reader = null;
115
 
116
		try {
117
			InputStream inputStream = connection.getInputStream();
118
 
119
			if ("gzip".equals(connection.getContentEncoding())) {
120
				inputStream = new GZIPInputStream(inputStream);
121
			}
122
 
123
			reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
124
			StringBuilder responseBuilder = new StringBuilder();
125
			String line;
126
 
127
			while ((line = reader.readLine()) != null) {
128
				responseBuilder.append(line);
129
			}
130
 
131
			JSONObject responseJson = new JSONObject(responseBuilder.toString());
132
			return responseJson.getJSONArray("data");
133
		} finally {
134
			if (reader != null) {
135
				try {
136
					reader.close();
137
				} catch (IOException e) {
138
					// Ignore
139
				}
140
			}
141
		}
142
	}
143
 
144
	public JSONArray getPendingPO() throws IOException, JSONException {
145
		String url = "http://50.116.10.120/reports/run.php?execute_mode=EXECUTE&xmlin=UnfulfilledPOItemsNew.xml&project=FOCOR&project_password=focor&target_format=JSON";
146
		HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
147
		connection.setRequestProperty("Accept-Encoding", "gzip");
148
		BufferedReader reader = null;
149
 
150
		try {
151
			InputStream inputStream = connection.getInputStream();
152
 
153
			if ("gzip".equals(connection.getContentEncoding())) {
154
				inputStream = new GZIPInputStream(inputStream);
155
			}
156
 
157
			reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
158
			StringBuilder responseBuilder = new StringBuilder();
159
			String line;
160
 
161
			while ((line = reader.readLine()) != null) {
162
				responseBuilder.append(line);
163
			}
164
 
165
			JSONObject responseJson = new JSONObject(responseBuilder.toString());
166
			return responseJson.getJSONArray("data");
167
		} finally {
168
			if (reader != null) {
169
				try {
170
					reader.close();
171
				} catch (IOException e) {
172
					// Ignore
173
				}
174
			}
175
		}
176
	}
177
 
178
	public Map<String, Map<String, Map<String, Integer>>> getItemMap() throws Exception {
179
		JSONArray availabilityJSONArray = this.getAvailability();
180
		JSONArray pendingPOJSONArray = this.getPendingPO();
181
		Map<String, Map<String, Map<String, Integer>>> availabilityItemMap = new HashMap<>();
182
 
183
		for (int i = 0; i < availabilityJSONArray.length(); i++) {
184
			JSONObject rawAvailability = availabilityJSONArray.getJSONObject(i);
185
			if (!availabilityItemMap.containsKey(rawAvailability.getString("Itemid"))) {
186
				availabilityItemMap.put(rawAvailability.getString("Itemid"),
187
						new HashMap<String, Map<String, Integer>>());
188
			}
189
			if (!availabilityItemMap.get(rawAvailability.getString("Itemid"))
190
					.containsKey(rawAvailability.getString("Warehouseid"))) {
191
				availabilityItemMap.get(rawAvailability.getString("Itemid"))
192
						.put(rawAvailability.getString("Warehouseid"), new HashMap<String, Integer>());
193
				availabilityItemMap.get(rawAvailability.getString("Itemid"))
194
						.get(rawAvailability.getString("Warehouseid")).put("netAvailability", 0);
195
				availabilityItemMap.get(rawAvailability.getString("Itemid"))
196
						.get(rawAvailability.getString("Warehouseid")).put("netPo", 0);
197
			}
198
			int netAvailability = availabilityItemMap.get(rawAvailability.getString("Itemid"))
199
					.get(rawAvailability.getString("Warehouseid")).get("netAvailability")
200
					+ rawAvailability.getInt("Netavailability");
201
			availabilityItemMap.get(rawAvailability.getString("Itemid")).get(rawAvailability.getString("Warehouseid"))
202
					.put("netAvailability", netAvailability);
203
		}
204
 
205
		for (int i = 0; i < pendingPOJSONArray.length(); i++) {
206
			JSONObject rawPendingPo = pendingPOJSONArray.getJSONObject(i);
207
 
208
			String itemId = rawPendingPo.getString("Itemid");
209
			String warehouseId = rawPendingPo.getString("Warehouseid");
210
			if (!availabilityItemMap.containsKey(itemId)) {
211
				availabilityItemMap.put(itemId, new HashMap<String, Map<String, Integer>>());
212
			}
213
			if (!availabilityItemMap.get(itemId).containsKey(warehouseId)) {
214
				availabilityItemMap.get(itemId).put(warehouseId, new HashMap<String, Integer>() {
215
					{
216
						put("netAvailability", 0);
217
						put("netPo", 0);
218
					}
219
				});
220
			}
221
			availabilityItemMap.get(itemId).get(warehouseId).put("netPo",
222
					availabilityItemMap.get(itemId).get(warehouseId).get("netPo") + rawPendingPo.getInt("Unfulfilled"));
223
 
224
		}
225
		return availabilityItemMap;
226
 
227
	}
228
 
31612 tejbeer 229
	public Map<Integer, List<String>> getLabels() {
230
		List<String> labels = Arrays.asList("partner-best-sellers", "partner-price-drop", "new-launches");
231
 
232
		Map<Integer, String> webListing = webListingRepository.selectByUrls(labels).stream()
233
				.collect(Collectors.toMap(x -> x.getId(), x -> x.getUrl()));
234
 
235
		Map<Integer, List<WebProductListing>> webProductListingMap = webProductListingRepository
236
				.selectAllByWebListingIds(new ArrayList<>(webListing.keySet())).stream()
237
				.collect(Collectors.groupingBy(x -> x.getWebListingId()));
238
 
239
		Map<Integer, List<String>> catalogLabelMap = new HashMap<>();
240
		for (Entry<Integer, List<WebProductListing>> webProductListingEntry : webProductListingMap.entrySet()) {
241
 
242
			for (WebProductListing webProduct : webProductListingEntry.getValue()) {
243
 
244
				if (!catalogLabelMap.containsKey(webProduct.getEntityId())) {
245
 
246
					catalogLabelMap.put(webProduct.getEntityId(), new ArrayList<>());
247
 
248
				}
249
 
250
				List<String> itemLabels = catalogLabelMap.get(webProduct.getEntityId());
251
				itemLabels.add(webListing.get(webProduct.getWebListingId()));
252
				catalogLabelMap.put(webProduct.getEntityId(), itemLabels);
253
 
254
			}
255
 
256
		}
257
 
258
		return catalogLabelMap;
259
	}
260
 
31620 tejbeer 261
	public void populateTagItems() throws Exception {
31604 tejbeer 262
		Map<String, Map<String, Map<String, Integer>>> availabilityItemMap = getItemMap();
263
		List<TagRanking> tagRankingList = tagRankingRepository.getAllTagRanking();
264
		List<Integer> rankingList = new ArrayList<>();
265
		Map<Integer, String> featureMap = new HashMap<>();
266
		for (TagRanking tagRanking : tagRankingList) {
267
			rankingList.add(tagRanking.getCatalogItemId());
268
			featureMap.put(tagRanking.getCatalogItemId(), tagRanking.getFeature());
269
		}
270
		Map<Integer, Map<String, Object>> catalogMap = new HashMap<>();
271
 
31612 tejbeer 272
		List<status> statuses = Arrays.asList(status.ACTIVE, status.PAUSED_BY_RISK, status.PARTIALLY_ACTIVE);
273
 
274
		Map<Integer, List<String>> catalogLabelMap = this.getLabels();
275
 
276
		logger.info("catalogLabelMap {}", catalogLabelMap);
277
 
278
		/*
279
		 * List<ItemTagModel> tuples =
280
		 * tagListingRepository.getAllItemTagByStatus(statuses).stream() .filter(x ->
281
		 * x.getItem().getCatalogItemId() == 1023747).collect(Collectors.toList());
282
		 */
283
 
31604 tejbeer 284
		List<ItemTagModel> tuples = tagListingRepository.getAllItemTagByStatus(statuses);
285
 
286
		Map<String, Object> projection = new HashMap<>();
287
		projection.put("defaultImageUrl", 1);
288
 
31612 tejbeer 289
		List<String> excludeBrands = Arrays.asList("Live Demo", "Dummy", "FOC HANDSET");
31604 tejbeer 290
 
291
		for (ItemTagModel result : tuples) {
292
			TagListing tag = result.getTagListing();
293
			com.spice.profitmandi.dao.entity.catalog.Item item = result.getItem();
294
			if (excludeBrands.contains(item.getBrand())) {
295
				continue;
296
			}
297
 
298
			if (!catalogMap.containsKey(item.getCatalogItemId())) {
299
				Map<String, Object> catalogObj = new HashMap<>();
300
				catalogObj.put("feature", "");
301
				catalogObj.put("title",
302
						String.join(" ", Arrays.asList(item.getBrand(), item.getModelName(), item.getModelNumber())
303
								.stream().filter(s -> s != null && !s.isEmpty()).collect(Collectors.toList())));
31612 tejbeer 304
				catalogObj.put("brand", new String[] { item.getBrand() });
31604 tejbeer 305
				if (item.getBrand().equals("Huawei") || item.getBrand().equals("Honor")) {
306
					catalogObj.put("brand", new String[] { "Huawei", "Honor" });
307
				}
308
				if (item.getBrand().equals("Mi") || item.getBrand().equals("Xiaomi")
309
						|| item.getBrand().equals("Redmi")) {
310
					catalogObj.put("brand", new String[] { "Mi", "Xiaomi", "Redmi" });
311
				}
312
				catalogObj.put("identifier", item.getCatalogItemId());
313
				catalogObj.put("items", new HashMap<Integer, Map<String, Object>>());
314
				Map<String, Object> filterMap = new HashMap<>();
315
				filterMap.put("_id", item.getCatalogItemId());
316
				// Don't include if catalog not available
317
				try {
31620 tejbeer 318
					String imageUrl = contentMongoClient.getEntityById(item.getCatalogItemId()).getDefaultImageUrl();
31604 tejbeer 319
 
31620 tejbeer 320
					if (imageUrl != null) {
321
						catalogObj.put("imageUrl", imageUrl);
322
					} else {
323
						catalogObj.put("imageUrl",
324
								"https://images.smartdukaan.com/uploads/campaigns/" + item.getCatalogItemId() + ".jpg");
325
					}
31604 tejbeer 326
				} catch (Exception e) {
327
					try {
328
						catalogObj.put("imageUrl",
329
								"https://images.smartdukaan.com/uploads/campaigns/" + item.getCatalogItemId() + ".jpg");
330
					} catch (Exception e2) {
331
						e2.printStackTrace();
332
					}
333
				}
334
				try {
335
					catalogObj.put("rank", rankingList.indexOf(item.getCatalogItemId()));
336
				} catch (Exception e) {
337
					// A very big number
338
					e.printStackTrace();
339
					catalogObj.put("rank", 50000000);
340
				}
341
				if (featureMap.containsKey(item.getCatalogItemId())
342
						&& featureMap.get(item.getCatalogItemId()) != null) {
343
					catalogObj.put("feature", featureMap.get(item.getCatalogItemId()));
344
 
345
					catalogObj.put("categoryId",
346
							CATEGORY_MASTER.contains(item.getCategoryId()) ? item.getCategoryId() : 6);
347
 
348
				}
349
 
350
				if (item.getCategoryId() == 10006) {
351
					catalogObj.put("categoryId", 3);
352
				}
353
				catalogObj.put("subCategoryId", item.getCategoryId());
31612 tejbeer 354
				catalogObj.put("create_timestamp",
355
						tag.getCreatedTimestamp().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
356
 
357
				if (catalogLabelMap.containsKey(item.getCatalogItemId())
358
						&& catalogLabelMap.get(item.getCatalogItemId()) != null) {
359
					catalogObj.put("labels", catalogLabelMap.get(item.getCatalogItemId()));
360
 
361
				}
31604 tejbeer 362
				catalogMap.put(item.getCatalogItemId(), catalogObj);
363
				catalogObj.put("avColor", new HashMap<>());
31612 tejbeer 364
 
31604 tejbeer 365
			}
366
 
367
			Map<String, Object> catalogObj = catalogMap.get(item.getCatalogItemId());
368
			Map<Integer, Integer> avColorMap = (Map<Integer, Integer>) catalogObj.get("avColor");
369
 
370
			if (availabilityItemMap.containsKey(String.valueOf(item.getId()))) {
371
				for (Map.Entry<String, Map<String, Integer>> entry : availabilityItemMap
372
						.get(String.valueOf(item.getId())).entrySet()) {
373
					String warehouseId = entry.getKey();
374
					Map<String, Integer> avMap = entry.getValue();
375
					if (!avColorMap.containsKey(Integer.parseInt(warehouseId))) {
376
						avColorMap.put(Integer.parseInt(warehouseId), 0);
377
					}
378
					int color = avColorMap.get(Integer.parseInt(warehouseId));
379
 
380
					if (avMap.get("netAvailability") > 0) {
381
						color = 2;
382
					} else if (avMap.get("netAvailability") + avMap.get("netPo") > 0) {
383
						color = Math.max(color, 1);
384
					} else {
385
						color = Math.max(color, 0);
386
					}
387
					avColorMap.put(Integer.parseInt(warehouseId), color);
388
 
389
					catalogObj.put("avColor", avColorMap);
390
 
391
				}
392
 
393
			}
394
 
395
			Map<Integer, Object> itemColorMap = (Map<Integer, Object>) catalogObj.get("items");
396
 
397
			if (!itemColorMap.containsKey(item.getId())) {
398
				itemColorMap.put(item.getId(), new HashMap<String, Object>() {
399
					{
400
						put("color", item.getColor().replace("f_", ""));
401
						put("tagPricing", new ArrayList<TagListing>());
402
					}
403
				});
404
			}
405
			Map<String, Object> itemMap = (Map<String, Object>) itemColorMap.get(item.getId());
406
			List<TagListing> tagPricing = (List<TagListing>) itemMap.get("tagPricing");
407
			tagPricing.add(tag);
408
		}
409
 
410
		logger.info("catalogObj {}", catalogMap);
411
		List<SolrInputDocument> catalogSolrObjs = new ArrayList<>();
412
		for (Entry<Integer, Map<String, Object>> entry : catalogMap.entrySet()) {
413
			int catalogId = entry.getKey();
414
			Map<String, Object> catalogValMap = entry.getValue();
415
			Map<Integer, Object> itemsMap = (Map<Integer, Object>) catalogValMap.get("items");
416
 
417
			boolean active = false;
418
			// Map<Integer, Object> itemsMap = (Map<String, Object>)
419
			// catalogValMap.get("items");
420
			List<SolrInputDocument> itemObjs = new ArrayList<>();
421
			float mop = 0;
422
 
423
			for (Entry<Integer, Object> itemEntry : itemsMap.entrySet()) {
424
				int itemId = itemEntry.getKey();
425
				Map<String, Object> itemMap = (Map<String, Object>) itemEntry.getValue();
426
				List<TagListing> tags = (List<TagListing>) itemMap.get("tagPricing");
427
				SolrInputDocument itemObj = new SolrInputDocument();
428
 
429
				for (TagListing taglist : tags) {
430
					active = active || (Boolean) taglist.isActive();
431
					itemObj.setField("id", "itemtag-" + itemId + "-" + taglist.getTagId());
432
					itemObj.setField("color_s", itemMap.get("color"));
433
					itemObj.setField("itemId_i", itemId);
434
					itemObj.setField("tagId_i", taglist.getTagId());
435
					itemObj.setField("mrp_f", taglist.getMrp());
436
					itemObj.setField("mop_f", taglist.getMop());
437
					itemObj.setField("sellingPrice_f", taglist.getSellingPrice());
438
					itemObj.setField("active_b", taglist.isActive());
439
					itemObj.setField("hot_deal_b", taglist.isHotDeals());
440
					mop = taglist.getMop();
441
 
442
				}
443
 
444
				itemObjs.add(itemObj);
445
			}
446
 
447
			logger.info("catalogValMap {}", catalogValMap);
448
 
449
			logger.info("rank {}", catalogValMap.get("rank"));
450
 
451
			SolrInputDocument catalogSolrObj = new SolrInputDocument();
452
 
453
			catalogSolrObj.setField("id", "catalog" + catalogId);
454
			catalogSolrObj.setField("rank_i", catalogValMap.get("rank"));
455
			catalogSolrObj.setField("title_s", catalogValMap.get("title"));
31612 tejbeer 456
			// catalogSolrObj.setField("_childDocuments_", itemObjs);
31604 tejbeer 457
			catalogSolrObj.addChildDocuments(itemObjs);
31616 tejbeer 458
			catalogSolrObj.setField("child_b", itemObjs.size() > 0);
31604 tejbeer 459
			catalogSolrObj.setField("catalogId_i", catalogId);
460
			catalogSolrObj.setField("imageUrl_s",
461
					catalogValMap.get("imageUrl").toString().replace("saholic", "smartdukaan"));
462
			catalogSolrObj.setField("feature_s", catalogValMap.get("feature"));
463
			catalogSolrObj.setField("brand_ss", catalogValMap.get("brand"));
464
			catalogSolrObj.setField("create_s", catalogValMap.get("create_timestamp"));
465
			catalogSolrObj.setField("categoryId_i", catalogValMap.get("categoryId"));
466
			catalogSolrObj.setField("subCategoryId_i", catalogValMap.get("subCategoryId"));
31612 tejbeer 467
			catalogSolrObj.setField("label_ss", catalogValMap.get("labels"));
31604 tejbeer 468
			catalogSolrObj.setField("w7573_i", 0);
469
			catalogSolrObj.setField("w7678_i", 0);
470
			catalogSolrObj.setField("w7720_i", 0);
471
			catalogSolrObj.setField("w8468_i", 0);
472
			catalogSolrObj.setField("w8889_i", 0);
473
			catalogSolrObj.setField("w8947_i", 0);
474
			catalogSolrObj.setField("w9213_i", 0);
475
			catalogSolrObj.setField("w9203_i", 0);
476
			catalogSolrObj.setField("mop_f", mop);
31612 tejbeer 477
			catalogSolrObj.setField("show_default_b", false);
478
 
479
			String[] brands = (String[]) catalogValMap.get("brand");
480
			for (String brand : brands) {
481
				if (brand != "FOC") {
482
					catalogSolrObj.setField("show_default_b", true);
483
 
484
				}
485
			}
31604 tejbeer 486
			Map<Integer, Integer> avColorMap = (Map<Integer, Integer>) catalogValMap.get("avColor");
487
 
488
			for (Entry<Integer, Integer> avColorEntry : avColorMap.entrySet()) {
489
				int whId = avColorEntry.getKey();
490
				int color = (int) avColorEntry.getValue();
491
				catalogSolrObj.setField("w" + whId + "_i", color);
492
			}
493
			catalogSolrObj.setField("active_b", active);
494
			logger.info("catalogSolrObj {}", catalogSolrObj.get("rank_i"));
495
 
496
			catalogSolrObjs.add(catalogSolrObj);
497
 
498
		}
499
 
500
		logger.info("catalogSolrObjs {}", catalogSolrObjs);
501
		solr.deleteByQuery("*:*");
502
		solr.add(catalogSolrObjs);
503
 
31612 tejbeer 504
		org.apache.solr.client.solrj.response.UpdateResponse updateResponse = solr.commit();
31604 tejbeer 505
		logger.info("solr {}", updateResponse.getStatus());
506
 
507
	}
508
 
509
	/*
510
	 * public void populateTagItems() throws Exception { Map<String, Map<String,
511
	 * Map<String, Integer>>> availabilityItemMap = getItemMap(); List<TagRanking>
512
	 * tagRankingList = tagRankingRepository.getAllTagRanking();
513
	 * 
514
	 * List<Integer> rankingList = new ArrayList<>(); SolrInputDocument featureMap =
515
	 * new SolrInputDocument(); for (TagRanking tagRanking : tagRankingList) {
516
	 * rankingList.add(tagRanking.getCatalogItemId());
517
	 * featureMap.addField(String.valueOf(tagRanking.getCatalogItemId()),
518
	 * tagRanking.getFeature()); }
519
	 * 
520
	 * Map<Integer, SolrInputDocument> catalogMap = new HashMap<>(); List<status>
521
	 * statuses = new ArrayList<>(); statuses.add(status.ACTIVE);
522
	 * statuses.add(status.PAUSED_BY_RISK); statuses.add(status.PARTIALLY_ACTIVE);
523
	 * List<ItemTagModel> tuples =
524
	 * tagListingRepository.getAllItemTagByStatus(statuses); SolrInputDocument
525
	 * projection = new SolrInputDocument();
526
	 * 
527
	 * projection.addField("defaultImageUrl", 1); List<String> excludeBrands =
528
	 * Arrays.asList("Live Demo", "FOC", "Dummy", "FOC HANDSET");
529
	 * 
530
	 * for (ItemTagModel result : tuples) { TagListing tag = result.getTagListing();
531
	 * com.spice.profitmandi.dao.entity.catalog.Item item = result.getItem(); if
532
	 * (excludeBrands.contains(item.getBrand())) { continue; }
533
	 * 
534
	 * if (!catalogMap.containsKey(item.getCatalogItemId())) { SolrInputDocument
535
	 * catalogObj = new SolrInputDocument(); catalogObj.addField("feature", "");
536
	 * catalogObj.addField("title", String.join(" ", Arrays.asList(item.getBrand(),
537
	 * item.getModelName(), item.getModelNumber()) .stream().filter(s -> s != null
538
	 * && !s.isEmpty()).collect(Collectors.toList()))); catalogObj.addField("brand",
539
	 * item.getBrand()); if (item.getBrand().equals("Huawei") ||
540
	 * item.getBrand().equals("Honor")) { catalogObj.addField("brand", new String[]
541
	 * { "Huawei", "Honor" }); } if (item.getBrand().equals("Mi") ||
542
	 * item.getBrand().equals("Xiaomi") || item.getBrand().equals("Redmi")) {
543
	 * catalogObj.addField("brand", new String[] { "Mi", "Xiaomi", "Redmi" }); }
544
	 * catalogObj.addField("identifier", item.getCatalogItemId());
545
	 * catalogObj.addField("items", new SolrInputDocument()); SolrInputDocument
546
	 * filterMap = new SolrInputDocument(); filterMap.addField("_id",
547
	 * item.getCatalogItemId());
548
	 * 
549
	 * try {
550
	 * 
551
	 * BrandCatalog brandCatalog = brandsRepository.selectByBrand(item.getBrand());
552
	 * catalogObj.addField("imageUrl", brandCatalog.getLogoUrl());
553
	 * System.out.println(catalogObj.get("imageUrl")); } catch (Exception e) { try {
554
	 * catalogObj.addField("imageUrl",
555
	 * "https://images.smartdukaan.com/uploads/campaigns/" + item.getCatalogItemId()
556
	 * + ".jpg"); } catch (Exception e2) { e2.printStackTrace(); } } try {
557
	 * catalogObj.addField("rank", rankingList.indexOf(item.getCatalogItemId())); }
558
	 * catch (Exception e) { // A very big number e.printStackTrace();
559
	 * catalogObj.addField("rank", 50000000); } if
560
	 * (featureMap.containsKey(String.valueOf(item.getCatalogItemId())) &&
561
	 * featureMap.get(String.valueOf(item.getCatalogItemId())) != null) {
562
	 * catalogObj.addField("feature", featureMap.get(item.getCatalogItemId()));
563
	 * 
564
	 * catalogObj.addField("categoryId",
565
	 * CATEGORY_MASTER.contains(item.getCategoryId()) ? item.getCategoryId() : 6);
566
	 * 
567
	 * }
568
	 * 
569
	 * if (item.getCategoryId() == 10006) { catalogObj.addField("categoryId", 3); }
570
	 * catalogObj.addField("subCategoryId", item.getCategoryId());
571
	 * catalogObj.addField("create_timestamp", tag.getCreatedTimestamp());
572
	 * catalogMap.put(item.getCatalogItemId(), catalogObj);
573
	 * catalogObj.addField("avColor", new SolrInputDocument());
574
	 * 
575
	 * // Don't include if catalog not available
576
	 * 
577
	 * catalogObj = catalogMap.get(item.getCatalogItemId()); if
578
	 * (availabilityItemMap.containsKey(String.valueOf(item.getId()))) { for
579
	 * (Map.Entry<String, Map<String, Integer>> entry : availabilityItemMap
580
	 * .get(String.valueOf(item.getId())).entrySet()) { String warehouseId =
581
	 * entry.getKey(); Map<String, Integer> avMap = entry.getValue();
582
	 * 
583
	 * SolrInputDocument avColorDoc = (SolrInputDocument)
584
	 * catalogObj.getFieldValue("avColor"); //.catalogObj.get if
585
	 * (!avColorDoc.containsKey(warehouseId)) { avColorDoc.addField(warehouseId, 0);
586
	 * } int color = (int) avColorDoc.getFieldValue(warehouseId);
587
	 * 
588
	 * if (avMap.get("netAvailability") > 0) { color = 2; } else if
589
	 * (avMap.get("netAvailability") + avMap.get("netPo") > 0) { color =
590
	 * Math.max(color, 1); } else { color = Math.max(color, 0); }
591
	 * 
592
	 * avColorDoc.addField(warehouseId, 0);
593
	 * 
594
	 * }
595
	 * 
596
	 * }
597
	 * 
598
	 * SolrInputDocument itemColorDoc = (SolrInputDocument)
599
	 * catalogObj.getFieldValue("items");
600
	 * 
601
	 * if (!itemColorDoc.containsKey(item.getId())) {
602
	 * 
603
	 * itemColorDoc.addField(String.valueOf(item.getId()), new SolrInputDocument() {
604
	 * { addField("color", item.getColor().replace("f_", ""));
605
	 * addField("tagPricing", new ArrayList<TagListing>()); } }); }
606
	 * 
607
	 * SolrInputDocument itemMap = (SolrInputDocument) itemColorDoc
608
	 * .getFieldValue(String.valueOf(item.getId()));
609
	 * 
610
	 * List<TagListing> tagPricing = (List<TagListing>)
611
	 * itemMap.getFieldValue("tagPricing"); tagPricing.add(tag);
612
	 * 
613
	 * List<Map<String, Object>> catalogObjs = new ArrayList<>(); for
614
	 * (Entry<Integer, HashMap<String, Object>> entry : catalogMap.entrySet()) { int
615
	 * catalogId = entry.getKey(); Map<String, Object> catalogValMap =
616
	 * entry.getValue();
617
	 * 
618
	 * boolean active = false; Map<String, Object> itemsMap = (Map<String, Object>)
619
	 * catalogValMap.get("items"); List<Map<String, Object>> itemObjs = new
620
	 * ArrayList<>(); float mop = 0;
621
	 * 
622
	 * for (Entry<String, Object> itemEntry : itemsMap.entrySet()) { int itemId =
623
	 * Integer.parseInt(itemEntry.getKey()); Map<String, Object> itemValMap =
624
	 * (Map<String, Object>) itemEntry.getValue(); List<TagListing> tags =
625
	 * (List<TagListing>) catalogValMap.get("tagPricing");
626
	 * 
627
	 * for (TagListing taglist : tags) { active = active || (Boolean)
628
	 * taglist.isActive(); Map<String, Object> itemObj = new HashMap<>();
629
	 * itemObj.put("id", "itemtag-" + itemId + "-" + tag.getTagId());
630
	 * itemObj.put("color_s", itemMap.get("color")); itemObj.put("itemId_i",
631
	 * itemId); itemObj.put("tagId_i", tag.getTagId()); itemObj.put("mrp_f",
632
	 * tag.getMrp()); itemObj.put("mop_f", tag.getMop());
633
	 * itemObj.put("sellingPrice_f", tag.getSellingPrice()); itemObj.put("active_b",
634
	 * tag.isActive()); itemObj.put("hot_deal_b", tag.isHotDeals()); mop =
635
	 * tag.getMop(); itemObjs.add(itemObj); } } // Map<String, Object> catalogObj =
636
	 * new HashMap<String, Object>(); catalogObj.put("id", "catalog" + catalogId);
637
	 * catalogObj.put("rank_i", catalogMap.get("rank")); catalogObj.put("title_s",
638
	 * catalogMap.get("title")); catalogObj.put("_childDocuments_", itemObjs);
639
	 * catalogObj.put("child_b", itemObjs.size() > 0); catalogObj.put("catalogId_i",
640
	 * catalogId); catalogObj.put("imageUrl_s",
641
	 * catalogMap.get("imageUrl").toString().replace("saholic", "smartdukaan"));
642
	 * catalogObj.put("feature_s", catalogMap.get("feature"));
643
	 * catalogObj.put("brand_ss", catalogMap.get("brand"));
644
	 * catalogObj.put("create_s", catalogMap.get("create_timestamp"));
645
	 * catalogObj.put("categoryId_i", catalogMap.get("categoryId"));
646
	 * catalogObj.put("subCategoryId_i", catalogMap.get("subCategoryId"));
647
	 * catalogObj.put("w7573_i", 0); catalogObj.put("w7678_i", 0);
648
	 * catalogObj.put("w7720_i", 0); catalogObj.put("w8468_i", 0);
649
	 * catalogObj.put("w8889_i", 0); catalogObj.put("w8947_i", 0);
650
	 * catalogObj.put("w9213_i", 0); catalogObj.put("w9203_i", 0);
651
	 * catalogObj.put("mop_f", mop); for (Entry<Integer, Integer> avColorEntry :
652
	 * avColorMap.entrySet()) { int whId = avColorEntry.getKey(); int color = (int)
653
	 * avColorEntry.getValue(); catalogObj.put("w" + whId + "_i", color);
654
	 * 
655
	 * catalogObj.put("active_b", active); catalogObjs.add(catalogObj);
656
	 * solr.deleteByQuery("*:*"); // solr.add((catalogObjs); }
657
	 * 
658
	 * } } }
659
	 * 
660
	 * }
661
	 */
662
 
663
	public void pushData() throws Exception {
664
		this.populateTagItems();
665
	}
666
}