|
|
|
@ -19,31 +19,62 @@ import com.keyware.shandan.frame.properties.CustomProperties; |
|
|
|
|
import com.keyware.shandan.system.entity.SysFile; |
|
|
|
|
import com.keyware.shandan.system.service.SysFileService; |
|
|
|
|
import com.keyware.shandan.system.utils.SysSettingUtil; |
|
|
|
|
import lombok.AllArgsConstructor; |
|
|
|
|
import lombok.Data; |
|
|
|
|
import lombok.Getter; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.Date; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.io.Serializable; |
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
public class ExportComponent { |
|
|
|
|
public class ExportComponent extends Thread { |
|
|
|
|
@Getter |
|
|
|
|
private final String exportId; |
|
|
|
|
private Status status; |
|
|
|
|
private final String fileName; |
|
|
|
|
private List<DirectoryResource> dataList; |
|
|
|
|
private final CustomProperties customProperties; |
|
|
|
|
private final SysFileService fileService; |
|
|
|
|
private final MetadataService metadataService; |
|
|
|
|
|
|
|
|
|
@Getter |
|
|
|
|
private final Progress progress; |
|
|
|
|
// Progress
|
|
|
|
|
|
|
|
|
|
public ExportComponent(String exportId) { |
|
|
|
|
this.exportId = exportId; |
|
|
|
|
this.customProperties = AppContext.getContext().getBean(CustomProperties.class); |
|
|
|
|
this.fileService = AppContext.getContext().getBean(SysFileService.class); |
|
|
|
|
this.metadataService = AppContext.getContext().getBean(MetadataService.class); |
|
|
|
|
this.exportId = exportId; |
|
|
|
|
this.status = Status.querying; |
|
|
|
|
fileName = "export_" + DateUtil.format(new Date(), "yyyyMMddHHmmss"); |
|
|
|
|
fileName = "目录导出_" + DateUtil.format(new Date(), "yyyyMMddHHmmss"); |
|
|
|
|
this.progress = new Progress(this.exportId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 开始打包 |
|
|
|
|
*/ |
|
|
|
|
@Override |
|
|
|
|
public void run() { |
|
|
|
|
String path = getExportPath(); |
|
|
|
|
try { |
|
|
|
|
this.progress.setTitle("正在生成临时数据文件"); |
|
|
|
|
dataList.forEach(resource -> { |
|
|
|
|
if ("file".equals(resource.getResourceType())) { |
|
|
|
|
exportFile(resource); |
|
|
|
|
} else { |
|
|
|
|
exportMetadata(resource); |
|
|
|
|
} |
|
|
|
|
this.progress.offsetIncrease(); |
|
|
|
|
}); |
|
|
|
|
this.progress.setTitle("正在打包数据文件"); |
|
|
|
|
ZipUtil.zip(path, path + ".zip", true); |
|
|
|
|
this.progress.setTitle("数据包准备完毕,开始下载"); |
|
|
|
|
this.progress.offsetIncrease(); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw new RuntimeException("导出数据在打包时出错", e); |
|
|
|
|
} finally { |
|
|
|
|
delete(path); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -60,7 +91,6 @@ public class ExportComponent { |
|
|
|
|
HttpUtil.downloadFile(fileDownloadUrl, serverFile); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
List<String> lines = new ArrayList<>(); |
|
|
|
|
lines.add("文件读取异常,请检查文件是否存在!"); |
|
|
|
|
String dirPath = serverFile.getParentFile().getPath(); |
|
|
|
|
String name = "(文件读取错误)" + serverFile.getName(); |
|
|
|
|
FileUtil.writeLines(lines, new File(dirPath, name), "utf-8"); |
|
|
|
@ -134,29 +164,6 @@ public class ExportComponent { |
|
|
|
|
writer.close(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 开始打包 |
|
|
|
|
*/ |
|
|
|
|
public boolean startPackage() { |
|
|
|
|
status = Status.packaging; |
|
|
|
|
String path = getExportPath(); |
|
|
|
|
try { |
|
|
|
|
dataList.forEach(resource -> { |
|
|
|
|
if ("file".equals(resource.getResourceType())) { |
|
|
|
|
exportFile(resource); |
|
|
|
|
} else { |
|
|
|
|
exportMetadata(resource); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
ZipUtil.zip(path, path + ".zip", true); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
delete(path); |
|
|
|
|
throw new RuntimeException("导出数据在打包时出错", e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 设置需要导出的数据 |
|
|
|
|
* |
|
|
|
@ -164,10 +171,8 @@ public class ExportComponent { |
|
|
|
|
*/ |
|
|
|
|
public void setData(List<DirectoryResource> dataList) { |
|
|
|
|
this.dataList = dataList; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setStatus(String status) { |
|
|
|
|
this.status = Status.valueOf(status); |
|
|
|
|
this.progress.setFileTotal(dataList.size()); |
|
|
|
|
this.progress.offsetIncrease(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public String getExportPath() { |
|
|
|
@ -197,12 +202,49 @@ public class ExportComponent { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@AllArgsConstructor |
|
|
|
|
private enum Status { |
|
|
|
|
querying("正在查询数据"), |
|
|
|
|
packaging("正在打包数据"), |
|
|
|
|
downloading("正在下载数据"); |
|
|
|
|
@Data |
|
|
|
|
static class Progress implements Serializable { |
|
|
|
|
|
|
|
|
|
private String exportId; |
|
|
|
|
private String title; |
|
|
|
|
private Integer fileTotal; |
|
|
|
|
private Integer offset = 0; |
|
|
|
|
|
|
|
|
|
public Progress(String exportId) { |
|
|
|
|
this.exportId = exportId; |
|
|
|
|
this.title = "正在查询需要导出的数据"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void setFileTotal(Integer fileTotal) { |
|
|
|
|
// 加2是因为需要增加一个查询步骤和一个打包的步骤
|
|
|
|
|
this.fileTotal = fileTotal + 2; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void offsetIncrease() { |
|
|
|
|
this.offset++; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取百分比 |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public int getPercentage() { |
|
|
|
|
return Math.round(offset / Float.valueOf(fileTotal) * 100); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 判断是否完成 |
|
|
|
|
* |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
public boolean getIsDone() { |
|
|
|
|
return Objects.equals(offset, fileTotal); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private final String text; |
|
|
|
|
public String getMsg() { |
|
|
|
|
return "(" + getPercentage() + "%) " + this.title; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|