yangys
2025-09-19 b0d0191a88912b352385349461b500a4964d693b
blade-service/blade-mdm/src/main/java/org/springblade/mdm/basesetting/machine/service/MachineService.java
@@ -1,6 +1,7 @@
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;
@@ -49,6 +50,9 @@
      if(existsByCode(vo.getCode(),null)){
         throw new ServiceException("机床编码已存在:"+vo.getCode());
      }
      if(existsByEquipmentCode(vo.getEquipmentCode(),null)){
         throw new ServiceException("设备编号已存在:"+vo.getEquipmentCode());
      }
      Machine machine = new Machine();
      BeanUtil.copyProperties(vo, machine);
@@ -63,7 +67,6 @@
      Path dir;
      if(StringUtils.isNotBlank(machine.getProgSendDir())){
         createDirIsNotExists(machine.getProgSendDir());
      }
      if(StringUtils.isNotBlank(machine.getProgReceiveDir())){
@@ -168,6 +171,17 @@
   }
   /**
    * 根据 设备编号判断机床是否存在
    * @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
@@ -178,7 +192,9 @@
      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();
@@ -186,6 +202,7 @@
      machine.setMachineSpec(vo.getMachineSpec());
      machine.setCode(vo.getCode());
      machine.setEquipmentCode(vo.getEquipmentCode());
      machine.setName(vo.getName());
      machine.setMachineGroupCode(vo.getMachineGroupCode());
      machine.setManufacturer(vo.getManufacturer());
@@ -321,4 +338,31 @@
      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 sendPath = escapeSqlWildcard(StringUtils.trim(StringUtils.removeEnd(StringUtils.removeStart(sendPathLine,"("),")")));
      List<Machine> machines = lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).likeLeft(Machine::getProgSendDir,sendPath).list();
      if(machines.isEmpty()){
         return null;
      }else{
         return machines.get(0);
      }
    }
}