|
|
@@ -0,0 +1,76 @@
|
|
|
+package cn.reghao.tnb.content.app.geo.controller;
|
|
|
+
|
|
|
+import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.jutil.web.WebResult;
|
|
|
+import cn.reghao.tnb.content.app.geo.db.mapper.GeoChinaMapper;
|
|
|
+import cn.reghao.tnb.content.app.geo.model.po.GeoJson;
|
|
|
+import cn.reghao.tnb.content.app.geo.model.vo.ChartMap;
|
|
|
+import cn.reghao.tnb.content.app.geo.model.vo.ChartMapData;
|
|
|
+import cn.reghao.tnb.content.app.geo.model.vo.SelectOption;
|
|
|
+import cn.reghao.tnb.content.app.util.RandomUtil;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author reghao
|
|
|
+ * @date 2025-08-19 15:59:13
|
|
|
+ */
|
|
|
+@Tag(name = "地图接口")
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/geo/chartmap")
|
|
|
+public class ChartMapController {
|
|
|
+ private final GeoChinaMapper geoChinaMapper;
|
|
|
+
|
|
|
+ public ChartMapController(GeoChinaMapper geoChinaMapper) {
|
|
|
+ this.geoChinaMapper = geoChinaMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/area", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String getGeoJsonList() {
|
|
|
+ List<GeoJson> list = geoChinaMapper.findGeoJsonObject();
|
|
|
+ List<SelectOption> selectOptionList = list.stream().map(geoJson -> {
|
|
|
+ String name = geoJson.getName();
|
|
|
+ int areaCode = geoJson.getAreaCode();
|
|
|
+ int deep = geoJson.getDeep();
|
|
|
+ String label = String.format("%s-%s", name, deep);
|
|
|
+ String value = String.format("%s-%s", areaCode, deep);
|
|
|
+ return new SelectOption(label, value);
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return WebResult.success(selectOptionList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping(value = "/geojson", produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
+ public String getGeoJson(@RequestParam("areaCode") String areaCodeStr) {
|
|
|
+ // /chartmap/geojson
|
|
|
+ String[] arr = areaCodeStr.split("-");
|
|
|
+ int areaCode = Integer.parseInt(arr[0]);
|
|
|
+ int deep = Integer.parseInt(arr[1]);
|
|
|
+ String geoJson = geoChinaMapper.findGeoJson(areaCode, deep);
|
|
|
+ List<ChartMapData> chartMapDataList = parseGeoJson(geoJson);
|
|
|
+ ChartMap chartMap = new ChartMap(geoJson, chartMapDataList);
|
|
|
+ return WebResult.success(chartMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ChartMapData> parseGeoJson(String geoJson) {
|
|
|
+ List<ChartMapData> list = new ArrayList<>();
|
|
|
+ JsonObject jsonObject = JsonConverter.jsonToJsonElement(geoJson).getAsJsonObject();
|
|
|
+ for (JsonElement jsonElement : jsonObject.get("features").getAsJsonArray()) {
|
|
|
+ JsonObject propertiesObject = jsonElement.getAsJsonObject().get("properties").getAsJsonObject();
|
|
|
+ int adcode = propertiesObject.get("adcode").getAsInt();
|
|
|
+ String name = propertiesObject.get("name").getAsString();
|
|
|
+ list.add(new ChartMapData(name, RandomUtil.getRandomNumber(1000)));
|
|
|
+ }
|
|
|
+
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+}
|