feat(ai-companion): 新增获取用户聊过天的AI角色列表接口
This commit is contained in:
@@ -62,4 +62,12 @@ public class AiCompanionController {
|
||||
List<AiCompanionVO> result = aiCompanionService.getLikedCompanions(userId);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
@GetMapping("/chatted")
|
||||
@Operation(summary = "获取当前用户聊过天的AI角色列表", description = "查询当前用户聊过天的所有AI角色,返回角色详细信息")
|
||||
public BaseResponse<List<AiCompanionVO>> getChattedCompanions() {
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
List<AiCompanionVO> result = aiCompanionService.getChattedCompanions(userId);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,4 +33,12 @@ public interface KeyboardAiChatMessageService extends IService<KeyboardAiChatMes
|
||||
* @return 聊天记录列表(时间正序)
|
||||
*/
|
||||
List<KeyboardAiChatMessage> getRecentMessages(Long userId, Long companionId, int limit);
|
||||
|
||||
/**
|
||||
* 获取用户聊过天的所有AI角色ID列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 聊过天的AI角色ID列表(按最近聊天时间倒序)
|
||||
*/
|
||||
List<Long> getChattedCompanionIds(Long userId);
|
||||
}
|
||||
|
||||
@@ -47,4 +47,12 @@ public interface KeyboardAiCompanionService extends IService<KeyboardAiCompanion
|
||||
* @return 点赞过的AI角色列表
|
||||
*/
|
||||
List<AiCompanionVO> getLikedCompanions(Long userId);
|
||||
|
||||
/**
|
||||
* 获取用户聊过天的AI角色列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 聊过天的AI角色列表
|
||||
*/
|
||||
List<AiCompanionVO> getChattedCompanions(Long userId);
|
||||
}
|
||||
|
||||
@@ -44,4 +44,20 @@ public class KeyboardAiChatMessageServiceImpl extends ServiceImpl<KeyboardAiChat
|
||||
Collections.reverse(messages);
|
||||
return messages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> getChattedCompanionIds(Long userId) {
|
||||
// 使用原生SQL查询用户聊过天的所有角色ID,按最近聊天时间倒序
|
||||
LambdaQueryWrapper<KeyboardAiChatMessage> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(KeyboardAiChatMessage::getUserId, userId)
|
||||
.select(KeyboardAiChatMessage::getCompanionId)
|
||||
.groupBy(KeyboardAiChatMessage::getCompanionId)
|
||||
.orderByDesc(KeyboardAiChatMessage::getCompanionId);
|
||||
|
||||
List<KeyboardAiChatMessage> messages = this.list(queryWrapper);
|
||||
return messages.stream()
|
||||
.map(KeyboardAiChatMessage::getCompanionId)
|
||||
.distinct()
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import com.yolo.keyborad.model.entity.KeyboardAiCompanionLike;
|
||||
import com.yolo.keyborad.model.vo.AiCompanionVO;
|
||||
import com.yolo.keyborad.service.KeyboardAiCompanionCommentService;
|
||||
import com.yolo.keyborad.service.KeyboardAiCompanionLikeService;
|
||||
import com.yolo.keyborad.service.KeyboardAiChatMessageService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -36,6 +37,9 @@ public class KeyboardAiCompanionServiceImpl extends ServiceImpl<KeyboardAiCompan
|
||||
@Resource
|
||||
private KeyboardAiCompanionCommentService companionCommentService;
|
||||
|
||||
@Resource
|
||||
private KeyboardAiChatMessageService chatMessageService;
|
||||
|
||||
@Override
|
||||
public IPage<AiCompanionVO> pageList(Integer pageNum, Integer pageSize) {
|
||||
Page<KeyboardAiCompanion> page = new Page<>(pageNum, pageSize);
|
||||
@@ -199,4 +203,63 @@ public class KeyboardAiCompanionServiceImpl extends ServiceImpl<KeyboardAiCompan
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AiCompanionVO> getChattedCompanions(Long userId) {
|
||||
// 获取用户聊过天的所有AI角色ID
|
||||
List<Long> chattedCompanionIds = chatMessageService.getChattedCompanionIds(userId);
|
||||
if (chattedCompanionIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
// 查询这些AI角色的详细信息(只返回已上线且可见的)
|
||||
LambdaQueryWrapper<KeyboardAiCompanion> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.in(KeyboardAiCompanion::getId, chattedCompanionIds)
|
||||
.eq(KeyboardAiCompanion::getStatus, 1)
|
||||
.eq(KeyboardAiCompanion::getVisibility, 1);
|
||||
List<KeyboardAiCompanion> companions = this.list(queryWrapper);
|
||||
|
||||
if (companions.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
// 获取实际查询到的角色ID
|
||||
List<Long> companionIds = companions.stream()
|
||||
.map(KeyboardAiCompanion::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 批量统计点赞数
|
||||
LambdaQueryWrapper<KeyboardAiCompanionLike> likeWrapper = new LambdaQueryWrapper<>();
|
||||
likeWrapper.in(KeyboardAiCompanionLike::getCompanionId, companionIds)
|
||||
.eq(KeyboardAiCompanionLike::getStatus, (short) 1);
|
||||
List<KeyboardAiCompanionLike> likes = companionLikeService.list(likeWrapper);
|
||||
Map<Long, Long> likeCountMap = likes.stream()
|
||||
.collect(Collectors.groupingBy(KeyboardAiCompanionLike::getCompanionId, Collectors.counting()));
|
||||
|
||||
// 批量统计评论数
|
||||
LambdaQueryWrapper<KeyboardAiCompanionComment> commentWrapper = new LambdaQueryWrapper<>();
|
||||
commentWrapper.in(KeyboardAiCompanionComment::getCompanionId, companionIds)
|
||||
.eq(KeyboardAiCompanionComment::getStatus, (short) 1);
|
||||
List<KeyboardAiCompanionComment> comments = companionCommentService.list(commentWrapper);
|
||||
Map<Long, Long> commentCountMap = comments.stream()
|
||||
.collect(Collectors.groupingBy(KeyboardAiCompanionComment::getCompanionId, Collectors.counting()));
|
||||
|
||||
// 获取当前用户已点赞的角色ID
|
||||
Set<Long> likedCompanionIds = companionLikeService.getLikedCompanionIds(userId, companionIds);
|
||||
|
||||
// 转换为VO并填充统计数据,保持原有顺序(按最近聊天时间)
|
||||
Map<Long, KeyboardAiCompanion> companionMap = companions.stream()
|
||||
.collect(Collectors.toMap(KeyboardAiCompanion::getId, c -> c));
|
||||
|
||||
return chattedCompanionIds.stream()
|
||||
.filter(companionMap::containsKey)
|
||||
.map(id -> {
|
||||
KeyboardAiCompanion entity = companionMap.get(id);
|
||||
AiCompanionVO vo = BeanUtil.copyProperties(entity, AiCompanionVO.class);
|
||||
vo.setLikeCount(likeCountMap.getOrDefault(entity.getId(), 0L).intValue());
|
||||
vo.setCommentCount(commentCountMap.getOrDefault(entity.getId(), 0L).intValue());
|
||||
vo.setLiked(likedCompanionIds.contains(entity.getId()));
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user