|
|
@@ -2,21 +2,20 @@ package com.fs.course.controller;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
+import cn.hutool.core.util.IdUtil;
|
|
|
+import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.config.cloud.CloudHostProper;
|
|
|
import com.fs.course.param.FsUserCourseCommentParam;
|
|
|
import com.fs.course.param.FsCourseWatchCommentPageParam;
|
|
|
import com.fs.course.vo.FsUserCourseCommentListVO;
|
|
|
import com.fs.course.vo.FsUserCourseCommentVO;
|
|
|
import com.fs.course.vo.FsCourseWatchCommentListVO;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.PutMapping;
|
|
|
-import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
import com.fs.common.annotation.Log;
|
|
|
import com.fs.common.core.controller.BaseController;
|
|
|
import com.fs.common.core.domain.AjaxResult;
|
|
|
@@ -27,6 +26,12 @@ import com.fs.course.service.IFsCourseWatchCommentService;
|
|
|
import com.fs.common.utils.poi.ExcelUtil;
|
|
|
import com.fs.common.core.page.TableDataInfo;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
/**
|
|
|
* 课堂评论Controller
|
|
|
*
|
|
|
@@ -150,4 +155,154 @@ public class FsUserCourseCommentController extends BaseController
|
|
|
{
|
|
|
return toAjax(fsUserCourseCommentService.deleteFsUserCourseCommentByCommentIds(commentIds));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 下载精选留言Excel导入模板
|
|
|
+ * 模板字段:昵称、评论内容、图片URL、视频URL
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('course:userCourseComment:add')")
|
|
|
+ @GetMapping("/importTemplate")
|
|
|
+ public void importTemplate(HttpServletResponse response) throws IOException {
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ String fileName = URLEncoder.encode("精选留言导入模板", "UTF-8");
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
|
+
|
|
|
+ try (Workbook workbook = new XSSFWorkbook(); OutputStream out = response.getOutputStream()) {
|
|
|
+ Sheet sheet = workbook.createSheet("精选留言");
|
|
|
+
|
|
|
+ // 表头样式
|
|
|
+ CellStyle headerStyle = workbook.createCellStyle();
|
|
|
+ Font headerFont = workbook.createFont();
|
|
|
+ headerFont.setBold(true);
|
|
|
+ headerStyle.setFont(headerFont);
|
|
|
+
|
|
|
+ // 表头行
|
|
|
+ Row headerRow = sheet.createRow(0);
|
|
|
+ String[] headers = {"昵称", "评论内容", "图片URL", "视频URL"};
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
+ Cell cell = headerRow.createCell(i);
|
|
|
+ cell.setCellValue(headers[i]);
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 示例行
|
|
|
+ Row exampleRow = sheet.createRow(1);
|
|
|
+ String[] examples = {"张三", "课程内容非常棒!", "", ""};
|
|
|
+ for (int i = 0; i < examples.length; i++) {
|
|
|
+ exampleRow.createCell(i).setCellValue(examples[i]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 自适应列宽
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ workbook.write(out);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导入精选留言(Excel解析)
|
|
|
+ * 解析Excel表格数据,按 type=3 保存为精选留言
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('course:userCourseComment:add')")
|
|
|
+ @Log(title = "精选留言导入", businessType = BusinessType.IMPORT)
|
|
|
+ @PostMapping("/importComments/{courseId}")
|
|
|
+ public AjaxResult importComments(@PathVariable Long courseId, @RequestParam("file") MultipartFile file) throws IOException {
|
|
|
+ if (file.isEmpty()) {
|
|
|
+ return AjaxResult.error("请选择要导入的文件");
|
|
|
+ }
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ if (originalFilename == null || (!originalFilename.endsWith(".xlsx") && !originalFilename.endsWith(".xls"))) {
|
|
|
+ return AjaxResult.error("仅支持Excel文件(.xls/.xlsx)格式");
|
|
|
+ }
|
|
|
+
|
|
|
+ try (Workbook workbook = WorkbookFactory.create(file.getInputStream())) {
|
|
|
+ Sheet sheet = workbook.getSheetAt(0);
|
|
|
+ if (sheet == null) {
|
|
|
+ return AjaxResult.error("Excel文件中未找到数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ int count = 0;
|
|
|
+ // 从第2行开始读取(第1行是表头)
|
|
|
+ for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
+ Row row = sheet.getRow(i);
|
|
|
+ if (row == null) continue;
|
|
|
+
|
|
|
+ String nickName = getCellStringValue(row, 0);
|
|
|
+ String content = getCellStringValue(row, 1);
|
|
|
+ String imgUrl = getCellStringValue(row, 2);
|
|
|
+ String videoUrl = getCellStringValue(row, 3);
|
|
|
+
|
|
|
+ // 昵称和内容至少有一个非空才导入
|
|
|
+ if ((nickName == null || nickName.trim().isEmpty()) && (content == null || content.trim().isEmpty())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拼接内容:文字 + 图片URL + 视频URL
|
|
|
+ StringBuilder contentBuilder = new StringBuilder();
|
|
|
+ if (content != null && !content.trim().isEmpty()) {
|
|
|
+ contentBuilder.append(content.trim());
|
|
|
+ }
|
|
|
+ if (imgUrl != null && !imgUrl.trim().isEmpty()) {
|
|
|
+ contentBuilder.append("\n").append(imgUrl.trim());
|
|
|
+ }
|
|
|
+ if (videoUrl != null && !videoUrl.trim().isEmpty()) {
|
|
|
+ contentBuilder.append("\n").append(videoUrl.trim());
|
|
|
+ }
|
|
|
+
|
|
|
+ FsUserCourseComment comment = new FsUserCourseComment();
|
|
|
+ comment.setCourseId(courseId);
|
|
|
+ comment.setType(3L); // 精选留言
|
|
|
+ comment.setParentId(0L);
|
|
|
+ comment.setNickName(nickName != null ? nickName.trim() : "");
|
|
|
+ comment.setContent(contentBuilder.toString());
|
|
|
+ comment.setReplyCount(0L);
|
|
|
+ comment.setLikes(0L);
|
|
|
+ comment.setIsDel(0);
|
|
|
+ comment.setCreateTime(new Date());
|
|
|
+ fsUserCourseCommentService.insertFsUserCourseComment(comment);
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ return AjaxResult.success("成功导入" + count + "条精选留言");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询精选留言历史列表(type=3)
|
|
|
+ */
|
|
|
+ @PreAuthorize("@ss.hasPermi('course:userCourseComment:list')")
|
|
|
+ @GetMapping("/featuredComments/{courseId}")
|
|
|
+ public TableDataInfo featuredComments(@PathVariable Long courseId) {
|
|
|
+ startPage();
|
|
|
+ FsUserCourseComment param = new FsUserCourseComment();
|
|
|
+ param.setCourseId(courseId);
|
|
|
+ param.setType(3L);
|
|
|
+ List<FsUserCourseComment> list = fsUserCourseCommentService.selectFsUserCourseCommentList(param);
|
|
|
+ return getDataTable(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提取Excel单元格文本
|
|
|
+ */
|
|
|
+ private String getCellStringValue(Row row, int colIndex) {
|
|
|
+ if (colIndex >= row.getLastCellNum()) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ Cell cell = row.getCell(colIndex);
|
|
|
+ if (cell == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ switch (cell.getCellType()) {
|
|
|
+ case STRING:
|
|
|
+ return cell.getStringCellValue();
|
|
|
+ case NUMERIC:
|
|
|
+ return String.valueOf((long) cell.getNumericCellValue());
|
|
|
+ case BOOLEAN:
|
|
|
+ return String.valueOf(cell.getBooleanCellValue());
|
|
|
+ default:
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|