yangys
2025-08-17 9970b367767e5c73f8e0a296615f29770ff4425f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
 
package org.springblade.mdm.program.service;
 
import com.alibaba.fastjson.JSONObject;
import io.netty.util.internal.StringUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.base.BizEntity;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.flow.entity.FlowProgramFile;
import org.springblade.mdm.flow.service.CureFlowService;
import org.springblade.mdm.flow.service.FlowCommonService;
import org.springblade.mdm.flow.service.FlowProgramFileService;
import org.springblade.mdm.program.entity.DncBackFile;
import org.springblade.mdm.program.entity.NcNode;
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.program.vo.DncSendBackFile;
import org.springblade.mdm.utils.EntityUtil;
import org.springblade.mdm.utils.FileContentUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
 
/**
 * DNC回传文件处理服务
 *
 * @author yangys
 */
@Slf4j
@Service
@AllArgsConstructor
public class DNCSendBackService extends BizServiceImpl<NcProgramExchangeMapper, NcProgramExchange> {
    private final CureFlowService cureFlowService;
    private final FlowProgramFileService flowProgramFileService;
    private final NcNodeService ncNodeService;
    private final OssTemplate ossTemplate;
    private final BladeRedis bladeRedis;
    private final FlowCommonService flowCommonService;
    private final DncBackFileService dncBackFileService;
    private String getFileKey(){
        return "dncimpfile-"+ AuthUtil.getUserId();
    }
    /**
     * dnc回传文件上传
     * @param file DNC回传文件
     * @return 压缩包内程序包名的列表
     */
    public List<DncSendBackData> dncSendBackUpload(MultipartFile file) {
        List<DncSendBackData> list;
        if(file == null || file.isEmpty()){
            throw new ServiceException("文件为空");
        }
 
        if(!StringUtils.endsWith(file.getOriginalFilename(),".zip")){
            throw new ServiceException("文件必须为zip包");
        }
        try {
            BladeFile bfile = ossTemplate.putFile(file);//上传,供后续入库使用
            //设置一个缓存,2小时过期
            bladeRedis.setEx(getFileKey(),bfile.getName(), Duration.ofHours(2));
 
            try(InputStream zipFileInputStream = ossTemplate.statFileStream(bfile.getName());) {
                //InputStream zipFileInputStream = file.getInputStream();//test
                list = parseProgramListFromZip(zipFileInputStream);
            }
        } catch (IOException e) {
            log.error("上传dnc回传文件失败",e);
            throw new ServiceException("解析DNC回传数据失败");
        }
        return list;
    }
 
 
    /**
     * 从压缩包 解析回传程序列表,这里解析目录即可,目录就是程序包名
     * @param inputStream 压缩包输入流
     * @return 回传程序列表
     * @throws IOException 文件操作异常
     */
    List<DncSendBackData> parseProgramListFromZip(InputStream inputStream) throws IOException {
        List<DncSendBackData> list = new ArrayList<>();
        Path tempZipFile = createTempFile(inputStream);
 
        List<String> fileEntryNameList = new ArrayList<>();
        List<String> dirEntryNameList = new ArrayList<>();
        try (ZipFile zipFile = new ZipFile(tempZipFile.toFile())) {
 
            Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
            ZipEntry entry;
            //获取所有的entry名称
            while (zipEntries.hasMoreElements()) {
                entry = zipEntries.nextElement();
 
                if(entry.isDirectory()){
                    dirEntryNameList.add(entry.getName());
                }else{
                    fileEntryNameList.add(entry.getName());
                }
            }
 
            NcNode programPackageNode;
            //目录列表,即程序包列表
            for(String entryName : dirEntryNameList){
                DncSendBackData progData = new DncSendBackData();
                String packageName = StringUtils.removeEnd(entryName,"/");
                progData.setProgramName(packageName);
                String statusLine = "";
                Optional<String> optFilename = fileEntryNameList.stream().filter(n -> n.startsWith(packageName)).findFirst();
                if(optFilename.isPresent()){
                    entry = zipFile.getEntry(optFilename.get());
                    InputStream ins = zipFile.getInputStream(entry);
                    progData.setFileBackTime(DateUtil.fromInstant(entry.getLastModifiedTime().toInstant()));
                    statusLine = FileContentUtil.readLineAt(ins,2);
                    if(statusLine.contains("SQ")){
                        //试切
                        programPackageNode = ncNodeService.getLastEditionTryingProgramPackage(packageName);
                    }else if(statusLine.contains("GH")){
                        //固化
                        programPackageNode = ncNodeService.getLastEditionCuredProgramPackage(packageName);
                    }else if(statusLine.contains("PL")){
                        //偏离
                        programPackageNode =ncNodeService.getLastEditionDeviationProgramPackage(packageName);
                    }else{
                        throw new ServiceException("未找到程序文件中的状态注释");
                    }
                    if(programPackageNode != null) {
                        progData.setId(programPackageNode.getId());
                        progData.setProgramNo(programPackageNode.getProgramNo());
 
                        List<String> fileEtriyNames  = fileEntryNameList.stream().filter(n -> n.startsWith(packageName)).toList();
                        List<DncSendBackFile> programFiles = new ArrayList<>();
                        fileEtriyNames.forEach( filePath ->{
                                DncSendBackFile backFile = new DncSendBackFile();
                                backFile.setEntryName(filePath);
                                backFile.setName(StringUtils.removeStart(filePath,entryName));
                                programFiles.add(backFile);
                        });
 
                        progData.setFiles(programFiles);
                        list.add(progData);
                    }else{
                        throw new ServiceException("找不到程序包名:"+packageName+statusLine);
                    }
                }else{
                    throw new ServiceException(entryName+"包下未找到文件"+statusLine);
                }
 
            }
        }
 
        return list;
    }
 
 
    /**
     * 入库回传文件,并启动固化流程
     * @param ids id列表逗号分隔,程序包名 节点的id
     */
    @Transactional
    public void dncFileAccept(String ids) throws IOException {
        List<Long> acceptIdList = Func.toLongList(ids);
        //
        NcProgramExchange exchange;
        String filekey = getFileKey();
        String zipFileName = bladeRedis.get(filekey);
        log.info("filekey={},文件名={}",filekey,zipFileName);
 
        //Map<Long,List<NcNode>> programPackageSubMap = new HashMap<>();
 
        Map<Long,List<FlowProgramFile>> pkgIdFileMap = dealWithBackFile(zipFileName,acceptIdList);
 
        ///List<NcNode> newProgramPackageList = updateNodeDataByDNCBackData(pkgFileName,idList,programPackageSubMap);
        //updateNodeDataByDNCBackData(pkgFileName,idList,programPackageSubMap);
        /*
        log.info("需要启动固化流程的程序包名数量:{}",newProgramPackageList.size());
        for(NcNode pkgNode :newProgramPackageList){
            exchange = new NcProgramExchange();
            exchange.setName(pkgNode.getName());
            exchange.setExchangeType(2);//回传
            exchange.setNcNodeId(pkgNode.getId());
 
            this.save(exchange);
        }*/
        //bladeRedis.del(filekey);
        //this.ossTemplate.removeFile(zipFileName);
        //log.info("删除oss文件:{}",zipFileName);
 
        //cureFlowService.startCure(newProgramPackageList,programPackageSubMap);
        cureFlowService.startCureNew(pkgIdFileMap);
 
    }
 
    /**
     * 处理回传文件
     * @param ossFileName
     * @param acceptIdList
     * @return
     * @throws IOException
     */
    private Map<Long, List<FlowProgramFile>> dealWithBackFile(String ossFileName, List<Long> acceptIdList) throws IOException{
        Map<Long, List<FlowProgramFile>> pkgIdFileMap = new HashMap<>();
 
        InputStream inputStream = this.ossTemplate.statFileStream(ossFileName);
        Path tempZipFile = createTempFile(inputStream);
        List<String> entryNameList = new ArrayList<>();
 
        ZipEntry entry;
        try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while(entries.hasMoreElements()) {
                entry = entries.nextElement();
                entryNameList.add(entry.getName());
            }
            log.info("allentrynames:{}",entryNameList);
 
            List<NcNode> allAcceptPackages =  this.ncNodeService.lambdaQuery().in(NcNode::getId,acceptIdList).list();
            //根据内部文件,读取和分析程序包和程序文件数据
            List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList();
            for(String dir : dirList){
                String programPackageName = StringUtils.removeEnd(dir,"/");
                Optional<NcNode> optPackageNode = allAcceptPackages.stream().filter(node -> StringUtils.equals(node.getName(),programPackageName)).findFirst();
 
                if(optPackageNode.isEmpty()){
                    throw new ServiceException("找不到程序"+programPackageName);
                }
 
                NcNode packageNode = optPackageNode.get();
                if(packageNode.hasCured()) {
                    throw new ServiceException(programPackageName + "已经固化,请勿重复入库。");
                }
 
                //检查是否在审批过程中
                //根据节点信息查询流程
                boolean active = flowCommonService.isProcessInstanceActive(packageNode.getProcessInstanceId());
                if(active){
                    throw new ServiceException(programPackageName+"正在审批中,请勿重复入库。");
                }
 
                //验证都过了,保存dncbackFile
                DncBackFile backFile = new DncBackFile();
                backFile.setNcNodeId(packageNode.getId());
                backFile.setOssName(ossFileName);
                dncBackFileService.save(backFile);
 
                List<FlowProgramFile> flowFiles = new ArrayList<>();
                //查找包下的文件数据,
                entryNameList.stream().filter(s -> s.startsWith(dir)).forEach(entryName -> {
                    log.info("{}下的文件:{}",dir,entryName);
 
                    if(!entryName.endsWith("/")){
                        //实际的文件
                        String fileName = StringUtils.removeStart(entryName,dir);//去除文件名路径部分
 
                        try {
                            FlowProgramFile newFlowFile = new FlowProgramFile();
                            newFlowFile.setProgramName(packageNode.getName());
                            newFlowFile.setProcessInstanceId(null);//先置为空,启动流程后设置该值
                            newFlowFile.setFileType("program");
                            newFlowFile.setName(fileName);
                            InputStream ins = zipFile.getInputStream(zipFile.getEntry(entryName));
                            BladeFile newOssFile = ossTemplate.putFile("mdm",fileName,ins);
                            newFlowFile.setOssName(newOssFile.getName());
 
                            flowFiles.add(newFlowFile);
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
 
                });
 
                pkgIdFileMap.put(packageNode.getId(),flowFiles);
            }
 
        }
 
        return pkgIdFileMap;
    }
 
    /**
     * 更新节点,主要是创建 程序包名 的新版本。
     * @param pkgFileName zip文件名
     * @param programPackageIdList 程序包名 节点的id列表
     * @param programPackageSubMap 新的 程序包节点id -> =文件列表 map,用于回传数据
     * @throws IOException 访问文件异常
     */
    /*
    List<NcNode> updateNodeDataByDNCBackData(String pkgFileName, List<Long> programPackageIdList,Map<Long,List<NcNode>> programPackageSubMap) throws IOException {
        InputStream inputStream = this.ossTemplate.statFileStream(pkgFileName);
        Path tempZipFile = createTempFile(inputStream);
        List<NcNode> newProgramPackageNodeList = new ArrayList<>();
        List<String> entryNameList = new ArrayList<>();
 
        ZipEntry entry;
        try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while(entries.hasMoreElements()) {
                entry = entries.nextElement();
                entryNameList.add(entry.getName());
            }
            log.info("allentrynames:{}",entryNameList);
 
            List<NcNode> allAcceptPackages =  this.ncNodeService.lambdaQuery().in(NcNode::getId,programPackageIdList).list();
            //根据内部文件,读取和分析程序包和程序文件数据
            List<String> dirList = entryNameList.stream().filter(s -> s.endsWith("/")).toList();
            for(String dir : dirList){
                String programPackageName = StringUtils.removeEnd(dir,"/");
                Optional<NcNode> optPackageNode = allAcceptPackages.stream().filter(node -> StringUtils.equals(node.getName(),programPackageName)).findFirst();
 
                if(optPackageNode.isEmpty()){
                    throw new ServiceException("找不到程序"+programPackageName);
                }
 
                NcNode packageNode = optPackageNode.get();
                if(packageNode.hasCured()) {
                    throw new ServiceException(programPackageName + "已经固化,请勿重复入库。");
                }
 
                //检查是否在审批过程中
                //根据节点信息查询流程
                boolean active = flowCommonService.isProcessInstanceActive(packageNode.getProcessInstanceId());
                if(active){
                    throw new ServiceException(programPackageName+"正在审批中,请勿重复入库。");
                }
 
                NcNode newProgramPkg = new NcNode();
                BeanUtils.copyProperties(packageNode, newProgramPkg);
                EntityUtil.clearBaseProperties(newProgramPkg);
                newProgramPkg.setIsLastEdition(1);
                ncNodeService.save(newProgramPkg);
                newProgramPackageNodeList.add(newProgramPkg);
 
                //旧数据更新为老版本
                packageNode.setIsLocked(1);//旧版自动锁定
                packageNode.setIsLastEdition(0);;
                ncNodeService.updateById(packageNode);
 
                //List<FlowProgramFile> newFlowFiles = new ArrayList<>();
                List<NcNode> newProgramNodes = new ArrayList<>();
                //查找包下的文件数据,
                entryNameList.stream().filter(s -> s.startsWith(dir)).forEach(entryName -> {
                    log.info("{}下的文件:{}",dir,entryName);
                    if(!entryName.endsWith("/")){
                        //实际的文件
                        String fileName = StringUtils.removeStart(entryName,dir);//去除文件名路径部分
                        NcNode oldProgramNode = this.ncNodeService.getLastEditionProgramFile(fileName,packageNode.getId());
                        if(oldProgramNode == null){
                            log.info("{}找不到程序文件",entryName);
                            throw new ServiceException(programPackageName+"下找不到程序文件"+fileName);
                        }
 
                        //创建新版本的程序节点
                        NcNode newProgramNode = new NcNode();
                        BeanUtils.copyProperties(oldProgramNode, newProgramNode);
                        EntityUtil.clearBaseProperties(newProgramNode);
                        newProgramNode.setIsLastEdition(1);
                        newProgramNode.setParentId(newProgramPkg.getId());
                        newProgramNode.setParentIds(newProgramPkg.getParentIds()+","+newProgramPkg.getId());
 
                        FlowProgramFile oldFlowFile = flowProgramFileService.getById(newProgramNode.getFlowProgramFileId());
 
                        FlowProgramFile newFlowFile = new FlowProgramFile();
                        BeanUtils.copyProperties(oldFlowFile, newFlowFile);
                        newFlowFile.setProcessInstanceId(null);//先置为空,启动流程后设置该值
                        EntityUtil.clearBaseProperties(newFlowFile);
 
                        try {
                            InputStream ins = zipFile.getInputStream(zipFile.getEntry(entryName));
                            BladeFile newOssFile = ossTemplate.putFile("mdm",fileName,ins);
                            newFlowFile.setOssName(newOssFile.getName());
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                        newProgramNode.setFlowProgramFile(newFlowFile);
                        newProgramNode.setVersionNumber(oldProgramNode.genNewVersionNumber());
                        newProgramNodes.add(newProgramNode);
                        //旧节点处理,咋办?如果导出工控网 重复导出呢?,isLastEdition不用设置了,因为 程序包节点 是新的
                    }
                });
 
                programPackageSubMap.put(newProgramPkg.getId(),newProgramNodes);
            }
 
        }
 
        return newProgramPackageNodeList;
    }
    */
    /**
     * 创建一个临时zip文件
     * @param inputStream 文件的输入流
     * @return path 文件
     * @throws IOException
     */
    Path createTempFile(InputStream inputStream) throws IOException {
        byte[] zipData = FileUtil.copyToByteArray(inputStream);
        Path tempFile = Files.createTempFile("tempzip"+System.currentTimeMillis(), ".zip");
        // 写入字节数据到临时文件
        Files.write(tempFile, zipData, StandardOpenOption.WRITE);
 
        return tempFile;
    }
 
    /**
     * 获取回传文件的内容
     * @param entryName 文件在压缩包内的路径
     * @return 文件内容文本
     */
    public String getEntryFileContent(String entryName) throws IOException {
        String result  = "";
        String zipFileName = bladeRedis.get(getFileKey());
        try(InputStream inputStream = this.ossTemplate.statFileStream(zipFileName);){
            Path tempZipFile = createTempFile(inputStream);
 
            ZipEntry entry;
            try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(tempZipFile.toFile())) {
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    entry = entries.nextElement();
                    if (!entryName.equals(entry.getName())) {
                        continue;
                    }
                    try (InputStream fileIns = zipFile.getInputStream(zipFile.getEntry(entryName))) {
                        ByteArrayInputStream bos = new ByteArrayInputStream(fileIns.readAllBytes());
                        boolean isText = StringUtils.endsWithIgnoreCase(entryName,".txt") || StringUtils.endsWithIgnoreCase(entryName,".nc")|| StringUtils.endsWithIgnoreCase(entryName,".xml");
                        if(!isText) {
                            isText = FileContentUtil.isTextFile(bos);
                        }
                        if (isText) {
                            bos.reset();
                            result = FileContentUtil.getContentFromStream(bos);
                        } else {
                            result = "<非文本文件>";
                        }
                    }
 
                }
            }
        }
 
        return result;
    }
 
}