|
|
package com.yoho.search.consumer.index.fullbuild.tbl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.base.utils.HttpClientUtils;
|
|
|
import com.yoho.search.base.utils.MD5Util;
|
|
|
import com.yoho.search.consumer.index.fullbuild.IIndexBuilder;
|
|
|
import com.yoho.search.consumer.service.logicService.common.ConsumerConfiger;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 全球购索引构建器(通过调用外部全球购接口的方式重建)
|
|
|
*/
|
|
|
@Component
|
|
|
public class TblProductIndexBuilder extends IIndexBuilder {
|
|
|
|
|
|
private final Logger INDEX_REBUILD_LOG = LoggerFactory.getLogger("INDEX_REBULDER");
|
|
|
|
|
|
@Autowired
|
|
|
private ConsumerConfiger configurer;
|
|
|
|
|
|
@Override
|
|
|
public int getTotalCount() throws Exception {
|
|
|
return getCount();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<?> getPageLists(int offset, int limit) throws Exception {
|
|
|
return getPageList(offset, limit);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 调接口建索引:获取总数量
|
|
|
*/
|
|
|
private int getCount() {
|
|
|
StringBuilder countUrl = new StringBuilder();
|
|
|
countUrl.append(configurer.getTblProductUrl());
|
|
|
countUrl.append("getproductscountforsearch?client_type=iphone&client_secret=");
|
|
|
String md5 = MD5Util.string2MD5("client_type=iphone&private_key=" + configurer.getTblPrimaryKey());
|
|
|
countUrl.append(md5);
|
|
|
INDEX_REBUILD_LOG.info("TblProductIndexBuilder getCount is [{}]", countUrl.toString());
|
|
|
String countJson = HttpClientUtils.getMethod(countUrl.toString());
|
|
|
if (StringUtils.isNotBlank(countJson)) {
|
|
|
JSONObject jsonObject = JSON.parseObject(countJson);
|
|
|
String count = jsonObject.get("data").toString();
|
|
|
if (StringUtils.isNotBlank(count)) {
|
|
|
int totalCount = Integer.parseInt(count);
|
|
|
return totalCount;
|
|
|
}
|
|
|
}
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 调接口建索引:调接口获取数据
|
|
|
*/
|
|
|
private List<Map> getPageList(int start, int limit) {
|
|
|
StringBuilder url = new StringBuilder();
|
|
|
url.append(configurer.getTblProductUrl())
|
|
|
.append("getproductslistforsearch?client_type=iphone")
|
|
|
.append("&limit=")
|
|
|
.append(limit)
|
|
|
.append("&offset=")
|
|
|
.append(start);
|
|
|
StringBuilder param = new StringBuilder();
|
|
|
param.append("client_type=iphone")
|
|
|
.append("&limit=")
|
|
|
.append(limit)
|
|
|
.append("&offset=")
|
|
|
.append(start)
|
|
|
.append("&private_key=")
|
|
|
.append(configurer.getTblPrimaryKey());
|
|
|
String md5 = MD5Util.string2MD5(param.toString());
|
|
|
url.append("&client_secret=").append(md5);
|
|
|
INDEX_REBUILD_LOG.info("TblProductIndexBuilder getPageList is [{}]", url.toString());
|
|
|
String json = HttpClientUtils.getMethod(url.toString());
|
|
|
if (StringUtils.isBlank(json)) {
|
|
|
return null;
|
|
|
}
|
|
|
JSONObject jsonObject = JSON.parseObject(json);
|
|
|
String data = jsonObject.getString("data");
|
|
|
if (StringUtils.isBlank(data)) {
|
|
|
return null;
|
|
|
}
|
|
|
return JSONArray.parseArray(jsonObject.getString("data"), Map.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getId(Object object) {
|
|
|
return UUID.randomUUID().toString();
|
|
|
}
|
|
|
|
|
|
} |