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

feat(energy): S-cfc-483 energy.dimension.view 只读配置查看实现使绿(POST /api/energy/config)

Sisyphus 1 час назад
Родитель
Сommit
600c1a2d74

+ 89 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/EnergyController.java

@@ -5,6 +5,7 @@ import com.etotem.cfc.common.Result;
 import com.etotem.cfc.dto.EnergySandboxDTO;
 import com.etotem.cfc.dto.MemberEnergyDTO;
 import com.etotem.cfc.dto.UnlockGateDTO;
+import com.etotem.cfc.entity.EnergyDimensionConfig;
 import com.etotem.cfc.entity.EnergyLog;
 import com.etotem.cfc.entity.UnlockGate;
 import com.etotem.cfc.entity.User;
@@ -282,6 +283,94 @@ public class EnergyController {
         return Result.success("配置成功");
     }
 
+    /**
+     * 家长端只读查看五维(身/心/智/行/富)定义与权重(SC-P-19 / energy.dimension.view)。
+     *
+     * <p>只读:仅读配置,不触发任何 Mapper 写操作(写配置走 admin.dimension.config / S-cfc-199)。
+     * 配置未同步或部分同步时按默认口径兜底并给出说明;已同步配置的权重合计 ≠100% 时返回 400。
+     * 显式指定非本家庭成员 memberId 时按 SC-X-19 隔离口径 403。</p>
+     */
+    @Operation(summary = "只读查看五维能量定义与权重(配置未同步时兜底)")
+    @PostMapping("/config")
+    public Result<Map<String, Object>> getDimensionConfigView(
+            @RequestBody(required = false) Map<String, Object> params,
+            @RequestAttribute(value = "familyId", required = false) Long familyId,
+            @RequestAttribute(value = "currentMemberId", required = false) Long currentMemberId) {
+
+        if (familyId == null) {
+            return Result.noFamily("请先创建或加入家庭");
+        }
+        Long memberId = params != null
+                ? ParamUtils.getLong(params.get("memberId"), currentMemberId) : currentMemberId;
+        // SC-X-19 横向越权:显式指定非本家庭成员一律 403;本人/未指定放行
+        if (memberId != null && currentMemberId != null && !memberId.equals(currentMemberId)
+                && !energyService.isMemberInFamily(memberId, familyId)) {
+            return Result.forbidden("无权访问该成员的能量配置");
+        }
+
+        // 五维默认口径(设计 §1.1:身/心/智/行/富;等权兜底合计 100%)
+        String[] codes = {"body", "mind", "wisdom", "action", "wealth"};
+        String[] names = {"身", "心", "智", "行", "富"};
+        String[] definitions = {
+                "身体维度:体质、精力、运动与生活习惯等身心健康基础",
+                "心理维度:情绪、性格、抗压与自我认知等心理状态",
+                "智力维度:学习、思维、认知与知识吸收等能力",
+                "行动维度:执行力、目标管理、坚持与自律等行为习惯",
+                "财富维度:财商、资源管理与价值观等财富素养"
+        };
+
+        List<EnergyDimensionConfig> configs = energyService.getDimensionConfigs();
+        Map<String, EnergyDimensionConfig> configByCode = new LinkedHashMap<String, EnergyDimensionConfig>();
+        double weightSum = 0.0;
+        boolean hasConfig = configs != null && !configs.isEmpty();
+        if (hasConfig) {
+            for (EnergyDimensionConfig config : configs) {
+                if (config == null || config.getDimension() == null) {
+                    continue;
+                }
+                String code = config.getDimension().trim().toLowerCase(Locale.ROOT);
+                configByCode.put(code, config);
+                weightSum += dimensionWeightPercent(config.getWeight());
+            }
+            // TC-P-19-E:已同步配置权重合计必须 =100%,否则 400 拒绝,不静默展示非法权重
+            if (configByCode.isEmpty() || Math.abs(weightSum - 100.0) > 1e-6) {
+                return Result.badRequest("权重合计需为 100%");
+            }
+        }
+
+        boolean fallback = !hasConfig || configByCode.size() < codes.length;
+        Map<String, Object> data = new LinkedHashMap<String, Object>();
+        for (int i = 0; i < codes.length; i++) {
+            String code = codes[i];
+            EnergyDimensionConfig config = configByCode.get(code);
+            Map<String, Object> dim = new LinkedHashMap<String, Object>();
+            dim.put("code", code);
+            dim.put("name", names[i]);
+            dim.put("weight", config != null && config.getWeight() != null
+                    ? config.getWeight() : (hasConfig ? 0 : 20));
+            String definition = config != null && config.getDefinition() != null
+                    && !config.getDefinition().trim().isEmpty() ? config.getDefinition() : definitions[i];
+            dim.put("definition", definition);
+            data.put(code, dim);
+        }
+        data.put("fallback", fallback);
+        data.put("synced", !fallback);
+        if (fallback) {
+            data.put("notice", "部分维度配置未同步,已按默认口径兜底展示");
+            return Result.success("五维配置部分未同步,已使用兜底默认值", data);
+        }
+        return Result.success(data);
+    }
+
+    /** ratio(0~1) 或百分比(0~100) 统一折算为百分比口径(与配置契约一致)。 */
+    private static double dimensionWeightPercent(Integer weight) {
+        if (weight == null) {
+            return 0.0;
+        }
+        double value = weight.doubleValue();
+        return value > 0.0 && value <= 1.0 ? value * 100.0 : value;
+    }
+
     @Operation(summary = "获取五维分数历史")
     @PostMapping("/score-history")
     public Result<List<com.etotem.cfc.entity.FiveDimensionScore>> getScoreHistory(

+ 5 - 0
cfc-backend/src/main/java/com/etotem/cfc/entity/EnergyDimensionConfig.java

@@ -1,6 +1,7 @@
 package com.etotem.cfc.entity;
 
 import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
 import lombok.Data;
@@ -25,5 +26,9 @@ public class EnergyDimensionConfig implements Serializable {
 
     private Integer isActive;
 
+    /** 五维口径说明(SC-P-19 定义载体;非数据库列,配置未同步时由兜底逻辑填充)。 */
+    @TableField(exist = false)
+    private String definition;
+
     private Date updatedAt;
 }

+ 24 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/EnergyService.java

@@ -1615,6 +1615,10 @@ public class EnergyService {
     @Resource
     private EnergyDimensionMapper energyDimensionMapper;
 
+    /** SC-P-19 energy.dimension.view:五维配置只读查询(家长端查看定义/权重)。 */
+    @Resource
+    private EnergyDimensionConfigMapper energyDimensionConfigMapper;
+
     @Resource
     private EnergySourceConfigMapper energySourceConfigMapper;
 
@@ -1962,6 +1966,26 @@ public class EnergyService {
         return false;
     }
 
+    /**
+     * SC-P-19 energy.dimension.view:只读查询五维配置(身/心/智/行/富定义与权重)。
+     *
+     * <p>家长端配置查看为只读操作,仅读 energy_dimension_config,不触发任何写入;
+     * 配置未同步(空表/缺维度)由控制器按默认口径兜底。异常时返回空列表走兜底,
+     * 保证只读查看不会 500(TC-P-19-E)。</p>
+     */
+    public List<EnergyDimensionConfig> getDimensionConfigs() {
+        if (energyDimensionConfigMapper == null) {
+            return Collections.emptyList();
+        }
+        try {
+            List<EnergyDimensionConfig> configs = energyDimensionConfigMapper.selectList(null);
+            return configs == null ? Collections.<EnergyDimensionConfig>emptyList() : configs;
+        } catch (Exception e) {
+            log.warn("读取五维配置异常,走兜底默认口径: {}", e.getMessage());
+            return Collections.emptyList();
+        }
+    }
+
     /**
      * 幂等/去重守卫入口(SC-X-21 重复请求幂等)。
      *