This commit is contained in:
TigerNiu
2025-11-26 09:15:55 +08:00
parent 49485a7ac1
commit 264236c74a
7 changed files with 202 additions and 3 deletions

View File

@@ -1,7 +1,9 @@
package com.whu.edu.LyStatistic.MapLyStatistic.Controller;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.DistrictStatsDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.StreetStatsDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotStatsDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotBoundaryDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Service.StatisticsService;
import com.whu.edu.LyStatistic.Util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
@@ -12,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.List;
@RestController
@RequestMapping("/api/stats")
@@ -61,6 +64,67 @@ public class StatisticsController {
}
}
@GetMapping("/street")
public ApiResponse<StreetStatsDTO> getStreetStats(@RequestParam String district) {
try {
// 1⃣ 获取 Map<街道名, PlotStatsDTO>
Map<String, PlotStatsDTO> statsMap = statisticsService.getStreetStatsByDistrict(district);
// 2⃣ 构建统计 DTO
Map<String, String> completionStatus = new LinkedHashMap<>();
Map<String, Double> plotArea = new LinkedHashMap<>();
Map<String, Integer> plotCount = new LinkedHashMap<>();
for (Map.Entry<String, PlotStatsDTO> entry : statsMap.entrySet()) {
String street = entry.getKey();
PlotStatsDTO stats = entry.getValue();
// 判断状态(逻辑与区级相同)
String status;
if (safeInt(stats.getApprovedCount()) > 0) {
status = "已完成";
} else if (safeInt(stats.getCollectedCount()) > 0) {
status = "未完成";
} else {
status = "未开始";
}
completionStatus.put(street, status);
plotArea.put(street, safeDouble(stats.getTotalArea()));
plotCount.put(street, safeInt(stats.getPlotCount()));
}
StreetStatsDTO dto = new StreetStatsDTO(completionStatus, plotArea, plotCount);
return ApiResponse.success(dto);
} catch (Exception e) {
return ApiResponse.error("街道统计查询失败:" + e.getMessage());
}
}
/**
* ✅ 新增接口:一次性获取所有小班边界(无参)
* URL: GET /api/stats/boundaries/all
*/
@GetMapping("/boundaries/all")
public ApiResponse<List<PlotBoundaryDTO>> getAllBoundaries() {
try {
long startTime = System.currentTimeMillis();
// 调用 Service 获取全量数据
List<PlotBoundaryDTO> list = statisticsService.getAllBoundaries();
long duration = System.currentTimeMillis() - startTime;
System.out.println("全量加载小班边界耗时: " + duration + "ms, 总数量: " + list.size());
return ApiResponse.success(list);
} catch (Exception e) {
return ApiResponse.error("获取所有图斑边界失败:" + e.getMessage());
}
}
// 防止 null
private int safeInt(Integer value) {
return (value != null) ? value : 0;

View File

@@ -0,0 +1,22 @@
package com.whu.edu.LyStatistic.MapLyStatistic.Dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Map;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PlotBoundaryDTO {
/**
* 小班唯一标识 (对应 SQL 中的 "ID")
*/
private String id;
/**
* 小班边界 GeoJSON (对应 SQL 中的 "shape")
* 存储内容示例: {"type":"MultiPolygon","coordinates":...}
*/
private String shape;
}

View File

@@ -0,0 +1,16 @@
package com.whu.edu.LyStatistic.MapLyStatistic.Dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Map;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class StreetStatsDTO {
private Map<String, String> completionStatus; // 区 -> 完成状态
private Map<String, Double> plotArea; // 区 -> 图斑面积
private Map<String, Integer> plotCount; // 区 -> 图斑数量
}

View File

@@ -1,6 +1,7 @@
package com.whu.edu.LyStatistic.MapLyStatistic.Mapper;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotStatsDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotBoundaryDTO;
//import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotDetailDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@@ -20,7 +21,12 @@ public interface TaskCommonMapper {
* @return 统计结果 DTO
*/
PlotStatsDTO selectPlotStats(@Param("schema") String schema);
/**
* 获取所有小班的ID和边界信息
* * @param schema 动态传入的数据库模式名
* @return 包含ID和GeoJSON的列表
*/
List<PlotBoundaryDTO> selectAllPlotBoundaries(@Param("schema") String schema);
// /**
// * 查询单个图斑详情
// * @param schema schema 名

View File

@@ -1,11 +1,14 @@
package com.whu.edu.LyStatistic.MapLyStatistic.Service;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotStatsDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotBoundaryDTO;
import com.whu.edu.LyStatistic.MapLyStatistic.Dto.UnitInfo;
import com.whu.edu.LyStatistic.MapLyStatistic.Mapper.InfoMapper;
import com.whu.edu.LyStatistic.MapLyStatistic.Mapper.TaskCommonMapper;
import com.whu.edu.LyStatistic.MapLyStatistic.Service.BaseService.BaseTaskQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -18,6 +21,8 @@ public class StatisticsService {
@Autowired
private BaseTaskQueryService baseQueryService; // 通用任务查询 Service
@Autowired
private TaskCommonMapper taskCommonMapper;
/**
* 查询单个区的统计信息
*/
@@ -84,6 +89,84 @@ public class StatisticsService {
return result;
}
/**
* 按街道统计(传入区名称)
* 返回 Map<街道名, PlotStatsDTO>
*/
public Map<String, PlotStatsDTO> getStreetStatsByDistrict(String district) {
// 1⃣ 获取该区所有任务
List<UnitInfo> tasksInDistrict = unitInfoMapper.findByDistrict(district);
// 防止该区没有数据
if (tasksInDistrict == null || tasksInDistrict.isEmpty()) {
return new LinkedHashMap<>();
}
// 2⃣ 按街道分组
Map<String, List<UnitInfo>> groupedByStreet =
tasksInDistrict.stream().collect(Collectors.groupingBy(UnitInfo::getVillage));
Map<String, PlotStatsDTO> result = new LinkedHashMap<>();
// 3⃣ 遍历每个街道,累加统计
for (Map.Entry<String, List<UnitInfo>> entry : groupedByStreet.entrySet()) {
String street = entry.getKey();
List<UnitInfo> streetTasks = entry.getValue();
// 初始化统计对象
PlotStatsDTO total = new PlotStatsDTO(0, 0.0, 0, 0, 0, 0);
// 遍历该街道的所有 schema
for (UnitInfo task : streetTasks) {
String schema = task.getSchemaCode();
PlotStatsDTO stats = baseQueryService.queryPlotStats(schema);
if (stats != null) {
total.setPlotCount(total.getPlotCount() + safeInt(stats.getPlotCount()));
total.setTotalArea(total.getTotalArea() + safeDouble(stats.getTotalArea()));
total.setUnPassedCount(total.getUnPassedCount() + safeInt(stats.getUnPassedCount()));
total.setAssignedCount(total.getAssignedCount() + safeInt(stats.getAssignedCount()));
total.setCollectedCount(total.getCollectedCount() + safeInt(stats.getCollectedCount()));
total.setApprovedCount(total.getApprovedCount() + safeInt(stats.getApprovedCount()));
}
}
// 添加到结果 Map
result.put(street, total);
}
return result;
}
public List<PlotBoundaryDTO> getAllBoundaries() {
List<PlotBoundaryDTO> totalBoundaries = new ArrayList<>();
// 1. 查询所有任务信息 (UnitInfo)
List<UnitInfo> allTasks = unitInfoMapper.findAll();
// 2. 遍历每个任务,查询对应的表
for (UnitInfo task : allTasks) {
String schema = task.getSchemaCode();
try {
// 调用 Mapper 查询该 schema 下的小班
List<PlotBoundaryDTO> boundaries = taskCommonMapper.selectAllPlotBoundaries(schema);
if (boundaries != null && !boundaries.isEmpty()) {
totalBoundaries.addAll(boundaries);
}
} catch (Exception e) {
// 捕获异常(例如某个表不存在),打印日志但不中断整个流程
System.err.println("查询 schema 失败: " + schema + ", 错误: " + e.getMessage());
}
}
return totalBoundaries;
}
// ✅ 防止 null 值累加
private int safeInt(Integer value) {
return (value != null) ? value : 0;

View File

@@ -2,9 +2,9 @@ spring.application.name=LydcStatistic
spring.profiles.active=dev
server.port=9001
server.address=0.0.0.0
spring.datasource.host=127.0.0.1
spring.datasource.host=120.48.89.193
spring.datasource.port=5432
spring.datasource.database=tj_project
spring.datasource.database=lydc_statistic
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${spring.datasource.host}:${spring.datasource.port}/${spring.datasource.database}?useSSL=true&allowMultiQueries=true
spring.datasource.username=postgres

View File

@@ -40,4 +40,12 @@
WHERE plot_id = #{plotId}
</select>
<!-- 所有图斑id和shape -->
<select id="selectAllPlotBoundaries" resultType="com.whu.edu.LyStatistic.MapLyStatistic.Dto.PlotBoundaryDTO">
SELECT
"ID" AS id,
"shape" AS shapeGeoJson
FROM "${schema}".roottable1
</select>
</mapper>