|
|
package com.yoho.search.service.scene.others;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.aop.cache.SearchCacheAble;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.common.SearchRequestParams;
|
|
|
import com.yoho.search.common.utils.SearchApiResultUtils;
|
|
|
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.ProductListHelper;
|
|
|
import com.yoho.search.service.recall.beans.strategy.NotRecallTypeEnum;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
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/10/15
|
|
|
*/
|
|
|
@Service
|
|
|
public class ProductListBySknService {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ProductListBySknService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
@SearchCacheAble(cacheInMinute = 10, cacheName = "PRODUCT_LIST_BY_SKN")
|
|
|
public SearchApiResult productListBySkn(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 0、必传参数
|
|
|
if (!this.checkParam(paramMap)) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请传skn参数");
|
|
|
}
|
|
|
|
|
|
// 1)构造搜索参数
|
|
|
List<String> sknList = Arrays.asList(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN).split(","));
|
|
|
sknList = sknList.size() > 100 ? sknList.subList(0, 100) : sknList;
|
|
|
SearchParam searchParam = productListHelper.buildProductListBySknSearchParam(paramMap, sknList.size());
|
|
|
|
|
|
// 2)查询ES
|
|
|
SearchResult searchResult = searchCommonService.doSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParam);
|
|
|
if (searchResult == null) {
|
|
|
return new SearchApiResult().setCode(500).setMessage("execption");
|
|
|
}
|
|
|
|
|
|
// 3)获取返回结果
|
|
|
JSONObject productListResult = productListHelper.buildProductListResult(searchResult, searchParam.getSize(), false, NotRecallTypeEnum.NOT_PERSIONAL);
|
|
|
|
|
|
// 4)排序
|
|
|
List<Map<String, Object>> productReturnInfoList = (List<Map<String, Object>>) productListResult.get("product_list");
|
|
|
Map<String, Map<String, Object>> productReturnInfoMap = productReturnInfoList.stream().collect(Collectors.toMap(p -> p.get("product_skn").toString(), p -> p));
|
|
|
List<Map<String, Object>> productReturnInfoListSorted = new ArrayList<>(productReturnInfoList.size());
|
|
|
for (String skn : sknList) {
|
|
|
Map<String, Object> sknInfo = productReturnInfoMap.get(skn);
|
|
|
if (sknInfo != null) {
|
|
|
productReturnInfoListSorted.add(sknInfo);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//5)构造返回结果
|
|
|
productListResult.put("product_list", productReturnInfoListSorted);
|
|
|
productListResult.put("total", productReturnInfoListSorted.size());
|
|
|
productListResult.remove("page");
|
|
|
productListResult.remove("page_total");
|
|
|
productListResult.remove("page_size");
|
|
|
return new SearchApiResult().setData(productListResult);
|
|
|
} catch (Exception e) {
|
|
|
return SearchApiResultUtils.errorSearchApiResult(logger, paramMap, e);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private boolean checkParam(Map<String, String> paramMap) {
|
|
|
if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN))) {
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
} |
...
|
...
|
|