Procházet zdrojové kódy

content-service/geo add ChartMapController

reghao před 7 měsíci
rodič
revize
a7fc49d451

+ 76 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/geo/controller/ChartMapController.java

@@ -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;
+    }
+}

+ 5 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/geo/db/mapper/GeoChinaMapper.java

@@ -2,9 +2,11 @@ package cn.reghao.tnb.content.app.geo.db.mapper;
 
 import cn.reghao.jutil.jdk.db.BaseMapper;
 import cn.reghao.tnb.content.app.geo.model.po.GeoChina;
+import cn.reghao.tnb.content.app.geo.model.po.GeoJson;
 import cn.reghao.tnb.content.app.geo.model.vo.GeoArea;
 import cn.reghao.tnb.content.app.geo.model.vo.MapPoint;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 import java.util.List;
 
@@ -15,6 +17,9 @@ import java.util.List;
 @Mapper
 public interface GeoChinaMapper extends BaseMapper<GeoChina> {
     void saveMultiPolygon(GeoChina geoChina);
+    void saveGeoJosn(GeoJson geoJson);
     GeoArea findByPoint(MapPoint mapPoint);
     List<GeoArea> findAllArea();
+    String findGeoJson(@Param("areaCode") int areaCode, @Param("deep") int deep);
+    List<GeoJson> findGeoJsonObject();
 }

+ 18 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/geo/model/po/GeoJson.java

@@ -0,0 +1,18 @@
+package cn.reghao.tnb.content.app.geo.model.po;
+
+import cn.reghao.jutil.jdk.db.BaseObject;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @author reghao
+ * @date 2025-08-19 11:13:57
+ */
+@Setter
+@Getter
+public class GeoJson extends BaseObject<Integer> {
+    private int areaCode;
+    private int deep;
+    private String geoJson;
+    private transient String name;
+}

+ 19 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/geo/model/vo/ChartMap.java

@@ -0,0 +1,19 @@
+package cn.reghao.tnb.content.app.geo.model.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+
+/**
+ * @author reghao
+ * @date 2025-08-19 15:17:32
+ */
+@AllArgsConstructor
+@NoArgsConstructor
+@Getter
+public class ChartMap {
+    private String geoJson;
+    private List<ChartMapData> list;
+}

+ 17 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/geo/model/vo/ChartMapData.java

@@ -0,0 +1,17 @@
+package cn.reghao.tnb.content.app.geo.model.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author reghao
+ * @date 2025-08-19 15:05:14
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+@Getter
+public class ChartMapData {
+    private String name;
+    private int value;
+}

+ 15 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/geo/model/vo/SelectOption.java

@@ -0,0 +1,15 @@
+package cn.reghao.tnb.content.app.geo.model.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author reghao
+ * @date 2025-08-19 14:03:06
+ */
+@NoArgsConstructor
+@AllArgsConstructor
+public class SelectOption {
+    private String label;
+    private String value;
+}

+ 17 - 0
content/content-service/src/main/java/cn/reghao/tnb/content/app/util/RandomUtil.java

@@ -0,0 +1,17 @@
+package cn.reghao.tnb.content.app.util;
+
+import java.security.SecureRandom;
+import java.util.Random;
+
+/**
+ * @author reghao
+ * @date 2025-08-19 15:21:06
+ */
+public class RandomUtil {
+    private final static Random random = new SecureRandom();
+
+    public static int getRandomNumber(int max) {
+        int min = 0;
+        return min + random.nextInt(max - min);
+    }
+}

+ 23 - 0
content/content-service/src/main/resources/mapper/geo/GeoChinaMapper.xml

@@ -14,6 +14,12 @@
         values
         (#{id},#{pid},#{deep},#{name},#{extPath},point(#{longitude},#{latitude}),GeomFromText('MULTIPOLYGON(${multiPolygon})'))
     </insert>
+    <insert id="saveGeoJosn" useGeneratedKeys="true" keyProperty="id">
+        insert into geo_geo_json
+            (`area_code`,`deep`,`geo_json`)
+        values
+            (#{areaCode},#{deep},#{geoJson})
+    </insert>
 
     <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
         insert ignore into geo_china
@@ -24,6 +30,11 @@
         </foreach>
     </insert>
 
+    <select id="findById" resultType="cn.reghao.tnb.content.app.geo.model.po.GeoChina">
+        select *
+        from geo_china
+        where id=#{id}
+    </select>
     <select id="findByPoint" resultType="cn.reghao.tnb.content.app.geo.model.vo.GeoArea">
         select id,pid,deep,`name`,ext_path,x(geo) as lng,y(geo) as lat
         from geo_china
@@ -32,5 +43,17 @@
     <select id="findAllArea" resultType="cn.reghao.tnb.content.app.geo.model.vo.GeoArea">
         select id,pid,deep,`name`,ext_path,x(geo) as lng,y(geo) as lat
         from geo_china
+        where pid=0
+    </select>
+    <select id="findGeoJson" resultType="java.lang.String">
+        select geo_json
+        from geo_geo_json
+        where area_code=#{areaCode} and deep=#{deep}
+    </select>
+    <select id="findGeoJsonObject" resultType="cn.reghao.tnb.content.app.geo.model.po.GeoJson">
+        select geo_geo_json.area_code,geo_geo_json.deep,geo_china.`name`
+        from geo_geo_json
+        inner join geo_china
+        on geo_geo_json.area_code=geo_china.id
     </select>
 </mapper>