|
|
package com.yoho.search.service.scene.shopbrand;
|
|
|
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.cache.beans.AbstractCacheBean;
|
|
|
import com.yoho.search.common.SearchCommonService;
|
|
|
import com.yoho.search.core.es.model.SearchParam;
|
|
|
import com.yoho.search.core.es.model.SearchResult;
|
|
|
import com.yoho.search.service.helper.ProductListHelper;
|
|
|
import com.yoho.search.service.index.ShopsIndexBaseService;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
@Service
|
|
|
public class ShopProductCacheBean extends AbstractCacheBean<ShopProductRequest, ShopProductResponse, ShopProductRequestResponse> {
|
|
|
|
|
|
@Autowired
|
|
|
private SearchCommonService searchCommonService;
|
|
|
@Autowired
|
|
|
private ShopsIndexBaseService shopsIndexBaseService;
|
|
|
@Autowired
|
|
|
private ProductListHelper productListHelper;
|
|
|
|
|
|
|
|
|
public Map<String, ShopProductResponse> queryShopProductList(List<ShopProductRequest> requests) {
|
|
|
List<ShopProductRequestResponse> requestResponses = new ArrayList<>();
|
|
|
requests.forEach(e -> requestResponses.add(new ShopProductRequestResponse(e)));
|
|
|
//2、执行查询
|
|
|
this.bacthFillResponseWithCache(requestResponses,60);
|
|
|
//3、返回结果
|
|
|
Map<String, ShopProductResponse> responseMap = new HashMap<>();
|
|
|
requestResponses.forEach(e -> {
|
|
|
if (!CollectionUtils.isEmpty(e.getResponse().getProduct_list())) {
|
|
|
responseMap.put(e.getRequest().getShopId().toString(), e.getResponse());
|
|
|
}
|
|
|
});
|
|
|
return responseMap;
|
|
|
}
|
|
|
|
|
|
@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());
|
|
|
}
|
|
|
List<SearchResult> searchResults = searchCommonService.doMutiSearch(ISearchConstants.INDEX_NAME_PRODUCT_INDEX, searchParams);
|
|
|
Map<String, List<Map<String, Object>>> shopProductListMap = new HashMap<>();
|
|
|
Map<String, Long> shopProductCountMap = new HashMap<>();
|
|
|
List<String> shopIds = new ArrayList<>();
|
|
|
for (SearchResult searchResult : searchResults) {
|
|
|
if (!CollectionUtils.isEmpty(searchResult.getResultList())) {
|
|
|
String shopIdString = String.valueOf(searchResult.getResultList().get(0).get("shopId"));
|
|
|
if (StringUtils.isNotBlank(shopIdString) && !shopIds.contains(shopIdString)) {
|
|
|
shopIds.add(shopIdString);
|
|
|
}
|
|
|
shopProductListMap.put(shopIdString, searchResult.getResultList());
|
|
|
shopProductCountMap.put(shopIdString, searchResult.getTotal());
|
|
|
}
|
|
|
}
|
|
|
Map<String, Map<String, Object>> shopMap = shopsIndexBaseService.getShopsMapByIds(shopIds);
|
|
|
shopProductListMap = productListHelper.buildReturnInfoByEsSourceListMap(shopProductListMap);
|
|
|
for (ShopProductRequestResponse requestResponse : missCacheRequests) {
|
|
|
String shopId = requestResponse.getRequest().getShopId().toString();
|
|
|
ShopProductResponse response = new ShopProductResponse();
|
|
|
response.setCount(shopProductCountMap.get(shopId) != null ? shopProductCountMap.get(shopId).intValue() : 0);
|
|
|
response.setInfo(shopMap.get(shopId));
|
|
|
response.setProduct_list(shopProductListMap.get(shopId));
|
|
|
results.put(requestResponse.getRequest(), response);
|
|
|
}
|
|
|
return results;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void batchAddResponseToCache(List<ShopProductRequestResponse> shopProductRequestResponses) {
|
|
|
super.batchAddResponseToCache(shopProductRequestResponses);
|
|
|
}
|
|
|
} |
...
|
...
|
|