| | |
| | | import com.mes.device.vo.DeviceConfigVO; |
| | | import com.mes.device.vo.StatisticsVO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.IOException; |
| | |
| | | @Override |
| | | public boolean createDevice(DeviceConfig deviceConfig) { |
| | | try { |
| | | // 若未传则生成设备编码 |
| | | String code = StringUtils.trimToEmpty(deviceConfig.getDeviceCode()); |
| | | if (StringUtils.isBlank(code)) { |
| | | code = generateDeviceCode(); |
| | | deviceConfig.setDeviceCode(code); |
| | | } else { |
| | | deviceConfig.setDeviceCode(code); |
| | | } |
| | | |
| | | // 检查设备编号是否已存在 |
| | | if (isDeviceCodeExists(deviceConfig.getDeviceCode(), null)) { |
| | | log.warn("设备编号已存在: {}", deviceConfig.getDeviceCode()); |
| | | return false; |
| | | if (isDeviceCodeExists(code, null)) { |
| | | log.warn("设备编号已存在: {}", code); |
| | | throw new IllegalArgumentException("设备编码已存在"); |
| | | } |
| | | |
| | | // 兼容旧字段:统一将 device_id 填为 deviceCode,避免非空/唯一约束问题 |
| | | deviceConfig.setDeviceId(code); |
| | | |
| | | // 项目ID未传则使用默认项目(单项目场景可用),避免非空约束 |
| | | if (deviceConfig.getProjectId() == null) { |
| | | deviceConfig.setProjectId(1L); |
| | | } |
| | | |
| | | // 初始化设备状态为离线 |
| | |
| | | return result; |
| | | } catch (Exception e) { |
| | | log.error("创建设备配置失败", e); |
| | | return false; |
| | | throw e; |
| | | } |
| | | } |
| | | |
| | |
| | | if (isDeviceCodeExists(deviceConfig.getDeviceCode(), deviceConfig.getId())) { |
| | | log.warn("设备编号已存在: {}", deviceConfig.getDeviceCode()); |
| | | return false; |
| | | } |
| | | |
| | | // 同步 device_id 与 deviceCode,保持一致 |
| | | if (StringUtils.isNotBlank(deviceConfig.getDeviceCode())) { |
| | | deviceConfig.setDeviceId(deviceConfig.getDeviceCode().trim()); |
| | | } |
| | | |
| | | // 若项目ID缺失,使用默认项目 |
| | | if (deviceConfig.getProjectId() == null) { |
| | | deviceConfig.setProjectId(1L); |
| | | } |
| | | |
| | | boolean result = updateById(deviceConfig); |
| | |
| | | |
| | | @Override |
| | | public boolean isDeviceCodeExists(String deviceCode, Long excludeId) { |
| | | if (StringUtils.isBlank(deviceCode)) { |
| | | return false; |
| | | } |
| | | String trimmed = deviceCode.trim(); |
| | | |
| | | LambdaQueryWrapper<DeviceConfig> wrapper = new LambdaQueryWrapper<>(); |
| | | wrapper.eq(DeviceConfig::getDeviceCode, deviceCode); |
| | | wrapper.eq(DeviceConfig::getDeviceCode, trimmed); |
| | | wrapper.eq(DeviceConfig::getIsDeleted, 0); |
| | | |
| | | if (excludeId != null) { |
| | |
| | | return count(wrapper) > 0; |
| | | } |
| | | |
| | | /** |
| | | * 简单的设备编码生成器:DEV_前缀 |
| | | */ |
| | | private String generateDeviceCode() { |
| | | Long maxNo = getBaseMapper().selectMaxDeviceCodeNumber(); |
| | | long next = (maxNo == null ? 0 : maxNo) + 1; |
| | | // 左侧补零到6位,例如 DEV_000123 |
| | | return String.format("DEV_%06d", next); |
| | | } |
| | | |
| | | @Override |
| | | public boolean updateDeviceStatus(Long id, String status) { |
| | | try { |