Authored by wangnan

构建pi代码优化

... ... @@ -11,7 +11,6 @@ import com.yoho.search.dal.model.Brand;
import com.yoho.search.dal.model.Product;
import com.yoho.search.dal.model.ProductSort;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
... ... @@ -21,10 +20,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
... ... @@ -53,7 +49,8 @@ public class ProductIndexLogicService implements ApplicationContextAware {
logger.warn("There is no viewBuilder defined.");
return;
}
viewBuilderList = beanMap.values().stream().collect(Collectors.toList());
viewBuilderList = beanMap.values().stream().sorted(Comparator.comparing(ViewBuilder::getOrder)).collect(Collectors.toList());
System.out.println(viewBuilderList.stream().map(viewBuilder -> viewBuilder.getClass().getSimpleName() + ":" + viewBuilder.getOrder()).collect(Collectors.joining()));
}
... ... @@ -137,42 +134,9 @@ public class ProductIndexLogicService implements ApplicationContextAware {
}
viewBuilderList.stream().forEach(viewBuilder -> {
viewBuilder.build(productIndexBOs, ids, skns);
logger.info("ProductIndex is Building,running viewBuilder success", viewBuilder.getClass().getSimpleName());
});
// 构造一个由风格、款式和属性拼接而成的关键词字段 用于关键词推荐
productIndexBOs.forEach(productIndexBO -> {
productIndexBO.setProductAttrField(contrustAttrField(productIndexBO));
});
return productIndexBOs;
}
private String contrustAttrField(ProductIndexBO productIndexBO) {
StringBuffer sb = new StringBuffer(100);
String value = productIndexBO.getStyle();
if(StringUtils.isNotEmpty(value)){
sb.append(value).append(',');
}
value = productIndexBO.getStandardOnlyNames();
if(StringUtils.isNotEmpty(value)){
sb.append(value).append(',');
}
value = productIndexBO.getAttributeNames();
if(StringUtils.isNotEmpty(value)){
sb.append(value).append(',');
}
value = productIndexBO.getPattern();
if(StringUtils.isNotEmpty(value)){
sb.append(value).append(',');
}
if(sb.length() == 0){
return null;
}
return sb.substring(0, sb.length() - 1).replaceAll("\\/", ",");
}
}
... ...
package com.yoho.search.consumer.service.logic.productIndex.viewBuilder;
import com.yoho.search.consumer.service.bo.ProductIndexBO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by wangnan on 2017/3/27.
* 构造一个由风格、款式和属性拼接而成的关键词字段 用于关键词推荐
*/
@Component
public class AttrFieldBuilder implements ViewBuilder {
@Override
public void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> skns) {
for (ProductIndexBO productIndexBO : productIndexBOs) {
StringBuffer sb = new StringBuffer(100);
String value = productIndexBO.getStyle();
if (StringUtils.isNotEmpty(value)) {
sb.append(value).append(',');
}
value = productIndexBO.getStandardOnlyNames();
if (StringUtils.isNotEmpty(value)) {
sb.append(value).append(',');
}
value = productIndexBO.getAttributeNames();
if (StringUtils.isNotEmpty(value)) {
sb.append(value).append(',');
}
value = productIndexBO.getPattern();
if (StringUtils.isNotEmpty(value)) {
sb.append(value).append(',');
}
if (sb.length() == 0) {
continue;
}
productIndexBO.setProductAttrField(sb.substring(0, sb.length() - 1).replaceAll("\\/", ","));
}
}
@Override
public int getOrder() {
return 1;
}
}
... ...
... ... @@ -8,5 +8,10 @@ import java.util.List;
* Created by wangnan on 2017/1/9.
*/
public interface ViewBuilder {
default int getOrder() {
return 0;
}
void build(List<ProductIndexBO> productIndexBOs, List<Integer> ids, List<Integer> skns);
}
... ...