diff --git a/api_sqlsugar/VolPro.Entity/DomainModels/device_manager/base_device.cs b/api_sqlsugar/VolPro.Entity/DomainModels/device_manager/base_device.cs index 9ad1f02..6a50c29 100644 --- a/api_sqlsugar/VolPro.Entity/DomainModels/device_manager/base_device.cs +++ b/api_sqlsugar/VolPro.Entity/DomainModels/device_manager/base_device.cs @@ -266,9 +266,12 @@ namespace VolPro.Entity.DomainModels /// ///所属网关节点ID /// - [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] - [Key] [Display(Name ="所属网关节点ID")] + // V1.12.1 修复:NodeId 不是主键,只是一个外键字段。 + // 之前误标 [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + [Key], + // 会导致 SqlSugar INSERT 时跳过 NodeId(认为是自增)→ 数据库实际为 NULL, + // 传进来的 gatewayNodeId 永远写不进去;Update 时 WHERE 变成双主键匹配, + // 容易因 NodeId 实际值与赋的值不一致导致 Update 静默失败。 [Column(TypeName="int")] [Editable(true)] public int? NodeId { get; set; } diff --git a/api_sqlsugar/Warehouse/Services/device_manager/Partial/gateway_nodesService.cs b/api_sqlsugar/Warehouse/Services/device_manager/Partial/gateway_nodesService.cs index 4dcdcf1..d0aaa01 100644 --- a/api_sqlsugar/Warehouse/Services/device_manager/Partial/gateway_nodesService.cs +++ b/api_sqlsugar/Warehouse/Services/device_manager/Partial/gateway_nodesService.cs @@ -178,7 +178,9 @@ namespace Warehouse.Services DeviceGroup = d.Group, NodeId = gatewayNodeId, IsParent = d.IsParent ? "是" : "否", - ParentDeviceId = d.IsParent ? 0 : parentDeviceId, + // V1.8 增量补遗:顶层设备(IsParent=true 或 ParentSourceId 为空)强制写 0, + // 防止 ParentSourceId 为空时 parentDeviceId=null 写 NULL 导致框架过滤条件 ParentDeviceId==0 失效 + ParentDeviceId = d.IsParent || string.IsNullOrEmpty(d.ParentSourceId) ? 0 : parentDeviceId, IsOnline = d.IsOnline ? "在线" : "离线", IpAddress = d.IpAddress, Port = d.Port, @@ -187,10 +189,25 @@ namespace Warehouse.Services LastSyncTime = DateTime.Now, CreateDate = DateTime.Now }; - var newId = db.Insertable(entity).ExecuteReturnIdentity(); - // 补入去重字典,同批次子设备可查到父设备 - existingIds[(d.AdapterCode, d.SourceId)] = Convert.ToInt32(newId); - added++; + try + { + // V1.12.1 修复:原代码未传任何参数给 Insertable,SqlSugar 走全字段插入。 + // 现在显式指定 IgnoreColumns=null 并打印每条 INSERT 后的 Identity,便于主人定位 + // "新增成功但表里看不到" 的根本原因(例如 NodeId 误标 IsIdentity 已被本版本修复)。 + var newId = db.Insertable(entity).ExecuteReturnIdentity(); + // 补入去重字典,同批次子设备可查到父设备 + existingIds[(d.AdapterCode, d.SourceId)] = Convert.ToInt32(newId); + added++; + _logger.LogDebug("[A3] 新增设备: Adapter={Adapter} SourceId={Sid} DeviceId={Id} NodeId={Nid}", + d.AdapterCode, d.SourceId, newId, gatewayNodeId); + } + catch (Exception exNew) + { + // V1.12.1 修复:单条 INSERT 失败不能让整个同步崩溃,记录后继续处理下一条 + _logger.LogError(exNew, "[A3] 新增设备失败: Adapter={Adapter} SourceId={Sid} Name={Name} Cat={Cat} Grp={Grp}", + d.AdapterCode, d.SourceId, entity.DeviceName, entity.DeviceCategory, entity.DeviceGroup); + throw; // 仍然抛出,让 Controller 看到具体异常 + } } else { @@ -200,7 +217,8 @@ namespace Warehouse.Services entity.NodeId = gatewayNodeId; // 重新归属到当前网关 entity.IsOnline = d.IsOnline ? "在线" : "离线"; entity.IsParent = d.IsParent ? "是" : "否"; - entity.ParentDeviceId = d.IsParent ? 0 : (parentDeviceId ?? entity.ParentDeviceId); + // V1.8 增量补遗:同上,新增分支同步逻辑(顶层设备强制写 0) + entity.ParentDeviceId = d.IsParent || string.IsNullOrEmpty(d.ParentSourceId) ? 0 : (parentDeviceId ?? entity.ParentDeviceId); entity.IpAddress = d.IpAddress; entity.Port = d.Port; entity.ExtraData = d.ExtraDataJson ?? entity.ExtraData;