提交 | 用户 | 时间
|
449017
|
1 |
package com.iailab.module.model.mpk.service.impl; |
D |
2 |
|
|
3 |
import cn.hutool.core.io.FileUtil; |
|
4 |
import cn.hutool.core.util.RuntimeUtil; |
|
5 |
import cn.hutool.core.util.ZipUtil; |
|
6 |
import com.alibaba.fastjson.JSON; |
8b3ee3
|
7 |
import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
449017
|
8 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
D |
9 |
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
10 |
import com.iailab.framework.common.page.PageData; |
|
11 |
import com.iailab.framework.common.service.impl.BaseServiceImpl; |
|
12 |
import com.iailab.framework.common.util.date.DateUtils; |
|
13 |
import com.iailab.framework.common.util.object.ConvertUtils; |
|
14 |
import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
|
15 |
import com.iailab.module.infra.api.config.ConfigApi; |
|
16 |
import com.iailab.module.model.mpk.common.MdkConstant; |
|
17 |
import com.iailab.module.model.mpk.common.utils.GenUtils; |
|
18 |
import com.iailab.module.model.mpk.dao.MpkFileDao; |
1fea3e
|
19 |
import com.iailab.module.model.mpk.dto.GeneratorCodeHistoryDTO; |
D |
20 |
import com.iailab.module.model.mpk.dto.ModelMethodDTO; |
|
21 |
import com.iailab.module.model.mpk.dto.MpkFileDTO; |
|
22 |
import com.iailab.module.model.mpk.dto.ProjectPackageHistoryDTO; |
|
23 |
import com.iailab.module.model.mpk.entity.GeneratorCodeHistoryEntity; |
|
24 |
import com.iailab.module.model.mpk.entity.MpkFileEntity; |
|
25 |
import com.iailab.module.model.mpk.entity.ProjectPackageHistoryModelEntity; |
449017
|
26 |
import com.iailab.module.model.mpk.service.*; |
D |
27 |
import lombok.extern.slf4j.Slf4j; |
|
28 |
import org.apache.commons.io.FileUtils; |
|
29 |
import org.apache.commons.lang3.StringUtils; |
|
30 |
import org.apache.velocity.VelocityContext; |
|
31 |
import org.apache.velocity.app.Velocity; |
|
32 |
import org.springframework.beans.factory.annotation.Autowired; |
a27351
|
33 |
import org.springframework.beans.factory.annotation.Value; |
449017
|
34 |
import org.springframework.stereotype.Service; |
D |
35 |
import org.springframework.util.CollectionUtils; |
|
36 |
import org.springframework.web.multipart.MultipartFile; |
|
37 |
|
f7932f
|
38 |
import java.io.*; |
449017
|
39 |
import java.util.*; |
D |
40 |
import java.util.stream.Collectors; |
|
41 |
|
|
42 |
/** |
|
43 |
* @author PanZhibao |
|
44 |
* @Description |
|
45 |
* @createTime 2024年08月14日 |
|
46 |
*/ |
|
47 |
@Slf4j |
|
48 |
@Service |
0255ea
|
49 |
public class MpkFileServiceImpl extends BaseServiceImpl<MpkFileDao, MpkFileEntity> implements MpkFileService { |
449017
|
50 |
|
D |
51 |
@Autowired |
|
52 |
private GeneratorCodeHistoryService generatorCodeHistoryService; |
8b3ee3
|
53 |
|
449017
|
54 |
@Autowired |
D |
55 |
private ProjectPackageHistoryService projectPackageHistoryService; |
8b3ee3
|
56 |
|
449017
|
57 |
@Autowired |
D |
58 |
private ModelMethodService modelMethodService; |
8b3ee3
|
59 |
|
449017
|
60 |
@Autowired |
D |
61 |
private ProjectPackageHistoryModelService projectPackageHistoryModelService; |
|
62 |
|
|
63 |
@Autowired |
|
64 |
private ConfigApi configApi; |
|
65 |
|
1fea3e
|
66 |
@Value("${mpk.bak-file-path}") |
a27351
|
67 |
private String mpkBakFilePath; |
1fea3e
|
68 |
|
D |
69 |
@Value("${mpk.bak-resources}") |
|
70 |
private String mpkResources; |
449017
|
71 |
|
38e87c
|
72 |
/*@PostConstruct |
449017
|
73 |
public void init() { |
D |
74 |
mpkBakFilePath = configApi.getConfigValueByKey("mpkBakFilePath").getCheckedData(); |
38e87c
|
75 |
}*/ |
449017
|
76 |
|
D |
77 |
@Override |
|
78 |
public PageData<MpkFileDTO> page(Map<String, Object> params) { |
|
79 |
IPage<MpkFileEntity> page = baseDao.selectPage( |
|
80 |
getPage(params, "create_date", false), |
|
81 |
getWrapper(params) |
|
82 |
); |
|
83 |
|
|
84 |
return getPageData(page, MpkFileDTO.class); |
|
85 |
} |
|
86 |
|
|
87 |
@Override |
|
88 |
public List<MpkFileDTO> list(Map<String, Object> params) { |
|
89 |
List<MpkFileEntity> entityList = baseDao.selectList(getWrapper(params).orderByDesc("create_date")); |
|
90 |
|
|
91 |
return ConvertUtils.sourceToTarget(entityList, MpkFileDTO.class); |
|
92 |
} |
|
93 |
|
8b3ee3
|
94 |
private QueryWrapper<MpkFileEntity> getWrapper(Map<String, Object> params) { |
449017
|
95 |
String pyName = (String) params.get("pyName"); |
D |
96 |
String pyType = (String) params.get("pyType"); |
|
97 |
String remark = (String) params.get("remark"); |
d8db4b
|
98 |
String label = (String) params.get("label"); |
449017
|
99 |
|
D |
100 |
QueryWrapper<MpkFileEntity> wrapper = new QueryWrapper<>(); |
|
101 |
wrapper.like(StringUtils.isNotBlank(pyName), "py_name", pyName) |
|
102 |
.eq(StringUtils.isNotBlank(pyType), "py_type", pyType) |
|
103 |
.like(StringUtils.isNotBlank(remark), "remark", remark); |
d8db4b
|
104 |
|
潘 |
105 |
if (StringUtils.isNotBlank(label)) { |
|
106 |
wrapper.and(w -> { |
|
107 |
w.eq(StringUtils.isNotBlank(label),"menu_name", label) |
|
108 |
.or().eq(StringUtils.isNotBlank(label),"group_name", label); |
|
109 |
}); |
|
110 |
} |
449017
|
111 |
return wrapper; |
D |
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public MpkFileDTO get(String id) { |
|
116 |
MpkFileDTO entity = baseDao.get(id); |
|
117 |
|
|
118 |
return entity; |
|
119 |
} |
|
120 |
|
|
121 |
@Override |
8b3ee3
|
122 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
123 |
public void save(MpkFileDTO dto) { |
D |
124 |
MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); |
8b3ee3
|
125 |
entity.setId(UUID.randomUUID().toString()); |
ed8ba3
|
126 |
entity.setPkgName(dto.getPkgName().trim()); |
潘 |
127 |
entity.setFilePath(dto.getFilePath().trim()); |
449017
|
128 |
entity.setCreator(SecurityFrameworkUtils.getLoginUserId()); |
D |
129 |
entity.setCreateDate(new Date()); |
|
130 |
insert(entity); |
8b3ee3
|
131 |
modelMethodService.insertList(dto.getModelMethods(), entity.getId()); |
449017
|
132 |
} |
D |
133 |
|
|
134 |
@Override |
8b3ee3
|
135 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
136 |
public void update(MpkFileDTO dto) { |
D |
137 |
MpkFileEntity entity = ConvertUtils.sourceToTarget(dto, MpkFileEntity.class); |
|
138 |
entity.setUpdater(SecurityFrameworkUtils.getLoginUserId()); |
|
139 |
entity.setUpdateDate(new Date()); |
|
140 |
updateById(entity); |
8b3ee3
|
141 |
modelMethodService.deleteModelMethod(entity.getId()); |
潘 |
142 |
modelMethodService.insertList(dto.getModelMethods(), entity.getId()); |
449017
|
143 |
} |
D |
144 |
|
|
145 |
@Override |
8b3ee3
|
146 |
@DSTransactional(rollbackFor = Exception.class) |
449017
|
147 |
public void delete(String id) { |
D |
148 |
|
|
149 |
//删除源文件 |
|
150 |
MpkFileEntity mpkFileEntity = selectById(id); |
|
151 |
if (StringUtils.isNoneBlank(mpkFileEntity.getFilePath())) { |
|
152 |
File mpkFile = new File(mpkFileEntity.getFilePath()); |
|
153 |
if (mpkFile.exists()) { |
|
154 |
mpkFile.delete(); |
|
155 |
log.info("删除源文件备份文件:" + mpkFileEntity.getFilePath()); |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
//删除备份文件 |
8b3ee3
|
160 |
Map<String, Object> map1 = new HashMap<>(); |
潘 |
161 |
map1.put("mdkId", id); |
449017
|
162 |
List<GeneratorCodeHistoryDTO> list = generatorCodeHistoryService.list(map1); |
D |
163 |
list.forEach(e -> { |
|
164 |
File file = new File(e.getFilePath()); |
|
165 |
if (file.exists()) { |
|
166 |
file.delete(); |
|
167 |
log.info("删除生成代码备份文件:" + e.getFilePath()); |
|
168 |
} |
|
169 |
}); |
|
170 |
|
0255ea
|
171 |
//删除 会级联删除掉关联表 |
D |
172 |
deleteById(id); |
449017
|
173 |
} |
D |
174 |
|
|
175 |
@Override |
|
176 |
public byte[] generatorCode(String id, String remark, String zipFileName) { |
|
177 |
MpkFileDTO entity = baseDao.get(id); |
|
178 |
//生成代码 |
1fea3e
|
179 |
//设置velocity资源加载器 |
D |
180 |
Properties prop = new Properties(); |
|
181 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
182 |
Velocity.init(prop); |
449017
|
183 |
|
1fea3e
|
184 |
//封装模板数据 |
D |
185 |
Map<String, Object> map = new HashMap<>(); |
|
186 |
map.put("pkgName",entity.getPkgName()); |
|
187 |
map.put("modelMethods",entity.getModelMethods()); |
|
188 |
map.put("pyName",entity.getPyName()); |
|
189 |
map.put("pyModule",entity.getPyModule()); |
|
190 |
|
|
191 |
VelocityContext dataContext = new VelocityContext(map); |
|
192 |
|
|
193 |
//临时文件夹 |
|
194 |
File dirPath = new File("D:/DLUT/tmp/"); |
|
195 |
dirPath.deleteOnExit(); |
|
196 |
dirPath.mkdirs(); |
|
197 |
|
|
198 |
List<String> javaFilePaths = new ArrayList<>(); |
|
199 |
List<String> cppFilePaths = new ArrayList<>(); |
|
200 |
|
|
201 |
//生成java文件 |
|
202 |
File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java"); |
|
203 |
GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile); |
|
204 |
javaFilePaths.add(javaFile.getAbsolutePath()); |
|
205 |
|
|
206 |
//生成Impl.java文件 |
|
207 |
File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java"); |
|
208 |
GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile); |
|
209 |
javaFilePaths.add(implJavaFile.getAbsolutePath()); |
|
210 |
|
|
211 |
//生成.cpp文件 |
|
212 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp"); |
|
213 |
GenUtils.drawTemplate("cpp.vm", dataContext, cppFile); |
|
214 |
cppFilePaths.add(cppFile.getAbsolutePath()); |
|
215 |
|
|
216 |
//生成Jni.cpp文件 |
|
217 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp"); |
|
218 |
GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile); |
|
219 |
cppFilePaths.add(jniCppFile.getAbsolutePath()); |
|
220 |
|
|
221 |
//生成.h文件 |
|
222 |
File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h"); |
|
223 |
GenUtils.drawTemplate("h.vm", dataContext, hFile); |
|
224 |
|
|
225 |
//生成Jni.h文件 |
|
226 |
File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
|
227 |
GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
|
228 |
|
|
229 |
// 资源文件 |
|
230 |
FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); |
|
231 |
|
|
232 |
//生成dll文件 |
|
233 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; |
|
234 |
createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
|
235 |
//备份dll文件,用于后续运行 |
|
236 |
String dllBakPath = mpkBakFilePath + File.separator + MdkConstant.DLL + File.separator + entity.getPyName() + ".dll"; |
|
237 |
FileUtil.mkParentDirs(dllBakPath); |
|
238 |
FileUtil.copy(dllSavePath, dllBakPath, true); |
|
239 |
|
|
240 |
//utils + env java文件 |
|
241 |
File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java"); |
|
242 |
FileUtil.mkParentDirs(utilsJavaFile); |
|
243 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
|
244 |
javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
|
245 |
File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java"); |
|
246 |
FileUtil.mkParentDirs(envJavaFile); |
|
247 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
|
248 |
javaFilePaths.add(envJavaFile.getAbsolutePath()); |
|
249 |
// 生成class文件 |
|
250 |
createClassFile(javaFilePaths); |
|
251 |
// 删除java源文件 |
|
252 |
deleteJavaFile(javaFilePaths); |
|
253 |
// 打jar包 |
|
254 |
String jarSavePath = pkgJar(dirPath.getAbsolutePath()); |
|
255 |
//备份jar文件,用于后续运行 |
|
256 |
String jarBakPath = mpkBakFilePath + File.separator + MdkConstant.JAR + File.separator + entity.getPyName() + ".jar"; |
|
257 |
FileUtil.mkParentDirs(dllBakPath); |
|
258 |
FileUtil.copy(jarSavePath, jarBakPath, true); |
|
259 |
// 打zip包 |
|
260 |
String zipPath = mpkBakFilePath + File.separator + zipFileName; |
|
261 |
ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); |
|
262 |
byte[] bytes = FileUtil.readBytes(zipPath); |
449017
|
263 |
|
D |
264 |
//代码生成历史记录 |
|
265 |
GeneratorCodeHistoryEntity historyEntity = new GeneratorCodeHistoryEntity(); |
|
266 |
historyEntity.setId(UUID.randomUUID().toString()); |
|
267 |
historyEntity.setMdkId(id); |
1fea3e
|
268 |
historyEntity.setFileName(zipFileName); |
D |
269 |
historyEntity.setFilePath(zipPath); |
449017
|
270 |
historyEntity.setRemark(remark); |
D |
271 |
historyEntity.setCreateTime(new Date()); |
|
272 |
generatorCodeHistoryService.insert(historyEntity); |
|
273 |
|
1fea3e
|
274 |
// 删除临时文件 |
3009e2
|
275 |
try { |
D |
276 |
FileUtils.deleteDirectory(dirPath); |
|
277 |
} catch (IOException e) { |
|
278 |
throw new RuntimeException("删除临时文件异常"); |
|
279 |
} |
1fea3e
|
280 |
return bytes; |
449017
|
281 |
} |
D |
282 |
|
|
283 |
@Override |
8b3ee3
|
284 |
@DSTransactional(rollbackFor = Exception.class) |
f7932f
|
285 |
public byte[] packageModel(List<String> ids, String projectId, String projectName, String zipFileName, String logs, String version) throws IOException, InterruptedException { |
449017
|
286 |
List<MpkFileDTO> entities = baseDao.selectByIds(ids); |
D |
287 |
|
|
288 |
//模板数据 |
1fea3e
|
289 |
// Map<String, Object> map = new HashMap<>(); |
D |
290 |
// map.put("entities", entities); |
|
291 |
// VelocityContext context = new VelocityContext(map); |
449017
|
292 |
|
D |
293 |
//临时文件夹 |
1fea3e
|
294 |
File dirPath = new File("D:/DLUT/tmp/"); |
D |
295 |
dirPath.deleteOnExit(); |
|
296 |
dirPath.mkdirs(); |
449017
|
297 |
|
D |
298 |
//设置velocity资源加载器 |
|
299 |
Properties prop = new Properties(); |
|
300 |
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); |
|
301 |
Velocity.init(prop); |
|
302 |
|
a27351
|
303 |
//生成menu.xml文件 |
2cc235
|
304 |
LinkedHashMap<String, LinkedHashMap<String, List<MpkFileDTO>>> collect = entities.stream().collect(Collectors.groupingBy(MpkFileDTO::getMenuName, LinkedHashMap::new, Collectors.groupingBy(e -> StringUtils.isNotBlank(e.getGroupName()) ? e.getGroupName() : "default_group",LinkedHashMap::new,Collectors.toList()))); |
a27351
|
305 |
Map<String, Object> map1 = new HashMap<>(); |
8b3ee3
|
306 |
map1.put("collects", collect); |
c7009e
|
307 |
File xmlFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "menu.xml"); |
8b3ee3
|
308 |
GenUtils.drawTemplate("menu.xml.vm", new VelocityContext(map1), xmlFile); |
449017
|
309 |
|
D |
310 |
List<String> javaFilePaths = new ArrayList<>(); |
f7932f
|
311 |
List<String> cppFilePaths = new ArrayList<>(); |
449017
|
312 |
|
D |
313 |
//生成java文件 |
|
314 |
for (MpkFileDTO entity : entities) { |
|
315 |
//封装模板数据 |
|
316 |
Map<String, Object> data = new HashMap<>(); |
8b3ee3
|
317 |
data.put("pkgName", entity.getPkgName()); |
潘 |
318 |
data.put("modelMethods", entity.getModelMethods()); |
|
319 |
data.put("pyName", entity.getPyName()); |
|
320 |
data.put("pyModule", entity.getPyModule()); |
449017
|
321 |
VelocityContext dataContext = new VelocityContext(data); |
D |
322 |
//生成java文件 |
|
323 |
File javaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + entity.getPyName() + ".java"); |
8b3ee3
|
324 |
GenUtils.drawTemplate("abstract.java.vm", dataContext, javaFile); |
449017
|
325 |
javaFilePaths.add(javaFile.getAbsolutePath()); |
D |
326 |
|
|
327 |
//生成Impl.java文件 |
|
328 |
File implJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + entity.getPkgName().replace(".", File.separator) + File.separator + MdkConstant.IMPL + File.separator + entity.getPyName() + "Impl.java"); |
8b3ee3
|
329 |
GenUtils.drawTemplate("impl.java.vm", dataContext, implJavaFile); |
449017
|
330 |
javaFilePaths.add(implJavaFile.getAbsolutePath()); |
D |
331 |
|
f7932f
|
332 |
//生成.cpp文件 |
D |
333 |
File cppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".cpp"); |
|
334 |
GenUtils.drawTemplate("cpp.vm", dataContext, cppFile); |
|
335 |
cppFilePaths.add(cppFile.getAbsolutePath()); |
|
336 |
|
|
337 |
//生成Jni.cpp文件 |
|
338 |
File jniCppFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.cpp"); |
|
339 |
GenUtils.drawTemplate("Jni.cpp.vm", dataContext, jniCppFile); |
|
340 |
cppFilePaths.add(jniCppFile.getAbsolutePath()); |
|
341 |
|
|
342 |
//生成.h文件 |
|
343 |
File hFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + ".h"); |
|
344 |
GenUtils.drawTemplate("h.vm", dataContext, hFile); |
|
345 |
|
|
346 |
//生成Jni.h文件 |
|
347 |
File jniHFile = new File(dirPath.getAbsolutePath() + File.separator + MdkConstant.C + File.separator + entity.getPyName() + "Jni.h"); |
|
348 |
GenUtils.drawTemplate("Jni.h.vm", dataContext, jniHFile); |
|
349 |
|
449017
|
350 |
// 添加python源文件 |
c7009e
|
351 |
String pyFilePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.ALGS + File.separator + entity.getPyModule().replace(".", File.separator) + File.separator + entity.getPyName() + ".pyd"; |
449017
|
352 |
FileUtil.mkParentDirs(pyFilePath); |
8b3ee3
|
353 |
FileUtil.copy(entity.getFilePath(), pyFilePath, true); |
449017
|
354 |
} |
D |
355 |
|
1fea3e
|
356 |
// 资源文件 |
D |
357 |
FileUtil.copy(mpkResources + File.separator + "libs", dirPath.getAbsolutePath(), true); |
c7009e
|
358 |
|
f7932f
|
359 |
//生成dll文件 |
c7009e
|
360 |
String dllSavePath = dirPath.getAbsolutePath() + File.separator + MdkConstant.LIBS + File.separator + "IAIL.MDK.Mid.Jni.dll"; |
f7932f
|
361 |
createDllFile(dirPath.getAbsolutePath(),cppFilePaths,dllSavePath); |
D |
362 |
|
449017
|
363 |
//utils + env java文件 |
c7009e
|
364 |
File utilsJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "utils" + File.separator + "AlgsUtils.java"); |
449017
|
365 |
FileUtil.mkParentDirs(utilsJavaFile); |
1fea3e
|
366 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/utils/AlgsUtils.java", utilsJavaFile.getAbsolutePath(), true); |
449017
|
367 |
javaFilePaths.add(utilsJavaFile.getAbsolutePath()); |
c7009e
|
368 |
File envJavaFile = new File(dirPath.getAbsolutePath() + File.separator + "IAILMDK" + File.separator + "iail" + File.separator + "mdk" + File.separator + "model" + File.separator + "common" + File.separator + "Environment.java"); |
449017
|
369 |
FileUtil.mkParentDirs(envJavaFile); |
1fea3e
|
370 |
FileUtil.copy(mpkResources + File.separator +"IAILMDK/common/Environment.java", envJavaFile.getAbsolutePath(), true); |
449017
|
371 |
javaFilePaths.add(envJavaFile.getAbsolutePath()); |
D |
372 |
// 生成class文件 |
|
373 |
createClassFile(javaFilePaths); |
|
374 |
// 删除java源文件 |
|
375 |
deleteJavaFile(javaFilePaths); |
|
376 |
// 打jar包 |
|
377 |
pkgJar(dirPath.getAbsolutePath()); |
|
378 |
// 本次更新日志 |
|
379 |
ProjectPackageHistoryDTO dto = new ProjectPackageHistoryDTO(); |
|
380 |
String historyId = UUID.randomUUID().toString(); |
|
381 |
dto.setId(historyId); |
|
382 |
dto.setProjectId(projectId); |
|
383 |
dto.setFileName(zipFileName); |
f7932f
|
384 |
dto.setLog(logs); |
449017
|
385 |
dto.setVersion(version); |
D |
386 |
dto.setModelNames(entities.stream().map(MpkFileDTO::getPyName).collect(Collectors.joining(","))); |
|
387 |
dto.setCreateTime(new Date()); |
|
388 |
// 生成更新日志 |
8b3ee3
|
389 |
createLog(projectId, projectName, dirPath.getAbsolutePath(), dto, version); |
449017
|
390 |
// 打zip包 |
D |
391 |
String zipPath = mpkBakFilePath + File.separator + zipFileName; |
8b3ee3
|
392 |
ZipUtil.zip(dirPath.getAbsolutePath(), zipPath); |
449017
|
393 |
byte[] bytes = FileUtil.readBytes(zipPath); |
D |
394 |
// 记录打包日志 |
|
395 |
dto.setFilePath(zipPath); |
|
396 |
projectPackageHistoryService.save(dto); |
|
397 |
// 插入打包历史-模型关联表 |
|
398 |
List<ProjectPackageHistoryModelEntity> historyModelList = new ArrayList<>(entities.size()); |
|
399 |
entities.forEach(e -> { |
|
400 |
ProjectPackageHistoryModelEntity entity = new ProjectPackageHistoryModelEntity(); |
|
401 |
entity.setId(UUID.randomUUID().toString()); |
|
402 |
entity.setProjectId(projectId); |
|
403 |
entity.setPackageHistoryId(historyId); |
|
404 |
entity.setPyName(e.getPyName()); |
067d7a
|
405 |
entity.setPyChineseName(e.getPyChineseName()); |
449017
|
406 |
entity.setPkgName(e.getPkgName()); |
D |
407 |
entity.setPyModule(e.getPyModule()); |
|
408 |
entity.setRemark(e.getRemark()); |
0255ea
|
409 |
List<ModelMethodDTO> methods = e.getModelMethods(); |
449017
|
410 |
if (!CollectionUtils.isEmpty(methods)) { |
D |
411 |
entity.setMethodInfo(JSON.toJSONString(methods)); |
|
412 |
} |
|
413 |
historyModelList.add(entity); |
|
414 |
}); |
|
415 |
projectPackageHistoryModelService.insertBatch(historyModelList); |
|
416 |
// 删除临时文件 |
|
417 |
FileUtils.deleteDirectory(dirPath); |
|
418 |
return bytes; |
|
419 |
} |
|
420 |
|
1fea3e
|
421 |
private void createDllFile(String dirPath, List<String> cppFilePaths, String dllSavePath) { |
D |
422 |
try { |
|
423 |
String paths = cppFilePaths.stream().collect(Collectors.joining(" ")); |
|
424 |
String command = "cl /LD " + paths + " /EHsc /o " + dllSavePath + " /link " + "IAIL.MDK.Mid.Windows.lib"; |
|
425 |
log.info("执行cmd命令生成dll:" + command); |
|
426 |
ProcessBuilder builder = new ProcessBuilder("cmd", "/c", command); |
|
427 |
builder.directory(new File(dirPath + File.separator + MdkConstant.LIBS)); |
|
428 |
Process process = builder.start(); |
|
429 |
// 获取命令输出结果 |
|
430 |
InputStream inputStream = process.getInputStream(); |
|
431 |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "GBK")); // 设置编码为GBK |
|
432 |
String line; |
|
433 |
while ((line = reader.readLine()) != null) { |
|
434 |
log.info(line); |
|
435 |
} |
|
436 |
// 等待命令执行完成 |
|
437 |
process.waitFor(); |
|
438 |
} catch (Exception e) { |
|
439 |
throw new RuntimeException("执行cmd命令生成dll异常"); |
f7932f
|
440 |
} |
D |
441 |
} |
|
442 |
|
449017
|
443 |
@Override |
8b3ee3
|
444 |
public Map<String, String> savePyFile(MultipartFile file) throws IOException { |
449017
|
445 |
File dir = new File(mpkBakFilePath); |
D |
446 |
if (!dir.exists()) { |
|
447 |
dir.mkdirs(); |
|
448 |
} |
|
449 |
String fileName = file.getOriginalFilename(); |
1fea3e
|
450 |
String fileSuffix = fileName.substring(fileName.lastIndexOf(".")); |
D |
451 |
File saveFile = new File(dir.getAbsolutePath() + File.separator + UUID.randomUUID() + fileSuffix); |
|
452 |
|
449017
|
453 |
saveFile.createNewFile(); |
D |
454 |
// 保存 |
|
455 |
file.transferTo(saveFile); |
|
456 |
|
8b3ee3
|
457 |
Map<String, String> result = new HashMap<>(2); |
潘 |
458 |
result.put("filePath", saveFile.getAbsolutePath()); |
449017
|
459 |
result.put("fileName", fileName); |
D |
460 |
|
|
461 |
return result; |
|
462 |
} |
|
463 |
|
8b3ee3
|
464 |
private void createLog(String projectId, String projectName, String dirPath, ProjectPackageHistoryDTO dto, String version) throws IOException { |
潘 |
465 |
Map<String, Object> map = new HashMap<>(); |
|
466 |
map.put("projectId", projectId); |
449017
|
467 |
List<ProjectPackageHistoryDTO> list = projectPackageHistoryService.list(map); |
D |
468 |
list.add(dto); |
|
469 |
// 按照日期分组再排序 |
|
470 |
HashMap<String, List<ProjectPackageHistoryDTO>> dataMap = list.stream().collect( |
|
471 |
Collectors.groupingBy(e -> DateUtils.format(e.getCreateTime(), DateUtils.DATE_PATTERN_POINT), |
8b3ee3
|
472 |
LinkedHashMap::new, |
潘 |
473 |
Collectors.collectingAndThen(Collectors.toList(), e -> e.stream().sorted(Comparator.comparing(ProjectPackageHistoryDTO::getCreateTime)).collect(Collectors.toList())))); |
449017
|
474 |
|
D |
475 |
Map<String, Object> data = new HashMap<>(); |
8b3ee3
|
476 |
data.put("dataMap", dataMap); |
潘 |
477 |
data.put("projectName", projectName); |
|
478 |
data.put("version", version); |
|
479 |
data.put("now", DateUtils.format(new Date(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); |
449017
|
480 |
|
D |
481 |
File logFile = new File(dirPath + File.separator + "更新日志.txt"); |
8b3ee3
|
482 |
GenUtils.drawTemplate("log.txt.vm", data, logFile); |
449017
|
483 |
} |
D |
484 |
|
3009e2
|
485 |
private String pkgJar(String dirPath) { |
D |
486 |
try { |
|
487 |
String jarSavePath = dirPath + File.separator + MdkConstant.LIBS + File.separator + "IAILMDK.jar"; |
|
488 |
StringBuilder sb = new StringBuilder(); |
|
489 |
sb.append("jar -cvf"); |
|
490 |
sb.append(" ").append(jarSavePath); |
|
491 |
sb.append(" -C ").append(dirPath).append(File.separator).append("IAILMDK").append(File.separator).append(" ."); |
|
492 |
log.info("执行cmd命令打jar包:" + sb); |
|
493 |
Process process = RuntimeUtil.exec(sb.toString()); |
|
494 |
process.waitFor(); |
|
495 |
return jarSavePath; |
|
496 |
} catch (InterruptedException e) { |
|
497 |
throw new RuntimeException("执行cmd命令打jar包异常"); |
|
498 |
} |
449017
|
499 |
} |
D |
500 |
|
|
501 |
private void deleteJavaFile(List<String> javaFilePaths) { |
|
502 |
for (String javaFilePath : javaFilePaths) { |
|
503 |
FileUtil.del(javaFilePath); |
|
504 |
} |
|
505 |
} |
|
506 |
|
3009e2
|
507 |
private void createClassFile(List<String> javaFilePaths){ |
D |
508 |
try { |
|
509 |
StringBuilder sb = new StringBuilder(); |
|
510 |
sb.append("javac -encoding utf-8"); |
|
511 |
for (String path : javaFilePaths) { |
|
512 |
sb.append(" ").append(path); |
|
513 |
} |
|
514 |
log.info("执行cmd命令生成class:" + sb); |
|
515 |
Process process = RuntimeUtil.exec(sb.toString()); |
|
516 |
process.waitFor(); |
|
517 |
} catch (InterruptedException e) { |
|
518 |
throw new RuntimeException("执行cmd命令生成class异常"); |
449017
|
519 |
} |
D |
520 |
} |
|
521 |
} |