|
@@ -1,5 +1,6 @@
|
|
|
package com.zhentao.userRelationships.service.impl;
|
|
|
|
|
|
+
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zhentao.shouye.domain.UserShouye;
|
|
@@ -8,16 +9,27 @@ import com.zhentao.user.domain.UserLogin;
|
|
|
import com.zhentao.user.mapper.UserLoginMapper;
|
|
|
import com.zhentao.userRelationships.domain.UserRelationships;
|
|
|
import com.zhentao.userRelationships.domain.UserRequest;
|
|
|
+import com.zhentao.userRelationships.domain.UserShouye;
|
|
|
+import com.zhentao.userRelationships.dto.FriendDto;
|
|
|
+
|
|
|
+import com.zhentao.userRelationships.dto.FriendsTDto;
|
|
|
import com.zhentao.userRelationships.mapper.UserRequestMapper;
|
|
|
+import com.zhentao.userRelationships.mapper.UserShouyeMapper;
|
|
|
import com.zhentao.userRelationships.service.UserRelationshipsService;
|
|
|
import com.zhentao.userRelationships.mapper.UserRelationshipsMapper;
|
|
|
+
|
|
|
+import com.zhentao.userRelationships.util.PinyinUtil;
|
|
|
import com.zhentao.userRelationships.util.SnowflakeUtil;
|
|
|
+
|
|
|
import com.zhentao.vo.Result;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
@@ -189,9 +201,78 @@ public class UserRelationshipsServiceImpl extends ServiceImpl<UserRelationshipsM
|
|
|
return Result.OK(null, "好友申请已拒绝");
|
|
|
}
|
|
|
}
|
|
|
+ //根据当前登录的用户获取所有好友并且按照首字母进行排序
|
|
|
+ @Override
|
|
|
+ public List<Map<String, Object>> getFriendsByUserIdAndSort(Long userId) {
|
|
|
+ // 创建一个查询条件对象,用于后续查询用户关系状态为1(例如:好友关系有效)且用户ID为指定值的记录
|
|
|
+ QueryWrapper<UserRelationships> queryWrapper=new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("status",1)
|
|
|
+ .eq("user_id",userId);
|
|
|
+ // 执行查询,获取用户的关系记录列表
|
|
|
+ List<UserRelationships> relationships=this.list(queryWrapper);
|
|
|
+ // 如果没有找到任何关系记录,则返回一个空列表
|
|
|
+ if(relationships.isEmpty()){
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ // 从关系记录中提取所有好友ID
|
|
|
+ List<Long> friendIds = relationships.stream()
|
|
|
+ .map(UserRelationships::getFriendId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ // 根据提取的好友ID列表,查询所有对应的好友登录信息
|
|
|
+ List<UserLogin> friends = userLoginMapper.selectBatchIds(friendIds);
|
|
|
+ // 如果没有找到任何好友信息,则返回一个空列表
|
|
|
+ if (friends.isEmpty()) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ // 3. 转换为FriendDto并处理首字母
|
|
|
+ // 使用Java Stream API对朋友列表进行处理,转换为FriendsTDto列表
|
|
|
+ List<FriendsTDto> FriendsTDtos = friends.stream().map(friend -> {
|
|
|
+ // 创建一个新的FriendsTDto对象
|
|
|
+ FriendsTDto friendsTDto=new FriendsTDto();
|
|
|
+ // 设置朋友的ID
|
|
|
+ friendsTDto.setFriendId(friend.getId());
|
|
|
+ // 设置朋友的昵称
|
|
|
+ friendsTDto.setNickName(friend.getNickName());
|
|
|
+ // 设置朋友的头像
|
|
|
+ friendsTDto.setAvatar(friend.getAvatar());
|
|
|
+ // 获取昵称首字母(拼音缩写)
|
|
|
+ String firstLetter = PinyinUtil.getFirstLetter(friend.getNickName());
|
|
|
+ // 设置昵称首字母
|
|
|
+ friendsTDto.setFirstLetter(firstLetter);
|
|
|
+ // 返回处理后的FriendsTDto对象
|
|
|
+ return friendsTDto;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ // 4. 按首字母分组(使用TreeMap保证字母顺序)
|
|
|
+ Map<String, List<FriendsTDto>> groupByFirstLetter = FriendsTDtos.stream()
|
|
|
+ .collect(Collectors.groupingBy(
|
|
|
+ FriendsTDto::getFirstLetter,
|
|
|
+ TreeMap::new,
|
|
|
+ Collectors.toList()
|
|
|
+ ));
|
|
|
+
|
|
|
+ // 5. 整理结果(先A-Z,再#)
|
|
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
+ // 处理A-Z字母分组
|
|
|
+ for (char c = 'A'; c <= 'Z'; c++) {
|
|
|
+ String letter = String.valueOf(c);
|
|
|
+ if (groupByFirstLetter.containsKey(letter)) {
|
|
|
+ // 对每组好友按昵称拼音排序
|
|
|
+ groupByFirstLetter.get(letter).sort(
|
|
|
+ Comparator.comparing(f -> PinyinUtil.getFullPinyin(f.getNickName()))
|
|
|
+ );
|
|
|
+ result.add(Collections.singletonMap(letter, groupByFirstLetter.get(letter)));
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
-
|
|
|
+ // 处理特殊字符分组(#)
|
|
|
+ if (groupByFirstLetter.containsKey("#")) {
|
|
|
+ groupByFirstLetter.get("#").sort(
|
|
|
+ Comparator.comparing(f -> PinyinUtil.getFullPinyin(f.getNickName()))
|
|
|
+ );
|
|
|
+ result.add(Collections.singletonMap("#", groupByFirstLetter.get("#")));
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|