From a45dd146aeaa5de72c75975ef04911cbd65c661a Mon Sep 17 00:00:00 2001 From: XSHA AI Date: Fri, 31 Oct 2025 09:01:29 +0000 Subject: [PATCH] AI generated changes for conversation 42 --- .../course/controller/PageController.java | 154 ++++++++ .../resources/templates/course-publish.html | 366 ++++++++++++++++++ src/main/resources/templates/index.html | 1 + 3 files changed, 521 insertions(+) create mode 100644 src/main/resources/templates/course-publish.html diff --git a/src/main/java/com/chinaweal/youfool/course/controller/PageController.java b/src/main/java/com/chinaweal/youfool/course/controller/PageController.java index 7ebc2f3..9212a21 100644 --- a/src/main/java/com/chinaweal/youfool/course/controller/PageController.java +++ b/src/main/java/com/chinaweal/youfool/course/controller/PageController.java @@ -19,9 +19,17 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import com.chinaweal.youfool.framework.springboot.rest.RestResult; import com.chinaweal.youfool.framework.springboot.rest.BaseResultCode; +import com.baomidou.dynamic.datasource.annotation.DSTransactional; +import java.io.File; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.nio.file.Paths; +import java.nio.file.Path; +import java.util.UUID; /** * 页面控制器 @@ -223,6 +231,152 @@ public class PageController { } } + /** + * 发布课程页面 + * + * @return 发布课程页面 + */ + @GetMapping("/publish") + public String publishCoursePage(Model model) { + if (!StpUtil.isLogin()) { + return "redirect:/course/login"; + } + return "course-publish"; + } + + /** + * 发布课程 + * + * @param courseName 课程名称 + * @param courseDesc 课程描述 + * @param videoFile 课程视频文件(可选) + * @param attachment 课程附件文件(可选) + * @param model 模型对象 + * @return 发布结果 + */ + @PostMapping("/publish") + @DSTransactional + @ResponseBody + public RestResult publishCourse( + @RequestParam String courseName, + @RequestParam String courseDesc, + @RequestParam(required = false) MultipartFile videoFile, + @RequestParam(required = false) MultipartFile attachment, + Model model) { + + if (!StpUtil.isLogin()) { + return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "请先登录"); + } + + if (courseName == null || courseName.trim().isEmpty()) { + return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "课程标题不能为空"); + } + + if (courseDesc == null || courseDesc.trim().isEmpty()) { + return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "课程描述不能为空"); + } + + try { + // 创建课程 + Course course = new Course(); + course.setCourseName(courseName.trim()); + course.setCourseDesc(courseDesc.trim()); + course.setCourseDate(LocalDate.now()); + course.setCreateBy(StpUtil.getLoginIdAsString()); + course.setCreateTime(LocalDateTime.now()); + + boolean courseSaved = courseService.save(course); + if (!courseSaved) { + return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "创建课程失败"); + } + + String courseId = course.getId(); + + // 处理视频文件 + if (videoFile != null && !videoFile.isEmpty()) { + String videoAttachmentId = saveUploadFile(videoFile, courseId, "video"); + if (videoAttachmentId != null) { + CourseVideo courseVideo = new CourseVideo(); + courseVideo.setCourseId(courseId); + courseVideo.setSortIdx(1); + courseVideo.setVideoName(videoFile.getOriginalFilename()); + courseVideo.setAttachmentId(videoAttachmentId); + courseVideo.setCreateBy(StpUtil.getLoginIdAsString()); + courseVideo.setCreateTime(LocalDateTime.now()); + + courseVideoService.save(courseVideo); + } + } + + // 处理附件文件 + if (attachment != null && !attachment.isEmpty()) { + saveUploadFile(attachment, courseId, "attachment"); + } + + return RestResult.ok(courseId, "课程发布成功"); + + } catch (Exception e) { + e.printStackTrace(); + return RestResult.error(BaseResultCode.BUSINESS_LOGIC_ERROR, "课程发布失败:" + e.getMessage()); + } + } + + /** + * 保存上传的文件 + * + * @param file 上传的文件 + * @param courseId 课程ID + * @param type 文件类型(video或attachment) + * @return 附件ID + */ + private String saveUploadFile(MultipartFile file, String courseId, String type) { + try { + if (file.isEmpty()) { + return null; + } + + String originalFilename = file.getOriginalFilename(); + if (originalFilename == null) { + return null; + } + + String fileFormat = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase(); + String newFileName = UUID.randomUUID().toString() + "." + fileFormat; + + // 创建上传目录 + String uploadDir = "../uploads/courses/" + LocalDate.now().getYear() + "/" + + String.format("%02d", LocalDate.now().getMonthValue()) + "/" + + String.format("%02d", LocalDate.now().getDayOfMonth()); + Path uploadPath = Paths.get(uploadDir); + if (!uploadPath.toFile().exists()) { + uploadPath.toFile().mkdirs(); + } + + String absolutePath = uploadDir + "/" + newFileName; + + // 保存文件 + File dest = new File(absolutePath); + file.transferTo(dest); + + // 创建附件记录 + CourseAttachment attachment = new CourseAttachment(); + attachment.setCourseId(courseId); + attachment.setFileName(originalFilename); + attachment.setFileFormat(fileFormat); + attachment.setAbsoluteName(absolutePath); + attachment.setFileSize(file.getSize()); + attachment.setCreateBy(StpUtil.getLoginIdAsString()); + attachment.setCreateTime(LocalDateTime.now()); + + boolean saved = courseAttachmentService.save(attachment); + return saved ? attachment.getId() : null; + + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + /** * 流式播放课程视频 * diff --git a/src/main/resources/templates/course-publish.html b/src/main/resources/templates/course-publish.html new file mode 100644 index 0000000..5c31914 --- /dev/null +++ b/src/main/resources/templates/course-publish.html @@ -0,0 +1,366 @@ + + + + + + 发布课程 - 课程管理系统 + + + +
+

课程管理系统

+ +
+ +
+
+

发布新课程

+ +
+ +
+
+ + +
+ +
+ + +
+ +
+ +
+ +
+ 请选择视频文件,文件大小不超过100MB +
+
+
+ +
+ +
+ +
+ 请选择课程附件,文件大小不超过100MB +
+
+
+ +
+ +
+ +
+ 正在发布课程,请稍候... +
+
+
+
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 0818adb..0e46a38 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -211,6 +211,7 @@

课程管理系统