Authored by unknown

拆分优惠券接口

  1 +package com.yoho.search.service.restapi.split;
  2 +
  3 +import java.util.Map;
  4 +
  5 +import javax.servlet.http.HttpServletRequest;
  6 +
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Controller;
  9 +import org.springframework.web.bind.annotation.RequestMapping;
  10 +import org.springframework.web.bind.annotation.RequestMethod;
  11 +import org.springframework.web.bind.annotation.ResponseBody;
  12 +
  13 +import com.yoho.search.service.split.ICouponSearchService;
  14 +import com.yoho.search.service.utils.HttpServletRequestUtils;
  15 +import com.yoho.search.service.vo.SearchApiResult;
  16 +
  17 +/**
  18 + * 优惠券列表接口
  19 + *
  20 + * @author gufei.hu
  21 + *
  22 + */
  23 +@Controller
  24 +public class CouponSearchController {
  25 +
  26 + @Autowired
  27 + private ICouponSearchService couponSearchService;
  28 +
  29 + /**
  30 + * 优惠券商品列表
  31 + *
  32 + * @return
  33 + */
  34 + @RequestMapping(method = RequestMethod.GET, value = "/coupon/productList")
  35 + @ResponseBody
  36 + public SearchApiResult couponProductList(HttpServletRequest request) {
  37 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  38 + return couponSearchService.couponProductList(paramMap);
  39 + }
  40 +
  41 + /**
  42 + * 优惠券商品列表筛选项
  43 + *
  44 + * @return
  45 + */
  46 + @RequestMapping(method = RequestMethod.GET, value = "/coupon/aggregations")
  47 + @ResponseBody
  48 + public SearchApiResult couponAggregations(HttpServletRequest request) {
  49 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  50 + return couponSearchService.couponAggregations(paramMap);
  51 + }
  52 +}
@@ -103,14 +103,6 @@ public class BaseService { @@ -103,14 +103,6 @@ public class BaseService {
103 return page; 103 return page;
104 } 104 }
105 105
106 - protected List<AbstractAggregationBuilder> getAggregationBuilders(List<IAggregation> aggregations) {  
107 - List<AbstractAggregationBuilder> builders = new ArrayList<>();  
108 - for (IAggregation aggregation : aggregations) {  
109 - builders.add(aggregation.getBuilder());  
110 - }  
111 - return builders;  
112 - }  
113 -  
114 protected SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception { 106 protected SearchParam buildProductListSearchParam(Map<String, String> paramMap) throws Exception {
115 // 1)验证查询条数 107 // 1)验证查询条数
116 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 108 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  1 +package com.yoho.search.service.split;
  2 +
  3 +import java.util.Map;
  4 +
  5 +import com.yoho.search.service.vo.SearchApiResult;
  6 +
  7 +public interface ICouponSearchService {
  8 +
  9 + /**
  10 + * 优惠券商品列表
  11 + *
  12 + * @param paramMap
  13 + * @return
  14 + */
  15 + public SearchApiResult couponProductList(Map<String, String> paramMap);
  16 +
  17 + /**
  18 + * 优惠券筛选项
  19 + *
  20 + * @param paramMap
  21 + * @return
  22 + */
  23 + public SearchApiResult couponAggregations(Map<String, String> paramMap);
  24 +
  25 +}
@@ -6,6 +6,7 @@ import java.util.Map; @@ -6,6 +6,7 @@ import java.util.Map;
6 6
7 import javax.annotation.PostConstruct; 7 import javax.annotation.PostConstruct;
8 8
  9 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
9 import org.elasticsearch.search.aggregations.Aggregation; 10 import org.elasticsearch.search.aggregations.Aggregation;
10 import org.slf4j.Logger; 11 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 12 import org.slf4j.LoggerFactory;
@@ -36,7 +37,7 @@ import com.yoho.search.service.vo.SearchApiResult; @@ -36,7 +37,7 @@ import com.yoho.search.service.vo.SearchApiResult;
36 37
37 @Service 38 @Service
38 public class SplitSelectionsService extends BaseService { 39 public class SplitSelectionsService extends BaseService {
39 - 40 +
40 private static final Logger logger = LoggerFactory.getLogger(SplitSelectionsService.class); 41 private static final Logger logger = LoggerFactory.getLogger(SplitSelectionsService.class);
41 42
42 @Autowired 43 @Autowired
@@ -68,7 +69,15 @@ public class SplitSelectionsService extends BaseService { @@ -68,7 +69,15 @@ public class SplitSelectionsService extends BaseService {
68 void init() { 69 void init() {
69 aggregationSearchCache = searchCacheFactory.getSelectionsForAppCache(); 70 aggregationSearchCache = searchCacheFactory.getSelectionsForAppCache();
70 } 71 }
71 - 72 +
  73 + private List<AbstractAggregationBuilder> getAggregationBuilders(List<IAggregation> aggregations) {
  74 + List<AbstractAggregationBuilder> builders = new ArrayList<>();
  75 + for (IAggregation aggregation : aggregations) {
  76 + builders.add(aggregation.getBuilder());
  77 + }
  78 + return builders;
  79 + }
  80 +
72 /** 81 /**
73 * @聚合选项: 82 * @聚合选项:
74 * @1、全球购 83 * @1、全球购
@@ -83,12 +92,17 @@ public class SplitSelectionsService extends BaseService { @@ -83,12 +92,17 @@ public class SplitSelectionsService extends BaseService {
83 * @10、尺码 92 * @10、尺码
84 * @11、风格 93 * @11、风格
85 */ 94 */
  95 +
86 public JSONObject getCommonFilters(Map<String, String> paramMap) { 96 public JSONObject getCommonFilters(Map<String, String> paramMap) {
  97 + List<IAggregation> aggregations = this.getCommonAggregations(paramMap);
  98 + return this.getCommonFilters(paramMap, aggregations);
  99 + }
  100 +
  101 + public JSONObject getCommonFilters(Map<String, String> paramMap, List<IAggregation> aggregations) {
87 try { 102 try {
88 // 1)构造搜索参数 103 // 1)构造搜索参数
89 SearchParam searchParam = searchParamHelper.buildDefault(paramMap); 104 SearchParam searchParam = searchParamHelper.buildDefault(paramMap);
90 // 2)构造聚合报文 105 // 2)构造聚合报文
91 - List<IAggregation> aggregations = this.getCommonFilterAggregations(paramMap);  
92 searchParam.setAggregationBuilders(this.getAggregationBuilders(aggregations)); 106 searchParam.setAggregationBuilders(this.getAggregationBuilders(aggregations));
93 // 3)构造其他参数 107 // 3)构造其他参数
94 final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX; 108 final String productIndexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
@@ -121,8 +135,8 @@ public class SplitSelectionsService extends BaseService { @@ -121,8 +135,8 @@ public class SplitSelectionsService extends BaseService {
121 return new JSONObject(); 135 return new JSONObject();
122 } 136 }
123 } 137 }
124 -  
125 - private List<IAggregation> getCommonFilterAggregations(Map<String, String> paramMap) { 138 +
  139 + public List<IAggregation> getCommonAggregations(Map<String, String> paramMap) {
126 List<IAggregation> aggregations = new ArrayList<>(); 140 List<IAggregation> aggregations = new ArrayList<>();
127 // 1)促销 141 // 1)促销
128 aggregations.add(aggregationFactoryService.getPromotionAggregation(1000)); 142 aggregations.add(aggregationFactoryService.getPromotionAggregation(1000));
@@ -148,10 +162,33 @@ public class SplitSelectionsService extends BaseService { @@ -148,10 +162,33 @@ public class SplitSelectionsService extends BaseService {
148 aggregations.add(aggregationFactoryService.getStyleAggregation(paramMap)); 162 aggregations.add(aggregationFactoryService.getStyleAggregation(paramMap));
149 return aggregations; 163 return aggregations;
150 } 164 }
151 - 165 +
  166 + public List<IAggregation> getCouponAggregations(Map<String, String> paramMap) {
  167 + List<IAggregation> aggregations = new ArrayList<>();
  168 + // 1)性别
  169 + aggregations.add(aggregationFactoryService.getGenderNewAggregation());
  170 + // 2)品牌
  171 + aggregations.add(aggregationFactoryService.getBrandAggregation(paramMap));
  172 + // 3)人群
  173 + aggregations.add(aggregationFactoryService.getAgeLevelAggregation());
  174 + // 4)品类
  175 + aggregations.add(aggregationFactoryService.getSortGroupAggregation(paramMap));
  176 + // 5)颜色
  177 + aggregations.add(aggregationFactoryService.getColorAggregation(paramMap));
  178 + // 6)尺码
  179 + aggregations.add(aggregationFactoryService.getSizeAggregation());
  180 + // 7)价格
  181 + aggregations.add(aggregationFactoryService.getPriceAggregation());
  182 + // 8)折扣
  183 + aggregations.add(aggregationFactoryService.getDiscountAggregation());
  184 + // 9)风格
  185 + aggregations.add(aggregationFactoryService.getStyleAggregation(paramMap));
  186 + return aggregations;
  187 + }
  188 +
152 public JSONArray getRecommendBrands(Map<String, String> paramMap) { 189 public JSONArray getRecommendBrands(Map<String, String> paramMap) {
153 SearchApiResult searchApiResult = aggRecommendService.aggRecommendBrand(paramMap); 190 SearchApiResult searchApiResult = aggRecommendService.aggRecommendBrand(paramMap);
154 - return (JSONArray)searchApiResult.getData(); 191 + return (JSONArray) searchApiResult.getData();
155 } 192 }
156 193
157 } 194 }
  1 +package com.yoho.search.service.split.impl;
  2 +
  3 +import java.util.List;
  4 +import java.util.Map;
  5 +
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Service;
  10 +
  11 +import com.alibaba.fastjson.JSONObject;
  12 +import com.yoho.search.core.es.agg.IAggregation;
  13 +import com.yoho.search.service.split.ICouponSearchService;
  14 +import com.yoho.search.service.split.common.SplitProductListService;
  15 +import com.yoho.search.service.split.common.SplitSelectionsService;
  16 +import com.yoho.search.service.utils.SearchApiResultUtils;
  17 +import com.yoho.search.service.vo.SearchApiResult;
  18 +
  19 +@Service
  20 +public class CouponSearchServiceImpl implements ICouponSearchService{
  21 +
  22 + private static final Logger logger = LoggerFactory.getLogger(CouponSearchServiceImpl.class);
  23 +
  24 + @Autowired
  25 + private SplitProductListService splitProductListService;
  26 + @Autowired
  27 + private SplitSelectionsService splitSelectionsService;
  28 +
  29 + @Override
  30 + public SearchApiResult couponProductList(Map<String, String> paramMap) {
  31 + try {
  32 + return splitProductListService.productList(paramMap);
  33 + } catch (Exception e) {
  34 + logger.error("[func=couponProductList][params=" + paramMap + "]", e);
  35 + return SearchApiResultUtils.errorSearchApiResult("couponProductList", paramMap, e);
  36 + }
  37 + }
  38 +
  39 + @Override
  40 + public SearchApiResult couponAggregations(Map<String, String> paramMap) {
  41 + // 1、获取优惠券页面的筛选结果
  42 + List<IAggregation> couponAggregations = splitSelectionsService.getCouponAggregations(paramMap);
  43 + JSONObject couponFilters = splitSelectionsService.getCommonFilters(paramMap,couponAggregations);
  44 + Map<String, Object> result = new JSONObject();
  45 + result.put("filter", couponFilters);
  46 + return new SearchApiResult().setData(result);
  47 + }
  48 +
  49 +}