Authored by zhaojun2

修改红人cache类名

package com.yoho.search.service.scene.others.hongren;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.cache.beans.AbstractCacheBean;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.core.es.model.SearchResult;
import com.yoho.search.common.SearchCommonService;
import com.yoho.search.service.index.BrandIndexBaseService;
import com.yoho.search.service.helper.ProductListHelper;
import com.yoho.search.service.helper.SknImgHelper;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class HrShopProductCacheBean extends AbstractCacheBean<HrShopProductRequest, HrShopProductResponse, HrShopProductRequestResponse> {
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SknImgHelper sknImgHelper;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ProductListHelper productListHelper;
public List<HrShopProductResponse> getShopSknByShopId(List<HrShopProductRequest> shopSknRequests, String hrShopIds) {
List<HrShopProductRequestResponse> shopSknRequestResponses = new ArrayList<>();
for (HrShopProductRequest request : shopSknRequests) {
shopSknRequestResponses.add(new HrShopProductRequestResponse(request));
}
//2、执行父类方法
this.bacthFillResponseWithCache(shopSknRequestResponses, shopSknRequestResponses.size());
List<HrShopProductResponse> shopProductResponses = shopSknRequestResponses.stream().map(HrShopProductRequestResponse::getResponse).filter(Objects::nonNull).collect(Collectors.toList());
for (HrShopProductResponse response : shopProductResponses) {
response.setHr_shop_id(hrShopIds);
}
//3、返回结果
return shopProductResponses;
}
@Override
protected boolean useEhCache() {
return false;
}
@Override
protected Map<HrShopProductRequest, HrShopProductResponse> queryMissCacheRequestResults(List<HrShopProductRequestResponse> missCacheRequests) {
Map<HrShopProductRequest, HrShopProductResponse> results = new HashMap<>();
if (missCacheRequests == null || missCacheRequests.isEmpty()) {
return results;
}
List<SearchParam> searchParams = new ArrayList<>();
for (HrShopProductRequestResponse requestResponse : missCacheRequests) {
searchParams.add(requestResponse.getRequest().searchParam());
}
//2、执行搜索
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
Set<Integer> brandIds = new HashSet<>();
for (SearchResult searchResult : searchResults) {
List<Map<String, Object>> resultList = searchResult.getResultList();
brandIds.addAll(resultList.stream().map(result -> MapUtils.getInteger(result, ProductIndexEsField.brandId, 0)).collect(Collectors.toSet()));
}
Map<String, Map<String, Object>> brandMap = new HashMap<>();
if (!CollectionUtils.isEmpty(brandIds)) {
brandMap = brandIndexBaseService.getBrandMapByIds(brandIds);
}
//获得变价计划
Map<String, List<Map<String, Object>>> shopProductListMap = new HashMap<>();
for (int i = 0; i < missCacheRequests.size(); i++) {
HrShopProductRequest request = missCacheRequests.get(i).getRequest();
SearchResult searchResult = searchResults.get(i);
if (request != null && searchResult != null) {
shopProductListMap.put(request.getShopId().toString(), searchResult.getResultList());
}
}
shopProductListMap = productListHelper.buildReturnInfoByEsSourceListMap(shopProductListMap);
for (int i = 0; i < missCacheRequests.size(); i++) {
HrShopProductRequest request = missCacheRequests.get(i).getRequest();
List<Map<String, Object>> productList = shopProductListMap.get(request.getShopId().toString());
HrShopProductResponse response = buildResponse(productList, brandMap, request);
results.put(request, response);
}
return results;
}
private HrShopProductResponse buildResponse(List<Map<String, Object>> productList, Map<String, Map<String, Object>> brandMap, HrShopProductRequest request) {
HrShopProductResponse response = new HrShopProductResponse();
List<HrShopProductResponse.ShopProduct> shopProductList = new ArrayList<>();
response.setShop_id(request.getShopId());
response.setShop_product_list(shopProductList);
if (!CollectionUtils.isEmpty(productList)) {
Map<String, Object> product = productList.get(0);
response.setShop_name(MapUtils.getString(product, "shop_name", ""));
//1、构建结果
for (Map<String, Object> productInfo : productList) {
HrShopProductResponse.ShopProduct shopProduct = new HrShopProductResponse.ShopProduct();
shopProduct.setProduct_skn(MapUtils.getInteger(productInfo, "product_skn", 0));
shopProduct.setProduct_name(MapUtils.getString(productInfo, "product_name", ""));
shopProduct.setGender(MapUtils.getString(productInfo, "gender", "1,3"));
shopProduct.setSales_price(MapUtils.getDouble(productInfo, "sales_price", 0.0));
shopProduct.setDefault_images(MapUtils.getString(productInfo, "default_images", ""));
String brandId = MapUtils.getString(productInfo, "brand_id", "");
Map<String, Object> brand = brandMap.get(brandId);
if (brand != null) {
shopProduct.setBrand_name(MapUtils.getString(brand, "brand_name", ""));
shopProduct.setBrand_ico(MapUtils.getString(brand, "brand_ico", ""));
}
shopProduct.setProduct_price_plan_list(MapUtils.getObject(productInfo, "product_price_plan_list", Collections.emptyList()));
List<JSONObject> goodsList = JSON.parseObject(JSON.toJSONString(productInfo.get("goods_list")), new TypeReference<List<JSONObject>>() {});
sknImgHelper.genImage(goodsList, shopProduct);
shopProductList.add(shopProduct);
}
}
return response;
}
}
... ...
package com.yoho.search.service.scene.others.hongren;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.search.base.utils.MD5Util;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.core.es.model.SearchParam;
import com.yoho.search.cache.CacheTimeConstants;
import com.yoho.search.cache.model.ICacheRequest;
import com.yoho.search.service.recall.models.common.ParamQueryFilter;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import java.util.Arrays;
import java.util.List;
public class HrShopProductRequest implements ICacheRequest {
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);
private ParamQueryFilter paramQueryFilter;
private Integer shopId;
private RedisKeyBuilder redisKeyBuilder;
public HrShopProductRequest(ParamQueryFilter paramQueryFilter, Integer shopId) {
this.paramQueryFilter = paramQueryFilter;
this.shopId = shopId;
this.redisKeyBuilder = genRedisKeyBuilder();
}
public RedisKeyBuilder genRedisKeyBuilder() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(paramQueryFilter == null ? "" : paramQueryFilter.getParamMd5Key());
stringBuilder.append(shopId);
String value = MD5Util.string2MD5(stringBuilder.toString());
return RedisKeyBuilder.newInstance().appendFixed("YOHOSEARCH:").appendFixed("SHOP_SKN:").appendVar(cacheTimeInMinute()).appendFixed(":").appendVar(value);
}
@Override
public RedisKeyBuilder redisKeyBuilder() {
return redisKeyBuilder;
}
@Override
public int cacheTimeInMinute() {
return CacheTimeConstants.CACHE_10_MINUTE;
}
public ParamQueryFilter getParamQueryFilter() {
return paramQueryFilter;
}
public Integer getShopId() {
return shopId;
}
public void setShopId(Integer shopId) {
this.shopId = shopId;
}
public SearchParam searchParam() {
SearchParam searchParam = new SearchParam();
if (paramQueryFilter != null && paramQueryFilter.getParamQuery() != null) {
searchParam.setQuery(paramQueryFilter.getParamQuery());
}
BoolQueryBuilder realFilter = QueryBuilders.boolQuery();
realFilter.must(QueryBuilders.termQuery(ProductIndexEsField.shopId, shopId));
if (paramQueryFilter != null && paramQueryFilter.getParamFilter() != null) {
realFilter.must(paramQueryFilter.getParamFilter());
}
searchParam.setFiter(realFilter);
searchParam.setIncludeFields(includeFields);
searchParam.setSortBuilders(Arrays.asList(SortBuilders.fieldSort(ProductIndexEsField.heatValue).order(SortOrder.DESC), SortBuilders.fieldSort(ProductIndexEsField.shelveTime).order(SortOrder.DESC)));
searchParam.setOffset(0);
searchParam.setSize(10);
return searchParam;
}
}
... ...
package com.yoho.search.service.scene.others.hongren;
import com.alibaba.fastjson.JSON;
import com.yoho.search.base.utils.Transfer;
import com.yoho.search.cache.model.AbstractCacheRequestResponse;
public class HrShopProductRequestResponse extends AbstractCacheRequestResponse<HrShopProductRequest, HrShopProductResponse> {
public HrShopProductRequestResponse(HrShopProductRequest request) {
super(request);
}
@Override
public Transfer<String, HrShopProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, HrShopProductResponse.class);
}
@Override
public Transfer<HrShopProductResponse, String> getFromResponseTransfer() {
return (v) -> JSON.toJSONString(v);
}
}
... ...
package com.yoho.search.service.scene.others.hongren;
import java.util.List;
public class HrShopProductResponse {
private String hr_shop_id;
private Integer shop_id;
private String shop_name;
private List<ShopProduct> shop_product_list;
public static class ShopProduct {
private Integer product_skn;
private String product_name;
private String gender;
private String default_images;
private String cover_1;
private String cover_2;
private Double sales_price;
private Object product_price_plan_list;
private String brand_name;
private String brand_ico;
public Integer getProduct_skn() {
return product_skn;
}
public void setProduct_skn(Integer product_skn) {
this.product_skn = product_skn;
}
public String getProduct_name() {
return product_name;
}
public void setProduct_name(String product_name) {
this.product_name = product_name;
}
public Double getSales_price() {
return sales_price;
}
public void setSales_price(Double sales_price) {
this.sales_price = sales_price;
}
public String getDefault_images() {
return default_images;
}
public void setDefault_images(String default_images) {
this.default_images = default_images;
}
public String getCover_1() {
return cover_1;
}
public void setCover_1(String cover_1) {
this.cover_1 = cover_1;
}
public String getCover_2() {
return cover_2;
}
public void setCover_2(String cover_2) {
this.cover_2 = cover_2;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Object getProduct_price_plan_list() {
return product_price_plan_list;
}
public void setProduct_price_plan_list(Object product_price_plan_list) {
this.product_price_plan_list = product_price_plan_list;
}
public String getBrand_name() {
return brand_name;
}
public void setBrand_name(String brand_name) {
this.brand_name = brand_name;
}
public String getBrand_ico() {
return brand_ico;
}
public void setBrand_ico(String brand_ico) {
this.brand_ico = brand_ico;
}
}
public Integer getShop_id() {
return shop_id;
}
public void setShop_id(Integer shop_id) {
this.shop_id = shop_id;
}
public String getShop_name() {
return shop_name;
}
public void setShop_name(String shop_name) {
this.shop_name = shop_name;
}
public String getHr_shop_id() {
return hr_shop_id;
}
public void setHr_shop_id(String hr_shop_id) {
this.hr_shop_id = hr_shop_id;
}
public List<ShopProduct> getShop_product_list() {
return shop_product_list;
}
public void setShop_product_list(List<ShopProduct> shop_product_list) {
this.shop_product_list = shop_product_list;
}
}
... ...
... ... @@ -43,7 +43,7 @@ public class SearchHongRenService extends BaseSceneService {
@Autowired
private AggregationFactory aggregationFactory;
@Autowired
private ShopProductCacheBean shopSknCacheBean;
private HrShopProductCacheBean shopSknCacheBean;
public SearchApiResult shopList(Map<String, String> paramMap) {
int pageSize = StringUtils.isBlank(paramMap.get("viewNum")) ? 10 : Integer.parseInt(paramMap.get("viewNum"));
... ... @@ -116,11 +116,11 @@ public class SearchHongRenService extends BaseSceneService {
List<Integer> subShopIds = com.yoho.search.base.utils.CollectionUtils.safeSubList(shopIds, pageSize * (page - 1), pageSize * page);
if (CollectionUtils.isNotEmpty(subShopIds)) {
List<ShopProductRequest> shopProductRequests = subShopIds.stream().map(shopId -> {
return new ShopProductRequest(new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter()), shopId);
List<HrShopProductRequest> shopProductRequests = subShopIds.stream().map(shopId -> {
return new HrShopProductRequest(new ParamQueryFilter(searchParam.getQuery(), (BoolQueryBuilder)searchParam.getFiter()), shopId);
}).collect(Collectors.toList());
String hrShopIds = paramMap.get(SearchRequestParams.PARAM_SEARCH_HR_SHOP);
List<ShopProductResponse> responseList = shopSknCacheBean.getShopSknByShopId(shopProductRequests, hrShopIds);
List<HrShopProductResponse> responseList = shopSknCacheBean.getShopSknByShopId(shopProductRequests, hrShopIds);
dataMap.put("shop_product_list", responseList);
}
}
... ...