feat(exam): 实现按学生批量生成并下载试题功能
- 增加学生多选功能和生成试题按钮,支持批量操作 - 新增ExamGenerateDialog组件,提供选择年级和难度界面 - 设计后端接口支持多个学生ID,生成对应的试题文档 - 在后端实现批量生成Word文档并压缩打包下载 - 新增StudentDetail业务对象,完善学生信息展示 - 优化了Mapper接口及XML,支持批量查询学生和班级数据 - 提供前端API封装用于调用试题生成和下载服务 - 实现下载失败时的错误处理与提示机制
This commit is contained in:
@@ -31,7 +31,7 @@ public class ClassController {
|
|||||||
int total = classService.getClassCount();
|
int total = classService.getClassCount();
|
||||||
|
|
||||||
List<ClassDO> classList = classService.findClassList(page, pageSize);
|
List<ClassDO> classList = classService.findClassList(page, pageSize);
|
||||||
Map<Integer, GradeDO> classId2GradeMap = classService.findClassId2GradeMap(classList.stream().map(ClassDO::getId).toList());
|
Map<Integer, GradeDO> classId2GradeMap = classService.findClassIds2GradeMap(classList.stream().map(ClassDO::getId).toList());
|
||||||
List<FindClassRspVO> findClassRspVOS = classList.stream().map(classDO -> FindClassRspVO.builder()
|
List<FindClassRspVO> findClassRspVOS = classList.stream().map(classDO -> FindClassRspVO.builder()
|
||||||
.id(classDO.getId())
|
.id(classDO.getId())
|
||||||
.title(classDO.getTitle())
|
.title(classDO.getTitle())
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
package com.yinlihupo.enlish.service.controller;
|
package com.yinlihupo.enlish.service.controller;
|
||||||
|
|
||||||
|
|
||||||
import com.deepoove.poi.XWPFTemplate;
|
|
||||||
import com.deepoove.poi.config.Configure;
|
|
||||||
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
|
|
||||||
import com.yinlihupo.enlish.service.domain.dataobject.ExamWordsDO;
|
import com.yinlihupo.enlish.service.domain.dataobject.ExamWordsDO;
|
||||||
import com.yinlihupo.enlish.service.domain.dataobject.ExamWordsJudgeResultDO;
|
import com.yinlihupo.enlish.service.domain.dataobject.ExamWordsJudgeResultDO;
|
||||||
import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO;
|
import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO;
|
||||||
|
import com.yinlihupo.enlish.service.model.bo.StudentDetail;
|
||||||
import com.yinlihupo.enlish.service.model.bo.Word;
|
import com.yinlihupo.enlish.service.model.bo.Word;
|
||||||
import com.yinlihupo.enlish.service.model.vo.exam.*;
|
import com.yinlihupo.enlish.service.model.vo.exam.*;
|
||||||
import com.yinlihupo.enlish.service.service.ExamWordsJudgeService;
|
import com.yinlihupo.enlish.service.service.ExamWordsJudgeService;
|
||||||
import com.yinlihupo.enlish.service.service.ExamWordsService;
|
import com.yinlihupo.enlish.service.service.ExamWordsService;
|
||||||
|
import com.yinlihupo.enlish.service.service.StudentService;
|
||||||
import com.yinlihupo.enlish.service.service.VocabularyService;
|
import com.yinlihupo.enlish.service.service.VocabularyService;
|
||||||
|
import com.yinlihupo.enlish.service.utils.WordExportUtil;
|
||||||
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
|
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
|
||||||
import com.yinlihupo.framework.common.response.PageResponse;
|
import com.yinlihupo.framework.common.response.PageResponse;
|
||||||
import com.yinlihupo.framework.common.response.Response;
|
import com.yinlihupo.framework.common.response.Response;
|
||||||
@@ -22,14 +22,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.util.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.URLEncoder;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@RequestMapping("/exam/words/")
|
@RequestMapping("/exam/words/")
|
||||||
@RestController
|
@RestController
|
||||||
@@ -44,8 +37,10 @@ public class ExamWordsController {
|
|||||||
private String templateWordPath;
|
private String templateWordPath;
|
||||||
@Resource
|
@Resource
|
||||||
private ExamWordsJudgeService examWordsJudgeService;
|
private ExamWordsJudgeService examWordsJudgeService;
|
||||||
|
@Resource
|
||||||
|
private StudentService studentService;
|
||||||
|
|
||||||
@PostMapping("genexam")
|
@PostMapping("generate")
|
||||||
public void generateFeltExamWords(@RequestBody GenerateExamWordsReqVO generateExamWordsReqVO, HttpServletResponse response) {
|
public void generateFeltExamWords(@RequestBody GenerateExamWordsReqVO generateExamWordsReqVO, HttpServletResponse response) {
|
||||||
Integer gradeId = generateExamWordsReqVO.getGradeId();
|
Integer gradeId = generateExamWordsReqVO.getGradeId();
|
||||||
Integer level = generateExamWordsReqVO.getLevel();
|
Integer level = generateExamWordsReqVO.getLevel();
|
||||||
@@ -56,43 +51,25 @@ public class ExamWordsController {
|
|||||||
try {
|
try {
|
||||||
ExamWordsDO examWordsDO = examWordsService.generateExamWords(gradeId, level, studentIds);
|
ExamWordsDO examWordsDO = examWordsService.generateExamWords(gradeId, level, studentIds);
|
||||||
List<VocabularyBankDO> vocabularyBankDOS = vocabularyService.findVocabularyBankDOListById(examWordsDO.getWordIds());
|
List<VocabularyBankDO> vocabularyBankDOS = vocabularyService.findVocabularyBankDOListById(examWordsDO.getWordIds());
|
||||||
|
|
||||||
List<Word> assessmentWords = vocabularyBankDOS.stream().map(vocabularyBankDO -> Word.builder()
|
List<Word> assessmentWords = vocabularyBankDOS.stream().map(vocabularyBankDO -> Word.builder()
|
||||||
.id(vocabularyBankDO.getId())
|
.id(vocabularyBankDO.getId())
|
||||||
.title(vocabularyBankDO.getWord())
|
.title(vocabularyBankDO.getWord())
|
||||||
.definition(vocabularyBankDO.getDefinition())
|
.definition(vocabularyBankDO.getDefinition())
|
||||||
.build()).toList();
|
.build()).toList();
|
||||||
|
|
||||||
// 配置 POI-TL
|
List<StudentDetail> studentDetailList = studentService.getStudentDetailList(studentIds);
|
||||||
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
|
List<Map<String, Object>> maps = studentDetailList.stream().map(studentDetail -> {
|
||||||
Configure config = Configure.builder()
|
Map<String, Object> data = new HashMap<>();
|
||||||
.bind("words", policy)
|
data.put("examId", examWordsDO.getId());
|
||||||
.bind("answer", policy)
|
data.put("studentId", studentDetail.getId());
|
||||||
.build();
|
data.put("studentStr", studentDetail.getName() + studentDetail.getClassName() + studentDetail.getGradeName());
|
||||||
|
data.put("examStr", examWordsDO.getTitle());
|
||||||
|
data.put("words", assessmentWords);
|
||||||
|
data.put("answer", assessmentWords);
|
||||||
|
return data;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
Map<String, Object> data = new HashMap<>();
|
WordExportUtil.generateExamWords(maps, response, templateWordPath);
|
||||||
data.put("examId", examWordsDO.getId());
|
|
||||||
data.put("studentId", studentIds.get(0));
|
|
||||||
data.put("studentStr","小明三班一年级");
|
|
||||||
data.put("examStr", examWordsDO.getTitle());
|
|
||||||
data.put("words", assessmentWords);
|
|
||||||
data.put("answer", assessmentWords);
|
|
||||||
|
|
||||||
// 2. 设置文件名编码
|
|
||||||
String fileName = URLEncoder.encode(examWordsDO.getId() + "摸底测试.docx", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
|
||||||
|
|
||||||
// 3. 设置响应头
|
|
||||||
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
|
||||||
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);
|
|
||||||
|
|
||||||
try (InputStream inputStream = new FileInputStream(this.templateWordPath);
|
|
||||||
XWPFTemplate template = XWPFTemplate.compile(inputStream, config);
|
|
||||||
OutputStream out = response.getOutputStream()) {
|
|
||||||
|
|
||||||
template.render(data);
|
|
||||||
template.write(out);
|
|
||||||
out.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
||||||
|
|||||||
@@ -12,4 +12,6 @@ public interface ClassDOMapper {
|
|||||||
List<ClassDO> selectClassDOList(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);
|
List<ClassDO> selectClassDOList(@Param("startIndex") Integer startIndex, @Param("pageSize") Integer pageSize);
|
||||||
|
|
||||||
Integer selectClassDOCount();
|
Integer selectClassDOCount();
|
||||||
|
|
||||||
|
List<ClassDO> selectClassDOListByIds(@Param("classIds") List<Integer> classIds);
|
||||||
}
|
}
|
||||||
@@ -12,4 +12,6 @@ public interface StudentDOMapper {
|
|||||||
int selectStudentCount();
|
int selectStudentCount();
|
||||||
|
|
||||||
StudentDO selectStudentById(Integer id);
|
StudentDO selectStudentById(Integer id);
|
||||||
|
|
||||||
|
List<StudentDO> selectStudentDOListByIds(@Param("ids") List<Integer> ids);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.yinlihupo.enlish.service.model.bo;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
public class StudentDetail {
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
private Integer classId;
|
||||||
|
private String className;
|
||||||
|
private Integer gradeId;
|
||||||
|
private String gradeName;
|
||||||
|
}
|
||||||
@@ -14,5 +14,5 @@ public interface ClassService {
|
|||||||
|
|
||||||
int getClassCount();
|
int getClassCount();
|
||||||
|
|
||||||
Map<Integer, GradeDO> findClassId2GradeMap(List<Integer> classIds);
|
Map<Integer, GradeDO> findClassIds2GradeMap(List<Integer> classIds);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.yinlihupo.enlish.service.service;
|
|||||||
|
|
||||||
|
|
||||||
import com.yinlihupo.enlish.service.domain.dataobject.StudentDO;
|
import com.yinlihupo.enlish.service.domain.dataobject.StudentDO;
|
||||||
|
import com.yinlihupo.enlish.service.model.bo.StudentDetail;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -12,4 +13,6 @@ public interface StudentService {
|
|||||||
int getAllStudents();
|
int getAllStudents();
|
||||||
|
|
||||||
StudentDO getStudentById(Integer studentId);
|
StudentDO getStudentById(Integer studentId);
|
||||||
|
|
||||||
|
List<StudentDetail> getStudentDetailList(List<Integer> studentIdList);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public class ClassServiceImpl implements ClassService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Map<Integer, GradeDO> findClassId2GradeMap(List<Integer> classIds) {
|
public Map<Integer, GradeDO> findClassIds2GradeMap(List<Integer> classIds) {
|
||||||
|
|
||||||
List<GradeClassDO> gradeClassDOS = gradeClassDOMapper.selectByClassIds(classIds);
|
List<GradeClassDO> gradeClassDOS = gradeClassDOMapper.selectByClassIds(classIds);
|
||||||
List<GradeDO> gradeDOS = gradeDOMapper.selectByGradeIds(gradeClassDOS.stream().map(GradeClassDO::getGradeId).toList());
|
List<GradeDO> gradeDOS = gradeDOMapper.selectByGradeIds(gradeClassDOS.stream().map(GradeClassDO::getGradeId).toList());
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public class ExamWordsServiceImpl implements ExamWordsService {
|
|||||||
ExamWordsDO examWordsDO = ExamWordsDO.builder()
|
ExamWordsDO examWordsDO = ExamWordsDO.builder()
|
||||||
.gradeId(gradeId)
|
.gradeId(gradeId)
|
||||||
.level(level)
|
.level(level)
|
||||||
.title("测试")
|
.title(LocalDateTime.now() + "测试")
|
||||||
.createdAt(LocalDateTime.now())
|
.createdAt(LocalDateTime.now())
|
||||||
.wordIds(vocabularyBankDOS.stream().map(VocabularyBankDO::getId).toList())
|
.wordIds(vocabularyBankDOS.stream().map(VocabularyBankDO::getId).toList())
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
@@ -1,19 +1,30 @@
|
|||||||
package com.yinlihupo.enlish.service.service.student;
|
package com.yinlihupo.enlish.service.service.student;
|
||||||
|
|
||||||
|
|
||||||
|
import com.yinlihupo.enlish.service.domain.dataobject.ClassDO;
|
||||||
|
import com.yinlihupo.enlish.service.domain.dataobject.GradeDO;
|
||||||
import com.yinlihupo.enlish.service.domain.dataobject.StudentDO;
|
import com.yinlihupo.enlish.service.domain.dataobject.StudentDO;
|
||||||
|
import com.yinlihupo.enlish.service.domain.mapper.ClassDOMapper;
|
||||||
|
import com.yinlihupo.enlish.service.domain.mapper.GradeDOMapper;
|
||||||
import com.yinlihupo.enlish.service.domain.mapper.StudentDOMapper;
|
import com.yinlihupo.enlish.service.domain.mapper.StudentDOMapper;
|
||||||
|
import com.yinlihupo.enlish.service.model.bo.StudentDetail;
|
||||||
import com.yinlihupo.enlish.service.service.StudentService;
|
import com.yinlihupo.enlish.service.service.StudentService;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class StudentServiceImpl implements StudentService {
|
public class StudentServiceImpl implements StudentService {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private StudentDOMapper studentDOMapper;
|
private StudentDOMapper studentDOMapper;
|
||||||
|
@Resource
|
||||||
|
private GradeDOMapper gradeDOMapper;
|
||||||
|
@Resource
|
||||||
|
private ClassDOMapper classDOMapper;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<StudentDO> getStudentsByClassIdAndGradeId(Integer classId, Integer gradeId, String name, Integer pageNo, Integer pageSize) {
|
public List<StudentDO> getStudentsByClassIdAndGradeId(Integer classId, Integer gradeId, String name, Integer pageNo, Integer pageSize) {
|
||||||
@@ -30,4 +41,29 @@ public class StudentServiceImpl implements StudentService {
|
|||||||
public StudentDO getStudentById(Integer studentId) {
|
public StudentDO getStudentById(Integer studentId) {
|
||||||
return studentDOMapper.selectStudentById(studentId);
|
return studentDOMapper.selectStudentById(studentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<StudentDetail> getStudentDetailList(List<Integer> studentIdList) {
|
||||||
|
|
||||||
|
List<StudentDO> studentDOS = studentDOMapper.selectStudentDOListByIds(studentIdList);
|
||||||
|
Map<Integer, GradeDO> gradeId2GradeDO = gradeDOMapper.selectByGradeIds(studentDOS.stream().map(StudentDO::getGradeId).toList()).stream().collect(Collectors.toMap(
|
||||||
|
GradeDO::getId,
|
||||||
|
gradeDO -> gradeDO
|
||||||
|
));
|
||||||
|
Map<Integer, ClassDO> classId2ClassDO = classDOMapper.selectClassDOListByIds(studentDOS.stream().map(StudentDO::getClassId).toList()).stream().collect(Collectors.toMap(
|
||||||
|
ClassDO::getId,
|
||||||
|
classDO -> classDO
|
||||||
|
));
|
||||||
|
|
||||||
|
return studentDOS.stream().map(studentDO -> StudentDetail.builder()
|
||||||
|
.id(studentDO.getId())
|
||||||
|
.name(studentDO.getName())
|
||||||
|
.classId(studentDO.getClassId())
|
||||||
|
.className(classId2ClassDO.get(studentDO.getClassId()) != null ? classId2ClassDO.get(studentDO.getClassId()).getTitle() : "")
|
||||||
|
.gradeId(studentDO.getGradeId())
|
||||||
|
.gradeName(gradeId2GradeDO.get(studentDO.getGradeId()) != null ? gradeId2GradeDO.get(studentDO.getGradeId()).getTitle() : "")
|
||||||
|
.build()
|
||||||
|
).toList();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
package com.yinlihupo.enlish.service.utils;
|
||||||
|
|
||||||
|
import com.deepoove.poi.XWPFTemplate;
|
||||||
|
import com.deepoove.poi.config.Configure;
|
||||||
|
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
public class WordExportUtil {
|
||||||
|
|
||||||
|
private static final Configure config;
|
||||||
|
|
||||||
|
static {
|
||||||
|
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
|
||||||
|
config = Configure.builder()
|
||||||
|
.bind("words", policy)
|
||||||
|
.bind("answer", policy)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 公共入口:根据数据量决定是导出单文件还是压缩包
|
||||||
|
*/
|
||||||
|
public static void generateExamWords(List<Map<String, Object>> data, HttpServletResponse response, String templateWordPath) {
|
||||||
|
if (data == null || data.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (data.size() == 1) {
|
||||||
|
// 如果只有一份数据,直接导出 docx,用户体验更好
|
||||||
|
generateExamWordsDocx(data.get(0), response, templateWordPath);
|
||||||
|
} else {
|
||||||
|
// 如果有多份数据,打包导出
|
||||||
|
generateExamWordsZip(data, response, templateWordPath);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
throw new RuntimeException("导出文档失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 核心补充:批量渲染并打包为 ZIP
|
||||||
|
*/
|
||||||
|
private static void generateExamWordsZip(List<Map<String, Object>> data, HttpServletResponse response, String templateWordPath) throws IOException {
|
||||||
|
// 1. 设置响应头为 ZIP
|
||||||
|
String zipName = URLEncoder.encode("批量导出_摸底测试.zip", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
|
||||||
|
response.setContentType("application/zip");
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + zipName);
|
||||||
|
|
||||||
|
// 2. 创建 ZipOutputStream 包装 Response 输出流
|
||||||
|
// 使用 try-with-resources 自动关闭 zipOut
|
||||||
|
try (ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream())) {
|
||||||
|
|
||||||
|
int index = 1;
|
||||||
|
for (Map<String, Object> itemData : data) {
|
||||||
|
// 3. 确定压缩包内的文件名
|
||||||
|
// 优先从 map 中获取 'fileName' 字段,否则使用默认编号
|
||||||
|
String entryName = (String) itemData.getOrDefault("fileName", "摸底测试_" + index);
|
||||||
|
// 确保文件名后缀正确
|
||||||
|
if (!entryName.endsWith(".docx")) {
|
||||||
|
entryName += ".docx";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. 创建 ZIP 条目
|
||||||
|
ZipEntry zipEntry = new ZipEntry(entryName);
|
||||||
|
zipOut.putNextEntry(zipEntry);
|
||||||
|
|
||||||
|
// 5. 加载模板并渲染
|
||||||
|
// 注意:每次循环都需要重新读取模板流,因为 poi-tl 渲染会修改文档结构
|
||||||
|
try (InputStream templateInputStream = new FileInputStream(templateWordPath)) {
|
||||||
|
XWPFTemplate template = XWPFTemplate.compile(templateInputStream, config);
|
||||||
|
template.render(itemData);
|
||||||
|
|
||||||
|
// 6. 将渲染后的文档直接写入 ZipOutputStream
|
||||||
|
// template.write 默认不关闭流,所以这里是安全的
|
||||||
|
template.write(zipOut);
|
||||||
|
|
||||||
|
// 显式关闭 template (释放 POI 资源)
|
||||||
|
template.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7. 关闭当前条目
|
||||||
|
zipOut.closeEntry();
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 8. 循环结束,zipOut 会在 try 块结束时自动 finish 和 close
|
||||||
|
response.flushBuffer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 现有的单文件导出逻辑
|
||||||
|
*/
|
||||||
|
private static void generateExamWordsDocx(Map<String, Object> data, HttpServletResponse response, String templateWordPath) throws IOException {
|
||||||
|
|
||||||
|
String fileName = URLEncoder.encode("摸底测试" + ".docx", StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20");
|
||||||
|
|
||||||
|
// 3. 设置响应头
|
||||||
|
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||||
|
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);
|
||||||
|
|
||||||
|
try (InputStream inputStream = new FileInputStream(templateWordPath)) {
|
||||||
|
XWPFTemplate template = XWPFTemplate.compile(inputStream, config);
|
||||||
|
OutputStream out = response.getOutputStream();
|
||||||
|
template.render(data);
|
||||||
|
template.write(out);
|
||||||
|
template.close(); // 最好关闭 template
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,5 +23,14 @@
|
|||||||
from `class`
|
from `class`
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="selectClassDOListByIds" resultType="com.yinlihupo.enlish.service.domain.dataobject.ClassDO">
|
||||||
|
select *
|
||||||
|
from `class`
|
||||||
|
where id in
|
||||||
|
<foreach collection="classIds" item="classId" separator="," close=")" open="(" index="">
|
||||||
|
#{classId}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -38,4 +38,13 @@
|
|||||||
from student
|
from student
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="selectStudentDOListByIds" resultMap="BaseResultMap">
|
||||||
|
select *
|
||||||
|
from student
|
||||||
|
where id in
|
||||||
|
<foreach item="id" collection="ids" separator="," open="(" close=")" index="index">
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import axios from "@/axios";
|
import axios from "@/axios";
|
||||||
|
import { showMessage } from "../composables/util";
|
||||||
|
|
||||||
export function uploadExamWordsPng(data) {
|
export function uploadExamWordsPng(data) {
|
||||||
return axios.post('/exam/words/submit', data)
|
return axios.post('/exam/words/submit', data)
|
||||||
@@ -16,3 +17,64 @@ export function getExamWordsDetailResult(id) {
|
|||||||
id: id
|
id: id
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function generateExamWords(data) {
|
||||||
|
return axios.post('/exam/words/generate', data, {
|
||||||
|
// 1. 重要:必须指定响应类型为 blob,否则下载的文件会损坏(乱码)
|
||||||
|
responseType: 'blob',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json; application/octet-stream' // 根据需要调整
|
||||||
|
}
|
||||||
|
}).then(response => {
|
||||||
|
// 2. 提取文件名 (处理后端设置的 Content-Disposition)
|
||||||
|
// 后端示例: header("Content-Disposition", "attachment; filename*=UTF-8''" + fileName)
|
||||||
|
let fileName = 'download.zip'; // 默认兜底文件名
|
||||||
|
|
||||||
|
const contentDisposition = response.headers['content-disposition'];
|
||||||
|
if (contentDisposition) {
|
||||||
|
// 正则提取 filename*=utf-8''xxx.zip 或 filename="xxx.zip"
|
||||||
|
const fileNameMatch = contentDisposition.match(/filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/);
|
||||||
|
if (fileNameMatch && fileNameMatch[1]) {
|
||||||
|
// 后端如果用了 URLEncoder,这里需要 decode
|
||||||
|
fileName = decodeURIComponent(fileNameMatch[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 开始下载流程
|
||||||
|
resolveBlob(response.data, fileName);
|
||||||
|
}).catch(error => {
|
||||||
|
console.error('下载失败', error);
|
||||||
|
showMessage('下载失败' + error, 'error');
|
||||||
|
// 注意:如果后端报错返回 JSON,因为 responseType 是 blob,
|
||||||
|
// 这里看到的 error.response.data 也是 blob,需要转回文本才能看到错误信息
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.readAsText(error.response.data);
|
||||||
|
reader.onload = () => {
|
||||||
|
console.log(JSON.parse(reader.result)); // 打印后端实际报错
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolveBlob = (res, fileName) => {
|
||||||
|
// 创建 Blob 对象,可以指定 type,也可以让浏览器自动推断
|
||||||
|
const blob = new Blob([res], { type: 'application/octet-stream' });
|
||||||
|
|
||||||
|
// 兼容 IE/Edge (虽然现在很少用了)
|
||||||
|
if (window.navigator.msSaveOrOpenBlob) {
|
||||||
|
navigator.msSaveBlob(blob, fileName);
|
||||||
|
} else {
|
||||||
|
// 创建一个临时的 URL 指向 Blob
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = window.URL.createObjectURL(blob);
|
||||||
|
link.download = fileName;
|
||||||
|
|
||||||
|
// 触发点击
|
||||||
|
link.style.display = 'none';
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
|
||||||
|
// 清理资源
|
||||||
|
document.body.removeChild(link);
|
||||||
|
window.URL.revokeObjectURL(link.href);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
94
enlish-vue/src/layouts/components/ExamGenerateDialog.vue
Normal file
94
enlish-vue/src/layouts/components/ExamGenerateDialog.vue
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<template>
|
||||||
|
<el-dialog v-model="visible" title="生成试题" width="520px" :close-on-click-modal="false">
|
||||||
|
<div class="space-y-4" v-loading="loading">
|
||||||
|
<el-form label-width="80px">
|
||||||
|
<el-form-item label="年级">
|
||||||
|
<el-select v-model="gradeId" placeholder="请选择年级" style="width: 240px">
|
||||||
|
<el-option v-for="g in gradeOptions" :key="g.id" :label="g.title" :value="g.id" />
|
||||||
|
</el-select>
|
||||||
|
</el-form-item>
|
||||||
|
<el-form-item label="难度">
|
||||||
|
<el-select v-model="level" placeholder="请选择难度" style="width: 240px">
|
||||||
|
<el-option :label="'一级'" :value="1" />
|
||||||
|
<el-option :label="'二级'" :value="2" />
|
||||||
|
<el-option :label="'三级'" :value="3" />
|
||||||
|
</el-select>
|
||||||
|
</el-form-item>
|
||||||
|
</el-form>
|
||||||
|
<div class="text-sm text-gray-500">
|
||||||
|
已选学生数量:{{ studentIds.length }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<template #footer>
|
||||||
|
<div class="flex justify-end gap-2">
|
||||||
|
<el-button @click="visible = false">取消</el-button>
|
||||||
|
<el-button type="primary" :disabled="!gradeId || !level" @click="handleGenerate">生成并下载</el-button>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
import { ref, computed, watch, onMounted } from 'vue'
|
||||||
|
import { getGradeList } from '@/api/grade'
|
||||||
|
import { generateExamWords } from '@/api/exam'
|
||||||
|
|
||||||
|
const props = defineProps({
|
||||||
|
modelValue: { type: Boolean, default: false },
|
||||||
|
studentIds: { type: Array, default: () => [] },
|
||||||
|
defaultGradeId: { type: [Number, String], default: null }
|
||||||
|
})
|
||||||
|
const emit = defineEmits(['update:modelValue'])
|
||||||
|
|
||||||
|
const visible = computed({
|
||||||
|
get: () => props.modelValue,
|
||||||
|
set: (val) => emit('update:modelValue', val)
|
||||||
|
})
|
||||||
|
|
||||||
|
const loading = ref(false)
|
||||||
|
const gradeOptions = ref([])
|
||||||
|
const gradeId = ref(null)
|
||||||
|
const level = ref(null)
|
||||||
|
|
||||||
|
async function fetchGrades() {
|
||||||
|
loading.value = true
|
||||||
|
try {
|
||||||
|
const res = await getGradeList(1, 100)
|
||||||
|
const d = res?.data
|
||||||
|
gradeOptions.value = Array.isArray(d?.data) ? d.data : []
|
||||||
|
if (props.defaultGradeId && !gradeId.value) {
|
||||||
|
gradeId.value = Number(props.defaultGradeId)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
loading.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleGenerate() {
|
||||||
|
if (!gradeId.value || !level.value || props.studentIds.length === 0) return
|
||||||
|
await generateExamWords({
|
||||||
|
gradeId: Number(gradeId.value),
|
||||||
|
level: Number(level.value),
|
||||||
|
studentIds: props.studentIds
|
||||||
|
})
|
||||||
|
ElMessage.success('生成任务已提交,正在下载')
|
||||||
|
visible.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => props.modelValue,
|
||||||
|
(v) => {
|
||||||
|
if (v) {
|
||||||
|
level.value = null
|
||||||
|
gradeId.value = props.defaultGradeId ? Number(props.defaultGradeId) : null
|
||||||
|
fetchGrades()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if (visible.value) fetchGrades()
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped></style>
|
||||||
@@ -35,8 +35,19 @@
|
|||||||
selectedGradeId }})</el-tag>
|
selectedGradeId }})</el-tag>
|
||||||
<el-button type="primary" @click="fetchStudents">查询</el-button>
|
<el-button type="primary" @click="fetchStudents">查询</el-button>
|
||||||
<el-button @click="resetStudentFilters">重置</el-button>
|
<el-button @click="resetStudentFilters">重置</el-button>
|
||||||
|
<el-button type="success" :disabled="selectedStudentIds.length === 0" @click="showGenerateDialog = true">
|
||||||
|
生成试题
|
||||||
|
</el-button>
|
||||||
</div>
|
</div>
|
||||||
<el-table :data="students" border class="w-full" v-loading="studentLoading">
|
<el-table
|
||||||
|
ref="studentTableRef"
|
||||||
|
:data="students"
|
||||||
|
border
|
||||||
|
class="w-full"
|
||||||
|
v-loading="studentLoading"
|
||||||
|
@selection-change="onStudentSelectionChange"
|
||||||
|
>
|
||||||
|
<el-table-column type="selection" width="48" />
|
||||||
<el-table-column prop="id" label="ID" width="80" />
|
<el-table-column prop="id" label="ID" width="80" />
|
||||||
<el-table-column prop="name" label="姓名" min-width="120" />
|
<el-table-column prop="name" label="姓名" min-width="120" />
|
||||||
<el-table-column prop="classId" label="班级ID" width="100" />
|
<el-table-column prop="classId" label="班级ID" width="100" />
|
||||||
@@ -47,6 +58,11 @@
|
|||||||
:total="studentTotalCount" :page-size="studentPageSize" :current-page="studentPageNo"
|
:total="studentTotalCount" :page-size="studentPageSize" :current-page="studentPageNo"
|
||||||
@current-change="handleStudentPageChange" @size-change="handleStudentSizeChange" />
|
@current-change="handleStudentPageChange" @size-change="handleStudentSizeChange" />
|
||||||
</div>
|
</div>
|
||||||
|
<ExamGenerateDialog
|
||||||
|
v-model="showGenerateDialog"
|
||||||
|
:student-ids="selectedStudentIds"
|
||||||
|
:default-grade-id="selectedGradeId"
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6" v-loading="gradeLoading">
|
<div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6" v-loading="gradeLoading">
|
||||||
@@ -76,6 +92,7 @@ import { ref, onMounted } from 'vue'
|
|||||||
import { getClassList } from '@/api/class'
|
import { getClassList } from '@/api/class'
|
||||||
import { getGradeList } from '@/api/grade'
|
import { getGradeList } from '@/api/grade'
|
||||||
import { getStudentList } from '@/api/student'
|
import { getStudentList } from '@/api/student'
|
||||||
|
import ExamGenerateDialog from '@/layouts/components/ExamGenerateDialog.vue'
|
||||||
|
|
||||||
const classes = ref([])
|
const classes = ref([])
|
||||||
const pageNo = ref(1)
|
const pageNo = ref(1)
|
||||||
@@ -85,6 +102,7 @@ const loading = ref(false)
|
|||||||
const classTableRef = ref(null)
|
const classTableRef = ref(null)
|
||||||
const selectedClassId = ref(null)
|
const selectedClassId = ref(null)
|
||||||
const selectedClassTitle = ref('')
|
const selectedClassTitle = ref('')
|
||||||
|
|
||||||
const grades = ref([])
|
const grades = ref([])
|
||||||
const gradePageNo = ref(1)
|
const gradePageNo = ref(1)
|
||||||
const gradePageSize = ref(10)
|
const gradePageSize = ref(10)
|
||||||
@@ -93,12 +111,16 @@ const gradeLoading = ref(false)
|
|||||||
const gradeTableRef = ref(null)
|
const gradeTableRef = ref(null)
|
||||||
const selectedGradeId = ref(null)
|
const selectedGradeId = ref(null)
|
||||||
const selectedGradeTitle = ref('')
|
const selectedGradeTitle = ref('')
|
||||||
|
|
||||||
const students = ref([])
|
const students = ref([])
|
||||||
const studentPageNo = ref(1)
|
const studentPageNo = ref(1)
|
||||||
const studentPageSize = ref(10)
|
const studentPageSize = ref(10)
|
||||||
const studentTotalCount = ref(0)
|
const studentTotalCount = ref(0)
|
||||||
const studentLoading = ref(false)
|
const studentLoading = ref(false)
|
||||||
const studentName = ref('')
|
const studentName = ref('')
|
||||||
|
const studentTableRef = ref(null)
|
||||||
|
const selectedStudentIds = ref([])
|
||||||
|
const showGenerateDialog = ref(false)
|
||||||
|
|
||||||
async function fetchClasses() {
|
async function fetchClasses() {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
@@ -178,6 +200,9 @@ function handleStudentSizeChange(s) {
|
|||||||
studentPageNo.value = 1
|
studentPageNo.value = 1
|
||||||
fetchStudents()
|
fetchStudents()
|
||||||
}
|
}
|
||||||
|
function onStudentSelectionChange(rows) {
|
||||||
|
selectedStudentIds.value = rows.map(r => r.id)
|
||||||
|
}
|
||||||
function onClassRowClick(row) {
|
function onClassRowClick(row) {
|
||||||
selectedClassId.value = row.id
|
selectedClassId.value = row.id
|
||||||
selectedClassTitle.value = row.title
|
selectedClassTitle.value = row.title
|
||||||
|
|||||||
Reference in New Issue
Block a user