|
|
package com.yoho.search.service.scene.searchlike;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.CollectionUtils;
|
|
|
import com.yoho.search.common.cache.aop.SearchCacheAble;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.models.SearchApiResult;
|
|
|
import com.yoho.search.service.base.SearchDynamicConfigService;
|
|
|
import com.yoho.search.service.base.SearchRequestParams;
|
|
|
import com.yoho.search.service.base.index.ProductIndexBaseService;
|
|
|
|
|
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 店铺外找相似
|
|
|
*
|
|
|
* @author gufei.hu
|
|
|
*
|
|
|
*/
|
|
|
@Service
|
|
|
public class SearchLikeNotInShopService{
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(SearchLikeNotInShopService.class);
|
|
|
|
|
|
@Autowired
|
|
|
private SearchLikeHelper searchLikeHelper;
|
|
|
@Autowired
|
|
|
private ProductIndexBaseService productIndexBaseService;
|
|
|
@Autowired
|
|
|
private SearchDynamicConfigService searchDynamicConfigService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
/**
|
|
|
* 店铺外推荐
|
|
|
*
|
|
|
* @param paramMap
|
|
|
* @return
|
|
|
*/
|
|
|
@SearchCacheAble(cacheInMinute = 600, cacheName = "SEARCH_LIKE_NOT_IN_SHOP", includeParams = { "product_skn", "viewNum" })
|
|
|
public SearchApiResult searchLikeNotInShop(Map<String, String> paramMap) {
|
|
|
try {
|
|
|
// 1、获取参数
|
|
|
String productSkn = paramMap.get(SearchRequestParams.PARAM_SEARCH_PRODUCT_SKN);
|
|
|
if (StringUtils.isBlank(productSkn)) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("请输入SKN");
|
|
|
}
|
|
|
// 2、检测分页参数【默认30条,最多60条】
|
|
|
int pageSize = searchLikeHelper.getPageSize(paramMap);
|
|
|
|
|
|
// 2、获取当前查询的SKN的基本信息
|
|
|
JSONObject productInfoInEs = searchLikeHelper.getProductInfoInEs(productSkn);
|
|
|
if (productInfoInEs == null) {
|
|
|
return new SearchApiResult().setCode(400).setMessage("SKN不存在");
|
|
|
}
|
|
|
|
|
|
// 3、设置SearchParams
|
|
|
List<SearchParam> searchParams = new ArrayList<SearchParam>();
|
|
|
int sameSortCount = searchDynamicConfigService.getSearchLikeNotInShopSameSortPercent() * pageSize / 100;
|
|
|
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), sameSortCount, true));
|
|
|
searchParams.add(this.builderSearchParam(productInfoInEs, Arrays.asList(productSkn), pageSize - sameSortCount, false));
|
|
|
|
|
|
// 4、获取搜索结果[截取条数]
|
|
|
List<Map<String, Object>> tempProductList = searchLikeHelper.queryProductList(searchParams);
|
|
|
if (tempProductList.size() > pageSize) {
|
|
|
tempProductList = CollectionUtils.safeSubList(tempProductList,0, pageSize);
|
|
|
}
|
|
|
|
|
|
// 5、构造真实返回结果
|
|
|
List<Map<String, Object>> productListResults = new ArrayList<Map<String, Object>>();
|
|
|
if (!tempProductList.isEmpty()) {
|
|
|
productListResults = productListHelper.buildReturnInfoByEsSourceList(tempProductList);
|
|
|
}
|
|
|
JSONObject result = new JSONObject();
|
|
|
result.put("page", 1);
|
|
|
result.put("page_total", 1);
|
|
|
result.put("page_size", pageSize);
|
|
|
result.put("total", productListResults.size());
|
|
|
result.put("product_info", searchLikeHelper.genProductInfoResult(productInfoInEs));
|
|
|
result.put("product_list", productListResults);
|
|
|
// 8、结果加入缓存
|
|
|
return new SearchApiResult().setData(result);
|
|
|
} catch (Exception e) {
|
|
|
logger.error(e.getMessage(), e);
|
|
|
return new SearchApiResult().setData(null).setMessage("searchLikeNotInShop Exception").setCode(500);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SearchParam builderSearchParam(JSONObject productInfoInEs, List<String> notProductSkns, int pageSize, boolean inSameSort) {
|
|
|
return searchLikeHelper.builderSearchLikeNotInShopCharactersSearchParam(productInfoInEs, notProductSkns, pageSize,inSameSort);
|
|
|
}
|
|
|
|
|
|
|
|
|
} |