fix all via Repository.DbContext
This commit is contained in:
@@ -1,30 +1,25 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using VolPro.Core.DbSqlSugar;
|
||||
using VolPro.Core.Extensions;
|
||||
using VolPro.Entity.DomainModels;
|
||||
using Warehouse.IServices;
|
||||
using Warehouse.IRepositories;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace VolPro.Warehouse.Services;
|
||||
|
||||
/// <summary>
|
||||
/// MC4.0 同步引擎:对象树 → 区域匹配 + 设备 Upsert
|
||||
/// </summary>
|
||||
/// <summary>MC4.0 同步引擎:对象树 → 区域匹配 + 设备 Upsert</summary>
|
||||
public class SyncEngine
|
||||
{
|
||||
private readonly IServiceProvider _sp;
|
||||
|
||||
public SyncEngine(IServiceProvider sp) => _sp = sp;
|
||||
|
||||
private Iwarehouse_regionsService GetRegionService() =>
|
||||
_sp.GetService<Iwarehouse_regionsService>()!;
|
||||
private Iwarehouse_devicepointService GetPointService() =>
|
||||
_sp.GetService<Iwarehouse_devicepointService>()!;
|
||||
private Ibase_deviceService GetDeviceService() =>
|
||||
_sp.GetService<Ibase_deviceService>()!;
|
||||
private Iwarehouse_regionsRepository GetRegionRepo() =>
|
||||
_sp.GetService<Iwarehouse_regionsRepository>()!;
|
||||
private Iwarehouse_devicepointRepository GetPointRepo() =>
|
||||
_sp.GetService<Iwarehouse_devicepointRepository>()!;
|
||||
private Ibase_deviceRepository GetDeviceRepo() =>
|
||||
_sp.GetService<Ibase_deviceRepository>()!;
|
||||
|
||||
/// <summary>处理 MC4.0 对象树,匹配区域并 Upsert 设备</summary>
|
||||
public async Task<SyncStats> ProcessMc4TreeAsync(
|
||||
int gatewayNodeId, string adapterCode, List<Mc4TreeNode> tree)
|
||||
public async Task<SyncStats> ProcessMc4TreeAsync(int gatewayNodeId, string adapterCode, List<Mc4TreeNode> tree)
|
||||
{
|
||||
var stats = new SyncStats();
|
||||
foreach (var node in tree)
|
||||
@@ -35,14 +30,13 @@ public class SyncEngine
|
||||
private async Task ProcessNodeAsync(int gatewayNodeId, string adapterCode,
|
||||
Mc4TreeNode node, int? parentDeviceId, SyncStats stats)
|
||||
{
|
||||
if (node.Type == 1) // 区域节点 → 匹配 warehouse_regions + warehouse_devicepoint
|
||||
if (node.Type == 1)
|
||||
{
|
||||
int pointId = await MatchOrCreatePoint(node);
|
||||
// 递归处理子节点,子设备归属到此点位
|
||||
foreach (var child in node.Children)
|
||||
await ProcessNodeAsync(gatewayNodeId, adapterCode, child, null, stats);
|
||||
}
|
||||
else if (node.Type == 2) // 设备节点 → Upsert base_device
|
||||
else if (node.Type == 2)
|
||||
{
|
||||
int deviceId = await UpsertDevice(gatewayNodeId, adapterCode, node, parentDeviceId);
|
||||
if (stats.DeviceIds.TryGetValue(node.Id, out _))
|
||||
@@ -58,34 +52,25 @@ public class SyncEngine
|
||||
|
||||
private async Task<int> MatchOrCreatePoint(Mc4TreeNode node)
|
||||
{
|
||||
// 按名称匹配已有区域
|
||||
var regionSvc = GetRegionService();
|
||||
var region = await regionSvc.FindAsIQueryable(x => x.RegionName == node.Name)
|
||||
.FirstOrDefaultAsync();
|
||||
var regionRepo = GetRegionRepo();
|
||||
var regionDb = regionRepo.DbContext;
|
||||
|
||||
var region = regionDb.Queryable<warehouse_regions>()
|
||||
.First(x => x.RegionName == node.Name);
|
||||
if (region == null)
|
||||
{
|
||||
region = new warehouse_regions
|
||||
{
|
||||
RegionName = node.Name ?? $"MC4_{node.Id}",
|
||||
ParentId = null,
|
||||
|
||||
};
|
||||
regionSvc.Add<object>(region);
|
||||
region = new warehouse_regions { RegionName = node.Name ?? $"MC4_{node.Id}" };
|
||||
regionDb.Insertable(region).ExecuteCommand();
|
||||
}
|
||||
|
||||
// 在此区域下找/建点位
|
||||
var pointSvc = GetPointService();
|
||||
var point = await pointSvc.FindAsIQueryable(x => x.RegionId == region.Id)
|
||||
.FirstOrDefaultAsync();
|
||||
var pointRepo = GetPointRepo();
|
||||
var pointDb = pointRepo.DbContext;
|
||||
var point = pointDb.Queryable<warehouse_devicepoint>()
|
||||
.First(x => x.RegionId == region.Id);
|
||||
if (point == null)
|
||||
{
|
||||
point = new warehouse_devicepoint
|
||||
{
|
||||
PointName = node.Name ?? $"MC4_PT_{node.Id}",
|
||||
RegionId = region.Id,
|
||||
|
||||
};
|
||||
pointSvc.Add<object>(point);
|
||||
point = new warehouse_devicepoint { PointName = node.Name ?? $"MC4_PT_{node.Id}", RegionId = region.Id };
|
||||
pointDb.Insertable(point).ExecuteCommand();
|
||||
}
|
||||
return point.PointID;
|
||||
}
|
||||
@@ -93,11 +78,12 @@ public class SyncEngine
|
||||
private async Task<int> UpsertDevice(int gatewayNodeId, string adapterCode,
|
||||
Mc4TreeNode node, int? parentDeviceId)
|
||||
{
|
||||
var svc = GetDeviceService();
|
||||
var deviceRepo = GetDeviceRepo();
|
||||
var db = deviceRepo.DbContext;
|
||||
var sourceId = node.Id.ToString();
|
||||
var existing = await svc.FindAsIQueryable(
|
||||
x => x.AdapterCode == adapterCode && x.SourceId == sourceId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
var existing = db.Queryable<base_device>()
|
||||
.First(x => x.AdapterCode == adapterCode && x.SourceId == sourceId);
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
@@ -105,33 +91,29 @@ public class SyncEngine
|
||||
existing.LastSyncTime = DateTime.Now;
|
||||
existing.ParentDeviceId = parentDeviceId ?? existing.ParentDeviceId;
|
||||
if (node.Option != null)
|
||||
existing.ExtraData = System.Text.Json.JsonSerializer.Serialize(node.Option);
|
||||
existing.SetModifyDefaultVal();
|
||||
// 通过 ServiceBase 的基础方法更新;
|
||||
existing.ExtraData = JsonSerializer.Serialize(node.Option);
|
||||
db.Updateable(existing).ExecuteCommand();
|
||||
return existing.DeviceId;
|
||||
}
|
||||
else
|
||||
|
||||
var device = new base_device
|
||||
{
|
||||
var device = new base_device
|
||||
{
|
||||
DeviceName = node.Name ?? $"MC4_DEV_{node.Id}",
|
||||
AdapterCode = adapterCode,
|
||||
SourceId = sourceId,
|
||||
DeviceCategory = MapCategory(node.ObjectType, node.Tag),
|
||||
DeviceGroup = "IoT设备",
|
||||
GatewayNodeId = gatewayNodeId,
|
||||
ParentDeviceId = parentDeviceId,
|
||||
IsParent = node.Children?.Count > 0 ? "是" : "否",
|
||||
IsOnline = "在线",
|
||||
Enable = "启用",
|
||||
LastSyncTime = DateTime.Now,
|
||||
ExtraData = node.Option != null
|
||||
? System.Text.Json.JsonSerializer.Serialize(node.Option)
|
||||
: null
|
||||
};
|
||||
svc.Add<object>(device);
|
||||
return device.DeviceId;
|
||||
}
|
||||
DeviceName = node.Name ?? $"MC4_DEV_{node.Id}",
|
||||
AdapterCode = adapterCode,
|
||||
SourceId = sourceId,
|
||||
DeviceCategory = MapCategory(node.ObjectType, node.Tag),
|
||||
DeviceGroup = "IoT设备",
|
||||
GatewayNodeId = gatewayNodeId,
|
||||
ParentDeviceId = parentDeviceId,
|
||||
IsParent = node.Children?.Count > 0 ? "是" : "否",
|
||||
IsOnline = "在线",
|
||||
Enable = "启用",
|
||||
LastSyncTime = DateTime.Now,
|
||||
CreateDate = DateTime.Now,
|
||||
ExtraData = node.Option != null ? JsonSerializer.Serialize(node.Option) : null
|
||||
};
|
||||
db.Insertable(device).ExecuteCommand();
|
||||
return device.DeviceId;
|
||||
}
|
||||
|
||||
private static string MapCategory(int objectType, string? tag) =>
|
||||
@@ -145,14 +127,7 @@ public class SyncEngine
|
||||
};
|
||||
}
|
||||
|
||||
public class SyncStats
|
||||
{
|
||||
public int Added { get; set; }
|
||||
public int Updated { get; set; }
|
||||
public Dictionary<int, int> DeviceIds { get; set; } = new();
|
||||
}
|
||||
|
||||
/// <summary>简化的 MC4 树节点(网关→Vol.Pro 传输用)</summary>
|
||||
public class SyncStats { public int Added; public int Updated; public Dictionary<int, int> DeviceIds = new(); }
|
||||
public class Mc4TreeNode
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
Reference in New Issue
Block a user