|
@@ -3,7 +3,10 @@ package com.qmth.distributed.print.business.templete.push;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
|
|
+import com.qmth.distributed.print.business.entity.TBSyncTask;
|
|
|
|
+import com.qmth.distributed.print.business.service.TBSyncTaskService;
|
|
import com.qmth.distributed.print.business.templete.importData.AsyncImportTaskTemplete;
|
|
import com.qmth.distributed.print.business.templete.importData.AsyncImportTaskTemplete;
|
|
|
|
+import com.qmth.teachcloud.common.config.DictionaryConfig;
|
|
import com.qmth.teachcloud.common.contant.SpringContextHolder;
|
|
import com.qmth.teachcloud.common.contant.SpringContextHolder;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.entity.TBTask;
|
|
import com.qmth.teachcloud.common.entity.TBTask;
|
|
@@ -19,8 +22,10 @@ import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.text.MessageFormat;
|
|
import java.text.MessageFormat;
|
|
@@ -36,6 +41,11 @@ import java.util.StringJoiner;
|
|
* @Date: 2021-10-31
|
|
* @Date: 2021-10-31
|
|
*/
|
|
*/
|
|
public abstract class AsyncPushTaskTemplate {
|
|
public abstract class AsyncPushTaskTemplate {
|
|
|
|
+ @Resource
|
|
|
|
+ private DictionaryConfig dictionaryConfig;
|
|
|
|
+ @Resource
|
|
|
|
+ private TBSyncTaskService tbSyncTaskService;
|
|
|
|
+
|
|
private final static Logger log = LoggerFactory.getLogger(AsyncPushTaskTemplate.class);
|
|
private final static Logger log = LoggerFactory.getLogger(AsyncPushTaskTemplate.class);
|
|
|
|
|
|
public static final String BEGIN_TITLE = "->开始准备处理同步的";
|
|
public static final String BEGIN_TITLE = "->开始准备处理同步的";
|
|
@@ -60,45 +70,35 @@ public abstract class AsyncPushTaskTemplate {
|
|
/**
|
|
/**
|
|
* 创建txt文件
|
|
* 创建txt文件
|
|
*
|
|
*
|
|
- * @param tbTask
|
|
|
|
|
|
+ * @param tbSyncTask
|
|
*/
|
|
*/
|
|
- public void createTxt(TBTask tbTask) {
|
|
|
|
|
|
+ public void createTxt(TBSyncTask tbSyncTask) {
|
|
// OssUtil ossUtil = SpringContextHolder.getBean(OssUtil.class);
|
|
// OssUtil ossUtil = SpringContextHolder.getBean(OssUtil.class);
|
|
TBTaskService tbTaskService = SpringContextHolder.getBean(TBTaskService.class);
|
|
TBTaskService tbTaskService = SpringContextHolder.getBean(TBTaskService.class);
|
|
ByteArrayOutputStream out = null;
|
|
ByteArrayOutputStream out = null;
|
|
InputStream inputStream = null;
|
|
InputStream inputStream = null;
|
|
try {
|
|
try {
|
|
- JSONObject jsonObject = JSONObject.parseObject(tbTask.getImportFilePath());
|
|
|
|
- if (Objects.isNull(jsonObject)) {
|
|
|
|
- jsonObject = new JSONObject();
|
|
|
|
- LocalDateTime nowTime = LocalDateTime.now();
|
|
|
|
- StringJoiner stringJoiner = new StringJoiner("");
|
|
|
|
- stringJoiner.add(UploadFileEnum.FILE.getTitle()).add(File.separator);
|
|
|
|
- stringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
|
|
|
|
- .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
|
|
|
|
- .add(String.format("%02d", nowTime.getDayOfMonth()))
|
|
|
|
- .add(File.separator).add(SystemConstant.getUuid()).add(TXT_PREFIX);
|
|
|
|
- jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
|
|
|
|
- jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
|
|
|
|
- }
|
|
|
|
- String path = (String) jsonObject.get(SystemConstant.PATH);
|
|
|
|
- path = path.replaceAll("\\\\","/");
|
|
|
|
- String type = (String) jsonObject.get(SystemConstant.TYPE);
|
|
|
|
-
|
|
|
|
out = new ByteArrayOutputStream();
|
|
out = new ByteArrayOutputStream();
|
|
- out.write(tbTask.getSummary().getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
+ out.write(tbSyncTask.getSummary().getBytes(StandardCharsets.UTF_8));
|
|
byte[] bookByteAry = out.toByteArray();
|
|
byte[] bookByteAry = out.toByteArray();
|
|
inputStream = new ByteArrayInputStream(bookByteAry);
|
|
inputStream = new ByteArrayInputStream(bookByteAry);
|
|
|
|
+ LocalDateTime nowTime = LocalDateTime.now();
|
|
StringJoiner stringJoiner = new StringJoiner("");
|
|
StringJoiner stringJoiner = new StringJoiner("");
|
|
- if (tbTask.getType() == TaskTypeEnum.CREATE_PDF) {
|
|
|
|
- path = path.replaceAll(UploadFileEnum.PDF.getTitle(), UploadFileEnum.FILE.getTitle());
|
|
|
|
- }
|
|
|
|
|
|
+ stringJoiner.add(UploadFileEnum.FILE.getTitle()).add(File.separator);
|
|
|
|
+ stringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
|
|
|
|
+ .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
|
|
|
|
+ .add(String.format("%02d", nowTime.getDayOfMonth()))
|
|
|
|
+ .add(File.separator).add(SystemConstant.getUuid()).add(TXT_PREFIX);
|
|
|
|
+ String path = stringJoiner.toString().replaceAll("\\\\","/");
|
|
|
|
+ boolean oss = dictionaryConfig.sysDomain().isOss();
|
|
|
|
|
|
- if (Objects.equals(type, SystemConstant.OSS)) {//上传至oss
|
|
|
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
|
+ if (oss) {//上传至oss
|
|
path = path.substring(0, path.lastIndexOf("/") + 1);
|
|
path = path.substring(0, path.lastIndexOf("/") + 1);
|
|
- stringJoiner.add(path).add(SystemConstant.getUuid()).add(TXT_PREFIX).toString();
|
|
|
|
|
|
+ stringJoiner.add(path).add(SystemConstant.getUuid()).add(TXT_PREFIX);
|
|
FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
|
|
FileStoreUtil fileStoreUtil = SpringContextHolder.getBean(FileStoreUtil.class);
|
|
fileStoreUtil.ossUpload(stringJoiner.toString(), inputStream, DigestUtils.md5Hex(new ByteArrayInputStream(bookByteAry)), fileStoreUtil.getUploadEnumByPath(stringJoiner.toString()).getFssType());
|
|
fileStoreUtil.ossUpload(stringJoiner.toString(), inputStream, DigestUtils.md5Hex(new ByteArrayInputStream(bookByteAry)), fileStoreUtil.getUploadEnumByPath(stringJoiner.toString()).getFssType());
|
|
|
|
+ json.put(SystemConstant.TYPE, SystemConstant.OSS);
|
|
} else {//上传至服务器
|
|
} else {//上传至服务器
|
|
File finalFile = new File(stringJoiner.toString());
|
|
File finalFile = new File(stringJoiner.toString());
|
|
if (!finalFile.exists()) {
|
|
if (!finalFile.exists()) {
|
|
@@ -106,19 +106,18 @@ public abstract class AsyncPushTaskTemplate {
|
|
finalFile.createNewFile();
|
|
finalFile.createNewFile();
|
|
}
|
|
}
|
|
FileUtils.copyInputStreamToFile(inputStream, finalFile);
|
|
FileUtils.copyInputStreamToFile(inputStream, finalFile);
|
|
|
|
+ json.put(SystemConstant.TYPE, SystemConstant.LOCAL);
|
|
}
|
|
}
|
|
- JSONObject json = new JSONObject();
|
|
|
|
json.put(SystemConstant.PATH, stringJoiner.toString());
|
|
json.put(SystemConstant.PATH, stringJoiner.toString());
|
|
- json.put(SystemConstant.TYPE, type);
|
|
|
|
json.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
|
|
json.put(SystemConstant.UPLOAD_TYPE, UploadFileEnum.FILE);
|
|
|
|
|
|
- tbTask.setReportFilePath(json.toJSONString());
|
|
|
|
|
|
+ tbSyncTask.setReportFilePath(json.toJSONString());
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error("请求出错", e);
|
|
log.error("请求出错", e);
|
|
- StringJoiner stringJoinerSummary = new StringJoiner("").add(tbTask.getSummary()).add("\n");
|
|
|
|
|
|
+ StringJoiner stringJoinerSummary = new StringJoiner("").add(tbSyncTask.getSummary()).add("\n");
|
|
stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), EXCEPTION_CREATE_TXT_TITLE, EXCEPTION_DATA, e.getMessage()));
|
|
stringJoinerSummary.add(MessageFormat.format("{0}{1}{2}{3}", DateUtil.format(new Date(), SystemConstant.DEFAULT_DATE_PATTERN), EXCEPTION_CREATE_TXT_TITLE, EXCEPTION_DATA, e.getMessage()));
|
|
- tbTask.setSummary(stringJoinerSummary.toString());
|
|
|
|
- tbTask.setResult(TaskResultEnum.ERROR);
|
|
|
|
|
|
+ tbSyncTask.setSummary(stringJoinerSummary.toString());
|
|
|
|
+ tbSyncTask.setResult(TaskResultEnum.ERROR);
|
|
// TBTask dbTask = tbTaskService.getById(tbTask.getId());
|
|
// TBTask dbTask = tbTaskService.getById(tbTask.getId());
|
|
// if (tbTask.getVersion() == dbTask.getVersion()) {
|
|
// if (tbTask.getVersion() == dbTask.getVersion()) {
|
|
// tbTask.setResetCount(new AtomicInteger(tbTask.getResetCount()).incrementAndGet());
|
|
// tbTask.setResetCount(new AtomicInteger(tbTask.getResetCount()).incrementAndGet());
|
|
@@ -140,14 +139,8 @@ public abstract class AsyncPushTaskTemplate {
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
- tbTask.setStatus(TaskStatusEnum.FINISH);
|
|
|
|
- if (tbTask.getType() == TaskTypeEnum.CREATE_PDF) {
|
|
|
|
- tbTask.setResultFilePath(tbTask.getImportFilePath());
|
|
|
|
- tbTask.setImportFileName(null);
|
|
|
|
- tbTask.setImportFilePath(null);
|
|
|
|
- tbTask.setErrorMessage(null);
|
|
|
|
- }
|
|
|
|
- tbTaskService.updateById(tbTask);
|
|
|
|
|
|
+ tbSyncTask.setStatus(TaskStatusEnum.FINISH);
|
|
|
|
+ tbSyncTaskService.updateById(tbSyncTask);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|