Files
yjearth/src/main/java/com/yj/earth/business/controller/PoiInfoController.java

166 lines
6.4 KiB
Java
Raw Normal View History

2025-10-27 17:18:33 +08:00
package com.yj.earth.business.controller;
import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yj.earth.business.domain.PoiInfo;
import com.yj.earth.business.service.PoiInfoService;
import com.yj.earth.common.util.ApiResponse;
import com.yj.earth.common.util.SQLiteUtil;
import com.yj.earth.vo.PoiVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
2025-10-30 15:30:14 +08:00
import org.sqlite.JDBC;
2025-10-27 17:18:33 +08:00
import javax.annotation.Resource;
2025-10-30 15:30:14 +08:00
import java.io.File;
import java.sql.*;
2025-10-27 17:18:33 +08:00
import java.util.ArrayList;
import java.util.List;
@RestController
@Tag(name = "POI文件管理")
@RequestMapping("/poiInfo")
public class PoiInfoController {
@Resource
private PoiInfoService poiInfoService;
@Operation(summary = "添加POI文件")
@PostMapping("/add")
public ApiResponse add(@Parameter(description = "POI文件路径") @RequestParam(required = true) String path) {
PoiInfo poiInfo = new PoiInfo();
poiInfo.setPath(path);
poiInfo.setName(FileUtil.mainName(path));
poiInfo.setIsEnable(0);
poiInfoService.save(poiInfo);
return ApiResponse.success(null);
}
@Operation(summary = "删除POI文件")
@PostMapping("/delete")
public ApiResponse delete(@Parameter(description = "POI文件ID") @RequestParam(required = true) String id) {
poiInfoService.removeById(id);
return ApiResponse.success(null);
}
@Operation(summary = "获取所有POI文件")
@GetMapping("/list")
public ApiResponse list() {
LambdaQueryWrapper<PoiInfo> queryWrapper = new QueryWrapper<PoiInfo>().lambda();
// 把启用的排在最前面
queryWrapper.orderByDesc(PoiInfo::getIsEnable);
queryWrapper.orderByAsc(PoiInfo::getCreatedAt);
return ApiResponse.success(poiInfoService.list(queryWrapper));
}
@Operation(summary = "启用POI文件")
@PostMapping("/enable")
public ApiResponse enable(@Parameter(description = "地图文件ID") @RequestParam(required = true) String id) {
PoiInfo poiInfo = poiInfoService.getById(id);
poiInfo.setIsEnable(1);
poiInfoService.updateById(poiInfo);
// 除这条数据以外的全部为禁用
List<PoiInfo> list = poiInfoService.list();
for (PoiInfo info : list) {
if (!info.getId().equals(id)) {
info.setIsEnable(0);
poiInfoService.updateById(info);
}
}
return ApiResponse.success(null);
}
@Operation(summary = "禁用POI文件")
@PostMapping("/disable")
public ApiResponse disable(@Parameter(description = "地图文件ID") @RequestParam(required = true) String id) {
PoiInfo poiInfo = new PoiInfo();
poiInfo.setId(id);
poiInfo.setIsEnable(0);
poiInfoService.updateById(poiInfo);
return ApiResponse.success(null);
}
@Operation(summary = "POI搜索")
@GetMapping("/search")
public ApiResponse search(
@Parameter(description = "分页页码") Integer pageNum,
@Parameter(description = "分页大小") Integer pageSize,
@Parameter(description = "名称搜索") String name) {
int offset = (pageNum - 1) * pageSize;
// 查询启用的POI文件
PoiInfo poiInfo = poiInfoService.getOne(new QueryWrapper<PoiInfo>().lambda().eq(PoiInfo::getIsEnable, 1));
if (poiInfo == null) {
return ApiResponse.failure("未找到启用的POI数据文件");
}
String path = poiInfo.getPath();
// 构建查询SQL
2025-10-30 15:30:14 +08:00
StringBuilder dataSql = new StringBuilder("SELECT id, name, lng, lat FROM data WHERE 1=1");
2025-10-27 17:18:33 +08:00
List<Object> params = new ArrayList<>();
// 名称搜索条件
if (name != null && !name.trim().isEmpty()) {
dataSql.append(" AND name LIKE ?");
params.add("%" + name.trim() + "%");
}
// 分页条件
dataSql.append(" LIMIT ? OFFSET ?");
// 执行查询
2025-10-30 15:30:14 +08:00
try (Connection connection = getConnectionByAbsolutePath(path);
2025-10-27 17:18:33 +08:00
PreparedStatement dataPs = connection.prepareStatement(dataSql.toString())) {
// 设置参数
int paramIndex = 1;
for (Object param : params) {
dataPs.setObject(paramIndex++, param);
}
dataPs.setInt(paramIndex++, pageSize);
dataPs.setInt(paramIndex++, offset);
// 处理结果集
List<PoiVo> poiList = new ArrayList<>();
try (ResultSet dataRs = dataPs.executeQuery()) {
while (dataRs.next()) {
PoiVo poi = new PoiVo();
poi.setId(dataRs.getLong("id"));
poi.setName(dataRs.getString("name"));
poi.setLng(dataRs.getString("lng"));
poi.setLat(dataRs.getString("lat"));
poiList.add(poi);
}
}
return ApiResponse.success(poiList);
} catch (Exception e) {
return ApiResponse.failure("POI数据查询失败" + e.getMessage());
}
}
2025-10-30 15:30:14 +08:00
/**
* 根据绝对路径获取SQLite数据库连接
*/
public static Connection getConnectionByAbsolutePath(String absolutePath) throws SQLException {
if (absolutePath == null || absolutePath.trim().isEmpty()) {
throw new SQLException("数据库文件路径不能为空");
}
File dbFile = new File(absolutePath.trim());
if (!dbFile.exists()) {
throw new SQLException("数据库文件不存在:" + absolutePath);
}
if (!dbFile.isFile()) {
throw new SQLException("路径指向的不是文件:" + absolutePath);
}
if (!dbFile.canRead()) {
throw new SQLException("没有权限读取数据库文件:" + absolutePath);
}
try {
Class.forName(JDBC.class.getName());
} catch (ClassNotFoundException e) {
throw new SQLException("SQLite驱动加载失败、请检查依赖是否正确", e);
}
String jdbcUrl = "jdbc:sqlite:" + absolutePath;
Connection connection = DriverManager.getConnection(jdbcUrl);
return connection;
}
2025-10-27 17:18:33 +08:00
}