package com.chowbox.controller; import com.chowbox.model.ApiResponse; import com.chowbox.model.FridgeMatchResult; import com.chowbox.service.FridgeService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.Map; @Slf4j @RestController @RequestMapping("/api/fridge") public class FridgeController { @Autowired private FridgeService fridgeService; @PostMapping("/match") public ApiResponse> match(@RequestBody Map body) { Object ingObj = body.get("ingredients"); log.info("Fridge match request: ingredients type={}, value={}", ingObj != null ? ingObj.getClass().getSimpleName() : "null", ingObj); List ingredients = new ArrayList<>(); if (ingObj instanceof List) { for (Object item : (List) ingObj) { if (item != null) ingredients.add(item.toString()); } } if (ingredients.isEmpty()) { return ApiResponse.fail(400, "请输入至少一种食材"); } // 接收前端自定义常备调料 List staples = new ArrayList<>(); Object stapleObj = body.get("staples"); if (stapleObj instanceof List) { for (Object item : (List) stapleObj) { if (item != null) staples.add(item.toString()); } } log.info("Fridge match: parsed ingredients={}, staples={}", ingredients, staples); List results = fridgeService.matchRecipes(ingredients, staples); log.info("Fridge match: {} results", results.size()); return ApiResponse.ok(results); } }