查询附近的人(可以查看版)
This commit is contained in:
@@ -6,7 +6,6 @@ import com.bao.dating.common.ResultCode;
|
||||
import com.bao.dating.context.UserContext;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
@@ -131,7 +130,7 @@ public class UserController {
|
||||
}
|
||||
|
||||
@GetMapping("/nearby")
|
||||
public Result<List<User>> nearby( @RequestParam(defaultValue = "5") double distance){
|
||||
public Result<List<UserInfoVO>> nearby(@RequestParam(defaultValue = "5") double distance){
|
||||
// 获取当前线程的用户 ID
|
||||
Long userId = UserContext.getUserId();
|
||||
if (userId == null) {
|
||||
@@ -158,10 +157,10 @@ public class UserController {
|
||||
longitude = (longitude != null) ? longitude : 0.0;
|
||||
|
||||
// 查询附近用户
|
||||
List<User> nearbyUsers = userService.findNearbyUsers(latitude, longitude, distance);
|
||||
List<UserInfoVO> nearbyUsers = userService.findNearbyUsers(latitude, longitude, distance);
|
||||
|
||||
// 返回成功结果
|
||||
return Result.success(ResultCode.SUCCESS, "获取附近用户成功", nearbyUsers);
|
||||
return Result.success(ResultCode.SUCCESS, "查询成功", nearbyUsers);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import com.bao.dating.pojo.entity.Comments;
|
||||
import org.apache.ibatis.annotations.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface CommentsMapper {
|
||||
// 添加评论
|
||||
@Insert("INSERT INTO dating.comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.apache.ibatis.annotations.Param;
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface PostFavoriteMapper {
|
||||
public interface PostFavoriteMapper {
|
||||
//查询当前已收藏所有用户
|
||||
List<Long> selectUserIDByPostID(@Param("postId") Long postId);
|
||||
int addPostFavorite(PostFavorite postFavorite);
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.bao.dating.mapper;
|
||||
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@@ -46,5 +47,5 @@ public interface UserMapper {
|
||||
* @param maxLng 最大经度
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<User> findByLatLngRange( @Param("minLat") double minLat, @Param("maxLat") double maxLat, @Param("minLng") double minLng, @Param("maxLng") double maxLng);
|
||||
List<UserInfoVO> findByLatLngRange(@Param("minLat") double minLat, @Param("maxLat") double maxLat, @Param("minLng") double minLng, @Param("maxLng") double maxLng);
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class User implements Serializable {
|
||||
|
||||
private String userPhone;
|
||||
|
||||
private Double latitude; // 纬度
|
||||
private Double latitude;
|
||||
|
||||
private Double longitude; // 经度
|
||||
private Double longitude;
|
||||
}
|
||||
|
||||
@@ -69,5 +69,5 @@ public interface UserService {
|
||||
* @param radiusKm 半径 km
|
||||
* @return 用户列表
|
||||
*/
|
||||
List<User> findNearbyUsers(double lat,double lng,double radiusKm);
|
||||
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
|
||||
}
|
||||
|
||||
@@ -376,7 +376,7 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findNearbyUsers(double lat, double lng, double radiusKm) {
|
||||
public List<UserInfoVO> findNearbyUsers(double lat, double lng, double radiusKm) {
|
||||
//先用经纬度范围筛选(矩形框,提高性能)
|
||||
double delta = radiusKm / 111.0; // 1° ≈ 111km
|
||||
double minLat = lat - delta;// 最小纬度
|
||||
@@ -388,11 +388,11 @@ public class UserServiceImpl implements UserService {
|
||||
System.out.println("Min Latitude: " + minLat + ", Max Latitude: " + maxLat);
|
||||
System.out.println("Min Longitude: " + minLng + ", Max Longitude: " + maxLng);
|
||||
|
||||
List<User> byLatLngRange = userMapper.findByLatLngRange(minLat, maxLat, minLng, maxLng);
|
||||
List<UserInfoVO> byLatLngRange = userMapper.findByLatLngRange(minLat, maxLat, minLng, maxLng);
|
||||
|
||||
//精确计算距离,筛选在半径内的用户
|
||||
List<User> result = new ArrayList<>();
|
||||
for (User u:byLatLngRange){
|
||||
List<UserInfoVO> result = new ArrayList<>();
|
||||
for (UserInfoVO u:byLatLngRange){
|
||||
// 检查用户是否有经纬度信息
|
||||
if (u.getLatitude() != null && u.getLongitude() != null) {
|
||||
double distance = DistanceUtil.calculate(lat, lng, u.getLatitude(), u.getLongitude());
|
||||
|
||||
@@ -5,6 +5,22 @@
|
||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||
|
||||
<!--根据用户名查询用户-->
|
||||
<resultMap id="UserInfoVOResultMap" type="com.bao.dating.pojo.vo.UserInfoVO">
|
||||
<id property="userId" column="user_id"/>
|
||||
<result property="userName" column="user_name"/>
|
||||
<result property="nickname" column="nickname"/>
|
||||
<result property="avatarUrl" column="avatar_url"/>
|
||||
<result property="backgroundUrl" column="background_url"/>
|
||||
<result property="gender" column="gender"/>
|
||||
<result property="birthday" column="birthday"/>
|
||||
<result property="hobbies" column="hobbies" typeHandler="com.bao.dating.handler.ListToJsonTypeHandler"/>
|
||||
<result property="signature" column="signature"/>
|
||||
<result property="createdAt" column="created_at"/>
|
||||
<result property="updatedAt" column="updated_at"/>
|
||||
<result property="latitude" column="user_latitude"/>
|
||||
<result property="longitude" column="user_longitude"/>
|
||||
</resultMap>
|
||||
|
||||
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||
SELECT
|
||||
user_id,
|
||||
@@ -82,7 +98,21 @@
|
||||
<select id="selectByPhone" resultType="com.bao.dating.pojo.entity.User">
|
||||
select * from dating.user where user_phone =#{phone}
|
||||
</select>
|
||||
<select id="findByLatLngRange" resultType="com.bao.dating.pojo.entity.User">
|
||||
SELECT * FROM user WHERE user_latitude BETWEEN #{minLat} AND #{maxLat} AND user_longitude BETWEEN #{minLng} AND #{maxLng}
|
||||
<select id="findByLatLngRange" resultMap="UserInfoVOResultMap">
|
||||
SELECT
|
||||
user_id,
|
||||
user_name,
|
||||
nickname,
|
||||
avatar_url,
|
||||
background_url,
|
||||
gender,
|
||||
birthday,
|
||||
hobbies,
|
||||
signature,
|
||||
created_at,
|
||||
updated_at,
|
||||
user_latitude,
|
||||
user_longitude
|
||||
FROM user WHERE user_latitude BETWEEN #{minLat} AND #{maxLat} AND user_longitude BETWEEN #{minLng} AND #{maxLng}
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user