liujie
2026-09-14 82a8bc62d5b9d5d607f6952329952b17eda2f0a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.tourguide.modules.review.util;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * 将评价图片字段兼容解析为图片地址列表。
 * 新数据使用 JSON 数组,历史数据可能仍是 JSON 字符串或逗号分隔值。
 */
public final class ReviewImageParser {
    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private static final int MAX_IMAGE_COUNT = 3;
 
    private ReviewImageParser() {
    }
 
    public static List<String> parse(String value) {
        if (value == null || value.trim().isEmpty()) {
            return Collections.emptyList();
        }
 
        try {
            JsonNode node = OBJECT_MAPPER.readTree(value.trim());
            if (node.isArray()) {
                List<String> images = new ArrayList<String>();
                for (JsonNode item : node) {
                    if (item.isTextual()) {
                        addImage(images, item.asText());
                    }
                }
                return images;
            }
            if (node.isTextual()) {
                return parseDelimited(node.asText());
            }
        } catch (Exception ignored) {
            // 兼容早期非 JSON 存储格式,继续按逗号分隔值解析。
        }
        return parseDelimited(value);
    }
 
    private static List<String> parseDelimited(String value) {
        List<String> images = new ArrayList<String>();
        for (String item : value.split(",")) {
            addImage(images, item);
        }
        return images;
    }
 
    private static void addImage(List<String> images, String value) {
        if (images.size() >= MAX_IMAGE_COUNT || value == null) {
            return;
        }
        String image = value.trim();
        if (!image.isEmpty() && !images.contains(image)) {
            images.add(image);
        }
    }
}