| | |
| | | |
| | | import com.baomidou.mybatisplus.mapper.EntityWrapper; |
| | | import com.baomidou.mybatisplus.service.impl.ServiceImpl; |
| | | import com.stylefeng.guns.core.util.ToolUtil; |
| | | import com.stylefeng.guns.modular.crossCity.dao.LocationMapper; |
| | | import com.stylefeng.guns.modular.crossCity.dao.SiteMapper; |
| | | import com.stylefeng.guns.modular.crossCity.model.Site; |
| | | import com.stylefeng.guns.modular.crossCity.server.ISiteService; |
| | | import com.stylefeng.guns.modular.crossCity.warpper.SiteWarpper; |
| | | import com.stylefeng.guns.modular.system.util.GDMapElectricFenceUtil; |
| | | import com.stylefeng.guns.modular.system.util.model.GeoFencingPolygon; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.ObjectProvider; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.geo.GeoJsonPoint; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.*; |
| | |
| | | private LocationMapper locationMapper; |
| | | |
| | | @Autowired |
| | | private MongoTemplate mongoTemplate; |
| | | private ObjectProvider<MongoTemplate> mongoTemplateProvider; |
| | | |
| | | @Autowired |
| | | private RestTemplate restTemplate; |
| | | |
| | | private static final String AMAP_KEY = "6dd64582ed31b84134ac38b2355eb82b"; |
| | | |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | if(Integer.valueOf(map.get("type").toString()) == 2){//电子围栏 |
| | | String[] split = lonLat.split(","); |
| | | GeoJsonPoint point = new GeoJsonPoint(Double.valueOf(split[0]), Double.valueOf(split[1])); |
| | | Query query = Query.query(Criteria.where("geoJsonPolygon").intersects(point)); |
| | | List<GeoFencingPolygon> geoFencingPolygons = mongoTemplate.find(query, GeoFencingPolygon.class); |
| | | List<Integer> s_sites = geoFencingPolygons.stream().map(GeoFencingPolygon::getSiteId).collect(Collectors.toList()); |
| | | if(s_sites.contains(siteId)){ |
| | | if(matchAreaType2(siteId, lonLat)){ |
| | | return true; |
| | | } |
| | | } |
| | |
| | | public List<Map<String, Object>> queryLocation(Integer siteId) throws Exception { |
| | | return locationMapper.queryLocation(siteId); |
| | | } |
| | | |
| | | /** |
| | | * 根据经纬度查询所在站点(命中多个时返回第一个) |
| | | */ |
| | | @Override |
| | | public Site querySiteByLonLat(Double lon, Double lat) throws Exception { |
| | | if (lon == null || lat == null) { |
| | | return null; |
| | | } |
| | | String adCode = getAdCodeByLonLat(lon, lat); |
| | | String lonLat = lon + "," + lat; |
| | | List<Site> siteList = this.selectList(new EntityWrapper<Site>().eq("state", 1).orderBy("id", true)); |
| | | if (siteList == null || siteList.size() <= 0) { |
| | | return null; |
| | | } |
| | | for (Site site : siteList) { |
| | | if (site == null || site.getId() == null) { |
| | | continue; |
| | | } |
| | | List<Map<String, Object>> locations = queryLocation(site.getId()); |
| | | if (locations == null || locations.size() <= 0) { |
| | | continue; |
| | | } |
| | | if (matchAreaType1(locations, adCode) || matchAreaType2(site.getId(), lonLat)) { |
| | | return site; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private boolean matchAreaType1(List<Map<String, Object>> locations, String adCode) { |
| | | if (ToolUtil.isEmpty(adCode)) { |
| | | return false; |
| | | } |
| | | if (adCode.length() < 4) { |
| | | return false; |
| | | } |
| | | String province = adCode.substring(0, 2) + "0000"; |
| | | String city = adCode.substring(0, 4) + "00"; |
| | | for (Map<String, Object> map : locations) { |
| | | if (map == null || map.get("type") == null) { |
| | | continue; |
| | | } |
| | | if (Integer.valueOf(map.get("type").toString()) != 1) { |
| | | continue; |
| | | } |
| | | if (map.get("districtCode") != null && adCode.equals(map.get("districtCode").toString())) { |
| | | return true; |
| | | } |
| | | if (map.get("cityCode") != null && city.equals(map.get("cityCode").toString())) { |
| | | return true; |
| | | } |
| | | if (map.get("provinceCode") != null && province.equals(map.get("provinceCode").toString())) { |
| | | return true; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | private boolean matchAreaType2(Integer siteId, String lonLat) { |
| | | MongoTemplate mongoTemplate; |
| | | try { |
| | | mongoTemplate = mongoTemplateProvider.getIfAvailable(); |
| | | } catch (Exception ex) { |
| | | return false; |
| | | } |
| | | if (mongoTemplate == null || ToolUtil.isEmpty(lonLat)) { |
| | | return false; |
| | | } |
| | | String[] split = lonLat.split(","); |
| | | if (split.length != 2) { |
| | | return false; |
| | | } |
| | | GeoJsonPoint point = new GeoJsonPoint(Double.valueOf(split[0]), Double.valueOf(split[1])); |
| | | Query query = Query.query(Criteria.where("geoJsonPolygon").intersects(point)); |
| | | List<GeoFencingPolygon> geoFencingPolygons = mongoTemplate.find(query, GeoFencingPolygon.class); |
| | | if (geoFencingPolygons == null || geoFencingPolygons.size() <= 0) { |
| | | return false; |
| | | } |
| | | List<Integer> siteIds = geoFencingPolygons.stream().map(GeoFencingPolygon::getSiteId).collect(Collectors.toList()); |
| | | return siteIds.contains(siteId); |
| | | } |
| | | |
| | | private String getAdCodeByLonLat(Double lon, Double lat) { |
| | | try { |
| | | String location = lon + "," + lat; |
| | | String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + AMAP_KEY + "&location=" + location; |
| | | String body = restTemplate.getForObject(url, String.class); |
| | | if (ToolUtil.isEmpty(body)) { |
| | | return null; |
| | | } |
| | | com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSON.parseObject(body); |
| | | if (!"1".equals(jsonObject.getString("status"))) { |
| | | return null; |
| | | } |
| | | com.alibaba.fastjson.JSONObject regeocode = jsonObject.getJSONObject("regeocode"); |
| | | if (regeocode == null) { |
| | | return null; |
| | | } |
| | | com.alibaba.fastjson.JSONObject addressComponent = regeocode.getJSONObject("addressComponent"); |
| | | if (addressComponent == null) { |
| | | return null; |
| | | } |
| | | return addressComponent.getString("adcode"); |
| | | } catch (Exception ex) { |
| | | return null; |
| | | } |
| | | } |
| | | } |