Subversion Repositories SmartDukaan

Rev

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

Rev 33574 Rev 33973
Line 1... Line 1...
1
package com.spice.profitmandi.dao.service.solr;
1
package com.spice.profitmandi.dao.service.solr;
2
 
2
 
-
 
3
import com.fasterxml.jackson.core.JsonProcessingException;
-
 
4
import com.fasterxml.jackson.databind.ObjectMapper;
3
import com.google.gson.Gson;
5
import com.google.gson.Gson;
4
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
5
import com.spice.profitmandi.common.model.ProfitMandiConstants;
7
import com.spice.profitmandi.common.model.ProfitMandiConstants;
-
 
8
import com.spice.profitmandi.dao.entity.catalog.SuperCatalogMapping;
6
import com.spice.profitmandi.dao.entity.catalog.TagListing;
9
import com.spice.profitmandi.dao.entity.catalog.TagListing;
7
import com.spice.profitmandi.dao.entity.catalog.TagRanking;
10
import com.spice.profitmandi.dao.entity.catalog.TagRanking;
8
import com.spice.profitmandi.dao.entity.dtr.WebProductListing;
11
import com.spice.profitmandi.dao.entity.dtr.WebProductListing;
9
import com.spice.profitmandi.dao.entity.similarModel.SimilarModel;
12
import com.spice.profitmandi.dao.entity.similarModel.SimilarModel;
-
 
13
import com.spice.profitmandi.dao.repository.catalog.SuperCatalogMappingRepository;
10
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
14
import com.spice.profitmandi.dao.repository.catalog.TagListingRepository;
11
import com.spice.profitmandi.dao.repository.catalog.TagRankingRepository;
15
import com.spice.profitmandi.dao.repository.catalog.TagRankingRepository;
12
import com.spice.profitmandi.dao.repository.dtr.Mongo;
16
import com.spice.profitmandi.dao.repository.dtr.Mongo;
13
import com.spice.profitmandi.dao.repository.dtr.WebListingRepository;
17
import com.spice.profitmandi.dao.repository.dtr.WebListingRepository;
14
import com.spice.profitmandi.dao.repository.dtr.WebProductListingRepository;
18
import com.spice.profitmandi.dao.repository.dtr.WebProductListingRepository;
15
import com.spice.profitmandi.dao.repository.similarModel.SimilarModelRepository;
19
import com.spice.profitmandi.dao.repository.similarModel.SimilarModelRepository;
-
 
20
import com.spice.profitmandi.service.catalog.SuperCatalogModel;
16
import com.spice.profitmandi.service.tag.ItemTagModel;
21
import com.spice.profitmandi.service.tag.ItemTagModel;
17
import in.shop2020.model.v1.catalog.status;
22
import in.shop2020.model.v1.catalog.status;
18
import org.apache.logging.log4j.LogManager;
23
import org.apache.logging.log4j.LogManager;
19
import org.apache.logging.log4j.Logger;
24
import org.apache.logging.log4j.Logger;
20
import org.apache.solr.client.solrj.SolrClient;
25
import org.apache.solr.client.solrj.SolrClient;
Line 60... Line 65...
60
    private WebListingRepository webListingRepository;
65
    private WebListingRepository webListingRepository;
61
 
66
 
62
    @Autowired
67
    @Autowired
63
    private WebProductListingRepository webProductListingRepository;
68
    private WebProductListingRepository webProductListingRepository;
64
 
69
 
-
 
70
    @Autowired
-
 
71
    private SuperCatalogMappingRepository superCatalogMappingRepository;
-
 
72
 
65
    @Value("${new.solr.url}")
73
    @Value("${new.solr.url}")
66
    private String solrUrl;
74
    private String solrUrl;
67
 
75
 
68
    @Value("${reportico.url}")
76
    @Value("${reportico.url}")
69
    private String reporticoUrl;
77
    private String reporticoUrl;
Line 268... Line 276...
268
 
276
 
269
        List<status> statuses = Arrays.asList(status.ACTIVE, status.PAUSED_BY_RISK, status.PARTIALLY_ACTIVE);
277
        List<status> statuses = Arrays.asList(status.ACTIVE, status.PAUSED_BY_RISK, status.PARTIALLY_ACTIVE);
270
 
278
 
271
        Map<Integer, List<String>> catalogLabelMap = this.getLabels();
279
        Map<Integer, List<String>> catalogLabelMap = this.getLabels();
272
 
280
 
273
        logger.info("catalogLabelMap {}", catalogLabelMap);
281
        //logger.info("catalogLabelMap {}", catalogLabelMap);
274
 
282
 
275
        List<ItemTagModel> itemTagModels = tagListingRepository.getAllItemTagByStatus(statuses);
283
        List<ItemTagModel> itemTagModels = tagListingRepository.getAllItemTagByStatus(statuses);
276
 
284
 
277
        Map<String, Object> projection = new HashMap<>();
285
        Map<String, Object> projection = new HashMap<>();
278
        projection.put("defaultImageUrl", 1);
286
        projection.put("defaultImageUrl", 1);
Line 412... Line 420...
412
            List<TagListing> tagPricing = (List<TagListing>) itemMap.get("tagPricing");
420
            List<TagListing> tagPricing = (List<TagListing>) itemMap.get("tagPricing");
413
            tagPricing.add(tagListing);
421
            tagPricing.add(tagListing);
414
        }
422
        }
415
        //logger.info("catalogObj {}", catalogMap);
423
        //logger.info("catalogObj {}", catalogMap);
416
 
424
 
-
 
425
        List<SuperCatalogModel> superCatalogMappingList = superCatalogMappingRepository.selectJoinedData();
-
 
426
        List<SuperCatalogMapping> superCatalogs = superCatalogMappingRepository.selectAll();
417
        List<SolrInputDocument> catalogSolrObjs = new ArrayList<>();
427
        List<SolrInputDocument> catalogSolrObjs = new ArrayList<>();
418
        for (Entry<Integer, Map<String, Object>> entry : catalogMap.entrySet()) {
428
        for (Entry<Integer, Map<String, Object>> entry : catalogMap.entrySet()) {
419
            int catalogId = entry.getKey();
429
            int catalogId = entry.getKey();
-
 
430
            Optional<SuperCatalogModel> superCatalogMapping = findMappingByCatalogId(superCatalogMappingList, catalogId);
420
            Map<String, Object> catalogValMap = entry.getValue();
431
            Map<String, Object> catalogValMap = entry.getValue();
421
            Map<Integer, Object> itemsMap = (Map<Integer, Object>) catalogValMap.get("items");
432
            Map<Integer, Object> itemsMap = (Map<Integer, Object>) catalogValMap.get("items");
422
 
433
 
423
            boolean active = false;
434
            boolean active = false;
424
            // Map<Integer, Object> itemsMap = (Map<String, Object>)
435
            // Map<Integer, Object> itemsMap = (Map<String, Object>)
Line 447... Line 458...
447
                    mop = taglist.getMop();
458
                    mop = taglist.getMop();
448
                    dp = taglist.getSellingPrice();
459
                    dp = taglist.getSellingPrice();
449
                }
460
                }
450
                itemObjs.add(itemObj);
461
                itemObjs.add(itemObj);
451
            }
462
            }
452
            //logger.info("catalogValMap {}", catalogValMap);
-
 
453
            //logger.info("rank {}", catalogValMap.get("rank"));
-
 
454
 
463
 
455
            SolrInputDocument catalogSolrObj = new SolrInputDocument();
464
            SolrInputDocument catalogSolrObj = new SolrInputDocument();
456
            catalogSolrObj.setField("id", "catalog" + catalogId);
465
            catalogSolrObj.setField("id", "catalog" + catalogId);
457
            catalogSolrObj.setField("rank_i", catalogValMap.get("rank"));
466
            catalogSolrObj.setField("rank_i", catalogValMap.get("rank"));
458
            catalogSolrObj.setField("title_s", catalogValMap.get("title"));
467
            catalogSolrObj.setField("title_s", catalogValMap.get("title"));
459
            // catalogSolrObj.setField("_childDocuments_", itemObjs);
-
 
460
            catalogSolrObj.addChildDocuments(itemObjs);
468
            catalogSolrObj.addChildDocuments(itemObjs);
461
            catalogSolrObj.setField("child_b", itemObjs.size() > 0);
469
            catalogSolrObj.setField("child_b", itemObjs.size() > 0);
462
            catalogSolrObj.setField("catalogId_i", catalogId);
470
            catalogSolrObj.setField("catalogId_i", catalogId);
-
 
471
            if (superCatalogMapping.isPresent()) {
-
 
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 {
-
 
478
                catalogSolrObj.setField("superCatalog_i", 0);
-
 
479
                catalogSolrObj.setField("superCatalog_s", "_");
-
 
480
                catalogSolrObj.setField("superCatalogVariants_s", "[]");
-
 
481
            }
463
            catalogSolrObj.setField("imageUrl_s",
482
            catalogSolrObj.setField("imageUrl_s",
464
                    catalogValMap.get("imageUrl").toString().replace("saholic", "smartdukaan"));
483
                    catalogValMap.get("imageUrl").toString().replace("saholic", "smartdukaan"));
465
            catalogSolrObj.setField("feature_s", catalogValMap.get("feature"));
484
            catalogSolrObj.setField("feature_s", catalogValMap.get("feature"));
466
            catalogSolrObj.setField("brand_ss", catalogValMap.get("brand"));
485
            catalogSolrObj.setField("brand_ss", catalogValMap.get("brand"));
467
            catalogSolrObj.setField("create_s", catalogValMap.get("create_timestamp"));
486
            catalogSolrObj.setField("create_s", catalogValMap.get("create_timestamp"));
Line 475... Line 494...
475
 
494
 
476
            String[] brands = (String[]) catalogValMap.get("brand");
495
            String[] brands = (String[]) catalogValMap.get("brand");
477
            for (String brand : brands) {
496
            for (String brand : brands) {
478
                if (brand.equals("FOC")) {
497
                if (brand.equals("FOC")) {
479
                    catalogSolrObj.setField("show_default_b", false);
498
                    catalogSolrObj.setField("show_default_b", false);
480
 
-
 
481
                }
499
                }
482
 
500
 
483
                if (brand.equals("Live Demo")) {
501
                if (brand.equals("Live Demo")) {
484
                    catalogSolrObj.setField("show_default_b", false);
502
                    catalogSolrObj.setField("show_default_b", false);
485
 
-
 
486
                }
503
                }
487
            }
504
            }
488
            Map<Integer, Integer> avColorMap = (Map<Integer, Integer>) catalogValMap.get("avColor");
505
            Map<Integer, Integer> avColorMap = (Map<Integer, Integer>) catalogValMap.get("avColor");
489
 
506
 
490
 
-
 
491
            for (Integer warehouseId : ProfitMandiConstants.WAREHOUSE_MAP.keySet()) {
507
            for (Integer warehouseId : ProfitMandiConstants.WAREHOUSE_MAP.keySet()) {
492
                catalogSolrObj.setField("w" + warehouseId + "_i", 0);
508
                catalogSolrObj.setField("w" + warehouseId + "_i", 0);
493
            }
509
            }
494
            for (Entry<Integer, Integer> avColorEntry : avColorMap.entrySet()) {
510
            for (Entry<Integer, Integer> avColorEntry : avColorMap.entrySet()) {
495
                int whId = avColorEntry.getKey();
511
                int whId = avColorEntry.getKey();
Line 511... Line 527...
511
                    }
527
                    }
512
                    if (modelPrice > endPrice) break;
528
                    if (modelPrice > endPrice) break;
513
                }
529
                }
514
                similalarModels.remove(Integer.valueOf(catalogId));
530
                similalarModels.remove(Integer.valueOf(catalogId));
515
            }
531
            }
516
            catalogSolrObj.setField("similarModels_ii", similalarModels == null ? Collections.EMPTY_LIST : similalarModels);
532
            catalogSolrObj.setField("similarModels_is", similalarModels == null ? Collections.EMPTY_LIST : similalarModels);
517
 
533
 
518
 
534
 
519
            catalogSolrObj.setField("active_b", active);
535
            catalogSolrObj.setField("active_b", active);
520
            logger.info("catalogSolrObj {}", catalogSolrObj.get("rank_i"));
-
 
521
            catalogSolrObjs.add(catalogSolrObj);
536
            catalogSolrObjs.add(catalogSolrObj);
522
        }
537
        }
523
 
538
 
524
 
-
 
525
        logger.info("catalogSolrObjs {}", catalogSolrObjs);
-
 
526
 
-
 
527
        String solrPath = "http://" + solrUrl + ":8984/solr/demo";
539
        String solrPath = "http://" + solrUrl + ":8984/solr/demo";
528
        SolrClient solr = new HttpSolrClient.Builder(solrPath).build();
540
        SolrClient solr = new HttpSolrClient.Builder(solrPath).build();
529
 
541
 
530
        solr.deleteByQuery("*:*");
542
        solr.deleteByQuery("*:*");
531
        solr.add(catalogSolrObjs);
543
        solr.add(catalogSolrObjs);
Line 536... Line 548...
536
    }
548
    }
537
 
549
 
538
    public void pushData() throws Exception {
550
    public void pushData() throws Exception {
539
        this.populateTagItems();
551
        this.populateTagItems();
540
    }
552
    }
-
 
553
 
-
 
554
    public Optional<SuperCatalogModel> findMappingByCatalogId(List<SuperCatalogModel> superCatalogMappingList, int id) {
-
 
555
 
-
 
556
        return superCatalogMappingList.parallelStream()
-
 
557
                .filter(mapping -> mapping.getCatalogId() == id)
-
 
558
                .findFirst();
-
 
559
    }
-
 
560
 
-
 
561
    public String findMappingVariantsByCatalogId(List<SuperCatalogMapping> superCatalogMappingList, int id) throws JsonProcessingException {
-
 
562
        List<Map<String, Object>> variantList = new ArrayList<>();
-
 
563
        List<SuperCatalogMapping> matchingVariantNames = superCatalogMappingList.parallelStream()
-
 
564
                .filter(mapping -> mapping.getSuperCatalogId() == id)
-
 
565
                .collect(Collectors.toList());
-
 
566
 
-
 
567
        if (!matchingVariantNames.isEmpty()) {
-
 
568
            for (SuperCatalogMapping matchingVariantName : matchingVariantNames) {
-
 
569
                logger.info("matchingVariantName: "+matchingVariantName);
-
 
570
                   if (matchingVariantName != null && matchingVariantName.getVariantName() != null) {
-
 
571
                    Map<String, Object> variantObj = new HashMap<>();
-
 
572
                    variantObj.put("catalogId", matchingVariantName.getCatalogId());
-
 
573
                    variantObj.put("variantName", matchingVariantName.getVariantName());
-
 
574
                    variantList.add(variantObj);
-
 
575
                }
-
 
576
            }
-
 
577
        }
-
 
578
 
-
 
579
        if (variantList.isEmpty()) {
-
 
580
            return "[]";
-
 
581
        }
-
 
582
 
-
 
583
        variantList.sort((v1, v2) -> {
-
 
584
            int[] n1 = extractNumbers((String) v1.get("variantName"));
-
 
585
            int[] n2 = extractNumbers((String) v2.get("variantName"));
-
 
586
            int cmp = Integer.compare(n1[0], n2[0]);
-
 
587
            return cmp != 0 ? cmp : Integer.compare(n1[1], n2[1]);
-
 
588
        });
-
 
589
 
-
 
590
        ObjectMapper mapper = new ObjectMapper();
-
 
591
        return mapper.writeValueAsString(variantList);
-
 
592
    }
-
 
593
 
-
 
594
    private int[] extractNumbers(String input) {
-
 
595
        String[] parts = input.split("\\s+");
-
 
596
        int[] result = new int[2];
-
 
597
 
-
 
598
        for (int i = 0; i < Math.min(parts.length, 2); i++) {
-
 
599
            String part = parts[i].toUpperCase();
-
 
600
            if (part.endsWith("GB")) {
-
 
601
                result[i] = Integer.parseInt(part.replace("GB", "").trim());
-
 
602
            } else if (part.endsWith("TB")) {
-
 
603
                result[i] = Integer.parseInt(part.replace("TB", "").trim()) * 1024;
-
 
604
            }
-
 
605
        }
-
 
606
 
-
 
607
        return result;
-
 
608
    }
-
 
609
 
541
}
610
}