文旅景点,资讯, 商家店铺,旅游团
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package org.springblade.common.handler;
|
||||
|
||||
import org.apache.ibatis.type.BaseTypeHandler;
|
||||
import org.apache.ibatis.type.JdbcType;
|
||||
import org.apache.ibatis.type.MappedJdbcTypes;
|
||||
import org.apache.ibatis.type.MappedTypes;
|
||||
import org.locationtech.jts.geom.Coordinate;
|
||||
import org.locationtech.jts.geom.GeometryFactory;
|
||||
import org.locationtech.jts.geom.Point;
|
||||
import org.locationtech.jts.io.ParseException;
|
||||
import org.locationtech.jts.io.WKTReader;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
@MappedJdbcTypes(JdbcType.OTHER) // 对应 PG 的 point 类型
|
||||
@MappedTypes(Point.class)
|
||||
public class PgJtsPointTypeHandler extends BaseTypeHandler<Point> {
|
||||
|
||||
private static final GeometryFactory FACTORY = new GeometryFactory();
|
||||
private static final WKTReader WKT_READER = new WKTReader(FACTORY);
|
||||
|
||||
@Override
|
||||
public void setNonNullParameter(PreparedStatement ps, int i, Point parameter, JdbcType jdbcType) throws SQLException {
|
||||
if (parameter == null) {
|
||||
ps.setNull(i, java.sql.Types.OTHER);
|
||||
return;
|
||||
}
|
||||
|
||||
double x = parameter.getX(); // 经度
|
||||
double y = parameter.getY(); // 纬度
|
||||
|
||||
// 【修正】使用 WKT 格式:POINT(经度 纬度)
|
||||
// 注意:中间是空格,不是逗号;必须有 POINT 前缀
|
||||
String wkt = String.format("POINT(%f %f)", x, y);
|
||||
|
||||
// 打印日志方便调试(可选)
|
||||
// System.out.println("Inserting WKT: " + wkt);
|
||||
|
||||
// 将 WKT 字符串传入,PostGIS 会自动识别并转换
|
||||
ps.setObject(i, wkt, java.sql.Types.OTHER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
// 关键:获取字符串并解析
|
||||
return parse(rs.getString(columnName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
return parse(rs.getString(columnIndex));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Point getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
return parse(cs.getString(columnIndex));
|
||||
}
|
||||
|
||||
private Point parse(String value) throws SQLException {
|
||||
if (value == null || value.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 1. 尝试直接作为 WKT 解析 (兼容 "POINT(x y)")
|
||||
try {
|
||||
return (Point) WKT_READER.read(value);
|
||||
} catch (ParseException e) {
|
||||
// 2. 如果失败,尝试解析 PG 原生格式 "(x,y)"
|
||||
// PG 原生格式不是标准 WKT,WKTReader 会报错,进入这里
|
||||
if (value.startsWith("(") && value.endsWith(")")) {
|
||||
try {
|
||||
String content = value.substring(1, value.length() - 1);
|
||||
String[] parts = content.split(",");
|
||||
if (parts.length == 2) {
|
||||
double x = Double.parseDouble(parts[0].trim());
|
||||
double y = Double.parseDouble(parts[1].trim());
|
||||
return FACTORY.createPoint(new Coordinate(x, y));
|
||||
}
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new SQLException("Invalid Point format: " + value, ex);
|
||||
}
|
||||
}
|
||||
throw new SQLException("Failed to parse Point: " + value, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.springblade.common.utils;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class IdCardValidatorUtil {
|
||||
|
||||
private static final Pattern REGEX_18 = Pattern.compile("^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[\\dXx]$");
|
||||
|
||||
// 加权因子
|
||||
private static final int[] WEIGHTS = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
||||
// 校验码映射
|
||||
private static final String[] CHECK_CODES = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
|
||||
|
||||
public static Boolean validateIdCard(String idCard) {
|
||||
if (StringUtils.isBlank(idCard)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 格式初筛 (15位直接淘汰,只留18位)
|
||||
if (!REGEX_18.matcher(idCard).matches()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 核心算法校验 (校验最后一位是否正确)
|
||||
return verifyCheckSum(idCard.toUpperCase());
|
||||
}
|
||||
|
||||
/**
|
||||
* ISO 7064:1983.MOD 11-2 校验算法
|
||||
*/
|
||||
private static boolean verifyCheckSum(String idCard) {
|
||||
int sum = 0;
|
||||
// 只取前17位进行计算
|
||||
for (int i = 0; i < 17; i++) {
|
||||
int digit = Character.getNumericValue(idCard.charAt(i));
|
||||
if (digit < 0 || digit > 9) {
|
||||
return false;
|
||||
}
|
||||
sum += digit * WEIGHTS[i];
|
||||
}
|
||||
|
||||
int mod = sum % 11;
|
||||
String expectedCheckCode = CHECK_CODES[mod];
|
||||
String actualCheckCode = String.valueOf(idCard.charAt(17));
|
||||
|
||||
return expectedCheckCode.equals(actualCheckCode);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user