Subversion Repositories SmartDukaan

Rev

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