Authored by wangnan

实时skn促销列表需求

@@ -2,6 +2,7 @@ package com.yoho.search.service.aggregations.impls; @@ -2,6 +2,7 @@ package com.yoho.search.service.aggregations.impls;
2 2
3 import com.yoho.search.core.es.agg.IAggregation; 3 import com.yoho.search.core.es.agg.IAggregation;
4 import com.yoho.search.service.base.index.*; 4 import com.yoho.search.service.base.index.*;
  5 +import com.yoho.search.service.base.index.promotion.PromotionIndexBaseService;
5 import com.yoho.search.service.helper.SearchAfterCacheHelper; 6 import com.yoho.search.service.helper.SearchAfterCacheHelper;
6 import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch; 7 import com.yoho.search.service.scorer.personal.PersonalVectorFeatureSearch;
7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,13 +28,15 @@ public class AggregationFactoryService { @@ -27,13 +28,15 @@ public class AggregationFactoryService {
27 @Autowired 28 @Autowired
28 private ShopsIndexBaseService shopsIndexBaseService; 29 private ShopsIndexBaseService shopsIndexBaseService;
29 @Autowired 30 @Autowired
30 - private PromotionIndexBaseService promotionIndexBaseService; 31 + private ZqNameIndexBaseService zqNameIndexBaseService;
31 @Autowired 32 @Autowired
32 private PersonalVectorFeatureSearch personalVectorFeatureSearch; 33 private PersonalVectorFeatureSearch personalVectorFeatureSearch;
33 @Autowired 34 @Autowired
34 private ProductIndexBaseService productIndexBaseService; 35 private ProductIndexBaseService productIndexBaseService;
35 @Autowired 36 @Autowired
36 private CustomizeTagBaseService customizeTagBaseService; 37 private CustomizeTagBaseService customizeTagBaseService;
  38 + @Autowired
  39 + private PromotionIndexBaseService promotionIndexBaseService;
37 40
38 public IAggregation getAgeLevelAggregation() { 41 public IAggregation getAgeLevelAggregation() {
39 return new AgeLevelAggregation(); 42 return new AgeLevelAggregation();
@@ -124,7 +127,7 @@ public class AggregationFactoryService { @@ -124,7 +127,7 @@ public class AggregationFactoryService {
124 } 127 }
125 128
126 public IAggregation getPromotionAggregation(int aggCount) { 129 public IAggregation getPromotionAggregation(int aggCount) {
127 - return new PromotionAggregation(promotionIndexBaseService, aggCount); 130 + return new PromotionAggregation(zqNameIndexBaseService, aggCount);
128 } 131 }
129 132
130 public IAggregation getKeywordAggregation(Map<String, String> paramMap, int aggCount) { 133 public IAggregation getKeywordAggregation(Map<String, String> paramMap, int aggCount) {
1 package com.yoho.search.service.aggregations.impls; 1 package com.yoho.search.service.aggregations.impls;
2 2
  3 +import com.yoho.search.base.utils.ProductIndexEsField;
  4 +import com.yoho.search.service.base.index.ZqNameIndexBaseService;
  5 +import org.apache.commons.collections.CollectionUtils;
  6 +import org.elasticsearch.search.aggregations.Aggregation;
  7 +
3 import java.util.ArrayList; 8 import java.util.ArrayList;
4 import java.util.HashMap; 9 import java.util.HashMap;
5 import java.util.List; 10 import java.util.List;
6 import java.util.Map; 11 import java.util.Map;
7 import java.util.stream.Collectors; 12 import java.util.stream.Collectors;
8 13
9 -import org.apache.commons.collections.CollectionUtils;  
10 -import org.elasticsearch.search.aggregations.Aggregation;  
11 -  
12 -import com.yoho.search.base.utils.ProductIndexEsField;  
13 -import com.yoho.search.service.base.index.PromotionIndexBaseService;  
14 -  
15 /** 14 /**
16 * Created by wangnan on 2017/5/11. 15 * Created by wangnan on 2017/5/11.
17 */ 16 */
18 public class PromotionAggregation extends AbstractSingleFieldAggregation { 17 public class PromotionAggregation extends AbstractSingleFieldAggregation {
19 18
20 - private PromotionIndexBaseService promotionIndexBaseService; 19 + private ZqNameIndexBaseService zqNameIndexBaseService;
21 20
22 - public PromotionAggregation(PromotionIndexBaseService promotionIndexBaseService, int count) { 21 + public PromotionAggregation(ZqNameIndexBaseService zqNameIndexBaseService, int count) {
23 super(count); 22 super(count);
24 - this.promotionIndexBaseService = promotionIndexBaseService; 23 + this.zqNameIndexBaseService = zqNameIndexBaseService;
25 } 24 }
26 25
27 @Override 26 @Override
@@ -49,10 +48,10 @@ public class PromotionAggregation extends AbstractSingleFieldAggregation { @@ -49,10 +48,10 @@ public class PromotionAggregation extends AbstractSingleFieldAggregation {
49 if (CollectionUtils.isEmpty(promotionIdList)) { 48 if (CollectionUtils.isEmpty(promotionIdList)) {
50 return new ArrayList<>(); 49 return new ArrayList<>();
51 } 50 }
52 - // 调用promotion索引查询title 51 + // 调用zqname索引查询title
53 Map<String, String> promotionParam = new HashMap<>(); 52 Map<String, String> promotionParam = new HashMap<>();
54 promotionParam.put("ids", promotionIdList.stream().collect(Collectors.joining(","))); 53 promotionParam.put("ids", promotionIdList.stream().collect(Collectors.joining(",")));
55 - List<Map<String, Object>> promotionList = promotionIndexBaseService.queryListByParam(promotionParam); 54 + List<Map<String, Object>> promotionList = zqNameIndexBaseService.queryZQNameListByParam(promotionParam);
56 // promotion_title去重,is_promotion用逗号连接 55 // promotion_title去重,is_promotion用逗号连接
57 Map<String, Map<String, Object>> titleMap = new HashMap<>(); 56 Map<String, Map<String, Object>> titleMap = new HashMap<>();
58 for (Map<String, Object> map : promotionList) { 57 for (Map<String, Object> map : promotionList) {
1 package com.yoho.search.service.aggregations.impls; 1 package com.yoho.search.service.aggregations.impls;
2 2
3 -import java.util.ArrayList;  
4 -import java.util.Date;  
5 -import java.util.List;  
6 -import java.util.Map;  
7 -import java.util.stream.Collectors;  
8 - 3 +import com.yoho.search.base.utils.DateUtil;
  4 +import com.yoho.search.base.utils.ISearchConstants;
  5 +import com.yoho.search.base.utils.ProductIndexEsField;
  6 +import com.yoho.search.core.es.agg.AbstractAggregation;
  7 +import com.yoho.search.models.RecommendPromotionAggVO;
  8 +import com.yoho.search.service.base.index.promotion.PromotionIndexBaseService;
9 import org.elasticsearch.index.query.BoolQueryBuilder; 9 import org.elasticsearch.index.query.BoolQueryBuilder;
10 import org.elasticsearch.index.query.QueryBuilders; 10 import org.elasticsearch.index.query.QueryBuilders;
11 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder; 11 import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
@@ -17,12 +17,11 @@ import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuil @@ -17,12 +17,11 @@ import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuil
17 import org.elasticsearch.search.aggregations.bucket.terms.LongTerms; 17 import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
18 import org.elasticsearch.search.aggregations.bucket.terms.Terms; 18 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
19 19
20 -import com.yoho.search.base.utils.DateUtil;  
21 -import com.yoho.search.base.utils.ISearchConstants;  
22 -import com.yoho.search.base.utils.ProductIndexEsField;  
23 -import com.yoho.search.core.es.agg.AbstractAggregation;  
24 -import com.yoho.search.models.RecommendPromotionAggVO;  
25 -import com.yoho.search.service.base.index.PromotionIndexBaseService; 20 +import java.util.ArrayList;
  21 +import java.util.Date;
  22 +import java.util.List;
  23 +import java.util.Map;
  24 +import java.util.stream.Collectors;
26 25
27 public class RecommendPromotionAggregation extends AbstractAggregation { 26 public class RecommendPromotionAggregation extends AbstractAggregation {
28 private static final String TERM_AGGREGATION_NAME = "promotionIdAgg"; 27 private static final String TERM_AGGREGATION_NAME = "promotionIdAgg";
@@ -5,6 +5,9 @@ import com.alibaba.fastjson.JSONObject; @@ -5,6 +5,9 @@ import com.alibaba.fastjson.JSONObject;
5 import com.yoho.search.base.models.PromotionCond; 5 import com.yoho.search.base.models.PromotionCond;
6 import com.yoho.search.base.utils.DateUtil; 6 import com.yoho.search.base.utils.DateUtil;
7 import com.yoho.search.base.utils.ProductIndexEsField; 7 import com.yoho.search.base.utils.ProductIndexEsField;
  8 +import com.yoho.search.service.base.index.promotion.PromotionCondService;
  9 +import com.yoho.search.service.base.index.promotion.PromotionMatchService;
  10 +import com.yoho.search.service.base.index.promotion.PromotionTypeService;
8 import org.apache.commons.collections.CollectionUtils; 11 import org.apache.commons.collections.CollectionUtils;
9 import org.apache.commons.collections.MapUtils; 12 import org.apache.commons.collections.MapUtils;
10 import org.apache.commons.lang.StringUtils; 13 import org.apache.commons.lang.StringUtils;
@@ -28,7 +31,11 @@ public class ProductIndexBaseService { @@ -28,7 +31,11 @@ public class ProductIndexBaseService {
28 @Autowired 31 @Autowired
29 private ProductPricePlanIndexBaseService productPricePlanIndexBaseService; 32 private ProductPricePlanIndexBaseService productPricePlanIndexBaseService;
30 @Autowired 33 @Autowired
31 - private PromotionIndexBaseService promotionIndexBaseService; 34 + private PromotionCondService promotionCondService;
  35 + @Autowired
  36 + private PromotionMatchService promotionMatchService;
  37 + @Autowired
  38 + private PromotionTypeService promotionTypeService;
32 39
33 40
34 // 获取从source中不返回的字段定义,用以节省带宽 41 // 获取从source中不返回的字段定义,用以节省带宽
@@ -348,48 +355,55 @@ public class ProductIndexBaseService { @@ -348,48 +355,55 @@ public class ProductIndexBaseService {
348 355
349 356
350 /** 357 /**
351 - * 获取商品列表[并返回促销信息] 358 + * 获取商品列表[并返回促销标签信息]
  359 + * add by wangnan in 2018/1/29
352 */ 360 */
353 public List<Map<String, Object>> getProductListWithPromotion(List<Map<String, Object>> productEsSourceList) { 361 public List<Map<String, Object>> getProductListWithPromotion(List<Map<String, Object>> productEsSourceList) {
354 if (productEsSourceList == null || productEsSourceList.isEmpty()) { 362 if (productEsSourceList == null || productEsSourceList.isEmpty()) {
355 return new ArrayList<>(); 363 return new ArrayList<>();
356 } 364 }
357 - //获取结果列表中的skn 365 +
  366 + //1.获取结果列表中的skn列表
358 String[] sknStr = new String[productEsSourceList.size()]; 367 String[] sknStr = new String[productEsSourceList.size()];
359 for (int i = 0; i < productEsSourceList.size(); i++) { 368 for (int i = 0; i < productEsSourceList.size(); i++) {
360 sknStr[i] = MapUtils.getString(productEsSourceList.get(i), "product_skn", ""); 369 sknStr[i] = MapUtils.getString(productEsSourceList.get(i), "product_skn", "");
361 } 370 }
362 - //全量查promotion索引内容,1分钟缓存  
363 - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();  
364 - List<PromotionCond> promotionCondList = promotionIndexBaseService.getPromotionCondList();  
365 371
366 - //处理每个skn匹配的促销 372 + //2.全量查promotion索引内容,用于和每个skn匹配,1分钟缓存
  373 + List<PromotionCond> promotionCondList = promotionCondService.getPromotionCondList();
  374 +
  375 + //3.构建每个skn匹配的促销
  376 + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
367 for (Map<String, Object> productEsSource : productEsSourceList) { 377 for (Map<String, Object> productEsSource : productEsSourceList) {
368 - //获取商品信息map 378 + //3.1 获取skn字段信息
369 Map<String, Object> productMap = this.getProductMapFromEsSource(productEsSource); 379 Map<String, Object> productMap = this.getProductMapFromEsSource(productEsSource);
370 - //获取该skn匹配上的促销 380 + //3.2 获取该skn匹配上的促销
371 List<PromotionCond> matchedPromotionCondList = promotionCondList.stream() 381 List<PromotionCond> matchedPromotionCondList = promotionCondList.stream()
372 - .filter(promotionCond -> promotionIndexBaseService.matchProduct(promotionCond, productMap)).collect(Collectors.toList());  
373 - //TODO: 查询促销类型->促销名映射的索引  
374 - Map<String, String> promotionTypeMap = new HashMap<>(); 382 + .filter(promotionCond -> promotionMatchService.matchProduct(promotionCond, productMap))
  383 + .collect(Collectors.toList());
  384 + //3.3 查询促销类型->促销名映射的索引获取map,用于填充促销标签名称,1分钟缓存
  385 + Map<String, String> promotionTypeMap = promotionTypeService.getPromotionTypeMap();
  386 + //3.4 生成促销标签对象列表
375 if (CollectionUtils.isNotEmpty(matchedPromotionCondList)) { 387 if (CollectionUtils.isNotEmpty(matchedPromotionCondList)) {
376 - List<JSONObject> matchedPromotions = matchedPromotionCondList.stream().map(promotionCond -> {  
377 - JSONObject matchedPromotion = new JSONObject();  
378 - matchedPromotion.put("id", promotionCond.getPromotionId());  
379 - matchedPromotion.put("type", promotionCond.getPromotionType());  
380 - matchedPromotion.put("startTime", promotionCond.getStartTime());  
381 - matchedPromotion.put("endTime", promotionCond.getEndTime());  
382 - if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {  
383 - matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType()));  
384 - }  
385 - return matchedPromotion;  
386 - }).collect(Collectors.toList());  
387 - //插入促销信息 388 + List<JSONObject> matchedPromotions = matchedPromotionCondList.stream()
  389 + .map(promotionCond -> {
  390 + JSONObject matchedPromotion = new JSONObject();
  391 + matchedPromotion.put("id", promotionCond.getPromotionId());
  392 + matchedPromotion.put("type", promotionCond.getPromotionType());
  393 + matchedPromotion.put("startTime", promotionCond.getStartTime());
  394 + matchedPromotion.put("endTime", promotionCond.getEndTime());
  395 + if (!promotionTypeMap.isEmpty() && promotionCond.getShowStatus() == 1) {
  396 + matchedPromotion.put("name", promotionTypeMap.get(promotionCond.getPromotionType()));
  397 + }else{
  398 + matchedPromotion.put("name", "undefined");
  399 + }
  400 + return matchedPromotion;
  401 + }).collect(Collectors.toList());
  402 + //3.5 列表中插入促销标签信息
388 productMap.put("promotion_tag", matchedPromotions); 403 productMap.put("promotion_tag", matchedPromotions);
389 } 404 }
390 results.add(productMap); 405 results.add(productMap);
391 } 406 }
392 return results; 407 return results;
393 } 408 }
394 -  
395 } 409 }
  1 +package com.yoho.search.service.base.index;
  2 +
  3 +import com.yoho.search.base.utils.ConvertUtils;
  4 +import com.yoho.search.base.utils.ISearchConstants;
  5 +import com.yoho.search.core.es.model.SearchParam;
  6 +import com.yoho.search.core.es.model.SearchResult;
  7 +import com.yoho.search.service.base.SearchCommonService;
  8 +import com.yoho.search.service.base.index.promotion.PromotionIndexBaseService;
  9 +import org.apache.commons.collections.MapUtils;
  10 +import org.apache.commons.lang.StringUtils;
  11 +import org.elasticsearch.index.query.BoolQueryBuilder;
  12 +import org.elasticsearch.index.query.QueryBuilders;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
  15 +import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.stereotype.Service;
  17 +
  18 +import java.util.ArrayList;
  19 +import java.util.HashMap;
  20 +import java.util.List;
  21 +import java.util.Map;
  22 +
  23 +/**
  24 + * @Author: wangnan
  25 + * @Date: Created in 2018/1/29
  26 + * 专区名称索引服务,用于促销标签聚合里面
  27 + */
  28 +@Service
  29 +public class ZqNameIndexBaseService {
  30 +
  31 + @Autowired
  32 + private SearchCommonService searchCommonService;
  33 + private static final Logger logger = LoggerFactory.getLogger(PromotionIndexBaseService.class);
  34 + private static final String INDEX_NAME = ISearchConstants.INDEX_NAME_ZQ_NAME;
  35 +
  36 + public List<Map<String, Object>> queryZQNameListByParam(Map<String, String> paramMap) {
  37 + try {
  38 + // 1、构建SearchParam
  39 + SearchParam searchParam = new SearchParam();
  40 + BoolQueryBuilder boolFilterForShops = this.constructFilterBuilder(paramMap);
  41 + searchParam.setFiter(boolFilterForShops);
  42 + searchParam.setSize(10000);
  43 + searchParam.setQuery(QueryBuilders.matchAllQuery());
  44 + // 2、调搜索,并将结果加入缓存
  45 + List<Map<String, Object>> promotionsList = this.queryZQNameListByParam(searchParam);
  46 + return promotionsList;
  47 + } catch (Exception e) {
  48 + logger.error(e.getMessage(), e);
  49 + return new ArrayList<>();
  50 + }
  51 + }
  52 +
  53 + private List<Map<String, Object>> queryZQNameListByParam(SearchParam searchParam) {
  54 + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  55 + SearchResult searchResult = searchCommonService.doSearch(INDEX_NAME, searchParam);
  56 + if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) {
  57 + return results;
  58 + }
  59 + for (Map<String, Object> esMap : searchResult.getResultList()) {
  60 + results.add(this.getMap(esMap));
  61 + }
  62 + return results;
  63 + }
  64 +
  65 + private BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) {
  66 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  67 + if (paramMap.containsKey("ids") && StringUtils.isNotBlank(paramMap.get("ids"))) {
  68 + int[] ids = ConvertUtils.stringToIntArray(paramMap.get("ids"), ",");
  69 + boolFilter.must(QueryBuilders.termsQuery("id", ids));
  70 + }
  71 + return boolFilter;
  72 + }
  73 +
  74 + private Map<String, Object> getMap(Map<String, Object> esMap) {
  75 + Map<String, Object> map = new HashMap<>();
  76 + int id = MapUtils.getIntValue(esMap, "id", 0);
  77 + map.put("is_promotion", id + "");
  78 + map.put("promotion_title", MapUtils.getString(esMap, "title", ""));
  79 + return map;
  80 + }
  81 +}
1 -package com.yoho.search.service.base.index; 1 +package com.yoho.search.service.base.index.promotion;
2 2
3 import com.alibaba.fastjson.JSONArray; 3 import com.alibaba.fastjson.JSONArray;
4 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
5 import com.google.common.cache.CacheBuilder; 5 import com.google.common.cache.CacheBuilder;
6 import com.google.common.cache.CacheLoader; 6 import com.google.common.cache.CacheLoader;
7 import com.google.common.cache.LoadingCache; 7 import com.google.common.cache.LoadingCache;
8 -import com.yoho.search.base.helper.PromotionHelper;  
9 import com.yoho.search.base.models.PromotionCond; 8 import com.yoho.search.base.models.PromotionCond;
10 import com.yoho.search.base.models.PromotionIndexBO; 9 import com.yoho.search.base.models.PromotionIndexBO;
11 -import com.yoho.search.base.models.PromotionMatchFactor;  
12 -import com.yoho.search.base.utils.ConvertUtils;  
13 -import com.yoho.search.base.utils.DateUtil;  
14 import com.yoho.search.base.utils.ISearchConstants; 10 import com.yoho.search.base.utils.ISearchConstants;
15 -import com.yoho.search.base.utils.ProductIndexEsField;  
16 import com.yoho.search.core.es.model.SearchParam; 11 import com.yoho.search.core.es.model.SearchParam;
17 import com.yoho.search.core.es.model.SearchResult; 12 import com.yoho.search.core.es.model.SearchResult;
18 -import com.yoho.search.models.RecommendPromotionAggVO;  
19 import com.yoho.search.service.base.SearchCommonService; 13 import com.yoho.search.service.base.SearchCommonService;
20 -import com.yoho.search.service.base.SearchRequestParams;  
21 import org.apache.commons.collections.MapUtils; 14 import org.apache.commons.collections.MapUtils;
22 -import org.apache.commons.lang.StringUtils;  
23 import org.elasticsearch.index.query.BoolQueryBuilder; 15 import org.elasticsearch.index.query.BoolQueryBuilder;
24 import org.elasticsearch.index.query.QueryBuilders; 16 import org.elasticsearch.index.query.QueryBuilders;
25 import org.slf4j.Logger; 17 import org.slf4j.Logger;
@@ -27,52 +19,50 @@ import org.slf4j.LoggerFactory; @@ -27,52 +19,50 @@ import org.slf4j.LoggerFactory;
27 import org.springframework.beans.factory.annotation.Autowired; 19 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.stereotype.Service; 20 import org.springframework.stereotype.Service;
29 import org.springframework.util.Assert; 21 import org.springframework.util.Assert;
30 -import org.springframework.util.CollectionUtils;  
31 22
32 -import java.util.*; 23 +import java.util.ArrayList;
  24 +import java.util.List;
  25 +import java.util.Map;
33 import java.util.concurrent.TimeUnit; 26 import java.util.concurrent.TimeUnit;
34 import java.util.stream.Collectors; 27 import java.util.stream.Collectors;
35 28
  29 +
36 /** 30 /**
37 - * Created by wangnan on 2017/5/11. 31 + * 用于生成promotionCondList列表,判断skn匹配的促销,1分钟缓存。
  32 + * @Author: wangnan
  33 + * @Date: Created in 2018/1/29
38 */ 34 */
39 @Service 35 @Service
40 -public class PromotionIndexBaseService { 36 +public class PromotionCondService {
  37 +
  38 + @Autowired
  39 + private SearchCommonService searchCommonService;
41 40
42 - private static final Logger logger = LoggerFactory.getLogger(PromotionIndexBaseService.class);  
43 - private static final String INDEX_NAME = ISearchConstants.INDEX_NAME_PROMOTION;  
44 - private static final String CACHEKEY = "CacheKey";  
45 - private static final String PROMOTIONCONDLISTCACHEKEY = "PromotionCondListCacheKey"; 41 + private static final Logger logger = LoggerFactory.getLogger(PromotionCondService.class);
  42 + private static final String CACHE_KEY = "CacheKey";
  43 + private static final String PROMOTION_COND_LIST_CACHE_KEY = "promotionCondListCacheKey";
46 private volatile List<PromotionCond> promotionCondList = new ArrayList<>(); 44 private volatile List<PromotionCond> promotionCondList = new ArrayList<>();
47 45
48 //PromotionCondList Guava Cache 46 //PromotionCondList Guava Cache
49 - LoadingCache<String, JSONObject> PromotionCondListCache = CacheBuilder.newBuilder() 47 + LoadingCache<String, JSONObject> promotionCondListCache = CacheBuilder.newBuilder()
50 .maximumSize(30000) 48 .maximumSize(30000)
51 .expireAfterWrite(1, TimeUnit.MINUTES) 49 .expireAfterWrite(1, TimeUnit.MINUTES)
52 .build(new CacheLoader<String, JSONObject>() { 50 .build(new CacheLoader<String, JSONObject>() {
53 public JSONObject load(String key) { 51 public JSONObject load(String key) {
54 JSONObject jsonObject = new JSONObject(); 52 JSONObject jsonObject = new JSONObject();
55 - jsonObject.put(PROMOTIONCONDLISTCACHEKEY, buildPromotionCondList()); 53 + jsonObject.put(PROMOTION_COND_LIST_CACHE_KEY, buildPromotionCondList());
56 return jsonObject; 54 return jsonObject;
57 } 55 }
58 }); 56 });
59 57
60 - @Autowired  
61 - private SearchCommonService searchCommonService;  
62 - @Autowired  
63 - private PromotionHelper promotionHelper;  
64 -  
65 - /*******************************以下方法用于生成商品列表中每个skn的促销列表逻辑**********************************************/  
66 -  
67 -  
68 /** 58 /**
69 * 获取PromotionCondList,走缓存,1分钟 59 * 获取PromotionCondList,走缓存,1分钟
70 */ 60 */
71 public List<PromotionCond> getPromotionCondList() { 61 public List<PromotionCond> getPromotionCondList() {
72 try { 62 try {
73 - JSONObject jsonObject = PromotionCondListCache.get(CACHEKEY); 63 + JSONObject jsonObject = promotionCondListCache.get(CACHE_KEY);
74 Assert.isTrue(jsonObject != null, "[cache jsonObject cannot be null][fun=getPromotionCondList]"); 64 Assert.isTrue(jsonObject != null, "[cache jsonObject cannot be null][fun=getPromotionCondList]");
75 - promotionCondList = (List<PromotionCond>) jsonObject.get(PROMOTIONCONDLISTCACHEKEY); 65 + promotionCondList = (List<PromotionCond>) jsonObject.get(PROMOTION_COND_LIST_CACHE_KEY);
76 } catch (Exception e) { 66 } catch (Exception e) {
77 logger.error(e.getMessage()); 67 logger.error(e.getMessage());
78 } 68 }
@@ -171,15 +161,15 @@ public class PromotionIndexBaseService { @@ -171,15 +161,15 @@ public class PromotionIndexBaseService {
171 Assert.notEmpty(conditionParamJSONObj, "conditionParamJSONObj cannot be empty"); 161 Assert.notEmpty(conditionParamJSONObj, "conditionParamJSONObj cannot be empty");
172 promotionCond.setAggregator(PromotionCond.Aggregator.from(conditionParamJSONObj.getString("aggregator"))); 162 promotionCond.setAggregator(PromotionCond.Aggregator.from(conditionParamJSONObj.getString("aggregator")));
173 // conditions可能是数组或者对象 163 // conditions可能是数组或者对象
174 - Object condtionsObj = conditionParamJSONObj.get("conditions");  
175 - if (condtionsObj instanceof JSONArray) {  
176 - JSONArray condtionsJSONArr = (JSONArray) condtionsObj;  
177 - Assert.notEmpty(condtionsJSONArr, "condtionsJSONArr is empty For " + promotionIndexBO.getId());  
178 - promotionCond.setCondDetailList(condtionsJSONArr.stream().map(obj -> (JSONObject) obj).map(e -> constructPromotionCondDetail(e)).collect(Collectors.toList())); 164 + Object conditionsObject = conditionParamJSONObj.get("conditions");
  165 + if (conditionsObject instanceof JSONArray) {
  166 + JSONArray conditionsJSONArr = (JSONArray) conditionsObject;
  167 + Assert.notEmpty(conditionsJSONArr, "conditionsJSONArr is empty For " + promotionIndexBO.getId());
  168 + promotionCond.setCondDetailList(conditionsJSONArr.stream().map(obj -> (JSONObject) obj).map(e -> constructPromotionCondDetail(e)).collect(Collectors.toList()));
179 } else { 169 } else {
180 - JSONObject condtionsJSONObj = (JSONObject) condtionsObj;  
181 - Assert.notEmpty(condtionsJSONObj, "condtionsJSONObj is empty For " + promotionIndexBO.getId());  
182 - promotionCond.setCondDetailList(condtionsJSONObj.keySet().stream().map(key -> condtionsJSONObj.getJSONObject(key)).map(e -> constructPromotionCondDetail(e)).collect(Collectors.toList())); 170 + JSONObject conditionsJSONObj = (JSONObject) conditionsObject;
  171 + Assert.notEmpty(conditionsJSONObj, "conditionsJSONObj is empty For " + promotionIndexBO.getId());
  172 + promotionCond.setCondDetailList(conditionsJSONObj.keySet().stream().map(key -> conditionsJSONObj.getJSONObject(key)).map(e -> constructPromotionCondDetail(e)).collect(Collectors.toList()));
183 } 173 }
184 Assert.notEmpty(promotionCond.getCondDetailList(), "condDetailList cannot be empty"); 174 Assert.notEmpty(promotionCond.getCondDetailList(), "condDetailList cannot be empty");
185 return promotionCond; 175 return promotionCond;
@@ -197,17 +187,17 @@ public class PromotionIndexBaseService { @@ -197,17 +187,17 @@ public class PromotionIndexBaseService {
197 PromotionCond.CondDetail condDetail = new PromotionCond.CondDetail(); 187 PromotionCond.CondDetail condDetail = new PromotionCond.CondDetail();
198 condDetail.setAggregator(PromotionCond.Aggregator.from(condDetailJSONObj.getString("aggregator"))); 188 condDetail.setAggregator(PromotionCond.Aggregator.from(condDetailJSONObj.getString("aggregator")));
199 // conditions可能是数组或者对象 189 // conditions可能是数组或者对象
200 - Object condtionsObj = condDetailJSONObj.get("conditions");  
201 - if (condtionsObj == null) { 190 + Object conditionsObj = condDetailJSONObj.get("conditions");
  191 + if (conditionsObj == null) {
202 condDetail.setCondItemList(new ArrayList<>()); 192 condDetail.setCondItemList(new ArrayList<>());
203 return condDetail; 193 return condDetail;
204 } 194 }
205 - if (condtionsObj instanceof JSONArray) {  
206 - JSONArray condItemsJSONArray = (JSONArray) condtionsObj; 195 + if (conditionsObj instanceof JSONArray) {
  196 + JSONArray condItemsJSONArray = (JSONArray) conditionsObj;
207 condDetail.setCondItemList(condItemsJSONArray.isEmpty() ? new ArrayList<>() : 197 condDetail.setCondItemList(condItemsJSONArray.isEmpty() ? new ArrayList<>() :
208 condItemsJSONArray.stream().map(obj -> (JSONObject) obj).map(e -> constructPromotionCondItemBO(e)).collect(Collectors.toList())); 198 condItemsJSONArray.stream().map(obj -> (JSONObject) obj).map(e -> constructPromotionCondItemBO(e)).collect(Collectors.toList()));
209 } else { 199 } else {
210 - JSONObject condItemsJSONObj = (JSONObject) condtionsObj; 200 + JSONObject condItemsJSONObj = (JSONObject) conditionsObj;
211 condDetail.setCondItemList(condItemsJSONObj.isEmpty() ? new ArrayList<>() : 201 condDetail.setCondItemList(condItemsJSONObj.isEmpty() ? new ArrayList<>() :
212 condItemsJSONObj.keySet().stream().map(key -> condItemsJSONObj.getJSONObject(key)).map(e -> constructPromotionCondItemBO(e)).collect(Collectors.toList())); 202 condItemsJSONObj.keySet().stream().map(key -> condItemsJSONObj.getJSONObject(key)).map(e -> constructPromotionCondItemBO(e)).collect(Collectors.toList()));
213 } 203 }
@@ -215,7 +205,7 @@ public class PromotionIndexBaseService { @@ -215,7 +205,7 @@ public class PromotionIndexBaseService {
215 } 205 }
216 206
217 /** 207 /**
218 - * 构建PromotionCondDetail中的CondItemB 208 + * 构建PromotionCondDetail中的CondItem
219 */ 209 */
220 private PromotionCond.CondItem constructPromotionCondItemBO(JSONObject condItemJSONObj) { 210 private PromotionCond.CondItem constructPromotionCondItemBO(JSONObject condItemJSONObj) {
221 Assert.notEmpty(condItemJSONObj, "condItemJSONObj cannot be empty"); 211 Assert.notEmpty(condItemJSONObj, "condItemJSONObj cannot be empty");
@@ -226,137 +216,4 @@ public class PromotionIndexBaseService { @@ -226,137 +216,4 @@ public class PromotionIndexBaseService {
226 Assert.notNull(condItem); 216 Assert.notNull(condItem);
227 return condItem; 217 return condItem;
228 } 218 }
229 -  
230 -  
231 - /**  
232 - * 用matchCondDetail  
233 - */  
234 - public boolean matchProduct(PromotionCond promotionCond, Map<String, Object> productMap) {  
235 - boolean matchResult;  
236 - if (promotionCond == null) {  
237 - return false;  
238 - }  
239 - if (promotionCond.getAggregator() == PromotionCond.Aggregator.ALL) {  
240 - matchResult = promotionCond.getCondDetailList().stream().allMatch(condDetail -> matchCondDetail(condDetail, productMap));  
241 - } else {  
242 - matchResult = promotionCond.getCondDetailList().stream().anyMatch(condDetail -> matchCondDetail(condDetail, productMap));  
243 - }  
244 - if (matchResult) {  
245 - logger.debug("[func=PromotionCond.matchResult][skn={}][promotionId={}][promotionTitle={}]",  
246 - MapUtils.getIntValue(productMap, "id"), promotionCond.getPromotionId(), promotionCond.getPromotionTitle());  
247 - }  
248 - return matchResult;  
249 - }  
250 -  
251 - /**  
252 - * 匹配matchCondDetail  
253 - */  
254 - private boolean matchCondDetail(PromotionCond.CondDetail condDetail, Map<String, Object> productMap) {  
255 - if (CollectionUtils.isEmpty(condDetail.getCondItemList())) {  
256 - // 当没有具体条件,比如【全场满99元加5元换购YOHO!当期热销新刊】这种时所有productindex都匹配  
257 - return true;  
258 - } else if (condDetail.getAggregator() == PromotionCond.Aggregator.ALL) {  
259 - return condDetail.getCondItemList().stream().allMatch(condItem -> matchProduct(condItem, productMap));  
260 - } else {  
261 - return condDetail.getCondItemList().stream().anyMatch(condItem -> matchProduct(condItem, productMap));  
262 - }  
263 - }  
264 -  
265 - /**  
266 - * 生成一个参数对象去匹配促销,逻辑抽到了core工程的helper中  
267 - */  
268 - private boolean matchProduct(PromotionCond.CondItem condItem, Map<String, Object> productMap) {  
269 - PromotionMatchFactor promotionMatchFactor = new PromotionMatchFactor();  
270 - promotionMatchFactor.setBrandId(MapUtils.getIntValue(productMap, "brand_id"));  
271 - promotionMatchFactor.setSmallSortId(MapUtils.getIntValue(productMap, "smal_sort_id"));  
272 - promotionMatchFactor.setMiddleSortId(MapUtils.getIntValue(productMap, "middle_sort_id"));  
273 - promotionMatchFactor.setMaxSortId(MapUtils.getIntValue(productMap, "max_sort_id"));  
274 - promotionMatchFactor.setIspromotion(MapUtils.getIntValue(productMap, "is_promotion"));  
275 - promotionMatchFactor.setProductskn(MapUtils.getIntValue(productMap, "product_skn"));  
276 - return promotionHelper.matchProduct(condItem, promotionMatchFactor);  
277 - }  
278 -  
279 -  
280 - /*******************************以下方法用于促销标签聚合逻辑***************************************************/  
281 -  
282 - public List<Map<String, Object>> queryListByParam(Map<String, String> paramMap) {  
283 - try {  
284 - // 1、构建SearchParam  
285 - SearchParam searchParam = new SearchParam();  
286 - BoolQueryBuilder boolFilterForShops = this.constructFilterBuilder(paramMap);  
287 - searchParam.setFiter(boolFilterForShops);  
288 - searchParam.setSize(10000);  
289 - searchParam.setQuery(QueryBuilders.matchAllQuery());  
290 - // 2、调搜索,并将结果加入缓存  
291 - List<Map<String, Object>> promotionsList = this.queryListByParam(searchParam);  
292 - return promotionsList;  
293 - } catch (Exception e) {  
294 - logger.error(e.getMessage(), e);  
295 - return new ArrayList<>();  
296 - }  
297 - }  
298 -  
299 - public List<RecommendPromotionAggVO> getPromotionInfosByIds(String indexName, List<Integer> ids) {  
300 - SearchParam searchParam = new SearchParam();  
301 - BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();  
302 - boolQueryBuilder.must(QueryBuilders.termsQuery(ProductIndexEsField.id, ids));  
303 - boolQueryBuilder.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));  
304 - boolQueryBuilder.mustNot(QueryBuilders.termQuery(SearchRequestParams.PROMOTIONINDEX_ISDEL, "Y"));  
305 - boolQueryBuilder.mustNot(QueryBuilders.termQuery(SearchRequestParams.PROMOTIONINDEX_COMMONBANNER, ""));  
306 -  
307 - searchParam.setFiter(boolQueryBuilder);  
308 - searchParam.setSize(ids.size());  
309 - SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);  
310 - if (searchResult == null || CollectionUtils.isEmpty(searchResult.getResultList())) {  
311 - return Collections.emptyList();  
312 - }  
313 - List<RecommendPromotionAggVO> resultList = new ArrayList<>();  
314 - long total = searchResult.getTotal();  
315 - for (Map<String, Object> esMap : searchResult.getResultList()) {  
316 - if (!CollectionUtils.isEmpty(esMap)) {  
317 - resultList.add(parsePromotionInfos(esMap, total));  
318 - }  
319 - }  
320 - return resultList;  
321 - }  
322 -  
323 - private List<Map<String, Object>> queryListByParam(SearchParam searchParam) {  
324 - List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();  
325 - SearchResult searchResult = searchCommonService.doSearch(INDEX_NAME, searchParam);  
326 - if (searchResult == null || searchResult.getResultList() == null || searchResult.getResultList().isEmpty()) {  
327 - return results;  
328 - }  
329 - for (Map<String, Object> esMap : searchResult.getResultList()) {  
330 - results.add(this.getMap(esMap));  
331 - }  
332 - return results;  
333 - }  
334 -  
335 - private BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) {  
336 - BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();  
337 - if (paramMap.containsKey("ids") && StringUtils.isNotBlank(paramMap.get("ids"))) {  
338 - int[] ids = ConvertUtils.stringToIntArray(paramMap.get("ids"), ",");  
339 - boolFilter.must(QueryBuilders.termsQuery("id", ids));  
340 - }  
341 - return boolFilter;  
342 - }  
343 -  
344 - private Map<String, Object> getMap(Map<String, Object> esMap) {  
345 - Map<String, Object> map = new HashMap<>();  
346 - int id = MapUtils.getIntValue(esMap, "id", 0);  
347 - map.put("is_promotion", id + "");  
348 - map.put("promotion_title", MapUtils.getString(esMap, "title", ""));  
349 - return map;  
350 - }  
351 -  
352 - private RecommendPromotionAggVO parsePromotionInfos(Map<String, Object> esMap, long total) {  
353 - RecommendPromotionAggVO recommendPromotionAggVO = new RecommendPromotionAggVO();  
354 - recommendPromotionAggVO.setPromotion_info_id(MapUtils.getIntValue(esMap, "id", 0));  
355 - recommendPromotionAggVO.setTitle(MapUtils.getString(esMap, "title", ""));  
356 - recommendPromotionAggVO.setCoverimg_url(MapUtils.getString(esMap, "commonBanner", ""));  
357 - recommendPromotionAggVO.setTotal_count(total);  
358 - return recommendPromotionAggVO;  
359 - }  
360 -  
361 - /***********************以下方法用于促销类型->促销名映射的索引****************************************/  
362 } 219 }
  1 +package com.yoho.search.service.base.index.promotion;
  2 +
  3 +import com.yoho.search.base.utils.ProductIndexEsField;
  4 +import com.yoho.search.core.es.model.SearchParam;
  5 +import com.yoho.search.core.es.model.SearchResult;
  6 +import com.yoho.search.models.RecommendPromotionAggVO;
  7 +import com.yoho.search.service.base.SearchCommonService;
  8 +import com.yoho.search.service.base.SearchRequestParams;
  9 +import org.apache.commons.collections.MapUtils;
  10 +import org.elasticsearch.index.query.BoolQueryBuilder;
  11 +import org.elasticsearch.index.query.QueryBuilders;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.stereotype.Service;
  14 +import org.springframework.util.CollectionUtils;
  15 +
  16 +import java.util.ArrayList;
  17 +import java.util.Collections;
  18 +import java.util.List;
  19 +import java.util.Map;
  20 +
  21 +/**
  22 + * Created by wangnan on 2017/5/11
  23 + * 促销索引服务,用于聚合促销
  24 + */
  25 +@Service
  26 +public class PromotionIndexBaseService {
  27 +
  28 + @Autowired
  29 + private SearchCommonService searchCommonService;
  30 +
  31 + public List<RecommendPromotionAggVO> getPromotionInfosByIds(String indexName, List<Integer> ids) {
  32 + SearchParam searchParam = new SearchParam();
  33 + BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  34 + boolQueryBuilder.must(QueryBuilders.termsQuery(ProductIndexEsField.id, ids));
  35 + boolQueryBuilder.must(QueryBuilders.termQuery(ProductIndexEsField.status, 1));
  36 + boolQueryBuilder.mustNot(QueryBuilders.termQuery(SearchRequestParams.PROMOTIONINDEX_ISDEL, "Y"));
  37 + boolQueryBuilder.mustNot(QueryBuilders.termQuery(SearchRequestParams.PROMOTIONINDEX_COMMONBANNER, ""));
  38 +
  39 + searchParam.setFiter(boolQueryBuilder);
  40 + searchParam.setSize(ids.size());
  41 + SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
  42 + if (searchResult == null || CollectionUtils.isEmpty(searchResult.getResultList())) {
  43 + return Collections.emptyList();
  44 + }
  45 + List<RecommendPromotionAggVO> resultList = new ArrayList<>();
  46 + long total = searchResult.getTotal();
  47 + for (Map<String, Object> esMap : searchResult.getResultList()) {
  48 + if (!CollectionUtils.isEmpty(esMap)) {
  49 + resultList.add(parsePromotionInfos(esMap, total));
  50 + }
  51 + }
  52 + return resultList;
  53 + }
  54 +
  55 + private RecommendPromotionAggVO parsePromotionInfos(Map<String, Object> esMap, long total) {
  56 + RecommendPromotionAggVO recommendPromotionAggVO = new RecommendPromotionAggVO();
  57 + recommendPromotionAggVO.setPromotion_info_id(MapUtils.getIntValue(esMap, "id", 0));
  58 + recommendPromotionAggVO.setTitle(MapUtils.getString(esMap, "title", ""));
  59 + recommendPromotionAggVO.setCoverimg_url(MapUtils.getString(esMap, "commonBanner", ""));
  60 + recommendPromotionAggVO.setTotal_count(total);
  61 + return recommendPromotionAggVO;
  62 + }
  63 +}
  1 +package com.yoho.search.service.base.index.promotion;
  2 +
  3 +import com.yoho.search.base.helper.PromotionHelper;
  4 +import com.yoho.search.base.models.PromotionCond;
  5 +import com.yoho.search.base.models.PromotionMatchFactor;
  6 +import org.apache.commons.collections.MapUtils;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.stereotype.Service;
  11 +import org.springframework.util.CollectionUtils;
  12 +
  13 +import java.util.Map;
  14 +
  15 +/**
  16 + * 判断促销和skn是否匹配逻辑
  17 + * @Author: wangnan
  18 + * @Date: Created in 2018/1/29
  19 + */
  20 +@Service
  21 +public class PromotionMatchService {
  22 +
  23 + private static final Logger logger = LoggerFactory.getLogger(PromotionMatchService.class);
  24 +
  25 + @Autowired
  26 + private PromotionHelper promotionHelper;
  27 +
  28 + /**
  29 + * 判断促销和skn是否匹配
  30 + */
  31 + public boolean matchProduct(PromotionCond promotionCond, Map<String, Object> productMap) {
  32 + boolean matchResult;
  33 + if (promotionCond == null) {
  34 + return false;
  35 + }
  36 + if (promotionCond.getAggregator() == PromotionCond.Aggregator.ALL) {
  37 + matchResult = promotionCond.getCondDetailList().stream().allMatch(condDetail -> matchCondDetail(condDetail, productMap));
  38 + } else {
  39 + matchResult = promotionCond.getCondDetailList().stream().anyMatch(condDetail -> matchCondDetail(condDetail, productMap));
  40 + }
  41 + if (matchResult) {
  42 + logger.debug("[func=PromotionCond.matchResult][skn={}][promotionId={}][promotionTitle={}]",
  43 + MapUtils.getIntValue(productMap, "id"), promotionCond.getPromotionId(), promotionCond.getPromotionTitle());
  44 + }
  45 + return matchResult;
  46 + }
  47 +
  48 + /**
  49 + * 匹配matchCondDetail
  50 + */
  51 + private boolean matchCondDetail(PromotionCond.CondDetail condDetail, Map<String, Object> productMap) {
  52 + if (CollectionUtils.isEmpty(condDetail.getCondItemList())) {
  53 + // 当没有具体条件,比如【全场满99元加5元换购YOHO!当期热销新刊】这种时所有productindex都匹配
  54 + return true;
  55 + } else if (condDetail.getAggregator() == PromotionCond.Aggregator.ALL) {
  56 + return condDetail.getCondItemList().stream().allMatch(condItem -> matchProduct(condItem, productMap));
  57 + } else {
  58 + return condDetail.getCondItemList().stream().anyMatch(condItem -> matchProduct(condItem, productMap));
  59 + }
  60 + }
  61 +
  62 + /**
  63 + * 生成一个参数对象去匹配促销,逻辑抽到了core工程的helper中,暂时consumer和service都调用
  64 + */
  65 + private boolean matchProduct(PromotionCond.CondItem condItem, Map<String, Object> productMap) {
  66 + PromotionMatchFactor promotionMatchFactor = new PromotionMatchFactor();
  67 + promotionMatchFactor.setBrandId(MapUtils.getIntValue(productMap, "brand_id"));
  68 + promotionMatchFactor.setSmallSortId(MapUtils.getIntValue(productMap, "smal_sort_id"));
  69 + promotionMatchFactor.setMiddleSortId(MapUtils.getIntValue(productMap, "middle_sort_id"));
  70 + promotionMatchFactor.setMaxSortId(MapUtils.getIntValue(productMap, "max_sort_id"));
  71 + promotionMatchFactor.setIspromotion(MapUtils.getIntValue(productMap, "is_promotion"));
  72 + promotionMatchFactor.setProductskn(MapUtils.getIntValue(productMap, "product_skn"));
  73 + return promotionHelper.matchProduct(condItem, promotionMatchFactor);
  74 + }
  75 +}
  1 +package com.yoho.search.service.base.index.promotion;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.google.common.cache.CacheBuilder;
  5 +import com.google.common.cache.CacheLoader;
  6 +import com.google.common.cache.LoadingCache;
  7 +import com.yoho.search.base.utils.ISearchConstants;
  8 +import com.yoho.search.core.es.model.SearchParam;
  9 +import com.yoho.search.core.es.model.SearchResult;
  10 +import com.yoho.search.service.base.SearchCommonService;
  11 +import org.apache.commons.collections.MapUtils;
  12 +import org.elasticsearch.index.query.QueryBuilders;
  13 +import org.slf4j.Logger;
  14 +import org.slf4j.LoggerFactory;
  15 +import org.springframework.beans.factory.annotation.Autowired;
  16 +import org.springframework.stereotype.Service;
  17 +import org.springframework.util.Assert;
  18 +
  19 +import java.util.HashMap;
  20 +import java.util.Map;
  21 +import java.util.concurrent.TimeUnit;
  22 +
  23 +/**
  24 + * 查询promotiontype索引 用于生成 促销类型->促销名映射,1分钟缓存。
  25 + * @Author: wangnan
  26 + * @Date: Created in 2018/1/29
  27 + */
  28 +@Service
  29 +public class PromotionTypeService {
  30 +
  31 + @Autowired
  32 + private SearchCommonService searchCommonService;
  33 +
  34 + private static final Logger logger = LoggerFactory.getLogger(PromotionTypeService.class);
  35 + private static final String PROMOTION_TYPE_MAP_CACHE_KEY = "promotionTypeMapCacheKey";
  36 + private volatile Map<String, String> promotionTypeMap = new HashMap<>();
  37 + private static final String CACHE_KEY = "CacheKey";
  38 +
  39 + LoadingCache<String, JSONObject> promotionTypeMapCache = CacheBuilder.newBuilder()
  40 + .maximumSize(30000)
  41 + .expireAfterWrite(1, TimeUnit.MINUTES)
  42 + .build(new CacheLoader<String, JSONObject>() {
  43 + public JSONObject load(String key) {
  44 + JSONObject jsonObject = new JSONObject();
  45 + jsonObject.put(PROMOTION_TYPE_MAP_CACHE_KEY, buildPromotionTypeMap());
  46 + return jsonObject;
  47 + }
  48 + });
  49 +
  50 +
  51 + public Map<String, String> getPromotionTypeMap() {
  52 + try {
  53 + JSONObject jsonObject = promotionTypeMapCache.get(CACHE_KEY);
  54 + Assert.isTrue(jsonObject != null, "[cache jsonObject cannot be null]");
  55 + promotionTypeMap = (Map<String, String>) jsonObject.get(PROMOTION_TYPE_MAP_CACHE_KEY);
  56 + } catch (Exception e) {
  57 + logger.error(e.getMessage());
  58 + }
  59 + return promotionTypeMap;
  60 + }
  61 +
  62 + private Map<String, String> buildPromotionTypeMap() {
  63 + try {
  64 + SearchParam searchPromotionParam = buildPromotionTypeSearchParam();
  65 + SearchResult searchPromotionResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PROMOTIONTYPE, searchPromotionParam);
  66 + return this.getPromotionTypeMap(searchPromotionResult);
  67 + } catch (Exception e) {
  68 + logger.error(e.getMessage(), e);
  69 + return new HashMap<>();
  70 + }
  71 + }
  72 +
  73 + private SearchParam buildPromotionTypeSearchParam() {
  74 + SearchParam searchPromotionParam = new SearchParam();
  75 + searchPromotionParam.setSize(10000);
  76 + searchPromotionParam.setQuery(QueryBuilders.matchAllQuery());
  77 + return searchPromotionParam;
  78 + }
  79 +
  80 + private Map<String, String> getPromotionTypeMap(SearchResult searchPromotionResult) {
  81 + if (searchPromotionResult == null || searchPromotionResult.getResultList() == null || searchPromotionResult.getResultList().size() == 0) {
  82 + return new HashMap<>();
  83 + }
  84 + Map<String, String> promotionTypeMap = new HashMap<>();
  85 + for (Map<String, Object> promotionEsSource : searchPromotionResult.getResultList()) {
  86 + promotionTypeMap.put(MapUtils.getString(promotionEsSource, "promotionType", ""), MapUtils.getString(promotionEsSource, "info", ""));
  87 + }
  88 + return promotionTypeMap;
  89 + }
  90 +}