Authored by wangnan9279

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

package com.yoho.search.restapi.ufo;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.ufo.UfoFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @author wangnan
* @version 2018/12/27
*/
@Controller
public class UfoFavoriteController {
@Autowired
private UfoFavoriteService ufoFavoriteService;
@RequestMapping(method = RequestMethod.GET, value = "/ufo/favoriteProductList")
@ResponseBody
public SearchApiResult favoriteProductList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return ufoFavoriteService.productList(paramMap);
}
}
... ...
... ... @@ -2,7 +2,7 @@ package com.yoho.search.restapi.ufo;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.ufo.UfoProductListForPlatformService;
import com.yoho.search.service.scene.ufo.UfoPlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
... ... @@ -17,15 +17,15 @@ import java.util.Map;
* @version 2018/12/20
*/
@Controller
public class UfoProductListForPlatformController {
public class UfoPlatformController {
@Autowired
private UfoProductListForPlatformService ufoProductListForPlatformService;
private UfoPlatformService ufoPlatformService;
@RequestMapping(method = RequestMethod.GET, value = "/ufo/productListForPlatform")
@ResponseBody
public SearchApiResult productListForPlatform(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return ufoProductListForPlatformService.productListForPlatform(paramMap);
return ufoPlatformService.productListForPlatform(paramMap);
}
}
... ...
package com.yoho.search.restapi.ufo;
import com.yoho.search.common.utils.HttpServletRequestUtils;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.scene.ufo.UfoSoonSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/**
* @author wangnan
* @version 2018/12/27
*/
@Controller
public class UfoSoonSaleController {
@Autowired
private UfoSoonSaleService ufoSoonSaleService;
@RequestMapping(method = RequestMethod.GET, value = "/ufo/soonSaleProductList")
@ResponseBody
public SearchApiResult favoriteProductList(HttpServletRequest request) {
Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
return ufoSoonSaleService.productList(paramMap);
}
}
... ...
... ... @@ -61,7 +61,7 @@ public class UfoSearchQueryHelper extends BaseService {
public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
//只id过滤
//只id过滤(抽独立接口了,下次删掉)
if ("Y".equals(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_IS_ID_FILTER))) {
this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id);
return boolFilter;
... ... @@ -69,13 +69,11 @@ public class UfoSearchQueryHelper extends BaseService {
//硬过滤
boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
//是否是即将发售列表
//是否是即将发售列表(抽独立接口了,下次删掉)
long now = DateUtil.getCurrentTimeSecond();
if ("Y".equals(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_IS_SOON_SALE))) {
boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).gt(now));
paramMap.put("order", "saleTime:asc");
} else {
boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).lte(now));
}
//参数过滤
this.addMustIntTermsQuery(boolFilter, paramMap, filterParamName, SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, UfoProductIndexEsField.id);
... ...
package com.yoho.search.service.scene.ufo;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.common.SearchRequestParams;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.helper.UfoSearchQueryHelper;
import com.yoho.search.service.index.UfoProductIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author wangnan
* @version 2018/12/27
*/
@Service
public class UfoFavoriteService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private UfoProductIndexBaseService ufoProductIndexBaseService;
@Autowired
private UfoSearchQueryHelper ufoSearchQueryHelper;
private static final String RETURN_LIST_NAME = "product_list";
@SearchCacheAble(cacheName = "UFO_FAVORITE_LIST", cacheInMinute = 1)
public SearchApiResult productList(Map<String, String> paramMap) {
try {
// 1、参数校验
if (StringUtils.isBlank(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID))) {
return new SearchApiResult().setCode(400).setMessage("id参数必传");
}
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
}
if (pageSize > 100) {
paramMap.put("viewNum", "100");
}
// 2. 构建SearchParam
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = constructFilterBuilder(paramMap);
searchParam.setFiter(boolFilter);
searchParam.setAggregationBuilders(null);
searchParam.setSize(pageSize);
searchParam.setOffset((page - 1) * pageSize);
// 设置返回的结果
searchParam.setIncludeFields(ufoProductIndexBaseService.getUfoProductIndexIncludeFields());
// 执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
// 构造返回结果
List<Map<String, Object>> returnInfoList = ufoProductIndexBaseService.buildProductReturnInfoList(searchResult.getResultList());
JSONObject dataMap = new JSONObject();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", pageSize);
dataMap.put("page_total", searchResult.getTotalPage());
Map<String, Map<String, Object>> productReturnInfoMap = returnInfoList.stream().collect(Collectors.toMap(p -> p.get("id").toString(), p -> p));
List<Map<String, Object>> productReturnInfoListSorted = new ArrayList<>(productReturnInfoMap.size());
List<String> idList = Arrays.asList(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID).split(","));
for (String id : idList) {
Map<String, Object> sknInfo = productReturnInfoMap.get(id);
if (sknInfo != null) {
productReturnInfoListSorted.add(sknInfo);
}
}
dataMap.put(RETURN_LIST_NAME, productReturnInfoListSorted);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setCode(500);
}
}
public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
List<Integer> values = ConvertUtils.stringToIntList(paramMap.get(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID), ",");
if (values == null || values.isEmpty()) {
return boolFilter;
}
boolFilter.must(QueryBuilders.termsQuery(SearchRequestParams.UFOPRODUCTINDEX_PARAM_ID, values));
return boolFilter;
}
}
... ...
... ... @@ -27,7 +27,7 @@ import java.util.Map;
* @version 2018/12/20
*/
@Service
public class UfoProductListForPlatformService {
public class UfoPlatformService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
... ...
package com.yoho.search.service.scene.ufo;
import com.alibaba.fastjson.JSONObject;
import com.yoho.search.aop.cache.SearchCacheAble;
import com.yoho.search.base.utils.DateUtil;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.UfoProductIndexEsField;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.models.SearchApiResult;
import com.yoho.search.service.helper.UfoSearchQueryHelper;
import com.yoho.search.service.index.UfoProductIndexBaseService;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author wangnan
* @version 2018/12/27
*/
@Component
public class UfoSoonSaleService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private UfoProductIndexBaseService ufoProductIndexBaseService;
@Autowired
private UfoSearchQueryHelper ufoSearchQueryHelper;
private static final String RETURN_LIST_NAME = "product_list";
@SearchCacheAble(cacheName = "UFO_SOON_SALE_LIST", cacheInMinute = 3)
public SearchApiResult productList(Map<String, String> paramMap) {
try {
// 1、参数校验
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
int page = StringUtils.isBlank(paramMap.get("page")) ? 1 : Integer.parseInt(paramMap.get("page"));
if (page < 1 || pageSize < 0 || page * pageSize > 1000000) {
return new SearchApiResult().setCode(400).setMessage("分页参数不合法");
}
if (pageSize > 100) {
paramMap.put("viewNum", "100");
}
// 2. 构建SearchParam
SearchParam searchParam = new SearchParam();
BoolQueryBuilder boolFilter = constructFilterBuilder(paramMap, null);
searchParam.setFiter(boolFilter);
searchParam.setAggregationBuilders(null);
searchParam.setSize(pageSize);
searchParam.setOffset((page - 1) * pageSize);
// 设置排序字段
List<SortBuilder<?>> sortBuilders = new ArrayList<>();
sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.saleTime).order(SortOrder.ASC));
sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.orderBy).order(SortOrder.DESC));
sortBuilders.add(SortBuilders.fieldSort(UfoProductIndexEsField.id).order(SortOrder.DESC));
searchParam.setSortBuilders(ufoSearchQueryHelper.buildSortList(paramMap));
// 设置返回的结果
List<String> includeFields = ufoProductIndexBaseService.getUfoProductIndexIncludeFields();
searchParam.setIncludeFields(includeFields);
// 执行搜索
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_UFO_PRODUCT_INDEX, searchParam);
// 构造返回结果
List<Map<String, Object>> returnInfoList = ufoProductIndexBaseService.buildProductReturnInfoList(searchResult.getResultList());
JSONObject dataMap = new JSONObject();
dataMap.put("total", searchResult.getTotal());
dataMap.put("page", searchResult.getPage());
dataMap.put("page_size", pageSize);
dataMap.put("page_total", searchResult.getTotalPage());
dataMap.put(RETURN_LIST_NAME, returnInfoList);
return new SearchApiResult().setData(dataMap);
} catch (Exception e) {
logger.error(e.getMessage(), e);
return new SearchApiResult().setData(null).setCode(500);
}
}
public BoolQueryBuilder constructFilterBuilder(Map<String, String> paramMap, String filterParamName) throws Exception {
BoolQueryBuilder boolFilter = QueryBuilders.boolQuery();
//硬过滤
boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.delStatus, 0));
boolFilter.must(QueryBuilders.termQuery(UfoProductIndexEsField.shelveStatus, 1));
//发售列表
boolFilter.must(QueryBuilders.rangeQuery(UfoProductIndexEsField.saleTime).gt(DateUtil.getCurrentTimeSecond()));
return boolFilter;
}
}
... ...