You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
8.3 KiB
223 lines
8.3 KiB
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<String, Object> createPlan(@RequestBody TrainingPlan plan,
|
|
@RequestParam Long memberId,
|
|
@RequestParam Long coachId) {
|
|
Map<String, Object> 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<String, Object> getAllPlans() {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<TrainingPlan> 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<String, Object> getPlanById(@PathVariable Long id) {
|
|
Map<String, Object> 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<String, Object> getMemberPlans(@PathVariable Long memberId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<TrainingPlan> plans = trainingPlanService.getMemberPlans(memberId);
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", plans);
|
|
return response;
|
|
}
|
|
|
|
// 获取教练制定的训练计划
|
|
@GetMapping("/coach/{coachId}")
|
|
public Map<String, Object> getCoachPlans(@PathVariable Long coachId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<TrainingPlan> plans = trainingPlanService.getCoachPlans(coachId);
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", plans);
|
|
return response;
|
|
}
|
|
|
|
// 获取活跃的训练计划
|
|
@GetMapping("/member/{memberId}/active")
|
|
public Map<String, Object> getActivePlans(@PathVariable Long memberId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<TrainingPlan> plans = trainingPlanService.getActivePlansByMember(memberId);
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", plans);
|
|
return response;
|
|
}
|
|
|
|
// 更新训练计划
|
|
@PutMapping("/{id}")
|
|
public Map<String, Object> updatePlan(@PathVariable Long id, @RequestBody TrainingPlan plan) {
|
|
Map<String, Object> 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<String, Object> updateProgress(@PathVariable Long id, @RequestBody Map<String, Integer> data) {
|
|
Map<String, Object> 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<String, Object> markAsCompleted(@PathVariable Long id) {
|
|
Map<String, Object> 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<String, Object> pausePlan(@PathVariable Long id) {
|
|
Map<String, Object> 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<String, Object> resumePlan(@PathVariable Long id) {
|
|
Map<String, Object> 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<String, Object> recommendTraining(@PathVariable Long userId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Optional<User> 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<String, Object> getStats(@PathVariable Long memberId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Map<String, Object> 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;
|
|
}
|
|
} |