|
|
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;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|