1.增加导出数据接口。

This commit is contained in:
sxlong 2024-11-26 09:31:30 +08:00
parent 4bc0c04706
commit 286f23ad21
10 changed files with 556 additions and 191 deletions

View File

@ -29,4 +29,9 @@ pagehelper.reasonable=false
server.port=8888
zip.password=~~BYE6cWGBJJjsU9DczYtZIQ==
upload.file.dest.path=G:/data/
#windows文件存储目录
upload.file.dest.path=E:/comacFileStore/
#文件缓存路径
upload.file.cache.dir=/szsd/cache/
#文件存储路径
upload.file.store.dir=/szsd/fileStore/

View File

@ -62,4 +62,18 @@ public interface EDDataService {
*/
Boolean createMultiLevelDirectory(String path);
/**
* 导出数据
* @param parames
* @return
*/
String exportData(EDDataParams parames);
/**
* 解压后的数据目录
* @param folderPath
* @return
*/
Boolean importData(String folderPath);
}

View File

@ -3,37 +3,65 @@
*/
package com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.impl;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.poi.excel.cell.CellSetter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataInfo;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.model.EDDataPage;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.parames.EDDataParams;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.repository.EDDataRepository;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.indicator.service.EDDataService;
import com.electromagnetic.industry.software.data.manage.domain.boardservice.user.repository.CategoryRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import electromagnetic.data.framework.share.exception.LoggerConstant;
import electromagnetic.data.framework.share.id.IdWorker;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import electromagnetic.data.framework.share.util.EleZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.*;
/**
* @author
* @version $Id: IndicatorCardServiceImpl.java, v 0.1 2024-07-26 17:20
*/
@Service
@Slf4j
public class EDDataServiceImpl implements EDDataService {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstant.DOMAIN_SERVICE);
// private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstant.DOMAIN_SERVICE);
@Resource
private EDDataRepository edDataRepository;
@Resource
private CategoryRepository categoryRepository;
@Value("${zip.password}")
private String passwordZIP;
@Value("${upload.file.dest.path}")
private String uploadFilePath;
@Value("${upload.file.cache.dir}")
private String fileCacheDir;
@Value("${upload.file.store.dir}")
private String fileStoreDir;
/**
* 创建文件/文件夹数据信息
@ -42,7 +70,44 @@ public class EDDataServiceImpl implements EDDataService {
*/
public Boolean createDataInfo(EDDataInfo edDataInfo)
{
edDataRepository.createDataInfo(edDataInfo);
// 获取上级目录的名称
Category categoryParent = new Category();
categoryParent.setCategoryId(edDataInfo.getCategoryId());
List<Category> categoryParentList = categoryRepository.selectCategories(categoryParent);
if(categoryParentList.size() < 1)
{
return Boolean.FALSE;
}
else
{
// 获取新文件夹路径信息
categoryParent = categoryParentList.get(0);
String osName = System.getProperty("os.name").toLowerCase();
String storageDirectory = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir;
String folderParent = categoryParent.getCategoryId() + "_" + categoryParent.getCategoryName() ;
String folderNew = storageDirectory + "/" + folderParent + "/" + edDataInfo.getDataName();
// 判断文件夹名称是否存在
EDDataParams folderParames = new EDDataParams();
folderParames.setParentId(edDataInfo.getCategoryId());
List<EDDataInfo> childFileInfoList = edDataRepository.getDataInfoList(folderParames);
for(EDDataInfo fileInfo : childFileInfoList)
{
if(fileInfo.getDataName() == edDataInfo.getDataName()){
return Boolean.FALSE;
}
}
// 将文件夹数据写到数据库中
if(edDataRepository.createDataInfo(edDataInfo)) {
if (!FileUtil.exist(folderNew)){
FileUtil.mkdir(folderNew);
}
}
}
return Boolean.TRUE;
}
@ -145,4 +210,302 @@ public class EDDataServiceImpl implements EDDataService {
}
/**
* 导出数据
* @param parames
* @return
*/
public String exportData(EDDataParams parames)
{
// 获取文件夹编码ID数组
List<String> categoryIdInputList = Arrays.asList(parames.getDataIdArr());
String osName = System.getProperty("os.name").toLowerCase();
fileCacheDir = osName.startsWith("win") ? uploadFilePath + fileCacheDir : fileCacheDir;
String exportDataDirectory = "";
{
exportDataDirectory = fileCacheDir + "/ExportData";
File cacheDirectory = new File(exportDataDirectory);
if (!cacheDirectory.exists()) {
cacheDirectory.mkdirs();
}
}
List<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
List<Category> categoryInputList = new ArrayList(); //客户端输入的目录数组map
Map<String, Category> categoryInputMap = new HashMap(); //客户端输入的目录数组map
for (Category category : categoryAllList)
{
categoryAllMap.put(category.getCategoryId(), category);
}
for (String categoryId : categoryIdInputList)
{
Category category = categoryAllMap.get(categoryId);
categoryInputList.add(category);
categoryInputMap.put(categoryId, category);
}
List<Category> categoryHighestList = new ArrayList();
List<Category> highestToTopCategoriesPathList = new ArrayList(); //输入的最高级目录到 所有目录的顶级目录的 路径目录数组
// 从客户端上传的输入目录数组中 找到最高级(不是最顶级)目录树节点
{
Category categoryHighestParent = null;
Category categoryHighestParentTemp = null;
String categoryIdParent = "";
for (Category category : categoryInputList)
{
// 从输入目录数组找到最高级父节点目录
categoryHighestParentTemp = category;
do{
categoryIdParent = categoryHighestParentTemp.getParentId();
categoryHighestParent = categoryHighestParentTemp;
categoryHighestParentTemp = categoryInputMap.get(categoryIdParent);
}
while(categoryHighestParentTemp != null);
categoryHighestList.add(categoryHighestParent);
}
for (Category category : categoryHighestList) {
List<Category> topCategoriesPathList = categoryRepository.getTopCategoriesPathList(category, categoryAllMap);
for (Category categoryPath : topCategoriesPathList) {
if(!highestToTopCategoriesPathList.contains(categoryPath)) {
highestToTopCategoriesPathList.add(categoryPath);
}
}
}
}
// 导出目录树数据为json文件
{
List<Category> categoryExportList = new ArrayList(); //需要导出的目录数据数组
categoryExportList.addAll(highestToTopCategoriesPathList);
for (Category category : categoryInputList) {
if(!categoryExportList.contains(category)) {
categoryExportList.add(category);
}
}
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 将List<Person>转换为JSON字符串
String jsonString = "";
try {
jsonString = objectMapper.writeValueAsString(categoryExportList);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// 将JSON字符串写入文件
File jsonFile = new File(exportDataDirectory + "/categoryList.json");
try {
objectMapper.writeValue(jsonFile, jsonString);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
}
// 导出文件信息数据和文件数据
{
// 保存文件信息数据为json文件
EDDataParams folderParames = new EDDataParams();
List<EDDataInfo> fileAllList = edDataRepository.getDataInfoList(folderParames); //获取所有文件信息数据
Map<String, EDDataInfo> fileAllMap = new HashMap(); //所有文件数组map
for (EDDataInfo fileFinfo : fileAllList)
{
fileAllMap.put(fileFinfo.getDataId(), fileFinfo);
}
List<EDDataInfo> fileExportList = new ArrayList(); //需要导出的文件信息数据数组
for (EDDataInfo fileFinfo : fileAllList)
{
if(categoryInputMap.get(fileFinfo.getCategoryId()) != null)
{
fileExportList.add(fileFinfo);
}
}
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 将List<Person>转换为JSON字符串
String jsonString = "";
try {
jsonString = objectMapper.writeValueAsString(fileExportList);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// 将JSON字符串写入文件
File jsonFile = new File(exportDataDirectory + "/fileInfoList.json");
try {
objectMapper.writeValue(jsonFile, jsonString);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
// 将文件 复制到 数据导出的缓存目录中
//String storageDirectory = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir;
//error Path source = Paths.get(storageDirectory + "/testFolder3");
//Path target = Paths.get(exportDataDirectory);
//FileUtil.copy(source, target); // StandardCopyOption.REPLACE_EXISTING
//System.out.println("文件数据已成功复制到目标目录。");
}
// 将目录树数据 文件夹及文件夹内数据 进行压缩打包
{
String zipPathFileName = fileCacheDir + "/comac_exportData1.comac";
String enCodeZipPathFileName = fileCacheDir + "/comac_exportData.comac";
// EleZipUtil.zipFile(exportDataDirectory, zipPathFileName, passwordZIP);
ZipUtil.zip(exportDataDirectory, zipPathFileName);
String password = "adknfhkj87654knd";
AES aes = SecureUtil.aes(password.getBytes()); // aesKey是加密密钥
try(
InputStream inputStream = new FileInputStream(zipPathFileName);
OutputStream outputStream = new FileOutputStream(enCodeZipPathFileName);
) {
aes.encrypt(inputStream, outputStream, true);
} catch (Exception e) {
log.error("文件加密错误..", e);
}
System.out.println("目录树数据+文件数据已成功复制到目标目录。" + zipPathFileName);
}
// 删除 导出数据的 缓存目录
FileUtil.del(exportDataDirectory);
return fileCacheDir + "/comac_exportData.comac";
}
/**
* 将解压后的数据导入到数据库中
* @param folderPath 解压后的数据目录
* @return
*/
public Boolean importData(String folderPath)
{
// 获取所有目录树节点数据
List<Category> categoryAllList = categoryRepository.getAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
for (Category category : categoryAllList)
{
categoryAllMap.put(category.getCategoryId(), category);
}
// 获取所有文件信息数据
EDDataParams parames = new EDDataParams();
List<EDDataInfo> fileInfoAllList = edDataRepository.getDataInfoList(parames);
Map<String, EDDataInfo> fileInfoAllMap = new HashMap(); //所有文件数组map
for (EDDataInfo fileInfo : fileInfoAllList)
{
fileInfoAllMap.put(fileInfo.getDataId(), fileInfo);
}
// 读取并反序列化目录树的JSON文件数据为List<Category>然后写入到数据库中
List<Category> categoryImportList = null;
{
// 读取文件数据
try {
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 指定要读取的JSON文件路径
File jsonFile = new File(folderPath + "/categoryList.json");
// 将JSON数据从文件中读取并反序列化为List<Person>
categoryImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class));
if (categoryImportList == null) {
System.out.println("读取并反序列化JSON文件数据为List<Category>失败!");
return Boolean.FALSE;
}
} catch (IOException e) {
e.printStackTrace();
}
// 将目录树数据写入到数据库中
for (Category category : categoryImportList) {
if (categoryAllMap.get(category.getCategoryId()) == null) {
categoryRepository.createCategory(category);
}
}
}
// 读取并反序列化文件信息的JSON文件数据为List<EDDataInfo>然后写入到数据库中
List<EDDataInfo> fileInfoImportList = null;
{
// 读取文件数据
try {
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 指定要读取的JSON文件路径
File jsonFile = new File(folderPath + "/fileInfoList.json");
// 将JSON数据从文件中读取并反序列化为List<Person>
fileInfoImportList = objectMapper.readValue(jsonFile, objectMapper.getTypeFactory().constructCollectionType(List.class, Category.class));
if (fileInfoImportList == null) {
log.info("读取并反序列化JSON文件数据为List<EDDataInfo>失败!");
return Boolean.FALSE;
}
} catch (IOException e) {
e.printStackTrace();
}
// 将文件信息数据写入到数据库中
for (EDDataInfo fileInfo : fileInfoImportList) {
if (fileInfoAllMap.get(fileInfo.getDataId()) == null) {
edDataRepository.createDataInfo(fileInfo);
}
}
}
// 将解压后的文件夹和文件 移动到 上传文件的存储目录中
{
String osName = System.getProperty("os.name").toLowerCase();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir;
String cacheFilePath = folderPath;
try {
for (EDDataInfo fileInfo : fileInfoImportList) {
if (fileInfoAllMap.get(fileInfo.getDataId()) == null) {
if (!FileUtil.exist(storageFilePath)){
FileUtil.mkdir(storageFilePath);
}
Path source = Paths.get(cacheFilePath);
Path target = Paths.get(storageFilePath);
Files.move(source, target);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return Boolean.TRUE;
}
}

View File

@ -2,7 +2,9 @@ package com.electromagnetic.industry.software.data.manage.domain.boardservice.us
import com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public interface CategoryRepository {
@ -30,4 +32,37 @@ public interface CategoryRepository {
*/
List<Category> selectChildCategories(Category category);
/**
* 创建目录树节点数据
* @return
*/
Boolean createCategory(Category category);
/**
* 通过 节点 获取到顶级节点的树路径 从指定的节点数组中
* @param category
* @param categoryMap
* @return
*/
List<Category> getTopCategoriesPathList(Category category, Map<String, Category> categoryMap);
/**
* 通过 节点 获取父节点 从指定的节点数组中
* @param category
* @param categoryList
* @return
*/
Category getParentCategories(Category category, List<Category> categoryList);
/**
* 获通过 节点ID 获取父节点 从指定的节点数组中
* @param categoryId
* @param categoryList
* @return
*/
Category getParentCategories(String categoryId, List<Category> categoryList);
}

View File

@ -18,7 +18,7 @@ import java.util.List;
* @date: 2024/1/12
* @version: 1.0
*/
public class ZipUtil {
public class EleZipUtil {
/**
* 压缩指定路径的文件
* @param srcFilePath 待压缩文件路径

View File

@ -59,7 +59,7 @@ public interface EDDataFacade {
* @param request
* @return
*/
ElectromagneticResult<String> batchExport(EDDataRequest request);
ElectromagneticResult<Map<String, String>> batchExport(EDDataRequest request);
/**
* 导入

View File

@ -31,4 +31,11 @@ public interface CategoryMapper {
* @return
*/
List<Category> selectChildCategories(Category category);
/**
* 创建目录树节点数据
* @return
*/
Boolean createCategory(Category category);
}

View File

@ -6,7 +6,9 @@ import com.electromagnetic.industry.software.data.manage.repository.dao.Category
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Repository
public class CategoryRepositoryImpl implements CategoryRepository {
@ -44,4 +46,76 @@ public class CategoryRepositoryImpl implements CategoryRepository {
@Override
public List<Category> selectChildCategories(Category category) { return categoryMapper.selectChildCategories(category); }
/**
* 创建目录树节点数据
* @return
*/
@Override
public Boolean createCategory(Category category) { return categoryMapper.createCategory(category); }
/**
* 通过 节点 获取到顶级节点的树路径 从指定的节点数组中
* @param category
* @param categoryMap
* @return
*/
public List<Category> getTopCategoriesPathList(Category category, Map<String, Category> categoryMap)
{
Category categoryParent = categoryMap.get(category.getParentId());
if(categoryParent != null)
{
List<Category> topCategoriesPathList = getTopCategoriesPathList(categoryParent, categoryMap);
if(topCategoriesPathList == null)
{
topCategoriesPathList = new ArrayList();
}
topCategoriesPathList.add(0, categoryParent);
return topCategoriesPathList;
}
return null;
}
/**
* 通过 节点 获取父节点 从指定的节点数组中
* @param category
* @param categoryList
* @return
*/
public Category getParentCategories(Category category, List<Category> categoryList)
{
Category categoryParent = null;
for(Category iter : categoryList)
{
if(categoryParent == null && iter.getCategoryId().equals(category.getParentId()))
{
categoryParent = iter;
break;
}
}
return categoryParent;
}
/**
* 获通过 节点ID 获取父节点 从指定的节点数组中
* @param categoryId
* @param categoryList
* @return
*/
public Category getParentCategories(String categoryId, List<Category> categoryList)
{
Category category = null;
for(Category iter : categoryList)
{
if(category == null && iter.getCategoryId().equals(categoryId))
{
category = iter;
break;
}
}
return getParentCategories(category, categoryList);
}
}

View File

@ -45,4 +45,23 @@
where parent_id = parentId and category_status="available"
</select>
<insert id="createCategory" parameterType="com.electromagnetic.industry.software.data.manage.domain.boardservice.category.model.Category">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long">
SELECT LAST_INSERT_ID()
</selectKey>
insert into ed_data_info (id,
category_type_id, parent_id, category_id,
category_name, category_status,
creator, creator_name, gmt_create,
modifier, modifier_name, gmt_modified,
effect_flag
)
values (#{id,jdbcType=VARCHAR}, #{categoryTypeId,jdbcType=VARCHAR},
#{parentId,jdbcType=VARCHAR}, #{categoryId,jdbcType=VARCHAR},
#{categoryName,jdbcType=VARCHAR}, #{categoryStatus,jdbcType=VARCHAR},
#{creator,jdbcType=VARCHAR}, #{creatorName,jdbcType=VARCHAR}, now(), #{modifier,jdbcType=VARCHAR},
#{modifierName,jdbcType=VARCHAR},now(),1
)
</insert>
</mapper>

View File

@ -1,8 +1,7 @@
package com.electromagnetic.industry.software.data.manage.service.facade;
package com.electromagnetic.industry.software.data.manage.service.facade;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileMode;
import cn.hutool.core.util.ZipUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@ -23,8 +22,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import electromagnetic.data.framework.share.id.IdWorker;
import electromagnetic.data.framework.share.model.ElectromagneticResult;
import electromagnetic.data.framework.share.model.ElectromagneticResultUtil;
import electromagnetic.data.framework.share.util.EleZipUtil;
import lombok.extern.slf4j.Slf4j;
import electromagnetic.data.framework.share.util.ZipUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
@ -44,13 +43,16 @@ public class EDDataFacadeImpl implements EDDataFacade {
@Resource
private EDDataService edDataService;
@Resource
private CategoryService categoryService;
@Value("${upload.file.dest.path}")
private String uploadFilePath;
@Value("${zip.password}")
private String passwordZIP;
@Value("${upload.file.dest.path}")
private String uploadFilePath;
@Value("${upload.file.cache.dir}")
private String fileCacheDir;
@Value("${upload.file.store.dir}")
private String fileStoreDir;
private static final String UPLOAD_FILE_CHUNK_SUFFIX = ".part";
@ -71,7 +73,7 @@ public class EDDataFacadeImpl implements EDDataFacade {
if(Boolean.TRUE) {
if (request.getUserId() == null || request.getUserId().isEmpty()) request.setUserId(IdWorker.getSnowFlakeIdString());
if (request.getUserName() == null || request.getUserName().isEmpty()) request.setUserName("user");
if (request.getName() == null || request.getName().isEmpty()) request.setName("testFolder");
//if (request.getName() == null || request.getName().isEmpty()) request.setName("testFolder");
//if (request.getParentId() == null || request.getParentId().isEmpty()) request.setParentId(IdWorker.getSnowFlakeIdString());
}
@ -89,51 +91,8 @@ public class EDDataFacadeImpl implements EDDataFacade {
edDataInfo.setVersion("1.0.0");
edDataInfo.setDataStatus("publish");
String parentFolderPath = "";
// 获取上级目录的名称
if(!request.getParentId().isEmpty()) {
EDDataParams parames = new EDDataParams();
parames.setDataId(request.getParentId());
EDDataInfo edDataInfoParent = edDataService.getDataInfo(parames);
if(edDataInfoParent == null) {
ElectromagneticResultUtil.fail(HttpStatus.BAD_REQUEST.toString(),"上级文件夹为空!");
}
String folderNameParent = edDataInfoParent.getDataName();
String folderIdParent = edDataInfoParent.getDataId();
// 记录当前目录的文件夹路径和文件夹id路径
JSONObject implantJsonObject = JSON.parseObject(edDataInfoParent.getImplantJson());
if(implantJsonObject == null) {
implantJsonObject = new JSONObject();
parentFolderPath = folderNameParent;
implantJsonObject.put("folderPath", parentFolderPath);
implantJsonObject.put("folderIdPath", folderIdParent);
}
else
{
String folderPath = implantJsonObject.getString("folderPath");
String folderIdPath = implantJsonObject.getString("folderIdPath");
parentFolderPath = folderPath + "/" + folderNameParent;
implantJsonObject.put("folderPath", parentFolderPath);
implantJsonObject.put("folderIdPath", folderIdPath + "/" + folderIdParent);
}
String newImplantJson = implantJsonObject.toJSONString();
edDataInfo.setImplantJson(newImplantJson);
}
boolean isSuccess = edDataService.createDataInfo(edDataInfo);
if(isSuccess)
{
// 获取文件存储的文件夹路径
String storageDirectory = "D:/fileTemp/" + parentFolderPath + "/" + edDataInfo.getDataName();
if(!edDataService.checkFolderIsExist(storageDirectory)) {
edDataService.createMultiLevelDirectory(storageDirectory);
}
}
return ElectromagneticResultUtil.success(Boolean.TRUE);
Boolean isSuccess = edDataService.createDataInfo(edDataInfo);
return ElectromagneticResultUtil.success(isSuccess);
}
@ -220,10 +179,10 @@ public class EDDataFacadeImpl implements EDDataFacade {
fileName = fileFullName.substring(fileFullName.lastIndexOf("."));
}
// 获取文件存储的文件夹路径
String storageDirectory = "D:/fileTemp";
if(!edDataService.checkFolderIsExist(storageDirectory)) {
edDataService.createMultiLevelDirectory(storageDirectory);
String osName = System.getProperty("os.name").toLowerCase();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir;
if(!edDataService.checkFolderIsExist(storageFilePath)) {
edDataService.createMultiLevelDirectory(storageFilePath);
}
String parentFolderPath = ""; //上级文件夹路径
@ -251,7 +210,7 @@ public class EDDataFacadeImpl implements EDDataFacade {
}
// 文件保存目录路径
String fileSavePath = storageDirectory + "/" + parentFolderPath;
String fileSavePath = storageFilePath + "/" + parentFolderPath;
String treeName = "目录树名称";
//String newFileName = treeName + "," + parentFolderName + "," + fileFullName;
String newFileName = fileFullName;
@ -292,8 +251,8 @@ public class EDDataFacadeImpl implements EDDataFacade {
boolean isSuccess = edDataService.createDataInfo(edDataInfo);
String userHome = System.getProperty("user.home");
File cacheDirectory = new File(userHome + "\\AppData\\Local\\Temp\\EDData\\Upload");
String cacheUploadFilePath = osName.startsWith("win") ? uploadFilePath + fileCacheDir : fileCacheDir;
File cacheDirectory = new File(cacheUploadFilePath + "/Upload");
if (!cacheDirectory.exists()) {
cacheDirectory.mkdirs();
}
@ -351,7 +310,8 @@ public class EDDataFacadeImpl implements EDDataFacade {
//EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
// 获取文件存储的文件夹路径
String storageDirectory = "D:/fileTemp";
String osName = System.getProperty("os.name").toLowerCase();
String storageFilePath = osName.startsWith("win") ? uploadFilePath + fileStoreDir : fileStoreDir;
EDDataParams parames = new EDDataParams();
List<String> dataPathArr = new ArrayList();
for(String dataId : request.getDataIdArr())
@ -362,7 +322,7 @@ public class EDDataFacadeImpl implements EDDataFacade {
{
JSONObject implantJsonObject = JSON.parseObject(edDataInfo.getImplantJson());
String filePath = implantJsonObject.getString("folderPath");
filePath = storageDirectory + "/" + filePath + "/" + edDataInfo.getDataName();
filePath = storageFilePath + "/" + filePath + "/" + edDataInfo.getDataName();
dataPathArr.add(filePath);
}
}
@ -379,132 +339,18 @@ public class EDDataFacadeImpl implements EDDataFacade {
* @return
*/
@Override
public ElectromagneticResult<String> batchExport(EDDataRequest request) {
public ElectromagneticResult<Map<String, String>> batchExport(EDDataRequest request) {
//1根据用户选择层级树编码查出所有文件和文件夹list
//2循环list将每个文件复制到新建目录并进行重命名命名规则目录树编码+,+文件夹编码有则填写无则为空+,+文件编码
//3打包新建为zip并根据生产下载地址域名+文件路径+文件
//4返回前端下载的地址
// **********在导出的过程中可能会出现有用户上传文件的情况
// 获取文件夹编码ID数组
List<String> categoryIdInputList = Arrays.asList(request.getDataIdArr());
String cacheFolder = ""; // 缓存文件夹的绝对路径
String exportDataDirectory = "";
{
// 获取缓存文件夹的绝对路径
String userHome = System.getProperty("user.home");
cacheFolder = userHome + "\\AppData\\Local\\Temp\\EDData"; // 缓存文件夹的绝对路径
exportDataDirectory = cacheFolder + "\\ExportData";
File cacheDirectory = new File(exportDataDirectory);
if (!cacheDirectory.exists()) {
cacheDirectory.mkdirs();
}
}
List<Category> categoryAllList = categoryService.selectAllCategories();
Map<String, Category> categoryAllMap = new HashMap(); //所有目录数组map
List<Category> categoryInputList = new ArrayList(); //客户端输入的目录数组map
Map<String, Category> categoryInputMap = new HashMap(); //客户端输入的目录数组map
for (Category category : categoryAllList)
{
categoryAllMap.put(category.getCategoryId(), category);
}
for (String categoryId : categoryIdInputList)
{
Category category = categoryAllMap.get(categoryId);
categoryInputList.add(category);
categoryInputMap.put(categoryId, category);
}
List<Category> categoryHighestList = new ArrayList();
List<Category> highestToTopCategoriesPathList = new ArrayList(); //输入的最高级目录到 所有目录的顶级目录的 路径目录数组
// 从客户端上传的输入目录数组中 找到最高级(不是最顶级)目录树节点
{
Category categoryHighestParent = null;
Category categoryHighestParentTemp = null;
String categoryIdParent = "";
for (Category category : categoryInputList)
{
// 从输入目录数组找到最高级父节点目录
categoryHighestParentTemp = category;
do{
categoryIdParent = categoryHighestParentTemp.getParentId();
categoryHighestParent = categoryHighestParentTemp;
categoryHighestParentTemp = categoryInputMap.get(categoryIdParent);
}
while(categoryHighestParentTemp != null);
categoryHighestList.add(categoryHighestParent);
}
for (Category category : categoryHighestList) {
List<Category> topCategoriesPathList = categoryService.getTopCategoriesPathList(category, categoryAllMap);
for (Category categoryPath : topCategoriesPathList) {
if(!highestToTopCategoriesPathList.contains(categoryPath)) {
highestToTopCategoriesPathList.add(categoryPath);
}
}
}
}
// 导出目录数据为json文件
{
List<Category> categoryExportList = new ArrayList(); //需要导出的目录数据数组
categoryExportList.addAll(highestToTopCategoriesPathList);
for (Category category : categoryInputList) {
if(!categoryExportList.contains(category)) {
categoryExportList.add(category);
}
}
// 创建ObjectMapper对象用于JSON处理
ObjectMapper objectMapper = new ObjectMapper();
// 将List<Person>转换为JSON字符串
String jsonString = "";
try {
jsonString = objectMapper.writeValueAsString(categoryExportList);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// 将JSON字符串写入文件
File jsonFile = new File(exportDataDirectory + "/categoryList.json");
try {
objectMapper.writeValue(jsonFile, jsonString);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("目录树数据已成功导出为JSON文件。" + jsonFile.getAbsolutePath());
}
// 导出文件和文件夹数据
{
String storageDirectory = "D:/fileTemp";
Path source = Paths.get(storageDirectory + "/testFolder3");
Path target = Paths.get(exportDataDirectory);
FileUtil.copy(source, target); // StandardCopyOption.REPLACE_EXISTING
System.out.println("文件数据已成功复制到目标目录。");
}
// 将目录树数据 文件夹及文件夹内数据 进行压缩打包
{
String zipPathFileName = cacheFolder + "/comac_exportData.comac";
ZipUtil.zipFile(exportDataDirectory, zipPathFileName, passwordZIP);
System.out.println("目录树数据+文件数据已成功复制到目标目录。" + zipPathFileName);
}
// 删除 导出数据的 缓存目录
FileUtil.del(exportDataDirectory);
return ElectromagneticResultUtil.success(cacheFolder + "\\comac_exportData.zip");
EDDataParams parames= EDDataMappers.INSTANCE.getEDDataParames(request);
String fileUrl = edDataService.exportData(parames);
Map<String, String> result = new HashMap<>();
result.put("url", fileUrl);
return ElectromagneticResultUtil.success(result);
}
/**
@ -614,8 +460,9 @@ public class EDDataFacadeImpl implements EDDataFacade {
ZipUtil.unzip(destZipPath, uploadFilePath + identifier + File.separator);
// 文件信息存入数据库
Boolean result = edDataService.importData(destZipPath);
return ElectromagneticResultUtil.success(true);
return ElectromagneticResultUtil.success(result);
}
@Override
@ -636,4 +483,5 @@ public class EDDataFacadeImpl implements EDDataFacade {
}
}