|
@@ -0,0 +1,208 @@
|
|
|
+package com.jkcredit.info.query.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jkcredit.common.constant.CommonConstant;
|
|
|
+import com.jkcredit.common.model.CommonResponseObject;
|
|
|
+import com.jkcredit.info.query.enums.CatalogEnum;
|
|
|
+import com.jkcredit.info.query.service.InformationQueryService;
|
|
|
+import com.jkcredit.info.query.util.CatalogConcurrentHashMapUtil;
|
|
|
+import com.jkcredit.info.query.util.CookieUtil;
|
|
|
+import com.jkcredit.info.query.util.HttpUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.FormBody;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.jsoup.Jsoup;
|
|
|
+import org.jsoup.nodes.Document;
|
|
|
+import org.jsoup.nodes.Element;
|
|
|
+import org.jsoup.select.Elements;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.net.UnknownHostException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author xusonglin
|
|
|
+ * @version V1.0
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class InformationQueryServiceImpl implements InformationQueryService {
|
|
|
+ @Value("${server.port}")
|
|
|
+ private String serverPort;
|
|
|
+ @Value("${supplier.mainPageRequestUrl}")
|
|
|
+ private String mainPageRequestUrl;
|
|
|
+ @Value("${supplier.catalogUrl}")
|
|
|
+ private String catalogUrl;
|
|
|
+ @Value("${supplier.queryResultUrl}")
|
|
|
+ private String queryResultUrl;
|
|
|
+ @Value("${supplier.hostUrl}")
|
|
|
+ private String hostUrl;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonResponseObject query(String keyWord, String type) {
|
|
|
+ String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
|
|
|
+ + UUID.randomUUID().toString();
|
|
|
+ log.info("keyWord:{}, type:{}, requestId:{}", keyWord, type, requestId);
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ String cookie = CookieUtil.getAuthenticationCookie();
|
|
|
+ if (StringUtils.isBlank(cookie)) {
|
|
|
+ CommonResponseObject result = CommonResponseObject.failed("查询失败",
|
|
|
+ CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
|
|
|
+ log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
|
|
|
+ , keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ Map<String, JSONObject> catalogIdMap = CatalogConcurrentHashMapUtil.getInstance();
|
|
|
+ if (catalogIdMap.size() == 0) {
|
|
|
+ // 查询节点id
|
|
|
+ FormBody formBody = new FormBody.Builder()
|
|
|
+ .add("pinyin", "false")
|
|
|
+ .add("keyWord", keyWord)
|
|
|
+ .build();
|
|
|
+ String catalogContent = HttpUtil.doPost(catalogUrl, formBody, cookie);
|
|
|
+ catalogIdMap = getCatalogIds(catalogContent);
|
|
|
+ CatalogConcurrentHashMapUtil.getInstance().putAll(catalogIdMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> detailInfo;
|
|
|
+ String id = "";
|
|
|
+ String queryId = "";
|
|
|
+ for (Map.Entry<String, JSONObject> entry : catalogIdMap.entrySet()) {
|
|
|
+ long detailStartTime = System.currentTimeMillis();
|
|
|
+ String catalogName = entry.getKey();
|
|
|
+ if (type.equals(catalogName)) {
|
|
|
+ JSONObject catalogIdObject = entry.getValue();
|
|
|
+ queryId = catalogIdObject.getString("queryId");
|
|
|
+ id = catalogIdObject.getString("id");
|
|
|
+ } else {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ log.info("detailCostTime:{}", System.currentTimeMillis() - detailStartTime);
|
|
|
+ }
|
|
|
+ String encodeKeyWord = "";
|
|
|
+ try {
|
|
|
+ encodeKeyWord = URLEncoder.encode(keyWord, "utf-8");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ StringBuffer stringBuffer = new StringBuffer(queryResultUrl);
|
|
|
+ stringBuffer.append("?pinyin=false");
|
|
|
+ stringBuffer.append("&keyWord=").append(encodeKeyWord);
|
|
|
+ stringBuffer.append("&reportQuery.id=").append(queryId);
|
|
|
+ stringBuffer.append("&resource.id=").append(id);
|
|
|
+ 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);
|
|
|
+ log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
|
|
|
+ , keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ CommonResponseObject result = CommonResponseObject.noResult(getLocalHost(), requestId);
|
|
|
+ log.info("keyWord:{}, type:{}, requestId:{}, result:{}, costTime:{}"
|
|
|
+ , keyWord, type, requestId, JSON.toJSONString(result), (System.currentTimeMillis() - startTime));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // todo 处理列表多条返回的情况
|
|
|
+ private Map<String, String> getDetailInfo(String content) {
|
|
|
+ Document doc = Jsoup.parse(content);
|
|
|
+ // 获取详情请求url
|
|
|
+ String url = doc.select("a").attr("url").replace("../", hostUrl);
|
|
|
+ if (StringUtils.isBlank(url)) {
|
|
|
+ return new HashMap<>();
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ System.out.println(detailInfo.toString());
|
|
|
+ return detailInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, JSONObject> getCatalogIds(String catalogContent) {
|
|
|
+ // dom解析
|
|
|
+ Document doc = Jsoup.parse(catalogContent);
|
|
|
+ Elements elements = doc.select("li[class=sjzyli]");
|
|
|
+ Map<String, JSONObject> catalogIdMap = new HashMap<>();
|
|
|
+ for (int i = 0; i < elements.size(); i++) {
|
|
|
+ Element element = elements.get(i);
|
|
|
+
|
|
|
+ String name = element.select("li").attr("name");
|
|
|
+ String id = element.select("li").attr("id");
|
|
|
+ String queryId = element.select("li").attr("queryId");
|
|
|
+ JSONObject catalogIdObject = new JSONObject();
|
|
|
+ catalogIdObject.put("id", id);
|
|
|
+ catalogIdObject.put("queryId", queryId);
|
|
|
+ if (name.equals(CatalogEnum.BASIC_INFO.getDesc())) {
|
|
|
+ // 全国人口基本信息
|
|
|
+ catalogIdMap.put(CatalogEnum.BASIC_INFO.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.DRIVER_INFO.getDesc())) {
|
|
|
+ // 全国驾驶人
|
|
|
+ catalogIdMap.put(CatalogEnum.DRIVER_INFO.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.STOLEN_VEHICLE_INFO.getDesc())) {
|
|
|
+ // 被盗抢车辆
|
|
|
+ catalogIdMap.put(CatalogEnum.STOLEN_VEHICLE_INFO.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.IMMIGRATION_INFO.getDesc())) {
|
|
|
+ // 出入境证件
|
|
|
+ catalogIdMap.put(CatalogEnum.IMMIGRATION_INFO.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.ILLEGAL_INFO.getDesc())) {
|
|
|
+ // 违法犯罪人员
|
|
|
+ catalogIdMap.put(CatalogEnum.ILLEGAL_INFO.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.ESCAPED_CRIMINAL_INFO.getDesc())) {
|
|
|
+ // 全国在逃人员
|
|
|
+ catalogIdMap.put(CatalogEnum.ESCAPED_CRIMINAL_INFO.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.MOVEMENT_RECORD.getDesc())) {
|
|
|
+ // 出入境记录
|
|
|
+ catalogIdMap.put(CatalogEnum.MOVEMENT_RECORD.getValue(), catalogIdObject);
|
|
|
+ } else if (name.equals(CatalogEnum.VEHICLE_INFO.getDesc())) {
|
|
|
+ // 全国机动车登记
|
|
|
+ catalogIdMap.put(CatalogEnum.VEHICLE_INFO.getValue(), catalogIdObject);
|
|
|
+ } else {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return catalogIdMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CommonResponseObject loadCookie() {
|
|
|
+ String requestId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())
|
|
|
+ + UUID.randomUUID().toString();
|
|
|
+ String cookie = CookieUtil.getAuthenticationCookie();
|
|
|
+ if (StringUtils.isBlank(cookie)) {
|
|
|
+ return CommonResponseObject.failed("查询失败", CommonConstant.NETWORK_DISABLED, getLocalHost(), requestId);
|
|
|
+ } else {
|
|
|
+ return CommonResponseObject.success(new HashMap<>(), getLocalHost(), requestId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getLocalHost() {
|
|
|
+ InetAddress address = null;
|
|
|
+ try {
|
|
|
+ address = InetAddress.getLocalHost();
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ if (address != null) {
|
|
|
+ return address.getHostAddress() + ":" + this.serverPort;
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|