Authored by Gino Zhang

storage、goods增量时过滤无库存的尺码

package com.yoho.search.consumer.index.increment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
... ... @@ -24,39 +8,36 @@ import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.common.CostStatistics;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.index.increment.bulks.StorageSkuIndexBulkService;
import com.yoho.search.consumer.index.increment.rule.AbstractStorageRelatedMqListener;
import com.yoho.search.consumer.service.base.GoodsService;
import com.yoho.search.consumer.service.base.Product15DaySalesNumService;
import com.yoho.search.consumer.service.base.ProductService;
import com.yoho.search.consumer.service.base.StorageService;
import com.yoho.search.consumer.service.bo.ProductGoodsBO;
import com.yoho.search.consumer.service.bo.StorageSkuBO;
import com.yoho.search.consumer.service.logic.StorageSkuLogicService;
import com.yoho.search.consumer.service.logic.productIndex.ProductGoodsLogicService;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.dal.model.Goods;
import com.yoho.search.dal.model.Storage;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class GoodsMqListener extends AbstractMqListener implements ChannelAwareMessageListener {
public class GoodsMqListener extends AbstractStorageRelatedMqListener implements ChannelAwareMessageListener {
private static final Logger logger = LoggerFactory.getLogger(GoodsMqListener.class);
@Autowired
private IYohoIndexService indexService;
@Autowired
private GoodsService goodsService;
@Autowired
private StorageService storageService;
@Autowired
private StorageSkuIndexBulkService storageSkuIndexService;
@Autowired
private Product15DaySalesNumService product15DaySalesNumService;
@Autowired
private StorageSkuLogicService storageSkuLogicService;
@Autowired
private ProductService productService;
@Autowired
private ProductGoodsLogicService productGoodsLogicService;
public void onMessage(Message message, Channel channel) throws Exception {
... ... @@ -172,46 +153,11 @@ public class GoodsMqListener extends AbstractMqListener implements ChannelAwareM
logger.info("[model=GoodsMqListener_UpdateIndexNew][step=genGoodsList][productId={}][cost={}]", productId, costStatistics.getCost());
// 第三步、组装库存数据
this.fillProductStorage(productId, indexData);
this.fillStorageNumAndSizeInfo(productId, indexData);
logger.info("[model=GoodsMqListener_UpdateIndexNew][step=fillProductStorage][productId={}][cost={}]", productId, costStatistics.getCost());
// 第四步:更新索引
this.updateProductIndexWithDataMap(indexData, productId, key, begin);
logger.info("[model=GoodsMqListener_UpdateIndexNew][step=updateProductIndexWithDataMap][productId={}][cost={}]", productId, costStatistics.getCost());
}
/**
* 更新库存
*
* @param productId
* @param indexData
*/
private void fillProductStorage(Integer productId, Map<String, Object> indexData) {
// 第三步、更新库存
List<Storage> storageList = storageService.getStoragesByProductId(productId);
if (storageList.isEmpty()) {
indexData.put("storageNum", null);
indexData.put("isSoonSoldOut", null);
indexData.put("isSoldOut", null);
indexData.put("soldOut", null);
indexData.put("sizeIds", null);
} else {
int storageNum = 0;
Set<String> sizeSet = new HashSet<String>();
for (Storage stor : storageList) {
storageNum += stor.getStorageNum();
if (stor.getGoodsDimensionId() != null) {
sizeSet.add(stor.getGoodsDimensionId() + "");
}
}
indexData.put("storageNum", storageNum);
indexData.put("isSoonSoldOut", storageNum <= 2 ? "Y" : "N");
indexData.put("isSoldOut", storageNum == 0 ? "Y" : "N");
indexData.put("soldOut", storageNum == 0 ? 0 : 1);
if (!sizeSet.isEmpty()) {
indexData.put("sizeIds", StringUtils.join(sizeSet, ","));
}
}
}
}
... ...
package com.yoho.search.consumer.index.increment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
... ... @@ -25,41 +8,40 @@ import com.yoho.search.base.utils.ConvertUtils;
import com.yoho.search.base.utils.EventReportEnum;
import com.yoho.search.base.utils.ISearchConstants;
import com.yoho.search.consumer.common.CostStatistics;
import com.yoho.search.consumer.index.common.IYohoIndexService;
import com.yoho.search.consumer.index.increment.bulks.StorageSkuIndexBulkService;
import com.yoho.search.consumer.index.increment.rule.AbstractStorageRelatedMqListener;
import com.yoho.search.consumer.service.base.GoodsService;
import com.yoho.search.consumer.service.base.Product15DaySalesNumService;
import com.yoho.search.consumer.service.base.ProductPoolDetailService;
import com.yoho.search.consumer.service.base.ProductService;
import com.yoho.search.consumer.service.base.StorageService;
import com.yoho.search.consumer.service.bo.ProductGoodsBO;
import com.yoho.search.consumer.service.logic.ProductIndexLogicService;
import com.yoho.search.consumer.service.logic.productIndex.ProductGoodsLogicService;
import com.yoho.search.consumer.service.logic.productIndex.StorageUpdateTimeLogicService;
import com.yoho.search.core.es.utils.IgnoreSomeException;
import com.yoho.search.dal.model.Product;
import com.yoho.search.dal.model.Storage;
import com.yoho.search.dal.model.StorageUpdateTime;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class StorageMqListener extends AbstractMqListener implements ChannelAwareMessageListener {
public class StorageMqListener extends AbstractStorageRelatedMqListener implements ChannelAwareMessageListener {
private static final Logger logger = LoggerFactory.getLogger(StorageMqListener.class);
@Autowired
private IYohoIndexService indexService;
@Autowired
private StorageService storageService;
@Autowired
private StorageSkuIndexBulkService storageSkuIndexService;
@Autowired
private ProductPoolDetailService productPoolDetailService;
@Autowired
private ProductService productService;
@Autowired
private Product15DaySalesNumService product15DaySalesNumService;
@Autowired
private ProductIndexLogicService productIndexLogicService;
@Autowired
private StorageUpdateTimeLogicService storageUpdateTimeLogicService;
@Autowired
private GoodsService goodsService;
... ... @@ -155,30 +137,10 @@ public class StorageMqListener extends AbstractMqListener implements ChannelAwar
private void updateProductIndex(Integer productId, Integer productSkn, long begin, final String key) {
Map<String, Object> indexData = new HashMap<String, Object>();
indexData.put("productId", productId);
List<Storage> storageList = storageService.getStoragesByProductId(productId);
if (storageList.isEmpty()) {
indexData.put("storageNum", null);
indexData.put("isSoonSoldOut", null);
indexData.put("isSoldOut", null);
indexData.put("soldOut", null);
indexData.put("sizeIds", null);
} else {
int storageNum = 0;
Set<String> sizeSet = new HashSet<String>();
for (Storage stor : storageList) {
storageNum += stor.getStorageNum();
if (stor.getGoodsDimensionId() != null) {
sizeSet.add(stor.getGoodsDimensionId() + "");
}
}
indexData.put("storageNum", storageNum);
indexData.put("isSoonSoldOut", storageNum <= 2 ? "Y" : "N");
indexData.put("isSoldOut", storageNum == 0 ? "Y" : "N");
indexData.put("soldOut", storageNum == 0 ? 0 : 1);
if (!sizeSet.isEmpty()) {
indexData.put("sizeIds", StringUtils.join(sizeSet, ","));
}
}
// 设置库存和尺码信息
fillStorageNumAndSizeInfo(productId, indexData);
// 获取storageUpdateTime
List<Integer> productIds = new ArrayList<>();
List<Integer> skns = new ArrayList<>();
... ...
package com.yoho.search.consumer.index.increment.rule;
import com.yoho.search.consumer.index.increment.AbstractMqListener;
import com.yoho.search.consumer.service.base.SizeService;
import com.yoho.search.consumer.service.base.StorageService;
import com.yoho.search.dal.model.Storage;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Created by ginozhang on 2017/2/8.
* 将库存相关的增量更新代码抽象到父类,被Storage和Goods的MqListener共用。
*/
@Component
public abstract class AbstractStorageRelatedMqListener extends AbstractMqListener {
@Autowired
private StorageService storageService;
@Autowired
private SizeService sizeService;
protected void fillStorageNumAndSizeInfo(Integer productId, Map<String, Object> indexData) {
List<Storage> storageList = storageService.getStoragesByProductId(productId);
if (CollectionUtils.isNotEmpty(storageList)) {
// 过滤掉下架的无库存的storage
storageList = storageList.stream()
.filter(storage -> Integer.valueOf(1).equals(storage.getStatus()))
.filter(storage -> storage.getStorageNum() != null && storage.getStorageNum().intValue() > 0)
.collect(Collectors.toList());
}
if (CollectionUtils.isEmpty(storageList)) {
indexData.put("storageNum", 0);
indexData.put("isSoonSoldOut", "Y");
indexData.put("isSoldOut", "Y");
indexData.put("soldOut", 0);
indexData.put("sizeIds", null);
} else {
int storageNum = 0;
Set<String> sizeSet = new HashSet<String>();
for (Storage stor : storageList) {
storageNum += stor.getStorageNum();
if (stor.getGoodsDimensionId() != null) {
sizeSet.add(stor.getGoodsDimensionId() + "");
}
}
indexData.put("storageNum", storageNum);
indexData.put("isSoonSoldOut", storageNum <= 2 ? "Y" : "N");
indexData.put("isSoldOut", storageNum == 0 ? "Y" : "N");
indexData.put("soldOut", storageNum == 0 ? 0 : 1);
if (!sizeSet.isEmpty()) {
// 设置尺码信息
indexData.put("sizeIds", StringUtils.join(sizeSet, ","));
indexData.put("sizeNames", sizeService.getSizeNamesBySizeIds(sizeSet.stream().map(Integer::valueOf).collect(Collectors.toList())));
} else {
indexData.put("sizeIds", null);
indexData.put("sizeNames", null);
}
}
}
}
... ...
... ... @@ -2,10 +2,12 @@ package com.yoho.search.consumer.service.base;
import com.yoho.search.dal.SizeMapper;
import com.yoho.search.dal.model.Size;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;
@Component
//@Transactional
... ... @@ -45,4 +47,17 @@ public class SizeService {
public int count() {
return sizeMapper.count();
}
public String getSizeNamesBySizeIds(List<Integer> sizeIds){
if(CollectionUtils.isEmpty(sizeIds)){
return "";
}
List<Size> sizeList = sizeMapper.getByIds(sizeIds);
if(CollectionUtils.isEmpty(sizeList)){
return "";
}
// 和以前的数据保持一致 在末尾也加个逗号
return sizeList.stream().map(Size::getSizeName).collect(Collectors.joining(",","",","));
}
}
... ...