|
|
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;
|
|
|
}
|
|
|
|
|
|
} |