Authored by zhaojun2

add recall result

  1 +package com.yoho.search.restapi.tools;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.yoho.search.base.utils.CollectionUtils;
  5 +import com.yoho.search.base.utils.Transfer;
  6 +import com.yoho.search.base.utils.DateUtil;
  7 +import com.yoho.search.base.utils.SearchPageIdDefine;
  8 +import com.yoho.search.common.utils.HttpServletRequestUtils;
  9 +import com.yoho.search.common.utils.SearchKeyWordUtils;
  10 +import com.yoho.search.models.SearchApiResult;
  11 +import com.yoho.search.recall.beans.builder.UserRecallRequestBuilder;
  12 +import com.yoho.search.recall.beans.cache.SknReturnInfoCacheBean;
  13 +import com.yoho.search.recall.beans.cache.UserRecallCacheBean;
  14 +import com.yoho.search.recall.beans.strategy.StrategyEnum;
  15 +import com.yoho.search.recall.models.common.RecallSknInfo;
  16 +import com.yoho.search.recall.models.req.UserRecallRequest;
  17 +import com.yoho.search.recall.models.req.UserRecallResponse;
  18 +import com.yoho.search.service.base.SearchRequestParams;
  19 +import org.apache.commons.collections.MapUtils;
  20 +import org.apache.commons.lang.StringUtils;
  21 +import org.slf4j.Logger;
  22 +import org.slf4j.LoggerFactory;
  23 +import org.springframework.beans.factory.annotation.Autowired;
  24 +import org.springframework.web.bind.annotation.RequestMapping;
  25 +import org.springframework.web.bind.annotation.RequestMethod;
  26 +import org.springframework.web.bind.annotation.RestController;
  27 +
  28 +import javax.servlet.http.HttpServletRequest;
  29 +import java.util.*;
  30 +import java.util.stream.Collectors;
  31 +
  32 +@RestController
  33 +@RequestMapping(value = "/tools")
  34 +public class RecallResultController {
  35 +
  36 + private static final Logger LOGGER = LoggerFactory.getLogger("RecallResultController");
  37 +
  38 + @Autowired
  39 + private UserRecallRequestBuilder userRecallRequestBuilder;
  40 + @Autowired
  41 + private UserRecallCacheBean userRecallCacheBean;
  42 + @Autowired
  43 + private SknReturnInfoCacheBean sknReturnInfoCacheBean;
  44 +
  45 + private List<String> supportPageIdList = Arrays.asList(SearchPageIdDefine.PAGE_ID_SORT, SearchPageIdDefine.PAGE_ID_NEW, SearchPageIdDefine.PAGE_ID_ZQ, SearchPageIdDefine.PAGE_ID_POOL);
  46 +
  47 + @RequestMapping(method = RequestMethod.GET, value = "/getRecallResult")
  48 + public SearchApiResult userVectorSortBrand(HttpServletRequest request) {
  49 + Map<String, String> paramMap = HttpServletRequestUtils.transParamType(request);
  50 + if (paramMap.get("pageId") == null || !supportPageIdList.contains(paramMap.get("pageId"))) {
  51 + return new SearchApiResult().setCode(400).setMessage("不支持的pageId");
  52 + }
  53 + switch (paramMap.get("pageId")) {
  54 + case SearchPageIdDefine.PAGE_ID_SORT:
  55 + if (!checkSortParam(paramMap)) {
  56 + return new SearchApiResult().setCode(400).setMessage("缺少品类参数");
  57 + }
  58 + addSortParams(paramMap);
  59 + break;
  60 + case SearchPageIdDefine.PAGE_ID_NEW:
  61 + addNewParams(paramMap);
  62 + break;
  63 + case SearchPageIdDefine.PAGE_ID_ZQ:
  64 + if (!checkZqParam(paramMap)) {
  65 + return new SearchApiResult().setCode(400).setMessage("缺少专区ID");
  66 + }
  67 + addDefaultParamsToParamMap(paramMap);
  68 + break;
  69 + case SearchPageIdDefine.PAGE_ID_POOL:
  70 + if (!checkPoolParam(paramMap)) {
  71 + return new SearchApiResult().setCode(400).setMessage("缺少商品池ID");
  72 + }
  73 + addDefaultParamsToParamMap(paramMap);
  74 + break;
  75 + }
  76 + return recallProductList(paramMap);
  77 + }
  78 +
  79 +
  80 + private boolean checkSortParam(Map<String, String> paramMap) {
  81 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MAXSORT))) {
  82 + return true;
  83 + }
  84 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_MIDDLESORT))) {
  85 + return true;
  86 + }
  87 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_SMALLSORT))) {
  88 + return true;
  89 + }
  90 + if (StringUtils.isNotBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_COMMONSORT))) {
  91 + return true;
  92 + }
  93 + return false;
  94 + }
  95 +
  96 + private void addSortParams(Map<String, String> paramMap) {
  97 + addDefaultParamsToParamMap(paramMap);
  98 + paramMap.put(SearchRequestParams.PARAM_SEARCH_CONTAIN_GLOBAL, "Y");// 包含全球购
  99 + }
  100 +
  101 + private void addNewParams(Map<String, String> paramMap) {
  102 + addDefaultParamsToParamMap(paramMap);
  103 + // 默认30天内假上新
  104 + long end = DateUtil.getLastTimeSecond(new Date());
  105 + long begin = DateUtil.getFirstTimeSecond(DateUtil.addDay(new Date(), -30));
  106 + paramMap.put(SearchRequestParams.PARAM_SEARCH_SHELVETIME, begin + "," + end);
  107 + }
  108 +
  109 + private boolean checkZqParam(Map<String, String> paramMap) {
  110 + if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_ISPROMOTION))) {
  111 + return false;
  112 + }
  113 + return true;
  114 + }
  115 +
  116 + private boolean checkPoolParam(Map<String, String> paramMap) {
  117 + if (StringUtils.isBlank(paramMap.get(SearchRequestParams.PARAM_SEARCH_FILTER_POOLID))) {
  118 + return false;
  119 + }
  120 + return true;
  121 + }
  122 +
  123 + private void addDefaultParamsToParamMap(Map<String, String> paramMap) {
  124 + paramMap.put(SearchRequestParams.PARAM_SEARCH_PAGEID, paramMap.get("pageId"));// 根据场景划分的页面id
  125 + paramMap.put(SearchRequestParams.PARAM_SEARCH_GLOBAL_FILTER_BRAND, "Y");// 页面屏蔽
  126 + paramMap.put(SearchRequestParams.PARAM_SEARCH_STATUS, "1");// 上架
  127 + paramMap.put(SearchRequestParams.PARAM_SEARCH_STOCKNUM, "1");// 有库存
  128 + paramMap.put(SearchRequestParams.PARAM_SEARCH_SHOWSOLDOUT, "1");// 显示延期显示的商品
  129 + paramMap.put(SearchRequestParams.PARAM_SEARCH_ISOUTLETS, "2");// 非奥莱
  130 + paramMap.put(SearchRequestParams.PARAM_SEARCH_ATTRIBUTE_NOT, "2");// 非赠品
  131 + paramMap.put(SearchRequestParams.PARAM_SEARCH_NEEDSMALLSORT, "1");// 品类聚合时带上小分类
  132 + paramMap.put(SearchRequestParams.PARAM_SEARCH_AGG_WITH_PARAM_BRAND, "Y");// 聚合时使用参数中自带的参数
  133 + // 关键词反转码
  134 + String keyword = SearchKeyWordUtils.getParamKeyword(paramMap, SearchRequestParams.PARAM_SEARCH_QUERY);// 转码
  135 + if (!StringUtils.isBlank(keyword)) {
  136 + paramMap.put(SearchRequestParams.PARAM_SEARCH_QUERY, keyword);
  137 + paramMap.put("is_encode", "0");
  138 + }
  139 + }
  140 +
  141 + private SearchApiResult recallProductList(Map<String, String> paramMap) {
  142 + try {
  143 + UserRecallRequest userRecallRequest = userRecallRequestBuilder.buildUserRecallRequest(paramMap, 300);
  144 + UserRecallResponse userRecallResponse = userRecallCacheBean.queryRecallResult(userRecallRequest, false);
  145 + List<RecallSknInfo> recallSknInfos = userRecallResponse.getSknList();
  146 + List<Integer> productSknList = recallSknInfos.stream().map(RecallSknInfo::getProductSkn).collect(Collectors.toList());
  147 + List<Map<String, Object>> productInfoList = sknReturnInfoCacheBean.queryProductListBySkn(productSknList, productSknList.size());
  148 + Map<Integer, RecallSknInfo> sknRecallTypeMap = CollectionUtils.toMap(recallSknInfos, (Transfer<RecallSknInfo, Integer>)(recallSknInfo) -> {return recallSknInfo.getProductSkn();});
  149 +
  150 +
  151 + List<Map<String, Object>> briefProductInfoList = new ArrayList<>();
  152 + for (Map<String, Object> productInfo : productInfoList) {
  153 + Map<String, Object> briefProductInfo = new HashMap<>();
  154 + briefProductInfo.put("product_skn", productInfo.get("product_skn"));
  155 + briefProductInfo.put("brand_name", productInfo.get("brand_name"));
  156 + briefProductInfo.put("small_sort_name", productInfo.get("small_sort_name"));
  157 + briefProductInfo.put("middle_sort_name", productInfo.get("middle_sort_name"));
  158 + briefProductInfo.put("product_name", productInfo.get("product_name"));
  159 + int productSkn = MapUtils.getIntValue(productInfo, "product_skn", 0);
  160 + RecallSknInfo recallSknInfo = sknRecallTypeMap.get(productSkn);
  161 + if (recallSknInfo == null || recallSknInfo.getRecallType() == null) {
  162 + briefProductInfo.put("recall_type", StrategyEnum.DEFAULT_HEAT_VALUE.name());
  163 + } else {
  164 + briefProductInfo.put("recall_type", recallSknInfo.getRecallType());
  165 + }
  166 + briefProductInfoList.add(briefProductInfo);
  167 + }
  168 + //4、构造返回结果
  169 + JSONObject dataMap = new JSONObject();
  170 + dataMap.put("product_list", briefProductInfoList);
  171 + return new SearchApiResult().setData(dataMap);
  172 + } catch (Exception e){
  173 + LOGGER.error(e.getMessage(), e);
  174 + return new SearchApiResult().setData(null).setCode(500).setMessage("Exception");
  175 + }
  176 + }
  177 +
  178 +}