|
| 1 | +package com.ligg.flowclient.service; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.ligg.api.bangumiv0api.BangumiV0Client; |
| 6 | +import com.ligg.common.entity.BangumiSubjectEntity; |
| 7 | +import com.ligg.common.model.CoverImages; |
| 8 | +import com.ligg.common.thirdparty.bangumi.enums.SubjectImageType; |
| 9 | +import com.ligg.flowclient.mapper.BangumiSubjectMapper; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | +import org.springframework.util.StringUtils; |
| 14 | + |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | + |
| 17 | +/** |
| 18 | + * 条目图片处理服务 |
| 19 | + */ |
| 20 | +@Slf4j |
| 21 | +@Component |
| 22 | +@RequiredArgsConstructor |
| 23 | +public class ImageBackfillService { |
| 24 | + |
| 25 | + private final BangumiSubjectMapper bangumiSubjectMapper; |
| 26 | + private final BangumiV0Client bangumiV0Client; |
| 27 | + private final ObjectMapper objectMapper; |
| 28 | + |
| 29 | + private final ConcurrentHashMap<Integer, Object> locks = new ConcurrentHashMap<>(); |
| 30 | + |
| 31 | + /** |
| 32 | + * 获取条目封面图。DB 有数据则直接返回,否则调 API 获取并回写。 |
| 33 | + * |
| 34 | + * @param subjectImagesJson bangumi_subject.images 的 JSON 值,可为空 |
| 35 | + * @param subjectId 条目 ID |
| 36 | + * @return 封面图,获取失败返回空 CoverImages |
| 37 | + */ |
| 38 | + public CoverImages resolve(String subjectImagesJson, int subjectId) { |
| 39 | + CoverImages images; |
| 40 | + if (StringUtils.hasText(subjectImagesJson) && !"null".equals(subjectImagesJson)) { |
| 41 | + try { |
| 42 | + images = objectMapper.readValue(subjectImagesJson, CoverImages.class); |
| 43 | + } catch (JsonProcessingException e) { |
| 44 | + log.warn("解析条目图片 JSON 失败, subjectId={}", subjectId, e); |
| 45 | + images = new CoverImages(); |
| 46 | + } |
| 47 | + } else { |
| 48 | + images = fetchAndSave(subjectId); |
| 49 | + } |
| 50 | + if (images.getLarge() == null) images.setLarge(""); |
| 51 | + if (images.getCommon() == null) images.setCommon(""); |
| 52 | + if (images.getMedium() == null) images.setMedium(""); |
| 53 | + if (images.getSmall() == null) images.setSmall(""); |
| 54 | + if (images.getGrid() == null) images.setGrid(""); |
| 55 | + return images; |
| 56 | + } |
| 57 | + |
| 58 | + private CoverImages fetchAndSave(int subjectId) { |
| 59 | + // 锁对象不删除:避免 remove 后其他线程创建新锁绕过互斥,造成重复请求。 |
| 60 | + // subjectId 数量有限(≈3 万),全量常驻仅约 2 MB,可接受。 |
| 61 | + Object lock = locks.computeIfAbsent(subjectId, k -> new Object()); |
| 62 | + synchronized (lock) { |
| 63 | + // 双重检查 |
| 64 | + BangumiSubjectEntity entity = bangumiSubjectMapper.selectById(subjectId); |
| 65 | + if (entity != null && StringUtils.hasText(entity.getImages()) |
| 66 | + && !"null".equals(entity.getImages())) { |
| 67 | + try { |
| 68 | + return objectMapper.readValue(entity.getImages(), CoverImages.class); |
| 69 | + } catch (JsonProcessingException e) { |
| 70 | + log.warn("双重检查时解析条目图片 JSON 失败, subjectId={}", subjectId, e); |
| 71 | + // JSON 损坏,fall-through 重新从 API 获取 |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + CoverImages images = new CoverImages(); |
| 76 | + boolean anySucceeded = false; |
| 77 | + |
| 78 | + for (SubjectImageType type : SubjectImageType.values()) { |
| 79 | + try { |
| 80 | + String url = bangumiV0Client.getSubjectImageUrl(subjectId, type); |
| 81 | + if (StringUtils.hasText(url)) { |
| 82 | + setField(images, type, url); |
| 83 | + anySucceeded = true; |
| 84 | + } |
| 85 | + } catch (Exception e) { |
| 86 | + log.warn("获取条目图片失败, subjectId={}, type={}", subjectId, type.getValue(), e); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (anySucceeded) { |
| 91 | + try { |
| 92 | + String json = objectMapper.writeValueAsString(images); |
| 93 | + BangumiSubjectEntity update = new BangumiSubjectEntity(); |
| 94 | + update.setId(subjectId); |
| 95 | + update.setImages(json); |
| 96 | + bangumiSubjectMapper.updateById(update); |
| 97 | + log.info("条目图片已回写 DB, subjectId={}, images={}", subjectId, json); |
| 98 | + } catch (JsonProcessingException e) { |
| 99 | + log.error("序列化条目图片 JSON 失败, subjectId={}", subjectId, e); |
| 100 | + } |
| 101 | + } else { |
| 102 | + log.warn("条目图片获取全部失败, subjectId={}", subjectId); |
| 103 | + } |
| 104 | + return images; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static void setField(CoverImages images, SubjectImageType type, String url) { |
| 109 | + switch (type) { |
| 110 | + case LARGE -> images.setLarge(url); |
| 111 | + case COMMON -> images.setCommon(url); |
| 112 | + case MEDIUM -> images.setMedium(url); |
| 113 | + case SMALL -> images.setSmall(url); |
| 114 | + case GRID -> images.setGrid(url); |
| 115 | + default -> { /* 枚举新增值时编译器会警告,此处防御性空操作 */ } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments