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 2ae672241..27942a8b1 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 @@ -24,6 +24,7 @@ import com.fangjia.open.model.*; import net.sf.json.JSONArray; import net.sf.json.JSONObject; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.lucene.index.Term; @@ -85,7 +86,6 @@ import com.fangjia.service.mongo.fangjia.ServerAreaService; import com.fangjia.service.open.WaitCrawlKeywordsService; import com.fangjia.service.sql.FjtEvaluationService; import com.fangjia.service.web.AddressService; -import org.sothis.web.mvc.annotation.Param; @Authority(role = Role.USER) public class AddressController { @@ -116,6 +116,8 @@ public class AddressController { private static final String GRADE_CONSUME = "消费评分"; public final static OpenConfig CONFIG = OpenConfig.getConfig(); + private static final String ES_USER = "developer"; + private static final String ES_PASSWORD = "5e8edd851d2fdfbd7415232c67367cc3"; /** * @deprecated see parse2Action @@ -522,7 +524,57 @@ public class AddressController { MatchDistrictResult matchResult = addressMatchProxyService.checkMatchDistrictCorrect(param); jsonResult.put("matchResult", matchResult); } else if (jsonResult != null) { - jsonResult.put("matchResult", new MatchDistrictResult()); + // 判断是否为自然村 + boolean matchedVillage = false; + try { + if (jsonResult.get("searchAddress") != null) { + JSONObject searchAddress = (JSONObject) jsonResult.get("searchAddress"); + String city = searchAddress.containsKey("city") ? searchAddress.getString("city") : null; + String region = searchAddress.containsKey("district") ? searchAddress.getString("district") : null; + String name = searchAddress.containsKey("keywords") ? searchAddress.getString("keywords") : null; + String township = searchAddress.containsKey("township") ? searchAddress.getString("township") : null; + if (StringUtils.isNotBlank(city) && StringUtils.isNotBlank(region) && StringUtils.isNotBlank(name)) { + List villages = findVillage(city, region, name); + if (CollectionUtils.isNotEmpty(villages)) { + if (villages.size() == 1 || StringUtils.isBlank(township)) { + JSONObject village = villages.get(0).getJSONObject("_source"); + jsonResult.put("id", villages.get(0).getString("_id")); + jsonResult.put("province", village.getString("province")); + jsonResult.put("city", village.getString("city")); + jsonResult.put("district", village.getString("region")); + jsonResult.put("township", village.getString("township")); + jsonResult.put("name", village.getString("name")); + jsonResult.put("lng", village.getJSONObject("location").getDouble("lon")); + jsonResult.put("lat", village.getJSONObject("location").getDouble("lat")); + jsonResult.put("propertyType", "自然村"); + matchedVillage = true; + } else { + for (JSONObject source : villages) { + JSONObject village = source.getJSONObject("_source"); + if (village.getString("township").contains(township) || township.contains(village.getString("township"))) { + jsonResult.put("id", source.getString("_id")); + jsonResult.put("province", village.getString("province")); + jsonResult.put("city", village.getString("city")); + jsonResult.put("district", village.getString("region")); + jsonResult.put("township", village.getString("township")); + jsonResult.put("name", village.getString("name")); + jsonResult.put("lng", village.getJSONObject("location").getDouble("lon")); + jsonResult.put("lat", village.getJSONObject("location").getDouble("lat")); + jsonResult.put("propertyType", "自然村"); + matchedVillage = true; + break; + } + } + } + } + } + } + } catch(Exception ex) { + ex.printStackTrace(); + } + if (matchedVillage == false) { + jsonResult.put("matchResult", new MatchDistrictResult()); + } } resultModel.setResult(json.get("result")); mav.setModel(resultModel); @@ -918,6 +970,60 @@ public class AddressController { return ret; } + private JSONObject addTerm(String name, String value){ + JSONObject term = new JSONObject(); + JSONObject match = new JSONObject(); + match.put(name, value); + term.put("term", match); + return term; + } + + /*** + * 获取自然村 + * @param city + * @param region + * @param name + * @return + */ + private List findVillage(String city, String region, String name) { + JSONObject reqBody = new JSONObject(); + reqBody.put("from", 0); + reqBody.put("size", 5); + JSONObject query = new JSONObject(); + JSONObject bool = new JSONObject(); + JSONArray must = new JSONArray(); + must.add(addTerm("city", city)); + must.add(addTerm("region", region)); + must.add(addTerm("name", name)); + 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/village/_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(); + } + /** * 加载测试数据 *