suerprisePlus
2024-08-28 5b14811aaeb72e828d948068e899c65877aaca9a
src/main/java/com/yb/controller/XzController.java
@@ -1,57 +1,211 @@
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.entity.ReqEntity;
import com.yb.helper.RsaHelper;
import com.yb.util.EntityHttpUtil;
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 java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@Tag(name="新智接口")
@Tag(name = "新智接口")
@RestController
@RequestMapping("/api/v1")
public class XzController {
    @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 JSONObject jsonObject;
    private JSONObject dataObjject;
    private Coordinate coord;
    private Object filObj;
    @PostMapping("/poiMap")
    @Operation(summary = "显示地点")
    public R poiMap(@Parameter String place,@Parameter String mid){
        System.out.println("rec message:"+place+"  mid:"+mid);
        HashMap<String,Object> hash = new HashMap<>();
        hash.put("func","poiMap");
        hash.put("mid","mid");
    public R poiMap(@Parameter String place, @Parameter String mid) throws Exception {
        System.out.println("rec message:" + place + "  mid:" + mid);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "poiMap");
        hash.put("mid", mid);
        ReqEntity reqEntity = new ReqEntity();
        reqEntity.setPlace(place);
        reqEntity.setX("120.9671");
        reqEntity.setY("13.5748");
        hash.put("poi",reqEntity);
        String item = getQueryFiled(place);
        JSONObject obj = JSONObject.parseObject(item);
        reqEntity.setX(obj.getString("lon"));
        reqEntity.setY(obj.getString("lat"));
//        System.out.print(obj);
        reqEntity.setPlace(obj.getString("enti_name"));
        System.out.println("rec message:" +reqEntity);
        hash.put("poi", reqEntity);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
    @PostMapping("/aroundPoi")
    @Operation(summary = "显示地点")
    public R aroundPoi(@Parameter String place, @Parameter String mid, @Parameter String type, @Parameter int radius) throws Exception {
        System.out.println("rec message:" + place + "  mid:" + mid + "  type:" + type + "  radius:" + radius);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "aroundpoi");
        hash.put("mid", mid);
        String item = getQueryFiled(place);
        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);
        JSONArray dataArray =JSONArray.parseArray(itemList);
      ArrayList<ReqEntity> arrayList= new ArrayList<>();
        for(int i =0;i< dataArray.size();i++ ){
            Object element = dataArray.get(i);
            if (element instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) element;
                ReqEntity reqEntity= new ReqEntity();
                reqEntity.setX(jsonObject.getString("lon"));
                reqEntity.setY(jsonObject.getString("lat"));
                reqEntity.setPlace(jsonObject.getString("enti_name"));
                arrayList.add(reqEntity);
            }
        }
        System.out.println("rec message:" +arrayList);
        hash.put("poi", arrayList);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
        return R.ok(hash);
    }
    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 = new HashMap<>();
        formData.put("token", token);
        formData.put("start", "1");
        formData.put("count", "0");
        formData.put("containCount", "true");
        formData.put("layerid", layerId);
        formData.put("dbid", dbid);
        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 = new HashMap<>();
        formData.put("token", token);
        formData.put("start", "1");
        formData.put("count", "0");
        formData.put("containCount", "true");
        formData.put("layerid", layerId);
        formData.put("dbid", dbid);
        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 aroundPoi(@RequestBody ReqEntity reqEntity){
        System.out.println("rec message:"+reqEntity);
        HashMap<String,Object> hash = new HashMap<>();
        hash.put("func","aroundPoi");
        hash.put("mid","mid");
    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");
//        reqEntity.setX("120.9671");
//        reqEntity.setY("13.5748");
        list.add(reqEntity);
        list.add(reqEntity);
        hash.put("poi",list);
        hash.put("poi", list);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);
@@ -78,15 +232,13 @@
*/
    @PostMapping("/queryMeta")
    @Operation(summary = "元信息查询")
    public R queryMeta(@Parameter String mid){
    public R queryMeta(@Parameter String mid) {
        System.out.println("queryMeta ");
        HashMap<String,Object> hash = new HashMap<>();
        hash.put("func","queryMeta");
        hash.put("mid",mid);
        HashMap<String, Object> hash = new HashMap<>();
        hash.put("func", "queryMeta");
        hash.put("mid", mid);
        List<ReqEntity> list = new ArrayList<>();
        hash.put("meta",list);
        hash.put("meta", list);
        String message = JSON.toJSONString(hash);
        webSocketController.sendAllMessage(message);