Authored by zhaojun2

修改红人cache类名

  1 +package com.yoho.search.service.scene.others.hongren;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.JSONObject;
  5 +import com.alibaba.fastjson.TypeReference;
  6 +import com.yoho.search.base.utils.ISearchConstants;
  7 +import com.yoho.search.base.utils.ProductIndexEsField;
  8 +import com.yoho.search.cache.beans.AbstractCacheBean;
  9 +import com.yoho.search.core.es.model.SearchParam;
  10 +import com.yoho.search.core.es.model.SearchResult;
  11 +import com.yoho.search.common.SearchCommonService;
  12 +import com.yoho.search.service.index.BrandIndexBaseService;
  13 +import com.yoho.search.service.helper.ProductListHelper;
  14 +import com.yoho.search.service.helper.SknImgHelper;
  15 +import org.apache.commons.collections.MapUtils;
  16 +import org.springframework.beans.factory.annotation.Autowired;
  17 +import org.springframework.stereotype.Component;
  18 +import org.springframework.util.CollectionUtils;
  19 +
  20 +import java.util.*;
  21 +import java.util.stream.Collectors;
  22 +
  23 +@Component
  24 +public class HrShopProductCacheBean extends AbstractCacheBean<HrShopProductRequest, HrShopProductResponse, HrShopProductRequestResponse> {
  25 +
  26 + @Autowired
  27 + private SearchCommonService searchCommonService;
  28 + @Autowired
  29 + private SknImgHelper sknImgHelper;
  30 + @Autowired
  31 + private BrandIndexBaseService brandIndexBaseService;
  32 + @Autowired
  33 + private ProductListHelper productListHelper;
  34 +
  35 + public List<HrShopProductResponse> getShopSknByShopId(List<HrShopProductRequest> shopSknRequests, String hrShopIds) {
  36 + List<HrShopProductRequestResponse> shopSknRequestResponses = new ArrayList<>();
  37 + for (HrShopProductRequest request : shopSknRequests) {
  38 + shopSknRequestResponses.add(new HrShopProductRequestResponse(request));
  39 + }
  40 + //2、执行父类方法
  41 + this.bacthFillResponseWithCache(shopSknRequestResponses, shopSknRequestResponses.size());
  42 + List<HrShopProductResponse> shopProductResponses = shopSknRequestResponses.stream().map(HrShopProductRequestResponse::getResponse).filter(Objects::nonNull).collect(Collectors.toList());
  43 + for (HrShopProductResponse response : shopProductResponses) {
  44 + response.setHr_shop_id(hrShopIds);
  45 + }
  46 + //3、返回结果
  47 + return shopProductResponses;
  48 + }
  49 +
  50 + @Override
  51 + protected boolean useEhCache() {
  52 + return false;
  53 + }
  54 +
  55 + @Override
  56 + protected Map<HrShopProductRequest, HrShopProductResponse> queryMissCacheRequestResults(List<HrShopProductRequestResponse> missCacheRequests) {
  57 + Map<HrShopProductRequest, HrShopProductResponse> results = new HashMap<>();
  58 + if (missCacheRequests == null || missCacheRequests.isEmpty()) {
  59 + return results;
  60 + }
  61 + List<SearchParam> searchParams = new ArrayList<>();
  62 + for (HrShopProductRequestResponse requestResponse : missCacheRequests) {
  63 + searchParams.add(requestResponse.getRequest().searchParam());
  64 + }
  65 + //2、执行搜索
  66 + List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
  67 + Set<Integer> brandIds = new HashSet<>();
  68 + for (SearchResult searchResult : searchResults) {
  69 + List<Map<String, Object>> resultList = searchResult.getResultList();
  70 + brandIds.addAll(resultList.stream().map(result -> MapUtils.getInteger(result, ProductIndexEsField.brandId, 0)).collect(Collectors.toSet()));
  71 + }
  72 + Map<String, Map<String, Object>> brandMap = new HashMap<>();
  73 + if (!CollectionUtils.isEmpty(brandIds)) {
  74 + brandMap = brandIndexBaseService.getBrandMapByIds(brandIds);
  75 + }
  76 + //获得变价计划
  77 + Map<String, List<Map<String, Object>>> shopProductListMap = new HashMap<>();
  78 + for (int i = 0; i < missCacheRequests.size(); i++) {
  79 + HrShopProductRequest request = missCacheRequests.get(i).getRequest();
  80 + SearchResult searchResult = searchResults.get(i);
  81 + if (request != null && searchResult != null) {
  82 + shopProductListMap.put(request.getShopId().toString(), searchResult.getResultList());
  83 + }
  84 + }
  85 + shopProductListMap = productListHelper.buildReturnInfoByEsSourceListMap(shopProductListMap);
  86 + for (int i = 0; i < missCacheRequests.size(); i++) {
  87 + HrShopProductRequest request = missCacheRequests.get(i).getRequest();
  88 + List<Map<String, Object>> productList = shopProductListMap.get(request.getShopId().toString());
  89 + HrShopProductResponse response = buildResponse(productList, brandMap, request);
  90 + results.put(request, response);
  91 + }
  92 + return results;
  93 + }
  94 +
  95 + private HrShopProductResponse buildResponse(List<Map<String, Object>> productList, Map<String, Map<String, Object>> brandMap, HrShopProductRequest request) {
  96 + HrShopProductResponse response = new HrShopProductResponse();
  97 + List<HrShopProductResponse.ShopProduct> shopProductList = new ArrayList<>();
  98 + response.setShop_id(request.getShopId());
  99 + response.setShop_product_list(shopProductList);
  100 +
  101 + if (!CollectionUtils.isEmpty(productList)) {
  102 + Map<String, Object> product = productList.get(0);
  103 + response.setShop_name(MapUtils.getString(product, "shop_name", ""));
  104 + //1、构建结果
  105 + for (Map<String, Object> productInfo : productList) {
  106 + HrShopProductResponse.ShopProduct shopProduct = new HrShopProductResponse.ShopProduct();
  107 + shopProduct.setProduct_skn(MapUtils.getInteger(productInfo, "product_skn", 0));
  108 + shopProduct.setProduct_name(MapUtils.getString(productInfo, "product_name", ""));
  109 + shopProduct.setGender(MapUtils.getString(productInfo, "gender", "1,3"));
  110 + shopProduct.setSales_price(MapUtils.getDouble(productInfo, "sales_price", 0.0));
  111 + shopProduct.setDefault_images(MapUtils.getString(productInfo, "default_images", ""));
  112 + String brandId = MapUtils.getString(productInfo, "brand_id", "");
  113 + Map<String, Object> brand = brandMap.get(brandId);
  114 + if (brand != null) {
  115 + shopProduct.setBrand_name(MapUtils.getString(brand, "brand_name", ""));
  116 + shopProduct.setBrand_ico(MapUtils.getString(brand, "brand_ico", ""));
  117 + }
  118 + shopProduct.setProduct_price_plan_list(MapUtils.getObject(productInfo, "product_price_plan_list", Collections.emptyList()));
  119 + List<JSONObject> goodsList = JSON.parseObject(JSON.toJSONString(productInfo.get("goods_list")), new TypeReference<List<JSONObject>>() {});
  120 + sknImgHelper.genImage(goodsList, shopProduct);
  121 + shopProductList.add(shopProduct);
  122 + }
  123 + }
  124 + return response;
  125 + }
  126 +
  127 +}
  1 +package com.yoho.search.service.scene.others.hongren;
  2 +
  3 +import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
  4 +import com.yoho.search.base.utils.MD5Util;
  5 +import com.yoho.search.base.utils.ProductIndexEsField;
  6 +import com.yoho.search.core.es.model.SearchParam;
  7 +import com.yoho.search.cache.CacheTimeConstants;
  8 +import com.yoho.search.cache.model.ICacheRequest;
  9 +import com.yoho.search.service.recall.models.common.ParamQueryFilter;
  10 +import org.elasticsearch.index.query.BoolQueryBuilder;
  11 +import org.elasticsearch.index.query.QueryBuilders;
  12 +import org.elasticsearch.search.sort.SortBuilders;
  13 +import org.elasticsearch.search.sort.SortOrder;
  14 +
  15 +import java.util.Arrays;
  16 +import java.util.List;
  17 +
  18 +public class HrShopProductRequest implements ICacheRequest {
  19 + private static final List<String> includeFields = Arrays.asList(ProductIndexEsField.productSkn, ProductIndexEsField.productName, ProductIndexEsField.shopId, ProductIndexEsField.shopName, ProductIndexEsField.defaultImages, ProductIndexEsField.goodsList, ProductIndexEsField.salesPrice,ProductIndexEsField.gender, ProductIndexEsField.brandId);
  20 + private ParamQueryFilter paramQueryFilter;
  21 + private Integer shopId;
  22 + private RedisKeyBuilder redisKeyBuilder;
  23 +
  24 + public HrShopProductRequest(ParamQueryFilter paramQueryFilter, Integer shopId) {
  25 + this.paramQueryFilter = paramQueryFilter;
  26 + this.shopId = shopId;
  27 + this.redisKeyBuilder = genRedisKeyBuilder();
  28 + }
  29 +
  30 + public RedisKeyBuilder genRedisKeyBuilder() {
  31 + StringBuilder stringBuilder = new StringBuilder();
  32 + stringBuilder.append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key());
  33 + stringBuilder.append(shopId);
  34 + String value = MD5Util.string2MD5(stringBuilder.toString());
  35 + return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SHOP_SKN:").appendVar(cacheTimeInMinute()).appendFixed(":").appendVar(value);
  36 + }
  37 +
  38 + @Override
  39 + public RedisKeyBuilder redisKeyBuilder() {
  40 + return redisKeyBuilder;
  41 + }
  42 +
  43 + @Override
  44 + public int cacheTimeInMinute() {
  45 + return CacheTimeConstants.CACHE_10_MINUTE;
  46 + }
  47 +
  48 + public ParamQueryFilter getParamQueryFilter() {
  49 + return paramQueryFilter;
  50 + }
  51 +
  52 + public Integer getShopId() {
  53 + return shopId;
  54 + }
  55 +
  56 + public void setShopId(Integer shopId) {
  57 + this.shopId = shopId;
  58 + }
  59 +
  60 + public SearchParam searchParam() {
  61 + SearchParam searchParam = new SearchParam();
  62 + if (paramQueryFilter != null && paramQueryFilter.getParamQuery() != null) {
  63 + searchParam.setQuery(paramQueryFilter.getParamQuery());
  64 + }
  65 + BoolQueryBuilder realFilter = QueryBuilders.boolQuery();
  66 + realFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
  67 + if (paramQueryFilter != null && paramQueryFilter.getParamFilter() != null) {
  68 + realFilter.must(paramQueryFilter.getParamFilter());
  69 + }
  70 + searchParam.setFiter(realFilter);
  71 + searchParam.setIncludeFields(includeFields);
  72 + searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC), SortBuilders.fieldSort(ProductIndexEsField.shelveTime).order(SortOrder.DESC)));
  73 + searchParam.setOffset(0);
  74 + searchParam.setSize(10);
  75 + return searchParam;
  76 + }
  77 +}
  1 +package com.yoho.search.service.scene.others.hongren;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.yoho.search.base.utils.Transfer;
  5 +import com.yoho.search.cache.model.AbstractCacheRequestResponse;
  6 +
  7 +public class HrShopProductRequestResponse extends AbstractCacheRequestResponse<HrShopProductRequest, HrShopProductResponse> {
  8 +
  9 + public HrShopProductRequestResponse(HrShopProductRequest request) {
  10 + super(request);
  11 + }
  12 +
  13 + @Override
  14 + public Transfer<String, HrShopProductResponse> getToResponseTransfer() {
  15 + return (v) -> JSON.parseObject(v, HrShopProductResponse.class);
  16 + }
  17 +
  18 + @Override
  19 + public Transfer<HrShopProductResponse, String> getFromResponseTransfer() {
  20 + return (v) -> JSON.toJSONString(v);
  21 + }
  22 +}
  1 +package com.yoho.search.service.scene.others.hongren;
  2 +
  3 +import java.util.List;
  4 +
  5 +public class HrShopProductResponse {
  6 + private String hr_shop_id;
  7 + private Integer shop_id;
  8 + private String shop_name;
  9 + private List<ShopProduct> shop_product_list;
  10 +
  11 + public static class ShopProduct {
  12 + private Integer product_skn;
  13 + private String product_name;
  14 + private String gender;
  15 + private String default_images;
  16 + private String cover_1;
  17 + private String cover_2;
  18 + private Double sales_price;
  19 + private Object product_price_plan_list;
  20 + private String brand_name;
  21 + private String brand_ico;
  22 +
  23 + public Integer getProduct_skn() {
  24 + return product_skn;
  25 + }
  26 +
  27 + public void setProduct_skn(Integer product_skn) {
  28 + this.product_skn = product_skn;
  29 + }
  30 +
  31 + public String getProduct_name() {
  32 + return product_name;
  33 + }
  34 +
  35 + public void setProduct_name(String product_name) {
  36 + this.product_name = product_name;
  37 + }
  38 +
  39 + public Double getSales_price() {
  40 + return sales_price;
  41 + }
  42 +
  43 + public void setSales_price(Double sales_price) {
  44 + this.sales_price = sales_price;
  45 + }
  46 +
  47 + public String getDefault_images() {
  48 + return default_images;
  49 + }
  50 +
  51 + public void setDefault_images(String default_images) {
  52 + this.default_images = default_images;
  53 + }
  54 +
  55 + public String getCover_1() {
  56 + return cover_1;
  57 + }
  58 +
  59 + public void setCover_1(String cover_1) {
  60 + this.cover_1 = cover_1;
  61 + }
  62 +
  63 + public String getCover_2() {
  64 + return cover_2;
  65 + }
  66 +
  67 + public void setCover_2(String cover_2) {
  68 + this.cover_2 = cover_2;
  69 + }
  70 +
  71 + public String getGender() {
  72 + return gender;
  73 + }
  74 +
  75 + public void setGender(String gender) {
  76 + this.gender = gender;
  77 + }
  78 +
  79 + public Object getProduct_price_plan_list() {
  80 + return product_price_plan_list;
  81 + }
  82 +
  83 + public void setProduct_price_plan_list(Object product_price_plan_list) {
  84 + this.product_price_plan_list = product_price_plan_list;
  85 + }
  86 +
  87 + public String getBrand_name() {
  88 + return brand_name;
  89 + }
  90 +
  91 + public void setBrand_name(String brand_name) {
  92 + this.brand_name = brand_name;
  93 + }
  94 +
  95 + public String getBrand_ico() {
  96 + return brand_ico;
  97 + }
  98 +
  99 + public void setBrand_ico(String brand_ico) {
  100 + this.brand_ico = brand_ico;
  101 + }
  102 + }
  103 +
  104 + public Integer getShop_id() {
  105 + return shop_id;
  106 + }
  107 +
  108 + public void setShop_id(Integer shop_id) {
  109 + this.shop_id = shop_id;
  110 + }
  111 +
  112 + public String getShop_name() {
  113 + return shop_name;
  114 + }
  115 +
  116 + public void setShop_name(String shop_name) {
  117 + this.shop_name = shop_name;
  118 + }
  119 +
  120 + public String getHr_shop_id() {
  121 + return hr_shop_id;
  122 + }
  123 +
  124 + public void setHr_shop_id(String hr_shop_id) {
  125 + this.hr_shop_id = hr_shop_id;
  126 + }
  127 +
  128 + public List<ShopProduct> getShop_product_list() {
  129 + return shop_product_list;
  130 + }
  131 +
  132 + public void setShop_product_list(List<ShopProduct> shop_product_list) {
  133 + this.shop_product_list = shop_product_list;
  134 + }
  135 +}
@@ -43,7 +43,7 @@ public class SearchHongRenService extends BaseSceneService { @@ -43,7 +43,7 @@ public class SearchHongRenService extends BaseSceneService {
43 @Autowired 43 @Autowired
44 private AggregationFactory aggregationFactory; 44 private AggregationFactory aggregationFactory;
45 @Autowired 45 @Autowired
46 - private ShopProductCacheBean shopSknCacheBean; 46 + private HrShopProductCacheBean shopSknCacheBean;
47 47
48 public SearchApiResult shopList(Map<String, String> paramMap) { 48 public SearchApiResult shopList(Map<String, String> paramMap) {
49 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum")); 49 int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
@@ -116,11 +116,11 @@ public class SearchHongRenService extends BaseSceneService { @@ -116,11 +116,11 @@ public class SearchHongRenService extends BaseSceneService {
116 List<Integer> subShopIds = com.yoho.search.base.utils.CollectionUtils.safeSubList(shopIds, pageSize * (page - 1), pageSize * page); 116 List<Integer> subShopIds = com.yoho.search.base.utils.CollectionUtils.safeSubList(shopIds, pageSize * (page - 1), pageSize * page);
117 117
118 if (CollectionUtils.isNotEmpty(subShopIds)) { 118 if (CollectionUtils.isNotEmpty(subShopIds)) {
119 - List<ShopProductRequest> shopProductRequests = subShopIds.stream().map(shopId -> {  
120 - return new ShopProductRequest(new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter()), shopId); 119 + List<HrShopProductRequest> shopProductRequests = subShopIds.stream().map(shopId -> {
  120 + return new HrShopProductRequest(new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter()), shopId);
121 }).collect(Collectors.toList()); 121 }).collect(Collectors.toList());
122 String hrShopIds = paramMap.get(SearchRequestParams.PARAM_SEARCH_HR_SHOP); 122 String hrShopIds = paramMap.get(SearchRequestParams.PARAM_SEARCH_HR_SHOP);
123 - List<ShopProductResponse> responseList = shopSknCacheBean.getShopSknByShopId(shopProductRequests, hrShopIds); 123 + List<HrShopProductResponse> responseList = shopSknCacheBean.getShopSknByShopId(shopProductRequests, hrShopIds);
124 dataMap.put("shop_product_list", responseList); 124 dataMap.put("shop_product_list", responseList);
125 } 125 }
126 } 126 }