style(service): 添加详细注释并优化人设列表查询逻辑
This commit is contained in:
@@ -65,61 +65,95 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
return character;
|
return character;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 查询所有人设列表并标记用户是否已添加
|
||||||
|
*
|
||||||
|
* @return 人设响应列表,包含是否已添加的标记
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
public List<KeyboardCharacterRespVO> selectListWithRank() {
|
||||||
|
// 获取当前登录用户ID
|
||||||
long userId = StpUtil.getLoginIdAsLong();
|
long userId = StpUtil.getLoginIdAsLong();
|
||||||
|
|
||||||
// 先从缓存获取所有人设列表
|
// 定义缓存key,用于存储所有人设列表
|
||||||
String cacheKey = "character:list:all";
|
String cacheKey = "character:list:all";
|
||||||
|
// 尝试从Redis缓存中获取人设列表
|
||||||
List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
|
List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
|
||||||
|
|
||||||
|
// 如果缓存中没有数据
|
||||||
if (keyboardCharacters == null) {
|
if (keyboardCharacters == null) {
|
||||||
// 缓存未命中,从数据库查询
|
// 从数据库查询所有未删除的人设,按rank升序排列
|
||||||
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
|
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
|
||||||
.eq(KeyboardCharacter::getDeleted, false)
|
.eq(KeyboardCharacter::getDeleted, false)
|
||||||
.orderByAsc(KeyboardCharacter::getRank));
|
.orderByAsc(KeyboardCharacter::getRank));
|
||||||
// 缓存到Redis,7天过期
|
|
||||||
|
// 将查询结果缓存到Redis,设置7天过期时间
|
||||||
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
|
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
|
||||||
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
|
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 查询当前用户已添加的人设列表
|
||||||
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
|
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
|
||||||
|
|
||||||
|
// 将KeyboardCharacter实体列表转换为KeyboardCharacterRespVO响应对象列表
|
||||||
List<KeyboardCharacterRespVO> keyboardCharacterRespVOS = BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
|
List<KeyboardCharacterRespVO> keyboardCharacterRespVOS = BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
|
||||||
|
|
||||||
|
// 遍历人设列表,标记每个人设是否已被当前用户添加
|
||||||
keyboardCharacterRespVOS.forEach(character -> {
|
keyboardCharacterRespVOS.forEach(character -> {
|
||||||
|
// 检查用户已添加列表中是否存在该人设ID
|
||||||
character.setAdded(userCharacterVOList.stream().anyMatch(userCharacter ->
|
character.setAdded(userCharacterVOList.stream().anyMatch(userCharacter ->
|
||||||
userCharacter.getCharacterId().equals(character.getId())));
|
userCharacter.getCharacterId().equals(character.getId())));
|
||||||
});
|
});
|
||||||
|
|
||||||
return keyboardCharacterRespVOS;
|
return keyboardCharacterRespVOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据标签ID查询人设列表并标记用户是否已添加
|
||||||
|
*
|
||||||
|
* @param tagId 标签ID
|
||||||
|
* @return 人设响应列表,包含是否已添加的标记
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<KeyboardCharacterRespVO> selectListByTag(Long tagId) {
|
public List<KeyboardCharacterRespVO> selectListByTag(Long tagId) {
|
||||||
// 先从缓存获取指定标签的人设列表
|
// 构建缓存key,用于存储指定标签的人设列表
|
||||||
String cacheKey = "character:list:tag:" + tagId;
|
String cacheKey = "character:list:tag:" + tagId;
|
||||||
|
// 尝试从Redis缓存中获取指定标签的人设列表
|
||||||
List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
|
List<KeyboardCharacter> keyboardCharacters = (List<KeyboardCharacter>) redisTemplate.opsForValue().get(cacheKey);
|
||||||
|
|
||||||
|
// 如果缓存中没有数据
|
||||||
if (keyboardCharacters == null) {
|
if (keyboardCharacters == null) {
|
||||||
// 缓存未命中,从数据库查询
|
// 从数据库查询指定标签的未删除人设,按rank降序排列
|
||||||
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
|
keyboardCharacters = keyboardCharacterMapper.selectList(new LambdaQueryWrapper<KeyboardCharacter>()
|
||||||
.eq(KeyboardCharacter::getDeleted, false)
|
.eq(KeyboardCharacter::getDeleted, false)
|
||||||
.eq(KeyboardCharacter::getTag, tagId)
|
.eq(KeyboardCharacter::getTag, tagId)
|
||||||
.orderByDesc(KeyboardCharacter::getRank));
|
.orderByDesc(KeyboardCharacter::getRank));
|
||||||
// 缓存到Redis,7天过期
|
|
||||||
|
// 将查询结果缓存到Redis,设置7天过期时间
|
||||||
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
|
if (keyboardCharacters != null && !keyboardCharacters.isEmpty()) {
|
||||||
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
|
redisTemplate.opsForValue().set(cacheKey, keyboardCharacters, 7, TimeUnit.DAYS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取当前登录用户ID
|
||||||
long userId = StpUtil.getLoginIdAsLong();
|
long userId = StpUtil.getLoginIdAsLong();
|
||||||
|
|
||||||
|
// 查询当前用户已添加的人设列表
|
||||||
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
|
List<KeyboardUserCharacterVO> userCharacterVOList = keyboardUserCharacterMapper.selectByUserId(userId);
|
||||||
|
|
||||||
|
// 将KeyboardCharacter实体列表转换为KeyboardCharacterRespVO响应对象列表
|
||||||
List<KeyboardCharacterRespVO> keyboardCharacterRespVOS = BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
|
List<KeyboardCharacterRespVO> keyboardCharacterRespVOS = BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
|
||||||
|
|
||||||
|
// 遍历人设列表,标记每个人设是否已被当前用户添加
|
||||||
keyboardCharacterRespVOS.forEach(character -> {
|
keyboardCharacterRespVOS.forEach(character -> {
|
||||||
|
// 检查用户已添加列表中是否存在该人设ID
|
||||||
character.setAdded(userCharacterVOList.stream().anyMatch(userCharacter ->
|
character.setAdded(userCharacterVOList.stream().anyMatch(userCharacter ->
|
||||||
userCharacter.getCharacterId().equals(character.getId())));
|
userCharacter.getCharacterId().equals(character.getId())));
|
||||||
});
|
});
|
||||||
|
|
||||||
return keyboardCharacterRespVOS;
|
return keyboardCharacterRespVOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,14 +263,29 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
|||||||
return BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
|
return BeanUtil.copyToList(keyboardCharacters, KeyboardCharacterRespVO.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 为新用户添加默认人设
|
||||||
|
* 从所有人设列表中选取前5个,自动添加到用户的人设列表中
|
||||||
|
*
|
||||||
|
* @param userId 用户ID
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void addDefaultUserCharacter(Long userId) {
|
public void addDefaultUserCharacter(Long userId) {
|
||||||
|
// 创建用户人设添加DTO对象,用于复用添加逻辑
|
||||||
KeyboardUserCharacterAddDTO keyboardUserCharacterAddDTO = new KeyboardUserCharacterAddDTO();
|
KeyboardUserCharacterAddDTO keyboardUserCharacterAddDTO = new KeyboardUserCharacterAddDTO();
|
||||||
|
|
||||||
|
// 获取所有人设列表(未登录状态),并限制取前5个
|
||||||
Stream<KeyboardCharacterRespVO> limit = selectListWithNotLoginRank().stream().limit(5);
|
Stream<KeyboardCharacterRespVO> limit = selectListWithNotLoginRank().stream().limit(5);
|
||||||
|
|
||||||
|
// 遍历前5个人设,为用户添加默认人设
|
||||||
limit.forEach(character -> {
|
limit.forEach(character -> {
|
||||||
|
// 设置人设ID
|
||||||
keyboardUserCharacterAddDTO.setCharacterId(character.getId());
|
keyboardUserCharacterAddDTO.setCharacterId(character.getId());
|
||||||
|
// 设置人设表情符号
|
||||||
keyboardUserCharacterAddDTO.setEmoji(character.getEmoji());
|
keyboardUserCharacterAddDTO.setEmoji(character.getEmoji());
|
||||||
this.addUserCharacter(keyboardUserCharacterAddDTO,userId);
|
// 调用添加用户人设的方法
|
||||||
|
this.addUserCharacter(keyboardUserCharacterAddDTO, userId);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user