|
@@ -6,6 +6,7 @@ import com.jkcredit.common.constant.CommonConstant;
|
|
import com.jkcredit.common.model.CommonResponseObject;
|
|
import com.jkcredit.common.model.CommonResponseObject;
|
|
import com.jkcredit.info.query.enums.CatalogEnum;
|
|
import com.jkcredit.info.query.enums.CatalogEnum;
|
|
import com.jkcredit.info.query.service.InformationQueryService;
|
|
import com.jkcredit.info.query.service.InformationQueryService;
|
|
|
|
+import com.jkcredit.info.query.task.AsyncQuery;
|
|
import com.jkcredit.info.query.util.CatalogConcurrentHashMapUtil;
|
|
import com.jkcredit.info.query.util.CatalogConcurrentHashMapUtil;
|
|
import com.jkcredit.info.query.util.CookieUtil;
|
|
import com.jkcredit.info.query.util.CookieUtil;
|
|
import com.jkcredit.info.query.util.HttpUtil;
|
|
import com.jkcredit.info.query.util.HttpUtil;
|
|
@@ -16,15 +17,17 @@ import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.select.Elements;
|
|
import org.jsoup.select.Elements;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
-import java.net.InetAddress;
|
|
|
|
-import java.net.URLEncoder;
|
|
|
|
-import java.net.UnknownHostException;
|
|
|
|
|
|
+import java.net.*;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.concurrent.CountDownLatch;
|
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
|
+import java.util.concurrent.Future;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author xusonglin
|
|
* @author xusonglin
|
|
@@ -43,6 +46,8 @@ public class InformationQueryServiceImpl implements InformationQueryService {
|
|
private String queryResultUrl;
|
|
private String queryResultUrl;
|
|
@Value("${supplier.hostUrl}")
|
|
@Value("${supplier.hostUrl}")
|
|
private String hostUrl;
|
|
private String hostUrl;
|
|
|
|
+ @Autowired
|
|
|
|
+ AsyncQuery asyncQuery;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public CommonResponseObject query(String keyWord, String type) {
|
|
public CommonResponseObject query(String keyWord, String type) {
|
|
@@ -70,11 +75,9 @@ public class InformationQueryServiceImpl implements InformationQueryService {
|
|
CatalogConcurrentHashMapUtil.getInstance().putAll(catalogIdMap);
|
|
CatalogConcurrentHashMapUtil.getInstance().putAll(catalogIdMap);
|
|
}
|
|
}
|
|
|
|
|
|
- Map<String, String> detailInfo;
|
|
|
|
String id = "";
|
|
String id = "";
|
|
String queryId = "";
|
|
String queryId = "";
|
|
for (Map.Entry<String, JSONObject> entry : catalogIdMap.entrySet()) {
|
|
for (Map.Entry<String, JSONObject> entry : catalogIdMap.entrySet()) {
|
|
- long detailStartTime = System.currentTimeMillis();
|
|
|
|
String catalogName = entry.getKey();
|
|
String catalogName = entry.getKey();
|
|
if (type.equals(catalogName)) {
|
|
if (type.equals(catalogName)) {
|
|
JSONObject catalogIdObject = entry.getValue();
|
|
JSONObject catalogIdObject = entry.getValue();
|
|
@@ -83,7 +86,6 @@ public class InformationQueryServiceImpl implements InformationQueryService {
|
|
} else {
|
|
} else {
|
|
continue;
|
|
continue;
|
|
}
|
|
}
|
|
- log.info("detailCostTime:{}", System.currentTimeMillis() - detailStartTime);
|
|
|
|
}
|
|
}
|
|
String encodeKeyWord = "";
|
|
String encodeKeyWord = "";
|
|
try {
|
|
try {
|
|
@@ -97,10 +99,10 @@ public class InformationQueryServiceImpl implements InformationQueryService {
|
|
stringBuffer.append("&reportQuery.id=").append(queryId);
|
|
stringBuffer.append("&reportQuery.id=").append(queryId);
|
|
stringBuffer.append("&resource.id=").append(id);
|
|
stringBuffer.append("&resource.id=").append(id);
|
|
String resultContent = HttpUtil.doGet(stringBuffer.toString(), cookie);
|
|
String resultContent = HttpUtil.doGet(stringBuffer.toString(), cookie);
|
|
- detailInfo = getDetailInfo(resultContent);
|
|
|
|
- log.info("totalCostTime:{}", (System.currentTimeMillis() - startTime));
|
|
|
|
- if (detailInfo != null && detailInfo.size() != 0) {
|
|
|
|
- CommonResponseObject result = CommonResponseObject.success(detailInfo, getLocalHost(), requestId);
|
|
|
|
|
|
+ List<Map<String, String>> detailInfos = getDetailInfo(resultContent);
|
|
|
|
+
|
|
|
|
+ if (detailInfos.size() != 0) {
|
|
|
|
+ CommonResponseObject result = CommonResponseObject.success(detailInfos, getLocalHost(), requestId);
|
|
log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
|
|
log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
|
|
, keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
|
|
, keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
|
|
return result;
|
|
return result;
|
|
@@ -112,27 +114,49 @@ public class InformationQueryServiceImpl implements InformationQueryService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // todo 处理列表多条返回的情况
|
|
|
|
- private Map<String, String> getDetailInfo(String content) {
|
|
|
|
|
|
+ // 处理列表多条返回的情况
|
|
|
|
+ private List<Map<String, String>> getDetailInfo(String content) {
|
|
|
|
+ List<Map<String, String>> results = new ArrayList<>();
|
|
|
|
+
|
|
Document doc = Jsoup.parse(content);
|
|
Document doc = Jsoup.parse(content);
|
|
// 获取详情请求url
|
|
// 获取详情请求url
|
|
- String url = doc.select("a").attr("url").replace("../", hostUrl);
|
|
|
|
- if (StringUtils.isBlank(url)) {
|
|
|
|
- return new HashMap<>();
|
|
|
|
|
|
+ List<String> urls = doc.select("a").eachAttr("url");
|
|
|
|
+ CountDownLatch countDownLatch = new CountDownLatch(urls.size());
|
|
|
|
+ List<String> detailContents = new ArrayList<>();
|
|
|
|
+ for (String url : urls) {
|
|
|
|
+ url = url.replace("../", hostUrl);
|
|
|
|
+ try {
|
|
|
|
+ Future<String> detailInfo = asyncQuery.detailInfoQuery(url);
|
|
|
|
+ String detailInfoContent = detailInfo.get();
|
|
|
|
+ detailContents.add(detailInfoContent);
|
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
|
+ log.error("getDetailInfo.InterruptedException:", ie);
|
|
|
|
+ } catch (ExecutionException e) {
|
|
|
|
+ log.error("getDetailInfo.ExecutionException:", e);
|
|
|
|
+ } finally {
|
|
|
|
+ countDownLatch.countDown();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- System.out.println(url);
|
|
|
|
- // 请求详情信息
|
|
|
|
- String detailInfoContent = HttpUtil.doGet(url, CookieUtil.getAuthenticationCookie());
|
|
|
|
- Document detailDoc = Jsoup.parse(detailInfoContent);
|
|
|
|
- Elements elements = detailDoc.select(".detail-info-list");
|
|
|
|
- Map<String, String> detailInfo = new HashMap<>();
|
|
|
|
- for (int i = 0; i < elements.size(); i++) {
|
|
|
|
- String dataName = detailDoc.select(".detail-info-list").get(i).select("span").get(1).attr("data-name");
|
|
|
|
- String dataValue = detailDoc.select(".detail-info-list").get(i).select("span").get(1).text();
|
|
|
|
- detailInfo.put(dataName, dataValue);
|
|
|
|
|
|
+ try {
|
|
|
|
+ countDownLatch.await();
|
|
|
|
+ } catch (InterruptedException ie) {
|
|
|
|
+ log.error("getDetailInfo.InterruptedException:", ie);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (String detailInfoContent : detailContents) {
|
|
|
|
+ Document detailDoc = Jsoup.parse(detailInfoContent);
|
|
|
|
+ Elements elements = detailDoc.select(".detail-info-list");
|
|
|
|
+ Map<String, String> detailMap = new HashMap<>();
|
|
|
|
+ for (int i = 0; i < elements.size(); i++) {
|
|
|
|
+ String dataName = detailDoc.select(".detail-info-list").get(i).select("span").get(1).attr("data-name");
|
|
|
|
+ String dataValue = detailDoc.select(".detail-info-list").get(i).select("span").get(1).text();
|
|
|
|
+ detailMap.put(dataName, dataValue);
|
|
|
|
+ }
|
|
|
|
+ if (detailMap.size() != 0) {
|
|
|
|
+ results.add(detailMap);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- System.out.println(detailInfo.toString());
|
|
|
|
- return detailInfo;
|
|
|
|
|
|
+ return results;
|
|
}
|
|
}
|
|
|
|
|
|
private Map<String, JSONObject> getCatalogIds(String catalogContent) {
|
|
private Map<String, JSONObject> getCatalogIds(String catalogContent) {
|
|
@@ -188,7 +212,7 @@ public class InformationQueryServiceImpl implements InformationQueryService {
|
|
if (StringUtils.isBlank(cookie)) {
|
|
if (StringUtils.isBlank(cookie)) {
|
|
return CommonResponseObject.failed("查询失败", CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
|
|
return CommonResponseObject.failed("查询失败", CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
|
|
} else {
|
|
} else {
|
|
- return CommonResponseObject.success(new HashMap<>(), getLocalHost(), requestId);
|
|
|
|
|
|
+ return CommonResponseObject.success(new ArrayList<>(), getLocalHost(), requestId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|