Authored by wangnan9279

goods列表抽离

  1 +package com.yoho.search.service.helper;
  2 +
  3 +import com.yoho.search.base.utils.ProductIndexEsField;
  4 +import org.apache.commons.collections.CollectionUtils;
  5 +import org.apache.commons.collections.MapUtils;
  6 +import org.apache.commons.lang.StringUtils;
  7 +import org.slf4j.Logger;
  8 +import org.slf4j.LoggerFactory;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import java.util.ArrayList;
  12 +import java.util.HashMap;
  13 +import java.util.List;
  14 +import java.util.Map;
  15 +
  16 +/**
  17 + * @author wangnan
  18 + * @version 2018/5/31
  19 + */
  20 +@Component
  21 +public class GoodsCoverHelper {
  22 +
  23 + private final Logger logger = LoggerFactory.getLogger(this.getClass());
  24 +
  25 + public void buildGoodsCover(Map<String, Object> map, Map<String, Object> productMap) {
  26 + try {
  27 + List<HashMap<String, Object>> goodsList = (List<HashMap<String, Object>>) MapUtils.getObject(map, ProductIndexEsField.goodsList, new ArrayList<>());
  28 + if (CollectionUtils.isEmpty(goodsList)) {
  29 + return;
  30 + }
  31 + //默认用第一个
  32 + HashMap<String, Object> firstGoods = goodsList.get(0);
  33 + String imagesUrl = MapUtils.getString(firstGoods, "images_url");
  34 + String cover1 = MapUtils.getString(firstGoods, "cover_1");
  35 + String cover2 = MapUtils.getString(firstGoods, "cover_2");
  36 + for (HashMap<String, Object> goodsMap : goodsList) {
  37 + // 如果此skc是默认的,用这个默认的
  38 + if ("Y".equals(MapUtils.getString(goodsMap, "is_default"))) {
  39 + imagesUrl = MapUtils.getString(goodsMap, "images_url");
  40 + cover1 = MapUtils.getString(goodsMap, "cover_1");
  41 + cover2 = MapUtils.getString(goodsMap, "cover_2");
  42 + }
  43 + }
  44 + cover1 = StringUtils.isNotBlank(cover1) ? cover1 : imagesUrl;
  45 + cover2 = StringUtils.isNotBlank(cover2) ? cover2 : imagesUrl;
  46 + productMap.put("cover_1", cover1);
  47 + productMap.put("cover_2", cover2);
  48 + } catch (Exception e) {
  49 + logger.error(e.getMessage(), e);
  50 + productMap.put("cover_1", "");
  51 + productMap.put("cover_2", "");
  52 + }
  53 + }
  54 +
  55 +
  56 +}
1 -package com.yoho.search.service.helper;  
2 -  
3 -import com.yoho.search.base.utils.ProductIndexEsField;  
4 -import com.yoho.search.common.utils.ImageUrlAssist;  
5 -import org.apache.commons.collections.CollectionUtils;  
6 -import org.apache.commons.collections.MapUtils;  
7 -import org.apache.commons.lang.StringUtils;  
8 -import org.slf4j.Logger;  
9 -import org.slf4j.LoggerFactory;  
10 -import org.springframework.stereotype.Component;  
11 -  
12 -import java.util.ArrayList;  
13 -import java.util.HashMap;  
14 -import java.util.List;  
15 -import java.util.Map;  
16 -  
17 -/**  
18 - * @author wangnan  
19 - * @version 2018/5/31  
20 - */  
21 -@Component  
22 -public class SknDefaultImagesHelper {  
23 -  
24 - private final Logger logger = LoggerFactory.getLogger(this.getClass());  
25 -  
26 - public String buildDefaultImages(Map<String, Object> map) {  
27 - String defaultImages = MapUtils.getString(map, ProductIndexEsField.defaultImages, "");  
28 - try {  
29 - String productGender = MapUtils.getString(map, ProductIndexEsField.gender, "");  
30 - List<HashMap<String, Object>> goodsList = (List<HashMap<String, Object>>) MapUtils.getObject(map, ProductIndexEsField.goodsList, new ArrayList<>());  
31 - if (CollectionUtils.isEmpty(goodsList)) {  
32 - return defaultImages;  
33 - }  
34 - HashMap<String, Object> firstGood = new HashMap<>(1);  
35 - boolean useFirstGoods = true;  
36 - for (int i = 0; i < goodsList.size(); i++) {  
37 - HashMap<String, Object> goodsMap = goodsList.get(i);  
38 - // 有可能,goodsList中的所有good都没有cover1或者cover2,最终就采用第一个good  
39 - if (i == 0 && StringUtils.isNotEmpty(MapUtils.getString(goodsMap, "images_url"))) {  
40 - firstGood = goodsMap;  
41 - }  
42 - // 此skc是默认的,则将图片赋值给skn  
43 - if ("Y".equals(MapUtils.getString(goodsMap, "is_default"))) {  
44 - defaultImages = getDefaultImages(goodsMap, productGender);  
45 - useFirstGoods = false;  
46 - }  
47 - }  
48 - // 如果还未赋值,则取第一个skc产品的默认图片  
49 - if (useFirstGoods) {  
50 - defaultImages = getDefaultImages(firstGood, productGender);  
51 - }  
52 - if (StringUtils.isNotBlank(defaultImages) && !defaultImages.startsWith("http://")) {  
53 - defaultImages = ImageUrlAssist.getAllProductPicUrl(defaultImages, "goodsimg", "center", "d2hpdGU=");  
54 - }  
55 - return defaultImages;  
56 - } catch (Exception e) {  
57 - logger.error(e.getMessage(), e);  
58 - }  
59 - return defaultImages;  
60 - }  
61 -  
62 - /**  
63 - * 男生频道进入:  
64 - * 筛选条件为男,展示男士商品和通用款,有男封的优先显示男封,没有男封,显示默认单品图。  
65 - * 筛选条件为女,展示女士商品和通用款,有女封的优先显示女封,没有女封,显示默认单品图。  
66 - * 筛选条件为男女,展示男/女商品和通用款,优先显示封面图,商品是男则显示男封,商品是女则显示女封,没有封面图,显示默认单品图。如果是通用款男封女封都有,优先显示男封,如果没有封面图,显示默认单品图。  
67 - * 女生频道进入:  
68 - * 筛选条件为男,展示男士商品和通用款,有男封的优先显示男封,没有男封,显示默认单品图。  
69 - * 筛选条件为女,展示女士商品和通用款,有女封的优先显示女封,没有女封,显示默认单品图。  
70 - * 筛选条件为男女,展示男/女商品和通用款,优先显示封面图,没有封面图,显示默认单品图。如果是通用款男封女封都有,优先显示女封。  
71 - * 潮童、创意生活频道进入:  
72 - * 筛选条件为男,展示男士商品和通用款,有男封的优先显示男封,没有男封,显示默认单品图。  
73 - * 筛选条件为女,展示女士商品和通用款,有女封的优先显示女封,没有女封,显示默认单品图。  
74 - * 筛选条件为男女,展示男/女商品和通用款,优先显示封面图,没有封面图,显示默认单品图。如果是通用款男封女封都有,优先显示男封。  
75 - */  
76 - private String getDefaultImages(HashMap<String, Object> goodsMap, String productGender) {  
77 - String images_url = ImageUrlAssist.getAllProductPicUrl(MapUtils.getString(goodsMap, "images_url"), "goodsimg", "center", "d2hpdGU=");  
78 - String conver_1 = ImageUrlAssist.getAllProductPicUrl(MapUtils.getString(goodsMap, "cover_1"), "goodsimg", "center", "d2hpdGU=");  
79 - String conver_2 = ImageUrlAssist.getAllProductPicUrl(MapUtils.getString(goodsMap, "cover_2"), "goodsimg", "center", "d2hpdGU=");  
80 - if ("1".equals(productGender)) {  
81 - return StringUtils.isNotBlank(conver_1) ? conver_1 : images_url;  
82 - } else if ("2".equals(productGender)) {  
83 - return StringUtils.isNotBlank(conver_2) ? conver_2 : images_url;  
84 - } else {  
85 - return images_url;  
86 - }  
87 - }  
88 -  
89 -}  
@@ -2,7 +2,7 @@ package com.yoho.search.service.index; @@ -2,7 +2,7 @@ package com.yoho.search.service.index;
2 2
3 import com.alibaba.fastjson.JSONArray; 3 import com.alibaba.fastjson.JSONArray;
4 import com.yoho.search.base.utils.ProductIndexEsField; 4 import com.yoho.search.base.utils.ProductIndexEsField;
5 -import com.yoho.search.service.helper.SknDefaultImagesHelper; 5 +import com.yoho.search.service.helper.GoodsCoverHelper;
6 import org.apache.commons.collections.MapUtils; 6 import org.apache.commons.collections.MapUtils;
7 import org.apache.commons.lang.StringUtils; 7 import org.apache.commons.lang.StringUtils;
8 import org.slf4j.Logger; 8 import org.slf4j.Logger;
@@ -22,7 +22,7 @@ public class ProductIndexBaseService { @@ -22,7 +22,7 @@ public class ProductIndexBaseService {
22 private static final Logger logger = LoggerFactory.getLogger(ProductIndexBaseService.class); 22 private static final Logger logger = LoggerFactory.getLogger(ProductIndexBaseService.class);
23 23
24 @Autowired 24 @Autowired
25 - private SknDefaultImagesHelper sknDefaultImagesHelper; 25 + private GoodsCoverHelper goodsCoverHelper;
26 26
27 27
28 // 获取从source中不返回的字段定义,用以节省带宽 28 // 获取从source中不返回的字段定义,用以节省带宽
@@ -142,8 +142,9 @@ public class ProductIndexBaseService { @@ -142,8 +142,9 @@ public class ProductIndexBaseService {
142 productMap.put("student_price", "Y".equalsIgnoreCase(isStudentPrice) ? MapUtils.getDoubleValue(map, ProductIndexEsField.studentPrice) : null); 142 productMap.put("student_price", "Y".equalsIgnoreCase(isStudentPrice) ? MapUtils.getDoubleValue(map, ProductIndexEsField.studentPrice) : null);
143 productMap.put("is_student_rebate", MapUtils.getString(map, ProductIndexEsField.isstudentrebate, "N")); 143 productMap.put("is_student_rebate", MapUtils.getString(map, ProductIndexEsField.isstudentrebate, "N"));
144 144
145 - // 把gateway逻辑抽过来,用goodsList直接生成default_images  
146 - productMap.put("default_images", sknDefaultImagesHelper.buildDefaultImages(map)); 145 + // 把gateway逻辑抽过来,抽取cover1,cover2
  146 + goodsCoverHelper.buildGoodsCover(map, productMap);
  147 + productMap.put("default_images", MapUtils.getString(map, ProductIndexEsField.defaultImages, ""));
147 productMap.put("skn_default_img", MapUtils.getString(map, ProductIndexEsField.sknDefaultImg, "")); 148 productMap.put("skn_default_img", MapUtils.getString(map, ProductIndexEsField.sknDefaultImg, ""));
148 149
149 productMap.put("edit_time", MapUtils.getIntValue(map, ProductIndexEsField.editTime)); 150 productMap.put("edit_time", MapUtils.getIntValue(map, ProductIndexEsField.editTime));