|
|
@@ -15,19 +15,40 @@ import java.util.Map;
|
|
|
@RequestMapping("/api/nutrition/profile")
|
|
|
public class UserNutritionProfileController {
|
|
|
|
|
|
+ /** 未显式设定目标时的默认日目标(SC-P-69:食谱推荐侧始终有目标可用)。 */
|
|
|
+ private static final int DEFAULT_CALORIE_TARGET = 2000;
|
|
|
+ private static final int DEFAULT_PROTEIN_TARGET = 60;
|
|
|
+ private static final int DEFAULT_FAT_TARGET = 50;
|
|
|
+ private static final int DEFAULT_CARB_TARGET = 200;
|
|
|
+ private static final int DEFAULT_FIBER_TARGET = 25;
|
|
|
+
|
|
|
+ /** 热量合理区间上限(TC-P-69-E:超上限返回 400『热量需在合理区间』)。 */
|
|
|
+ private static final int CALORIE_TARGET_MAX = 5000;
|
|
|
+ /** 结构性不可能值闸门:超出该量级属于非法入参,直接由守卫抛异常拦截,不落库。 */
|
|
|
+ private static final int CALORIE_TARGET_HARD_MAX = 500000;
|
|
|
+
|
|
|
@Resource
|
|
|
private UserNutritionProfileService service;
|
|
|
|
|
|
@Operation(summary = "获取当前用户营养偏好")
|
|
|
@PostMapping("/get")
|
|
|
public Result<UserNutritionProfile> get(@RequestAttribute("userId") Long userId) {
|
|
|
- return Result.success(service.getByUserId(userId));
|
|
|
+ UserNutritionProfile profile = service.getByUserId(userId);
|
|
|
+ if (profile != null) {
|
|
|
+ applyDefaultTargets(profile);
|
|
|
+ }
|
|
|
+ return Result.success(profile);
|
|
|
}
|
|
|
|
|
|
- @Operation(summary = "保存营养偏好")
|
|
|
+ @Operation(summary = "保存营养偏好(含每日营养/热量目标)")
|
|
|
@PostMapping("/save")
|
|
|
public Result<String> save(@RequestAttribute("userId") Long userId,
|
|
|
@RequestBody Map<String, Object> params) {
|
|
|
+ Result<String> invalid = validateNutritionTargets(params);
|
|
|
+ if (invalid != null) {
|
|
|
+ return invalid;
|
|
|
+ }
|
|
|
+
|
|
|
UserNutritionProfile profile = service.getByUserId(userId);
|
|
|
if (profile == null) {
|
|
|
profile = new UserNutritionProfile();
|
|
|
@@ -48,7 +69,101 @@ public class UserNutritionProfileController {
|
|
|
if (params.get("cookingAbility") != null) {
|
|
|
profile.setCookingAbility((String) params.get("cookingAbility"));
|
|
|
}
|
|
|
+
|
|
|
+ applyDefaultTargets(profile);
|
|
|
+ applyNutritionTargets(profile, params);
|
|
|
service.save(profile);
|
|
|
return Result.success("ok");
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 营养目标合法性守卫(SC-P-69 / TC-P-69-E 边界)。
|
|
|
+ * 返回 null 表示合法;返回 400 Result 表示普通越界,抛 IllegalArgumentException 表示结构性非法入参。
|
|
|
+ * 两条路径均在落库之前拦截。
|
|
|
+ */
|
|
|
+ private Result<String> validateNutritionTargets(Map<String, Object> params) {
|
|
|
+ Integer calorie = null;
|
|
|
+ try {
|
|
|
+ calorie = parseTargetValue(params.get("calorieTarget"));
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ return Result.badRequest("热量需在合理区间");
|
|
|
+ }
|
|
|
+ if (calorie != null) {
|
|
|
+ // -1「取消/无限目标」哨兵与不可能量级值视为结构性非法,硬拦截。
|
|
|
+ if (calorie == -1 || calorie > CALORIE_TARGET_HARD_MAX) {
|
|
|
+ throw new IllegalArgumentException("目标热量非法:" + calorie);
|
|
|
+ }
|
|
|
+ if (calorie < 0 || calorie > CALORIE_TARGET_MAX) {
|
|
|
+ return Result.badRequest("热量需在合理区间");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String[] macroNames = {"proteinTarget", "fatTarget", "carbTarget", "fiberTarget"};
|
|
|
+ int[] macroMax = {300, 200, 800, 100};
|
|
|
+ for (int i = 0; i < macroNames.length; i++) {
|
|
|
+ Integer value;
|
|
|
+ try {
|
|
|
+ value = parseTargetValue(params.get(macroNames[i]));
|
|
|
+ } catch (RuntimeException e) {
|
|
|
+ return Result.badRequest("营养目标需在合理区间");
|
|
|
+ }
|
|
|
+ if (value != null && (value < 0 || value > macroMax[i])) {
|
|
|
+ return Result.badRequest("营养目标需在合理区间");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 将合法目标写入档案字段,供食谱推荐侧读取(只读取本能力产出)。 */
|
|
|
+ private void applyNutritionTargets(UserNutritionProfile profile, Map<String, Object> params) {
|
|
|
+ Integer calorie = parseTargetValue(params.get("calorieTarget"));
|
|
|
+ if (calorie != null) {
|
|
|
+ profile.setCalorieTarget(calorie);
|
|
|
+ }
|
|
|
+ String[] macroNames = {"proteinTarget", "fatTarget", "carbTarget", "fiberTarget"};
|
|
|
+ for (String name : macroNames) {
|
|
|
+ Integer value = parseTargetValue(params.get(name));
|
|
|
+ if (value != null) {
|
|
|
+ if ("proteinTarget".equals(name)) {
|
|
|
+ profile.setProteinTarget(value);
|
|
|
+ } else if ("fatTarget".equals(name)) {
|
|
|
+ profile.setFatTarget(value);
|
|
|
+ } else if ("carbTarget".equals(name)) {
|
|
|
+ profile.setCarbTarget(value);
|
|
|
+ } else {
|
|
|
+ profile.setFiberTarget(value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 存量档案缺失目标时回填默认值,保证食谱推荐始终有目标可用。 */
|
|
|
+ private void applyDefaultTargets(UserNutritionProfile profile) {
|
|
|
+ if (profile.getCalorieTarget() == null) {
|
|
|
+ profile.setCalorieTarget(DEFAULT_CALORIE_TARGET);
|
|
|
+ }
|
|
|
+ if (profile.getProteinTarget() == null) {
|
|
|
+ profile.setProteinTarget(DEFAULT_PROTEIN_TARGET);
|
|
|
+ }
|
|
|
+ if (profile.getFatTarget() == null) {
|
|
|
+ profile.setFatTarget(DEFAULT_FAT_TARGET);
|
|
|
+ }
|
|
|
+ if (profile.getCarbTarget() == null) {
|
|
|
+ profile.setCarbTarget(DEFAULT_CARB_TARGET);
|
|
|
+ }
|
|
|
+ if (profile.getFiberTarget() == null) {
|
|
|
+ profile.setFiberTarget(DEFAULT_FIBER_TARGET);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 兼容 Integer / Long / 字符串数字;格式错误交由守卫转成 400,不抛原始异常。 */
|
|
|
+ private Integer parseTargetValue(Object raw) {
|
|
|
+ if (raw == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (raw instanceof Number) {
|
|
|
+ return ((Number) raw).intValue();
|
|
|
+ }
|
|
|
+ return Integer.valueOf(raw.toString().trim());
|
|
|
+ }
|
|
|
}
|