make code style better

main
PeterAlbus 3 years ago
parent 068af770ee
commit 2b6951e386

2
.gitignore vendored

@ -32,4 +32,4 @@ build/
### VS Code ### ### VS Code ###
.vscode/ .vscode/
/src/main/resources/5963105_www.peteralbus.com.pfx /src/main/resources/5963105_www.peteralbus.com.pfx
/src/main/resources/application.yml.example /src/main/resources/application.yml

@ -16,6 +16,10 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/**
* The type Estimate controller.
* @author PeterAlbus
*/
@CrossOrigin @CrossOrigin
@RestController @RestController
@RequestMapping("/estimate") @RequestMapping("/estimate")
@ -25,6 +29,9 @@ public class EstimateController {
*/ */
EstimateUtil estimateUtil; EstimateUtil estimateUtil;
/**
* The Estimate service.
*/
EstimateService estimateService; EstimateService estimateService;
/** /**
* The Earthquake info service. * The Earthquake info service.
@ -41,6 +48,7 @@ public class EstimateController {
{ {
this.earthquakeInfoService = earthquakeInfoService; this.earthquakeInfoService = earthquakeInfoService;
} }
/** /**
* Sets estimate util. * Sets estimate util.
* *
@ -51,10 +59,11 @@ public class EstimateController {
{ {
this.estimateUtil = estimateUtil; this.estimateUtil = estimateUtil;
} }
/** /**
* Sets estimate service. * Sets estimate service.
* *
* @param estimateService * @param estimateService the service of estimate
*/ */
@Autowired @Autowired
public void setAnalyzeService(EstimateService estimateService) { public void setAnalyzeService(EstimateService estimateService) {
@ -72,7 +81,8 @@ public class EstimateController {
System.out.println(earthquakeId); System.out.println(earthquakeId);
int count = estimateService.queryAnalyze(earthquakeId); int count = estimateService.queryAnalyze(earthquakeId);
Estimate estimate = new Estimate(); Estimate estimate = new Estimate();
if(count==0){ if(count==0)
{
Map<String, Object> mapParameter = new HashMap<String, Object>(); Map<String, Object> mapParameter = new HashMap<String, Object>();
mapParameter.put("earthquakeId",earthquakeId); mapParameter.put("earthquakeId",earthquakeId);
EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0); EarthquakeInfo earthquakeInfo=earthquakeInfoService.queryInfoWithLine(mapParameter).get(0);
@ -84,7 +94,8 @@ public class EstimateController {
shortRadius = earthquakeInfo.getIntensityLineList().get(2).getShortRadius(); shortRadius = earthquakeInfo.getIntensityLineList().get(2).getShortRadius();
System.out.println(longRadius); System.out.println(longRadius);
LocalDateTime earthquakeTime = earthquakeInfo.getEarthquakeTime(); LocalDateTime earthquakeTime = earthquakeInfo.getEarthquakeTime();
double radians = Math.toRadians(latitude);//将角度转换为弧度。 //将角度转换为弧度。
double radians = Math.toRadians(latitude);
double minLongitude = longitude-shortRadius/(111-Math.cos(radians)), double minLongitude = longitude-shortRadius/(111-Math.cos(radians)),
maxLongitude = longitude+shortRadius/(111-Math.cos(radians)), maxLongitude = longitude+shortRadius/(111-Math.cos(radians)),
minLatitude = latitude-longRadius/111, minLatitude = latitude-longRadius/111,
@ -94,9 +105,9 @@ public class EstimateController {
estimate.setPredictEconomy(estimateUtil.economyPredict(earthquakeInfo.getHighIntensity())); estimate.setPredictEconomy(estimateUtil.economyPredict(earthquakeInfo.getHighIntensity()));
estimate.setEarthquakeId(earthquakeId); estimate.setEarthquakeId(earthquakeId);
estimate.setPopulation(population); estimate.setPopulation(population);
Date date = new Date(); LocalDateTime date = LocalDateTime.now();
estimate.setGmt_time(date); estimate.setGmtCreate(date);
estimateService.insertAnalyze(estimate); int result=estimateService.insertAnalyze(estimate);
} }
else else
{ {

@ -10,28 +10,63 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
/**
* The type Hospital controller.
* @author PeterAlbus
*/
@CrossOrigin @CrossOrigin
@RestController @RestController
public class HospitalController { public class HospitalController {
@Autowired
/**
* The Hospital service.
*/
HospitalService hospitalService; HospitalService hospitalService;
/**
* Sets hospital service.
*
* @param hospitalService the hospital service
*/
@Autowired
public void setHospitalService(HospitalService hospitalService)
{
this.hospitalService = hospitalService;
}
/**
* Find all hospital list.
*
* @return the list
*/
@RequestMapping("/findAllHospital") @RequestMapping("/findAllHospital")
public List<Hospital> findAllHospital(){ public List<Hospital> findAllHospital(){
return hospitalService.findAllHospital(); return hospitalService.findAllHospital();
} }
/**
* Calculate distance distance.
*
* @param lng the lng
* @param lat the lat
* @return the distance
*/
@RequestMapping("/calculateDistance") @RequestMapping("/calculateDistance")
public Distance calculateDistance(double lng, double lat){//输入经纬度的值遍历所有Hosiptai的经纬度并比较其距离 public Distance calculateDistance(double lng, double lat)
// return getDistance(121.446014,31.215937,121.446028464238,31.2158502442799 ); {
//输入经纬度的值遍历所有Hospital的经纬度并比较其距离
//return getDistance(121.446014,31.215937,121.446028464238,31.2158502442799 );
System.out.print("进入函数calculate"); System.out.print("进入函数calculate");
List<Hospital> hospitals=hospitalService.findAllHospital(); List<Hospital> hospitals=hospitalService.findAllHospital();
double minDistance=Double.MAX_VALUE; Double minDistance=Double.MAX_VALUE;
Distance distance=new Distance(); Distance distance=new Distance();
for(int i=0;i<hospitals.size();i++){ for (Hospital hospital : hospitals)
Hospital hospital=hospitals.get(i); {
double distanceTwoPlaces=getDistance(hospital.getLon(),hospital.getLat(),lng,lat); Double distanceTwoPlaces = getDistance(hospital.getLon(), hospital.getLat(), lng, lat);
System.out.println(distanceTwoPlaces); System.out.println(distanceTwoPlaces);
minDistance=Math.min(minDistance,distanceTwoPlaces); minDistance = Math.min(minDistance, distanceTwoPlaces);
if(minDistance==distanceTwoPlaces){ if (minDistance.equals(distanceTwoPlaces))
{
distance.setDistance(minDistance); distance.setDistance(minDistance);
distance.setEndLon(hospital.getLon()); distance.setEndLon(hospital.getLon());
distance.setEndLat(hospital.getLat()); distance.setEndLat(hospital.getLat());
@ -44,6 +79,16 @@ public class HospitalController {
return distance; return distance;
} }
private static final double EARTH_RADIUS = 6378.137; private static final double EARTH_RADIUS = 6378.137;
/**
* Gets distance.
*
* @param longitude1 the longitude 1
* @param latitude1 the latitude 1
* @param longitude2 the longitude 2
* @param latitude2 the latitude 2
* @return the distance
*/
public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) { public static double getDistance(double longitude1, double latitude1, double longitude2, double latitude2) {
// 纬度 // 纬度
double lat1 = Math.toRadians(latitude1); double lat1 = Math.toRadians(latitude1);
@ -56,16 +101,25 @@ public class HospitalController {
// 经度之差 // 经度之差
double b = lng1 - lng2; double b = lng1 - lng2;
// 计算两点距离的公式 // 计算两点距离的公式
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
// 弧长乘地球半径, 返回单位: 千米 // 弧长乘地球半径, 返回单位: 千米
s = s * EARTH_RADIUS; s = s * EARTH_RADIUS;
return s; return s;
} }
// Haversine公式的最终实现方式可以有多种比如借助转角度的函数atan2
public static double getDistance2(double longitude1, double latitude1,
double longitude2, double latitude2) {
/**
* Gets distance 2.
*
* @param longitude1 the longitude 1
* @param latitude1 the latitude 1
* @param longitude2 the longitude 2
* @param latitude2 the latitude 2
* @return the distance 2
*/
public static double getDistance2(double longitude1, double latitude1, double longitude2, double latitude2)
{
//Haversine公式的最终实现方式可以有多种比如借助转角度的函数atan2
double latDistance = Math.toRadians(longitude1 - longitude2); double latDistance = Math.toRadians(longitude1 - longitude2);
double lngDistance = Math.toRadians(latitude1 - latitude2); double lngDistance = Math.toRadians(latitude1 - latitude2);

@ -4,15 +4,19 @@ import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/**
* The type Distance.
* @author PeterAlbus
*/
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class Distance { public class Distance {
private double startLon; private Double startLon;
private double startLat; private Double startLat;
private double endLon; private Double endLon;
private double endLat; private Double endLat;
private double distance; private Double distance;
private String endName; private String endName;
private String endAddress; private String endAddress;
} }

@ -29,6 +29,7 @@ import java.util.List;
public class EarthquakeInfo implements Serializable public class EarthquakeInfo implements Serializable
{ {
@TableId(type= IdType.ASSIGN_ID) @TableId(type= IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long earthquakeId; private Long earthquakeId;
private String earthquakeName; private String earthquakeName;
private Double magnitude; private Double magnitude;

@ -3,11 +3,15 @@ package com.peteralbus.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString; import lombok.ToString;
import org.apache.tomcat.jni.Local;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
@ToString @ToString
@ -16,10 +20,14 @@ import java.util.Date;
@NoArgsConstructor @NoArgsConstructor
public class Estimate { public class Estimate {
@TableId(type= IdType.ASSIGN_ID) @TableId(type= IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long analyzeId; private Long analyzeId;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long earthquakeId; private Long earthquakeId;
private Double predictDeath; private Double predictDeath;
private Double predictEconomy; private Double predictEconomy;
private int population; private Integer population;
private Date gmt_time; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
private LocalDateTime gmtCreate;
} }

@ -1,16 +1,22 @@
package com.peteralbus.entity; package com.peteralbus.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
/**
* The type Hospital.
* @author PeterAlbus
*/
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@TableName("fire_center")
public class Hospital { public class Hospital {
private int id; private Integer id;
private double lon; private Double lon;
private double lat; private Double lat;
private String name; private String name;
private String address; private String address;
private String pname; private String pname;

@ -2,6 +2,7 @@ package com.peteralbus.entity;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@ -21,11 +22,13 @@ import java.io.Serializable;
public class IntensityLine implements Serializable public class IntensityLine implements Serializable
{ {
@TableId(type= IdType.ASSIGN_ID) @TableId(type= IdType.ASSIGN_ID)
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long lineId; private Long lineId;
private Double longRadius; private Double longRadius;
private Double shortRadius; private Double shortRadius;
private Double angle; private Double angle;
private Integer intensity; private Integer intensity;
@JsonFormat(shape = JsonFormat.Shape.STRING)
private Long earthquakeId; private Long earthquakeId;
public IntensityLine(Double longRadius, Double shortRadius, Double angle, Integer intensity, Long earthquakeId) public IntensityLine(Double longRadius, Double shortRadius, Double angle, Integer intensity, Long earthquakeId)

@ -1,15 +1,17 @@
package com.peteralbus.mapper; package com.peteralbus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.peteralbus.entity.Hospital; import com.peteralbus.entity.Hospital;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
@Repository /**
* The interface Hospital mapper.
* @author PeterAlbus
*/
@Mapper @Mapper
public interface HospitalMapper { public interface HospitalMapper extends BaseMapper<Hospital>
{
public List<Hospital> findAllHospital();
} }

@ -3,6 +3,9 @@ package com.peteralbus.service;
import com.peteralbus.entity.Estimate; import com.peteralbus.entity.Estimate;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
/**
* The interface Estimate service.
*/
public interface EstimateService { public interface EstimateService {
/** /**
* Get * Get
@ -14,7 +17,28 @@ public interface EstimateService {
* @return the double * @return the double
*/ */
double getPopulation(@Param("minLongitude") Double minLongitude, @Param("maxLongitude") Double maxLongitude, @Param("minLatitude") Double minLatitude, @Param("maxLatitude") Double maxLatitude); double getPopulation(@Param("minLongitude") Double minLongitude, @Param("maxLongitude") Double maxLongitude, @Param("minLatitude") Double minLatitude, @Param("maxLatitude") Double maxLatitude);
/**
* Insert analyze int.
*
* @param estimate the estimate
* @return the int
*/
int insertAnalyze(Estimate estimate); int insertAnalyze(Estimate estimate);
/**
* Query analyze by id estimate.
*
* @param earthquakeId the earthquake id
* @return the estimate
*/
Estimate queryAnalyzeById(long earthquakeId); Estimate queryAnalyzeById(long earthquakeId);
/**
* Query analyze int.
*
* @param earthquakeId the earthquake id
* @return the int
*/
int queryAnalyze(@Param("earthquakeId") long earthquakeId); int queryAnalyze(@Param("earthquakeId") long earthquakeId);
} }

@ -4,6 +4,15 @@ import com.peteralbus.entity.Hospital;
import java.util.List; import java.util.List;
/**
* The interface Hospital service.
* @author PeterAlbus
*/
public interface HospitalService { public interface HospitalService {
/**
* Find all hospital list.
*
* @return the list
*/
public List<Hospital> findAllHospital(); public List<Hospital> findAllHospital();
} }

@ -6,10 +6,23 @@ import com.peteralbus.service.EstimateService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/**
* The type Estimate service.
* @author PeterAlbus
*/
@Service @Service
public class EstimateServiceImpl implements EstimateService { public class EstimateServiceImpl implements EstimateService {
/**
* The Estimate mapper.
*/
EstimateMapper estimateMapper; EstimateMapper estimateMapper;
/**
* Set analyze mapper.
*
* @param estimateMapper the estimate mapper
*/
@Autowired @Autowired
public void setAnalyzeMapper(EstimateMapper estimateMapper){ public void setAnalyzeMapper(EstimateMapper estimateMapper){
this.estimateMapper = estimateMapper; this.estimateMapper = estimateMapper;

@ -8,12 +8,28 @@ import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
/**
* The type Hospital service.
* @author PeterAlbus
*/
@Service @Service
public class HospitalServiceImpl implements HospitalService { public class HospitalServiceImpl implements HospitalService {
@Autowired
private HospitalMapper hospitalMapper; private HospitalMapper hospitalMapper;
/**
* Sets hospital mapper.
*
* @param hospitalMapper the hospital mapper
*/
@Autowired
public void setHospitalMapper(HospitalMapper hospitalMapper)
{
this.hospitalMapper = hospitalMapper;
}
@Override @Override
public List<Hospital> findAllHospital() { public List<Hospital> findAllHospital() {
return hospitalMapper.findAllHospital(); return hospitalMapper.selectList(null);
} }
} }

@ -26,7 +26,7 @@
</select> </select>
<select id="queryAnalyzeById" resultType="Estimate"> <select id="queryAnalyzeById" resultType="Estimate">
select predict_death,predict_economy,population select analyze_id, earthquake_id, predict_death, predict_economy, population, gmt_create
from estimate from estimate
where earthquake_id = #{earthquakeId} where earthquake_id = #{earthquakeId}
</select> </select>

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.peteralbus.mapper.HospitalMapper">
<select id="findAllHospital" resultType="com.peteralbus.entity.Hospital">
select * from hospital_center
</select>
</mapper>
Loading…
Cancel
Save