| | |
| | | package org.springblade.mdm.basesetting.machine.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.MybatisUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.mp.base.BizServiceImpl; |
| | |
| | | import org.springblade.mdm.basesetting.machine.vo.MachineSaveVO; |
| | | import org.springblade.mdm.basesetting.machine.vo.MachineVO; |
| | | import org.springblade.mdm.basesetting.producedivision.service.MdmDeptService; |
| | | import org.springblade.mdm.commons.service.MachineDirTranslator; |
| | | import org.springblade.mdm.commons.service.ParamService; |
| | | import org.springblade.mdm.machinefile.entity.MachineFile; |
| | | import org.springblade.mdm.machinefile.filewatch.DynamicDirectoryWatcher; |
| | |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.util.List; |
| | | import java.util.Optional; |
| | | |
| | | @Service |
| | | public class MachineService extends BizServiceImpl<MachineMapper, Machine> { |
| | |
| | | private DynamicDirectoryWatcher dynamicDirectoryWatcher; |
| | | @Autowired |
| | | private ParamService paramService; |
| | | @Autowired |
| | | private MachineDirTranslator machineDirTranslator; |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void saveMachine(MachineSaveVO vo) throws IOException { |
| | | checkMachine(vo); |
| | | if(existsByCode(vo.getCode(),null)){ |
| | | throw new ServiceException("机床编码已存在:"+vo.getCode()); |
| | | } |
| | | if(existsByEquipmentCode(vo.getEquipmentCode(),null)){ |
| | | throw new ServiceException("设备编号已存在:"+vo.getEquipmentCode()); |
| | | } |
| | | |
| | | Machine machine = new Machine(); |
| | |
| | | void makeMachineDirs(Machine machine) throws IOException { |
| | | Path dir; |
| | | if(StringUtils.isNotBlank(machine.getProgSendDir())){ |
| | | createDirIsNotExists(machine.getProgSendDir()); |
| | | |
| | | createDirIsNotExists(machineDirTranslator.trans(machine.getProgSendDir())); |
| | | } |
| | | |
| | | if(StringUtils.isNotBlank(machine.getProgReceiveDir())){ |
| | | createDirIsNotExists(machine.getProgReceiveDir()); |
| | | if(StringUtils.isNotBlank(machineDirTranslator.trans(machine.getProgReceiveDir()))){ |
| | | createDirIsNotExists(machineDirTranslator.trans(machine.getProgReceiveDir())); |
| | | } |
| | | |
| | | if(StringUtils.isNotBlank(machine.getProgTempDir())){ |
| | | createDirIsNotExists(machine.getProgTempDir()); |
| | | if(StringUtils.isNotBlank(machineDirTranslator.trans(machine.getProgTempDir()))){ |
| | | createDirIsNotExists(machineDirTranslator.trans(machine.getProgTempDir())); |
| | | } |
| | | } |
| | | |
| | |
| | | ProgramAnnotationService annoService = SpringUtil.getBean(ProgramAnnotationService.class); |
| | | |
| | | if(StringUtils.isNotBlank(machine.getProgSendDir())) { |
| | | |
| | | String sendDir = machineDirTranslator.trans(machine.getProgSendDir()); |
| | | |
| | | WatchInfo watchInfoSend = new WatchInfo(); |
| | | watchInfoSend.setMachine(machine); |
| | | watchInfoSend.setDirType(MachineFile.DIR_TYPE_SEND); |
| | | watchInfoSend.setPath(machine.getProgSendDir()); |
| | | watchInfoSend.setPath(sendDir); |
| | | |
| | | if(!dynamicDirectoryWatcher.containsPath(Paths.get(machine.getProgSendDir()))) { |
| | | dynamicDirectoryWatcher.addDirectory(Paths.get(machine.getProgSendDir()), |
| | | Path path = Paths.get(sendDir); |
| | | if(!dynamicDirectoryWatcher.containsPath(path)) { |
| | | dynamicDirectoryWatcher.addDirectory(path, |
| | | new MachineFileChangeListener(machineFileService, |
| | | annoService, |
| | | watchInfoSend)); |
| | |
| | | } |
| | | |
| | | if(StringUtils.isNotBlank(machine.getProgReceiveDir())) { |
| | | |
| | | String recDir = machineDirTranslator.trans(machine.getProgReceiveDir()); |
| | | |
| | | WatchInfo watchInfoRec = new WatchInfo(); |
| | | watchInfoRec.setMachine(machine); |
| | | watchInfoRec.setDirType(MachineFile.DIR_TYPE_REC); |
| | | watchInfoRec.setPath(machine.getProgReceiveDir()); |
| | | if(!dynamicDirectoryWatcher.containsPath(Paths.get(machine.getProgReceiveDir()))) { |
| | | dynamicDirectoryWatcher.addDirectory(Paths.get(machine.getProgReceiveDir()), |
| | | watchInfoRec.setPath(recDir); |
| | | Path path = Paths.get(recDir); |
| | | if(!dynamicDirectoryWatcher.containsPath(path)) { |
| | | dynamicDirectoryWatcher.addDirectory(path, |
| | | new MachineFileChangeListener(machineFileService, |
| | | annoService, |
| | | watchInfoRec)); |
| | |
| | | } |
| | | |
| | | if(StringUtils.isNotBlank(machine.getProgTempDir())) { |
| | | String tempDir = machineDirTranslator.trans(machine.getProgTempDir()); |
| | | WatchInfo watchInfoTemp = new WatchInfo(); |
| | | watchInfoTemp.setMachine(machine); |
| | | watchInfoTemp.setDirType(MachineFile.DIR_TYPE_TEMP); |
| | | watchInfoTemp.setPath(machine.getProgTempDir()); |
| | | if(!dynamicDirectoryWatcher.containsPath(Paths.get(machine.getProgTempDir()))) { |
| | | dynamicDirectoryWatcher.addDirectory(Paths.get(machine.getProgTempDir()), |
| | | watchInfoTemp.setPath(tempDir); |
| | | Path path = Paths.get(machine.getProgTempDir()); |
| | | if(!dynamicDirectoryWatcher.containsPath(path)) { |
| | | dynamicDirectoryWatcher.addDirectory(path, |
| | | new MachineFileChangeListener(machineFileService, |
| | | annoService, |
| | | watchInfoTemp)); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据 设备编号判断机床是否存在 |
| | | * @param equipmentCode 设备编号 |
| | | * @param excludeId 排除id |
| | | * @return 是否存在 |
| | | */ |
| | | boolean existsByEquipmentCode(String equipmentCode,Long excludeId){ |
| | | return this.lambdaQuery().eq(Machine::getEquipmentCode, equipmentCode).ne(excludeId!=null,Machine::getId, excludeId).count()>0; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 修改机床信息 |
| | | * @param vo |
| | | * @return |
| | |
| | | if(existsByCode(vo.getCode(),vo.getId())){ |
| | | throw new ServiceException("机床编码已存在:"+vo.getCode()); |
| | | } |
| | | |
| | | if(existsByEquipmentCode(vo.getEquipmentCode(),vo.getId())){ |
| | | throw new ServiceException("设备编号已存在:"+vo.getEquipmentCode()); |
| | | } |
| | | Machine machine = this.getById(vo.getId()); |
| | | |
| | | Machine machineBak = new Machine(); |
| | |
| | | |
| | | machine.setMachineSpec(vo.getMachineSpec()); |
| | | machine.setCode(vo.getCode()); |
| | | machine.setEquipmentCode(vo.getEquipmentCode()); |
| | | machine.setName(vo.getName()); |
| | | machine.setMachineGroupCode(vo.getMachineGroupCode()); |
| | | machine.setManufacturer(vo.getManufacturer()); |
| | |
| | | return lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list(); |
| | | } |
| | | |
| | | public String escapeSqlWildcard(String value) { |
| | | if (StringUtils.isBlank(value)) { |
| | | return value; |
| | | } |
| | | return value.replace("\\", "\\\\") |
| | | .replace("%", "\\%") |
| | | .replace("_", "\\_"); |
| | | } |
| | | /** |
| | | * 根据下发路径的注释查询机床 |
| | | * @param sendPathLine 下发路径注释文本 |
| | | */ |
| | | public Machine getMachineBySendPathAnnotation(String sendPathLine) { |
| | | //路径为空,不匹配任何机器 |
| | | if(StringUtils.isBlank(sendPathLine)){ |
| | | return null; |
| | | } |
| | | //去掉末尾括号(发那科) ,末尾路径分隔符 |
| | | String oriPath = StringUtils.removeEnd(StringUtils.removeEnd(StringUtils.removeEnd(sendPathLine,")"),"/"),"\\"); |
| | | |
| | | String tempPathRepl1 = StringUtils.replace(oriPath,"/","\\");//反斜杠版本的路径 |
| | | String tempPathRepl2 = StringUtils.replace(oriPath,"\\","/");//正斜杠版本的路径 |
| | | |
| | | //去掉前2位可能的注释(*-)。 |
| | | String sendPath1 = escapeSqlWildcard(StringUtils.trim(StringUtils.substring(tempPathRepl1,2))); |
| | | String sendPath2 = escapeSqlWildcard(StringUtils.trim(StringUtils.substring(tempPathRepl2,2))); |
| | | |
| | | Optional<Machine> machineOpt = lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).and(w -> { |
| | | w.like(Machine::getProgSendDir,sendPath1).or().like(Machine::getProgSendDir,sendPath2); |
| | | }).last("limit 1").oneOpt(); |
| | | |
| | | return machineOpt.orElse(null); |
| | | } |
| | | |
| | | /** |
| | | * 根据设备编号查询机床 |
| | | * @param equipmentCode 设备编号 |
| | | * @return 机床对象 |
| | | */ |
| | | public Machine getByEquipmentCode(String equipmentCode) { |
| | | return this.lambdaQuery().eq(Machine::getEquipmentCode, equipmentCode).one(); |
| | | } |
| | | } |