北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
13693261870
2023-10-06 6fccf8fc6f7073b86ff001aadc9f7ebb138a3596
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
package com.smartearth.poiexcel.controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartearth.poiexcel.entity.EntEntity;
import com.smartearth.poiexcel.entity.ResponseMsg;
import com.smartearth.poiexcel.entity.StaticData;
import com.smartearth.poiexcel.mapper.EntMapper;
import com.smartearth.poiexcel.mapper.QiYeMapper;
import com.smartearth.poiexcel.service.EntService;
import com.smartearth.poiexcel.utils.HttpUtils;
import com.smartearth.poiexcel.utils.StringHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 企业控制器
 * @author WWW
 * @date 2023-10-05
 */
@Api(tags = "企业控制器")
@RestController
@RequestMapping("/ent")
@SuppressWarnings("ALL")
public class EntController extends BaseController {
    @Resource
    EntService entService;
 
    @Resource
    EntMapper entMapper;
 
    @Resource
    QiYeMapper qiYeMapper;
 
    @Value("${address.code.url}")
    private String addressCodeUrl;
 
    @ApiOperation(value = "查询令牌")
    @GetMapping({"/selectToken"})
    public ResponseMsg<Object> selectToken() {
        try {
            String token = entService.selectToken();
 
            return success(StringHelper.isEmpty(token) ? 0 : 1, token);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @ApiOperation(value = "查询企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", dataType = "String", paramType = "query", example = ""),
            @ApiImplicitParam(name = "startDate", value = "开始日期", dataType = "String", paramType = "query", example = "2023-06-29"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", dataType = "String", paramType = "query", example = "2023-06-30"),
            @ApiImplicitParam(name = "qylabel", value = "标签", dataType = "Integer", paramType = "query", example = "开业"),
            @ApiImplicitParam(name = "showCount", value = "显示记录数", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "currentPage", value = "当前分页数", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping({"/selectEnts"})
    public ResponseMsg<Object> selectEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) {
        try {
            if (StringHelper.isEmpty(token)) {
                token = entService.selectToken();
            }
            if (StringHelper.isEmpty(token)) {
                return fail("查询令牌失败");
            }
            if (null == showCount || showCount < 1) {
                showCount = 10;
            }
            if (null == currentPage || currentPage < 1) {
                currentPage = 1;
            }
 
            List<EntEntity> list = entService.selectEnts(token, startDate, endDate, qylabel, showCount, currentPage);
 
            return success(null == list || list.isEmpty() ? 0 : list.size(), list);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @ApiOperation(value = "插入企业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌", dataType = "String", paramType = "query", example = ""),
            @ApiImplicitParam(name = "startDate", value = "开始日期", dataType = "String", paramType = "query", example = "2023-06-29"),
            @ApiImplicitParam(name = "endDate", value = "结束日期", dataType = "String", paramType = "query", example = "2023-06-30"),
            @ApiImplicitParam(name = "qylabel", value = "标签", dataType = "Integer", paramType = "query", example = "开业"),
            @ApiImplicitParam(name = "showCount", value = "显示记录数", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "currentPage", value = "当前分页数", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping({"/insertEnts"})
    public ResponseMsg<Object> insertEnts(String token, String startDate, String endDate, String qylabel, Integer showCount, Integer currentPage) {
        try {
            if (StringHelper.isEmpty(token)) {
                token = entService.selectToken();
            }
            if (StringHelper.isEmpty(token)) {
                return fail("查询令牌失败");
            }
            if (null == showCount || showCount < 1) {
                showCount = 10;
            }
            if (null == currentPage || currentPage < 1) {
                currentPage = 1;
            }
 
            List<EntEntity> list = entService.selectEnts(token, startDate, endDate, qylabel, showCount, currentPage);
            if (null == list || list.isEmpty()) {
                return fail("查询企业为空");
            }
 
            Integer rows = entService.insertEnts(list);
 
            return success(rows);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer", paramType = "query", example = "10"),
            @ApiImplicitParam(name = "pageIndex", value = "分页数(从1开始)", dataType = "Integer", paramType = "query", example = "1")
    })
    @GetMapping(value = "/selectByPage")
    public ResponseMsg<Object> selectByPage(Integer pageSize, Integer pageIndex) {
        try {
            if (null == pageSize || pageSize < 1) {
                pageSize = 10;
            }
            if (null == pageIndex || pageIndex < 1) {
                pageIndex = 1;
            }
            int count = qiYeMapper.selectCount();
            List<EntEntity> list = qiYeMapper.selectByPage(pageSize, StaticData.I100 * (pageIndex - 1));
 
            return success(count, list);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    @ApiOperation(value = "批量更新企业坐标")
    @GetMapping({"/updateCoords"})
    public ResponseMsg<Object> updateCoords() {
        try {
            int rows = 0;
            int count = qiYeMapper.selectCount();
            if (0 == count) {
                return success("没有数据需要更新", count);
            }
 
            int pages = (count - 1) / StaticData.I100 + 1;
            for (int i = 1; i <= pages; i++) {
                List<EntEntity> list = qiYeMapper.selectByPage(StaticData.I100, StaticData.I100 * (i - 1));
                if (null == list || list.isEmpty()) {
                    continue;
                }
 
                for (EntEntity ent : list) {
                    if (!StringHelper.isEmpty(ent.getAddress())) {
                        setEntCoord(ent);
                    }
                }
 
                rows += qiYeMapper.updates(list);
            }
 
            return success(rows);
        } catch (Exception ex) {
            return fail(ex, -1);
        }
    }
 
    /**
     * 设置企业坐标值
     */
    private void setEntCoord(EntEntity ent) {
        try {
            String json = selectAddrCode(ent.getAddress());
            if (StringHelper.isEmpty(json)) {
                return;
            }
 
            JSONObject obj = JSONObject.parseObject(json);
            if (null == obj || !StaticData.S1.equals(obj.get("status"))) {
                return;
            }
 
            JSONArray jsonArray = obj.getJSONArray("geocodes");
            if (null == jsonArray || jsonArray.isEmpty()) {
                return;
            }
 
            JSONObject object = jsonArray.getJSONObject(0);
            /*String addr = object.getString("formatted_address");
            if (StringHelper.isEmpty(addr)) {
                return;
            }
 
            String encoding = StringHelper.getEncoding(addr);
            if (encoding != null) {
                addr = new String(addr.getBytes(encoding));
            }
            ent.setAddress(addr);*/
 
            String location = object.getString("location");
            String[] split = location.split(",");
            ent.setX(Double.parseDouble(split[0]));
            ent.setY(Double.parseDouble(split[1]));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    private String selectAddrCode(String address) throws Exception {
        StringBuilder sb = new StringBuilder(addressCodeUrl);
        sb.append("?address=").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(sb.toString(), null, headers);
    }
}