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