导入相关功能调通简单场景。
This commit is contained in:
parent
0e7a54ba50
commit
7ba8c10d34
|
|
@ -420,53 +420,53 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
|
|
||||||
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
|
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
|
||||||
public void update2Database(String prjDirPath) {
|
public void update2Database(String prjDirPath) {
|
||||||
|
List<EdFileInfo> importAllFiles = JSONUtil.toList(prjDirPath + File.separator + "mysql.info", EdFileInfo.class);
|
||||||
List<EdFileInfo> importFileInfos = JSONUtil.toList(prjDirPath + File.separator + "mysql.info", EdFileInfo.class);
|
|
||||||
// 首先处理导入的文件夹及其下属文件
|
// 首先处理导入的文件夹及其下属文件
|
||||||
// 找出文件夹
|
// 找出文件夹
|
||||||
List<EdFileInfo> needMove2FileSystemFiles = new ArrayList<>();
|
List<EdFileInfo> importDirs = importAllFiles.stream()
|
||||||
List<EdFileInfo> importDirs = importFileInfos.stream()
|
|
||||||
.filter(e -> e.getDataType().equals(EleDataTypeEnum.FOLDER.code))
|
.filter(e -> e.getDataType().equals(EleDataTypeEnum.FOLDER.code))
|
||||||
.filter(e -> !e.getPrjDir()).collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
List<EdFileInfo> allObjs = new ArrayList<>();
|
||||||
|
List<EdFileInfo> prjFolders = importDirs.stream().filter(EdFileInfo::getPrjDir).collect(Collectors.toList());
|
||||||
|
allObjs.addAll(prjFolders);
|
||||||
for (EdFileInfo edFileInfo : importDirs) {
|
for (EdFileInfo edFileInfo : importDirs) {
|
||||||
String fileName = edFileInfo.getFileName();
|
String fileName = edFileInfo.getFileName();
|
||||||
String parentId = edFileInfo.getParentId();
|
String parentId = edFileInfo.getParentId();
|
||||||
List<EdFileInfo> folders = queryChildFolders(parentId);
|
List<EdFileInfo> folders = queryChildFolders(parentId);
|
||||||
Map<String, EdFileInfo> foldersMap = folders.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
Map<String, EdFileInfo> foldersMap = folders.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
||||||
List<String> folderNames = folders.stream().map(EdFileInfo::getFileName).collect(Collectors.toList());
|
List<String> folderNames = folders.stream().map(EdFileInfo::getFileName).collect(Collectors.toList());
|
||||||
String saveSysFilePath;
|
|
||||||
if (folderNames.contains(fileName)) { // 有同名文件夹
|
if (folderNames.contains(fileName)) { // 有同名文件夹
|
||||||
// 判断id是否相同
|
// 判断id是否相同
|
||||||
if (foldersMap.get(fileName).getId().equals(edFileInfo.getId())) { // id相同,不做处理
|
if (foldersMap.get(fileName).getId().equals(edFileInfo.getId())) { // id相同,不做处理
|
||||||
log.info("id相同,不做处理");
|
log.info("id相同,不做处理");
|
||||||
} else { // 文件名重复,导入的文件名需要添加“_1”
|
} else { // 文件名重复,导入的文件名需要添加“_1”
|
||||||
edFileInfo.setFileNote(fileName + "_1");
|
edFileInfo.setFileNote(fileName + "_1");
|
||||||
this.baseMapper.insert(edFileInfo);
|
allObjs.add(edFileInfo);
|
||||||
}
|
}
|
||||||
} else { // 没有同名文件夹
|
} else { // 没有同名文件夹
|
||||||
this.saveOrUpdate(edFileInfo); // 如果导出的文件夹在线上已经被作废,这里则直接恢复,需要注意的是,在作废文件时,需要将作废文件移动到回收站
|
allObjs.add(edFileInfo);
|
||||||
}
|
}
|
||||||
needMove2FileSystemFiles.add(edFileInfo);
|
|
||||||
}
|
}
|
||||||
// 处理文件
|
// 处理文件
|
||||||
importFileInfos = importFileInfos.stream()
|
List<EdFileInfo> importFiles = importAllFiles.stream()
|
||||||
.filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code))
|
.filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
Map<String, List<EdFileInfo>> fileInfoMap = new HashMap<>();
|
Map<String, List<EdFileInfo>> fileInfoMap = new HashMap<>();
|
||||||
for (EdFileInfo edFileInfo : importFileInfos) {
|
for (EdFileInfo edFileInfo : importFiles) {
|
||||||
List<EdFileInfo> list = fileInfoMap.getOrDefault(edFileInfo.getFileId(), new ArrayList<>());
|
List<EdFileInfo> list = fileInfoMap.getOrDefault(edFileInfo.getFileId(), new ArrayList<>());
|
||||||
list.add(edFileInfo);
|
list.add(edFileInfo);
|
||||||
fileInfoMap.put(edFileInfo.getFileId(), list);
|
fileInfoMap.put(edFileInfo.getFileId(), list);
|
||||||
}
|
}
|
||||||
|
for (Map.Entry<String, List<EdFileInfo>> entry : fileInfoMap.entrySet()) {
|
||||||
|
String k = entry.getKey();
|
||||||
|
List<EdFileInfo> v = entry.getValue();
|
||||||
|
|
||||||
fileInfoMap.forEach((k, v) -> {
|
|
||||||
List<EdFileInfo> dbFileInfos = this.baseMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class).eq(EdFileInfo::getFileId, k).orderByAsc(EdFileInfo::getFileVersion));
|
List<EdFileInfo> dbFileInfos = this.baseMapper.selectList(Wrappers.lambdaQuery(EdFileInfo.class).eq(EdFileInfo::getFileId, k).orderByAsc(EdFileInfo::getFileVersion));
|
||||||
if (CollUtil.isEmpty(dbFileInfos)) { // 在线上没有找到,则该批文件是从线下上传的
|
if (CollUtil.isEmpty(dbFileInfos)) { // 在线上没有找到,则该批文件是从线下上传的
|
||||||
EdFileInfo fileInfo = v.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).findFirst().get();
|
EdFileInfo fileInfo = v.stream().filter(e -> e.getEffectFlag().equals(EffectFlagEnum.EFFECT.code)).findFirst().get();
|
||||||
resetFileInfoName(fileInfo);
|
resetFileInfoName(fileInfo);
|
||||||
this.saveBatch(v);
|
allObjs.addAll(v);
|
||||||
needMove2FileSystemFiles.addAll(v);
|
|
||||||
} else {
|
} else {
|
||||||
Map<String, EdFileInfo> dbIdMap = dbFileInfos.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
Map<String, EdFileInfo> dbIdMap = dbFileInfos.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
||||||
Map<String, EdFileInfo> importIdMap = v.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
Map<String, EdFileInfo> importIdMap = v.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
||||||
|
|
@ -494,7 +494,6 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
} else { // 该文件是线下上传的。
|
} else { // 该文件是线下上传的。
|
||||||
importFileInfo.setFileVersion(start);
|
importFileInfo.setFileVersion(start);
|
||||||
saveObjs.add(importFileInfo);
|
saveObjs.add(importFileInfo);
|
||||||
needMove2FileSystemFiles.add(importFileInfo);
|
|
||||||
}
|
}
|
||||||
++start;
|
++start;
|
||||||
}
|
}
|
||||||
|
|
@ -506,10 +505,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
int effect = Objects.equals(saveObj.getId(), effectId) ? EffectFlagEnum.EFFECT.code : EffectFlagEnum.NOT_EFFECTIVE.code;
|
int effect = Objects.equals(saveObj.getId(), effectId) ? EffectFlagEnum.EFFECT.code : EffectFlagEnum.NOT_EFFECTIVE.code;
|
||||||
saveObj.setEffectFlag(effect);
|
saveObj.setEffectFlag(effect);
|
||||||
}
|
}
|
||||||
this.saveOrUpdateBatch(saveObjs);
|
allObjs.addAll(saveObjs);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
update2FileSystem(needMove2FileSystemFiles, prjDirPath);
|
this.saveOrUpdateBatch(allObjs);
|
||||||
|
update2FileSystem(importAllFiles, prjDirPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetFileInfoName(EdFileInfo fileInfo) {
|
private void resetFileInfoName(EdFileInfo fileInfo) {
|
||||||
|
|
@ -532,13 +532,18 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update2FileSystem(List<EdFileInfo> needMove2FileSystemFiles, String prjDirPath) {
|
private void update2FileSystem(List<EdFileInfo> needMove2FileSystemFiles, String prjDirPath) {
|
||||||
Map<String, String> idNameMaps = needMove2FileSystemFiles.stream().collect(Collectors.toMap(EdFileInfo::getId, EdFileInfo::getFileName));
|
Map<String, EdFileInfo> maps = needMove2FileSystemFiles.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
|
||||||
List<EdFileInfo> files = needMove2FileSystemFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code)).collect(Collectors.toList());
|
List<EdFileInfo> files = needMove2FileSystemFiles.stream().filter(e -> e.getDataType().equals(EleDataTypeEnum.FILE.code)).collect(Collectors.toList());
|
||||||
for (EdFileInfo edFileInfo : files) {
|
for (EdFileInfo edFileInfo : files) {
|
||||||
String filePath = edFileInfo.getFilePath();
|
String filePath = edFileInfo.getFilePath();
|
||||||
StringBuilder sysFilePath = new StringBuilder();
|
StringBuilder sysFilePath = new StringBuilder();
|
||||||
for (String id : filePath.split(MYSQL_FILE_PATH_SPLIT)) {
|
for (String id : filePath.split(MYSQL_FILE_PATH_SPLIT)) {
|
||||||
sysFilePath.append(idNameMaps.get(id)).append(File.separator);
|
EdFileInfo fileInfo = maps.get(id);
|
||||||
|
if (fileInfo.getDataType().equals(EleDataTypeEnum.FOLDER.code)) {
|
||||||
|
sysFilePath.append(fileInfo.getFileName()).append(File.separator);
|
||||||
|
} else {
|
||||||
|
sysFilePath.append(fileInfo.getFileName()).append(".").append(fileInfo.getFileType()).append(".").append(fileInfo.getFileCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
String destPath = commonService.getEleDataPath() + File.separator + sysFilePath;
|
String destPath = commonService.getEleDataPath() + File.separator + sysFilePath;
|
||||||
String sourcePath = prjDirPath + File.separator + sysFilePath;
|
String sourcePath = prjDirPath + File.separator + sysFilePath;
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,10 @@ public class FileSystemServiceImpl implements FileSystemService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void moveFile(String source, String destination) {
|
public void moveFile(String source, String destination) {
|
||||||
|
String destParentDir = FileUtil.getParent(destination, 1);
|
||||||
|
if (!FileUtil.exist(destParentDir)) {
|
||||||
|
FileUtil.mkdir(destParentDir);
|
||||||
|
}
|
||||||
FileUtil.move(new File(source), new File(destination), true);
|
FileUtil.move(new File(source), new File(destination), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,72 @@
|
||||||
|
//import cn.hutool.core.io.FileUtil;
|
||||||
|
//import cn.hutool.core.util.IdUtil;
|
||||||
|
//import cn.hutool.core.util.ZipUtil;
|
||||||
|
//import cn.hutool.crypto.SecureUtil;
|
||||||
|
//import cn.hutool.crypto.symmetric.AES;
|
||||||
|
//import com.electromagnetic.industry.software.common.exception.BizException;
|
||||||
|
//import com.electromagnetic.industry.software.common.util.UserThreadLocal;
|
||||||
//import com.electromagnetic.industry.software.manage.Application;
|
//import com.electromagnetic.industry.software.manage.Application;
|
||||||
//import com.electromagnetic.industry.software.manage.mapper.CategoryMapper;
|
//import com.electromagnetic.industry.software.manage.service.EdFileInfoService;
|
||||||
//import com.electromagnetic.industry.software.manage.pojo.models.Category;
|
//import com.electromagnetic.industry.software.manage.service.serviceimpl.EdFileInfoServiceImpl;
|
||||||
//import org.junit.jupiter.api.Test;
|
//import org.junit.jupiter.api.Test;
|
||||||
|
//import org.springframework.beans.factory.annotation.Value;
|
||||||
//import org.springframework.boot.test.context.SpringBootTest;
|
//import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
//import org.springframework.core.env.Environment;
|
||||||
//
|
//
|
||||||
|
//import javax.annotation.PostConstruct;
|
||||||
//import javax.annotation.Resource;
|
//import javax.annotation.Resource;
|
||||||
//import java.util.List;
|
//import java.io.File;
|
||||||
|
//import java.io.InputStream;
|
||||||
|
//import java.io.OutputStream;
|
||||||
|
//import java.nio.file.Files;
|
||||||
|
//import java.nio.file.Paths;
|
||||||
//
|
//
|
||||||
//@SpringBootTest(classes = Application.class)
|
//@SpringBootTest(classes = Application.class)
|
||||||
//public class Test1 {
|
//public class Test1 {
|
||||||
//
|
//
|
||||||
// @Resource
|
// @Resource
|
||||||
// private CategoryMapper categoryMapper;
|
// private EdFileInfoServiceImpl edFileInfoServiceImpl;
|
||||||
|
//
|
||||||
|
// @Value("${file.security.passwd}")
|
||||||
|
// private String password;
|
||||||
|
//
|
||||||
|
// private String downloadDataDir = "";
|
||||||
|
// private String uploadDataDir = "";
|
||||||
|
//
|
||||||
|
// @Resource
|
||||||
|
// private Environment environment;
|
||||||
|
//
|
||||||
|
// @PostConstruct
|
||||||
|
// public void init() {
|
||||||
|
// String osName = System.getProperty("os.name").toLowerCase();
|
||||||
|
// uploadDataDir = osName.startsWith("win") ? environment.getProperty("data.upload.windows.tmp.path") : environment.getProperty("data.upload.linux.tmp.path");
|
||||||
|
// downloadDataDir = osName.startsWith("win") ? environment.getProperty("data.download.windows.tmp.path") : environment.getProperty("data.download.linux.tmp.path");
|
||||||
|
// }
|
||||||
//
|
//
|
||||||
// @Test
|
// @Test
|
||||||
// public void test() {
|
// public void testImport() {
|
||||||
// List<Category> categories = categoryMapper.selectAllCategories();
|
// String currentUserId = UserThreadLocal.getUserId();
|
||||||
// System.out.println("categories = " + categories);
|
// String destColibPath = "E:/tmp/C92934test.colib";
|
||||||
|
// String mainName = FileUtil.mainName(destColibPath);
|
||||||
|
// String parentDir = FileUtil.getParent(destColibPath, 1);
|
||||||
|
// String zipDirPath = parentDir + File.separator + mainName + ".zip";
|
||||||
|
// AES aes = SecureUtil.aes(password.getBytes());
|
||||||
|
// try (
|
||||||
|
// InputStream inputStream = Files.newInputStream(Paths.get(destColibPath));
|
||||||
|
// OutputStream outputStream = Files.newOutputStream(Paths.get(zipDirPath));
|
||||||
|
// ) {
|
||||||
|
// aes.decrypt(inputStream, outputStream, true);
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// String info = "文件上传错误";
|
||||||
|
// System.out.println("info = " + info);
|
||||||
|
// }
|
||||||
|
// String uuid = IdUtil.fastSimpleUUID();
|
||||||
|
// String tmpDir = uploadDataDir + currentUserId + File.separator + uuid + File.separator;
|
||||||
|
// ZipUtil.unzip(zipDirPath, tmpDir);
|
||||||
|
//
|
||||||
|
// edFileInfoServiceImpl.update2Database(tmpDir);
|
||||||
|
//
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
//}
|
//}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue