package com.gym.controller; import com.gym.model.TrainingPlan; import com.gym.model.User; import com.gym.service.TrainingPlanService; import com.gym.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @RestController @RequestMapping("/api/training-plans") public class TrainingPlanController { @Autowired private TrainingPlanService trainingPlanService; @Autowired private UserService userService; // 创建训练计划 @PostMapping public Map createPlan(@RequestBody TrainingPlan plan, @RequestParam Long memberId, @RequestParam Long coachId) { Map response = new HashMap<>(); try { TrainingPlan createdPlan = trainingPlanService.createPlan(plan, memberId, coachId); response.put("code", 200); response.put("message", "训练计划创建成功"); response.put("data", createdPlan); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // 获取所有训练计划 @GetMapping public Map getAllPlans() { Map response = new HashMap<>(); List plans = trainingPlanService.getAllPlans(); response.put("code", 200); response.put("message", "获取成功"); response.put("data", plans); response.put("count", plans.size()); return response; } // 获取单个训练计划 @GetMapping("/{id}") public Map getPlanById(@PathVariable Long id) { Map response = new HashMap<>(); return trainingPlanService.getPlanById(id) .map(plan -> { response.put("code", 200); response.put("message", "获取成功"); response.put("data", plan); return response; }) .orElseGet(() -> { response.put("code", 404); response.put("message", "训练计划不存在"); return response; }); } // 获取会员的训练计划 @GetMapping("/member/{memberId}") public Map getMemberPlans(@PathVariable Long memberId) { Map response = new HashMap<>(); List plans = trainingPlanService.getMemberPlans(memberId); response.put("code", 200); response.put("message", "获取成功"); response.put("data", plans); return response; } // 获取教练制定的训练计划 @GetMapping("/coach/{coachId}") public Map getCoachPlans(@PathVariable Long coachId) { Map response = new HashMap<>(); List plans = trainingPlanService.getCoachPlans(coachId); response.put("code", 200); response.put("message", "获取成功"); response.put("data", plans); return response; } // 获取活跃的训练计划 @GetMapping("/member/{memberId}/active") public Map getActivePlans(@PathVariable Long memberId) { Map response = new HashMap<>(); List plans = trainingPlanService.getActivePlansByMember(memberId); response.put("code", 200); response.put("message", "获取成功"); response.put("data", plans); return response; } // 更新训练计划 @PutMapping("/{id}") public Map updatePlan(@PathVariable Long id, @RequestBody TrainingPlan plan) { Map response = new HashMap<>(); try { TrainingPlan updatedPlan = trainingPlanService.updatePlan(id, plan); response.put("code", 200); response.put("message", "更新成功"); response.put("data", updatedPlan); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // 更新训练进度 @PutMapping("/{id}/progress") public Map updateProgress(@PathVariable Long id, @RequestBody Map data) { Map response = new HashMap<>(); try { Integer completedDays = data.get("completedDays"); TrainingPlan updatedPlan = trainingPlanService.updateProgress(id, completedDays); response.put("code", 200); response.put("message", "进度更新成功"); response.put("data", updatedPlan); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // 标记为完成 @PostMapping("/{id}/complete") public Map markAsCompleted(@PathVariable Long id) { Map response = new HashMap<>(); try { TrainingPlan completedPlan = trainingPlanService.markAsCompleted(id); response.put("code", 200); response.put("message", "训练计划已完成"); response.put("data", completedPlan); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // 暂停训练计划 @PostMapping("/{id}/pause") public Map pausePlan(@PathVariable Long id) { Map response = new HashMap<>(); try { TrainingPlan pausedPlan = trainingPlanService.pausePlan(id); response.put("code", 200); response.put("message", "训练计划已暂停"); response.put("data", pausedPlan); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // 恢复训练计划 @PostMapping("/{id}/resume") public Map resumePlan(@PathVariable Long id) { Map response = new HashMap<>(); try { TrainingPlan resumedPlan = trainingPlanService.resumePlan(id); response.put("code", 200); response.put("message", "训练计划已恢复"); response.put("data", resumedPlan); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // AI训练推荐 @GetMapping("/recommend/{userId}") public Map recommendTraining(@PathVariable Long userId) { Map response = new HashMap<>(); try { Optional userOpt = userService.getUserById(userId); if (userOpt.isPresent()) { String recommendation = trainingPlanService.recommendTraining(userOpt.get()); response.put("code", 200); response.put("message", "推荐成功"); response.put("data", recommendation); } else { response.put("code", 404); response.put("message", "用户不存在"); } } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } // 获取统计数据 @GetMapping("/member/{memberId}/stats") public Map getStats(@PathVariable Long memberId) { Map response = new HashMap<>(); try { Map stats = trainingPlanService.getStats(memberId); response.put("code", 200); response.put("message", "获取成功"); response.put("data", stats); } catch (Exception e) { response.put("code", 400); response.put("message", e.getMessage()); } return response; } }