修复国际化查询缺失时回退英文逻辑
This commit is contained in:
@@ -24,6 +24,7 @@ import com.yolo.keyborad.service.KeyboardAiCompanionService;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -36,6 +37,8 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class KeyboardAiCompanionServiceImpl extends ServiceImpl<KeyboardAiCompanionMapper, KeyboardAiCompanion> implements KeyboardAiCompanionService {
|
||||
|
||||
private static final String DEFAULT_FALLBACK_LOCALE = "en";
|
||||
|
||||
@Resource
|
||||
private KeyboardAiCompanionLikeService companionLikeService;
|
||||
|
||||
@@ -326,6 +329,29 @@ public class KeyboardAiCompanionServiceImpl extends ServiceImpl<KeyboardAiCompan
|
||||
|
||||
private Map<Long, KeyboardAiCompanionI18n> getCompanionI18nMap(List<Long> companionIds, String acceptLanguage) {
|
||||
String locale = RequestLocaleUtils.resolveLanguage(acceptLanguage);
|
||||
if (companionIds == null || companionIds.isEmpty() || !StringUtils.hasText(locale)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<Long, KeyboardAiCompanionI18n> requestedLocaleMap = queryI18nMap(companionIds, locale);
|
||||
if (requestedLocaleMap.size() == companionIds.size() || DEFAULT_FALLBACK_LOCALE.equalsIgnoreCase(locale)) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
List<Long> missingCompanionIds = companionIds.stream()
|
||||
.filter(companionId -> !requestedLocaleMap.containsKey(companionId))
|
||||
.toList();
|
||||
if (missingCompanionIds.isEmpty()) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
Map<Long, KeyboardAiCompanionI18n> fallbackMap = queryI18nMap(missingCompanionIds, DEFAULT_FALLBACK_LOCALE);
|
||||
if (fallbackMap.isEmpty()) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
Map<Long, KeyboardAiCompanionI18n> mergedMap = new LinkedHashMap<>(requestedLocaleMap);
|
||||
fallbackMap.forEach(mergedMap::putIfAbsent);
|
||||
return mergedMap;
|
||||
}
|
||||
|
||||
private Map<Long, KeyboardAiCompanionI18n> queryI18nMap(List<Long> companionIds, String locale) {
|
||||
if (companionIds == null || companionIds.isEmpty() || !StringUtils.hasText(locale)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.util.stream.Stream;
|
||||
@Service
|
||||
public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterMapper, KeyboardCharacter> implements KeyboardCharacterService{
|
||||
|
||||
private static final String DEFAULT_FALLBACK_LOCALE = "en";
|
||||
private static final String CHARACTER_CACHE_KEY_PREFIX = "character:";
|
||||
private static final String CHARACTER_LIST_CACHE_KEY = "character:list:all";
|
||||
private static final String CHARACTER_TAG_CACHE_KEY_PREFIX = "character:list:tag:";
|
||||
@@ -353,6 +354,29 @@ public class KeyboardCharacterServiceImpl extends ServiceImpl<KeyboardCharacterM
|
||||
if (characterIds == null || characterIds.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<Long, KeyboardCharacterI18n> requestedLocaleMap = queryI18nMap(characterIds, locale);
|
||||
if (requestedLocaleMap.size() == characterIds.size() || DEFAULT_FALLBACK_LOCALE.equalsIgnoreCase(locale)) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
List<Long> missingCharacterIds = characterIds.stream()
|
||||
.filter(characterId -> !requestedLocaleMap.containsKey(characterId))
|
||||
.toList();
|
||||
if (missingCharacterIds.isEmpty()) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
Map<Long, KeyboardCharacterI18n> fallbackMap = queryI18nMap(missingCharacterIds, DEFAULT_FALLBACK_LOCALE);
|
||||
if (fallbackMap.isEmpty()) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
Map<Long, KeyboardCharacterI18n> mergedMap = new LinkedHashMap<>(requestedLocaleMap);
|
||||
fallbackMap.forEach(mergedMap::putIfAbsent);
|
||||
return mergedMap;
|
||||
}
|
||||
|
||||
private Map<Long, KeyboardCharacterI18n> queryI18nMap(List<Long> characterIds, String locale) {
|
||||
if (characterIds == null || characterIds.isEmpty() || !StringUtils.hasText(locale)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
List<KeyboardCharacterI18n> i18nList = keyboardCharacterI18nMapper.selectList(
|
||||
new LambdaQueryWrapper<KeyboardCharacterI18n>()
|
||||
.eq(KeyboardCharacterI18n::getLocale, locale)
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -27,6 +28,8 @@ import java.util.stream.Collectors;
|
||||
@Service
|
||||
public class TagServiceImpl extends ServiceImpl<KeyboardTagMapper, KeyboardTag> implements TagService {
|
||||
|
||||
private static final String DEFAULT_FALLBACK_LOCALE = "en";
|
||||
|
||||
@Resource
|
||||
private KeyboardTagMapper keyboardTagMapper;
|
||||
|
||||
@@ -63,6 +66,29 @@ public class TagServiceImpl extends ServiceImpl<KeyboardTagMapper, KeyboardTag>
|
||||
if (tagIds == null || tagIds.isEmpty()) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
Map<Integer, KeyboardTagI18n> requestedLocaleMap = queryI18nMap(tagIds, locale);
|
||||
if (requestedLocaleMap.size() == tagIds.size() || DEFAULT_FALLBACK_LOCALE.equalsIgnoreCase(locale)) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
List<Integer> missingTagIds = tagIds.stream()
|
||||
.filter(tagId -> !requestedLocaleMap.containsKey(tagId))
|
||||
.toList();
|
||||
if (missingTagIds.isEmpty()) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
Map<Integer, KeyboardTagI18n> fallbackMap = queryI18nMap(missingTagIds, DEFAULT_FALLBACK_LOCALE);
|
||||
if (fallbackMap.isEmpty()) {
|
||||
return requestedLocaleMap;
|
||||
}
|
||||
Map<Integer, KeyboardTagI18n> mergedMap = new LinkedHashMap<>(requestedLocaleMap);
|
||||
fallbackMap.forEach(mergedMap::putIfAbsent);
|
||||
return mergedMap;
|
||||
}
|
||||
|
||||
private Map<Integer, KeyboardTagI18n> queryI18nMap(List<Integer> tagIds, String locale) {
|
||||
if (tagIds == null || tagIds.isEmpty() || !StringUtils.hasText(locale)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
List<KeyboardTagI18n> i18nList = keyboardTagI18nMapper.selectList(
|
||||
new LambdaQueryWrapper<KeyboardTagI18n>()
|
||||
.in(KeyboardTagI18n::getTagId, tagIds)
|
||||
|
||||
Reference in New Issue
Block a user