| | |
| | | |
| | | package org.springblade.mdm.gkw.programnode.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.tool.utils.Func; |
| | | 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.basesetting.producedivision.entity.MdmDept; |
| | | import org.springblade.mdm.gkw.programnode.entity.MachineFile; |
| | | import org.springblade.mdm.gkw.programnode.entity.MachineFileChangeHis; |
| | | import org.springblade.mdm.gkw.programnode.mapper.MachineFileMapper; |
| | | import org.springblade.mdm.gkw.programnode.vo.MachineFileQueryVO; |
| | | import org.springblade.mdm.machineback.vo.MachineBackFileHandleQueryVO; |
| | | import org.springblade.mdm.machineback.vo.MachineBackFileQueryVO; |
| | | import org.springblade.mdm.machineback.vo.MachineBackFileVO; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | 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.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class MachineFileService extends BizServiceImpl<MachineFileMapper, MachineFile> { |
| | | private final MachineService machineService; |
| | | private final MachineFileChangeHisService machineFileChangeHisService; |
| | | private final OssTemplate ossTemplate; |
| | | /** |
| | | * 检测文件是否存在 |
| | | * @param name 文件名 |
| | | * @param dirType 文件夹类型 |
| | | * @param machineCode 所属机床 |
| | | * @return 是否存在于库内 |
| | | */ |
| | | public boolean fileExists(String name, String dirType,String machineCode) { |
| | | return this.lambdaQuery().eq(MachineFile::getName, name) |
| | | .eq(MachineFile::getDirType, dirType) |
| | | .eq(MachineFile::getMachineCode, machineCode).count()>0; |
| | | } |
| | | |
| | | @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); |
| | | |
| | | BladeFile bfile; |
| | | try (InputStream inputStream = Files.newInputStream(filePath)) { |
| | | bfile = ossTemplate.putFile(machineFile.getName(), inputStream); |
| | | } |
| | | String charsetName = "UTF-8"; |
| | | try (InputStream inputStream = Files.newInputStream(filePath)) { |
| | | // 使用输入流读取文件内容 |
| | | charsetName = FileContentUtil.detectFromInputStream(inputStream); |
| | | |
| | | //保存历史记录 |
| | | MachineFileChangeHis his = new MachineFileChangeHis(); |
| | | his.setMachineFileId(id); |
| | | his.setOssName(bfile.getName()); |
| | | |
| | | this.machineFileChangeHisService.save(his); |
| | | } catch (IOException e) { |
| | | log.error("读取文件编码失败",e); |
| | | throw new ServiceException("获取文件编码失败"); |
| | | } |
| | | FileUtils.writeStringToFile(filePath.toFile(),content,charsetName); |
| | | } |
| | | |
| | | /** |
| | | * 获取文件的输入流 |
| | | * @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<MachineBackFileVO> machineBackFilePageQuery(MachineBackFileQueryVO query) { |
| | | return this.baseMapper.machineBackFilePageQuery(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<MachineBackFileVO> handlePageQuery(MachineBackFileHandleQueryVO query) { |
| | | IPage<MachineBackFileVO> 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); |
| | | } |
| | | } |