Authored by hugufei

fix遗留的取cover_1,cover_2的方式

package com.yoho.search.service.helper;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Method;
import java.util.List;
@Component
public class SknImgHelper<Response> {
public void genImage(List<JSONObject> goodsList, Response response) {
if(CollectionUtils.isEmpty(goodsList)){
return;
}
JSONObject firstGood = null;
for (JSONObject good : goodsList) {
if ("Y".equals(good.getString("is_default")) && StringUtils.isNotEmpty(good.getString("images_url"))) {
genImage(good, response);
return;
}
// 有可能,goodlist中的所有good都没有cover1或者cover2,最终就采用第一个good
if(null == firstGood && StringUtils.isNotEmpty(good.getString("images_url"))){
firstGood = good;
}
}
if (firstGood != null) {
genImage(firstGood, response);
}
}
private void genImage(JSONObject good, Response response) {
String images_url = good.getString("images_url");
String conver_1 = good.getString("cover_1");
String conver_2 = good.getString("cover_2");
try {
Method[] methods = response.getClass().getDeclaredMethods();
// 循环查找想要的方法
for(Method method : methods) {
if("setCover_1".equals(method.getName())) {
method.invoke(response, (StringUtils.isEmpty(conver_1) ? images_url : conver_1));
}
if("setCover_2".equals(method.getName())) {
method.invoke(response, (StringUtils.isEmpty(conver_2) ? images_url : conver_2));
}
}
} catch (Exception e) {
}
}
}
... ... @@ -11,7 +11,6 @@ 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;
... ... @@ -26,8 +25,6 @@ public class HrShopProductCacheBean extends AbstractCacheBean<HrShopProductReque
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SknImgHelper sknImgHelper;
@Autowired
private BrandIndexBaseService brandIndexBaseService;
@Autowired
private ProductListHelper productListHelper;
... ... @@ -109,6 +106,8 @@ public class HrShopProductCacheBean extends AbstractCacheBean<HrShopProductReque
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", ""));
shopProduct.setCover_1(MapUtils.getString(productInfo, "cover_1", ""));
shopProduct.setCover_2(MapUtils.getString(productInfo, "cover_2", ""));
String brandId = MapUtils.getString(productInfo, "brand_id", "");
Map<String, Object> brand = brandMap.get(brandId);
if (brand != null) {
... ... @@ -116,8 +115,6 @@ public class HrShopProductCacheBean extends AbstractCacheBean<HrShopProductReque
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);
}
}
... ...
... ... @@ -16,7 +16,19 @@ 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 static final List<String> includeFields =
Arrays.asList(ProductIndexEsField.productSkn,
ProductIndexEsField.productName,
ProductIndexEsField.shopId,
ProductIndexEsField.shopName,
ProductIndexEsField.defaultImages,
ProductIndexEsField.cover_1,
ProductIndexEsField.cover_2,
ProductIndexEsField.salesPrice,
ProductIndexEsField.gender,
ProductIndexEsField.brandId);
private ParamQueryFilter paramQueryFilter;
private Integer shopId;
private RedisKeyBuilder redisKeyBuilder;
... ...
package com.yoho.search.service.scene.sknimg;
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.common.SearchCommonService;
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.helper.SknImgHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class SknImgsCacheBean extends AbstractCacheBean<SknImgsResquest, SknImgsResponse, SknImgsRequestResponse> {
@Autowired
private SearchCommonService searchCommonService;
@Autowired
private SknImgHelper sknImgHelper;
@Override
protected boolean useEhCache() {
... ... @@ -75,8 +72,8 @@ public class SknImgsCacheBean extends AbstractCacheBean<SknImgsResquest, SknImgs
response.setDefault_images(MapUtils.getString(product, ProductIndexEsField.defaultImages, ""));
response.setSkn_default_img(MapUtils.getString(product, ProductIndexEsField.sknDefaultImg, ""));
response.setGender(MapUtils.getString(product, ProductIndexEsField.gender, ""));
List<JSONObject> goodsList = JSON.parseObject(JSON.toJSONString(product.get(ProductIndexEsField.goodsList)), new TypeReference<List<JSONObject>>() {});
sknImgHelper.genImage(goodsList, response);
response.setCover_1(MapUtils.getString(product, ProductIndexEsField.cover_1, ""));
response.setCover_2(MapUtils.getString(product, ProductIndexEsField.cover_2, ""));
defaultImgTempMap.put(MapUtils.getString(product,ProductIndexEsField.productSkn,""), response);
}
for (SknImgsRequestResponse requestResponse : missCacheRequests) {
... ...
... ... @@ -9,7 +9,14 @@ import java.util.Arrays;
import java.util.List;
public class SknImgsResquest implements ICacheRequest {
private static final List<String> includeFields = Arrays.asList(ProductIndexEsField.productSkn,ProductIndexEsField.defaultImages, ProductIndexEsField.sknDefaultImg, ProductIndexEsField.goodsList,ProductIndexEsField.gender);
private static final List<String> includeFields = Arrays.asList(
ProductIndexEsField.productSkn,
ProductIndexEsField.defaultImages,
ProductIndexEsField.sknDefaultImg,
ProductIndexEsField.cover_1,
ProductIndexEsField.cover_2,
ProductIndexEsField.gender);
private Integer productSkn;
public SknImgsResquest(Integer productSkn){
... ...