Subversion Repositories SmartDukaan

Rev

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