Authored by wangnan9279

ufo 销售日历 聚合日期数量接口

@@ -29,4 +29,12 @@ public class UfoSaleCalendarListController { @@ -29,4 +29,12 @@ public class UfoSaleCalendarListController {
29 return ufoSaleCalendarService.SaleCalendarProductList(paramMap); 29 return ufoSaleCalendarService.SaleCalendarProductList(paramMap);
30 } 30 }
31 31
  32 +
  33 + @RequestMapping(method = RequestMethod.GET, value = "/ufo/SaleDateCountList")
  34 + @ResponseBody
  35 + public SearchApiResult SaleDateCountList(HttpServletRequest request) {
  36 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  37 + return ufoSaleCalendarService.SaleDateCountList(paramMap);
  38 + }
  39 +
32 } 40 }
@@ -7,6 +7,7 @@ import com.yoho.search.base.utils.ISearchConstants; @@ -7,6 +7,7 @@ import com.yoho.search.base.utils.ISearchConstants;
7 import com.yoho.search.base.utils.UfoProductIndexEsField; 7 import com.yoho.search.base.utils.UfoProductIndexEsField;
8 import com.yoho.search.common.SearchCommonService; 8 import com.yoho.search.common.SearchCommonService;
9 import com.yoho.search.common.SearchRequestParams; 9 import com.yoho.search.common.SearchRequestParams;
  10 +import com.yoho.search.common.utils.UfoImageUrlAssist;
10 import com.yoho.search.core.es.model.SearchParam; 11 import com.yoho.search.core.es.model.SearchParam;
11 import com.yoho.search.core.es.model.SearchResult; 12 import com.yoho.search.core.es.model.SearchResult;
12 import com.yoho.search.models.SearchApiResult; 13 import com.yoho.search.models.SearchApiResult;
@@ -14,6 +15,11 @@ import com.yoho.search.service.index.UfoProductIndexBaseService; @@ -14,6 +15,11 @@ import com.yoho.search.service.index.UfoProductIndexBaseService;
14 import org.apache.commons.collections.MapUtils; 15 import org.apache.commons.collections.MapUtils;
15 import org.elasticsearch.index.query.BoolQueryBuilder; 16 import org.elasticsearch.index.query.BoolQueryBuilder;
16 import org.elasticsearch.index.query.QueryBuilders; 17 import org.elasticsearch.index.query.QueryBuilders;
  18 +import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
  19 +import org.elasticsearch.search.aggregations.Aggregation;
  20 +import org.elasticsearch.search.aggregations.AggregationBuilders;
  21 +import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;
  22 +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
17 import org.elasticsearch.search.sort.SortBuilder; 23 import org.elasticsearch.search.sort.SortBuilder;
18 import org.elasticsearch.search.sort.SortBuilders; 24 import org.elasticsearch.search.sort.SortBuilders;
19 import org.elasticsearch.search.sort.SortOrder; 25 import org.elasticsearch.search.sort.SortOrder;
@@ -42,6 +48,7 @@ public class UfoSaleCalendarService { @@ -42,6 +48,7 @@ public class UfoSaleCalendarService {
42 private UfoProductIndexBaseService ufoProductIndexBaseService; 48 private UfoProductIndexBaseService ufoProductIndexBaseService;
43 49
44 private static final String RETURN_LIST_NAME = "product_list"; 50 private static final String RETURN_LIST_NAME = "product_list";
  51 + private static final String SALE_TIME_AGG_NAME = "sale_time_agg";
45 52
46 @SearchCacheAble(cacheName = "UFO_SALE_CALENDAR_PRODUCT_LIST", cacheInMinute = 3) 53 @SearchCacheAble(cacheName = "UFO_SALE_CALENDAR_PRODUCT_LIST", cacheInMinute = 3)
47 public SearchApiResult SaleCalendarProductList(Map<String, String> paramMap) { 54 public SearchApiResult SaleCalendarProductList(Map<String, String> paramMap) {
@@ -99,7 +106,7 @@ public class UfoSaleCalendarService { @@ -99,7 +106,7 @@ public class UfoSaleCalendarService {
99 Map<String, Object> productMap = new HashMap<String, Object>(); 106 Map<String, Object> productMap = new HashMap<String, Object>();
100 productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0)); 107 productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0));
101 productMap.put("product_name", MapUtils.getString(map, UfoProductIndexEsField.productName, "")); 108 productMap.put("product_name", MapUtils.getString(map, UfoProductIndexEsField.productName, ""));
102 - productMap.put("default_images", MapUtils.getString(map, UfoProductIndexEsField.defaultImages, "")); 109 + productMap.put("default_images", UfoImageUrlAssist.getAllProductPicUrl(MapUtils.getString(map, UfoProductIndexEsField.defaultImages, ""), "goodsimg", "center", "d2hpdGU="));
103 Double price = MapUtils.getDouble(map, UfoProductIndexEsField.price); 110 Double price = MapUtils.getDouble(map, UfoProductIndexEsField.price);
104 productMap.put("price", price == null || price == -1d ? null : price); 111 productMap.put("price", price == null || price == -1d ? null : price);
105 Integer saleTime = MapUtils.getInteger(map, UfoProductIndexEsField.saleTime, 0); 112 Integer saleTime = MapUtils.getInteger(map, UfoProductIndexEsField.saleTime, 0);
@@ -108,4 +115,56 @@ public class UfoSaleCalendarService { @@ -108,4 +115,56 @@ public class UfoSaleCalendarService {
108 productMap.put("sale_time", month + "." + day); 115 productMap.put("sale_time", month + "." + day);
109 return productMap; 116 return productMap;
110 } 117 }
  118 +
  119 + @SearchCacheAble(cacheName = "UFO_SALE_DATE_COUNT_LIST", cacheInMinute = 3)
  120 + public SearchApiResult SaleDateCountList(Map<String, String> paramMap) {
  121 + try {
  122 + SearchParam searchParam = new SearchParam();
  123 + searchParam.setSize(0);
  124 + //1.构造过滤
  125 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  126 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
  127 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
  128 + searchParam.setFiter(boolFilter);
  129 + //2.构造聚合
  130 + List<AbstractAggregationBuilder<?>> list = new ArrayList<>();
  131 + TermsAggregationBuilder firstAggregationBuilder = AggregationBuilders.terms(SALE_TIME_AGG_NAME).field(UfoProductIndexEsField.saleDate).size(200000);
  132 + list.add(firstAggregationBuilder);
  133 + searchParam.setAggregationBuilders(list);
  134 + //3.执行搜索
  135 + final String indexName = ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX;
  136 + SearchResult searchResult = searchCommonService.doSearch(indexName, searchParam);
  137 + if (searchResult == null || searchResult.getAggMaps() == null) {
  138 + return new SearchApiResult().setData(null).setCode(500);
  139 + }
  140 + Map<String, Aggregation> aggregationResult = searchResult.getAggMaps();
  141 + if (!aggregationResult.containsKey(SALE_TIME_AGG_NAME)) {
  142 + return new SearchApiResult().setData(null).setCode(500);
  143 + }
  144 + //4.构造返回数据结构
  145 + List<Map<String, Object>> saleDateCountList = this.getSeriesDateCountList(aggregationResult);
  146 + JSONObject jsonObject = new JSONObject();
  147 + jsonObject.put("sale_date_count_list", saleDateCountList);
  148 + jsonObject.put("total", saleDateCountList.size());
  149 + return new SearchApiResult().setData(jsonObject);
  150 + } catch (Exception e) {
  151 + logger.error(e.getMessage(), e);
  152 + return new SearchApiResult().setData(null).setCode(500);
  153 + }
  154 +
  155 + }
  156 +
  157 + private List<Map<String, Object>> getSeriesDateCountList(Map<String, Aggregation> aggregationResult) {
  158 + List<Map<String, Object>> returnList = new ArrayList<>();
  159 + MultiBucketsAggregation saleDateCountAgg = ((MultiBucketsAggregation) aggregationResult.get(SALE_TIME_AGG_NAME));
  160 + for (MultiBucketsAggregation.Bucket brandIdBucket : saleDateCountAgg.getBuckets()) {
  161 + Map<String, Object> saleDate = new HashMap<>();
  162 + saleDate.put("sale_date", brandIdBucket.getKeyAsString());
  163 + saleDate.put("count", brandIdBucket.getDocCount());
  164 + returnList.add(saleDate);
  165 + }
  166 + return returnList;
  167 + }
  168 +
  169 +
111 } 170 }