|
package org.springblade.mdm.program.service;
|
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import lombok.AllArgsConstructor;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.codec.digest.DigestUtils;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
|
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
|
import org.apache.commons.compress.archivers.zip.ZipFile;
|
import org.apache.commons.compress.utils.SeekableInMemoryByteChannel;
|
import org.apache.commons.io.IOUtils;
|
import org.springblade.core.mp.base.BizServiceImpl;
|
import org.springblade.core.mp.support.Condition;
|
import org.springblade.core.mp.support.Query;
|
import org.springblade.core.tool.utils.FileUtil;
|
import org.springblade.core.tool.utils.Func;
|
import org.springblade.core.tool.utils.IoUtil;
|
import org.springblade.mdm.flow.service.CureFlowService;
|
import org.springblade.mdm.program.entity.NcProgram;
|
import org.springblade.mdm.program.entity.NcProgramExchange;
|
import org.springblade.mdm.program.mapper.NcProgramExchangeMapper;
|
import org.springblade.mdm.program.vo.DncSendBackData;
|
import org.springblade.mdm.utils.CustomBinaryReader;
|
import org.springblade.mdm.utils.FileExchangeUtil;
|
import org.springframework.stereotype.Service;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import java.io.*;
|
import java.nio.file.Files;
|
import java.nio.file.Path;
|
import java.nio.file.Paths;
|
import java.time.LocalDateTime;
|
import java.util.*;
|
|
/**
|
* 程序交换(dnc导入/导出)
|
*
|
* @author yangys
|
*/
|
@Slf4j
|
@Service
|
@AllArgsConstructor
|
public class NcProgramExchangeService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
|
private final CureFlowService cureFlowService;
|
private final NcProgramService ncProgramService;
|
/**
|
* dnc回传文件上传(解析后保存入upload表)
|
* @param file DNC回传文件
|
* @return
|
*/
|
public List<DncSendBackData> dncSendBackUpload(MultipartFile file) {
|
List<DncSendBackData> list;
|
try {
|
//String fileName = file.getOriginalFilename();
|
//InputStream zipFileInputStream = FileExchangeUtil.convertFileToZip(file.getInputStream());
|
InputStream zipFileInputStream = file.getInputStream();//test
|
|
byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream);
|
list = parseDncZipFromByteArray(bytes);
|
|
|
} catch (IOException e) {
|
log.error("上传dnc回传文件失败",e);
|
list = Collections.emptyList();
|
}
|
return list;
|
}
|
|
InputStream convertFileToZip(InputStream inputStream) throws IOException {
|
|
File tempFile = createTempFile();
|
FileOutputStream fos = new FileOutputStream(tempFile);
|
CustomBinaryReader.read(inputStream,fos);
|
|
|
FileInputStream dInstream = new FileInputStream(tempFile);
|
|
return dInstream;
|
}
|
|
/**
|
* 创建一个临时文件
|
* @return
|
* @throws IOException
|
*/
|
File createTempFile() throws IOException {
|
Path tempDir = Paths.get(System.getProperty("java.io.tmpdir"));
|
// 在临时目录中创建文件
|
String tfilename = "t"+System.currentTimeMillis();
|
Path tempFile = Files.createTempFile(tempDir, tfilename, ".tmp");
|
System.out.println("创建的临时文件: " + tempFile);
|
return tempFile.toFile();
|
}
|
public static List<DncSendBackData> parseDncZipFromByteArray(byte[] zipData) throws IOException {
|
List<DncSendBackData> list = new ArrayList<>();
|
//List<DncSendBackData> datas = ZipFileDirectoryScanner.getFilesInDirectoryRecursive(zipData, "");
|
|
Map<String,String> fileMd5Map = new HashMap<>();
|
Map<String,DncSendBackData> fileDataMap = new HashMap<>();
|
try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(zipData);
|
ZipFile zipFile = new ZipFile(channel)) {
|
|
ZipArchiveEntry entry;
|
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
|
while (entries.hasMoreElements()) {
|
//while ((entry = zis.getNextZipEntry()) != null) {
|
entry = entries.nextElement();
|
DncSendBackData prog = new DncSendBackData();
|
String entryName = entry.getName();
|
|
if (!entry.isDirectory()) {
|
//直接解析程序的json文件
|
if(entryName.equals(NcProgramExportDNCService.PROGRAM_JSON_FILE)){
|
|
try (InputStream inputStream = zipFile.getInputStream(entry)) {
|
String jsonStr = IoUtil.readToString(inputStream);
|
|
JSONArray jsonArray = JSONArray.parseArray(jsonStr);
|
for(int i=0;i<jsonArray.size();i++){
|
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
DncSendBackData d = new DncSendBackData();
|
d.setProgramName(jsonObject.getString("name"));
|
d.setId(jsonObject.getLong("id"));
|
d.setProgramNo(jsonObject.getString("code"));
|
d.setFileBackTime(LocalDateTime.now());//到达时间
|
|
fileDataMap.put(d.getProgramName(),d);
|
list.add(d);
|
}
|
|
}
|
}else{
|
try (InputStream inputStream = zipFile.getInputStream(entry)) {
|
fileMd5Map.put(entryName,DigestUtils.md5Hex(inputStream));//获取文件MD5
|
}
|
|
}
|
System.out.println("文件名: " + entry.getName());
|
System.out.println("大小: " + entry.getSize());
|
|
// 读取文件内容到字节数组
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
}
|
|
}
|
|
}
|
//设置md5值
|
fileDataMap.forEach((k,v)->{
|
if(fileMd5Map.containsKey(k)){
|
v.setMd5(fileMd5Map.get(k));
|
}
|
});
|
return list;
|
}
|
|
|
/**
|
* 入库回传文件,并启动固化流程
|
* @param ids id列表逗号分隔
|
* @return
|
*/
|
public void dncFileAccept(String ids) {
|
List<Long> idList = Func.toLongList(ids);
|
List<NcProgram> progList = ncProgramService.listByIds(idList);
|
NcProgramExchange exchange;
|
//NcProgram program;
|
//NcNode programNode;
|
|
for(NcProgram prog:progList){
|
exchange = new NcProgramExchange();
|
exchange.setName(prog.getName());
|
exchange.setExchangeType(2);//回传
|
exchange.setNcProgramId(prog.getId());
|
|
this.save(exchange);
|
|
}
|
|
cureFlowService.startCure(progList);
|
|
}
|
}
|