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
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
 
package org.springblade.mdm.machinefile.service;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.log.exception.ServiceException;
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.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.commons.service.MachineDirTranslator;
import org.springblade.mdm.machinefile.entity.MachineFile;
import org.springblade.mdm.machinefile.entity.MachineFileChangeHis;
import org.springblade.mdm.machinefile.mapper.MachineFileMapper;
import org.springblade.mdm.machinefile.vo.MachineFileQueryVO;
import org.springblade.mdm.machinefile.vo.MachineBackFileQueryVO;
import org.springblade.mdm.program.vo.CompareDataVO;
import org.springblade.mdm.statreport.vo.MachineFileExceptionQueryVO;
import org.springblade.mdm.utils.FileContentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
 
/**
 * 机床文件
 *
 * @author yangys
 */
@Slf4j
@Service
@AllArgsConstructor
public class MachineFileService extends BizServiceImpl<MachineFileMapper, MachineFile> {
    private final MachineService machineService;
    private final MachineFileChangeHisService machineFileChangeHisService;
    private final OssTemplate ossTemplate;
    private MachineDirTranslator machineDirTranslator;
    /**
     * 获取文件全路径
     * @param id 数据id
     * @return 文件全路径
     */
    public String getFilePath(Long id) {
        MachineFile machineFile = getById(id);
        Machine machine = machineService.getByCode(machineFile.getMachineCode());
 
        return getBasePath(machine, machineFile.getDirType()) + File.separator + machineFile.getName();
    }
 
    public String getFilePath(MachineFile machineFile) {
        Machine machine = machineService.getByCode(machineFile.getMachineCode());
        return getBasePath(machine, machineFile.getDirType()) + File.separator + machineFile.getName();
    }
    @Transactional(readOnly = true)
    public String getMachineFileContent(Long id) {
        MachineFile machineFile = getById(id);
        Machine machine = machineService.getByCode(machineFile.getMachineCode());
 
        String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
 
        String content;
 
        Path filePath = Paths.get(filePathStr);
        if(!filePath.toFile().exists()){
            return StringUtils.EMPTY;
        }
        try (InputStream inputStream = Files.newInputStream(filePath)) {
            // 使用输入流读取文件内容
            content = FileContentUtil.getContentFromStream(inputStream);
        } catch (IOException e) {
            log.error("读取文件md5失败",e);
            return StringUtils.EMPTY;
        }
 
        return content;
    }
 
    /**
     * 获取基本路径
     * @param machine 机床
     * @param dirType 目录类型
     * @return 目录基本路径
     */
    public static String getBasePath(Machine machine,String dirType){
        String dirPath;
        switch (dirType) {
            case MachineFile.DIR_TYPE_REC:
                dirPath = machine.getProgReceiveDir();
                break;
            case MachineFile.DIR_TYPE_SEND:
                dirPath = machine.getProgSendDir();
                break;
            case MachineFile.DIR_TYPE_TEMP:
                dirPath = machine.getProgTempDir();
                break;
            default:
                log.warn("目录类型不匹配:{}",dirType);
                return null;//不符合任何类型,直接退出
        }
        return dirPath;
    }
 
    @Transactional
    public void saveFileContent(Long id, String content) throws IOException {
        MachineFile machineFile = getById(id);
        Machine machine = machineService.getByCode(machineFile.getMachineCode());
 
        String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
        Path filePath = Paths.get(filePathStr);
 
        String ossName;
        BladeFile bfile;
        try (InputStream inputStream = Files.newInputStream(filePath)) {
            bfile = ossTemplate.putFile(machineFile.getName(), inputStream);
        }
        ossName =bfile.getName();
 
        try (InputStream inputStream = Files.newInputStream(filePath)) {
            // 使用输入流读取文件内容
            String charsetName = FileContentUtil.detectFromInputStream(inputStream);
            FileUtils.writeStringToFile(filePath.toFile(),content,charsetName);
        } catch (IOException e) {
            log.error("读取文件编码失败",e);
            throw new ServiceException("获取文件编码失败");
        }
 
        try (InputStream inputStream = Files.newInputStream(filePath)) {
            bfile = ossTemplate.putFile(machineFile.getName(), inputStream);
        }
        //保存历史记录
        MachineFileChangeHis his = new MachineFileChangeHis();
        his.setMachineFileId(id);
        his.setOssName(ossName);
        his.setOssNameAfter(bfile.getName());
        this.machineFileChangeHisService.save(his);
    }
 
    /**
     * 获取文件的输入流
     * @param machineFile
     * @return
     * @throws IOException
     */
    public InputStream getInputStream(MachineFile machineFile) throws IOException {
 
        Machine machine = machineService.getByCode(machineFile.getMachineCode());
 
        String filePathStr = getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
        Path filePath = Paths.get(filePathStr);
        return Files.newInputStream(filePath);
 
    }
 
    @Transactional(readOnly = true)
    public IPage<MachineFile> filePageForAccept(MachineBackFileQueryVO query) {
        return this.baseMapper.filePageForAccept(Condition.getPage(query),query);
    }
 
    public MachineFile getExistsFile(String name, String dirType, String machineCode) {
        return this.lambdaQuery().eq(MachineFile::getName, name)
            .eq(MachineFile::getDirType, dirType)
            .eq(MachineFile::getMachineCode, machineCode).list().stream().findFirst().orElse(null);
    }
 
    /**
     * 回传程序处理 分页查询
     * @param query
     * @return
     */
    /*
    public IPage<MachineAcceptedFileVO> handlePageQuery(MachineAcceptedFileHandleQueryVO query) {
        IPage<MachineAcceptedFileVO> page = this.getBaseMapper().handlePageQuery(Condition.getPage(query),query);
        return page;
    }*/
 
    /**
     * 机床文件分页查询
     * @param query
     * @return
     */
    public IPage<MachineFile> pageQuery(MachineFileQueryVO query) {
        return baseMapper.pageQuery(Condition.getPage(query),query);
    }
 
    /**
     * 获取修改记录前后2个文件内容
     * @param id
     * @return
     * @throws IOException
     */
    public CompareDataVO queryHisContentForCompare(Long id) throws IOException {
        CompareDataVO vo = new CompareDataVO();
        MachineFileChangeHis his = machineFileChangeHisService.getById(id);
        vo.setContent1(FileContentUtil.getContentFromStream(ossTemplate.statFileStream(his.getOssName())));
        vo.setContent2(FileContentUtil.getContentFromStream(ossTemplate.statFileStream(his.getOssNameAfter())));
 
        return vo;
    }
 
    /**
     * 刷新文件信息,不存在新增,存在则更新
     * @param mf
     */
    public void refreshFileData(MachineFile mf) {
        MachineFile existFileInDb = getExistsFile(mf.getName(),mf.getDirType(),mf.getMachineCode());
        if(existFileInDb == null) {
            save(mf);
        }else{
            log.info("文件在库中已存在:{}",mf.getName());
            existFileInDb.setFileSize(mf.getFileSize());
            existFileInDb.setProgramStatus(mf.getProgramStatus());
            existFileInDb.setFileCreateTime(mf.getFileCreateTime());
            existFileInDb.setFileModifyTime(mf.getFileModifyTime());
            existFileInDb.setStatus(MachineFile.STATUS_NORMAL);
            if(!com.alibaba.excel.util.StringUtils.equals(existFileInDb.getMd5(),mf.getMd5())){
                //文件内容发生变化了,设置状态为初始状态
                existFileInDb.setStatus(MachineFile.STATUS_NORMAL);
            }
            existFileInDb.setMd5(mf.getMd5());
            updateById(existFileInDb);
        }
    }
 
    /**
     * 物理删除文件信息
     * @param id 数据id
     */
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }
 
    /**
     * 异常文件记录查询
     * @param query
     * @return
     */
    @Transactional(readOnly = true)
    public IPage<MachineFile> exceptionFilePageQuery(MachineFileExceptionQueryVO query) {
        return baseMapper.exceptionFilePageQuery(Condition.getPage(query),query);
    }
 
    /**
     * 修改文件名称
     * @param id 文件id
     * @param name 新名称
     */
    public void changeName(Long id, String name) throws IOException {
        MachineFile machineFile = getById(id);
        if(machineFile.getName().equals(name)) {
            return;
        }
        Machine machine = machineService.getByCode(machineFile.getMachineCode());
        String baseDir = machineDirTranslator.trans(getBasePath(machine,machineFile.getDirType()));
        String targetFilePath = baseDir + File.separator + name;
        Path filePath = Paths.get(targetFilePath);
 
        if(filePath.toFile().exists()) {
            throw new ServiceException("同名文件已存在:"+name);
        }
        Files.move(Paths.get(baseDir+File.separator+machineFile.getName()),filePath);
        baseMapper.deleteById(id);
    }
 
    public MachineFile getByNameAndMachineInDir(String name, String machineCode, String dirType) {
        Optional<MachineFile> optFIle = this.lambdaQuery().eq(MachineFile::getName, name).eq(MachineFile::getMachineCode, machineCode)
            .eq(MachineFile::getDirType, dirType).eq(MachineFile::getStatus, MachineFile.STATUS_NORMAL).oneOpt();
        return optFIle.orElse(null);
    }
}