Просмотр исходного кода

feat(food): implement food.rule.pairingMatrix service + mapper (S-cfc-507)

E2E Test Bot 1 час назад
Родитель
Сommit
af9a9558d9

+ 12 - 0
cfc-backend/src/main/java/com/etotem/cfc/mapper/FoodPairingMatrixMapper.java

@@ -0,0 +1,12 @@
+package com.etotem.cfc.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.etotem.cfc.entity.FoodPairingMatrix;
+import org.apache.ibatis.annotations.Mapper;
+
+/**
+ * 食材配伍矩阵 Mapper(能力键 {@code food.rule.pairingMatrix},CR-03 / §4.3)。
+ */
+@Mapper
+public interface FoodPairingMatrixMapper extends BaseMapper<FoodPairingMatrix> {
+}

+ 168 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FoodPairingMatrixService.java

@@ -0,0 +1,168 @@
+package com.etotem.cfc.service;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.etotem.cfc.entity.FoodPairingMatrix;
+import com.etotem.cfc.mapper.FoodPairingMatrixMapper;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 食材配伍矩阵查询/效应计算/忌配伍判定服务(能力键 {@code food.rule.pairingMatrix},CR-03 / §4.3)。
+ */
+@Slf4j
+@Service
+public class FoodPairingMatrixService {
+
+    public static final List<String> RELATION_TYPES = Collections.unmodifiableList(Arrays.asList(
+            "synergy", "antagonism", "xiangsheng", "xiangke", "xiangwei", "xiangsha", "xiangwu"));
+    public static final List<String> SOURCES = Collections.unmodifiableList(Arrays.asList(
+            "modern_nutrition", "tcm_shi_liao"));
+    public static final double EFFECT_SCORE_MIN = -50.0;
+    public static final double EFFECT_SCORE_MAX = 50.0;
+    public static final int AVOIDANCE_MIN_INTENSITY = 3;
+
+    @Resource
+    private FoodPairingMatrixMapper foodPairingMatrixMapper;
+
+    public List<String> supportedRelationTypes() {
+        return RELATION_TYPES;
+    }
+
+    public boolean isValidRelationType(String relationType) {
+        return relationType != null && RELATION_TYPES.contains(relationType);
+    }
+
+    public List<String> supportedSources() {
+        return SOURCES;
+    }
+
+    public boolean isValidSource(String source) {
+        return source != null && SOURCES.contains(source);
+    }
+
+    public double clampEffectScore(double value) {
+        if (value < EFFECT_SCORE_MIN) {
+            return EFFECT_SCORE_MIN;
+        }
+        if (value > EFFECT_SCORE_MAX) {
+            return EFFECT_SCORE_MAX;
+        }
+        return value;
+    }
+
+    public double resolvePairingEffect(Long foodAId, Long foodBId, List<Map<String, Object>> rules) {
+        if (foodAId == null || foodBId == null || rules == null || rules.isEmpty()) {
+            return 0.0;
+        }
+        double total = 0.0;
+        for (Map<String, Object> rule : rules) {
+            if (rule == null || !isRuleEnabled(rule)) {
+                continue;
+            }
+            if (!matchesPair(rule, foodAId, foodBId)) {
+                continue;
+            }
+            total += readEffectScore(rule);
+        }
+        return clampEffectScore(total);
+    }
+
+    public boolean requiresAvoidance(Long foodAId, Long foodBId, List<Map<String, Object>> rules) {
+        if (foodAId == null || foodBId == null || rules == null || rules.isEmpty()) {
+            return false;
+        }
+        for (Map<String, Object> rule : rules) {
+            if (rule == null || !isRuleEnabled(rule)) {
+                continue;
+            }
+            if (!matchesPair(rule, foodAId, foodBId)) {
+                continue;
+            }
+            String relationType = readString(rule, "relationType");
+            int intensity = readIntensity(rule);
+            if (isAvoidanceRelation(relationType) && intensity >= AVOIDANCE_MIN_INTENSITY) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public List<FoodPairingMatrix> loadEnabledRules(Long foodAId, Long foodBId) {
+        if (foodAId == null || foodBId == null || foodPairingMatrixMapper == null) {
+            return Collections.emptyList();
+        }
+        LambdaQueryWrapper<FoodPairingMatrix> wrapper = new LambdaQueryWrapper<FoodPairingMatrix>()
+                .eq(FoodPairingMatrix::getEnabled, 1)
+                .and(w -> w
+                        .and(a -> a.eq(FoodPairingMatrix::getFoodAId, foodAId)
+                                .eq(FoodPairingMatrix::getFoodBId, foodBId))
+                        .or(b -> b.eq(FoodPairingMatrix::getFoodAId, foodBId)
+                                .eq(FoodPairingMatrix::getFoodBId, foodAId)));
+        List<FoodPairingMatrix> rules = foodPairingMatrixMapper.selectList(wrapper);
+        return rules == null ? Collections.<FoodPairingMatrix>emptyList() : rules;
+    }
+
+    private boolean isAvoidanceRelation(String relationType) {
+        return "xiangke".equals(relationType) || "antagonism".equals(relationType);
+    }
+
+    private boolean matchesPair(Map<String, Object> rule, Long foodAId, Long foodBId) {
+        Long ruleA = readLong(rule, "foodAId");
+        Long ruleB = readLong(rule, "foodBId");
+        if (ruleA == null || ruleB == null) {
+            return false;
+        }
+        return (ruleA.equals(foodAId) && ruleB.equals(foodBId))
+                || (ruleA.equals(foodBId) && ruleB.equals(foodAId));
+    }
+
+    private boolean isRuleEnabled(Map<String, Object> rule) {
+        Object enabled = rule.get("enabled");
+        if (enabled instanceof Number) {
+            return ((Number) enabled).intValue() == 1;
+        }
+        return Boolean.TRUE.equals(enabled);
+    }
+
+    private double readEffectScore(Map<String, Object> rule) {
+        Object score = rule.get("effectScore");
+        if (score instanceof Number) {
+            return ((Number) score).doubleValue();
+        }
+        return 0.0;
+    }
+
+    private int readIntensity(Map<String, Object> rule) {
+        Object intensity = rule.get("intensity");
+        if (intensity instanceof Number) {
+            return ((Number) intensity).intValue();
+        }
+        return 0;
+    }
+
+    private String readString(Map<String, Object> rule, String key) {
+        Object value = rule.get(key);
+        return value == null ? null : value.toString();
+    }
+
+    private Long readLong(Map<String, Object> rule, String key) {
+        Object value = rule.get(key);
+        if (value instanceof Number) {
+            return ((Number) value).longValue();
+        }
+        if (value instanceof String && !((String) value).isEmpty()) {
+            try {
+                return Long.valueOf((String) value);
+            } catch (NumberFormatException ignored) {
+                return null;
+            }
+        }
+        return null;
+    }
+}

+ 19 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FoodService.java

@@ -26,6 +26,25 @@ public class FoodService {
     @Resource
     @Resource
     private SeasonalFoodMapper seasonalFoodMapper;
     private SeasonalFoodMapper seasonalFoodMapper;
 
 
+    /**
+     * 食材配伍矩阵服务(能力键 {@code food.rule.pairingMatrix},§4.3 / G-3)。
+     * 推荐链路通过此服务查询食材 A×B 配伍关系,计算 effect_score 调整值并判定忌配伍(硬避让)。
+     */
+    @Resource
+    private FoodPairingMatrixService foodPairingMatrixService;
+
+    /** 查询食材 A×B 的配伍效应对推荐指数的调整值(§4.3「pairing_effect × w4」)。 */
+    public double resolvePairingEffect(Long foodAId, Long foodBId,
+                                       List<Map<String, Object>> pairingRules) {
+        return foodPairingMatrixService.resolvePairingEffect(foodAId, foodBId, pairingRules);
+    }
+
+    /** 判定食材 A×B 是否忌配伍、需硬避让(§4.3 / G-3 / CR-07)。 */
+    public boolean requiresAvoidance(Long foodAId, Long foodBId,
+                                     List<Map<String, Object>> pairingRules) {
+        return foodPairingMatrixService.requiresAvoidance(foodAId, foodBId, pairingRules);
+    }
+
     public List<Food> listAll(String category, String status) {
     public List<Food> listAll(String category, String status) {
         return listAll(category, status, null);
         return listAll(category, status, null);
     }
     }