Compare commits
1 Commits
master
...
feature/co
| Author | SHA1 | Date | |
|---|---|---|---|
| 290630cdba |
@@ -1,193 +1,234 @@
|
||||
package icu.sunway.ai_spring_example.Controllers;
|
||||
|
||||
import com.baomidou.dynamic.datasource.annotation.DS;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import icu.sunway.ai_spring_example.Common.ResponseEntity;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import icu.sunway.ai_spring_example.Common.ResponseEntity;
|
||||
|
||||
/**
|
||||
* 课程控制器
|
||||
* 提供课程展示和报名相关的功能
|
||||
*/
|
||||
@RestController
|
||||
@DS("master") // 指定sys_user库
|
||||
public class Course {
|
||||
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
/**
|
||||
* 获取课程列表
|
||||
*
|
||||
* @param params 查询参数,包含page、size、name、category、status等字段
|
||||
* @return 课程列表数据
|
||||
* 获取课程列表(仅名称模糊查询+分页)
|
||||
*/
|
||||
@GetMapping("/courses")
|
||||
public ResponseEntity<?> getCourseList(@RequestParam Map<String, Object> params) {
|
||||
// 从Map中获取查询参数
|
||||
try {
|
||||
Integer page = Integer.valueOf((String) params.getOrDefault("page", "1"));
|
||||
Integer size = Integer.valueOf((String) params.getOrDefault("size", "10"));
|
||||
String name = (String) params.get("name");
|
||||
String category = (String) params.get("category");
|
||||
Integer status = params.get("status") != null ? Integer.valueOf((String) params.get("status")) : null;
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据条件查询课程列表
|
||||
// 2. 处理分页
|
||||
// 构建SQL
|
||||
StringBuilder sql = new StringBuilder("SELECT id, course_name AS name, intro AS description, create_time FROM course WHERE 1=1");
|
||||
StringBuilder countSql = new StringBuilder("SELECT COUNT(*) FROM course WHERE 1=1");
|
||||
List<Object> sqlParams = new ArrayList<>();
|
||||
|
||||
// 模拟返回课程列表
|
||||
List<Map<String, Object>> courseList = List.of(
|
||||
Map.of("id", 1, "name", "Java基础课程", "category", "编程", "teacher", "张老师", "price", 99.00, "students", 150,
|
||||
"status", 1, "description", "Java入门到精通课程"),
|
||||
Map.of("id", 2, "name", "Python数据分析", "category", "数据科学", "teacher", "李老师", "price", 199.00, "students",
|
||||
80, "status", 1, "description", "Python数据分析实战课程"));
|
||||
// 仅名称模糊查询
|
||||
if (name != null && !name.isEmpty()) {
|
||||
sql.append(" AND course_name LIKE ?");
|
||||
countSql.append(" AND course_name LIKE ?");
|
||||
sqlParams.add("%" + name + "%");
|
||||
}
|
||||
|
||||
// 分页
|
||||
int offset = (page - 1) * size;
|
||||
sql.append(" LIMIT " + offset + ", " + size);
|
||||
|
||||
// 执行查询
|
||||
List<Map<String, Object>> courseList = jdbcTemplate.queryForList(sql.toString(), sqlParams.toArray());
|
||||
Long total = jdbcTemplate.queryForObject(countSql.toString(), sqlParams.toArray(), Long.class);
|
||||
|
||||
// 组装返回
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("list", courseList);
|
||||
responseData.put("total", 2);
|
||||
responseData.put("total", total);
|
||||
responseData.put("page", page);
|
||||
responseData.put("size", size);
|
||||
|
||||
return ResponseEntity.success("获取课程列表成功", responseData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.error(500, "获取课程列表失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取课程详情
|
||||
*
|
||||
* @param courseId 课程ID
|
||||
* @return 课程详细信息
|
||||
* 获取课程详情(仅关联章节核心字段)
|
||||
*/
|
||||
@GetMapping("/courses/{courseId}")
|
||||
public ResponseEntity<?> getCourseDetail(@PathVariable Long courseId) {
|
||||
// 参数验证
|
||||
if (courseId == null) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空");
|
||||
try {
|
||||
if (courseId == null || courseId <= 0) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空且必须为正整数");
|
||||
}
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据课程ID查询课程信息
|
||||
// 2. 查询课程章节、评价等关联信息
|
||||
// 查询课程基本信息
|
||||
String courseSql = "SELECT id, course_name AS name, intro AS description, create_time FROM course WHERE id = ?";
|
||||
List<Map<String, Object>> courseList = jdbcTemplate.queryForList(courseSql, courseId);
|
||||
if (courseList.isEmpty()) {
|
||||
return ResponseEntity.error(404, "课程不存在");
|
||||
}
|
||||
Map<String, Object> courseInfo = courseList.get(0);
|
||||
|
||||
// 模拟返回课程详情
|
||||
Map<String, Object> courseInfo = Map.of(
|
||||
"id", courseId, "name", "Java基础课程", "category", "编程", "teacher", "张老师", "price", 99.00,
|
||||
"students", 150, "status", 1, "description", "Java入门到精通课程",
|
||||
"chapters", List.of(
|
||||
Map.of("id", 1, "name", "Java简介", "duration", "30分钟"),
|
||||
Map.of("id", 2, "name", "变量与数据类型", "duration", "45分钟")),
|
||||
"ratings", Map.of("avg", 4.8, "count", 50));
|
||||
// 查询章节
|
||||
String chapterSql = """
|
||||
SELECT c.id, c.course_id, c.vedio_url, c.create_time
|
||||
FROM chapter c
|
||||
LEFT JOIN course co ON c.course_id = co.id
|
||||
WHERE c.course_id = ?
|
||||
ORDER BY c.id ASC
|
||||
""";
|
||||
List<Map<String, Object>> chapters = jdbcTemplate.queryForList(chapterSql, courseId);
|
||||
courseInfo.put("chapters", chapters);
|
||||
|
||||
return ResponseEntity.success("获取课程详情成功", courseInfo);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.error(500, "获取课程详情失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 课程报名
|
||||
*
|
||||
* @param enrollMap 报名信息,包含courseId、userId等字段
|
||||
* @return 报名结果
|
||||
*/
|
||||
@PostMapping("/courses/enroll")
|
||||
public ResponseEntity<?> enrollCourse(@RequestBody Map<String, Object> enrollMap) {
|
||||
// 从Map中获取报名信息
|
||||
try {
|
||||
Long courseId = (Long) enrollMap.get("courseId");
|
||||
Long userId = (Long) enrollMap.get("userId");
|
||||
|
||||
// 参数验证
|
||||
if (courseId == null) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空");
|
||||
if (courseId == null || courseId <= 0) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空且必须为正整数");
|
||||
}
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
if (userId == null || userId <= 0) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空且必须为正整数");
|
||||
}
|
||||
|
||||
// TODO: 实现报名逻辑
|
||||
// 1. 检查课程是否存在且可报名
|
||||
// 2. 检查用户是否已报名该课程
|
||||
// 3. 记录报名信息
|
||||
// 4. 更新课程报名人数
|
||||
// 检查课程是否存在
|
||||
String checkCourseSql = "SELECT id FROM course WHERE id = ?";
|
||||
if (jdbcTemplate.queryForList(checkCourseSql, courseId).isEmpty()) {
|
||||
return ResponseEntity.error(404, "课程不存在");
|
||||
}
|
||||
|
||||
// 模拟报名成功
|
||||
// 检查重复报名
|
||||
String checkEnrollSql = "SELECT id FROM course_user WHERE user_id = ? AND course_id = ?";
|
||||
if (!jdbcTemplate.queryForList(checkEnrollSql, userId, courseId).isEmpty()) {
|
||||
return ResponseEntity.error(400, "已报名该课程,无需重复报名");
|
||||
}
|
||||
|
||||
// 写入报名记录
|
||||
String insertSql = "INSERT INTO course_user (user_id, course_id, create_time) VALUES (?, ?, ?)";
|
||||
jdbcTemplate.update(insertSql, userId, courseId, new Timestamp(System.currentTimeMillis()));
|
||||
|
||||
// 组装返回
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("courseId", courseId);
|
||||
responseData.put("userId", userId);
|
||||
responseData.put("enrollTime", "2023-05-20 10:00:00");
|
||||
responseData.put("enrollTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
|
||||
|
||||
return ResponseEntity.success("课程报名成功", responseData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.error(500, "课程报名失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户报名的课程列表
|
||||
*
|
||||
* @param params 查询参数,包含userId、page、size等字段
|
||||
* @return 用户报名的课程列表
|
||||
*/
|
||||
@GetMapping("/user/courses")
|
||||
public ResponseEntity<?> getUserCourses(@RequestParam Map<String, Object> params) {
|
||||
// 从Map中获取查询参数
|
||||
try {
|
||||
Long userId = Long.valueOf((String) params.get("userId"));
|
||||
Integer page = Integer.valueOf((String) params.getOrDefault("page", "1"));
|
||||
Integer size = Integer.valueOf((String) params.getOrDefault("size", "10"));
|
||||
|
||||
// 参数验证
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
if (userId == null || userId <= 0) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空且必须为正整数");
|
||||
}
|
||||
|
||||
// TODO: 实现查询逻辑
|
||||
// 1. 根据用户ID查询已报名课程
|
||||
// 2. 处理分页
|
||||
// 关联查询
|
||||
int offset = (page - 1) * size;
|
||||
String sql = """
|
||||
SELECT cu.id, cu.course_id AS courseId, co.course_name AS courseName, cu.create_time AS enrollTime
|
||||
FROM course_user cu
|
||||
LEFT JOIN course co ON cu.course_id = co.id
|
||||
WHERE cu.user_id = ?
|
||||
LIMIT """ + offset + ", " + size;
|
||||
|
||||
// 模拟返回用户报名的课程列表
|
||||
List<Map<String, Object>> userCourseList = List.of(
|
||||
Map.of("id", 1, "courseId", 1, "courseName", "Java基础课程", "teacher", "张老师", "enrollTime",
|
||||
"2023-05-20 10:00:00", "status", "learning"),
|
||||
Map.of("id", 2, "courseId", 2, "courseName", "Python数据分析", "teacher", "李老师", "enrollTime",
|
||||
"2023-05-15 14:30:00", "status", "completed"));
|
||||
List<Map<String, Object>> userCourseList = jdbcTemplate.queryForList(sql, userId);
|
||||
// 仅保留必要字段,删除status补充
|
||||
userCourseList.forEach(item -> item.put("status", "learning")); // 可选:保留状态字段,仅做模拟
|
||||
|
||||
// 查询总数
|
||||
String countSql = "SELECT COUNT(*) FROM course_user WHERE user_id = ?";
|
||||
Long total = jdbcTemplate.queryForObject(countSql, new Object[]{userId}, Long.class);
|
||||
|
||||
// 组装返回
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("list", userCourseList);
|
||||
responseData.put("total", 2);
|
||||
responseData.put("total", total);
|
||||
responseData.put("page", page);
|
||||
responseData.put("size", size);
|
||||
|
||||
return ResponseEntity.success("获取用户课程列表成功", responseData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.error(500, "获取用户课程列表失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消课程报名
|
||||
*
|
||||
* @param courseId 课程ID
|
||||
* @param params 参数,包含userId字段
|
||||
* @return 取消报名结果
|
||||
*/
|
||||
@DeleteMapping("/courses/{courseId}/enroll")
|
||||
public ResponseEntity<?> cancelEnroll(@PathVariable Long courseId, @RequestParam Map<String, Object> params) {
|
||||
// 从Map中获取用户ID
|
||||
try {
|
||||
Long userId = Long.valueOf((String) params.get("userId"));
|
||||
|
||||
// 参数验证
|
||||
if (courseId == null) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空");
|
||||
if (courseId == null || courseId <= 0) {
|
||||
return ResponseEntity.error(400, "课程ID不能为空且必须为正整数");
|
||||
}
|
||||
if (userId == null) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空");
|
||||
if (userId == null || userId <= 0) {
|
||||
return ResponseEntity.error(400, "用户ID不能为空且必须为正整数");
|
||||
}
|
||||
|
||||
// TODO: 实现取消报名逻辑
|
||||
// 1. 检查报名记录是否存在
|
||||
// 2. 删除报名记录
|
||||
// 3. 更新课程报名人数
|
||||
// 检查报名记录
|
||||
String checkSql = "SELECT id FROM course_user WHERE user_id = ? AND course_id = ?";
|
||||
if (jdbcTemplate.queryForList(checkSql, userId, courseId).isEmpty()) {
|
||||
return ResponseEntity.error(404, "未报名该课程,无法取消");
|
||||
}
|
||||
|
||||
// 模拟取消报名成功
|
||||
// 删除记录
|
||||
String deleteSql = "DELETE FROM course_user WHERE user_id = ? AND course_id = ?";
|
||||
jdbcTemplate.update(deleteSql, userId, courseId);
|
||||
|
||||
// 组装返回
|
||||
Map<String, Object> responseData = new HashMap<>();
|
||||
responseData.put("courseId", courseId);
|
||||
responseData.put("userId", userId);
|
||||
|
||||
return ResponseEntity.success("取消课程报名成功", responseData);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.error(500, "取消课程报名失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user