刘光辉
13 小时以前 34981c30a78e8bbd7791131059a9210f9928b62c
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
package jnpf.base.controller;
 
import io.swagger.v3.oas.annotations.Parameter;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.base.Joiner;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.entity.ProvinceAtlasEntity;
import jnpf.base.model.province.AtlasJsonModel;
import jnpf.base.model.province.ProvinceListTreeVO;
import jnpf.base.service.ProvinceAtlasService;
import jnpf.constant.MsgCode;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import jnpf.util.wxutil.HttpUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 行政区划-地图
 *
 * @author JNPF开发平台组
 * @version V3.4.2
 * @copyright 引迈信息技术有限公司
 * @date 2023/1/29 10:41:25
 */
@Tag(name = "行政区划-地图", description = "atlas")
@RestController
@RequestMapping("/atlas")
@RequiredArgsConstructor
public class ProvinceAtlasController extends SuperController<ProvinceAtlasService, ProvinceAtlasEntity> {
 
 
    private final ProvinceAtlasService provinceAtlasService;
    public static final String ATLAS_URL = "https://geo.datav.aliyun.com/areas_v3/bound/geojson?code=";
 
    //树形递归
    private static boolean addChild(ProvinceListTreeVO node, List<ProvinceListTreeVO> list) {
        for (ProvinceListTreeVO n : list) {
            if (n.getId().equals(node.getParentId())) {
                if (n.getChildren() == null) {
                    n.setChildren(new ArrayList<>());
                }
                List<ProvinceListTreeVO> children = n.getChildren();
                children.add(node);
                n.setChildren(children);
                return true;
            }
            if (n.getChildren() != null && !n.getChildren().isEmpty()) {
                List<ProvinceListTreeVO> children = n.getChildren();
                if (addChild(node, children)) {
                    return true;
                }
            }
 
        }
        return false;
    }
 
    /**
     * 获取所有列表
     *
     * @return ignore
     */
    @Operation(summary = "获取所有列表")
    @GetMapping
    public ActionResult<List<ProvinceListTreeVO>> list() {
        List<ProvinceAtlasEntity> list = provinceAtlasService.getList();
        List<ProvinceListTreeVO> listVOS = JsonUtil.getJsonToList(list, ProvinceListTreeVO.class);
        listVOS.forEach(item -> {
            if( StringUtil.isNotEmpty(item.getAtlasCenter())){
                String[] split = item.getAtlasCenter().split(",");
                item.setCenterLong(new BigDecimal(split[0]));
                item.setCenterLat(new BigDecimal(split[1]));
            }
        });
        for (int i = 0; i < listVOS.size(); i++) {
            ProvinceListTreeVO item = listVOS.get(i);
            if (!StringUtil.isEmpty(item.getParentId()) && addChild(item, listVOS) && !listVOS.isEmpty()) {
                    listVOS.remove(item);
                    i--;
                }
 
        }
        return ActionResult.success(listVOS);
    }
 
    /**
     * 获取列表
     *
     * @param pid 主键
     * @return
     */
    @Operation(summary = "获取列表")
    @Parameter(name = "pid", description = "主键", required = true)
    @GetMapping("/list/{pid}")
    public ActionResult<List<ProvinceListTreeVO>> getListByPid(@PathVariable("pid") String pid) {
        List<ProvinceAtlasEntity> list = provinceAtlasService.getListByPid(pid);
        List<ProvinceListTreeVO> listVOS = JsonUtil.getJsonToList(list, ProvinceListTreeVO.class);
        return ActionResult.success(listVOS);
    }
 
    /**
     * 获取地图json
     *
     * @param code 编码
     * @return
     */
    @Operation(summary = "获取地图json")
    @Parameter(name = "code", description = "编码", required = true)
    @GetMapping("/geojson")
    public ActionResult<JSONObject> geojson(@RequestParam("code") String code) {
        ProvinceAtlasEntity oneByCode = provinceAtlasService.findOneByCode(code);
        if(oneByCode == null){
            return ActionResult.fail(MsgCode.SYS022.get());
        }
        List<ProvinceAtlasEntity> listByPid = provinceAtlasService.getListByPid(oneByCode.getId());
        String url = ATLAS_URL + code;
        if (CollectionUtils.isNotEmpty(listByPid)) {
            url += "_full";
        }
        JSONObject rstObj;
        try {
            rstObj = HttpUtil.httpRequest(url, "GET", null);
        } catch (Exception e) {
            return ActionResult.fail(MsgCode.SYS023.get());
        }
        if (rstObj == null) {
            return ActionResult.fail(MsgCode.SYS024.get());
        }
        return ActionResult.success(rstObj);
    }
 
    /**
     * 同步行政区划信息
     *
     * @return
     */
    @Operation(summary = "同步行政区划信息")
    @GetMapping("/crePy")
    public ActionResult<Object> crePy() {
        List<ProvinceAtlasEntity> list = provinceAtlasService.list();
        for (ProvinceAtlasEntity p : list) {
 
            String url = ATLAS_URL + p.getId();
            JSONObject rstObj;
            try {
                rstObj = HttpUtil.httpRequest(url, "GET", null);
                if (rstObj == null) {
                    provinceAtlasService.removeById(p);
                } else {
                    //将获取到的信息写入表
                    AtlasJsonModel jsonToBean = JsonUtil.getJsonToBean(rstObj, AtlasJsonModel.class);
                    List<BigDecimal> center = jsonToBean.getFeatures().get(0).getProperties().getCenter();
                    p.setAtlasCenter(Joiner.on(",").join(center));
                    //生成拼音
                    provinceAtlasService.updateById(p);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return ActionResult.fail(MsgCode.SYS023.get());
            }
        }
        return ActionResult.success();
    }
 
}