yangys
3 天以前 0ecd5acd3b9f320a487c68df2ea6234b4dacb12c
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
/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.mdm.program.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.*;
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.MachineService;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.program.entity.NcNode;
import org.springblade.mdm.program.entity.ProgramSeq;
import org.springblade.mdm.program.mapper.NcProgramMapper;
import org.springblade.mdm.program.entity.NcProgram;
import org.springblade.mdm.program.mapper.ProgramSeqMapper;
import org.springblade.mdm.program.vo.DncSendBackData;
import org.springblade.mdm.program.vo.NcNodeProgramQueryVO;
import org.springblade.mdm.program.vo.NcProgramUploadVO;
import org.springblade.mdm.program.vo.NcProgramVO;
import org.springblade.mdm.utils.FileContentUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
 
/**
 * 工作流服务实现类
 *
 * @author Chill
 */
@Slf4j
@Service
@AllArgsConstructor
public class NcProgramService  extends BizServiceImpl<NcProgramMapper, NcProgram> {
 
    private final MachineService machineService;
    private final OssTemplate ossTemplate;
    private final NcNodeService nodeService;
    private final ProgramSeqMapper seqMapper;
    /**
     * 查询现有固化的程序,暂定条件:零组件号相同,且是同一机床组
     * @param drwaingNo
     * @param machineCode
     * @return
     */
    public List<NcProgram> getCuredNcProgram(String drwaingNo,String machineCode) {
        Machine machine = machineService.getByCode(machineCode);
        return this.getBaseMapper().getCuredNcProgram(drwaingNo,machine.getMachineGroupCode());
    }
 
 
    /**
     * 上传程序文件到指定节点
     * @param uploadVO 上传文件表单数据
     */
    public void uploadProgramFile(NcProgramUploadVO uploadVO) {
 
        MultipartFile file = uploadVO.getFile();
        BladeFile bfile = ossTemplate.putFile(uploadVO.getFile());
        String link = bfile.getLink();
        //保存profame
        NcProgram prog = new NcProgram();
        prog.setCode(generageCode());
        prog.setName(file.getOriginalFilename());
        prog.setNcNodeId(uploadVO.getNodeId());
        prog.setOssName(bfile.getName());
        prog.setDrawingNo(uploadVO.getDrawingNo());
        prog.setProcessEdition(uploadVO.getProcessEdition());
        prog.setIsLastEdition(1);
        boolean isTextFile = false;
        try {
            isTextFile = FileContentUtil.isTextFile(file.getInputStream());
        } catch (IOException e) {
            log.warn("判断是否文本文件异常",e);
        }
        prog.setIsTextFile(isTextFile);
        prog.setUrl(link);
        prog.setCategory(uploadVO.getCategory());
 
        this.save(prog);
    }
 
    /**
     * 上传程序文件到指定节点
     * @param uploadVO 上传文件表单数据
     */
    public void uploadProgramFileNew(NcProgramUploadVO uploadVO) {
 
        MultipartFile file = uploadVO.getFile();
        BladeFile bfile = ossTemplate.putFile(uploadVO.getFile());
        String link = bfile.getLink();
        //保存profame
        NcProgram prog = new NcProgram();
        prog.setCode(generageCode());
        prog.setName(file.getOriginalFilename());
        prog.setNcNodeId(uploadVO.getNodeId());
        prog.setOssName(bfile.getName());
        prog.setDrawingNo(uploadVO.getDrawingNo());
        prog.setProcessEdition(uploadVO.getProcessEdition());
        prog.setIsLastEdition(1);
        prog.setMachineCode(uploadVO.getMachineCode());
        prog.setProcessName(uploadVO.getProcessName());
        boolean isTextFile = false;
        try {
            isTextFile = FileContentUtil.isTextFile(file.getInputStream());
        } catch (IOException e) {
            log.warn("判断是否文本文件异常",e);
        }
        prog.setIsTextFile(isTextFile);
        prog.setUrl(link);
        prog.setCategory(uploadVO.getCategory());
 
        NcNode node = new NcNode();
        node.setNodeType("70");//文件节点
        node.setName(prog.getName());
        node.setMachineCode(uploadVO.getMachineCode());
        node.setParentId(uploadVO.getNodeId());
        node.setDrawingNo(uploadVO.getDrawingNo());
        node.setProcessName(uploadVO.getProcessName());
 
        NcNode parentNode = nodeService.getById(uploadVO.getNodeId());
        node.setParentIds(parentNode.getParentIds()+","+uploadVO.getNodeId());
        //uploadVO.getre
        //node.setDescription(uploadVO.getDrawingNo());
        nodeService.save(node);
 
        prog.setBindNcNodeId(node.getId());
        this.save(prog);
    }
 
    String generageCode(){
        ProgramSeq program = new ProgramSeq();
        seqMapper.insert(program);
 
        DecimalFormat df = new DecimalFormat("0000000000");
        return df.format(program.getId());
    }
    /**
     * 删除一个程序
     * @param id 程序id
     */
    public String getFileContent(Long id) {
        String result  ="";
 
        NcProgram prog = this.getById(id);
        if(prog.getIsTextFile()!=null && prog.getIsTextFile()){
            String fileName = prog.getOssName();
            try (InputStream inputStream = ossTemplate.statFileStream(fileName)) {
                result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else{
            result = "非文本格式文件";
        }
        return result;
    }
 
    /**
     * 获取文件内饿哦那个
     * @param ncNodeId 节点id
     */
    public String getFileContentByNodeId(Long ncNodeId) {
        String result  ="";
        NcNode node = this.nodeService.getById(ncNodeId);
        LambdaQueryWrapper<NcProgram> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NcProgram::getBindNcNodeId, ncNodeId);
        wrapper.eq(NcProgram::getIsLastEdition,1);
        NcProgram prog = this.getOne(wrapper);
        if(prog.getIsTextFile()!=null && prog.getIsTextFile()){
            String fileName = prog.getOssName();
            try (InputStream inputStream = ossTemplate.statFileStream(fileName)) {
                result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }else{
            result = "非文本格式文件";
        }
        return result;
    }
    /**
     * 删除一个程序
     * @param id
     */
    public void removeProgram(Long id) {
        NcProgram prog = this.getById(id);
        ossTemplate.removeFile(prog.getOssName());
        this.getBaseMapper().deleteById(id);
    }
 
    public List<NcProgramVO> listByNode(Long nodeId) {
        NcNode node = nodeService.getById(nodeId);
        List<NcProgram> pList = this.list(Wrappers.lambdaQuery(NcProgram.class).eq(NcProgram::getNcNodeId, nodeId).eq(NcProgram::getIsLastEdition,1).orderByDesc(NcProgram::getCreateTime));
 
        List<NcProgramVO> result = new ArrayList<>();
 
        pList.forEach(prog -> {
            NcProgramVO vo = new NcProgramVO();
            BeanUtils.copyProperties(prog, vo);
            vo.setMachineCode(node.getMachineCode());
            result.add(vo);
        });
 
        return result;
    }
 
    /**
     * 升版(升级工序版次),升级工序版本(包括程序和其他附件)
     * @param bindNcNodeId 程序文件绑定的节点id
     * @param newProcessEdition 新版次
     */
    public void upgradeProcessEdition(Long bindNcNodeId,String newProcessEdition) {
        //NcProgram prog = this.getById(id);
        NcProgram prog = getByBindNodeId(bindNcNodeId);
 
        //将现有程序更新为非最新版本
        this.update(Wrappers.lambdaUpdate(NcProgram.class).eq(NcProgram::getBindNcNodeId,bindNcNodeId).set(NcProgram::getIsLastEdition,0));
 
        NcProgram newVerProg = new NcProgram();
        BeanUtils.copyProperties(prog, newVerProg);
        newVerProg.setId(null);
        newVerProg.setIsLastEdition(1);
        newVerProg.setProcessEdition(newProcessEdition);
 
        this.save(newVerProg);
    }
 
    /**
     * 分页查询,用于审批时选择程序
     * @param query
     * @return
     */
    public IPage<NcProgramVO> pageQuery(NcNodeProgramQueryVO query) {
        return this.getBaseMapper().pageQuery(Condition.getPage(query),query);
    }
 
    /**
     * 下发给你机床
     * @param bindNcNodeId 与程序绑定的节点id
     */
    public void sendByBindNodeId(Long bindNcNodeId) throws IOException {
        //NcNode node = this.nodeService.getById(bindNodeId);
        LambdaQueryWrapper<NcProgram> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NcProgram::getBindNcNodeId, bindNcNodeId);
        wrapper.eq(NcProgram::getIsLastEdition,1);
        NcProgram prog = this.getOne(wrapper);
 
        String filePath = getFilePath(prog);
        try(InputStream ins = ossTemplate.statFileStream(prog.getOssName());){
            File targetFile = new File(filePath);
            FileUtils.copyInputStreamToFile(ins, targetFile);
        }
    }
 
    String getFilePath(NcProgram prog){
        Machine machine = machineService.getByCode(prog.getMachineCode());
        String dirPath = machine.getProgSendDir();
 
        dirPath = StringUtils.removeEnd(StringUtils.removeEnd(dirPath,"/"),"\\");
        File dirs = new File(dirPath);
        if(!dirs.exists()){
            dirs.mkdirs();
        }
 
        return dirPath+File.separator+prog.getName();
    }
 
    /**
     *
     * @param bindNcNodeId
     * @return
     */
    public NcProgram getByBindNodeId(Long bindNcNodeId) {
 
        LambdaQueryWrapper<NcProgram> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NcProgram::getBindNcNodeId, bindNcNodeId);
        wrapper.eq(NcProgram::getIsLastEdition,1);
 
        Optional<NcProgram> progOpt = this.getOneOpt(wrapper);
 
        return progOpt.orElse(null);
    }
 
    /**
     * 根据程序名称获取最新的程序数据
     * @param name 程序名称
     * @return
     */
    public NcProgram getByName(String name) {
        LambdaQueryWrapper<NcProgram> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NcProgram::getName, name);
        wrapper.eq(NcProgram::getIsLastEdition,1);
        Optional<NcProgram> progOpt = this.getOneOpt(wrapper);
 
        return progOpt.orElse(null);
    }
 
    /**
     *
     * @param nodeId 上级节点id
     * @param name 程序名称
     */
    public boolean programExistsUnderNode(Long nodeId, String name) {
        LambdaQueryWrapper<NcProgram> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(nodeId!=null,NcProgram::getNcNodeId, nodeId);
        wrapper.eq(NcProgram::getName, name);
        wrapper.eq(NcProgram::getIsLastEdition,1);
 
        return this.count(wrapper)>0;
    }
 
    /**
     *
     * @param nodeId 上级节点id
     * @param name 程序名称
     */
    public boolean programExistsUnderOtherNode(Long nodeId, String name) {
        LambdaQueryWrapper<NcProgram> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(NcProgram::getNcNodeId, nodeId);
        wrapper.eq(NcProgram::getName, name);
        wrapper.eq(NcProgram::getIsLastEdition,1);
 
        return this.count(wrapper)>0;
    }
 
    /**
     * 显示程序的历史记录列表
     * @param bindNodeId
     * @return
     */
    public List<NcProgramVO> historyByBindNode(Long bindNodeId) {
 
        NcProgram program = this.getByBindNodeId(bindNodeId);
        List<NcProgram> list = this.lambdaQuery().eq(NcProgram::getName,program.getName())
            .eq(NcProgram::getNcNodeId,program.getNcNodeId()).orderByDesc(NcProgram::getCreateTime).list();
 
        return list.stream().map(p -> {
            NcProgramVO vo = new NcProgramVO();
            BeanUtils.copyProperties(p,vo);
            return vo;}).toList();
    }
}