Authored by hugufei

Merge branch 'master' into wn_FreeShippingCouponsPool

1 package com.yoho.search.service.scene; 1 package com.yoho.search.service.scene;
2 2
3 -import java.util.Map;  
4 -import java.util.concurrent.CompletableFuture;  
5 -import java.util.concurrent.ExecutorService;  
6 -import java.util.concurrent.Executors;  
7 -  
8 -import org.apache.commons.lang.StringUtils;  
9 -import org.slf4j.Logger;  
10 -import org.slf4j.LoggerFactory;  
11 -import org.springframework.beans.factory.annotation.Autowired;  
12 -import org.springframework.stereotype.Service;  
13 -  
14 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
15 import com.yoho.search.base.utils.SearchPageIdDefine; 4 import com.yoho.search.base.utils.SearchPageIdDefine;
16 import com.yoho.search.models.SearchApiResult; 5 import com.yoho.search.models.SearchApiResult;
@@ -22,136 +11,137 @@ import com.yoho.search.service.scene.common.AbstractSceneService; @@ -22,136 +11,137 @@ import com.yoho.search.service.scene.common.AbstractSceneService;
22 import com.yoho.search.service.scene.common.CommonSceneProductListService; 11 import com.yoho.search.service.scene.common.CommonSceneProductListService;
23 import com.yoho.search.service.scene.common.SceneSelectionsService; 12 import com.yoho.search.service.scene.common.SceneSelectionsService;
24 import com.yoho.search.service.scene.recall.SortRecallSceneService; 13 import com.yoho.search.service.scene.recall.SortRecallSceneService;
25 -import com.yoho.search.service.service.IAggRecommendService;  
26 -import com.yoho.search.service.service.IProductIndexService; 14 +import org.apache.commons.lang.StringUtils;
  15 +import org.slf4j.Logger;
  16 +import org.slf4j.LoggerFactory;
  17 +import org.springframework.beans.factory.annotation.Autowired;
  18 +import org.springframework.stereotype.Service;
  19 +
  20 +import java.util.Map;
  21 +import java.util.concurrent.CompletableFuture;
  22 +import java.util.concurrent.ExecutorService;
  23 +import java.util.concurrent.Executors;
27 24
28 @Service 25 @Service
29 public class SortSceneService extends AbstractSceneService { 26 public class SortSceneService extends AbstractSceneService {
30 27
31 - private static final Logger logger = LoggerFactory.getLogger(SortSceneService.class); 28 + private static final Logger logger = LoggerFactory.getLogger(SortSceneService.class);
32 29
33 - @Autowired  
34 - private CommonSceneProductListService sceneProductListService;  
35 - @Autowired  
36 - private SceneSelectionsService sceneSelectionsService;  
37 - @Autowired  
38 - private SearchCommonHelper searchCommonHelper;  
39 - @Autowired  
40 - private IProductIndexService productIndexService;  
41 - @Autowired  
42 - private IAggRecommendService aggRecommendService;  
43 - @Autowired  
44 - private SortRecallSceneService sortRecallSceneService;  
45 - @Autowired  
46 - private SearchDynamicConfigService searchDynamicConfigService;  
47 - @Autowired  
48 - private SceneAggregationsHelper sceneAggregationsHelper; 30 + @Autowired
  31 + private CommonSceneProductListService sceneProductListService;
  32 + @Autowired
  33 + private SceneSelectionsService sceneSelectionsService;
  34 + @Autowired
  35 + private SearchCommonHelper searchCommonHelper;
  36 + @Autowired
  37 + private SortRecallSceneService sortRecallSceneService;
  38 + @Autowired
  39 + private SearchDynamicConfigService searchDynamicConfigService;
  40 + @Autowired
  41 + private SceneAggregationsHelper sceneAggregationsHelper;
49 42
50 - @Override  
51 - public String pageId() {  
52 - return SearchPageIdDefine.PAGE_ID_SORT;  
53 - } 43 + @Override
  44 + public String pageId() {
  45 + return SearchPageIdDefine.PAGE_ID_SORT;
  46 + }
54 47
55 - @Override  
56 - public void addParamsToParamMap(Map<String, String> paramMap) {  
57 - super.addDefaultParamsToParamMap(paramMap);  
58 - paramMap.put(SearchRequestParams.PARAM_SEARCH_CONTAIN_GLOBAL, "Y");// 包含全球购  
59 - } 48 + @Override
  49 + public void addParamsToParamMap(Map<String, String> paramMap) {
  50 + super.addDefaultParamsToParamMap(paramMap);
  51 + paramMap.put(SearchRequestParams.PARAM_SEARCH_CONTAIN_GLOBAL, "Y");// 包含全球购
  52 + }
60 53
61 - private ExecutorService executorService = Executors.newFixedThreadPool(100); 54 + private ExecutorService executorService = Executors.newFixedThreadPool(100);
62 55
63 - private boolean checkParam(Map<String, String> paramMap) {  
64 - if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MAXSORT))) {  
65 - return true;  
66 - }  
67 - if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MIDDLESORT))) {  
68 - return true;  
69 - }  
70 - if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SMALLSORT))) {  
71 - return true;  
72 - }  
73 - if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_COMMONSORT))) {  
74 - return true;  
75 - }  
76 - return false;  
77 - } 56 + private boolean checkParam(Map<String, String> paramMap) {
  57 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MAXSORT))) {
  58 + return true;
  59 + }
  60 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MIDDLESORT))) {
  61 + return true;
  62 + }
  63 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SMALLSORT))) {
  64 + return true;
  65 + }
  66 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_COMMONSORT))) {
  67 + return true;
  68 + }
  69 + return false;
  70 + }
78 71
79 - /**  
80 - * @特殊参数:  
81 - * @1、paramNum-获取规则数量的接口  
82 - * @2、firstProductSkn-第一个展示的skn  
83 - */  
84 - @Override  
85 - public SearchApiResult productList(Map<String, String> paramMap) {  
86 - try {  
87 - // 0、必传品类相关参数  
88 - if (!this.checkParam(paramMap)) {  
89 - return new SearchApiResult().setCode(400).setMessage("请传品类相关参数");  
90 - }  
91 - // 1、添加默认参数  
92 - this.addParamsToParamMap(paramMap);  
93 - CompletableFuture<SearchApiResult> productListFuture = null;  
94 - // 2、获取商品列表-是否使用召回策略  
95 - if (goToRecall(paramMap)) {  
96 - productListFuture = CompletableFuture.supplyAsync(() -> sortRecallSceneService.recallProductList(this.newParamMap(paramMap)), executorService);  
97 - } else {  
98 - productListFuture = CompletableFuture.supplyAsync(() -> sceneProductListService.productList(this.newParamMap(paramMap)), executorService);  
99 - }  
100 - // 3、获取聚合结果  
101 - CompletableFuture<SearchApiResult> standardsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggStandard(this.newParamMap(paramMap)),  
102 - executorService);  
103 - CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggCustomizeTag(this.newParamMap(paramMap)),  
104 - executorService);  
105 - CompletableFuture<SearchApiResult> promotionsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggPromotion(this.newParamMap(paramMap)),  
106 - executorService);  
107 - // 4、组合结果  
108 - SearchApiResult productList = productListFuture.get();  
109 - SearchApiResult standards = standardsFuture.get();  
110 - SearchApiResult customizeTags = customizeTagFuture.get();  
111 - SearchApiResult promotions = promotionsFuture.get();  
112 - JSONObject jsonObject = (JSONObject) productList.getData();  
113 - jsonObject.put(STANDARD, standards.getData());  
114 - jsonObject.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());  
115 - jsonObject.put(RECOMMEND_PROMOTION_LIST, sceneAggregationsHelper.subRecommendPromotions(promotions.getData(), this.getPage(paramMap), 1));  
116 - return productList;  
117 - } catch (Exception e) {  
118 - logger.error(e.getMessage(), e);  
119 - return new SearchApiResult().setData(null).setMessage("SortProductList Exception").setCode(500);  
120 - }  
121 - } 72 + /**
  73 + * @特殊参数:
  74 + * @1、paramNum-获取规则数量的接口
  75 + * @2、firstProductSkn-第一个展示的skn
  76 + */
  77 + @Override
  78 + public SearchApiResult productList(Map<String, String> paramMap) {
  79 + try {
  80 + // 0、必传品类相关参数
  81 + if (!this.checkParam(paramMap)) {
  82 + return new SearchApiResult().setCode(400).setMessage("请传品类相关参数");
  83 + }
  84 + // 1、添加默认参数
  85 + this.addParamsToParamMap(paramMap);
  86 + CompletableFuture<SearchApiResult> productListFuture = null;
  87 + // 2、获取商品列表-是否使用召回策略
  88 + if (goToRecall(paramMap)) {
  89 + productListFuture = CompletableFuture.supplyAsync(() -> sortRecallSceneService.recallProductList(this.newParamMap(paramMap)), executorService);
  90 + } else {
  91 + productListFuture = CompletableFuture.supplyAsync(() -> sceneProductListService.productList(this.newParamMap(paramMap)), executorService);
  92 + }
  93 + // 3、获取聚合结果
  94 + CompletableFuture<SearchApiResult> standardsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggStandard(this.newParamMap(paramMap)),
  95 + executorService);
  96 + CompletableFuture<SearchApiResult> customizeTagFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggCustomizeTag(this.newParamMap(paramMap)),
  97 + executorService);
  98 + CompletableFuture<SearchApiResult> promotionsFuture = CompletableFuture.supplyAsync(() -> sceneAggregationsHelper.sceneAggPromotion(this.newParamMap(paramMap)),
  99 + executorService);
  100 + // 4、组合结果
  101 + SearchApiResult productList = productListFuture.get();
  102 + SearchApiResult standards = standardsFuture.get();
  103 + SearchApiResult customizeTags = customizeTagFuture.get();
  104 + SearchApiResult promotions = promotionsFuture.get();
  105 + JSONObject jsonObject = (JSONObject) productList.getData();
  106 + jsonObject.put(STANDARD, standards.getData());
  107 + jsonObject.put(CUSTOMIZE_TAG_LIST, customizeTags.getData());
  108 + jsonObject.put(RECOMMEND_PROMOTION_LIST, sceneAggregationsHelper.subRecommendPromotions(promotions.getData(), this.getPage(paramMap), 1));
  109 + return productList;
  110 + } catch (Exception e) {
  111 + logger.error(e.getMessage(), e);
  112 + return new SearchApiResult().setData(null).setMessage("SortProductList Exception").setCode(500);
  113 + }
  114 + }
122 115
123 - private boolean goToRecall(Map<String, String> paramMap) {  
124 - if (!searchDynamicConfigService.isSortPageRecallOpen()) {  
125 - return false;  
126 - }  
127 - if (!searchCommonHelper.isOrderEmpty(paramMap)) {  
128 - return false;  
129 - }  
130 - // int uid = searchCommonHelper.getUid(paramMap);  
131 - // if (uid <= 0) {  
132 - // return false;  
133 - // }  
134 - // if (!searchDynamicConfigService.isGoToSortRecallUser(uid)) {  
135 - // return false;  
136 - // }  
137 - return true;  
138 - } 116 + private boolean goToRecall(Map<String, String> paramMap) {
  117 + if (!searchDynamicConfigService.isSortPageRecallOpen()) {
  118 + return false;
  119 + }
  120 + if (!searchCommonHelper.isOrderEmpty(paramMap)) {
  121 + return false;
  122 + }
  123 + int uid = searchCommonHelper.getUid(paramMap);
  124 + if (uid <= 0) {
  125 + return false;
  126 + }
  127 + return true;
  128 + }
139 129
140 - @Override  
141 - public SearchApiResult aggregations(Map<String, String> paramMap) {  
142 - try {  
143 - // 0、必传品类相关参数  
144 - if (!this.checkParam(paramMap)) {  
145 - return new SearchApiResult().setCode(400).setMessage("请传品类相关参数");  
146 - }  
147 - // 1、添加默认参数  
148 - this.addParamsToParamMap(paramMap);  
149 - // 2、返回聚合结果  
150 - return sceneSelectionsService.aggregations(paramMap);  
151 - } catch (Exception e) {  
152 - logger.error(e.getMessage(), e);  
153 - return new SearchApiResult().setData(null).setMessage("SortAggregations Exception").setCode(500);  
154 - }  
155 - } 130 + @Override
  131 + public SearchApiResult aggregations(Map<String, String> paramMap) {
  132 + try {
  133 + // 0、必传品类相关参数
  134 + if (!this.checkParam(paramMap)) {
  135 + return new SearchApiResult().setCode(400).setMessage("请传品类相关参数");
  136 + }
  137 + // 1、添加默认参数
  138 + this.addParamsToParamMap(paramMap);
  139 + // 2、返回聚合结果
  140 + return sceneSelectionsService.aggregations(paramMap);
  141 + } catch (Exception e) {
  142 + logger.error(e.getMessage(), e);
  143 + return new SearchApiResult().setData(null).setMessage("SortAggregations Exception").setCode(500);
  144 + }
  145 + }
156 146
157 } 147 }