package com.cmvr.inspection.service; import com.cmvr.common.exception.GlobalException; import com.cmvr.common.robot.RobotEndpoint; import com.cmvr.common.robot.RobotEndpointResolver; import com.cmvr.inspection.domain.InspectionRobot; import com.cmvr.inspection.mapper.InspectionRobotMapper; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.util.List; @Service @RequiredArgsConstructor public class InspectionRobotEndpointResolver implements RobotEndpointResolver { private final InspectionRobotMapper robotMapper; @Override public RobotEndpoint resolve(String robotId) { String normalized = StringUtils.trimToNull(robotId); if (normalized == null) { throw new GlobalException("机器人ID不能为空"); } List robots = robotMapper.selectByRobotId(normalized); if (robots.isEmpty()) { throw new GlobalException("机器人不存在:" + normalized); } if (robots.size() > 1) { throw new GlobalException("机器人ID存在重复数据:" + normalized); } InspectionRobot robot = robots.get(0); if ("1".equals(robot.getArchivedStatus())) { throw new GlobalException("机器人已归档:" + normalized); } if (!"1".equals(robot.getConnectStatus())) { throw new GlobalException("机器人当前离线:" + normalized); } if (StringUtils.isBlank(robot.getIpAddress()) || robot.getPort() == null) { throw new GlobalException("机器人尚未上报gRPC地址:" + normalized); } return new RobotEndpoint(robot.getIpAddress(), robot.getPort().intValue()); } }