|
|
@@ -0,0 +1,480 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.etotem.cfc.entity.DietPreferences;
|
|
|
+import com.etotem.cfc.entity.FamilyMember;
|
|
|
+import com.etotem.cfc.entity.Food;
|
|
|
+import com.etotem.cfc.entity.FoodRecommendIndex;
|
|
|
+import com.etotem.cfc.entity.UserCurrentFoodRecommend;
|
|
|
+import com.etotem.cfc.mapper.FamilyMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.FoodRecommendIndexMapper;
|
|
|
+import com.etotem.cfc.mapper.UserCurrentFoodRecommendMapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户当前食材推荐(迁移504 / 个性化食谱生成)。
|
|
|
+ *
|
|
|
+ * <p>报告基线({@code food_recommend_idx})之上按问卷/偏好重算出「当前可用食材清单」,
|
|
|
+ * 食谱生成只读这份清单;重建只写 {@code user_current_food_recommend},绝不回写报告原始分。</p>
|
|
|
+ *
|
|
|
+ * <p>刻意不注入 {@link FoodRecommendService}(后者将注入本服务,避免循环依赖);
|
|
|
+ * 成员 → users.id 的归一在本服务内直接用 {@link FamilyMemberMapper} 完成。</p>
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class UserCurrentFoodService {
|
|
|
+
|
|
|
+ private static final int SELECTED_FOODS_LIMIT = 30;
|
|
|
+ private static final int ADJUSTMENT_REASON_MAX_LEN = 500;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private UserCurrentFoodRecommendMapper userCurrentFoodRecommendMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FoodRecommendIndexMapper foodRecommendIndexMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private HealthFilterService healthFilterService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private FamilyMemberMapper familyMemberMapper;
|
|
|
+
|
|
|
+ /** 仅用于补齐食材 category / allergens / dietType,使问卷判定口径与食材页一致 */
|
|
|
+ @Resource
|
|
|
+ private FoodService foodService;
|
|
|
+
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重建指定用户的当前食材推荐。
|
|
|
+ *
|
|
|
+ * @param userId 归一后的 users.id(调用方负责归一;与 food_recommend_idx.user_id 同口径)
|
|
|
+ * @param memberId 原始成员卡 id(family_members.id),仅用于加载问卷画像/偏好;可为 null
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public void rebuildForUser(Long userId, Long memberId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<FoodRecommendIndex> indices = foodRecommendIndexMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<FoodRecommendIndex>()
|
|
|
+ .eq(FoodRecommendIndex::getUserId, userId));
|
|
|
+ if (indices == null || indices.isEmpty()) {
|
|
|
+ // 无报告基线 → 清空当前表,生成侧 hasData=false
|
|
|
+ userCurrentFoodRecommendMapper.delete(new LambdaQueryWrapper<UserCurrentFoodRecommend>()
|
|
|
+ .eq(UserCurrentFoodRecommend::getUserId, userId));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 问卷画像/偏好按原始成员 id 加载,避免一个 users.id 对应多张成员卡时取错问卷
|
|
|
+ HealthFilterService.HealthProfile profile = null;
|
|
|
+ DietPreferences prefs = null;
|
|
|
+ try {
|
|
|
+ profile = healthFilterService.loadProfile(memberId, userId, null);
|
|
|
+ prefs = healthFilterService.loadDietPreferences(memberId);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("加载用户{}当前食材问卷画像失败 memberId={}: {}", userId, memberId, e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, Food> foodMap = loadFoods(indices);
|
|
|
+ Date now = new Date();
|
|
|
+ List<UserCurrentFoodRecommend> targetRows = new ArrayList<>();
|
|
|
+ Set<Long> baselineFoodIds = new HashSet<>();
|
|
|
+
|
|
|
+ for (FoodRecommendIndex idx : indices) {
|
|
|
+ Long foodId = idx.getFoodId();
|
|
|
+ if (foodId == null) {
|
|
|
+ // 唯一键 (user_id, food_id) 需要确定 food_id,跳过脏数据
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ baselineFoodIds.add(foodId);
|
|
|
+
|
|
|
+ Food food = buildFood(idx, foodMap.get(foodId));
|
|
|
+ String adjustment = "normal";
|
|
|
+ String reason = "";
|
|
|
+ if (profile != null) {
|
|
|
+ try {
|
|
|
+ HealthFilterService.FoodAdjustmentResult adj =
|
|
|
+ healthFilterService.assessFoodAdjustment(profile, prefs, food);
|
|
|
+ if (adj != null) {
|
|
|
+ adjustment = adj.adjustment != null ? adj.adjustment : "normal";
|
|
|
+ reason = adj.reason != null ? adj.reason : "";
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("食材{}问卷调整判定失败 userId={}: {}", food.getName(), userId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ int baseScore = idx.getIndexScore() != null ? idx.getIndexScore() : 0;
|
|
|
+ int currentScore = (int) Math.round(FoodRecommendWeightEngine.clamp(
|
|
|
+ baseScore + FoodRecommendWeightEngine.healthAdjustScore(adjustment)));
|
|
|
+
|
|
|
+ UserCurrentFoodRecommend row = new UserCurrentFoodRecommend();
|
|
|
+ row.setUserId(userId);
|
|
|
+ row.setFoodId(foodId);
|
|
|
+ row.setFoodName(idx.getFoodName() != null ? idx.getFoodName() : food.getName());
|
|
|
+ row.setCategory(food.getCategory());
|
|
|
+ row.setBaseScore(baseScore);
|
|
|
+ row.setSourceReportId(idx.getHealthReportId());
|
|
|
+ row.setAdjustment(adjustment);
|
|
|
+ row.setAdjustmentReason(truncate(reason, ADJUSTMENT_REASON_MAX_LEN));
|
|
|
+ row.setCurrentScore(currentScore);
|
|
|
+ row.setRebuiltAt(now);
|
|
|
+ targetRows.add(row);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除基线中已不存在的行;空基线时全删(notIn 空集合的 SQL 语义不可靠)
|
|
|
+ LambdaQueryWrapper<UserCurrentFoodRecommend> staleWrapper =
|
|
|
+ new LambdaQueryWrapper<UserCurrentFoodRecommend>().eq(UserCurrentFoodRecommend::getUserId, userId);
|
|
|
+ if (baselineFoodIds.isEmpty()) {
|
|
|
+ userCurrentFoodRecommendMapper.delete(staleWrapper);
|
|
|
+ } else {
|
|
|
+ userCurrentFoodRecommendMapper.delete(staleWrapper.notIn(UserCurrentFoodRecommend::getFoodId, baselineFoodIds));
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Long, UserCurrentFoodRecommend> existingMap = new HashMap<>();
|
|
|
+ List<UserCurrentFoodRecommend> existingRows = userCurrentFoodRecommendMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<UserCurrentFoodRecommend>()
|
|
|
+ .eq(UserCurrentFoodRecommend::getUserId, userId));
|
|
|
+ if (existingRows != null) {
|
|
|
+ for (UserCurrentFoodRecommend row : existingRows) {
|
|
|
+ if (row != null && row.getFoodId() != null) {
|
|
|
+ existingMap.put(row.getFoodId(), row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (UserCurrentFoodRecommend row : targetRows) {
|
|
|
+ UserCurrentFoodRecommend old = existingMap.get(row.getFoodId());
|
|
|
+ if (old == null) {
|
|
|
+ userCurrentFoodRecommendMapper.insert(row);
|
|
|
+ } else if (changed(old, row)) {
|
|
|
+ row.setId(old.getId());
|
|
|
+ userCurrentFoodRecommendMapper.updateById(row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 用户当前食材全量列表,按当前分降序 */
|
|
|
+ public List<UserCurrentFoodRecommend> getCurrentFoods(Long userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ return userCurrentFoodRecommendMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<UserCurrentFoodRecommend>()
|
|
|
+ .eq(UserCurrentFoodRecommend::getUserId, userId)
|
|
|
+ .orderByDesc(UserCurrentFoodRecommend::getCurrentScore));
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 是否已有当前食材数据 */
|
|
|
+ public boolean hasData(Long userId) {
|
|
|
+ if (userId == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ Long count = userCurrentFoodRecommendMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<UserCurrentFoodRecommend>()
|
|
|
+ .eq(UserCurrentFoodRecommend::getUserId, userId));
|
|
|
+ return count != null && count > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 为食谱生成构建逐成员上下文(个人食材 + 合并约束 + 缺食材成员)。
|
|
|
+ *
|
|
|
+ * <p>{@code strictAllExplicit} 只影响 {@code hasMissing} 语义:显式传入参与成员时为 true——任一成员
|
|
|
+ * 缺食材即由调用方阻断;隐式兜底成员时为 false——缺食材仅通过 {@code missingMemberNames} 提示,
|
|
|
+ * 由调用方决定是否继续生成。</p>
|
|
|
+ */
|
|
|
+ public Map<String, Object> buildGenerationContext(List<Long> participantMemberIds, boolean strictAllExplicit) {
|
|
|
+ Map<String, List<String>> constraints = new LinkedHashMap<String, List<String>>();
|
|
|
+ constraints.put("allergies", new ArrayList<String>());
|
|
|
+ constraints.put("diseases", new ArrayList<String>());
|
|
|
+ constraints.put("medications", new ArrayList<String>());
|
|
|
+ constraints.put("healthGoals", new ArrayList<String>());
|
|
|
+ constraints.put("cuisinePrefs", new ArrayList<String>());
|
|
|
+ constraints.put("flavorPrefs", new ArrayList<String>());
|
|
|
+ constraints.put("spiceLevels", new ArrayList<String>());
|
|
|
+ constraints.put("mustEat", new ArrayList<String>());
|
|
|
+
|
|
|
+ List<String> missingMemberNames = new ArrayList<String>();
|
|
|
+ List<Map<String, Object>> candidates = new ArrayList<Map<String, Object>>();
|
|
|
+
|
|
|
+ if (participantMemberIds != null) {
|
|
|
+ for (Long memberId : participantMemberIds) {
|
|
|
+ if (memberId == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ FamilyMember fm = familyMemberMapper.selectById(memberId);
|
|
|
+ Long uid = (fm != null && fm.getUserId() != null) ? fm.getUserId() : memberId;
|
|
|
+ String memberName = (fm != null && fm.getNickname() != null && !fm.getNickname().trim().isEmpty())
|
|
|
+ ? fm.getNickname() : String.valueOf(memberId);
|
|
|
+
|
|
|
+ List<UserCurrentFoodRecommend> foods = getCurrentFoods(uid);
|
|
|
+ boolean hasAvailableFood = false;
|
|
|
+ if (foods != null) {
|
|
|
+ for (UserCurrentFoodRecommend row : foods) {
|
|
|
+ if (row == null || "blocked".equalsIgnoreCase(row.getAdjustment())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ hasAvailableFood = true;
|
|
|
+ Map<String, Object> item = new LinkedHashMap<String, Object>();
|
|
|
+ item.put("memberId", memberId);
|
|
|
+ item.put("memberName", memberName);
|
|
|
+ item.put("name", row.getFoodName());
|
|
|
+ item.put("score", row.getCurrentScore());
|
|
|
+ item.put("adjustment", row.getAdjustment() != null ? row.getAdjustment() : "normal");
|
|
|
+ candidates.add(item);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!hasAvailableFood) {
|
|
|
+ missingMemberNames.add(memberName);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 硬禁忌取并集(安全优先);软偏好逐成员收集后交由 prompt 使用
|
|
|
+ try {
|
|
|
+ HealthFilterService.HealthProfile profile =
|
|
|
+ healthFilterService.loadProfile(memberId, uid, null);
|
|
|
+ if (profile != null) {
|
|
|
+ addAll(constraints.get("allergies"), profile.allergies);
|
|
|
+ addAll(constraints.get("diseases"), profile.diseases);
|
|
|
+ addAll(constraints.get("medications"), profile.medications);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("成员{}健康画像合并失败: {}", memberId, e.getMessage());
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ DietPreferences prefs = healthFilterService.loadDietPreferences(memberId);
|
|
|
+ if (prefs != null) {
|
|
|
+ addAll(constraints.get("healthGoals"), parseValues(prefs.getHealthGoals()));
|
|
|
+ addAll(constraints.get("cuisinePrefs"), parseValues(prefs.getCuisinePref()));
|
|
|
+ addAll(constraints.get("flavorPrefs"), parseValues(prefs.getFlavorPref()));
|
|
|
+ addAll(constraints.get("mustEat"), parseValues(prefs.getMustEat()));
|
|
|
+ if (prefs.getSpiceLevel() != null) {
|
|
|
+ constraints.get("spiceLevels").add(String.valueOf(prefs.getSpiceLevel()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("成员{}饮食偏好合并失败: {}", memberId, e.getMessage());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("成员{}当前食材解析失败,已跳过: {}", memberId, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按食材名去重:保留最大分数,任一人 boost 即标 boost;按分数降序取 Top 30
|
|
|
+ Map<String, Map<String, Object>> dedup = new LinkedHashMap<String, Map<String, Object>>();
|
|
|
+ Map<String, Boolean> boostSeen = new HashMap<String, Boolean>();
|
|
|
+ for (Map<String, Object> item : candidates) {
|
|
|
+ String name = (String) item.get("name");
|
|
|
+ if (name == null || name.trim().isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boolean boost = "boost".equalsIgnoreCase((String) item.get("adjustment"));
|
|
|
+ Map<String, Object> prev = dedup.get(name);
|
|
|
+ if (prev == null) {
|
|
|
+ dedup.put(name, new LinkedHashMap<String, Object>(item));
|
|
|
+ boostSeen.put(name, Boolean.valueOf(boost));
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ boostSeen.put(name, Boolean.valueOf(Boolean.TRUE.equals(boostSeen.get(name)) || boost));
|
|
|
+ if (intValue(item.get("score")) > intValue(prev.get("score"))) {
|
|
|
+ dedup.put(name, new LinkedHashMap<String, Object>(item));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Map<String, Object>> selected = new ArrayList<Map<String, Object>>(dedup.values());
|
|
|
+ for (Map<String, Object> item : selected) {
|
|
|
+ if (Boolean.TRUE.equals(boostSeen.get((String) item.get("name")))) {
|
|
|
+ item.put("adjustment", "boost");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ selected.sort((a, b) -> Integer.compare(intValue(b.get("score")), intValue(a.get("score"))));
|
|
|
+ if (selected.size() > SELECTED_FOODS_LIMIT) {
|
|
|
+ selected = new ArrayList<Map<String, Object>>(selected.subList(0, SELECTED_FOODS_LIMIT));
|
|
|
+ }
|
|
|
+
|
|
|
+ String selectedFoodsJson;
|
|
|
+ try {
|
|
|
+ selectedFoodsJson = objectMapper.writeValueAsString(selected);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("selected_foods 序列化失败: {}", e.getMessage());
|
|
|
+ selectedFoodsJson = "[]";
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Object> result = new LinkedHashMap<String, Object>();
|
|
|
+ result.put("selectedFoodsJson", selectedFoodsJson);
|
|
|
+ result.put("constraints", constraints);
|
|
|
+ result.put("missingMemberNames", missingMemberNames);
|
|
|
+ result.put("hasMissing", Boolean.valueOf(!missingMemberNames.isEmpty() && strictAllExplicit));
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ---------------------------------------------------------------- helpers
|
|
|
+
|
|
|
+ private Map<Long, Food> loadFoods(List<FoodRecommendIndex> indices) {
|
|
|
+ Map<Long, Food> map = new HashMap<Long, Food>();
|
|
|
+ Set<Long> foodIds = new HashSet<Long>();
|
|
|
+ for (FoodRecommendIndex idx : indices) {
|
|
|
+ if (idx != null && idx.getFoodId() != null) {
|
|
|
+ foodIds.add(idx.getFoodId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (foodIds.isEmpty()) {
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ List<Food> foods = foodService.listByIds(new ArrayList<Long>(foodIds));
|
|
|
+ if (foods != null) {
|
|
|
+ for (Food f : foods) {
|
|
|
+ if (f != null && f.getId() != null) {
|
|
|
+ map.put(f.getId(), f);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("当前食材分类/禁忌补齐失败: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 由报告基线行构造 Food:报告分/食材名来自 index,分类与禁忌来自 foods 表(存在时) */
|
|
|
+ private Food buildFood(FoodRecommendIndex idx, Food persisted) {
|
|
|
+ Food food = new Food();
|
|
|
+ if (persisted != null) {
|
|
|
+ food.setName(persisted.getName() != null ? persisted.getName() : idx.getFoodName());
|
|
|
+ food.setCategory(persisted.getCategory());
|
|
|
+ food.setAllergens(persisted.getAllergens());
|
|
|
+ food.setDietType(persisted.getDietType());
|
|
|
+ } else {
|
|
|
+ food.setName(idx.getFoodName());
|
|
|
+ }
|
|
|
+ food.setScore(idx.getIndexScore());
|
|
|
+ return food;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean changed(UserCurrentFoodRecommend old, UserCurrentFoodRecommend row) {
|
|
|
+ return !Objects.equals(old.getFoodName(), row.getFoodName())
|
|
|
+ || !Objects.equals(old.getCategory(), row.getCategory())
|
|
|
+ || !Objects.equals(old.getBaseScore(), row.getBaseScore())
|
|
|
+ || !Objects.equals(old.getSourceReportId(), row.getSourceReportId())
|
|
|
+ || !Objects.equals(old.getAdjustment(), row.getAdjustment())
|
|
|
+ || !Objects.equals(old.getAdjustmentReason(), row.getAdjustmentReason())
|
|
|
+ || !Objects.equals(old.getCurrentScore(), row.getCurrentScore());
|
|
|
+ }
|
|
|
+
|
|
|
+ private String truncate(String text, int maxLen) {
|
|
|
+ if (text == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ return text.length() <= maxLen ? text : text.substring(0, maxLen);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addAll(List<String> target, Set<String> values) {
|
|
|
+ if (target == null || values == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String v : values) {
|
|
|
+ if (v != null && !v.trim().isEmpty()) {
|
|
|
+ target.add(v.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addAll(List<String> target, List<String> values) {
|
|
|
+ if (target == null || values == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (String v : values) {
|
|
|
+ if (v != null && !v.trim().isEmpty()) {
|
|
|
+ target.add(v.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 兼容 JSON 数组/单值与逗号分隔字符串,解析异常时退化为分隔符切分 */
|
|
|
+ private List<String> parseValues(String raw) {
|
|
|
+ List<String> values = new ArrayList<String>();
|
|
|
+ if (raw == null || raw.trim().isEmpty()) {
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+ String text = raw.trim();
|
|
|
+ try {
|
|
|
+ JsonNode node = objectMapper.readTree(text);
|
|
|
+ if (node != null) {
|
|
|
+ if (node.isArray()) {
|
|
|
+ for (JsonNode child : node) {
|
|
|
+ appendNodeValue(values, child);
|
|
|
+ }
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+ if (node.isValueNode()) {
|
|
|
+ String v = node.asText();
|
|
|
+ if (!v.trim().isEmpty()) {
|
|
|
+ values.add(v.trim());
|
|
|
+ }
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+ if (node.isObject()) {
|
|
|
+ java.util.Iterator<JsonNode> it = node.elements();
|
|
|
+ while (it.hasNext()) {
|
|
|
+ appendNodeValue(values, it.next());
|
|
|
+ }
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 非 JSON → 按分隔符切分
|
|
|
+ }
|
|
|
+ for (String part : text.split("[,,、\\s]+")) {
|
|
|
+ if (!part.trim().isEmpty()) {
|
|
|
+ values.add(part.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return values;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void appendNodeValue(List<String> values, JsonNode node) {
|
|
|
+ if (node == null || node.isNull()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (node.isArray()) {
|
|
|
+ for (JsonNode child : node) {
|
|
|
+ appendNodeValue(values, child);
|
|
|
+ }
|
|
|
+ } else if (node.isValueNode()) {
|
|
|
+ String v = node.asText();
|
|
|
+ if (!v.trim().isEmpty()) {
|
|
|
+ values.add(v.trim());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int intValue(Object value) {
|
|
|
+ if (value instanceof Number) {
|
|
|
+ return ((Number) value).intValue();
|
|
|
+ }
|
|
|
+ if (value instanceof String) {
|
|
|
+ try {
|
|
|
+ return Integer.parseInt((String) value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|