Authored by hugufei

Merge branch 'master' into zj_groupshop

package com.yoho.search.common.utils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
public class ImageUrlAssist {
private static final String INIT_POSITION = "center";
private static final String INIT_BACKGROUND = "d2hpdGU=";
private static final String INIT_SIZE = "source";
private static final Logger logger = LoggerFactory.getLogger(ImageUrlAssist.class);
/**
* 图片尺寸字符串的分隔符
*/
private static final String SEPRATOR_SIZE = "x";
/**
* 获取图片的完整路径 包含参数
* @param imageUrl 相对路径
* @param bucket
* @param position
* @param background
* @return 图片的完整路径
*/
public static String getAllProductPicUrl(Object imageUrl, String bucket, String position, String background) {
if (!(imageUrl instanceof String) || StringUtils.isEmpty((String)imageUrl)) {
return "";
}
if (StringUtils.isEmpty(background)) {
background = INIT_BACKGROUND;
}
if (StringUtils.isEmpty(position)) {
position = INIT_POSITION;
}
return getUrl((String)imageUrl, bucket, null, null) + "?imageMogr2/thumbnail/{width}x{height}/background/"
+ background + "/position/" + position + "/quality/80";
}
/**
* 获取图片的路径,不含参数
* @param imageUrl
* @param bucket
* @return
*/
public static String getUrl(String imageUrl, String bucket, String source, Integer mode) {
if (StringUtils.isEmpty(imageUrl)) {
return "";
}
source = StringUtils.isEmpty(source) ? INIT_SIZE : source;
mode = null == mode ? 1 : mode;
Integer width = null;
Integer height = null;
if (StringUtils.isEmpty(source) && !INIT_SIZE.equals(source)) {
String[] split = source.split(SEPRATOR_SIZE);
if (split.length == 2) {
try {
width = Integer.valueOf(split[0]);
height = Integer.valueOf(split[1]);
} catch (NumberFormatException e) {
logger.warn("The source of goodsPic is not number. imageUrl is " + imageUrl + "; source : " + source, e);
}
}
}
String domain = getDomain(imageUrl);
if (StringUtils.isEmpty(domain)) {
return "";
}
return getImgPrivateUrl(bucket + imageUrl, width, height, mode, domain);
}
/**
* 获取私有的图片地址
* @param fileName
* @param width
* @param height
* @param mode
* @param domain
* @return
*/
private static String getImgPrivateUrl(String fileName, Integer width, Integer height, Integer mode, String domain) {
if (null == mode) {
mode = 1;
}
if (StringUtils.isEmpty(domain)) {
domain = "yhfair.qiniudn.com";
}
return makeRequest("http://" + domain + "/" + fileName.replaceAll("%2F", "/"), mode, width, height, domain, null, null);
}
/**
* 拼接图片的URL参数
* @param baseUrl
* @param mode
* @param width
* @param height
* @param domain
* @param quality
* @param format
* @return
*/
private static String makeRequest(String baseUrl, Integer mode, Integer width, Integer height, String domain,
String quality, String format) {
StringBuilder sb = new StringBuilder();
if (null != mode) {
sb.append(mode);
}
if (null != width) {
sb.append("/w/").append(width);
}
if (null != height) {
sb.append("/h/").append(height);
}
if (null != quality) {
sb.append("/q/").append(quality);
}
if (null != format) {
sb.append("/format/").append(format);
}
if (0 == sb.length()) {
return baseUrl;
}
if (null == width || null == height) {
return baseUrl;
}
return baseUrl + "?imageView/" + sb.toString();
}
private static String getDomain(String imageUrl) {
if (imageUrl.length() < 17) {
return "";
}
String node = imageUrl.substring(15, 17);
List<String> domainList = getDomainList(node);
if (domainList.isEmpty()) {
return "";
}
return domainList.get(Math.random() > 0.5 ? 1 : 0);
}
/**
* 图片的节点服务器列表
* @param node
* @return
*/
private static List<String> getDomainList(String node) {
if ("01".equals(node)) {
return Lists.newArrayList("img10.static.yhbimg.com", "img11.static.yhbimg.com");
}else if("02".equals(node)) {
return Lists.newArrayList("img12.static.yhbimg.com", "img13.static.yhbimg.com");
}
return new ArrayList<String>(0);
}
}
... ...
package com.yoho.search.service.helper;
import com.yoho.search.base.utils.ProductIndexEsField;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author wangnan
* @version 2018/5/31
*/
@Component
public class GoodsCoverHelper {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
public void buildGoodsCover(Map<String, Object> map, Map<String, Object> productMap) {
try {
String isGlobal = MapUtils.getString(map, ProductIndexEsField.isGlobal, "N");
if ("Y".equals(isGlobal)) {
productMap.put("cover_1", "");
productMap.put("cover_2", "");
return;
}
List<HashMap<String, Object>> goodsList = (List<HashMap<String, Object>>) MapUtils.getObject(map, ProductIndexEsField.goodsList, new ArrayList<>());
if (CollectionUtils.isEmpty(goodsList)) {
return;
}
String imagesUrl = "";
String cover1 = "";
String cover2 = "";
for (HashMap<String, Object> goodsMap : goodsList) {
// 如果此goods是is_default=Y的,用这个goods
if ("Y".equals(MapUtils.getString(goodsMap, "is_default"))) {
imagesUrl = MapUtils.getString(goodsMap, "images_url");
cover1 = MapUtils.getString(goodsMap, "cover_1");
cover2 = MapUtils.getString(goodsMap, "cover_2");
if (StringUtils.isNotBlank(imagesUrl)) {
break;
}
}
}
cover1 = StringUtils.isNotBlank(cover1) ? cover1 : imagesUrl;
cover2 = StringUtils.isNotBlank(cover2) ? cover2 : imagesUrl;
productMap.put("cover_1", cover1);
productMap.put("cover_2", cover2);
//default_images灾备
if (StringUtils.isBlank(MapUtils.getString(productMap, "default_images"))) {
String default_images = this.getImageNotNull(goodsList);
productMap.put("default_images", default_images);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
productMap.put("cover_1", "");
productMap.put("cover_2", "");
}
}
private String getImageNotNull(List<HashMap<String, Object>> goodsList) {
for (HashMap<String, Object> goodsMap : goodsList) {
String imagesUrl = MapUtils.getString(goodsMap, "images_url");
if (StringUtils.isNotBlank(imagesUrl)) {
return imagesUrl;
}
String cover1 = MapUtils.getString(goodsMap, "cover_1");
if (StringUtils.isNotBlank(cover1)) {
return cover1;
}
String cover2 = MapUtils.getString(goodsMap, "cover_2");
if (StringUtils.isNotBlank(cover2)) {
return cover2;
}
}
return null;
}
}
... ...
... ... @@ -2,10 +2,12 @@ package com.yoho.search.service.index;
import com.alibaba.fastjson.JSONArray;
import com.yoho.search.base.utils.ProductIndexEsField;
import com.yoho.search.service.helper.GoodsCoverHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
... ... @@ -19,6 +21,9 @@ public class ProductIndexBaseService {
private static final Logger logger = LoggerFactory.getLogger(ProductIndexBaseService.class);
@Autowired
private GoodsCoverHelper goodsCoverHelper;
// 获取从source中不返回的字段定义,用以节省带宽
private List<String> productIndexIncludeFields = new ArrayList<String>();
... ... @@ -108,7 +113,6 @@ public class ProductIndexBaseService {
return results;
}
@SuppressWarnings("unchecked")
public Map<String, Object> getProductMapFromEsSource(Map<String, Object> map) {
Map<String, Object> productMap = new HashMap<String, Object>();
... ... @@ -138,8 +142,10 @@ public class ProductIndexBaseService {
productMap.put("student_price", "Y".equalsIgnoreCase(isStudentPrice) ? MapUtils.getDoubleValue(map, ProductIndexEsField.studentPrice) : null);
productMap.put("is_student_rebate", MapUtils.getString(map, ProductIndexEsField.isstudentrebate, "N"));
productMap.put("default_images", MapUtils.getString(map, ProductIndexEsField.defaultImages, ""));
// 把gateway逻辑抽过来,抽取cover1,cover2
productMap.put("skn_default_img", MapUtils.getString(map, ProductIndexEsField.sknDefaultImg, ""));
productMap.put("default_images", MapUtils.getString(map, ProductIndexEsField.defaultImages, ""));
goodsCoverHelper.buildGoodsCover(map, productMap);
productMap.put("edit_time", MapUtils.getIntValue(map, ProductIndexEsField.editTime));
productMap.put("shelve_time", MapUtils.getIntValue(map, ProductIndexEsField.shelveTime));
... ...
... ... @@ -7,6 +7,7 @@ import java.util.Map;
import javax.annotation.PostConstruct;
import com.yoho.search.service.helper.GoodsCoverHelper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -21,6 +22,8 @@ public class WebProductIndexBaseService {
@Autowired
private ProductPricePlanIndexBaseService productPricePlanIndexBaseService;
@Autowired
private GoodsCoverHelper goodsCoverHelper;
// 获取从source中不返回的字段定义,用以节省带宽
private List<String> productIndexIncludeFields = new ArrayList<String>();
... ... @@ -100,8 +103,10 @@ public class WebProductIndexBaseService {
productMap.put("vip3_price", MapUtils.getDoubleValue(map, ProductIndexEsField.vip3Price, 0));
productMap.put("vip_discount_type", MapUtils.getIntValue(map, ProductIndexEsField.vipDiscountType, 1));
productMap.put("default_images", MapUtils.getString(map, ProductIndexEsField.defaultImages, ""));
// 把gateway逻辑抽过来,抽取cover1,cover2
productMap.put("first_shelve_time", MapUtils.getIntValue(map, ProductIndexEsField.firstShelveTime));
productMap.put("default_images", MapUtils.getString(map, ProductIndexEsField.defaultImages, ""));
goodsCoverHelper.buildGoodsCover(map, productMap);
productMap.put("gender", MapUtils.getString(map, ProductIndexEsField.gender, ""));
productMap.put("status", MapUtils.getIntValue(map, ProductIndexEsField.status, 0));
... ...