北京经济技术开发区经开区虚拟城市项目-【后端】-服务,Poi,企业,地块等定制接口
13693261870
2023-10-06 1c9d913d9974f0feb7c5756a665fcd08f22ee4b3
添加 批量更新企业坐标 接口
已修改4个文件
136 ■■■■■ 文件已修改
pom.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/smartearth/poiexcel/controller/EntController.java 105 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/smartearth/poiexcel/entity/StaticData.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/smartearth/poiexcel/utils/StringHelper.java 27 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -10,7 +10,7 @@
    </parent>
    <groupId>com.smartearth</groupId>
    <artifactId>poiExcel</artifactId>
    <version>0.1</version>
    <version>0.2</version>
    <name>poiExcel</name>
    <packaging>jar</packaging>
    <description>poiExcel</description>
src/main/java/com/smartearth/poiexcel/controller/EntController.java
@@ -1,19 +1,30 @@
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.util.StringUtils;
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;
/**
 * 企业控制器
@@ -26,6 +37,15 @@
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"})
@@ -109,4 +129,89 @@
            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 = 0; 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[1]));
            ent.setY(Double.parseDouble(split[0]));
        } 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);
    }
}
src/main/java/com/smartearth/poiexcel/entity/StaticData.java
@@ -182,6 +182,8 @@
    public final static String DOM = "DOM";
    public final static String STATUS = "status";
    public final static String LAYERS = "layers";
    public final static String REQUEST = "request";
src/main/java/com/smartearth/poiexcel/utils/StringHelper.java
@@ -3,10 +3,7 @@
import com.smartearth.poiexcel.entity.StaticData;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -243,4 +240,26 @@
        return list;
    }
    public static String getEncoding(String str) {
        List<String> codes = new ArrayList<>(Arrays.asList("UTF-8", "GBK", "GB2312", "ISO-8859-1"));
        for (String code : codes) {
            if (isEncoding(str, code)) {
                return code;
            }
        }
        return null;
    }
    public static boolean isEncoding(String str, String encode) {
        try {
            if (str.equals(new String(str.getBytes(), encode))) {
                return true;
            }
        } catch (Exception e) {
            //
        }
        return false;
    }
}