新需求开发

This commit is contained in:
wanghao
2025-09-17 14:30:53 +08:00
parent 3e4f5ebe3e
commit cd6b8e49e5
19 changed files with 991 additions and 44 deletions
@@ -0,0 +1,388 @@
package org.jeecg.untils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class CoordinateConverter {
public static double pi = 3.1415926535897932384626;
public static double a = 6378140.0;//1975年国际椭球体长半轴
public static double ee = 0.0033528131778969143;//1975年国际椭球体扁率
/**
* 将经纬度转换为度分秒格式或定位地图
* @param xy 经纬度字符串,格式如 "116.404,39.915"
* @param mode 1-定位地图,其他值-转换为度分秒
* @return 转换结果或错误信息
*/
public String convertToDMS(String xy, int mode) {
xy = xy.replace("°", ""); // 删除度符号
if (!isValidCoordinate(xy)) {
return "经纬度错误!请对照检查:\n"
+ "不能为空,且必须是数字;\n"
+ "经度不能大于179,不能小于-179\n"
+ "纬度不能大于89,不能小于-89。";
}
if (mode == 1) {
String[] parts = xy.split(",");
double longitude = Double.parseDouble(parts[0]);
double latitude = Double.parseDouble(parts[1]);
// 这里调用地图定位功能
return "已经定位!\n"
+ "上述经纬度位于当前地图的中心点\"+\"处,请查阅。\n"
+ "若图像不好,可换卫星图。";
} else {
return "转换结果是:\n" + convertToDegreeMinuteSecond(xy);
}
}
/**
* 检查是否是有效的经纬度
*/
private boolean isValidCoordinate(String xy) {
String[] parts = xy.split(",");
if (parts.length != 2) {
return false;
}
try {
double longitude = Double.parseDouble(parts[0]);
double latitude = Double.parseDouble(parts[1]);
if (Math.abs(longitude) > 179 || Math.abs(latitude) > 89) {
return false;
}
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**
* 将十进制经纬度转换为度分秒格式
*/
private String convertToDegreeMinuteSecond(String xy) {
String[] parts = xy.split(",");
double longitude = Double.parseDouble(parts[0]);
double latitude = Double.parseDouble(parts[1]);
String longitudeDMS = decimalToDMS(longitude, true);
String latitudeDMS = decimalToDMS(latitude, false);
return longitudeDMS + "," + latitudeDMS;
}
/**
* 将十进制转换为度分秒
* @param decimal 十进制值
* @param isLongitude 是否是经度
*/
private String decimalToDMS(double decimal, boolean isLongitude) {
String direction;
if (isLongitude) {
direction = decimal >= 0 ? "东经" : "西经";
} else {
direction = decimal >= 0 ? "北纬" : "南纬";
}
decimal = Math.abs(decimal);
int degrees = (int) decimal;
double remaining = (decimal - degrees) * 60;
int minutes = (int) remaining;
double seconds = (remaining - minutes) * 60;
return String.format("%s%d°%d'%.3f\"", direction, degrees, minutes, seconds);
}
/**
* 将度分秒转换为经纬度或定位地图
* @param dms 度分秒字符串,格式如 "东经116°23'14.000",北纬39°54'54.000"
* @param mode 1-定位地图,其他值-转换为经纬度
* @param isWest 是否是西经
* @param isSouth 是否是南纬
* @return 转换结果或错误信息
*/
public String convertToDecimal(String dms, int mode, boolean isWest, boolean isSouth) {
dms = dms.replace(",", "\",") + "\"";
if (!isValidDMS(dms)) {
return "度°分'秒\"错误!请对照检查:\n"
+ "度、分、秒不能为空,必须是数字,且不能是负数;\n"
+ "经°不能大于179,纬°不能大于89;分、秒不能大于、等于60。";
}
String longitudePrefix = isWest ? "西经" : "东经";
String latitudePrefix = isSouth ? "南纬" : "北纬";
String formattedDMS = longitudePrefix + dms.replace(",", "," + latitudePrefix);
String decimal = dmsToDecimal(formattedDMS);
// 格式化小数位数
String[] parts = decimal.split("");
double longitude = Double.parseDouble(parts[0].replace("经度", ""));
double latitude = Double.parseDouble(parts[1].replace("纬度", ""));
// 保留6位小数
String formattedLongitude = String.format("%.6f", longitude);
String formattedLatitude = String.format("%.6f", latitude);
String formattedResult = formattedLongitude + "-" + formattedLatitude;
if (mode == 1) {
// 这里调用地图定位功能
return "已经定位!\n"
+ "上述经纬度位于当前地图的中心点\"+\"处,请查阅。\n"
+ "若图像不好,可换卫星图。";
} else {
return formattedResult;
}
}
/**
* 检查是否是有效的度分秒格式
*/
private boolean isValidDMS(String dms) {
String[] parts = dms.split(",");
if (parts.length != 2) {
return false;
}
try {
String longitude = parts[0];
String latitude = parts[1];
String[] lonParts = longitude.split("[°'\"]");
String[] latParts = latitude.split("[°'\"]");
if (lonParts.length < 3 || latParts.length < 3) {
return false;
}
double lonDegrees = Double.parseDouble(lonParts[0]);
double lonMinutes = Double.parseDouble(lonParts[1]);
double lonSeconds = Double.parseDouble(lonParts[2]);
double latDegrees = Double.parseDouble(latParts[0]);
double latMinutes = Double.parseDouble(latParts[1]);
double latSeconds = Double.parseDouble(latParts[2]);
if (lonDegrees > 179 || lonDegrees < 0 || latDegrees > 89 || latDegrees < 0) {
return false;
}
if (lonMinutes >= 60 || lonMinutes < 0 || lonSeconds >= 60 || lonSeconds < 0 ||
latMinutes >= 60 || latMinutes < 0 || latSeconds >= 60 || latSeconds < 0) {
return false;
}
return true;
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
return false;
}
}
/**
* 将度分秒转换为十进制
*/
private String dmsToDecimal(String dms) {
String[] parts = dms.split(",");
String longitude = parts[0];
String latitude = parts[1];
double lonDecimal = parseDMS(longitude);
double latDecimal = parseDMS(latitude);
// 直接返回未格式化的原始值,由convertToDecimal统一处理格式
return lonDecimal + "" + latDecimal;
}
/**
* 解析度分秒字符串为十进制
*/
private double parseDMS(String dms) {
boolean isNegative = dms.startsWith("西经") || dms.startsWith("南纬");
dms = dms.replaceAll("[东经西经北纬南纬]", "");
String[] parts = dms.split("[°'\"]");
double degrees = Double.parseDouble(parts[0]);
double minutes = Double.parseDouble(parts[1]);
double seconds = Double.parseDouble(parts[2]);
double decimal = degrees + minutes / 60 + seconds / 3600;
return isNegative ? -decimal : decimal;
}
/**
* 批量转换度分秒坐标为十进制度坐标
* @param coordinates 坐标数组,每个元素格式为 "东经E:84°5215.564″ 北纬N45°3553.948″"
* @return 转换结果列表
*/
public List<String> batchConvertToDecimal(String[] coordinates) {
List<String> results = new ArrayList<>();
for (int i = 0; i < coordinates.length; i++) {
try {
String coord = coordinates[i];
// 分割经度和纬度
String[] parts = coord.split("\t");
if (parts.length < 2) {
parts = coord.split("\\s+");
}
if (parts.length >= 2) {
String dj = parts[0].trim();
String bw = parts[1].trim();
// 清洗数据格式
String cleanDj = cleanCoordinateString(dj, true);
String cleanBw = cleanCoordinateString(bw, false);
// 组合成标准DMS格式
String dms = cleanDj + "," + cleanBw;
// 转换并添加到结果列表
String result = convertToDecimal(dms, 0, false, false);
results.add((i + 1) + ". " + result.replace("转换结果是:\n", ""));
} else {
results.add((i + 1) + ". 格式错误: " + coord);
}
} catch (Exception e) {
results.add((i + 1) + ". 转换失败: " + coordinates[i] + " - 错误: " + e.getMessage());
}
}
return results;
}
/**
* 清洗坐标字符串
*/
private String cleanCoordinateString(String coord, boolean isLongitude) {
// 替换各种符号为统一格式
String cleaned = coord.replace("", ":")
.replace("", "'")
.replace("", "\"")
.replace("", "'")
.replace("'", "'")
.replace("\"", "\"")
.replace(" ", "");
// 移除前缀
if (isLongitude) {
cleaned = cleaned.replaceAll("[东经E]", "");
} else {
cleaned = cleaned.replaceAll("[北纬N]", "");
}
// 移除冒号
cleaned = cleaned.replace(":", "").trim();
return cleaned;
}
public String run(String msg){
String[] split = msg.split("-");
String dj = split[0];
String bw = split[1];
String cleanDj = dj.replace("", ":")
.replace("", "'")
.replace("", "\"")
.replaceAll("[东经E]", "")
.replaceAll("[东经N]", "")
.replace(":", "").trim();
String cleanBw = bw.replace("", ":")
.replace("", "'")
.replace("", "\"")
.replaceAll("[北纬N]", "")
.replaceAll("[北纬E]", "")
.replace(":", "").trim();
String dms = cleanDj + "," + cleanBw;
return convertToDecimal(dms, 0, false, false);
}
public static Gps GPS84ToGCJ02(double lon, double lat) {
if (outOfChina(lon, lat)) {
return null;
}
double dLat = transformLat(lon - 105.0, lat - 35.0);
double dLon = transformLon(lon - 105.0, lat - 35.0);
double radLat = lat / 180.0 * pi;
double magic = Math.sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = Math.sqrt(magic);
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
double mgLat = lat + dLat;
double mgLon = lon + dLon;
return new Gps(mgLon, mgLat);
}
private static boolean outOfChina(double lon, double lat) {
if (lon < 72.004 || lon > 137.8347)
return true;
return lat < 0.8293 || lat > 55.8271;
}
private static double transformLat(double x, double y) {
double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
+ 0.2 * Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
return ret;
}
private static double transformLon(double x, double y) {
double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
* Math.sqrt(Math.abs(x));
ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
* pi)) * 2.0 / 3.0;
return ret;
}
public static class Gps {
private String id;
private double longitude;
private double latitude;
public Gps(double lon, double lat) {
this.longitude = lon;
this.latitude = lat;
}
public Gps() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
}
}
@@ -913,4 +913,8 @@ public class SysDepartController {
return sysDepartService.queryDepartNameByOrgCodes(orgCodeSet);
}
@GetMapping("/departDataInit")
public void departDataInit(){
sysDepartService.departDataInit();
}
}
@@ -264,4 +264,6 @@ public interface ISysDepartService extends IService<SysDepart> {
List<BaseUser> queryUserByOrgCode(String orgCode);
Map<String, String> queryDepartNameByOrgCodes(Set<String> orgCodeSet);
void departDataInit();
}
@@ -15,6 +15,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstant;
@@ -38,11 +43,14 @@ import org.jeecg.modules.system.manager.UserCacheManager;
import org.jeecg.modules.system.mapper.*;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.untils.CoordinateConverter;
import org.jeecg.modules.system.util.FindsDepartsChildrenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
@@ -82,6 +90,8 @@ public class SysDepartServiceImpl extends ServiceImpl<SysDepartMapper, SysDepart
private SysUserRoleMapper sysUserRoleMapper;
@Autowired
private UserCacheManager userCacheManager;
@Autowired
private CoordinateConverter converter;
@Override
//todo departids字段做了调整,这里功能需要的话,后面再改
@@ -1520,4 +1530,135 @@ public class SysDepartServiceImpl extends ServiceImpl<SysDepartMapper, SysDepart
return departMapper.selectDepartGpsByDepartId(departId);
}
@Override
@Transactional
public void departDataInit() {
String filePath = "D:\\Project\\Java\\rk\\文档\\新疆油田-24\\各二级单位坐标位置更正统计9.15.xlsx";
// 获取目前的二级单位数据
List<SysDepart> secondDeparts = departMapper
.selectList(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getParentId, "1683768826307096578"));
// 二级单位名称分组
Map<String, SysDepart> secondMap = secondDeparts.stream()
.collect(Collectors.toMap(SysDepart::getDepartName, depart -> depart, (existing, replacement) -> existing));
// 获取目前的三级部门数据
List<SysDepart> threeDeparts = departMapper
.selectList(new LambdaQueryWrapper<SysDepart>()
.in(SysDepart::getParentId, secondDeparts.stream().map(SysDepart::getId).collect(Collectors.toList())));
// 三级部门名称分组
Map<String, SysDepart> threeMap = threeDeparts.stream()
.collect(Collectors.toMap(SysDepart::getDepartName, depart -> depart, (existing, replacement) -> existing));
// 获取目前的四级部门数据
List<SysDepart> fourDeparts = departMapper
.selectList(new LambdaQueryWrapper<SysDepart>()
.in(SysDepart::getParentId, threeDeparts.stream().map(SysDepart::getId).collect(Collectors.toList())));
// 三级部门名称分组
Map<String, SysDepart> fourMap = fourDeparts.stream()
.collect(Collectors.toMap(SysDepart::getDepartName, depart -> depart, (existing, replacement) -> existing));
List<SysDepart> secondDepartList = new ArrayList<>();
List<SysDepart> threeDepartList = new ArrayList<>();
List<SysDepart> fourDepartList = new ArrayList<>();
try (FileInputStream file = new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(file)) {
// 获取第一个工作表
XSSFSheet sheet = workbook.getSheetAt(0);
// 从第二行开始(索引为1
for (int i = 2; i <= sheet.getLastRowNum(); i++) {
XSSFRow row = sheet.getRow(i);
if (row != null) {
// 获取第一个单元格(索引0)的数据 二级单位名称
XSSFCell firstCell = row.getCell(0);
String firstCellValue = firstCell != null ? getCellValue(firstCell) : "[空]";
// 获取第二个单元格(索引1)的数据 三级部门名称
XSSFCell secondCell = row.getCell(1);
String secondCellValue = secondCell != null ? getCellValue(secondCell) : "[空]";
// 获取第三个单元格(索引1)的数据 经度
XSSFCell threeCell = row.getCell(2);
String threeCellValue = secondCell != null ? getCellValue(threeCell) : "[空]";
// 获取第四个单元格(索引1)的数据 经度
XSSFCell fourCell = row.getCell(3);
String fourCellValue = secondCell != null ? getCellValue(fourCell) : "[空]";
if (StrUtil.isNotBlank(firstCellValue)) {
SysDepart depart = secondMap.get(firstCellValue);
if (depart == null) {
// 现有不存在 需添加
SysDepart newDepart = new SysDepart();
newDepart.setDepartName(firstCellValue);
newDepart.setParentId("1683768826307096578");
newDepart.setOrgCategory("2");
newDepart.setOrgType("2");
this.saveDepartData(newDepart,GlobalUtils.getLoginUser().getUsername());
if (null != newDepart.getId()){
SysDepartGps departGps = new SysDepartGps();
departGps.setDepartId(newDepart.getId());
String run = converter.run(threeCellValue + "-" + fourCellValue);
System.out.println(run);
// // 经度
// departGps.setLng();
// // 纬度
// departGps.setLat();
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("读取Excel文件失败: " + e.getMessage());
}
}
/**
* 获取单元格的值
*/
private String getCellValue(XSSFCell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue().trim();
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
} else {
// 处理数字格式,避免科学计数法
double numericValue = cell.getNumericCellValue();
if (numericValue == Math.floor(numericValue)) {
// 如果是整数
return String.valueOf((long) numericValue);
} else {
// 如果是小数
return String.valueOf(numericValue);
}
}
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
try {
return String.valueOf(cell.getNumericCellValue());
} catch (Exception e) {
return cell.getStringCellValue().trim();
}
case BLANK:
return "";
default:
return "";
}
}
}