Subversion Repositories SmartDukaan

Rev

Rev 33499 | Rev 33546 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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