|
@@ -1,5 +1,24 @@
|
|
|
package com.qmth.themis.business.templete.impl;
|
|
|
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.StringJoiner;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
import com.qmth.themis.business.constant.SystemConstant;
|
|
|
import com.qmth.themis.business.dto.response.TEExamStudentDto;
|
|
|
import com.qmth.themis.business.enums.TaskStatusEnum;
|
|
@@ -9,17 +28,6 @@ import com.qmth.themis.business.templete.TaskExportCommon;
|
|
|
import com.qmth.themis.business.templete.TaskExportTemplete;
|
|
|
import com.qmth.themis.common.util.Result;
|
|
|
import com.qmth.themis.common.util.ResultUtil;
|
|
|
-import org.slf4j.Logger;
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.FileOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: 考场导出任务
|
|
@@ -31,7 +39,7 @@ import java.util.*;
|
|
|
@Service
|
|
|
public class TaskExamStudentExportTemplete implements TaskExportTemplete {
|
|
|
private final static Logger log = LoggerFactory.getLogger(TaskExamStudentExportTemplete.class);
|
|
|
-
|
|
|
+ private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
|
|
|
@Resource
|
|
|
TEExamStudentService teExamStudentService;
|
|
|
|
|
@@ -47,8 +55,8 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
|
|
|
long start = System.currentTimeMillis();
|
|
|
TaskExportCommon taskExportCommon = new TaskExportCommon(transMap);
|
|
|
taskExportCommon.init();
|
|
|
- FileOutputStream fos = null;
|
|
|
- File file = null;
|
|
|
+ ByteArrayOutputStream fos = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
StringJoiner stringJoiner = null;
|
|
|
String excelFilePath = null;
|
|
|
boolean oss = (boolean) taskExportCommon.getOssEnv().get(SystemConstant.OSS);
|
|
@@ -57,13 +65,8 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
|
|
|
if (!oss) {
|
|
|
stringJoiner.add(SystemConstant.FILES_DIR).add(File.separator);
|
|
|
}
|
|
|
- LocalDateTime nowTime = LocalDateTime.now();
|
|
|
- stringJoiner.add(String.valueOf(nowTime.getYear()))
|
|
|
- .add(File.separator).add(String.format("%02d", nowTime.getMonthValue()))
|
|
|
- .add(File.separator).add(String.format("%02d", nowTime.getDayOfMonth()));
|
|
|
- file = new File(SystemConstant.TEMP_FILES_DIR + "/" + uuid() + SystemConstant.EXCEL_PREFIX);
|
|
|
- file.createNewFile();
|
|
|
- fos = new FileOutputStream(file);
|
|
|
+ stringJoiner.add(sdf.format(new Date()));
|
|
|
+ fos = new ByteArrayOutputStream();
|
|
|
Long examId = (Long) transMap.get("examId");
|
|
|
Long activityId = (Long) transMap.get("activityId");
|
|
|
String identity = (String) transMap.get("identity");
|
|
@@ -77,7 +80,8 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
|
|
|
List<TEExamStudentDto> list = teExamStudentService.examStudentList(examId, activityId, identity, name, roomCode, courseCode, grade,
|
|
|
enable, classNo, hasPhoto);
|
|
|
ExportUtils.makeExcel(TEExamStudentDto.class, list, fos);
|
|
|
- excelFilePath = taskExportCommon.ossUpload(new FileInputStream(file), stringJoiner.toString());
|
|
|
+ inputStream = new ByteArrayInputStream(fos.toByteArray());
|
|
|
+ excelFilePath = taskExportCommon.ossUpload(inputStream, stringJoiner.toString());
|
|
|
taskExportCommon.getTbTaskHistory().setSummary("处理成功");
|
|
|
long end = System.currentTimeMillis();
|
|
|
log.info("导出考生数据结束,============耗时============:{}秒", (end - start) / 1000);
|
|
@@ -86,19 +90,21 @@ public class TaskExamStudentExportTemplete implements TaskExportTemplete {
|
|
|
taskExportCommon.setException(true);
|
|
|
taskExportCommon.getTbTaskHistory().setSummary("处理出错");
|
|
|
} finally {
|
|
|
- if (oss && file != null) {
|
|
|
- file.delete();
|
|
|
- }
|
|
|
+
|
|
|
taskExportCommon.getTbTaskHistory().setFinishTime(System.currentTimeMillis());
|
|
|
taskExportCommon.getTbTaskHistory().setStatus(TaskStatusEnum.FINISH);
|
|
|
taskExportCommon.getTbTaskHistory().setProgress(100.0d);
|
|
|
//这里写入txt文件
|
|
|
taskExportCommon.writeExportResultTxt(stringJoiner.toString(), "", excelFilePath);
|
|
|
+ if (Objects.nonNull(fos)) {
|
|
|
+ fos.flush();
|
|
|
+ fos.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(inputStream)) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
}
|
|
|
return ResultUtil.ok(Collections.singletonMap(SystemConstant.SUCCESS, true));
|
|
|
}
|
|
|
|
|
|
- private String uuid() {
|
|
|
- return UUID.randomUUID().toString().replaceAll("-", "");
|
|
|
- }
|
|
|
}
|