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 ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, ShopProductResponse, ShopProductRequestResponse> {
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SknImgHelper sknImgHelper;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ProductListHelper productListHelper;
public List<ShopProductResponse> getShopSknByShopId(List<ShopProductRequest> shopSknRequests, String hrShopIds) {
List<ShopProductRequestResponse> shopSknRequestResponses = new ArrayList<>();
for (ShopProductRequest request : shopSknRequests) {
shopSknRequestResponses.add(new ShopProductRequestResponse(request));
}
//2、执行父类方法
this.bacthFillResponseWithCache(shopSknRequestResponses, shopSknRequestResponses.size());
List<ShopProductResponse> shopProductResponses = shopSknRequestResponses.stream().map(ShopProductRequestResponse::getResponse).filter(Objects::nonNull).collect(Collectors.toList());
for (ShopProductResponse response : shopProductResponses) {
response.setHr_shop_id(hrShopIds);
}
//3、返回结果
return shopProductResponses;
}
@Override
protected boolean useEhCache() {
return false;
}
@Override
protected Map<ShopProductRequest, ShopProductResponse> queryMissCacheRequestResults(List<ShopProductRequestResponse> missCacheRequests) {
Map<ShopProductRequest, ShopProductResponse> results = new HashMap<>();
if (missCacheRequests == null || missCacheRequests.isEmpty()) {
return results;
}
List<SearchParam> searchParams = new ArrayList<>();
for (ShopProductRequestResponse 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++) {
ShopProductRequest 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++) {
ShopProductRequest request = missCacheRequests.get(i).getRequest();
List<Map<String, Object>> productList = shopProductListMap.get(request.getShopId().toString());
ShopProductResponse response = buildResponse(productList, brandMap, request);
results.put(request, response);
}
return results;
}
private ShopProductResponse buildResponse(List<Map<String, Object>> productList, Map<String, Map<String, Object>> brandMap, ShopProductRequest request) {
ShopProductResponse response = new ShopProductResponse();
List<ShopProductResponse.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) {
ShopProductResponse.ShopProduct shopProduct = new ShopProductResponse.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 ShopProductRequest 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 ShopProductRequest(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 ShopProductRequestResponse extends AbstractCacheRequestResponse<ShopProductRequest, ShopProductResponse> {
public ShopProductRequestResponse(ShopProductRequest request) {
super(request);
}
@Override
public Transfer<String, ShopProductResponse> getToResponseTransfer() {
return (v) -> JSON.parseObject(v, ShopProductResponse.class);
}
@Override
public Transfer<ShopProductResponse, String> getFromResponseTransfer() {
return (v) -> JSON.toJSONString(v);
}
}
package com.yoho.search.service.scene.others.hongren;
import java.util.List;
public class ShopProductResponse {
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;
}
}