suerprisePlus
2024-09-03 2e9e03c7971f1da71cc9e9bd7d2f159c244839f3
src/main/java/com/yb/controller/XzController.java
@@ -1,26 +1,16 @@
package com.yb.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yb.config.R;
import com.yb.config.XzConfig;
import com.yb.entity.ReqEntity;
import com.yb.helper.RsaHelper;
import com.yb.util.EntityHttpUtil;
import com.yb.service.XzService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.WKTWriter;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.stringtemplate.v4.ST;
import java.util.*;
@@ -32,42 +22,27 @@
    @Autowired
    WebSocketController webSocketController;
    @Autowired
    private EntityHttpUtil entityHttpUtil;
    @Value("${spring.geographical.canview}")
    String canview;
    @Value("${spring.geographical.entityName}")
    String entityName;
    @Value("${spring.geographical.dbid}")
    String dbid;
    @Value("${spring.geographical.layerId}")
    String layerId;
    @Value("${spring.geographical.queryEntity}")
    String queryEntity;
    @Value("${spring.geographical.publickey}")
    String publickey;
    @Value("${spring.geographical.token}")
    String token;
    private XzService xzService;
    @Autowired
    private XzConfig xzConfig;
    @PostMapping("/poiMap")
    @Operation(summary = "显示地点")
    public R poiMap(@RequestParam Map<String, String> allParams) throws Exception {
        String place = allParams.get("place");
        String mid = allParams.get("mid");
        System.out.println("rec poiMap:" + place + "  mid:" + mid);
        if (place == null || mid == null) {
            return R.error("数据传递错误");
        }
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "poiMap");
        hash.put("mid", mid);
        HashMap<String, Object> hash = xzService.getFuncMap("poiMap", mid);
        ReqEntity reqEntity = new ReqEntity();
        String item = getQueryFiled(place);
        String filed = "enti_name";
        String item = xzService.getQueryFiled(place, filed, xzConfig.layerId, xzConfig.dbid, xzConfig.queryEntity);
        System.out.println("rec poiMap:" + reqEntity);
        JSONObject obj = JSONObject.parseObject(item);
        reqEntity.setX(obj.getString("lon"));
        reqEntity.setY(obj.getString("lat"));
        reqEntity.setPlace(obj.getString("enti_name"));
        System.out.println("rec poiMap:" + reqEntity);
        reqEntity.setPlace(obj.getString(filed));
        hash.put("poi", reqEntity);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
@@ -77,31 +52,27 @@
    @PostMapping("/aroundPoi")
    @Operation(summary = "周边属性查询")
    public R aroundPoi(@RequestParam Map<String, String> allParams) throws Exception {
        System.out.println("rec aroundPoi:" + allParams);
        String place = allParams.get("place");
        String mid = allParams.get("mid");
        String type = allParams.get("type");
        String dis = allParams.get("radius");
        int radius= 0;
        if(dis != null){
            radius= Integer.parseInt(dis);
        int radius = 0;
        if (dis != null) {
            radius = Integer.parseInt(dis);
        }
//        int radius = Integer.parseInt();
        System.out.println("rec aroundPoi:" + place + "  mid:" + mid + "  type:" + type + "  radius:" + radius);
        if (type == null || place == null || mid == null) {
            return R.error("数据传递错误");
        }
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "aroundPoi");
        hash.put("mid", mid);
        System.out.println("rec aroundPoi:" + place + "  mid:" + mid + "  type:" + type + "  radius:" + radius);
        String item = getQueryFiled(place);
        HashMap<String, Object> hash = xzService.getFuncMap("aroundPoi", mid);
        String filed = "enti_name";
        String typeFiled = "sjnr";
        String item = xzService.getQueryFiled(place, filed, xzConfig.layerId, xzConfig.dbid, xzConfig.queryEntity);
        JSONObject obj = JSONObject.parseObject(item);
        Double lon = Double.parseDouble(obj.getString("lon"));
        Double lat = Double.parseDouble(obj.getString("lat"));
        String wkt = getWKt(lon, lat, radius);
        String itemList = getQueryAround(type, wkt);
//        System.out.print("ces: "+itemList);
        String wkt = xzService.getWKt(lon, lat, radius);
        String itemList = xzService.getQueryAround(type, wkt, typeFiled, xzConfig.layerId, xzConfig.dbid, xzConfig.queryEntity);
        JSONArray dataArray = JSONArray.parseArray(itemList);
        ArrayList<ReqEntity> arrayList = new ArrayList<>();
        for (int i = 0; i < dataArray.size(); i++) {
@@ -111,7 +82,7 @@
                ReqEntity reqEntity = new ReqEntity();
                reqEntity.setX(jsonObject.getString("lon"));
                reqEntity.setY(jsonObject.getString("lat"));
                reqEntity.setPlace(jsonObject.getString("enti_name"));
                reqEntity.setPlace(jsonObject.getString(filed));
                arrayList.add(reqEntity);
            }
        }
@@ -126,25 +97,14 @@
    @Operation(summary = "元信息查询")
    public R queryMeta(@RequestParam Map<String, String> allParams) {
        String mid = allParams.get("mid");
        System.out.println("rec queryMeta:" +"  mid:" + mid);
        System.out.println("rec queryMeta:" + "  mid:" + mid);
        if (mid == null) {
            return R.error("数据传递错误");
        }
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "queryMeta");
        hash.put("mid", mid);
        HashMap<String, Object> hash = xzService.getFuncMap("queryMeta", mid);
//        统计查询接口拼接
        JSONArray jsonArray = new JSONArray();
        HashMap<String, Object> staticField = new HashMap<>();
        String filed = "sjnr";
        staticField.put("type", "count");
        staticField.put("field", "sjnr");
        staticField.put("outfield", "count_" + filed);
        jsonArray.add(staticField);
        HashMap<String, String> formData = getHashMap();
        formData.put("statistics", jsonArray.toString());
        formData.put("groupby", filed);
        String filedString = entityHttpUtil.getPostMessage(queryEntity, formData);
        String filedString= xzService.getQueryMeta(xzConfig.layerId, xzConfig.dbid,filed,xzConfig.queryEntity);
        System.out.println("rec queryMeta:" + filedString);
        JSONObject jsonObject = JSON.parseObject(filedString);
        JSONArray dataArray = jsonObject.getJSONArray("data");
@@ -176,140 +136,5 @@
        return R.ok(hash);
    }
    public HashMap getHashMap() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("token", token);
        hashMap.put("containCount", "true");
        hashMap.put("layerid", layerId);
        hashMap.put("dbid", dbid);
        return hashMap;
    }
    public String getWKt(double lon, double lat, int radius) {
        // 创建一个坐标点
        Coordinate coord = new Coordinate(lon, lat);
        // 创建GeometryFactory实例,并指定坐标参考系统为WGS84
        GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
        // 使用坐标创建一个点
        Point point = geometryFactory.createPoint(coord);
        // 创建BufferOp实例,用于生成缓冲区
        BufferOp bufferOp = new BufferOp(point);
        int dis = 500;
        if (radius != 0) {
            dis = radius;
        }
        // 设置缓冲区距离为500米
        Geometry bufferedGeometry = bufferOp.getResultGeometry(dis);
        // 创建WKTWriter实例,用于将Geometry转换为WKT格式
//        WKTWriter wktWriter = new WKTWriter();
        // 将缓冲区Geometry转换为WKT格式
//        String wkt = wktWriter.write(bufferedGeometry);
        // 输出WKT
//        System.out.println("WKT: " + wkt);
        Envelope envelope = bufferedGeometry.getEnvelopeInternal();
        double[] wkt = new double[4];
        wkt[0] = envelope.getMinX();
        wkt[1] = envelope.getMinY();
        wkt[2] = envelope.getMaxX();
        wkt[3] = envelope.getMaxY();
        String arrayAsString = Arrays.toString(wkt);
        System.out.println("arrayAsString: " + arrayAsString);
        return arrayAsString;
    }
    public String getQueryAround(String type, String wkt) throws Exception {
        String query = "sjnr  like '%" + type + "%'" + "";
        String key = getPublickey();
        String encrypt = RsaHelper.encrypt(key, query);
        HashMap formData = getHashMap();
        formData.put("start", "1");
        formData.put("count", "0");
        formData.put("where", encrypt);
        formData.put("box", wkt);
        String jsonString = entityHttpUtil.getPostMessage(queryEntity, formData);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        JSONObject dataObjject = jsonObject.getJSONObject("data");
        JSONArray dataArray = dataObjject.getJSONArray("items");
        return dataArray.toString();
    }
    //关键字查询
    public String getQueryFiled(String place) throws Exception {
        String query = "enti_name like '%" + place + "%'" + "";
        String key = getPublickey();
        String encrypt = RsaHelper.encrypt(key, query);
        HashMap formData = getHashMap();
        formData.put("start", "1");
        formData.put("count", "0");
        formData.put("where", encrypt);
        String jsonString = entityHttpUtil.getPostMessage(queryEntity, formData);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        JSONObject dataObjject = jsonObject.getJSONObject("data");
        JSONArray dataArray = dataObjject.getJSONArray("items");
        return dataArray.getJSONObject(0).toString();
    }
    //获取秘钥接口
    public String getPublickey() {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", token);
        String jsonString = entityHttpUtil.getPostMessage(publickey, map);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        return jsonObject.getString("data");
    }
    @PostMapping("/aroundPoi2")
    @Operation(summary = "周边查询")
    public R aroundPoi2(@RequestBody ReqEntity reqEntity) {
        System.out.println("rec message:" + reqEntity);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "aroundPoi");
        hash.put("mid", "mid");
        List<ReqEntity> list = new ArrayList<>();
//        reqEntity.setX("120.9671");
//        reqEntity.setY("13.5748");
        list.add(reqEntity);
        list.add(reqEntity);
        hash.put("poi", list);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
    /*@GetMapping("/queryPath/{start_point}/{end_point}")
    @Operation(summary = "路线查询")
    public R queryPath(@PathVariable("start_point")String start_point,@PathVariable("end_point")String end_point){
        System.out.println("rec start_point: "+start_point+"  end_point: "+end_point);
        HashMap<String,Object> hash = new HashMap<>();
        hash.put("func","searchPath");
        List<ReqEntity> list = new ArrayList<>();
        hash.put("path",list);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
*/
    @PostMapping("/queryMeta2")
    @Operation(summary = "元信息查询")
    public R queryMeta2(@Parameter String mid) {
        System.out.println("queryMeta ");
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "queryMeta");
        hash.put("mid", mid);
        List<ReqEntity> list = new ArrayList<>();
        hash.put("meta", list);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
}