Authored by hugufei

获取默认图的逻辑优化

package com.yoho.search.dal.model;
/**
* Created by wangnan on 2016/6/30.
*/
public class GoodsCoverImage{
private Integer goodsId;
private String cover_1;
private String cover_2;
public GoodsCoverImage() {
}
public GoodsCoverImage(Integer goodsId) {
this.goodsId = goodsId;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public String getCover_2() {
return cover_2;
}
public void setCover_2(String cover_2) {
this.cover_2 = cover_2;
}
public String getCover_1() {
return cover_1;
}
public void setCover_1(String cover_1) {
this.cover_1 = cover_1;
}
}
package com.yoho.search.dal.model;
import lombok.Data;
@Data
public class GoodsImageInfo {
private Integer goodsId;
private String image_url;
private String cover_1;
private String cover_2;
public GoodsImageInfo(Integer goodsId) {
this.goodsId = goodsId;
}
}
... ...
package com.yoho.search.consumer.service.logicService;
import com.yoho.search.dal.model.GoodsCoverImage;
import com.yoho.search.dal.model.GoodsImageInfo;
import com.yoho.search.dal.model.GoodsImages;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
... ... @@ -13,34 +13,42 @@ import java.util.stream.Collectors;
@Component
public class GoodsRelatedImageLogicService {
/**
* 构造DefaultImages
*
* @param goodsImages
* @return
*/
public Map<Integer, String> buildProductDefaultImage(List<GoodsImages> goodsImages, Map<Integer, Integer> goodStorageMap) {
List<GoodsImages> validGoodsImages = goodsImages.stream().filter(a -> "Y".equalsIgnoreCase(a.getIsDefault()) && Integer.valueOf(1).equals(a.getStatus())).collect(Collectors.toList());
List<GoodsImages> storageGoodsImages = validGoodsImages.stream().filter(a -> goodStorageMap.containsKey(a.getGoodsId()) && goodStorageMap.get(a.getGoodsId()) > 0).collect(Collectors.toList());
Map<Integer, String> storageGoodsImagesMap = storageGoodsImages.stream().filter(a -> StringUtils.isNotBlank(a.getImageUrl())).collect(Collectors.toMap(GoodsImages::getProductId, GoodsImages::getImageUrl, (key1, key2) -> key1));
Map<Integer, String> validGoodsImagesMap = validGoodsImages.stream().filter(a -> StringUtils.isNotBlank(a.getImageUrl())).collect(Collectors.toMap(GoodsImages::getProductId, GoodsImages::getImageUrl, (key1, key2) -> key1));
validGoodsImagesMap.putAll(storageGoodsImagesMap);
return validGoodsImagesMap;
}
// 获取Goods的图片信息
public Map<Integer, GoodsImageInfo> selectGoodsCoverImage(List<GoodsImages> goodsImages) {
Map<Integer, GoodsImageInfo> goodsCoverImagesMap = new HashMap<>();
// 1、获取默认的有效的图
List<GoodsImages> defaultGoodsImages = goodsImages.stream().filter(a -> a.getStatus() == 1 && "Y".equalsIgnoreCase(a.getIsDefault())).collect(Collectors.toList());
for (GoodsImages goodsImage : defaultGoodsImages) {
GoodsImageInfo goodsImageInfo = goodsCoverImagesMap.computeIfAbsent(goodsImage.getGoodsId(), a -> new GoodsImageInfo(goodsImage.getGoodsId()));
String imageUrl = goodsImage.getImageUrl();
if (StringUtils.isNotBlank(imageUrl)) {
goodsImageInfo.setImage_url(imageUrl);
}
}
// 2、随便取一个有效的图片替代
List<GoodsImages> validGoodsImages = goodsImages.stream().filter(a -> a.getStatus() == 1).collect(Collectors.toList());
for (GoodsImages goodsImage : validGoodsImages) {
GoodsImageInfo goodsImageInfo = goodsCoverImagesMap.computeIfAbsent(goodsImage.getGoodsId(), a -> new GoodsImageInfo(goodsImage.getGoodsId()));
String imageUrl = goodsImage.getImageUrl();
//如果没有图片,则随便取一个有效的图片替代
if (StringUtils.isBlank(goodsImageInfo.getImage_url()) && StringUtils.isNotBlank(imageUrl)) {
goodsImageInfo.setImage_url(imageUrl);
}
}
public Map<Integer, GoodsCoverImage> selectGoodsCoverImage(List<GoodsImages> goodsImages) {
Map<Integer, GoodsCoverImage> goodsCoverImagesMap = new HashMap<>();
// 构建goodsCoverImagesMap
List<GoodsImages> validGoodsImages = goodsImages.stream().filter(a -> a.getGenderCover() != null && a.getGenderCover() > 0).collect(Collectors.toList());
for (GoodsImages coverImage : validGoodsImages) {
//3、处理男封女封
List<GoodsImages> coverImages = goodsImages.stream().filter(a -> a.getStatus() == 1 && a.getGenderCover() != null && a.getGenderCover() > 0).collect(Collectors.toList());
for (GoodsImages coverImage : coverImages) {
String cover1 = coverImage.getGenderCover() == 1 ? coverImage.getImageUrl() : null;
String cover2 = coverImage.getGenderCover() == 2 ? coverImage.getImageUrl() : null;
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.computeIfAbsent(coverImage.getGoodsId(), a -> new GoodsCoverImage(coverImage.getGoodsId()));
if (StringUtils.isBlank(goodsCoverImage.getCover_1()) && !StringUtils.isBlank(cover1)) {
goodsCoverImage.setCover_1(cover1);
GoodsImageInfo goodsImageInfo = goodsCoverImagesMap.computeIfAbsent(coverImage.getGoodsId(), a -> new GoodsImageInfo(coverImage.getGoodsId()));
if (StringUtils.isBlank(goodsImageInfo.getCover_1()) && !StringUtils.isBlank(cover1)) {
goodsImageInfo.setCover_1(cover1);
}
if (StringUtils.isBlank(goodsCoverImage.getCover_2()) && !StringUtils.isBlank((cover2))) {
goodsCoverImage.setCover_2(cover2);
if (StringUtils.isBlank(goodsImageInfo.getCover_2()) && !StringUtils.isBlank((cover2))) {
goodsImageInfo.setCover_2(cover2);
}
}
return goodsCoverImagesMap;
... ...
... ... @@ -37,7 +37,6 @@ public class ProductGoodsLogicService {
@Autowired
private StorageService storageService;
/**
* 全量增量都有调用
*/
... ... @@ -53,11 +52,10 @@ public class ProductGoodsLogicService {
//2、构建库存相关数据
Map<Integer, Integer> goodStorageNumMap = goodsRelatedStorageLogicService.buildGoodStorageNumMap(storageList);
Map<Integer, List<Storage>> productStorageMap = goodsRelatedStorageLogicService.buildProductStorageListMap(goodsList,storageList);
Map<Integer, List<Storage>> productStorageMap = goodsRelatedStorageLogicService.buildProductStorageListMap(goodsList, storageList);
//3、构建图片相关数据
Map<Integer, String> defaultImageMap = goodsRelatedImageLogicService.buildProductDefaultImage(goodsImages, goodStorageNumMap);
Map<Integer, GoodsCoverImage> goodsCoverImagesMap = goodsRelatedImageLogicService.selectGoodsCoverImage(goodsImages);
Map<Integer, GoodsImageInfo> goodsImageInfoMap = goodsRelatedImageLogicService.selectGoodsCoverImage(goodsImages);
//4、批量查询尺码信息
Map<Integer, String> sizeId2NameMap = productSizesLogicService.buildSizeId2NameMap(storageList);
... ... @@ -65,19 +63,17 @@ public class ProductGoodsLogicService {
//5、构建product2GoodsMap
Map<Integer, List<ProductGoodInfo>> productGoodsMap = new HashMap<>();
for (Goods goods : goodsList) {
ProductGoodInfo goodInfo = buildProductGoodInfo(goods, goodsCoverImagesMap, goodStorageNumMap);
ProductGoodInfo goodInfo = buildProductGoodInfo(goods, goodsImageInfoMap, goodStorageNumMap);
List<ProductGoodInfo> goodInfoList = productGoodsMap.computeIfAbsent(goods.getProductId(), a -> new ArrayList<>());
goodInfoList.add(goodInfo);
}
//6、构建productGoodsBOs
List<ProductGoodsBO> productGoodsBOs = new ArrayList<>();
for (Map.Entry<Integer, List<ProductGoodInfo>> entry : productGoodsMap.entrySet()) {
ProductGoodsBO productGoodsBO = new ProductGoodsBO(entry.getKey(), entry.getValue());
//填充图片
fillProductImage(productGoodsBO, defaultImageMap);
fillProductImage(productGoodsBO);
//填充颜色
fillColorInfo(productGoodsBO);
//填充尺码
... ... @@ -91,35 +87,83 @@ public class ProductGoodsLogicService {
return new ArrayList<>();
}
private void fillProductImage(ProductGoodsBO productGoodsBO, Map<Integer, String> defaultImageMap) {
String imagesUrl = "";
/**
* 转成good对象
*/
private ProductGoodInfo buildProductGoodInfo(Goods goods, Map<Integer, GoodsImageInfo> goodsImagesInfoMap, Map<Integer, Integer> goodStorageMap) {
Map<Integer, ProductColor> productColorsMap = basicDataCacheService.getColor();
ProductGoodInfo productGoodInfo = new ProductGoodInfo();
// 来自Goods
productGoodInfo.setGoods_id(String.valueOf(goods.getId()));
productGoodInfo.setColor_name(StringUtils.isBlank(goods.getColorName()) ? "" : goods.getColorName());
productGoodInfo.setImages_url(StringUtils.isBlank(goods.getColorImage()) ? "" : goods.getColorImage());
productGoodInfo.setIs_default(StringUtils.isBlank(goods.getIsDefault()) ? "N" : goods.getIsDefault());
productGoodInfo.setColor_id(goods.getColorId() == null ? "" : goods.getColorId().toString());
productGoodInfo.setStatus(goods.getStatus() == null ? "0" : goods.getStatus().toString());
productGoodInfo.setProduct_skc(goods.getProductSkc() == null ? "" : goods.getProductSkc().toString());
productGoodInfo.setColor_value("");
// 来自productColorsMap
ProductColor productColor = productColorsMap.get(goods.getColorId());
if (productColor != null) {
String colorCode = productColor.getColorCode();
productGoodInfo.setColor_code(colorCode);
}
// 来自goodsCoverImagesMap
GoodsImageInfo goodsImageInfo = goodsImagesInfoMap.get(goods.getId());
String cover_1 = "";
String cover_2 = "";
if (goodsImageInfo != null) {
cover_1 = goodsImageInfo.getCover_1();
cover_2 = goodsImageInfo.getCover_2();
// Images_url兜底
if (StringUtils.isBlank(productGoodInfo.getImages_url())) {
productGoodInfo.setImages_url(goodsImageInfo.getImage_url());
}
}
productGoodInfo.setCover_1(StringUtils.isBlank(cover_1) ? "" : cover_1);
productGoodInfo.setCover_2(StringUtils.isBlank(cover_2) ? "" : cover_2);
// 来自storageMap
Integer storage_num = goodStorageMap.get(goods.getId());
productGoodInfo.setStorage_num(storage_num == null ? "0" : storage_num.toString());
return productGoodInfo;
}
private void fillProductImage(ProductGoodsBO productGoodsBO) {
String defaultImage = "";
String cover1 = "";
String cover2 = "";
for (ProductGoodInfo goodInfo : productGoodsBO.getGoodsList()) {
// 如果此goods是is_default=Y的,用这个goods,否则不用
// 如果此goods是is_default=Y的,并且有库存用这个goods的信息
if ("Y".equals(goodInfo.getIs_default()) && Integer.valueOf(goodInfo.getStorage_num()) > 0) {
imagesUrl = goodInfo.getImages_url();
defaultImage = goodInfo.getImages_url();
cover1 = goodInfo.getCover_1();
cover2 = goodInfo.getCover_2();
if (StringUtils.isNotBlank(imagesUrl)) {
//找到了则退出循环
if (StringUtils.isNotBlank(defaultImage)) {
break;
}
}
}
cover1 = StringUtils.isNotBlank(cover1) ? cover1 : imagesUrl;
cover2 = StringUtils.isNotBlank(cover2) ? cover2 : imagesUrl;
productGoodsBO.setCover_1(cover1);
productGoodsBO.setCover_2(cover2);
String defaultImage = defaultImageMap.get(productGoodsBO.getProductId());
//default_images灾备
if (StringUtils.isBlank(defaultImage)) {
defaultImage = getImageNotNull(productGoodsBO.getGoodsList());
defaultImage = getImageNotNull(productGoodsBO.getGoodsList(), true);
if (StringUtils.isBlank(defaultImage)) {
defaultImage = getImageNotNull(productGoodsBO.getGoodsList(), false);
}
}
// cover1 cover2不存在则取defaultImage
cover1 = StringUtils.isNotBlank(cover1) ? cover1 : defaultImage;
cover2 = StringUtils.isNotBlank(cover2) ? cover2 : defaultImage;
productGoodsBO.setCover_1(cover1);
productGoodsBO.setCover_2(cover2);
productGoodsBO.setDefaultImage(defaultImage);
}
private String getImageNotNull(List<ProductGoodInfo> goodsList) {
private String getImageNotNull(List<ProductGoodInfo> goodsList, boolean filterStorage) {
for (ProductGoodInfo goods : goodsList) {
if (filterStorage && Integer.valueOf(goods.getStorage_num()) <= 0) {
continue;
}
String imagesUrl = goods.getImages_url();
if (StringUtils.isNotBlank(imagesUrl)) {
return imagesUrl;
... ... @@ -136,7 +180,6 @@ public class ProductGoodsLogicService {
return "";
}
/**
* 获取有库存且状态正常的color
*/
... ... @@ -163,40 +206,6 @@ public class ProductGoodsLogicService {
productGoodsBO.setColorNames(colorNameSet);
}
/**
* 转成good对象
*/
private ProductGoodInfo buildProductGoodInfo(Goods goods, Map<Integer, GoodsCoverImage> goodsCoverImagesMap, Map<Integer, Integer> goodStorageMap) {
Map<Integer, ProductColor> productColorsMap = basicDataCacheService.getColor();
ProductGoodInfo productGoodInfo = new ProductGoodInfo();
// 来自Goods
productGoodInfo.setGoods_id(String.valueOf(goods.getId()));
productGoodInfo.setColor_name(StringUtils.isBlank(goods.getColorName()) ? "" : goods.getColorName());
productGoodInfo.setImages_url(StringUtils.isBlank(goods.getColorImage()) ? "" : goods.getColorImage());
productGoodInfo.setIs_default(StringUtils.isBlank(goods.getIsDefault()) ? "N" : goods.getIsDefault());
productGoodInfo.setColor_id(goods.getColorId() == null ? "" : goods.getColorId().toString());
productGoodInfo.setStatus(goods.getStatus() == null ? "0" : goods.getStatus().toString());
productGoodInfo.setProduct_skc(goods.getProductSkc() == null ? "" : goods.getProductSkc().toString());
productGoodInfo.setColor_value("");
// 来自productColorsMap
ProductColor productColor = productColorsMap.get(goods.getColorId());
if (productColor != null) {
String colorCode = productColor.getColorCode();
productGoodInfo.setColor_code(colorCode);
}
// 来自goodsCoverImagesMap
GoodsCoverImage goodsCoverImage = goodsCoverImagesMap.get(goods.getId());
String cover_1 = "";
String cover_2 = "";
if (goodsCoverImage != null) {
cover_1 = goodsCoverImage.getCover_1();
cover_2 = goodsCoverImage.getCover_2();
}
productGoodInfo.setCover_1(StringUtils.isBlank(cover_1) ? "" : cover_1);
productGoodInfo.setCover_2(StringUtils.isBlank(cover_2) ? "" : cover_2);
// 来自storageMap
Integer storage_num = goodStorageMap.get(goods.getId());
productGoodInfo.setStorage_num(storage_num == null ? "0" : storage_num.toString());
return productGoodInfo;
}
}
... ...