From 992fcc6bcb45d4c800567d0681195b905f16c8d364433122a483e57a4c37c32d Mon Sep 17 00:00:00 2001 From: attiya <2413103649@qq.com> Date: Tue, 21 Oct 2025 14:29:10 +0800 Subject: [PATCH] =?UTF-8?q?GPS=E5=9D=90=E6=A0=87=E7=B3=BB=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cdzy/common/utils/ConvertUtil.java | 3 +- .../handler/PGpointTypeHandler.java | 60 +++++++++++++++++-- .../model/entity/EbikeBikeInfo.java | 12 +++- .../impl/EbikeBikeInfoServiceImpl.java | 25 ++++++-- .../impl/EbikeInventoryServiceImpl.java | 4 +- .../EbikeOperationsApplicationTests.java | 14 +++-- 6 files changed, 96 insertions(+), 22 deletions(-) diff --git a/ebike-common/src/main/java/com/cdzy/common/utils/ConvertUtil.java b/ebike-common/src/main/java/com/cdzy/common/utils/ConvertUtil.java index 0cd6060..650b560 100644 --- a/ebike-common/src/main/java/com/cdzy/common/utils/ConvertUtil.java +++ b/ebike-common/src/main/java/com/cdzy/common/utils/ConvertUtil.java @@ -7,8 +7,7 @@ import java.time.format.DateTimeFormatter; * 数据类型转换工具类 * * @author dingchao - * @date 2025/3/24 - * @modified by: + * @since 2025/3/24 */ public class ConvertUtil { diff --git a/ebike-operations/src/main/java/com/cdzy/operations/handler/PGpointTypeHandler.java b/ebike-operations/src/main/java/com/cdzy/operations/handler/PGpointTypeHandler.java index edaaed9..a259b17 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/handler/PGpointTypeHandler.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/handler/PGpointTypeHandler.java @@ -1,14 +1,18 @@ package com.cdzy.operations.handler; +import com.cdzy.common.ex.EbikeException; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.postgresql.geometric.PGpoint; import org.postgresql.util.PGobject; +import com.cdzy.common.utils.CoordinateUtil; import java.sql.*; /** - * PGpoint 类型处理器 - 直接处理 PGpoint 对象 + * PGpoint 类型处理器 - 集成 CoordinateUtil 实现坐标转换 + * 写入时:GCJ-02 → WGS-84 + * 读取时:WGS-84 → GCJ-02 */ public class PGpointTypeHandler implements TypeHandler { @@ -17,29 +21,39 @@ public class PGpointTypeHandler implements TypeHandler { if (parameter == null) { ps.setNull(i, Types.OTHER); } else { - // 直接设置 PGpoint 对象 - ps.setObject(i, parameter); + // 写入数据库时:GCJ-02 → WGS-84 + PGpoint wgs84Point = gcj02ToWgs84(parameter); + ps.setObject(i, wgs84Point); } } @Override public PGpoint getResult(ResultSet rs, String columnName) throws SQLException { Object object = rs.getObject(columnName); - return convertToPGpoint(object); + PGpoint wgs84Point = convertToPGpoint(object); + // 读取时:WGS-84 → GCJ-02 + return wgs84ToGcj02(wgs84Point); } @Override public PGpoint getResult(ResultSet rs, int columnIndex) throws SQLException { Object object = rs.getObject(columnIndex); - return convertToPGpoint(object); + PGpoint wgs84Point = convertToPGpoint(object); + // 读取时:WGS-84 → GCJ-02 + return wgs84ToGcj02(wgs84Point); } @Override public PGpoint getResult(CallableStatement cs, int columnIndex) throws SQLException { Object object = cs.getObject(columnIndex); - return convertToPGpoint(object); + PGpoint wgs84Point = convertToPGpoint(object); + // 读取时:WGS-84 → GCJ-02 + return wgs84ToGcj02(wgs84Point); } + /** + * 将数据库对象转换为 PGpoint + */ private PGpoint convertToPGpoint(Object object) { if (object == null) { return null; @@ -62,4 +76,38 @@ public class PGpointTypeHandler implements TypeHandler { return null; } + + /** + * GCJ-02 转 WGS-84(写入数据库时调用) + */ + private PGpoint gcj02ToWgs84(PGpoint gcj02Point) { + if (gcj02Point == null) { + return null; + } + + try { + // 使用 CoordinateUtil 进行坐标转换:GCJ-02 → WGS-84 + double[] wgs84 = CoordinateUtil.GCJ02ToWGS84(gcj02Point.x, gcj02Point.y); + return new PGpoint(wgs84[0], wgs84[1]); + } catch (Exception e) { + throw new RuntimeException("GCJ-02 → WGS-84 坐标系转换成错误"); + } + } + + /** + * WGS-84 转 GCJ-02(从数据库读取时调用) + */ + private PGpoint wgs84ToGcj02(PGpoint wgs84Point) { + if (wgs84Point == null) { + return null; + } + + try { + // 使用 CoordinateUtil 进行坐标转换:WGS-84 → GCJ-02 + double[] gcj02 = CoordinateUtil.WGS84ToGCJ02(wgs84Point.x, wgs84Point.y); + return new PGpoint(gcj02[0], gcj02[1]); + } catch (Exception e) { + throw new EbikeException("WGS-84 → GCJ-02 坐标系转换成错误"); + } + } } \ No newline at end of file diff --git a/ebike-operations/src/main/java/com/cdzy/operations/model/entity/EbikeBikeInfo.java b/ebike-operations/src/main/java/com/cdzy/operations/model/entity/EbikeBikeInfo.java index 85ab213..8c6affd 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/model/entity/EbikeBikeInfo.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/model/entity/EbikeBikeInfo.java @@ -1,5 +1,10 @@ package com.cdzy.operations.model.entity; +import com.cdzy.operations.handler.PGpointDeserializer; +import com.cdzy.operations.handler.PGpointSerializer; +import com.cdzy.operations.handler.PGpointTypeHandler; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Table; @@ -68,6 +73,9 @@ public class EbikeBikeInfo implements Serializable { /** * 定位 */ + @Column(typeHandler = PGpointTypeHandler.class) + @JsonSerialize(using = PGpointSerializer.class) + @JsonDeserialize(using = PGpointDeserializer.class) private PGpoint location; /** @@ -88,7 +96,7 @@ public class EbikeBikeInfo implements Serializable { /** * 创建时间 */ - @Column(onInsertValue = "now") + @Column(onInsertValue = "now()") private LocalDateTime createdAt; /** @@ -99,7 +107,7 @@ public class EbikeBikeInfo implements Serializable { /** * 修改时间 */ - @Column(onUpdateValue = "now") + @Column(onUpdateValue = "now()") private LocalDateTime updatedAt; /** diff --git a/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeInfoServiceImpl.java b/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeInfoServiceImpl.java index 2bef26d..7966c44 100644 --- a/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeInfoServiceImpl.java +++ b/ebike-operations/src/main/java/com/cdzy/operations/service/impl/EbikeBikeInfoServiceImpl.java @@ -3,12 +3,10 @@ package com.cdzy.operations.service.impl; import cn.dev33.satoken.stp.StpUtil; import com.cdzy.common.ex.EbikeException; import com.cdzy.operations.enums.BatteryStatus; +import com.cdzy.operations.enums.BikeQrStatus; import com.cdzy.operations.enums.BikeStatus; import com.cdzy.operations.mapper.*; -import com.cdzy.operations.model.entity.EbikeBatteryInfo; -import com.cdzy.operations.model.entity.EbikeBikeInfo; -import com.cdzy.operations.model.entity.EbikeEcuInfo; -import com.cdzy.operations.model.entity.EbikeInventoryRecord; +import com.cdzy.operations.model.entity.*; import com.cdzy.operations.model.vo.EbikeBikeBindVo; import com.cdzy.operations.model.vo.EbikeInventoryVo; import com.cdzy.operations.service.EbikeBikeInfoService; @@ -39,6 +37,9 @@ public class EbikeBikeInfoServiceImpl extends ServiceImpl= 0){ + throw new EbikeException("减少库存时填入数量不能大于/等于0"); } QueryWrapper queryWrapper = QueryWrapper.create() .where(EBIKE_INVENTORY.INVENTORY_TYPE.eq(ebikeInventory.getInventoryType())); diff --git a/ebike-operations/src/test/java/com/cdzy/operations/EbikeOperationsApplicationTests.java b/ebike-operations/src/test/java/com/cdzy/operations/EbikeOperationsApplicationTests.java index 9bfdc30..3efcb44 100644 --- a/ebike-operations/src/test/java/com/cdzy/operations/EbikeOperationsApplicationTests.java +++ b/ebike-operations/src/test/java/com/cdzy/operations/EbikeOperationsApplicationTests.java @@ -2,11 +2,14 @@ package com.cdzy.operations; import com.cdzy.operations.mapper.EbikeBikeInfoMapper; import com.cdzy.operations.model.entity.EbikeBikeInfo; +import com.mybatisflex.core.query.QueryWrapper; import org.junit.jupiter.api.Test; import org.postgresql.geometric.PGpoint; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import static com.cdzy.operations.model.entity.table.EbikeBikeInfoTableDef.EBIKE_BIKE_INFO; + @SpringBootTest class EbikeOperationsApplicationTests { @@ -15,11 +18,12 @@ class EbikeOperationsApplicationTests { @Test void contextLoads(){ - EbikeBikeInfo bikeInfo = new EbikeBikeInfo(); - // 创建测试点:X经度、纬度 - PGpoint point =new PGpoint(116.3974, 39.9093); - bikeInfo.setLocation(point); - ebikeBikeInfoMapper.insert(bikeInfo); + QueryWrapper queryWrapper = new QueryWrapper() + .where(EBIKE_BIKE_INFO.BIKE_INFO_ID.eq("338128115027443712")); + EbikeBikeInfo info = ebikeBikeInfoMapper.selectOneByQuery(queryWrapper); + PGpoint pGpoint = new PGpoint(104.066541, 30.572269); + info.setLocation(pGpoint); + ebikeBikeInfoMapper.update(info); } }