|
@@ -1,9 +1,31 @@
|
|
|
package com.qmth.distributed.print.business.templete.export;
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.qmth.boot.api.exception.ApiException;
|
|
|
+import com.qmth.distributed.print.business.entity.TBTask;
|
|
|
+import com.qmth.distributed.print.business.enums.TaskResultEnum;
|
|
|
+import com.qmth.distributed.print.business.enums.TaskStatusEnum;
|
|
|
+import com.qmth.distributed.print.business.enums.UploadFileEnum;
|
|
|
+import com.qmth.distributed.print.business.service.TBTaskService;
|
|
|
+import com.qmth.distributed.print.business.templete.importData.AsyncImportTaskTemplete;
|
|
|
+import com.qmth.distributed.print.business.util.OssUtil;
|
|
|
+import com.qmth.distributed.print.common.contant.SpringContextHolder;
|
|
|
+import com.qmth.distributed.print.common.contant.SystemConstant;
|
|
|
import com.qmth.distributed.print.common.util.Result;
|
|
|
+import com.qmth.distributed.print.common.util.ResultUtil;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.text.MessageFormat;
|
|
|
+import java.util.Date;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.StringJoiner;
|
|
|
|
|
|
/**
|
|
|
* @Description: 异步导出模版
|
|
@@ -13,6 +35,15 @@ import java.util.Map;
|
|
|
* @Date: 2021/3/29
|
|
|
*/
|
|
|
public abstract class AsyncExportTaskTemplete {
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(AsyncImportTaskTemplete.class);
|
|
|
+ public static final String BEGIN_TITLE = "->开始准备处理导出的";
|
|
|
+ public static final String FORMAT_TIME = DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN);
|
|
|
+ public static final String FINISH_TITLE = "->数据处理结束,共处理了";
|
|
|
+ public static final String FINISH_SIZE = "条数据";
|
|
|
+ public static final String EXCEPTION_TITLE = "->数据处理发生异常!";
|
|
|
+ public static final String EXCEPTION_DATA = "错误信息:";
|
|
|
+ public static final String TXT_PREFIX = ".txt";
|
|
|
+ public static final String EXCEPTION_CREATE_TXT_TITLE = "->创建导出日志时发生异常!";
|
|
|
|
|
|
/**
|
|
|
* 异步导出任务
|
|
@@ -24,4 +55,75 @@ public abstract class AsyncExportTaskTemplete {
|
|
|
Result exportTask(Map<String, Object> map) {
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ @Async
|
|
|
+ public abstract Result exportTask(Map<String, Object> map,Long printPlanId,String courseCode,String paperNumber,String examPlace,String examRoom,String packageCode) throws Exception;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建txt文件
|
|
|
+ *
|
|
|
+ * @param tbTask
|
|
|
+ */
|
|
|
+ public void createTxt(TBTask tbTask) {
|
|
|
+ OssUtil ossUtil = SpringContextHolder.getBean(OssUtil.class);
|
|
|
+ TBTaskService tbTaskService = SpringContextHolder.getBean(TBTaskService.class);
|
|
|
+ ByteArrayOutputStream out = null;
|
|
|
+ InputStream inputStream = null;
|
|
|
+ try {
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(tbTask.getImportFilePath());
|
|
|
+ String path = (String) jsonObject.get(SystemConstant.PATH);
|
|
|
+ String type = (String) jsonObject.get(SystemConstant.TYPE);
|
|
|
+
|
|
|
+ out = new ByteArrayOutputStream();
|
|
|
+ out.write(tbTask.getSummary().getBytes(StandardCharsets.UTF_8));
|
|
|
+ byte[] bookByteAry = out.toByteArray();
|
|
|
+ inputStream = new ByteArrayInputStream(bookByteAry);
|
|
|
+ StringJoiner stringJoiner = new StringJoiner("");
|
|
|
+ path = path.substring(0, path.lastIndexOf(File.separator) + 1);
|
|
|
+
|
|
|
+ stringJoiner.add(path).add(SystemConstant.getUuid()).add(TXT_PREFIX).toString();
|
|
|
+ if (Objects.equals(type, SystemConstant.OSS)) {//上传至oss
|
|
|
+ ossUtil.ossUpload(stringJoiner.toString(), inputStream, null);
|
|
|
+ } else {//上传至服务器
|
|
|
+ File finalFile = new File(stringJoiner.toString());
|
|
|
+ if (!finalFile.exists()) {
|
|
|
+ finalFile.getParentFile().mkdirs();
|
|
|
+ finalFile.createNewFile();
|
|
|
+ }
|
|
|
+ FileUtils.copyInputStreamToFile(inputStream, finalFile);
|
|
|
+ }
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+ json.put(SystemConstant.PATH, stringJoiner.toString());
|
|
|
+ json.put(SystemConstant.TYPE, type);
|
|
|
+ json.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
|
|
|
+
|
|
|
+ tbTask.setResult(TaskResultEnum.SUCCESS);
|
|
|
+ tbTask.setReportFilePath(json.toJSONString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求出错", e);
|
|
|
+ StringJoiner stringJoinerSummary = new StringJoiner("").add(tbTask.getSummary()).add("\n");
|
|
|
+ stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", FORMAT_TIME, EXCEPTION_CREATE_TXT_TITLE, EXCEPTION_DATA, e.getMessage()));
|
|
|
+ tbTask.setSummary(stringJoinerSummary.toString());
|
|
|
+ tbTask.setResult(TaskResultEnum.ERROR);
|
|
|
+ if (e instanceof ApiException) {
|
|
|
+ ResultUtil.error((ApiException) e, e.getMessage());
|
|
|
+ } else {
|
|
|
+ ResultUtil.error(e.getMessage());
|
|
|
+ }
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (Objects.nonNull(inputStream)) {
|
|
|
+ inputStream.close();
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(out)) {
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ tbTask.setStatus(TaskStatusEnum.FINISH);
|
|
|
+ tbTaskService.updateById(tbTask);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|