Explorar el Código

feat(recommend): 价格 Provider 实现(订单服务预留口子+降级)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jintao.geng hace 1 mes
padre
commit
e63612f5e7

+ 75 - 0
mango-domain/src/main/java/com/mangoo/rating/recommend/service/recommend/impl/EfficiencyPriceProviderImpl.java

@@ -0,0 +1,75 @@
+package com.mangoo.rating.recommend.service.recommend.impl;
+
+import com.mangoo.rating.recommend.config.RecommendProperties;
+import com.mangoo.rating.recommend.dto.product.ProductServiceLevelCacheDTO;
+import com.mangoo.rating.recommend.enums.EvaluateEfficiencyEnum;
+import com.mangoo.rating.recommend.service.recommend.EfficiencyPriceProvider;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 时效价格 Provider 实现(预留调订单服务的口子 + 降级)。
+ *
+ * 当前实现:未接通订单服务 Feign,直接返回空 Map,触发上层"待计算"降级。
+ * 后续接通时:在 fetchServiceLevels() 中通过 Feign 调订单服务、得到 ProductServiceLevelCacheDTO 列表,
+ *           再用 RecommendProperties.timeLimitMapping 把 timeLimit 字符串映射到 EvaluateEfficiencyEnum.code。
+ *
+ * @author gengjintao
+ * @date 2026/06/24
+ */
+@Slf4j
+@Component
+public class EfficiencyPriceProviderImpl implements EfficiencyPriceProvider {
+
+    @Resource
+    private RecommendProperties recommendProperties;
+
+    @Override
+    public Map<Integer, BigDecimal> loadEfficiencyPrices() {
+        try {
+            // 1. 从订单服务获取全部服务等级(预留口子,当前返回空列表 → 触发降级)
+            List<ProductServiceLevelCacheDTO> levels = fetchServiceLevels();
+            if (levels == null || levels.isEmpty()) {
+                log.info("时效价格未配置或订单服务未接通,全部走'待计算'降级");
+                return Collections.emptyMap();
+            }
+
+            // 2. 将服务等级的 timeLimit(字符串) → EvaluateEfficiencyEnum.code 后聚合
+            Map<Integer, BigDecimal> priceMap = new LinkedHashMap<>();
+            Map<String, Integer> mapping = recommendProperties.getTimeLimitMapping();
+            for (ProductServiceLevelCacheDTO lvl : levels) {
+                if (lvl == null || lvl.getTimeLimit() == null || lvl.getPrice() == null) {
+                    continue;
+                }
+                Integer effCode = mapping == null ? null : mapping.get(lvl.getTimeLimit().trim());
+                if (effCode == null || EvaluateEfficiencyEnum.getByCode(effCode) == null) {
+                    log.debug("无法映射服务等级 timeLimit={} 到时效 code,跳过", lvl.getTimeLimit());
+                    continue;
+                }
+                // 同档存在多条时,保留首个(订单服务侧应已去重;此处简单防御)
+                priceMap.putIfAbsent(effCode, lvl.getPrice());
+            }
+            return priceMap;
+        } catch (Exception e) {
+            // 价格能力任何异常都不应阻断推荐主体结果
+            log.warn("加载时效价格异常,本次降级为'待计算'", e);
+            return Collections.emptyMap();
+        }
+    }
+
+    /**
+     * 调订单服务取全部服务等级 —— 预留口子。
+     * 本期返回空列表(不接 Feign),后续接通时实现真实调用。
+     */
+    private List<ProductServiceLevelCacheDTO> fetchServiceLevels() {
+        // TODO: 后续接入 order-service Feign 客户端:调用其"全部服务等级"接口
+        return Collections.emptyList();
+    }
+}