yangys
2024-03-29 153cc3fd4ef015a8b1390b2eef3d102c5859a5e7
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
package com.qianwen.smartman.modules.dnc.service.impl;
 
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
 
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.qianwen.core.excel.util.ExcelUtil;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.oss.OssTemplate;
import com.qianwen.core.oss.model.BladeFile;
import com.qianwen.core.oss.model.OssFile;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.api.ResultCode;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.common.constant.CommonConstant;
import com.qianwen.smartman.common.constant.DncConstant;
import com.qianwen.smartman.common.utils.LocalDateTimeUtils;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.smartman.modules.cps.service.IWorkstationService;
import com.qianwen.smartman.modules.cps.vo.WorkstationVO;
import com.qianwen.smartman.modules.dnc.context.DncFileNameProcessRuleContext;
import com.qianwen.smartman.modules.dnc.context.DncFileNameProcessRuleFactory;
import com.qianwen.smartman.modules.dnc.dto.ArtFileInformationDTO;
import com.qianwen.smartman.modules.dnc.dto.DncOperationLogDto;
import com.qianwen.smartman.modules.dnc.entity.DncArtBag;
import com.qianwen.smartman.modules.dnc.entity.DncFixedSpace;
import com.qianwen.smartman.modules.dnc.entity.DncWsRelationTd;
import com.qianwen.smartman.modules.dnc.entity.TransferDirectory;
import com.qianwen.smartman.modules.dnc.entity.TransferDirectoryFile;
import com.qianwen.smartman.modules.dnc.enums.DncEnums;
import com.qianwen.smartman.modules.dnc.service.IDncFixedSpaceService;
import com.qianwen.smartman.modules.dnc.service.IDncOperationLogService;
import com.qianwen.smartman.modules.dnc.service.IDncWsRelationTdService;
import com.qianwen.smartman.modules.dnc.service.IDockingFtpService;
import com.qianwen.smartman.modules.dnc.service.ITransferDirectoryFileService;
import com.qianwen.smartman.modules.dnc.service.ITransferDirectoryService;
import com.qianwen.smartman.modules.dnc.vo.DncFixSpaceVO;
import com.qianwen.smartman.modules.dnc.vo.EntityInfoVO;
import com.qianwen.smartman.modules.dnc.vo.FtpDirectoryVO;
import com.qianwen.smartman.modules.resource.builder.oss.OssBuilder;
 
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
 
@Service
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/dnc/service/impl/DockingFtpServiceImpl.class */
public class DockingFtpServiceImpl implements IDockingFtpService {
    private static final Logger log = LoggerFactory.getLogger(DockingFtpServiceImpl.class);
    private final ITransferDirectoryService transferDirectoryService;
    private final ITransferDirectoryFileService transferDirectoryFileService;
    private final IWorkstationService workstationService;
    private final OssBuilder ossBuilder;
    private final IDncOperationLogService logService;
    private final IDncFixedSpaceService fixedSpaceService;
    private final IDncWsRelationTdService wsRelationTdService;
 
    public DockingFtpServiceImpl(final ITransferDirectoryService transferDirectoryService, final ITransferDirectoryFileService transferDirectoryFileService, final IWorkstationService workstationService, final OssBuilder ossBuilder, final IDncOperationLogService logService, final IDncFixedSpaceService fixedSpaceService, final IDncWsRelationTdService wsRelationTdService) {
        this.transferDirectoryService = transferDirectoryService;
        this.transferDirectoryFileService = transferDirectoryFileService;
        this.workstationService = workstationService;
        this.ossBuilder = ossBuilder;
        this.logService = logService;
        this.fixedSpaceService = fixedSpaceService;
        this.wsRelationTdService = wsRelationTdService;
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    public FtpDirectoryVO readDirectory(String path) {
        List<EntityInfoVO> list = findByDirectoryPath(path);
        return FtpDirectoryVO.builder().path(path).list(list).build();
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    public String readFile(String path) {
        Long workstationId = getWorkstationByPath();
        TransferDirectory transferDirectory = getTransferDirectoryByPath(workstationId);
        if (Objects.isNull(transferDirectory)) {
            throw new ServiceException(MessageUtils.message("dnc.transfer.directory.no", new Object[0]));
        }
        String fileName = fileName(path);
        if (Func.isNotBlank(fileName)) {
            DncFixedSpace dncFixedSpace = getDirectoryName(path, workstationId);
            if (dncFixedSpace == null) {
                throw new ServiceException(MessageUtils.message("dnc.ftp.relation.workstation.normal.use", new Object[0]));
            }
            TransferDirectoryFile transferDirectoryFile = this.transferDirectoryFileService.getOne(new QueryWrapper<TransferDirectoryFile>().lambda()
                    .eq(TransferDirectoryFile::getTransferDirectoryId, transferDirectory.getId())
                    .eq(TransferDirectoryFile::getOriginalFilename, fileName)
                    .eq(TransferDirectoryFile::getFileType, 1)
                    .eq(Func.isNotEmpty(dncFixedSpace), TransferDirectoryFile::getStorageSpaceId, dncFixedSpace.getId())
                    .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode()));
            /*
            TransferDirectoryFile transferDirectoryFile = (TransferDirectoryFile) this.transferDirectoryFileService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
                return v0.getTransferDirectoryId();
            }, transferDirectory.getId())).eq((v0) -> {
                return v0.getOriginalFilename();
            }, fileName)).eq((v0) -> {
                return v0.getFileType();
            }, 1)).eq(Func.isNotEmpty(dncFixedSpace), (v0) -> {
                return v0.getStorageSpaceId();
            }, dncFixedSpace.getId()).eq((v0) -> {
                return v0.getIsCurrent();
            }, DncEnums.CurrentVersion.YES.getCode()));*/
            if (transferDirectoryFile == null) {
                throw new ServiceException(MessageUtils.message("dnc.file.is.not.exists", new Object[0]));
            }
            String link = transferDirectoryFile.getLink();
            WorkstationVO workstationVO = this.workstationService.getDetail(transferDirectoryFile.getWorkstationId());
            this.logService.insertLog(new DncOperationLogDto().setLink(link).setWorkstationId(transferDirectoryFile.getWorkstationId()).setTargetPathName(workstationVO.getGroupName() + "/" + workstationVO.getName() + "/" + transferDirectoryFile.getOriginalFilename()).setTargetPathId(transferDirectory.getGroupAllPath()).setTargetType(DncEnums.LogFileSourceType.PROCESSING_PROGRAM.getCode()).setOperationTime(DateTime.now()).setSourceType(DncEnums.LogFileSourceType.FTP.getCode()).setOperationResponse(1).setSourcePathName(DncConstant.FTP_PATH).setOperatorName("机床回传").setOperationType(DncEnums.LogOperationType.DOWNLOAD.getCode()).setSuffix(transferDirectoryFile.getSuffix()).setObjectKey(transferDirectoryFile.getObjectKey()).setFileName(transferDirectoryFile.getFilename()).setVersion(transferDirectoryFile.getVersion()).setFileId(transferDirectoryFile.getId()));
            return link;
        }
        return "";
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    public EntityInfoVO getByPath(String path) {
        TransferDirectoryFile transferDirectoryFile = getTransferDirectoryFileByPath(path);
        return (EntityInfoVO) Optional.ofNullable(transferDirectoryFile.getId()).map(c -> {
            return EntityInfoVO.builder().id(String.valueOf(transferDirectoryFile.getId())).objectKey(transferDirectoryFile.getObjectKey()).isDirectory(Boolean.FALSE).canWrite(Boolean.FALSE).name(transferDirectoryFile.getFilename()).size(transferDirectoryFile.getContentLength()).creationTime(transferDirectoryFile.getCreateTime() != null ? LocalDateTimeUtils.DateToLocalDateTime(transferDirectoryFile.getCreateTime()) : LocalDateTime.now()).updateTime(transferDirectoryFile.getLastModified() == null ? LocalDateTime.now() : LocalDateTimeUtils.DateToLocalDateTime(transferDirectoryFile.getLastModified())).owner(String.valueOf(transferDirectoryFile.getCreateUser())).hash(transferDirectoryFile.getContentMd5()).build();
        }).orElse(new EntityInfoVO());
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    public EntityInfoVO createFile(String path, HttpServletRequest request) {
        Long workstationId = getWorkstationByPath();
        if (Func.isBlank(path)) {
            return new EntityInfoVO();
        }
        log.info("path:{}", path);
        DncFixedSpace dncFixedSpace = getDirectoryName(path, workstationId);
        if (dncFixedSpace == null) {
            throw new ServiceException(MessageUtils.message("dnc.ftp.relation.workstation.normal.use", new Object[0]));
        }
        log.info("storageSpace:{}", dncFixedSpace);
        String fileName = fileName(path);
        log.info("fileName:{}", fileName);
        Long workStationId = getWorkstationByPath();
        TransferDirectory transferDirectory = getTransferDirectoryByPath(workstationId);
        if (Objects.isNull(transferDirectory)) {
            throw new ServiceException(MessageUtils.message("dnc.transfer.directory.no", new Object[0]));
        }
        OssTemplate template = this.ossBuilder.template();
        BladeFile bladeFile = null;
        try {
            File file = new File(System.getProperty("java.io.tmpdir") + "/" + fileName);
            FileUtils.copyToFile(request.getInputStream(), file);
            MultipartFile multipartFile = ExcelUtil.fileToMultipartFile(file);
            log.info("multipartFile:{}", Boolean.valueOf(multipartFile.isEmpty()));
            bladeFile = template.putFile(multipartFile.getOriginalFilename(), multipartFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("BladeFile:{}", bladeFile);
        OssFile ossFile = template.statFile(((BladeFile) Objects.requireNonNull(bladeFile)).getName());
        log.info("OssFile:{}", ossFile);
        String hash = ossFile.getHash();
        TransferDirectoryFile currentFile = this.transferDirectoryFileService.getOne(Wrappers.<TransferDirectoryFile>lambdaQuery()
                .eq(TransferDirectoryFile::getTransferDirectoryId, transferDirectory.getId())
                .eq(TransferDirectoryFile::getFilename, fileName)
                .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode())
                .eq(TransferDirectoryFile::getFileType, DncEnums.FileType.PROCESSING_PROGRAM.getCode())
                .eq(TransferDirectoryFile::getStorageSpaceId, dncFixedSpace.getId()));
        /*
        TransferDirectoryFile currentFile = (TransferDirectoryFile) this.transferDirectoryFileService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getTransferDirectoryId();
        }, transferDirectory.getId())).eq((v0) -> {
            return v0.getFilename();
        }, fileName)).eq((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.YES.getCode())).eq((v0) -> {
            return v0.getFileType();
        }, DncEnums.FileType.PROCESSING_PROGRAM.getCode())).eq((v0) -> {
            return v0.getStorageSpaceId();
        }, dncFixedSpace.getId()));*/
        TransferDirectoryFile directoryFile = TransferDirectoryFile.builder().transferDirectoryId(transferDirectory.getId()).workstationId(workStationId).fileType(1).fileSource(DncEnums.FileSource.FTP.getCode()).filename(fileName).originalFilename(fileName).contentMd5(hash).contentLength(Long.valueOf(ossFile.getLength())).contentType(ossFile.getContentType()).objectKey(bladeFile.getName()).suffix(getFileSuffix(fileName)).storageSpace(dncFixedSpace.getSpaceLocation()).storageSpaceId(dncFixedSpace.getId()).isCurrent(DncEnums.CurrentVersion.YES.getCode()).whetherArchive(DncEnums.WhetherArchive.Archive.getCode()).link(ossFile.getLink()).build();
        if (Func.isNull(currentFile)) {
            directoryFile.setVersion(CommonConstant.VERSION_NUM);
            this.transferDirectoryFileService.save(directoryFile);
        } else if (!hash.equals(currentFile.getContentMd5())) {
            currentFile.setIsCurrent(DncEnums.CurrentVersion.NO.getCode());
            this.transferDirectoryFileService.updateById(currentFile);
            directoryFile.setVersion(Integer.valueOf(currentFile.getVersion().intValue() + CommonConstant.VERSION_NUM.intValue()));
            this.transferDirectoryFileService.save(directoryFile);
        }
        WorkstationVO workstationVO = this.workstationService.getDetail(workStationId);
        return (EntityInfoVO) Optional.ofNullable(directoryFile).map(c -> {
            this.logService.insertLog(new DncOperationLogDto().setLink(directoryFile.getLink()).setWorkstationId(directoryFile.getWorkstationId()).setTargetPathName(workstationVO.getGroupName() + "/" + workstationVO.getName() + "/" + directoryFile.getOriginalFilename()).setTargetPathId(transferDirectory.getGroupAllPath()).setTargetType(DncEnums.LogFileSourceType.PROCESSING_PROGRAM.getCode()).setOperationTime(DateTime.now()).setSourceType(DncEnums.LogFileSourceType.FTP.getCode()).setSourcePathName(DncConstant.FTP_PATH).setOperatorName("机床回传").setVersion(directoryFile.getVersion()).setOperationResponse(1).setOperationType(DncEnums.LogOperationType.UPLOAD.getCode()).setSuffix(directoryFile.getSuffix()).setObjectKey(directoryFile.getObjectKey()).setFileName(directoryFile.getFilename()).setFileId(directoryFile.getId()));
            return EntityInfoVO.builder().id(String.valueOf(c.getId())).objectKey(c.getObjectKey()).isDirectory(Boolean.FALSE).canWrite(Boolean.FALSE).name(c.getFilename()).size(c.getContentLength()).creationTime(c.getCreateTime() != null ? LocalDateTimeUtils.DateToLocalDateTime(c.getCreateTime()) : LocalDateTime.now()).updateTime(c.getLastModified() == null ? LocalDateTime.now() : LocalDateTimeUtils.DateToLocalDateTime(c.getLastModified())).owner(String.valueOf(c.getCreateUser())).hash(c.getContentMd5()).build();
        }).orElse(new EntityInfoVO());
    }
 
    private String getFileSuffix(String name) {
        int i = name.lastIndexOf(DncConstant.POINT) + CommonConstant.VERSION_NUM.intValue();
        if (i == CommonConstant.NOT_SEALED_ID.intValue()) {
            return "";
        }
        return name.substring(i);
    }
 
    private void coverFile(String path, Long workStationId) {
        this.transferDirectoryFileService.update(Wrappers.<TransferDirectoryFile>lambdaUpdate()
                .set(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.NO.getCode())
                .set(TransferDirectoryFile::getCovertTime, DateTime.now())
                .eq(TransferDirectoryFile::getFileType, DncEnums.FileType.PROCESSING_PROGRAM.getCode())
                .eq(TransferDirectoryFile::getOriginalFilename, path)
                .eq(TransferDirectoryFile::getWorkstationId, workStationId)
                .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode()));
        
        /*
        this.transferDirectoryFileService.update((Wrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) Wrappers.lambdaUpdate().set((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.NO.getCode())).set((v0) -> {
            return v0.getCovertTime();
        }, DateTime.now())).eq((v0) -> {
            return v0.getFileType();
        }, DncEnums.FileType.PROCESSING_PROGRAM.getCode())).eq((v0) -> {
            return v0.getOriginalFilename();
        }, path)).eq((v0) -> {
            return v0.getWorkstationId();
        }, workStationId)).eq((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.YES.getCode()));*/
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    @Transactional(rollbackFor = {Exception.class})
    public boolean renameFile(String oldFileName, String newFileName) {
        Long workstationId = getWorkstationByPath();
        DncFixedSpace dncFixedSpace = getDirectoryName(oldFileName, workstationId);
        if (dncFixedSpace == null) {
            throw new ServiceException(MessageUtils.message("dnc.ftp.relation.workstation.normal.use", new Object[0]));
        }
        Long storageSpaceId = dncFixedSpace.getId();
        String oldName = fileName(oldFileName);
        String newName = fileName(newFileName);
        
        Long count = Long.valueOf(this.transferDirectoryFileService.count(Wrappers.<TransferDirectoryFile>lambdaQuery()
                .eq(TransferDirectoryFile::getWorkstationId, workstationId)
                .eq(TransferDirectoryFile::getStorageSpaceId, storageSpaceId)
                .eq(TransferDirectoryFile::getFileType, DncEnums.FileType.PROCESSING_PROGRAM.getCode())
                .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode())
                .eq(TransferDirectoryFile::getOriginalFilename, newName)));
        
        /*
        Long count = Long.valueOf(this.transferDirectoryFileService.count((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getWorkstationId();
        }, workstationId)).eq((v0) -> {
            return v0.getStorageSpaceId();
        }, storageSpaceId)).eq((v0) -> {
            return v0.getFileType();
        }, DncEnums.FileType.PROCESSING_PROGRAM.getCode())).eq((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.YES.getCode())).eq((v0) -> {
            return v0.getOriginalFilename();
        }, newName)));*/
        if (count.longValue() > 0) {
            throw new ServiceException(MessageUtils.message("dnc.file.name.exist", new Object[0]));
        }
        TransferDirectoryFile directoryFile = getTransferDirectoryFile(storageSpaceId, oldName, workstationId);
        TransferDirectory directory = (TransferDirectory) this.transferDirectoryService.getById(directoryFile.getTransferDirectoryId());
        WorkstationVO workstationVO = this.workstationService.getDetail(workstationId);
        this.logService.insertLog(new DncOperationLogDto().setLink(directoryFile.getLink()).setWorkstationId(directoryFile.getWorkstationId()).setTargetPathName(workstationVO.getGroupName() + "/" + workstationVO.getName() + "/" + directoryFile.getOriginalFilename()).setTargetPathId(directory.getGroupAllPath()).setTargetType(DncEnums.LogFileSourceType.PROCESSING_PROGRAM.getCode()).setOperationTime(DateTime.now()).setSourceType(DncEnums.LogFileSourceType.FTP.getCode()).setOperationResponse(1).setOperationType(DncEnums.LogOperationType.TO_UPDATE.getCode()).setSuffix(directoryFile.getSuffix()).setObjectKey(directoryFile.getObjectKey()).setFileName(directoryFile.getFilename()).setVersion(directoryFile.getVersion()).setSourcePathName(DncConstant.FTP_PATH).setOperatorName("机床回传").setFileId(directoryFile.getId()));
        
        return this.transferDirectoryFileService.update(Wrappers.<TransferDirectoryFile>lambdaUpdate()
                .set(TransferDirectoryFile::getOriginalFilename, newName)
                .set(TransferDirectoryFile::getFilename, newName)
                .eq(TransferDirectoryFile::getFileType, DncEnums.FileType.PROCESSING_PROGRAM.getCode())
                .eq(TransferDirectoryFile::getWorkstationId, workstationId)
                .eq(TransferDirectoryFile::getStorageSpaceId, storageSpaceId)
                .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode())
                .and(i -> i.eq(TransferDirectoryFile::getOriginalFilename, oldName).or().eq(TransferDirectoryFile::getFilename, oldName)));
        /*
        return this.transferDirectoryFileService.update((Wrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) Wrappers.lambdaUpdate().set((v0) -> {
            return v0.getOriginalFilename();
        }, newName)).set((v0) -> {
            return v0.getFilename();
        }, newName)).eq((v0) -> {
            return v0.getFileType();
        }, DncEnums.FileType.PROCESSING_PROGRAM.getCode())).eq((v0) -> {
            return v0.getWorkstationId();
        }, workstationId)).eq((v0) -> {
            return v0.getStorageSpaceId();
        }, storageSpaceId)).eq((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.YES.getCode())).and(i -> {
            LambdaUpdateWrapper lambdaUpdateWrapper = (LambdaUpdateWrapper) ((LambdaUpdateWrapper) ((LambdaUpdateWrapper) i.eq((v0) -> {
                return v0.getOriginalFilename();
            }, oldName)).or()).eq((v0) -> {
                return v0.getFilename();
            }, oldName);
        }));*/
    }
 
    private TransferDirectoryFile getTransferDirectoryFile(Long storageSpaceId, String oldFileName, Long workstationId) {
        return (TransferDirectoryFile)this.transferDirectoryFileService.getOne(Wrappers.<TransferDirectoryFile>lambdaQuery()
                .eq(TransferDirectoryFile::getWorkstationId, workstationId)
                .eq(TransferDirectoryFile::getFileType, DncEnums.FileType.PROCESSING_PROGRAM.getCode())
                .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode())
                .eq(TransferDirectoryFile::getStorageSpaceId, storageSpaceId)
                .and(i -> i.eq(TransferDirectoryFile::getOriginalFilename, oldFileName).or().eq(TransferDirectoryFile::getFilename, oldFileName)));
        /*
        return (TransferDirectoryFile) this.transferDirectoryFileService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getWorkstationId();
        }, workstationId)).eq((v0) -> {
            return v0.getFileType();
        }, DncEnums.FileType.PROCESSING_PROGRAM.getCode())).eq((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.YES.getCode())).eq((v0) -> {
            return v0.getStorageSpaceId();
        }, storageSpaceId)).and(i -> {
            LambdaQueryWrapper lambdaQueryWrapper = (LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) i.eq((v0) -> {
                return v0.getOriginalFilename();
            }, oldFileName)).or()).eq((v0) -> {
                return v0.getFilename();
            }, oldFileName);
        }));*/
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    public boolean deleteFile(String filename) {
        Long workstationId = getWorkstationByPath();
        DncFixedSpace dncFixedSpace = getDirectoryName(filename, workstationId);
        if (dncFixedSpace == null) {
            throw new ServiceException(MessageUtils.message("dnc.ftp.relation.workstation.normal.use", new Object[0]));
        }
        Long storageSpaceId = dncFixedSpace.getId();
        String name = fileName(filename);
        TransferDirectoryFile directoryFile = getTransferDirectoryFile(storageSpaceId, name, workstationId);
        TransferDirectory directory = (TransferDirectory) this.transferDirectoryService.getById(directoryFile.getTransferDirectoryId());
        WorkstationVO workstationVO = this.workstationService.getDetail(workstationId);
        this.logService.insertLog(new DncOperationLogDto().setLink(directoryFile.getLink()).setWorkstationId(directoryFile.getWorkstationId()).setTargetPathName(workstationVO.getGroupName() + "/" + workstationVO.getName() + "/" + directoryFile.getOriginalFilename()).setTargetPathId(directory.getGroupAllPath()).setTargetType(DncEnums.LogFileSourceType.PROCESSING_PROGRAM.getCode()).setOperationTime(DateTime.now()).setSourceType(DncEnums.LogFileSourceType.FTP.getCode()).setOperationResponse(1).setOperationType(DncEnums.LogOperationType.DELETE.getCode()).setSuffix(directoryFile.getSuffix()).setObjectKey(directoryFile.getObjectKey()).setFileName(directoryFile.getFilename()).setSourcePathName(DncConstant.FTP_PATH).setOperatorName("机床回传").setVersion(directoryFile.getVersion()).setFileId(directoryFile.getId()));
        return this.transferDirectoryFileService.removeById(directoryFile);
    }
 
    @Override // org.springblade.modules.dnc.service.IDockingFtpService
    public Integer getOrderCwd(String path) {
        Long workstationId = getWorkstationByPath();
        Long ftpNodeParentId = getFtpNodeParentId(path, workstationId);
        if (Func.isBlank(path) || ftpNodeParentId.longValue() > 0) {
            return Integer.valueOf(ResultCode.SUCCESS.getCode());
        }
        return Integer.valueOf(ResultCode.NOT_FOUND.getCode());
    }
 
    private TransferDirectoryFile getTransferDirectoryFileByPath(String path) {
        Long workstationId = getWorkstationByPath();
        DncFixedSpace dncFixedSpace = getDirectoryName(path, workstationId);
        if (dncFixedSpace == null) {
            throw new ServiceException(MessageUtils.message("dnc.ftp.relation.workstation.normal.use", new Object[0]));
        }
        TransferDirectory transferDirectory = getTransferDirectoryByPath(workstationId);
        if (Objects.isNull(transferDirectory)) {
            throw new ServiceException(MessageUtils.message("dnc.transfer.directory.no", new Object[0]));
        }
        String fileName = fileName(path);
        
        return Optional.<TransferDirectoryFile>ofNullable(this.transferDirectoryFileService.getOne(new QueryWrapper<TransferDirectoryFile>().lambda()
                .eq(TransferDirectoryFile::getTransferDirectoryId, transferDirectory.getId())
                .eq(TransferDirectoryFile::getOriginalFilename, fileName)
                .eq(TransferDirectoryFile::getFileType, Integer.valueOf(1))
                .eq(TransferDirectoryFile::getIsCurrent, DncEnums.CurrentVersion.YES.getCode())
                .eq(Func.isNotEmpty(dncFixedSpace), TransferDirectoryFile::getStorageSpaceId, dncFixedSpace.getId()))
                )
            
            .orElse(new TransferDirectoryFile());
        
        /*
        return (TransferDirectoryFile) Optional.ofNullable(this.transferDirectoryFileService.getOne(((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) new QueryWrapper().lambda().eq((v0) -> {
            return v0.getTransferDirectoryId();
        }, transferDirectory.getId())).eq((v0) -> {
            return v0.getOriginalFilename();
        }, fileName)).eq((v0) -> {
            return v0.getFileType();
        }, 1)).eq((v0) -> {
            return v0.getIsCurrent();
        }, DncEnums.CurrentVersion.YES.getCode())).eq(Func.isNotEmpty(dncFixedSpace), (v0) -> {
            return v0.getStorageSpaceId();
        }, dncFixedSpace.getId()))).orElse(new TransferDirectoryFile());
        */
    }
 
    private DncFixedSpace getDirectoryName(String path, Long workstationId) {
       
        DncFixedSpace dncFixedSpace2;
        DncFixedSpace fixedSpace = null;
        String[] split = StrUtil.split(path, "/");
        if (Func.isNotEmpty(split)) {
            int len = split.length;
            if (len < 2) {
                Long rootNodeId = this.fixedSpaceService.getRootNodeId(String.valueOf(workstationId)).getRootNodeId();
                if (Func.isNotEmpty(rootNodeId)) {
                    DncFixedSpace dncFixedSpace = (DncFixedSpace)this.fixedSpaceService.getById(rootNodeId);
                    if (dncFixedSpace != null) {
                      fixedSpace = dncFixedSpace; 
                    }
                }
                /*
                if (Func.isNotEmpty(rootNodeId) && (dncFixedSpace2 = (DncFixedSpace) this.fixedSpaceService.getById(rootNodeId)) != null) {
                    fixedSpace = dncFixedSpace2;
                }*/
            }
            if (len >= 2) {
                String substr = path.substring(0, path.lastIndexOf("/"));
                Long parentId = getFtpNodeParentId(substr, workstationId);
                String directoryName = substr.substring(substr.lastIndexOf("/") + 1);
                DncWsRelationTd wsRelationTd = this.wsRelationTdService.getOne(Wrappers.<DncWsRelationTd>lambdaQuery().eq(DncWsRelationTd::getWorkstationId, workstationId));
                
                if (wsRelationTd != null) {
                    DncFixedSpace dncFixedSpace = this.fixedSpaceService.getOne(Wrappers.<DncFixedSpace>lambdaQuery()
                        .eq(DncFixedSpace::getTdId, wsRelationTd.getTransferDirectorId())
                        .eq(DncFixedSpace::getParentId, parentId)
                        .eq(DncFixedSpace::getName, directoryName));
                    if (dncFixedSpace != null) {
                      fixedSpace = dncFixedSpace;
                    }
                  } 
                
                /*
                DncWsRelationTd wsRelationTd = (DncWsRelationTd) this.wsRelationTdService.getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
                    return v0.getWorkstationId();
                }, workstationId));*/
                /*
                if (wsRelationTd != null) && (dncFixedSpace = (DncFixedSpace) this.fixedSpaceService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
                    return v0.getTdId();
                }, wsRelationTd.getTransferDirectorId())).eq((v0) -> {
                    return v0.getParentId();
                }, parentId)).eq((v0) -> {
                    return v0.getName();
                }, directoryName))) != null) {
                    fixedSpace = dncFixedSpace;
                }*/
            }
        }
        return fixedSpace;
    }
 
    private Integer getStorageSpace(String path) {
        Integer storageSpace = null;
        String[] split = StrUtil.split(path, "/");
        if (Func.isNotEmpty(split)) {
            int len = split.length;
            if (len < 2) {
                storageSpace = DncEnums.StorageSpace.ROUTINE.getCode();
            }
            if (len == 2) {
                String fixedSpace = split[split.length - 2];
                if (CommonConstant.FIXED_SPACE.equals(fixedSpace)) {
                    storageSpace = DncEnums.StorageSpace.FIXED.getCode();
                } else {
                    storageSpace = DncEnums.StorageSpace.ROUTINE.getCode();
                }
            }
        }
        return storageSpace;
    }
 
    private TransferDirectory getTransferDirectoryByPath(Long workstationId) {
        DncWsRelationTd dncWsRelationTd = this.wsRelationTdService.getOne(Wrappers.<DncWsRelationTd>lambdaQuery().eq(DncWsRelationTd::getWorkstationId, workstationId));
         /*
        DncWsRelationTd dncWsRelationTd = (DncWsRelationTd) this.wsRelationTdService.getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getWorkstationId();
        }, workstationId));*/
        if (dncWsRelationTd == null) {
            throw new ServiceException(MessageUtils.message("dnc.workstation.not.binding.ftp.directory", new Object[0]));
        }
        TransferDirectory transferDirectory = (TransferDirectory) this.transferDirectoryService.getById(dncWsRelationTd.getTransferDirectorId());
        if (transferDirectory == null) {
            throw new ServiceException(MessageUtils.message("dnc.relation.ftp.directory.not.exist", new Object[0]));
        }
        return transferDirectory;
    }
 
    private String fileName(String path) {
        String fileName = "";
        String[] split = StrUtil.split(path, "/");
        if (Func.isNotEmpty(split)) {
            int len = split.length;
            if (len < 2) {
                fileName = path;
            }
            if (len >= 2) {
                fileName = path.substring(path.lastIndexOf("/") + 1);
            }
        }
        return fileName;
    }
 
    private List<EntityInfoVO> findByDirectoryPath(String path) {
        Long rootNodeId;
        Long workstationId = getWorkstationByPath();
        int length = path.split("/").length;
        DncWsRelationTd dncWsRelationTd = this.wsRelationTdService.getOne(Wrappers.<DncWsRelationTd>lambdaQuery().eq(DncWsRelationTd::getWorkstationId, workstationId));
        /*
        DncWsRelationTd dncWsRelationTd = (DncWsRelationTd) this.wsRelationTdService.getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
            return v0.getWorkstationId();
        }, workstationId));*/
        if (dncWsRelationTd == null) {
            throw new ServiceException(MessageUtils.message("dnc.workstation.not.binding.ftp.directory", new Object[0]));
        }
        TransferDirectory transferDirectory = (TransferDirectory) this.transferDirectoryService.getById(dncWsRelationTd.getTransferDirectorId());
        if (Func.isEmpty(transferDirectory)) {
            return Lists.newArrayList();
        }
        List<TransferDirectoryFile> fileList = this.transferDirectoryFileService.findProgram(transferDirectory.getId());
        if (Func.isEmpty(fileList)) {
            return Lists.newArrayList();
        }
        List<Long> artFileIds = fileList.stream().filter(s -> {
            return DncEnums.FileSource.PROCESSING_PROGRAM.getCode().equals(s.getFileSource());
        }).map((v0) -> {
            return v0.getCraftFileId();
        }).collect(Collectors.toList());
        Map<Long, ArtFileInformationDTO> artFileInformationMap = this.transferDirectoryFileService.artFileInformationMap(artFileIds);
        if (StrUtil.isBlank(path) && (rootNodeId = this.fixedSpaceService.getRootNodeId(String.valueOf(workstationId)).getRootNodeId()) != null) {
            List<EntityInfoVO> collect = fileList.stream().filter(s2 -> {
                return rootNodeId.equals(s2.getStorageSpaceId());
            }).map(c -> {
                EntityInfoVO build = EntityInfoVO.builder().id(String.valueOf(c.getId())).objectKey(c.getObjectKey()).isDirectory(Boolean.FALSE).canWrite(Boolean.FALSE).name(c.getOriginalFilename()).size(c.getContentLength()).creationTime(c.getCreateTime() != null ? LocalDateTimeUtils.DateToLocalDateTime(c.getCreateTime()) : LocalDateTime.now()).updateTime(c.getLastModified() == null ? LocalDateTime.now() : LocalDateTimeUtils.DateToLocalDateTime(c.getLastModified())).owner(String.valueOf(c.getCreateUser())).hash(c.getContentMd5()).build();
                return getEntityInfoVO(artFileInformationMap, c, build);
            }).collect(Collectors.toList());
            List<DncFixSpaceVO> fixSpaceTree = this.fixedSpaceService.getFixSpaceTree(String.valueOf(workstationId), String.valueOf(rootNodeId));
            if (Func.isNotEmpty(fixSpaceTree)) {
                List<EntityInfoVO> fixSpaceList = getFixSpaceList(fixSpaceTree);
                collect.addAll(fixSpaceList);
            }
            return collect;
        } else if (length >= CommonConstant.VERSION_NUM.intValue()) {
            List<EntityInfoVO> collect2 = new ArrayList<>();
            DncFixedSpace dncFixedSpace = getDirectory(path, workstationId);
            if (dncFixedSpace == null) {
                throw new ServiceException(MessageUtils.message("dnc.ftp.relation.workstation.normal.use", new Object[0]));
            }
            List<DncFixSpaceVO> fixSpaceVOList = this.fixedSpaceService.getFixSpaceTree(String.valueOf(workstationId), String.valueOf(dncFixedSpace.getId()));
            if (Func.isNotEmpty(fixSpaceVOList)) {
                List<EntityInfoVO> fixSpaceList2 = getFixSpaceList(fixSpaceVOList);
                collect2.addAll(fixSpaceList2);
            }
            List<TransferDirectoryFile> files = fileList.stream().filter(s3 -> {
                return dncFixedSpace.getId().equals(s3.getStorageSpaceId());
            }).collect(Collectors.toList());
            if (Func.isNotEmpty(files)) {
                List<EntityInfoVO> entityInfos = files.stream().map(c2 -> {
                    EntityInfoVO build = EntityInfoVO.builder().id(String.valueOf(c2.getId())).isDirectory(Boolean.FALSE).canWrite(Boolean.FALSE).name(c2.getOriginalFilename()).size(c2.getContentLength()).creationTime(c2.getCreateTime() != null ? LocalDateTimeUtils.DateToLocalDateTime(c2.getCreateTime()) : LocalDateTime.now()).updateTime(c2.getLastModified() == null ? LocalDateTime.now() : LocalDateTimeUtils.DateToLocalDateTime(c2.getLastModified())).owner(String.valueOf(c2.getCreateUser())).hash(c2.getContentMd5()).build();
                    return getEntityInfoVO(artFileInformationMap, c2, build);
                }).collect(Collectors.toList());
                collect2.addAll(entityInfos);
            }
            return collect2;
        } else {
            return new ArrayList<>();
        }
    }
 
    private EntityInfoVO getEntityInfoVO(Map<Long, ArtFileInformationDTO> artFileInformationMap, TransferDirectoryFile c, EntityInfoVO build) {
        ArtFileInformationDTO fileInformation;
        if (DncEnums.FileSource.PROCESSING_PROGRAM.getCode().equals(c.getFileSource()) && !artFileInformationMap.isEmpty() && (fileInformation = artFileInformationMap.get(c.getCraftFileId())) != null) {
            String fileSourceNamePath = fileInformation.getFileSourceNamePath();
            DncArtBag dncArtBag = fileInformation.getDncArtBag();
            if (Func.isNotEmpty(dncArtBag) && Func.isNotBlank(fileSourceNamePath)) {
                DncFileNameProcessRuleContext context = DncFileNameProcessRuleContext.builder().artBag(dncArtBag).directoryPath(fileSourceNamePath).fileName(c.getFilename()).build();
                String dncFileName = DncFileNameProcessRuleFactory.getDncFileName(context);
                if (Func.isNotBlank(dncFileName)) {
                    build.setName(dncFileName);
                }
            }
        }
        return build;
    }
 
    private List<EntityInfoVO> getFixSpaceList(List<DncFixSpaceVO> fixSpaceVOList) {
        List<EntityInfoVO> collect = new ArrayList<>();
        fixSpaceVOList.forEach(item -> {
            EntityInfoVO dto = new EntityInfoVO();
            dto.setCanWrite(Boolean.FALSE);
            dto.setCreationTime(LocalDateTime.now());
            dto.setUpdateTime(LocalDateTime.now());
            dto.setHash("");
            dto.setSize(0L);
            dto.setOwner(DncConstant.OWNER);
            dto.setId("0");
            dto.setIsDirectory(Boolean.TRUE);
            dto.setName(item.getName());
            collect.add(dto);
        });
        return collect;
    }
 
    private DncFixedSpace getDirectory(String path, Long workstationId) {
        //DncFixedSpace dncFixedSpace;
        DncFixedSpace fixedSpace = null;
        String directoryName = "";
        String[] split = StrUtil.split(path, "/");
        if (Func.isNotEmpty(split)) {
            int len = split.length;
            if (len < 2) {
                directoryName = path;
            }
            if (len >= 2) {
                directoryName = path.substring(path.lastIndexOf("/") + 1);
            }
            Long parentId = getFtpNodeParentId(path, workstationId);
            DncWsRelationTd wsRelationTd = this.wsRelationTdService.getOne(Wrappers.<DncWsRelationTd>lambdaQuery().eq(DncWsRelationTd::getWorkstationId, workstationId));
            if (wsRelationTd != null) {
                DncFixedSpace dncFixedSpace = this.fixedSpaceService.getOne(Wrappers.<DncFixedSpace>lambdaQuery()
                    .eq(DncFixedSpace::getTdId, wsRelationTd.getTransferDirectorId())
                    .eq(DncFixedSpace::getName, directoryName)
                    .eq(DncFixedSpace::getParentId, parentId)
                    .eq(DncFixedSpace::getSpaceLocation, CommonConstant.CREATE_SAME_DIRECTORY));
                if (dncFixedSpace != null)
                  fixedSpace = dncFixedSpace; 
              } 
            
            /*
            DncWsRelationTd wsRelationTd = (DncWsRelationTd) this.wsRelationTdService.getOne((Wrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getWorkstationId();
            }, workstationId));
            if (wsRelationTd != null && (dncFixedSpace = (DncFixedSpace) this.fixedSpaceService.getOne((Wrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) ((LambdaQueryWrapper) Wrappers.lambdaQuery().eq((v0) -> {
                return v0.getTdId();
            }, wsRelationTd.getTransferDirectorId())).eq((v0) -> {
                return v0.getName();
            }, directoryName)).eq((v0) -> {
                return v0.getParentId();
            }, parentId)).eq((v0) -> {
                return v0.getSpaceLocation();
            }, CommonConstant.CREATE_SAME_DIRECTORY))) != null) {
                fixedSpace = dncFixedSpace;
            }*/
        }
        return fixedSpace;
    }
 
    private Long getFtpNodeParentId(String path, Long workstationId) {
        String completePathName;
        Long rootNodeId = this.fixedSpaceService.getRootNodeId(String.valueOf(workstationId)).getRootNodeId();
        DncFixedSpace rootNode = this.fixedSpaceService.getById(rootNodeId);
        String rootName = rootNode.getName();
        if (Func.isNotBlank(path)) {
            completePathName = rootName + "/" + path;
        } else {
            completePathName = rootName;
        }
        Map<Long, String> stringMap = this.fixedSpaceService.getFixSpaceLocation(workstationId);
        String str = completePathName;
        Long nodeId = (Long) (stringMap.entrySet().stream().filter(kvEntry -> {
            return Objects.equals(str, kvEntry.getValue());
        }).map((v0) -> {
            return v0.getKey();
        }).collect(Collectors.toList())).get(0);
        DncFixedSpace node = (DncFixedSpace) this.fixedSpaceService.getById(nodeId);
        return node.getParentId();
    }
 
    private Long getWorkstationByPath() {
        String machineCode = AuthUtil.getUser().getAccount();
        List<Long> ids = this.workstationService.findByMachineShortCode(machineCode);
        if (Func.isEmpty(ids)) {
            throw new ServiceException(MessageUtils.message("dnc.machine.no.workstation", new Object[0]));
        }
        return ids.get(0);
    }
}