| | |
| | | */ |
| | | 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.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; |
| | | |
| | | /** |
| | | * 工作流服务实现类 |
| | |
| | | public class NcProgramService extends BizServiceImpl<NcProgramMapper, NcProgram> { |
| | | |
| | | private final MachineService machineService; |
| | | |
| | | private final OssTemplate ossTemplate; |
| | | private final NcNodeService nodeService; |
| | | private final ProgramSeqMapper seqMapper; |
| | | /** |
| | | * 查询现有固化的程序,暂定条件:零组件号相同,且是同一机床组 |
| | | * @param partNo |
| | | * @param drwaingNo |
| | | * @param machineCode |
| | | * @return |
| | | */ |
| | | public NcProgram getCuredNcProgram(String partNo,String machineCode) { |
| | | public List<NcProgram> getCuredNcProgram(String drwaingNo,String machineCode) { |
| | | Machine machine = machineService.getByCode(machineCode); |
| | | return this.getBaseMapper().getCuredNcProgram(partNo,machine.getMachineGroupCode()); |
| | | return this.getBaseMapper().getCuredNcProgram(drwaingNo,machine.getMachineGroupCode()); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 上传程序文件到指定节点 |
| | | * @param file |
| | | * @param nodeId |
| | | * @param category 文件分类(程序文件/其他文件) |
| | | * @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()); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 上传程序文件到指定节点 |
| | | * @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()); |
| | | |
| | | 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 |
| | | * @param id 程序id |
| | | */ |
| | | public String getFileContent(Long id) { |
| | | String result =""; |
| | | |
| | | NcProgram prog = this.getById(id); |
| | | String fileName = prog.getOssName(); |
| | | try (InputStream inputStream = ossTemplate.statFileStream(fileName)) { |
| | | result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | 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; |
| | | } |
| | |
| | | |
| | | public List<NcProgramVO> listByNode(Long nodeId) { |
| | | NcNode node = nodeService.getById(nodeId); |
| | | List<NcProgram> pList = this.list(Wrappers.lambdaQuery(NcProgram.class).eq(NcProgram::getNcNodeId, nodeId).orderByDesc(NcProgram::getCreateTime)); |
| | | 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<>(); |
| | | |
| | |
| | | |
| | | 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 = 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(); |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * @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); |
| | | } |
| | | } |