From 532503a7e345e0a266f2b8ec19cd63756c2c9feb Mon Sep 17 00:00:00 2001 From: "shenjian@fangjia.com" Date: Mon, 29 May 2023 11:09:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B5=99=E5=95=86=E9=A3=8E?= =?UTF-8?q?=E9=99=A9=E8=AF=86=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../open/controllers/AddressController.java | 82 +++++++++++++++---- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddressController.java b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddressController.java index 3b9563cbe..ad2c3adbd 100644 --- a/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddressController.java +++ b/projects/fangjia-open/src/main/java/com/fangjia/open/controllers/AddressController.java @@ -87,7 +87,7 @@ import com.fangjia.service.open.WaitCrawlKeywordsService; import com.fangjia.service.sql.FjtEvaluationService; import com.fangjia.service.web.AddressService; -@Authority(role = Role.USER) +//@Authority(role = Role.USER) public class AddressController { private AddressService addressService; @@ -985,6 +985,14 @@ public class AddressController { return term; } + private JSONObject addMatchPhrase(String name, String value){ + JSONObject term = new JSONObject(); + JSONObject match = new JSONObject(); + match.put(name, value); + term.put("match_phrase", match); + return term; + } + /*** * 获取自然村 * @param city @@ -3279,7 +3287,7 @@ public class AddressController { return mav; } - @Authority(role = Role.USER) + //@Authority(role = Role.USER) public Object riskAction(AddesssMatchModel model) { RestfulModelAndView mav = new RestfulModelAndView(); // if (StringUtils.isEmpty(model.getCity())) { @@ -3322,20 +3330,26 @@ public class AddressController { searchKey.append(searchAddress.getString("streetNumber")); } if (StringUtils.isNotBlank(searchKey.toString()) && searchAddress.has("city") && StringUtils.isNotBlank(searchAddress.getString("city"))) { - JSONObject jsonObject = addressMatchProxyService.searchByAmap(searchAddress.getString("city"), model.getAddress()); - JSONArray poisData = jsonObject.getJSONArray("pois"); - for (int i = 0; i < poisData.size(); i++) { - String address = poisData.getJSONObject(i).getString("address"); - if (address.contains(searchKey.toString())) { - resultMap.put("riskLevel", "A"); - resultModel.setResult(resultMap); - mav.setModel(resultModel); - return mav; - } else { - continue; - } +// JSONObject jsonObject = addressMatchProxyService.searchByAmap(searchAddress.getString("city"), model.getAddress()); +// JSONArray poisData = jsonObject.getJSONArray("pois"); +// for (int i = 0; i < poisData.size(); i++) { +// String address = poisData.getJSONObject(i).getString("address"); +// if (address.contains(searchKey.toString())) { +// resultMap.put("riskLevel", "A"); +// resultModel.setResult(resultMap); +// mav.setModel(resultModel); +// return mav; +// } else { +// continue; +// } +// } + String city = searchAddress.getString("city"); + List poiList = this.findPois(city, searchKey.toString()); + if (CollectionUtils.isNotEmpty(poiList)) { + resultMap.put("riskLevel", "A"); + } else { + resultMap.put("riskLevel", "B"); } - resultMap.put("riskLevel", "B"); resultModel.setResult(resultMap); mav.setModel(resultModel); return mav; @@ -3352,6 +3366,44 @@ public class AddressController { return mav; } + private List findPois(String city, String address) { + JSONObject reqBody = new JSONObject(); + reqBody.put("from", 0); + reqBody.put("size", 1); + JSONObject query = new JSONObject(); + JSONObject bool = new JSONObject(); + JSONArray must = new JSONArray(); + must.add(addTerm("city", city)); + must.add(addMatchPhrase("address", address)); + bool.put("must", must); + query.put("bool", bool); + reqBody.put("query", query); + Base64 token = new Base64(); + String authenticationEncoding = token.encodeAsString((""+ES_USER+":"+ES_PASSWORD+"").getBytes()); + HttpRequest request = new HttpRequest(); + Map headers = new HashMap(); + headers.put("Content-Type", "application/json"); + headers.put("Authorization", "Basic "+authenticationEncoding+""); + try { + String response = request.postWithBody("http://192.168.0.45:9200/association/_search", reqBody, headers, "UTF-8"); + JSONObject json = JSONObject.fromObject(response); + if(!json.containsKey("hits") || !json.getJSONObject("hits").containsKey("hits")){ + return Collections.emptyList(); + } + JSONArray hits = json.getJSONObject("hits").getJSONArray("hits"); + List results = new ArrayList(); + for(int i=0 ;i < hits.size(); i++){ + JSONObject jsonObject = hits.getJSONObject(i); + results.add(jsonObject); + } + return results; + + } catch (HttpRequestException e) { + e.printStackTrace(); + } + return Collections.emptyList(); + } + public void setAddressService(AddressService addressService) { this.addressService = addressService; }