Authored by wangnan9279

1、抽独立的 即将发售 和 收藏列表 接口 ,2、把列表里面的即将发售时间过滤限制去掉

  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.UfoFavoriteService;
  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/27
  18 + */
  19 +@Controller
  20 +public class UfoFavoriteController {
  21 +
  22 + @Autowired
  23 + private UfoFavoriteService ufoFavoriteService;
  24 +
  25 + @RequestMapping(method = RequestMethod.GET, value = "/ufo/favoriteProductList")
  26 + @ResponseBody
  27 + public SearchApiResult favoriteProductList(HttpServletRequest request) {
  28 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  29 + return ufoFavoriteService.productList(paramMap);
  30 + }
  31 +
  32 +
  33 +}
@@ -2,7 +2,7 @@ package com.yoho.search.restapi.ufo; @@ -2,7 +2,7 @@ package com.yoho.search.restapi.ufo;
2 2
3 import com.yoho.search.common.utils.HttpServletRequestUtils; 3 import com.yoho.search.common.utils.HttpServletRequestUtils;
4 import com.yoho.search.models.SearchApiResult; 4 import com.yoho.search.models.SearchApiResult;
5 -import com.yoho.search.service.scene.ufo.UfoProductListForPlatformService; 5 +import com.yoho.search.service.scene.ufo.UfoPlatformService;
6 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Controller; 7 import org.springframework.stereotype.Controller;
8 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMapping;
@@ -17,15 +17,15 @@ import java.util.Map; @@ -17,15 +17,15 @@ import java.util.Map;
17 * @version 2018/12/20 17 * @version 2018/12/20
18 */ 18 */
19 @Controller 19 @Controller
20 -public class UfoProductListForPlatformController { 20 +public class UfoPlatformController {
21 21
22 @Autowired 22 @Autowired
23 - private UfoProductListForPlatformService ufoProductListForPlatformService; 23 + private UfoPlatformService ufoPlatformService;
24 24
25 @RequestMapping(method = RequestMethod.GET, value = "/ufo/productListForPlatform") 25 @RequestMapping(method = RequestMethod.GET, value = "/ufo/productListForPlatform")
26 @ResponseBody 26 @ResponseBody
27 public SearchApiResult productListForPlatform(HttpServletRequest request) { 27 public SearchApiResult productListForPlatform(HttpServletRequest request) {
28 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request); 28 Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
29 - return ufoProductListForPlatformService.productListForPlatform(paramMap); 29 + return ufoPlatformService.productListForPlatform(paramMap);
30 } 30 }
31 } 31 }
  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.UfoSoonSaleService;
  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/27
  18 + */
  19 +@Controller
  20 +public class UfoSoonSaleController {
  21 +
  22 + @Autowired
  23 + private UfoSoonSaleService ufoSoonSaleService;
  24 +
  25 + @RequestMapping(method = RequestMethod.GET, value = "/ufo/soonSaleProductList")
  26 + @ResponseBody
  27 + public SearchApiResult favoriteProductList(HttpServletRequest request) {
  28 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  29 + return ufoSoonSaleService.productList(paramMap);
  30 + }
  31 +}
@@ -61,7 +61,7 @@ public class UfoSearchQueryHelper extends BaseService { @@ -61,7 +61,7 @@ public class UfoSearchQueryHelper extends BaseService {
61 61
62 public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception { 62 public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
63 BoolQueryBuilder boolFilter = QueryBuilders.boolQuery(); 63 BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
64 - //只id过滤 64 + //只id过滤(抽独立接口了,下次删掉)
65 if ("Y".equals(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_IS_ID_FILTER))) { 65 if ("Y".equals(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_IS_ID_FILTER))) {
66 this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id); 66 this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id);
67 return boolFilter; 67 return boolFilter;
@@ -69,13 +69,11 @@ public class UfoSearchQueryHelper extends BaseService { @@ -69,13 +69,11 @@ public class UfoSearchQueryHelper extends BaseService {
69 //硬过滤 69 //硬过滤
70 boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0)); 70 boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
71 boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1)); 71 boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
72 - //是否是即将发售列表 72 + //是否是即将发售列表(抽独立接口了,下次删掉)
73 long now = DateUtil.getCurrentTimeSecond(); 73 long now = DateUtil.getCurrentTimeSecond();
74 if ("Y".equals(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_IS_SOON_SALE))) { 74 if ("Y".equals(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_IS_SOON_SALE))) {
75 boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).gt(now)); 75 boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).gt(now));
76 paramMap.put("order", "saleTime:asc"); 76 paramMap.put("order", "saleTime:asc");
77 - } else {  
78 - boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).lte(now));  
79 } 77 }
80 //参数过滤 78 //参数过滤
81 this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id); 79 this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id);
  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.ConvertUtils;
  6 +import com.yoho.search.base.utils.ISearchConstants;
  7 +import com.yoho.search.common.SearchCommonService;
  8 +import com.yoho.search.common.SearchRequestParams;
  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.index.query.BoolQueryBuilder;
  16 +import org.elasticsearch.index.query.QueryBuilders;
  17 +import org.slf4j.Logger;
  18 +import org.slf4j.LoggerFactory;
  19 +import org.springframework.beans.factory.annotation.Autowired;
  20 +import org.springframework.stereotype.Service;
  21 +
  22 +import java.util.ArrayList;
  23 +import java.util.Arrays;
  24 +import java.util.List;
  25 +import java.util.Map;
  26 +import java.util.stream.Collectors;
  27 +
  28 +/**
  29 + * @author wangnan
  30 + * @version 2018/12/27
  31 + */
  32 +@Service
  33 +public class UfoFavoriteService {
  34 +
  35 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  36 +
  37 + @Autowired
  38 + private SearchCommonService searchCommonService;
  39 + @Autowired
  40 + private UfoProductIndexBaseService ufoProductIndexBaseService;
  41 + @Autowired
  42 + private UfoSearchQueryHelper ufoSearchQueryHelper;
  43 +
  44 + private static final String RETURN_LIST_NAME = "product_list";
  45 +
  46 + @SearchCacheAble(cacheName = "UFO_FAVORITE_LIST", cacheInMinute = 1)
  47 + public SearchApiResult productList(Map<String, String> paramMap) {
  48 + try {
  49 + // 1、参数校验
  50 + if (StringUtils.isBlank(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID))) {
  51 + return new SearchApiResult().setCode(400).setMessage("id参数必传");
  52 + }
  53 + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  54 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  55 + if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
  56 + return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
  57 + }
  58 + if (pageSize > 100) {
  59 + paramMap.put("viewNum", "100");
  60 + }
  61 + // 2. 构建SearchParam
  62 + SearchParam searchParam = new SearchParam();
  63 + BoolQueryBuilder boolFilter = constructFilterBuilder(paramMap);
  64 + searchParam.setFiter(boolFilter);
  65 + searchParam.setAggregationBuilders(null);
  66 + searchParam.setSize(pageSize);
  67 + searchParam.setOffset((page - 1) * pageSize);
  68 + // 设置返回的结果
  69 + searchParam.setIncludeFields(ufoProductIndexBaseService.getUfoProductIndexIncludeFields());
  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 + JSONObject dataMap = new JSONObject();
  75 + dataMap.put("total", searchResult.getTotal());
  76 + dataMap.put("page", searchResult.getPage());
  77 + dataMap.put("page_size", pageSize);
  78 + dataMap.put("page_total", searchResult.getTotalPage());
  79 + Map<String, Map<String, Object>> productReturnInfoMap = returnInfoList.stream().collect(Collectors.toMap(p -> p.get("id").toString(), p -> p));
  80 + List<Map<String, Object>> productReturnInfoListSorted = new ArrayList<>(productReturnInfoMap.size());
  81 + List<String> idList = Arrays.asList(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID).split(","));
  82 + for (String id : idList) {
  83 + Map<String, Object> sknInfo = productReturnInfoMap.get(id);
  84 + if (sknInfo != null) {
  85 + productReturnInfoListSorted.add(sknInfo);
  86 + }
  87 + }
  88 + dataMap.put(RETURN_LIST_NAME, productReturnInfoListSorted);
  89 + return new SearchApiResult().setData(dataMap);
  90 + } catch (Exception e) {
  91 + logger.error(e.getMessage(), e);
  92 + return new SearchApiResult().setData(null).setCode(500);
  93 + }
  94 + }
  95 +
  96 + public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) throws Exception {
  97 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  98 + List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID), ",");
  99 + if (values == null || values.isEmpty()) {
  100 + return boolFilter;
  101 + }
  102 + boolFilter.must(QueryBuilders.termsQuery(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, values));
  103 + return boolFilter;
  104 + }
  105 +
  106 +}
@@ -27,7 +27,7 @@ import java.util.Map; @@ -27,7 +27,7 @@ import java.util.Map;
27 * @version 2018/12/20 27 * @version 2018/12/20
28 */ 28 */
29 @Service 29 @Service
30 -public class UfoProductListForPlatformService { 30 +public class UfoPlatformService {
31 31
32 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 32 private final Logger logger = LoggerFactory.getLogger(this.getClass());
33 33
  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.index.query.BoolQueryBuilder;
  16 +import org.elasticsearch.index.query.QueryBuilders;
  17 +import org.elasticsearch.search.sort.SortBuilder;
  18 +import org.elasticsearch.search.sort.SortBuilders;
  19 +import org.elasticsearch.search.sort.SortOrder;
  20 +import org.slf4j.Logger;
  21 +import org.slf4j.LoggerFactory;
  22 +import org.springframework.beans.factory.annotation.Autowired;
  23 +import org.springframework.stereotype.Component;
  24 +
  25 +import java.util.ArrayList;
  26 +import java.util.List;
  27 +import java.util.Map;
  28 +
  29 +/**
  30 + * @author wangnan
  31 + * @version 2018/12/27
  32 + */
  33 +@Component
  34 +public class UfoSoonSaleService {
  35 +
  36 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  37 +
  38 + @Autowired
  39 + private SearchCommonService searchCommonService;
  40 + @Autowired
  41 + private UfoProductIndexBaseService ufoProductIndexBaseService;
  42 + @Autowired
  43 + private UfoSearchQueryHelper ufoSearchQueryHelper;
  44 +
  45 + private static final String RETURN_LIST_NAME = "product_list";
  46 +
  47 + @SearchCacheAble(cacheName = "UFO_SOON_SALE_LIST", cacheInMinute = 3)
  48 + public SearchApiResult productList(Map<String, String> paramMap) {
  49 + try {
  50 + // 1、参数校验
  51 + int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
  52 + int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
  53 + if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
  54 + return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
  55 + }
  56 + if (pageSize > 100) {
  57 + paramMap.put("viewNum", "100");
  58 + }
  59 + // 2. 构建SearchParam
  60 + SearchParam searchParam = new SearchParam();
  61 + BoolQueryBuilder boolFilter = constructFilterBuilder(paramMap, null);
  62 + searchParam.setFiter(boolFilter);
  63 + searchParam.setAggregationBuilders(null);
  64 + searchParam.setSize(pageSize);
  65 + searchParam.setOffset((page - 1) * pageSize);
  66 + // 设置排序字段
  67 + List<SortBuilder<?>> sortBuilders = new ArrayList<>();
  68 + sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.saleTime).order(SortOrder.ASC));
  69 + sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.orderBy).order(SortOrder.DESC));
  70 + sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.id).order(SortOrder.DESC));
  71 + searchParam.setSortBuilders(ufoSearchQueryHelper.buildSortList(paramMap));
  72 + // 设置返回的结果
  73 + List<String> includeFields = ufoProductIndexBaseService.getUfoProductIndexIncludeFields();
  74 + searchParam.setIncludeFields(includeFields);
  75 + // 执行搜索
  76 + SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
  77 + // 构造返回结果
  78 + List<Map<String, Object>> returnInfoList = ufoProductIndexBaseService.buildProductReturnInfoList(searchResult.getResultList());
  79 + JSONObject dataMap = new JSONObject();
  80 + dataMap.put("total", searchResult.getTotal());
  81 + dataMap.put("page", searchResult.getPage());
  82 + dataMap.put("page_size", pageSize);
  83 + dataMap.put("page_total", searchResult.getTotalPage());
  84 + dataMap.put(RETURN_LIST_NAME, returnInfoList);
  85 + return new SearchApiResult().setData(dataMap);
  86 + } catch (Exception e) {
  87 + logger.error(e.getMessage(), e);
  88 + return new SearchApiResult().setData(null).setCode(500);
  89 + }
  90 + }
  91 +
  92 + public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
  93 + BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
  94 + //硬过滤
  95 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
  96 + boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
  97 + //发售列表
  98 + boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).gt(DateUtil.getCurrentTimeSecond()));
  99 + return boolFilter;
  100 + }
  101 +}