|
@@ -1,11 +1,14 @@
|
1
|
1
|
package com.lqkj.link.module.zone.service.impl;
|
2
|
2
|
|
|
3
|
+import cn.hutool.core.util.RandomUtil;
|
3
|
4
|
import com.lqkj.link.message.MessageBean;
|
4
|
5
|
import com.lqkj.link.module.authority.repository.UserInfoRepository;
|
5
|
6
|
import com.lqkj.link.module.zone.domain.MaterialInfo;
|
6
|
7
|
import com.lqkj.link.module.zone.domain.ModelCategory;
|
|
8
|
+import com.lqkj.link.module.zone.domain.ModelInfo;
|
7
|
9
|
import com.lqkj.link.module.zone.repository.MaterialInfoRepository;
|
8
|
10
|
import com.lqkj.link.module.zone.service.MaterialInfoService;
|
|
11
|
+import com.lqkj.link.util.Unzipper;
|
9
|
12
|
import org.checkerframework.checker.units.qual.A;
|
10
|
13
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
14
|
import org.springframework.data.domain.Page;
|
|
@@ -15,10 +18,10 @@ import org.springframework.stereotype.Service;
|
15
|
18
|
import org.springframework.transaction.annotation.Transactional;
|
16
|
19
|
import org.springframework.web.multipart.MultipartFile;
|
17
|
20
|
|
18
|
|
-import java.util.ArrayList;
|
19
|
|
-import java.util.Date;
|
20
|
|
-import java.util.List;
|
21
|
|
-import java.util.Objects;
|
|
21
|
+import java.io.File;
|
|
22
|
+import java.io.IOException;
|
|
23
|
+import java.util.*;
|
|
24
|
+import java.util.stream.Collectors;
|
22
|
25
|
|
23
|
26
|
@Service
|
24
|
27
|
public class MaterialInfoServiceImpl implements MaterialInfoService {
|
|
@@ -56,14 +59,60 @@ public class MaterialInfoServiceImpl implements MaterialInfoService {
|
56
|
59
|
return materialInfoRepository.pageQuery(name, pageable);
|
57
|
60
|
}
|
58
|
61
|
|
|
62
|
+ @Transactional
|
59
|
63
|
@Override
|
60
|
|
- public void batchAdd(MultipartFile file) {
|
61
|
|
- String fileName = file.getOriginalFilename();
|
62
|
|
- String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
|
63
|
|
- if (!suffix.equals("zip")) {
|
64
|
|
- throw new RuntimeException("上传文件类型必须是zip格式的材质文件");
|
|
64
|
+ public void batchAdd(String compressFilePath) {
|
|
65
|
+
|
|
66
|
+ try {
|
|
67
|
+ String unzipPath = "./upload/resource/material" + RandomUtil.randomNumbers(5) + "/";
|
|
68
|
+ Unzipper.unZipFiles(new File("." + compressFilePath), unzipPath);
|
|
69
|
+ Map<String, MaterialInfo> materialMap = materialInfoRepository.findAll().stream().collect(Collectors.toMap(MaterialInfo::getMaterialName, materialInfo -> materialInfo));
|
|
70
|
+
|
|
71
|
+ String modelFolderPath = unzipPath + "model";
|
|
72
|
+ File materialModelDir = new File(modelFolderPath);
|
|
73
|
+ if (materialModelDir.isDirectory()){
|
|
74
|
+ List<MaterialInfo> materialInfoList = new ArrayList<>();
|
|
75
|
+ File[] jsonFiles = materialModelDir.listFiles();
|
|
76
|
+ assert jsonFiles != null;
|
|
77
|
+ for (File jsonFile : jsonFiles) {
|
|
78
|
+ if (jsonFile.isFile() && jsonFile.getName().endsWith(".json")) {
|
|
79
|
+ String modelFileName = jsonFile.getName();
|
|
80
|
+ String modelPath = unzipPath + "model/";
|
|
81
|
+ if (modelFileName.matches(".*[一-龥]+.*")) {
|
|
82
|
+ // 如果包含中文,重命名
|
|
83
|
+ String newFileName = UUID.randomUUID() + ".json";
|
|
84
|
+ modelPath += newFileName;
|
|
85
|
+ boolean rename = jsonFile.renameTo(new File(modelPath));
|
|
86
|
+ System.out.println(rename);
|
|
87
|
+ // 材质图标也要重命名
|
|
88
|
+ File oldIconFile = new File(unzipPath + "icon/" + modelFileName.replace(".json", ".png"));
|
|
89
|
+ if (oldIconFile.exists()) {
|
|
90
|
+ File newIconFile = new File(unzipPath + "icon/" + newFileName.replace(".json", ".png"));
|
|
91
|
+ rename = oldIconFile.renameTo(newIconFile);
|
|
92
|
+ System.out.println(rename);
|
|
93
|
+ }
|
|
94
|
+ } else {
|
|
95
|
+ modelPath += modelFileName;
|
|
96
|
+ }
|
|
97
|
+ if (!materialMap.containsKey(modelFileName.substring(0, modelFileName.lastIndexOf(".")))) {
|
|
98
|
+ materialInfoList.add(new MaterialInfo(null, modelFileName.substring(0, modelFileName.lastIndexOf(".")), modelPath.substring(1),
|
|
99
|
+ modelPath.replace("model", "icon").replace("json", "png").substring(1), modelFileName, new Date()));
|
|
100
|
+ } else {
|
|
101
|
+ MaterialInfo materialInfo = materialMap.get(modelFileName.substring(0, modelFileName.lastIndexOf(".")));
|
|
102
|
+ materialInfo.setMaterialIcon(modelPath.replace("model", "icon").replace("json", "png").substring(1));
|
|
103
|
+ materialInfo.setCompressFileName(modelFileName.substring(1));
|
|
104
|
+ materialInfo.setJsonPath(modelPath);
|
|
105
|
+ materialInfo.setUpdateTime(new Date());
|
|
106
|
+ materialInfoList.add(materialInfo);
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ materialInfoRepository.saveAll(materialInfoList);
|
|
111
|
+ }
|
|
112
|
+ userInfoRepository.updateRefreshStatus();
|
|
113
|
+ } catch (IOException e) {
|
|
114
|
+ throw new RuntimeException(e);
|
65
|
115
|
}
|
66
|
|
- materialInfoRepository.saveAll(new ArrayList<>());
|
67
|
116
|
}
|
68
|
117
|
|
69
|
118
|
|