|
|
package com.yoho.search.consumer.index.increment.flow;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.ISearchConstants;
|
|
|
import com.yoho.search.consumer.index.common.IYohoIndexService;
|
|
|
import com.yoho.search.consumer.index.rebuild.RebuildFlagService;
|
|
|
import com.yoho.search.consumer.service.base.ProductService;
|
|
|
import com.yoho.search.consumer.suggests.common.RetryBusinessFlow;
|
|
|
import com.yoho.search.core.es.IElasticsearchClient;
|
|
|
import com.yoho.search.core.es.model.ESBluk;
|
|
|
import com.yoho.search.dal.ProductHeatValueMapper;
|
|
|
import com.yoho.search.dal.model.Product;
|
|
|
import com.yoho.search.dal.model.ProductHeatValue;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.elasticsearch.action.bulk.BulkResponse;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* Created by wangnan on 2017/4/10.
|
|
|
*/
|
|
|
@Component
|
|
|
public class ProductIndexHeatValueUpdateFlow implements RetryBusinessFlow {
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger("FLOW_EXECUTOR");
|
|
|
|
|
|
@Autowired
|
|
|
private RebuildFlagService rebuildFlagService;
|
|
|
@Autowired
|
|
|
private ProductHeatValueMapper productHeatValueMapper;
|
|
|
@Autowired
|
|
|
private ProductService productService;
|
|
|
@Autowired
|
|
|
private IYohoIndexService yohoIndexService;
|
|
|
|
|
|
@Override
|
|
|
public String flowName() {
|
|
|
return this.getClass().getSimpleName();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void init() {
|
|
|
rebuildFlagService.waitingRebuildingIndex();
|
|
|
rebuildFlagService.updateIsBuildingTrue();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int getTotalCount() {
|
|
|
return productHeatValueMapper.count(productHeatValueMapper.selectLatestDateId());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean doBusiness(int pageNo, int batchSize) {
|
|
|
int start = (pageNo - 1) * batchSize;
|
|
|
List<ProductHeatValue> productHeatValueList = productHeatValueMapper.selectHeatValueOfLatestDate(productHeatValueMapper.selectLatestDateId(),start, batchSize);
|
|
|
if (CollectionUtils.isEmpty(productHeatValueList)) {
|
|
|
return true;
|
|
|
}
|
|
|
List<Integer> sknList = productHeatValueList.stream().map(ProductHeatValue::getProductSkn).collect(Collectors.toList());
|
|
|
List<Product> productList = productService.selectListSkns(sknList);
|
|
|
Map<Integer, Integer> skn2IDMap = productList.stream().collect(Collectors.toMap(Product::getErpProductId, Product::getId));
|
|
|
logger.info("[{} business][pageNo={}][productHeatValueList={}]", flowName(), pageNo, productHeatValueList.size());
|
|
|
List<JSONObject> dataList = new ArrayList<JSONObject>();
|
|
|
productHeatValueList.stream().forEach((p)->{
|
|
|
if (skn2IDMap.get(p.getProductSkn()) != null) {
|
|
|
JSONObject product = new JSONObject();
|
|
|
product.put("id", skn2IDMap.get(p.getProductSkn()));
|
|
|
product.put("heatValue", p.getHeatValue());
|
|
|
dataList.add(product);
|
|
|
}
|
|
|
});
|
|
|
if (CollectionUtils.isEmpty(dataList)) {
|
|
|
return true;
|
|
|
}
|
|
|
final String indexName = ISearchConstants.INDEX_NAME_PRODUCT_INDEX;
|
|
|
List<ESBluk> bluks = new ArrayList<>(dataList.size());
|
|
|
for (JSONObject product : dataList) {
|
|
|
bluks.add(new ESBluk(product.toJSONString(), product.getInteger("id").toString(), indexName, indexName, false));
|
|
|
}
|
|
|
IElasticsearchClient client = yohoIndexService.getIndex(indexName).getIndexClient();
|
|
|
BulkResponse bulkResponse = client.bulk(bluks);
|
|
|
if (bulkResponse.hasFailures()) {
|
|
|
throw new RuntimeException(
|
|
|
String.format("bulk has failure,[yohoIndexName=[%s]],[pageNo=%s],[failureMessage=%s]", indexName, pageNo, bulkResponse.buildFailureMessage()));
|
|
|
}
|
|
|
logger.info("[{} business][pageNo={}][message=bulk succeed]", flowName(), pageNo);
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void finish(boolean doBusinessResult, Exception exception) {
|
|
|
logger.info("[{} business][doBusinessResult={}][exception={}]", flowName(), doBusinessResult, exception);
|
|
|
rebuildFlagService.updateIsBuildingFalse();
|
|
|
}
|
|
|
} |
...
|
...
|
|