yangys
2025-09-29 4c7296d45efe849dc70a3b2e2240c905481a91c9
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
package org.springblade.mdm.machinefile.service;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.oss.OssTemplate;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.gkw.programnode.vo.ProgramNameVO;
import org.springblade.mdm.gkw.task.entity.MachineBackTask;
import org.springblade.mdm.gkw.task.service.MachineBackTaskService;
import org.springblade.mdm.machinefile.entity.MachineFile;
import org.springblade.mdm.machinefile.entity.MachineAcceptedFile;
import org.springblade.mdm.machinefile.mapper.MachineAcceptedFileMapper;
import org.springblade.mdm.machinefile.vo.MachineAcceptedFileHandleQueryVO;
import org.springblade.mdm.machinefile.vo.MachineBackFileQueryVO;
import org.springblade.mdm.machinefile.vo.MachineAcceptedFileVO;
import org.springblade.mdm.program.service.programannotation.AnnotationData;
import org.springblade.mdm.program.service.programannotation.AnnotationProcessor;
import org.springblade.mdm.program.service.programannotation.AnnotationProcessorHelper;
import org.springblade.mdm.utils.ProgramFileNameParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
 
 
@Service
public class MachineAcceptedFileService extends BizServiceImpl<MachineAcceptedFileMapper, MachineAcceptedFile> {
    @Autowired
    private MachineFileService machineFileService;
    @Autowired
    private OssTemplate ossTemplate;
    @Autowired
    private MachineBackTaskService machineBackTaskService;
    @Autowired
    private AnnotationProcessorHelper annotationProcessorHelper;
    /**
     * 接受
     * @param ids  选定接受的id,逗号分隔
     */
    @Transactional
    public void accept(String ids) throws IOException {
 
        List<Long> idList = Func.toLongList(ids);
        for(Long id : idList){
            acceptFile(id);
        }
 
    }
 
    /**
     * 拒绝
     * @param ids 选定拒绝的id,逗号分隔
     */
    public void reject(String ids) {
        List<Long> idList = Func.toLongList(ids);
 
        MachineFile mf;
        for(Long id : idList){
            mf = machineFileService.getById(id);
            mf.reject();
            machineFileService.updateById(mf);
        }
 
    }
 
    @Transactional
    public void acceptAll() throws IOException {
        MachineBackFileQueryVO query = new MachineBackFileQueryVO();
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        IPage<MachineFile> aceptPage = machineFileService.filePageForAccept(query);
 
        for(MachineFile mf: aceptPage.getRecords()){
            acceptFile(mf.getId());
        }
    }
 
    /**
     * 接收文件
     * @param machineFileId 机床文件id
     * @throws IOException 文件操作异常
     */
    void acceptFile(Long machineFileId) throws IOException {
        MachineFile machineFile = machineFileService.getById(machineFileId);
        machineFile.accept();
        machineFileService.updateById(machineFile);
 
        //新建一个machineaceptedfile对象,存储接收后的数据
        MachineAcceptedFile machineAcceptedFile = new MachineAcceptedFile();
        machineAcceptedFile.setMachineFileId(machineFileId);
        machineAcceptedFile.setName(machineFile.getName());
        String fullPath = machineFileService.getFilePath(machineFile);
        try(InputStream inputStream = Files.newInputStream(Paths.get(fullPath));){
            BladeFile bfile = ossTemplate.putFile(machineFile.getName(),inputStream);
            machineAcceptedFile.setOssName(bfile.getName());
        }
        try(InputStream inputStream = Files.newInputStream(Paths.get(fullPath));){
            //都数据
            //Machine machine = this.machineService.getByCode(record.getMachineCode());
            //AnnotationProcessor processor = annotationProcessorHelper.getProcessor(machine.getControlSystem());
            //AnnotationData annotationData = processor.readAnnotationData(inputStream);
            machineAcceptedFile.setBackTaskId(getMachineBackTaskId(machineAcceptedFile.getName(),machineFile.getDeviation()));
        }
 
        save(machineAcceptedFile);
    }
 
    Long getMachineBackTaskId(String filename,String deviation){
        ProgramNameVO vo = ProgramFileNameParser.parseProgramName(filename);
        MachineBackTask task = machineBackTaskService.queryMatchTaskByProgramName(vo,deviation);
        if(task != null){
            return task.getId();
        }else{
            return null;
        }
    }
 
    /**
     * 机床回传程序处理分页查询
     * @param query 查询参数对象
     * @return 分页数据
     */
 
    public IPage<MachineAcceptedFileVO> handlePageQuery(MachineAcceptedFileHandleQueryVO query) {
        return  this.getBaseMapper().handlePageQuery(Condition.getPage(query),query);
    }
 
    /**
     * 根据导出的接受文件id,获取其中现场编程的MachineFIle
     * @param acceptedFiles  导出的接受文件列表
     * @return machineFile
     */
    public List<MachineFile> exportedProgramOnMachineFiles(List<MachineAcceptedFile> acceptedFiles) {
 
        List<Long> backTaskIds = acceptedFiles.stream().map(MachineAcceptedFile::getBackTaskId).filter(Objects::nonNull).toList();
 
        //现场编程的任务集合
        //List<MachineBackTask> onMachineTasks1 = machineBackTaskService.lambdaQuery().in(MachineBackTask::getId, backTaskIds).list();
        if(backTaskIds.isEmpty()){
            return Collections.emptyList();
        }
        List<MachineBackTask> onMachineTasks = machineBackTaskService.lambdaQuery()
            .eq(MachineBackTask::getTaskType,MachineBackTask.TASK_TYPE_ON_MACHINE)
            .list();
 
        List<Long> onMachineFileIds = new ArrayList<>();
        for(MachineAcceptedFile accFile : acceptedFiles){
            for(MachineBackTask backTask : onMachineTasks){
                if(backTask.getId().equals(accFile.getBackTaskId())){
                    onMachineFileIds.add(accFile.getMachineFileId());
                }
            }
        }
        if(onMachineFileIds.isEmpty()){
            return Collections.emptyList();
        }
 
        return machineFileService.lambdaQuery().in(MachineFile::getId,onMachineFileIds).list();
    }
 
    public List<MachineAcceptedFile> listByBackTaskId(Long backTaskId) {
        return lambdaQuery().eq(MachineAcceptedFile::getBackTaskId,backTaskId).list();
    }
}