更新POI
This commit is contained in:
@ -0,0 +1,137 @@
|
||||
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.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
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
|
||||
StringBuilder dataSql = new StringBuilder("SELECT id, name, address, lng, lat FROM tbl_pois WHERE 1=1");
|
||||
List<Object> params = new ArrayList<>();
|
||||
// 名称搜索条件
|
||||
if (name != null && !name.trim().isEmpty()) {
|
||||
dataSql.append(" AND name LIKE ?");
|
||||
params.add("%" + name.trim() + "%");
|
||||
}
|
||||
// 分页条件
|
||||
dataSql.append(" LIMIT ? OFFSET ?");
|
||||
// 执行查询
|
||||
try (Connection connection = SQLiteUtil.getConnection(path);
|
||||
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.setAddress(dataRs.getString("address"));
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user