Authored by wangnan9279

ufo 热销列表和发售日历列表

  1 +package com.yoho.search.restapi.ufo;
  2 +
  3 +import com.yoho.search.common.utils.HttpServletRequestUtils;
  4 +import com.yoho.search.models.SearchApiResult;
  5 +import com.yoho.search.service.scene.ufo.UfoHotSaleListService;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.stereotype.Controller;
  8 +import org.springframework.web.bind.annotation.RequestMapping;
  9 +import org.springframework.web.bind.annotation.RequestMethod;
  10 +import org.springframework.web.bind.annotation.ResponseBody;
  11 +
  12 +import javax.servlet.http.HttpServletRequest;
  13 +import java.util.Map;
  14 +
  15 +/**
  16 + * @author wangnan
  17 + * @version 2018/12/18
  18 + */
  19 +@Controller
  20 +public class UfoHotSaleListController {
  21 +
  22 + @Autowired
  23 + private UfoHotSaleListService ufoHotSaleListService;
  24 +
  25 + @RequestMapping(method = RequestMethod.GET, value = "/ufo/hotSaleProductList")
  26 + @ResponseBody
  27 + public SearchApiResult hotSaleProductList(HttpServletRequest request) {
  28 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  29 + return ufoHotSaleListService.hotSaleProductList(paramMap);
  30 + }
  31 +
  32 +}
  1 +package com.yoho.search.restapi.ufo;
  2 +
  3 +import com.yoho.search.common.utils.HttpServletRequestUtils;
  4 +import com.yoho.search.models.SearchApiResult;
  5 +import com.yoho.search.service.scene.ufo.UfoSaleCalendarService;
  6 +import org.springframework.beans.factory.annotation.Autowired;
  7 +import org.springframework.stereotype.Controller;
  8 +import org.springframework.web.bind.annotation.RequestMapping;
  9 +import org.springframework.web.bind.annotation.RequestMethod;
  10 +import org.springframework.web.bind.annotation.ResponseBody;
  11 +
  12 +import javax.servlet.http.HttpServletRequest;
  13 +import java.util.Map;
  14 +
  15 +/**
  16 + * @author wangnan
  17 + * @version 2018/12/18
  18 + */
  19 +@Controller
  20 +public class UfoSaleCalendarListController {
  21 +
  22 + @Autowired
  23 + private UfoSaleCalendarService ufoSaleCalendarService;
  24 +
  25 + @RequestMapping(method = RequestMethod.GET, value = "/ufo/saleCalendarProductList")
  26 + @ResponseBody
  27 + public SearchApiResult selectionList(HttpServletRequest request) {
  28 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  29 + return ufoSaleCalendarService.SaleCalendarProductList(paramMap);
  30 + }
  31 +
  32 +}
@@ -95,6 +95,28 @@ public class UfoSearchQueryHelper extends BaseService { @@ -95,6 +95,28 @@ public class UfoSearchQueryHelper extends BaseService {
95 return boolFilter; 95 return boolFilter;
96 } 96 }
97 97
  98 + public BoolQueryBuilder constructSaleCalendarFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
  99 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  100 + //硬过滤
  101 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
  102 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
  103 + //发售列表
  104 + long monthBeginTime = DateUtil.getCurrentMonthStartTime();
  105 + long monthEndTime = DateUtil.getCurrentMonthEndTime();
  106 + boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).gte(monthBeginTime));
  107 + boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).lte(monthEndTime));
  108 + return boolFilter;
  109 + }
  110 +
  111 + public BoolQueryBuilder constructHotSaleFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
  112 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  113 + //硬过滤
  114 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
  115 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
  116 + boolFilter.mustNot(QueryBuilders.termQuery(UfoProductIndexEsField.price, -1));
  117 + return boolFilter;
  118 + }
  119 +
98 public BoolQueryBuilder constructFilterBuilderForPlatform(Map<String, String> paramMap, String filterParamName) throws Exception { 120 public BoolQueryBuilder constructFilterBuilderForPlatform(Map<String, String> paramMap, String filterParamName) throws Exception {
99 BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); 121 BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
100 this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id); 122 this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id);
1 package com.yoho.search.service.index; 1 package com.yoho.search.service.index;
2 2
  3 +import com.yoho.search.base.utils.DateUtil;
3 import com.yoho.search.base.utils.UfoProductIndexEsField; 4 import com.yoho.search.base.utils.UfoProductIndexEsField;
4 import org.apache.commons.collections.MapUtils; 5 import org.apache.commons.collections.MapUtils;
5 -import org.slf4j.Logger;  
6 -import org.slf4j.LoggerFactory;  
7 import org.springframework.stereotype.Service; 6 import org.springframework.stereotype.Service;
8 7
9 import javax.annotation.PostConstruct; 8 import javax.annotation.PostConstruct;
@@ -19,11 +18,10 @@ import java.util.Map; @@ -19,11 +18,10 @@ import java.util.Map;
19 @Service 18 @Service
20 public class UfoProductIndexBaseService { 19 public class UfoProductIndexBaseService {
21 20
22 - private final Logger logger = LoggerFactory.getLogger(this.getClass());  
23 -  
24 // 获取从source中返回的字段定义,用以节省带宽 21 // 获取从source中返回的字段定义,用以节省带宽
25 private List<String> ufoProductIndexIncludeFields = new ArrayList<String>(); 22 private List<String> ufoProductIndexIncludeFields = new ArrayList<String>();
26 private List<String> ufoProductIndexIncludeFieldsPlatform = new ArrayList<String>(); 23 private List<String> ufoProductIndexIncludeFieldsPlatform = new ArrayList<String>();
  24 + private List<String> ufoSaleCalendarProductListIncludeFields = new ArrayList<String>();
27 25
28 26
29 @PostConstruct 27 @PostConstruct
@@ -37,6 +35,12 @@ public class UfoProductIndexBaseService { @@ -37,6 +35,12 @@ public class UfoProductIndexBaseService {
37 ufoProductIndexIncludeFieldsPlatform.add(UfoProductIndexEsField.productName); 35 ufoProductIndexIncludeFieldsPlatform.add(UfoProductIndexEsField.productName);
38 ufoProductIndexIncludeFieldsPlatform.add(UfoProductIndexEsField.shelveStatus); 36 ufoProductIndexIncludeFieldsPlatform.add(UfoProductIndexEsField.shelveStatus);
39 ufoProductIndexIncludeFieldsPlatform.add(UfoProductIndexEsField.delStatus); 37 ufoProductIndexIncludeFieldsPlatform.add(UfoProductIndexEsField.delStatus);
  38 +
  39 + ufoSaleCalendarProductListIncludeFields.add(UfoProductIndexEsField.id);
  40 + ufoSaleCalendarProductListIncludeFields.add(UfoProductIndexEsField.productName);
  41 + ufoSaleCalendarProductListIncludeFields.add(UfoProductIndexEsField.defaultImages);
  42 + ufoSaleCalendarProductListIncludeFields.add(UfoProductIndexEsField.price);
  43 + ufoSaleCalendarProductListIncludeFields.add(UfoProductIndexEsField.saleTime);
40 } 44 }
41 45
42 public List<String> getUfoProductIndexIncludeFields() { 46 public List<String> getUfoProductIndexIncludeFields() {
@@ -51,6 +55,12 @@ public class UfoProductIndexBaseService { @@ -51,6 +55,12 @@ public class UfoProductIndexBaseService {
51 return results; 55 return results;
52 } 56 }
53 57
  58 + public List<String> getUfoSaleCalendarProductListIncludeFields() {
  59 + List<String> results = new ArrayList<String>();
  60 + results.addAll(ufoSaleCalendarProductListIncludeFields);
  61 + return results;
  62 + }
  63 +
54 public List<Map<String, Object>> buildProductReturnInfoList(List<Map<String, Object>> productEsSourceList) { 64 public List<Map<String, Object>> buildProductReturnInfoList(List<Map<String, Object>> productEsSourceList) {
55 List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); 65 List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
56 for (Map<String, Object> productEsSource : productEsSourceList) { 66 for (Map<String, Object> productEsSource : productEsSourceList) {
@@ -67,6 +77,14 @@ public class UfoProductIndexBaseService { @@ -67,6 +77,14 @@ public class UfoProductIndexBaseService {
67 return results; 77 return results;
68 } 78 }
69 79
  80 + public List<Map<String, Object>> buildSaleCalendarProductReturnInfoList(List<Map<String, Object>> productEsSourceList) {
  81 + List<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
  82 + for (Map<String, Object> productEsSource : productEsSourceList) {
  83 + results.add(this.getProductMapFromEsSourceSaleCalendar(productEsSource));
  84 + }
  85 + return results;
  86 + }
  87 +
70 public Map<String, Object> getProductMapFromEsSource(Map<String, Object> map) { 88 public Map<String, Object> getProductMapFromEsSource(Map<String, Object> map) {
71 Map<String, Object> productMap = new HashMap<String, Object>(); 89 Map<String, Object> productMap = new HashMap<String, Object>();
72 productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0)); 90 productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0));
@@ -86,5 +104,20 @@ public class UfoProductIndexBaseService { @@ -86,5 +104,20 @@ public class UfoProductIndexBaseService {
86 return productMap; 104 return productMap;
87 } 105 }
88 106
  107 + public Map<String, Object> getProductMapFromEsSourceSaleCalendar(Map<String, Object> map) {
  108 + Map<String, Object> productMap = new HashMap<String, Object>();
  109 + productMap.put("id", MapUtils.getIntValue(map, UfoProductIndexEsField.id, 0));
  110 + productMap.put("product_name", MapUtils.getString(map, UfoProductIndexEsField.productName, ""));
  111 + productMap.put("default_images", MapUtils.getString(map, UfoProductIndexEsField.defaultImages, ""));
  112 + Double price = MapUtils.getDouble(map, UfoProductIndexEsField.price);
  113 + productMap.put("price", price == null || price == -1d ? null : price);
  114 + Integer saleTime = MapUtils.getInteger(map, UfoProductIndexEsField.saleTime, 0);
  115 + String month = DateUtil.TimeStamp2DateWithFormat(Long.valueOf(saleTime), "MM");
  116 + String day = DateUtil.TimeStamp2DateWithFormat(Long.valueOf(saleTime), "dd");
  117 + productMap.put("sale_time_month", month);
  118 + productMap.put("sale_time_day", day);
  119 + return productMap;
  120 + }
  121 +
89 122
90 } 123 }
  1 +package com.yoho.search.service.scene.ufo;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.aop.cache.SearchCacheAble;
  5 +import com.yoho.search.base.utils.ISearchConstants;
  6 +import com.yoho.search.base.utils.UfoProductIndexEsField;
  7 +import com.yoho.search.common.SearchCommonService;
  8 +import com.yoho.search.core.es.model.SearchParam;
  9 +import com.yoho.search.core.es.model.SearchResult;
  10 +import com.yoho.search.models.SearchApiResult;
  11 +import com.yoho.search.service.helper.UfoSearchQueryHelper;
  12 +import com.yoho.search.service.index.UfoProductIndexBaseService;
  13 +import org.apache.commons.lang.StringUtils;
  14 +import org.elasticsearch.index.query.BoolQueryBuilder;
  15 +import org.elasticsearch.search.sort.SortBuilder;
  16 +import org.elasticsearch.search.sort.SortBuilders;
  17 +import org.elasticsearch.search.sort.SortOrder;
  18 +import org.slf4j.Logger;
  19 +import org.slf4j.LoggerFactory;
  20 +import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.stereotype.Service;
  22 +
  23 +import java.util.ArrayList;
  24 +import java.util.List;
  25 +import java.util.Map;
  26 +
  27 +/**
  28 + * @author wangnan
  29 + * @version 2018/12/18
  30 + */
  31 +@Service
  32 +public class UfoHotSaleListService {
  33 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  34 +
  35 + @Autowired
  36 + private SearchCommonService searchCommonService;
  37 + @Autowired
  38 + private UfoProductIndexBaseService ufoProductIndexBaseService;
  39 + @Autowired
  40 + private UfoSearchQueryHelper ufoSearchQueryHelper;
  41 +
  42 + private static final String RETURN_LIST_NAME = "product_list";
  43 +
  44 + @SearchCacheAble(cacheName = "UFO_HOT_SALE_LIST", cacheInMinute = 10)
  45 + public SearchApiResult hotSaleProductList(Map<String, String> paramMap) {
  46 + try {
  47 + // 1、参数校验
  48 + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  49 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  50 + if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
  51 + return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
  52 + }
  53 + if (pageSize > 100) {
  54 + paramMap.put("viewNum", "100");
  55 + }
  56 + // 2. 构建SearchParam
  57 + SearchParam searchParam = new SearchParam();
  58 + searchParam.setAggregationBuilders(null);
  59 + searchParam.setSize(pageSize);
  60 + searchParam.setOffset((page - 1) * pageSize);
  61 + BoolQueryBuilder boolFilter = ufoSearchQueryHelper.constructHotSaleFilterBuilder(paramMap, null);
  62 + searchParam.setFiter(boolFilter);
  63 + // 设置排序字段
  64 + List<SortBuilder<?>> sortBuilder = new ArrayList<SortBuilder<?>>();
  65 + sortBuilder.add(SortBuilders.fieldSort(UfoProductIndexEsField.salesNumThirtyDay).order(SortOrder.DESC));
  66 + searchParam.setSortBuilders(sortBuilder);
  67 + // 设置返回的结果
  68 + List<String> includeFields = ufoProductIndexBaseService.getUfoProductIndexIncludeFields();
  69 + searchParam.setIncludeFields(includeFields);
  70 + // 执行搜索
  71 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
  72 + // 构造返回结果
  73 + List<Map<String, Object>> returnInfoList = ufoProductIndexBaseService.buildProductReturnInfoList(searchResult.getResultList());
  74 + this.addNumberTag(returnInfoList, page, pageSize);
  75 + JSONObject dataMap = new JSONObject();
  76 + dataMap.put("total", searchResult.getTotal());
  77 + dataMap.put("page", searchResult.getPage());
  78 + dataMap.put("page_size", pageSize);
  79 + dataMap.put("page_total", searchResult.getTotalPage());
  80 + dataMap.put(RETURN_LIST_NAME, returnInfoList);
  81 + return new SearchApiResult().setData(dataMap);
  82 + } catch (Exception e) {
  83 + logger.error(e.getMessage(), e);
  84 + return new SearchApiResult().setData(null).setCode(500);
  85 + }
  86 + }
  87 +
  88 + private void addNumberTag(List<Map<String, Object>> returnInfoList, int page, int pageSize) {
  89 + Integer start = (page - 1) * pageSize;
  90 + for (Map<String, Object> map : returnInfoList) {
  91 + start++;
  92 + map.put("serial_number", start.toString());
  93 + }
  94 + }
  95 +}
  1 +package com.yoho.search.service.scene.ufo;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.aop.cache.SearchCacheAble;
  5 +import com.yoho.search.base.utils.DateUtil;
  6 +import com.yoho.search.base.utils.ISearchConstants;
  7 +import com.yoho.search.base.utils.UfoProductIndexEsField;
  8 +import com.yoho.search.common.SearchCommonService;
  9 +import com.yoho.search.core.es.model.SearchParam;
  10 +import com.yoho.search.core.es.model.SearchResult;
  11 +import com.yoho.search.models.SearchApiResult;
  12 +import com.yoho.search.service.helper.UfoSearchQueryHelper;
  13 +import com.yoho.search.service.index.UfoProductIndexBaseService;
  14 +import org.apache.commons.lang.StringUtils;
  15 +import org.elasticsearch.search.sort.SortBuilder;
  16 +import org.elasticsearch.search.sort.SortBuilders;
  17 +import org.elasticsearch.search.sort.SortOrder;
  18 +import org.slf4j.Logger;
  19 +import org.slf4j.LoggerFactory;
  20 +import org.springframework.beans.factory.annotation.Autowired;
  21 +import org.springframework.stereotype.Service;
  22 +
  23 +import java.util.ArrayList;
  24 +import java.util.List;
  25 +import java.util.Map;
  26 +
  27 +/**
  28 + * @author wangnan
  29 + * @version 2018/12/18
  30 + */
  31 +@Service
  32 +public class UfoSaleCalendarService {
  33 +
  34 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  35 +
  36 + @Autowired
  37 + private SearchCommonService searchCommonService;
  38 + @Autowired
  39 + private UfoProductIndexBaseService ufoProductIndexBaseService;
  40 + @Autowired
  41 + private UfoSearchQueryHelper ufoSearchQueryHelper;
  42 +
  43 + private static final String RETURN_LIST_NAME = "product_list";
  44 +
  45 + @SearchCacheAble(cacheName = "UFO_SALE_CALENDAR_PRODUCT_LIST", cacheInMinute = 10)
  46 + public SearchApiResult SaleCalendarProductList(Map<String, String> paramMap) {
  47 + try {
  48 + // 参数校验
  49 + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  50 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  51 + if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
  52 + return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
  53 + }
  54 + if (pageSize > 100) {
  55 + paramMap.put("viewNum", "100");
  56 + }
  57 + // 构建SearchParam
  58 + SearchParam searchParam = new SearchParam();
  59 + searchParam.setSize(pageSize);
  60 + searchParam.setOffset((page - 1) * pageSize);
  61 + searchParam.setAggregationBuilders(null);
  62 + // 过滤
  63 + searchParam.setFiter(ufoSearchQueryHelper.constructSaleCalendarFilterBuilder(paramMap, null));
  64 + // 设置排序字段
  65 + List<SortBuilder<?>> sortBuilder = new ArrayList<SortBuilder<?>>();
  66 + sortBuilder.add(SortBuilders.fieldSort(UfoProductIndexEsField.saleTime).order(SortOrder.ASC));
  67 + searchParam.setSortBuilders(sortBuilder);
  68 + // 设置返回的结果
  69 + searchParam.setIncludeFields(ufoProductIndexBaseService.getUfoSaleCalendarProductListIncludeFields());
  70 + // 执行搜索
  71 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
  72 + // 构造返回结果
  73 + List<Map<String, Object>> returnInfoList = ufoProductIndexBaseService.buildSaleCalendarProductReturnInfoList(searchResult.getResultList());
  74 + JSONObject dataMap = new JSONObject();
  75 + dataMap.put("month", getThisMonth());
  76 + dataMap.put("total", searchResult.getTotal());
  77 + dataMap.put("page", searchResult.getPage());
  78 + dataMap.put("page_size", pageSize);
  79 + dataMap.put("page_total", searchResult.getTotalPage());
  80 + dataMap.put(RETURN_LIST_NAME, returnInfoList);
  81 + return new SearchApiResult().setData(dataMap);
  82 + } catch (Exception e) {
  83 + logger.error(e.getMessage(), e);
  84 + return new SearchApiResult().setData(null).setCode(500);
  85 + }
  86 + }
  87 +
  88 + private String getThisMonth() {
  89 + long monthBeginTime = DateUtil.getCurrentMonthStartTime();
  90 + String month = DateUtil.TimeStamp2DateWithFormat(monthBeginTime, "MM");
  91 + if (month.charAt(0) == '0') {
  92 + return month.charAt(1) + "";
  93 + }
  94 + return month;
  95 + }
  96 +}