/**
* BladeX Commercial License Agreement
* Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
*
* Use of this software is governed by the Commercial License Agreement
* obtained after purchasing a license from BladeX.
*
* 1. This software is for development use only under a valid license
* from BladeX.
*
* 2. Redistribution of this software's source code to any third party
* without a commercial license is strictly prohibited.
*
* 3. Licensees may copyright their own code but cannot use segments
* from this software for such purposes. Copyright of this software
* remains with BladeX.
*
* Using this software signifies agreement to this License, and the software
* must not be used for illegal purposes.
*
* THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
* not liable for any claims arising from secondary or illegal development.
*
* 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;
/**
* 工作流服务实现类
*
* @author Chill
*/
@Slf4j
@Service
@AllArgsConstructor
public class NcProgramService extends BizServiceImpl {
private final MachineService machineService;
private final OssTemplate ossTemplate;
private final NcNodeService nodeService;
private final ProgramSeqMapper seqMapper;
/**
* 查询现有固化的程序,暂定条件:零组件号相同,且是同一机床组
* @param drwaingNo
* @param machineCode
* @return
*/
public List 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());
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());
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 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 listByNode(Long nodeId) {
NcNode node = nodeService.getById(nodeId);
List pList = this.list(Wrappers.lambdaQuery(NcProgram.class).eq(NcProgram::getNcNodeId, nodeId).eq(NcProgram::getIsLastEdition,1).orderByDesc(NcProgram::getCreateTime));
List result = new ArrayList<>();
pList.forEach(prog -> {
NcProgramVO vo = new NcProgramVO();
BeanUtils.copyProperties(prog, vo);
vo.setMachineCode(node.getMachineCode());
result.add(vo);
});
return result;
}
/**
* 升版(升级工序版次),升级工序版本(包括程序和其他附件)
* @param id 程序文件id
* @param newProcessEdition 新版次
*/
public void upgradeProcessEdition(long id,String newProcessEdition) {
NcProgram prog = this.getById(id);
//将现有程序更新为非最新版本
this.update(Wrappers.lambdaUpdate(NcProgram.class).eq(NcProgram::getId,id).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 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 wrapper = new LambdaQueryWrapper<>();
wrapper.eq(NcProgram::getBindNcNodeId, bindNcNodeId);
wrapper.eq(NcProgram::getIsLastEdition,1);
NcProgram prog = this.getOne(wrapper);
String filePath = prog.getName();
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();
}
}