1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
@Service public class SocialNearbyService {
@Autowired private RedisTemplate<String, Object> redisTemplate;
@Autowired private AdvancedGeoService advancedGeoService;
@Autowired private UserService userService;
private final String SOCIAL_GEO_PREFIX = "social_geo:"; private final String USER_PREFERENCE_PREFIX = "user_preference:";
public void updateSocialLocation(String userId, double longitude, double latitude, Map<String, Object> socialInfo) { String geoKey = SOCIAL_GEO_PREFIX + "users";
advancedGeoService.updateUserLocation(userId, longitude, latitude, socialInfo);
updateSocialInfo(userId, socialInfo);
updateUserPreferences(userId, socialInfo); }
public List<SocialNearbyUser> findNearbySocialUsers(String userId, double radius, long limit) { Map<String, Object> userPreferences = getUserPreferences(userId);
List<NearbyUser> nearbyUsers = advancedGeoService.getNearbyUsersWithDetails(userId, radius, limit * 2);
List<SocialNearbyUser> socialNearbyUsers = new ArrayList<>();
for (NearbyUser nearbyUser : nearbyUsers) { Map<Object, Object> socialInfo = getUserSocialInfo(nearbyUser.getUserId());
double socialScore = calculateSocialScore(userPreferences, socialInfo);
if (socialScore > 0.3) { SocialNearbyUser socialUser = new SocialNearbyUser(); socialUser.setUserId(nearbyUser.getUserId()); socialUser.setDistance(nearbyUser.getDistance()); socialUser.setLatitude(nearbyUser.getLatitude()); socialUser.setLongitude(nearbyUser.getLongitude()); socialUser.setSocialScore(socialScore); socialUser.setSocialInfo(socialInfo);
socialNearbyUsers.add(socialUser); } }
socialNearbyUsers.sort((u1, u2) -> Double.compare(u2.getSocialScore(), u1.getSocialScore()));
return socialNearbyUsers.stream().limit(limit).collect(Collectors.toList()); }
public List<NearbyActivity> findNearbyActivities(String userId, double radius, long limit) { List<Point> userLocation = advancedGeoService.getLocation("users", userId); if (userLocation.isEmpty()) { return Collections.emptyList(); }
Point userPoint = userLocation.get(0);
List<GeoResult<RedisGeoCommands.GeoLocation<String>>> activityResults = advancedGeoService.getUsersInRange("activities", userPoint.getX(), userPoint.getY(), radius, DistanceUnit.KILOMETERS, limit);
List<NearbyActivity> nearbyActivities = new ArrayList<>();
for (GeoResult<RedisGeoCommands.GeoLocation<String>> result : activityResults) { String activityId = result.getContent().getName();
Map<Object, Object> activityInfo = getActivityInfo(activityId);
NearbyActivity activity = new NearbyActivity(); activity.setActivityId(activityId); activity.setDistance(result.getDistance().getValue()); activity.setLongitude(result.getContent().getPoint().getX()); activity.setLatitude(result.getContent().getPoint().getY()); activity.setActivityInfo(activityInfo);
nearbyActivities.add(activity); }
return nearbyActivities; }
private void updateSocialInfo(String userId, Map<String, Object> socialInfo) { String socialKey = "social_info:" + userId; redisTemplate.opsForHash().putAll(socialKey, socialInfo); redisTemplate.expire(socialKey, Duration.ofDays(30)); }
private Map<Object, Object> getUserSocialInfo(String userId) { String socialKey = "social_info:" + userId; return redisTemplate.opsForHash().entries(socialKey); }
private void updateUserPreferences(String userId, Map<String, Object> socialInfo) { String preferenceKey = USER_PREFERENCE_PREFIX + userId;
Map<String, Object> preferences = new HashMap<>(); preferences.put("age", socialInfo.get("age")); preferences.put("gender", socialInfo.get("gender")); preferences.put("interests", socialInfo.get("interests")); preferences.put("hobbies", socialInfo.get("hobbies"));
redisTemplate.opsForHash().putAll(preferenceKey, preferences); redisTemplate.expire(preferenceKey, Duration.ofDays(30)); }
private Map<String, Object> getUserPreferences(String userId) { String preferenceKey = USER_PREFERENCE_PREFIX + userId; Map<Object, Object> rawPreferences = redisTemplate.opsForHash().entries(preferenceKey);
Map<String, Object> preferences = new HashMap<>(); for (Map.Entry<Object, Object> entry : rawPreferences.entrySet()) { preferences.put(entry.getKey().toString(), entry.getValue()); }
return preferences; }
private double calculateSocialScore(Map<String, Object> userPreferences, Map<Object, Object> socialInfo) { double score = 0.0; int factors = 0;
if (userPreferences.containsKey("age") && socialInfo.containsKey("age")) { int userAge = Integer.parseInt(userPreferences.get("age").toString()); int socialAge = Integer.parseInt(socialInfo.get("age").toString()); int ageDiff = Math.abs(userAge - socialAge);
if (ageDiff <= 5) { score += 0.3; } else if (ageDiff <= 10) { score += 0.2; } else if (ageDiff <= 15) { score += 0.1; } factors++; }
if (userPreferences.containsKey("gender") && socialInfo.containsKey("gender")) { String userGender = userPreferences.get("gender").toString(); String socialGender = socialInfo.get("gender").toString();
if (!userGender.equals(socialGender)) { score += 0.2; } factors++; }
if (userPreferences.containsKey("interests") && socialInfo.containsKey("interests")) { String userInterests = userPreferences.get("interests").toString(); String socialInterests = socialInfo.get("interests").toString();
double interestOverlap = calculateInterestOverlap(userInterests, socialInterests); score += interestOverlap * 0.5; factors++; }
return factors > 0 ? score / factors : 0.0; }
private double calculateInterestOverlap(String userInterests, String socialInterests) { String[] userInterestArray = userInterests.split(","); String[] socialInterestArray = socialInterests.split(",");
Set<String> userInterestSet = Arrays.stream(userInterestArray) .map(String::trim) .collect(Collectors.toSet());
Set<String> socialInterestSet = Arrays.stream(socialInterestArray) .map(String::trim) .collect(Collectors.toSet());
Set<String> intersection = new HashSet<>(userInterestSet); intersection.retainAll(socialInterestSet);
Set<String> union = new HashSet<>(userInterestSet); union.addAll(socialInterestSet);
return union.isEmpty() ? 0.0 : (double) intersection.size() / union.size(); }
private Map<Object, Object> getActivityInfo(String activityId) { String activityKey = "activity_info:" + activityId; return redisTemplate.opsForHash().entries(activityKey); } }
|