|
@@ -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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|