北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
13693261870
2023-10-07 ff16d1213d5e43b21d3f72551c83dd007bec2261
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.smartearth.poiexcel.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartearth.poiexcel.entity.OrgPoiExcel;
import com.smartearth.poiexcel.utils.HttpUtils;
import com.smartearth.poiexcel.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
 
@Controller
@RequestMapping(path = "/api/poi/")
@Api(tags = {"POI相关接口"})
@Slf4j
@SuppressWarnings("ALL")
public class OrgPoiController {
 
    //北京数据平台poi接口转发
    @GetMapping(path = "/queryBJGdPoi")
    @ApiOperation(value = "查询GdPoi(北京数据平台)", notes = "多个关键字用“|”分割", hidden = true)
    public void queryBJGdPoi(
            @ApiParam(name = "keywords", value = "查询关键字", defaultValue = "数字北京大厦", required = true)
            @RequestParam(name = "keywords") String keywords,
            @ApiParam(name = "output", value = "返回数据格式(默认json,可选json/xml)", defaultValue = "json", required = true)
            @RequestParam(name = "output") String output,
            @ApiParam(name = "offset", value = "每页记录数据,默认20,不超过25", defaultValue = "20")
            @RequestParam(name = "offset") String offset,
            @ApiParam(name = "page", value = "当前页数,默认1,最大100", defaultValue = "1")
            @RequestParam(name = "page") String page,
            @ApiParam(name = "coord", value = "bjl地方坐标,cgcs2000国家2000坐标", defaultValue = "cgcs2000", required = true)
            @RequestParam(name = "coord") String coord,
            HttpServletResponse response
    ) throws IOException {
        try {
            //请求url
            String url = "http://172.26.64.84/service/NavigationService";
            //设置请求参数
            Map<String, String> params = new HashMap<>();
            params.put("request", "GdPoi");
            params.put("keywords", StringUtils.isEmpty(keywords) ? "" : keywords);
            params.put("output", StringUtils.isEmpty(output) ? "json" : output);
            params.put("offset", StringUtils.isEmpty(offset) ? "20" : offset);
            params.put("page", StringUtils.isEmpty(page) ? "1" : page);
            params.put("coord", StringUtils.isEmpty(coord) ? "cgcs2000" : coord);
            //设置请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Accept", "*/*");
            //Base64加密Authorization认证下的basic auth 用户名:密码
//            headers.put("Authorization","Basic ampqc2tmcTpKampza2ZxQDIwMjI=");
            headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
            //执行请求
            String responseString = HttpUtils.get(url, params, headers);
//            return Result.ok(responseString);
            //返回值在response响应中
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.getWriter().write(responseString);
        } catch (Exception e) {
            e.printStackTrace();
//            return  Result.error("未知错误");
            response.getWriter().write("未知错误");
        }
 
    }
 
    @GetMapping(path = "/queryBJbdPoi")
    @ApiOperation(value = "查询bdPoi(北京数据平台)", notes = "查询不支持多关键字检索,可查类型,如query=美食", hidden = true)
    public void queryBJbdPoi(
            @ApiParam(name = "query", value = "查询关键字", defaultValue = "数字北京大厦", required = true)
            @RequestParam(name = "query") String query,
            @ApiParam(name = "output", value = "返回数据格式(默认json,可选json/xml)", defaultValue = "json", required = true)
            @RequestParam(name = "output") String output,
            @ApiParam(name = "page_num", value = "分页页码,默认为0,0代表第一页", defaultValue = "0")
            @RequestParam(name = "page_num") String page_num,
            @ApiParam(name = "page_size", value = "单次POI数量,默认为10条,最大20条", defaultValue = "10")
            @RequestParam(name = "page_size") String page_size,
            @ApiParam(name = "coord", value = "bjl地方坐标,cgcs2000国家2000坐标", defaultValue = "cgcs2000", required = true)
            @RequestParam(name = "coord") String coord,
            HttpServletResponse response
    ) throws IOException {
        try {
            //请求url
            String url = "http://172.26.64.84/service/NavigationService";
            //设置请求参数
            Map<String, String> params = new HashMap<>();
            params.put("request", "bdPoi");
            params.put("query", StringUtils.isEmpty(query) ? "" : query);
            params.put("output", StringUtils.isEmpty(output) ? "json" : output);
            params.put("page_num", StringUtils.isEmpty(page_num) ? "0" : page_num);
            params.put("page_size", StringUtils.isEmpty(page_size) ? "10" : page_size);
            params.put("coord", StringUtils.isEmpty(coord) ? "cgcs2000" : coord);
            //设置请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Accept", "*/*");
            //Base64加密Authorization认证下的basic auth 用户名:密码
            headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
            //执行请求
            String responseString = HttpUtils.get(url, params, headers);
//            return Result.ok(responseString);
            //返回值在response响应中
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.getWriter().write(responseString);
        } catch (Exception e) {
            e.printStackTrace();
//            return  Result.error("未知错误");
            response.getWriter().write("未知错误");
        }
 
    }
 
    @GetMapping(path = "/queryBJhint")
    @ApiOperation(value = "输入提示(北京数据平台)", notes = "POI分类,多个类型间用“|”分隔", hidden = true)
    public void queryBJhint(
            @ApiParam(name = "keywords", value = "查询关键字", defaultValue = "数字北京大厦", required = true)
            @RequestParam(name = "keywords") String keywords,
            @ApiParam(name = "output", value = "返回数据格式(默认json,可选json/xml)", defaultValue = "json", required = true)
            @RequestParam(name = "output") String output,
//            @ApiParam(name = "type", value = "POI分类,多个类型间用“|”分隔,可选值:POI分类名称")
//            @RequestParam(name = "type",required = false) String type,
            @ApiParam(name = "datatype",
                    value = "返回的数据类型,多种数据类型用“|”分隔," +
                            "可选值:all-返回所有数据类型、poi-返回POI数据类型、" +
                            "bus-返回公交站点数据类型、busline-返回公交线路数据类型", defaultValue = "all")
            @RequestParam(name = "datatype") String datatype,
            @ApiParam(name = "coord", value = "bjl地方坐标,cgcs2000国家2000坐标", defaultValue = "cgcs2000", required = true)
            @RequestParam(name = "coord") String coord,
            HttpServletResponse response
    ) throws IOException {
        try {
            //请求url
            String url = "http://172.26.64.84/service/NavigationService?request=hint&type=&";
            //设置请求参数
            Map<String, String> params = new HashMap<>();
            params.put("keywords", StringUtils.isEmpty(keywords) ? "" : keywords);
            params.put("output", StringUtils.isEmpty(output) ? "json" : output);
//            params.put("type",StringUtils.isEmpty(type)?"":type);
            params.put("datatype", StringUtils.isEmpty(datatype) ? "all" : datatype);
            params.put("coord", StringUtils.isEmpty(coord) ? "cgcs2000" : coord);
            //设置请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Accept", "*/*");
            //Base64加密Authorization认证下的basic auth 用户名:密码
            headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
            //执行请求
            String responseString = HttpUtils.get(url, params, headers);
//            return Result.ok(responseString);
            //返回值在response响应中
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            response.getWriter().write(responseString);
        } catch (Exception e) {
            e.printStackTrace();
//            return  Result.error("未知错误");
            response.getWriter().write("未知错误");
        }
 
    }
 
    @Value("${excel.export.dir}")
    private String excelExportDir;
 
    @Value("${server.port}")
    private int serverPort;
 
    @ApiOperation(value = "从Excel导入导出",
            notes = "<p>将导入文件中待转换处理的地址表头修改为\"待转换地址\"(或者增加此列)</p>" +
                    "<p>文件导出至D:\\excel\\ExcelWrite.xlsx</p>")
    @RequestMapping(value = "/importBatchExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result importBatchExcel(HttpServletRequest request, @RequestPart("multipartFile") MultipartFile multipartFile) {
        ExcelWriter excelWriter = null;
        int k = 0;
        int j = 0;
        try {
//            String tmp = System.getProperty("java.io.tmpdir");
//            if (!tmp.endsWith(File.separator)) {
//                tmp += File.separator;
//            }
            File file = new File(excelExportDir + "ExcelRead.xlsx");
            FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
            //默认读取Excel文件的第一个sheet
            List<OrgPoiExcel> list = EasyExcel.read(file).head(OrgPoiExcel.class).sheet().doReadSync();
            List<OrgPoiExcel> listWrite = new ArrayList<>();
 
            File fileWrite = new File(excelExportDir + "ExcelWrite.xlsx");
//        EasyExcel.write(fileWrite, OrgPoiExcel.class).sheet("Sheet1").doWrite(listWrite);
            excelWriter = EasyExcel.write(fileWrite, OrgPoiExcel.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").build();
 
            for (int i = 0; i < list.size(); i++) {//i=60653
                OrgPoiExcel data = list.get(i);
//            String businessName = data.getBusinessName();
                String address = data.getAddress();
                String resultString = selectAddrCode(address);
                JSONObject jsonObject = JSONObject.parseObject(resultString);
                if (jsonObject != null && jsonObject.get("status").equals("1")) {
                    JSONArray jsonArray = jsonObject.getJSONArray("geocodes");
                    if (jsonArray != null && jsonArray.size() > 0) {
                        JSONObject object = jsonArray.getJSONObject(0);
//                String businessNameJson = object.getString("name");
                        String formatted_address = object.getString("formatted_address");
                        if (!StringUtils.isEmpty(formatted_address)) {
                            String encoding = getEncoding(formatted_address);
                            //log.info("encoding: {}", encoding);
                            if (!"UTF-8".equals(encoding) && !"".equals(encoding)) {
                                formatted_address = new String(formatted_address.getBytes(encoding), "UTF-8");
                            }
                            data.setStandardAddress(formatted_address);
                        } else {
                            data.setStandardAddress(null);
                        }
                        String location = object.getString("location");
                        String[] split = location.split(",");
                        data.setX(Double.parseDouble(split[1]));
                        data.setY(Double.parseDouble(split[0]));
                    } else {
                        data.setX(0.0);
                        data.setY(0.0);
                    }
                } else {
                    data.setX(0.0);
                    data.setY(0.0);
                    j++;
                    if (j == 300000) {
                        excelWriter.finish();
                        return Result.error("返回值为0超过300000次");
                    }
                }
                k++;
                System.out.println(k + ":" + data);
                listWrite.add(data);
                excelWriter.write(listWrite, writeSheet);
                listWrite.remove(0);
            }
//        EasyExcel.write(fileWrite, OrgPoiExcel.class).sheet("Sheet1").doWrite(listWrite);
            if (excelWriter != null) {
                excelWriter.finish();
            }
            StringBuilder url = new StringBuilder("http://");
            url.append(request.getLocalAddr()).append(":")
                    .append(request.getLocalPort())
                    .append("/api/poi/downloadFile");
            return Result.ok("下载文件,请在浏览其中打开地址 " + url.toString());
        } catch (Exception e) {
            e.printStackTrace();
            if (excelWriter != null) {
                excelWriter.finish();
            }
            return Result.error(e.toString());
        }
    }
 
    @GetMapping(value = "downloadFile")
    @ApiOperation(value = "下载文件", hidden = true)
    public void downloadFile(HttpServletResponse response) {
        File fileWrite = new File(excelExportDir + "ExcelWrite.xlsx");
        String fileName = "excel.xlsx";
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(fileWrite);
            int len;
            byte[] bytes = new byte[1024];
            ServletOutputStream outputStream = response.getOutputStream();
            while ((len = fis.read(bytes)) > 0) {
                outputStream.write(bytes, 0, len);
            }
            fis.close();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    private String selectBJGdPoi(
            String keywords) throws Exception {
        StringBuilder url = new StringBuilder(
                "http://172.26.64.84/service/NavigationService?request=GdPoi");
        //设置请求参数
        if (!StringUtils.isEmpty(keywords)) {
            url.append("&keywords=").append(keywords).append("&output=json&offset=20&page=1&coord=cgcs2000");
        }
        //设置请求头
        Map<String, String> headers = new HashMap<>();
//        headers.put("Authorization","Basic ampqc2tmcTpKampza2ZxQDIwMjI=");
        headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
        //执行请求
        return HttpUtils.get(url.toString(), null, headers);
    }
 
    @Value("${address.code.url}")
    private String addressCodeUrl;
 
    private String selectAddrCode(
            String address) throws Exception {
        StringBuilder url = new StringBuilder(addressCodeUrl).append(
                "?address=");
        //设置请求参数
        if (!StringUtils.isEmpty(address)) {
            url.append(address).append("&output=json&batch=true&coord=cgcs2000&adcode=yes");
        }
        //设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString(("jjjskfq" + ":" + "Jjjskfq@2022").getBytes()));
 
        //执行请求
        return HttpUtils.get(url.toString(), null, headers);
    }
 
    public static String getEncoding(String str) {
        String encode = "GB2312";
        try {
            if (isEncoding(str, encode)) { // 判断是不是GB2312
                return encode;
            }
        } catch (Exception exception) {
        }
        encode = "ISO-8859-1";
        try {
            if (isEncoding(str, encode)) { // 判断是不是ISO-8859-1
                return encode;
            }
        } catch (Exception exception1) {
        }
        encode = "UTF-8";
        try {
            if (isEncoding(str, encode)) { // 判断是不是UTF-8
                return encode;
            }
        } catch (Exception exception2) {
        }
        encode = "GBK";
        try {
            if (isEncoding(str, encode)) { // 判断是不是GBK
                return encode;
            }
        } catch (Exception exception3) {
        }
        return ""; // 如果都不是,说明输入的内容不属于常见的编码格式。
    }
 
    public static boolean isEncoding(String str, String encode) {
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}