feat(service): 新增根据ID获取AI角色详情接口
This commit is contained in:
@@ -70,4 +70,15 @@ public class AiCompanionController {
|
||||
List<AiCompanionVO> result = aiCompanionService.getChattedCompanions(userId);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
|
||||
@GetMapping("/{companionId}")
|
||||
@Operation(summary = "根据ID获取AI角色详情", description = "根据AI角色ID查询角色详细信息,包含点赞数、评论数和当前用户点赞状态")
|
||||
public BaseResponse<AiCompanionVO> getCompanionById(@PathVariable Long companionId) {
|
||||
if (companionId == null) {
|
||||
throw new BusinessException(ErrorCode.COMPANION_ID_EMPTY);
|
||||
}
|
||||
Long userId = StpUtil.getLoginIdAsLong();
|
||||
AiCompanionVO result = aiCompanionService.getCompanionById(userId, companionId);
|
||||
return ResultUtils.success(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,4 +55,13 @@ public interface KeyboardAiCompanionService extends IService<KeyboardAiCompanion
|
||||
* @return 聊过天的AI角色列表
|
||||
*/
|
||||
List<AiCompanionVO> getChattedCompanions(Long userId);
|
||||
|
||||
/**
|
||||
* 根据ID获取AI角色详情(带点赞数、评论数和当前用户点赞状态)
|
||||
*
|
||||
* @param userId 当前用户ID
|
||||
* @param companionId AI角色ID
|
||||
* @return AI角色详情
|
||||
*/
|
||||
AiCompanionVO getCompanionById(Long userId, Long companionId);
|
||||
}
|
||||
|
||||
@@ -262,4 +262,39 @@ public class KeyboardAiCompanionServiceImpl extends ServiceImpl<KeyboardAiCompan
|
||||
return vo;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public AiCompanionVO getCompanionById(Long userId, Long companionId) {
|
||||
// 查询AI角色
|
||||
KeyboardAiCompanion companion = this.getById(companionId);
|
||||
if (companion == null) {
|
||||
throw new BusinessException(ErrorCode.COMPANION_NOT_FOUND);
|
||||
}
|
||||
if (companion.getStatus() != 1 || companion.getVisibility() != 1) {
|
||||
throw new BusinessException(ErrorCode.COMPANION_NOT_FOUND);
|
||||
}
|
||||
|
||||
// 统计点赞数
|
||||
LambdaQueryWrapper<KeyboardAiCompanionLike> likeWrapper = new LambdaQueryWrapper<>();
|
||||
likeWrapper.eq(KeyboardAiCompanionLike::getCompanionId, companionId)
|
||||
.eq(KeyboardAiCompanionLike::getStatus, (short) 1);
|
||||
long likeCount = companionLikeService.count(likeWrapper);
|
||||
|
||||
// 统计评论数
|
||||
LambdaQueryWrapper<KeyboardAiCompanionComment> commentWrapper = new LambdaQueryWrapper<>();
|
||||
commentWrapper.eq(KeyboardAiCompanionComment::getCompanionId, companionId)
|
||||
.eq(KeyboardAiCompanionComment::getStatus, (short) 1);
|
||||
long commentCount = companionCommentService.count(commentWrapper);
|
||||
|
||||
// 获取当前用户点赞状态
|
||||
boolean liked = companionLikeService.hasLiked(userId, companionId);
|
||||
|
||||
// 转换为VO
|
||||
AiCompanionVO vo = BeanUtil.copyProperties(companion, AiCompanionVO.class);
|
||||
vo.setLikeCount((int) likeCount);
|
||||
vo.setCommentCount((int) commentCount);
|
||||
vo.setLiked(liked);
|
||||
|
||||
return vo;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user