|
@@ -0,0 +1,228 @@
|
|
|
+package com.qmth.distributed.print;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.qmth.distributed.print.bean.ExamTaskExportVo;
|
|
|
+import com.qmth.distributed.print.business.entity.ExamTask;
|
|
|
+import com.qmth.distributed.print.business.entity.ExamTaskDetail;
|
|
|
+import com.qmth.distributed.print.business.enums.ReviewStatusEnum;
|
|
|
+import com.qmth.distributed.print.business.service.ExamTaskDetailService;
|
|
|
+import com.qmth.distributed.print.business.service.ExamTaskService;
|
|
|
+import com.qmth.teachcloud.common.entity.BasicAttachment;
|
|
|
+import com.qmth.teachcloud.common.service.BasicAttachmentService;
|
|
|
+import com.qmth.teachcloud.common.util.OssUtil;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.*;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 导出学校下试卷
|
|
|
+ */
|
|
|
+@SpringBootTest
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+public class ExamTaskPaperExportTest {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private BasicAttachmentService basicAttachmentService;
|
|
|
+ @Resource
|
|
|
+ private ExamTaskService examTaskService;
|
|
|
+ @Resource
|
|
|
+ private ExamTaskDetailService examTaskDetailService;
|
|
|
+ @Resource
|
|
|
+ OssUtil ossUtil;
|
|
|
+
|
|
|
+
|
|
|
+ // 学校Id
|
|
|
+ private Long schoolId = 5l;
|
|
|
+ private String rootPath = "/Users/xiaofei/qmth/temporary/download";
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void export() {
|
|
|
+ // 考试目录命名规则:学期/考试
|
|
|
+ String examRoot = rootPath + File.separator + System.currentTimeMillis();
|
|
|
+ List<ExamTaskExportVo> examTaskExportVoList = new ArrayList<>();
|
|
|
+ List<ExamTask> examTaskList = this.listExamTask();
|
|
|
+ for (ExamTask examTask : examTaskList) {
|
|
|
+ String paperNumberRoot = examRoot + File.separator + String.format("%s(%s)", examTask.getCourseName(), examTask.getCourseCode()) + File.separator + examTask.getPaperNumber();
|
|
|
+ ExamTaskDetail examTaskDetail = examTaskDetailService.getByExamTaskId(examTask.getId());
|
|
|
+ if (examTaskDetail != null && StringUtils.isNotBlank(examTaskDetail.getPaperAttachmentIds())) {
|
|
|
+ List<JSONObject> objectList = JSON.parseArray(examTaskDetail.getPaperAttachmentIds(), JSONObject.class);
|
|
|
+ boolean success = false;
|
|
|
+
|
|
|
+ for (JSONObject jsonObject : objectList) {
|
|
|
+ String name = jsonObject.getString("name");
|
|
|
+ Long attachmentId = jsonObject.getLong("attachmentId");
|
|
|
+ if (attachmentId != null) {
|
|
|
+ BasicAttachment basicAttachment = basicAttachmentService.getById(attachmentId);
|
|
|
+ JSONObject object = JSON.parseObject(basicAttachment.getPath(), JSONObject.class);
|
|
|
+ String path = object.getString("path");
|
|
|
+ String type = object.getString("type");
|
|
|
+
|
|
|
+ // 下载试卷
|
|
|
+ if (type.equals("oss")) {
|
|
|
+ try {
|
|
|
+ String localFilePathFileName = paperNumberRoot + File.separator + name + "-" + basicAttachment.getName() + basicAttachment.getType();
|
|
|
+ File file = new File(localFilePathFileName);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ file.getParentFile().createNewFile();
|
|
|
+ }
|
|
|
+ ossUtil.ossDownload(path, localFilePathFileName);
|
|
|
+ success = file.exists();
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (success) {
|
|
|
+ examTaskExportVoList.add(new ExamTaskExportVo(examTask.getCourseCode(), examTask.getCourseName(), examTask.getPaperNumber(), examTaskDetail.getPaperType(), examTaskDetail.getExposedPaperType(), examTaskDetail.getUnexposedPaperType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 生成excel文件
|
|
|
+ try {
|
|
|
+ createPaperDownloadExcel(examRoot, examTaskExportVoList);
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<ExamTask> listExamTask() {
|
|
|
+ QueryWrapper<ExamTask> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(ExamTask::getSchoolId, schoolId)
|
|
|
+ .eq(ExamTask::getReviewStatus, ReviewStatusEnum.PASS)
|
|
|
+ .orderByAsc(ExamTask::getId);
|
|
|
+ return examTaskService.list(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成清单
|
|
|
+ *
|
|
|
+ * @param zipLocalRootPath 路径
|
|
|
+ * @param list 数据
|
|
|
+ */
|
|
|
+ public void createPaperDownloadExcel(String zipLocalRootPath, List<ExamTaskExportVo> list) throws IOException {
|
|
|
+ String fileName = "下载清单.xlsx";
|
|
|
+ XSSFWorkbook wb = new XSSFWorkbook();
|
|
|
+ XSSFSheet sheet = wb.createSheet("数据");
|
|
|
+
|
|
|
+ // 表头
|
|
|
+ CellStyle headerStyle = wb.createCellStyle();
|
|
|
+ headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ headerStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ headerStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ headerStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ headerStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ headerStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ headerStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ headerStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ headerStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ // 背景颜色
|
|
|
+ headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ headerStyle.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());
|
|
|
+ Font font = wb.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 12);
|
|
|
+ font.setFontName("宋体");
|
|
|
+ font.setColor(IndexedColors.WHITE.getIndex());
|
|
|
+ headerStyle.setFont(font);
|
|
|
+
|
|
|
+ // 数据
|
|
|
+ XSSFCellStyle dataStyle = wb.createCellStyle();
|
|
|
+ dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ dataStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ dataStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ dataStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ dataStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ dataStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
|
|
|
+ dataStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ // 背景颜色
|
|
|
+ dataStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ font = wb.createFont();
|
|
|
+ font.setFontHeightInPoints((short) 10);
|
|
|
+ font.setFontName("宋体");
|
|
|
+ dataStyle.setFont(font);
|
|
|
+
|
|
|
+ // 锁定样式
|
|
|
+ XSSFCellStyle lockStyle = wb.createCellStyle();
|
|
|
+ lockStyle.setLocked(true);//设置锁定
|
|
|
+
|
|
|
+ // 未锁定样式
|
|
|
+ XSSFCellStyle unlockStyle = wb.createCellStyle();
|
|
|
+ unlockStyle.setLocked(false);//设置未锁定
|
|
|
+
|
|
|
+ XSSFRow headRow = sheet.createRow(0);
|
|
|
+ // 表头
|
|
|
+ String[] fieldsNameList = {"课程代码", "课程名称", "试着编号", "上传所有卷型", "已曝光卷型", "未曝光卷型"};
|
|
|
+ for (int i = 0; i < fieldsNameList.length; i++) {
|
|
|
+ XSSFCell cell = headRow.createCell(i);
|
|
|
+ cell.setCellValue(fieldsNameList[i]);
|
|
|
+ cell.setCellStyle(headerStyle);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 1; i <= list.size(); i++) {
|
|
|
+ ExamTaskExportVo examTaskExportVo = list.get(i - 1);
|
|
|
+ XSSFRow row = sheet.createRow(i);
|
|
|
+
|
|
|
+ XSSFCell cell0 = row.createCell(0);
|
|
|
+// cell0.setCellStyle(lockStyle);
|
|
|
+ cell0.setCellStyle(dataStyle);
|
|
|
+ cell0.setCellValue(examTaskExportVo.getCourseCode());
|
|
|
+
|
|
|
+ XSSFCell cell1 = row.createCell(1);
|
|
|
+// cell1.setCellStyle(lockStyle);
|
|
|
+ cell1.setCellStyle(dataStyle);
|
|
|
+ cell1.setCellValue(examTaskExportVo.getCourseName());
|
|
|
+
|
|
|
+ XSSFCell cell2 = row.createCell(2);
|
|
|
+// cell2.setCellStyle(lockStyle);
|
|
|
+ cell2.setCellStyle(dataStyle);
|
|
|
+ cell2.setCellValue(examTaskExportVo.getPaperNumber());
|
|
|
+
|
|
|
+ XSSFCell cell3 = row.createCell(3);
|
|
|
+// cell3.setCellStyle(lockStyle);
|
|
|
+ cell3.setCellStyle(dataStyle);
|
|
|
+ cell3.setCellValue(examTaskExportVo.getPaperType());
|
|
|
+
|
|
|
+ XSSFCell cell4 = row.createCell(4);
|
|
|
+// cell4.setCellStyle(lockStyle);
|
|
|
+ cell4.setCellStyle(dataStyle);
|
|
|
+ cell4.setCellValue(examTaskExportVo.getExposePaperType());
|
|
|
+
|
|
|
+ XSSFCell cell5 = row.createCell(5);
|
|
|
+// cell5.setCellStyle(lockStyle);
|
|
|
+ cell5.setCellStyle(dataStyle);
|
|
|
+ cell5.setCellValue(examTaskExportVo.getUnExposePaperType());
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < fieldsNameList.length; i++) {
|
|
|
+ sheet.autoSizeColumn(i);
|
|
|
+ sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 17 / 10);
|
|
|
+ }
|
|
|
+
|
|
|
+ // sheet添加保护,这个一定要否则光锁定还是可以编辑的
|
|
|
+// sheet.protectSheet("Qmth87863577");
|
|
|
+
|
|
|
+ File file = new File(zipLocalRootPath, fileName);
|
|
|
+ FileOutputStream fout = new FileOutputStream(file);
|
|
|
+ wb.write(fout);
|
|
|
+ fout.close();
|
|
|
+ }
|
|
|
+}
|