提交 2d475799 编写于 作者: tangyaolong's avatar tangyaolong

动态模型代码补充

上级 41811b87
package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.lite.domain.DynamicModelConfig;
import cn.ibizlab.core.lite.domain.MetaDynamicModel;
import cn.ibizlab.core.lite.service.IMetaDynamicModelService;
import cn.ibizlab.core.lite.service.impl.MetaDynamicModelServiceImpl;
import cn.ibizlab.util.cache.listener.RedisPublisher;
import cn.ibizlab.util.client.IBZWFFeignClient;
......@@ -8,6 +10,7 @@ import cn.ibizlab.util.dict.StaticDict;
import cn.ibizlab.util.domain.FileItem;
import cn.ibizlab.util.enums.RedisChannelTopic;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.errors.InternalServerErrorException;
import cn.ibizlab.util.helper.FileHelper;
import cn.ibizlab.util.service.FileService;
import com.alibaba.fastjson.JSONArray;
......@@ -29,10 +32,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
* 实体[动态模型] 自定义服务对象
......@@ -47,27 +47,28 @@ public class MetaDynamicModelExService extends MetaDynamicModelServiceImpl {
return com.baomidou.mybatisplus.core.toolkit.ReflectionKit.getSuperClassGenericType(this.getClass().getSuperclass(), 1);
}
@Value("${ibiz.filePath:/app/file/}")
@Value("${ibiz.filePath:d:/app/file/}")
private String fileRoot;
@Autowired
private FileService fileService;
@Autowired
private IMetaDynamicModelService dynamicModelService;
@Autowired
@Lazy
private IBZWFFeignClient wfClient;
@Value("${ibiz.dynamic.publishpath:/app/file/dynamicModel/publicpath}")
@Value("${ibiz.dynamic.publishpath:d:/app/file/dynamicModel/publicpath}")
private String publishPath;
@Autowired
@Lazy
RedisTemplate<String, Object> redisOperations;
@Override
public boolean create(MetaDynamicModel et) {
unzip(et);
return super.create(et);
return unzip(et);
}
/**
......@@ -75,8 +76,11 @@ public class MetaDynamicModelExService extends MetaDynamicModelServiceImpl {
*
* @param et
*/
private void unzip(MetaDynamicModel et) {
private boolean unzip(MetaDynamicModel et) {
try {
File modelFile = null;
JSONObject system = null;
String strModelFile = et.getModelfile();
if (StringUtils.isEmpty(strModelFile))
throw new BadRequestAlertException("模型文件为空", "MetaDynamicModel", "unzip");
......@@ -84,19 +88,39 @@ public class MetaDynamicModelExService extends MetaDynamicModelServiceImpl {
if (!ObjectUtils.isEmpty(items) && items.size() > 1) {
throw new BadRequestAlertException("单次只允许上传一个模型文件", "MetaDynamicModel", "unzip");
}
File modelFile = fileService.getFile(items.get(0).getId());
JSONObject system = null;
String dirPath = this.fileRoot + "ibizutil" + File.separator + items.get(0).getId();
File parent = new File(dirPath);
if (parent.exists() && parent.isDirectory() && parent.listFiles().length > 0) {
modelFile = new File(FileHelper.getLatestFilePath(parent.listFiles()));
} else {
throw new InternalServerErrorException("文件未找到");
}
String unzipPath = modelFile.getParent().replace("ibizutil", "dynamicmodel");
if(".gz".equals(modelFile.getName().substring(modelFile.getName().lastIndexOf(".")))){
if(".tar.gz".equals(modelFile.getName().substring(modelFile.getName().indexOf(".")))){
FileHelper.unTarGz(modelFile, unzipPath, true);
system = getSystem(unzipPath);
}else if(".zip".equals(modelFile.getName().substring(modelFile.getName().lastIndexOf(".")))){
FileHelper.unzip(modelFile, unzipPath, true);
system = getSystem(unzipPath);
}else{
throw new BadRequestAlertException("文件后缀名异常,请以zip或tar.gz作为后缀名", "MetaDynamicModel", "unzip");
}
et.setDynainstid(system.getString("getPSDynaInstId"));
et.setSystemId(system.getString("name"));
super.create(et);
JSONArray array = system.getJSONArray("getPSDynaInsts");
if(!ObjectUtils.isEmpty(array)){
for(int i = 0; i < array.size(); i++) {
MetaDynamicModel model = new MetaDynamicModel();
JSONObject obj = array.getJSONObject(i);
model.setConfigname(obj.get("name").toString());
model.setDynainstid(obj.get("id").toString());
model.setPdynainstid(et.getConfigid());
dynamicModelService.create(model);
}
}
return true;
} catch (IOException e) {
throw new BadRequestAlertException("解析动态模型文件失败," + e, "MetaDynamicModel", "unzip");
}
......
......@@ -7,7 +7,15 @@ import org.springframework.util.StringUtils;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
......@@ -84,13 +92,15 @@ public class FileHelper {
TarEntry entry = null;
String fileName = null;
while((entry = tarIn.getNextEntry()) != null ){
fileName = exp ? entry.getName().replace(root,"") : entry.getName();
if(entry.isDirectory()){
//创建空目录
createDirectory(descDir,fileName);
continue;
}else{
fileName = exp ? entry.getName().replace(root,"") : entry.getName();
if(!root.equals(entry.getName().split("/")[0]) && entry.getName().contains("/")){
fileName = exp ? entry.getName().replace(entry.getName().split("/")[0],"") : entry.getName();
}else {
fileName = exp ? entry.getName().replace(root,"") : entry.getName();
}
//是文件
File tmpFile = new File(descDir + "/" + fileName);
log.debug("正在输出文件:{}",tmpFile.getPath());
......@@ -128,6 +138,36 @@ public class FileHelper {
}
}
/**
* 获取相同路径下最新的文件
* @param files 传入文件组
* @return
*/
public static String getLatestFilePath(File[] files) {
if (files == null) {
return null;
}
BasicFileAttributes attr = null;
Map<Long,String> compareMap = new HashMap();
Long format = 0L;
try {
for (File file : files) {
Path path = file.toPath();
attr = Files.readAttributes(path, BasicFileAttributes.class);
Instant instant = attr.creationTime().toInstant();
Long compareDate = Long.parseLong(DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneId.systemDefault()).format(instant));
format = compareDate > format ? compareDate : format ;
compareMap.put(format,path.toString());
}
} catch (IOException e) {
e.printStackTrace();
}
return compareMap.get(format);
}
/**
* 构建目录
* @param outputDir
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册