记得上下班打卡 | git大法好,push需谨慎

Commit 9607bee7 authored by wangyifan's avatar wangyifan

草莓护照v1.1- 优化演出名称查询效率

parent 2ee8d88c
package com.liquidnet.service.adam.dto;
import lombok.Data;
@Data
public class AdamCaomeiPerformanceIdTitleDto {
private String performanceId;
private String title;
}
......@@ -3,6 +3,7 @@ package com.liquidnet.service.adam.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeClaimCountDto;
import com.liquidnet.service.adam.dto.AdamCaomeiBadgeClaimUserDto;
import com.liquidnet.service.adam.dto.AdamCaomeiPerformanceIdTitleDto;
import com.liquidnet.service.adam.entity.AdamCaomeiBadge;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
......@@ -107,4 +108,16 @@ public interface AdamCaomeiBadgeMapper extends BaseMapper<AdamCaomeiBadge> {
@Select("select title from kylin_performances where performances_id = #{performanceId}")
String selectKylinPerformanceTitleById(@Param("performanceId") String performanceId);
@Select({
"<script>",
"select performances_id as performanceId, title",
"from kylin_performances",
"where performances_id in",
"<foreach collection='performanceIds' item='performanceId' open='(' separator=',' close=')'>",
"#{performanceId}",
"</foreach>",
"</script>"
})
List<AdamCaomeiPerformanceIdTitleDto> selectKylinPerformanceTitlesByIds(@Param("performanceIds") List<String> performanceIds);
}
......@@ -12,6 +12,7 @@ import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.commons.lang.util.SensitizeUtil;
import com.liquidnet.service.adam.constant.AdamRedisConst;
import com.liquidnet.service.adam.dto.AdamCaomeiPassportUserBadgeDto;
import com.liquidnet.service.adam.dto.AdamCaomeiPerformanceIdTitleDto;
import com.liquidnet.service.adam.dto.AdamUserInfoDto;
import com.liquidnet.service.adam.dto.vo.*;
import com.liquidnet.service.adam.entity.AdamCaomeiBadge;
......@@ -807,21 +808,62 @@ public class AdamRdmService {
* 读取不到时返回 null。
*/
public String getPerformanceTitleById(String performanceId) {
try {
KylinPerformanceVo vo = getKylinPerformanceVoById(performanceId);
if (vo != null) {
return vo.getTitle();
if (StringUtils.isEmpty(performanceId)) {
return null;
}
String id = performanceId.trim();
Map<String, String> map = getPerformanceTitleMapByIds(Collections.singletonList(id));
return map.get(id);
}
/**
* 批量解析演出 ID → 标题:先走 Kylin Redis,缓存未命中的 ID 合并为一次 DB IN 查询。
*/
public Map<String, String> getPerformanceTitleMapByIds(Collection<String> performanceIds) {
if (CollectionUtils.isEmpty(performanceIds)) {
return Collections.emptyMap();
}
List<String> ids = performanceIds.stream()
.filter(id -> !StringUtils.isEmpty(id))
.map(String::trim)
.distinct()
.collect(Collectors.toList());
if (ids.isEmpty()) {
return Collections.emptyMap();
}
Map<String, String> result = new HashMap<>(ids.size() * 2);
List<String> missingFromCache = new ArrayList<>();
for (String id : ids) {
try {
KylinPerformanceVo vo = getKylinPerformanceVoById(id);
if (vo != null) {
String title = vo.getTitle();
if (!StringUtils.isEmpty(title)) {
result.put(id, title);
}
} else {
missingFromCache.add(id);
}
} catch (Exception e) {
log.warn("[getPerformanceTitleMapByIds] 读取演出缓存失败, performanceId: {}", id, e);
missingFromCache.add(id);
}
// 从数据库查询
String title = adamCaomeiBadgeMapper.selectKylinPerformanceTitleById(performanceId);
if (!StringUtils.isEmpty(title)) {
return title;
}
if (!missingFromCache.isEmpty()) {
List<AdamCaomeiPerformanceIdTitleDto> rows =
adamCaomeiBadgeMapper.selectKylinPerformanceTitlesByIds(missingFromCache);
if (!CollectionUtils.isEmpty(rows)) {
for (AdamCaomeiPerformanceIdTitleDto row : rows) {
if (row == null || StringUtils.isEmpty(row.getPerformanceId()) || StringUtils.isEmpty(row.getTitle())) {
continue;
}
result.put(row.getPerformanceId().trim(), row.getTitle());
}
}
return null;
} catch (Exception e) {
log.warn("[getPerformanceTitleById] 读取演出缓存失败, performanceId: {}", performanceId, e);
return null;
}
return result;
}
/**
......
......@@ -225,9 +225,14 @@ public class AdamCaomeiPassportUserServiceImpl implements IAdamCaomeiPassportUse
log.info("[getPassportHome] 本次静默发放签证页后刷新用户徽章缓存, uid: {}", uid);
}
Map<String, String> claimedPerformanceTitleById = buildClaimedPerformanceTitleMap(rows);
// 首页货架:排除 type=4
List<AdamCaomeiBadge> shelfPublished = filterShelfPublished(published);
// 已领徽章 + 货架 type=2 的演出 ID 合并后批量解析名称,避免重复循环查库
Map<String, String> performanceTitleById = adamRdmService.getPerformanceTitleMapByIds(
collectPerformanceIdsForTitleLookup(rows, shelfPublished));
// 首页徽章墙:排除 type=4
List<AdamCaomeiPassportUserClaimedBadgeVo> claimed = toClaimedBadgeVos(rows, claimedPerformanceTitleById).stream()
List<AdamCaomeiPassportUserClaimedBadgeVo> claimed = toClaimedBadgeVos(rows, performanceTitleById).stream()
.filter(v -> !isVisaBadgeType(v.getType()))
.collect(Collectors.toList());
home.setClaimedBadges(claimed);
......@@ -237,10 +242,6 @@ public class AdamCaomeiPassportUserServiceImpl implements IAdamCaomeiPassportUse
log.info("[getPassportHome] 系统已上架的徽章数量, uid: {}, 数量: {}", uid, published.size());
// 首页货架:排除 type=4,仅 type=2 需要演出名称
List<AdamCaomeiBadge> shelfPublished = filterShelfPublished(published);
Map<String, String> performanceTitleById = buildPerformanceTitleMap(shelfPublished);
List<AdamCaomeiPassportBadgeShelfItemVo> allBadges = toShelfItems(
shelfPublished,
claimedMap,
......@@ -275,11 +276,10 @@ public class AdamCaomeiPassportUserServiceImpl implements IAdamCaomeiPassportUse
return ResponseDto.success(Collections.emptyList());
}
Map<String, String> performanceTitleById = buildPerformanceTitleMapByPerfIds(
Map<String, String> performanceTitleById = adamRdmService.getPerformanceTitleMapByIds(
visaRows.stream()
.map(AdamCaomeiPassportUserBadgeDto::getPerformanceId)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList())
);
......@@ -381,71 +381,31 @@ public class AdamCaomeiPassportUserServiceImpl implements IAdamCaomeiPassportUse
}).collect(Collectors.toList());
}
private Map<String, String> buildClaimedPerformanceTitleMap(List<AdamCaomeiPassportUserBadgeDto> claimedRows) {
List<String> perfIds = claimedRows.stream()
.filter(r -> r != null && r.getType() != null && r.getType() == 2)
.map(AdamCaomeiPassportUserBadgeDto::getPerformanceId)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList());
if (perfIds.isEmpty()) {
return Collections.emptyMap();
}
Map<String, String> map = new HashMap<>(perfIds.size() * 2);
for (String perfId : perfIds) {
String title = adamRdmService.getPerformanceTitleById(perfId);
if (StringUtils.isNotBlank(title)) {
map.put(perfId, title);
}
}
return map;
}
private static Map<String, AdamCaomeiPassportUserBadgeDto> toClaimedBadgeMap(List<AdamCaomeiPassportUserBadgeDto> rows) {
return rows.stream()
.filter(r -> StringUtils.isNotBlank(r.getBadgeId()))
.collect(Collectors.toMap(AdamCaomeiPassportUserBadgeDto::getBadgeId, Function.identity(), (a, b) -> a));
}
private Map<String, String> buildPerformanceTitleMap(List<AdamCaomeiBadge> published) {
List<String> perfIds = published.stream()
.filter(b -> b != null && b.getType() != null && b.getType() == 2)
.map(AdamCaomeiBadge::getPerformanceId)
.filter(StringUtils::isNotBlank)
.distinct()
.collect(Collectors.toList());
if (perfIds.isEmpty()) {
return Collections.emptyMap();
}
Map<String, String> map = new HashMap<>(perfIds.size() * 2);
for (String perfId : perfIds) {
String title = adamRdmService.getPerformanceTitleById(perfId);
if (StringUtils.isNotBlank(title)) {
map.put(perfId, title);
/** 收集首页需展示演出名称的场次 ID(已领 type=2/4 + 货架 type=2) */
private static List<String> collectPerformanceIdsForTitleLookup(List<AdamCaomeiPassportUserBadgeDto> userRows,
List<AdamCaomeiBadge> shelfPublished) {
Set<String> ids = new LinkedHashSet<>();
if (userRows != null) {
for (AdamCaomeiPassportUserBadgeDto r : userRows) {
if (r != null && needsPerformanceFields(r.getType()) && StringUtils.isNotBlank(r.getPerformanceId())) {
ids.add(r.getPerformanceId().trim());
}
}
}
return map;
}
/**
* 按演出 ID 批量解析名称(Kylin 演出缓存),供签证卡片等展示。
*/
private Map<String, String> buildPerformanceTitleMapByPerfIds(List<String> perfIds) {
if (perfIds == null || perfIds.isEmpty()) {
return Collections.emptyMap();
}
Map<String, String> map = new HashMap<>(perfIds.size() * 2);
for (String perfId : perfIds) {
if (StringUtils.isBlank(perfId)) {
continue;
}
String title = adamRdmService.getPerformanceTitleById(perfId);
if (StringUtils.isNotBlank(title)) {
map.put(perfId, title);
if (shelfPublished != null) {
for (AdamCaomeiBadge b : shelfPublished) {
if (b != null && b.getType() != null && b.getType() == 2 && StringUtils.isNotBlank(b.getPerformanceId())) {
ids.add(b.getPerformanceId().trim());
}
}
}
return map;
return new ArrayList<>(ids);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment