118 lines
4.8 KiB
C#
118 lines
4.8 KiB
C#
/*
|
|
*所有关于base_device类的业务代码应在此处编写
|
|
*可使用repository.调用常用方法,获取EF/Dapper等信息
|
|
*如果需要事务请使用repository.DbContextBeginTransaction
|
|
*也可使用DBServerProvider.手动获取数据库相关信息
|
|
*用户信息、权限、角色等使用UserContext.Current操作
|
|
*base_deviceService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
|
|
*/
|
|
using VolPro.Core.BaseProvider;
|
|
using VolPro.Core.Extensions.AutofacManager;
|
|
using VolPro.Entity.DomainModels;
|
|
using System.Linq;
|
|
using VolPro.Core.Utilities;
|
|
using System.Linq.Expressions;
|
|
using VolPro.Core.Extensions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Warehouse.IRepositories;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Warehouse.Services
|
|
{
|
|
public partial class base_deviceService
|
|
{
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
private readonly Ibase_deviceRepository _repository;//访问数据库
|
|
|
|
[ActivatorUtilitiesConstructor]
|
|
public base_deviceService(
|
|
Ibase_deviceRepository dbRepository,
|
|
IHttpContextAccessor httpContextAccessor
|
|
)
|
|
: base(dbRepository)
|
|
{
|
|
_httpContextAccessor = httpContextAccessor;
|
|
_repository = dbRepository;
|
|
//多租户会用到这init代码,其他情况可以不用
|
|
//base.Init(dbRepository);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取指定网关节点下的顶层设备列表(用于网关注册时返回所管设备)。
|
|
/// 返回 ParentDeviceId 为 null 的设备。
|
|
/// </summary>
|
|
public async Task<List<base_device>> GetDevicesByGatewayNodeAsync(int gatewayNodeId)
|
|
{
|
|
return await _repository.DbContext.Queryable<base_device>()
|
|
.Where(x => x.NodeId == gatewayNodeId && x.ParentDeviceId == null)
|
|
.ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按字段分治原则 Upsert 单个设备。
|
|
/// 首次入库时写全量(管理员字段),已有记录时仅更新网关字段(IsOnline/ExtraData等)。
|
|
/// </summary>
|
|
/// <param name="d">同步设备条目</param>
|
|
/// <param name="gatewayNodeId">网关节点ID</param>
|
|
/// <param name="existingIds">已有设备映射表 (AdapterCode, SourceId) → DeviceId</param>
|
|
[Obsolete("已迁移至 gateway_nodesService.SyncDevicesAsync")]
|
|
public async Task UpsertDeviceAsync(SyncDeviceItem d, int gatewayNodeId, Dictionary<(string, string), int> existingIds)
|
|
{
|
|
var db = _repository.DbContext;
|
|
var key = (d.AdapterCode, d.SourceId);
|
|
existingIds.TryGetValue(key, out var existingId);
|
|
bool isNew = existingId == 0;
|
|
|
|
// 解析父设备
|
|
int? parentDeviceId = null;
|
|
if (!string.IsNullOrEmpty(d.ParentSourceId))
|
|
{
|
|
existingIds.TryGetValue((d.AdapterCode, d.ParentSourceId), out var pid);
|
|
if (pid > 0) parentDeviceId = pid;
|
|
}
|
|
|
|
if (isNew)
|
|
{
|
|
var entity = new base_device
|
|
{
|
|
DeviceName = d.Name ?? $"DEV_{d.SourceId}",
|
|
AdapterCode = d.AdapterCode,
|
|
SourceId = d.SourceId,
|
|
DeviceCategory = d.Category,
|
|
DeviceGroup = d.Group,
|
|
NodeId = gatewayNodeId,
|
|
IsParent = d.IsParent ? "是" : "否",
|
|
ParentDeviceId = parentDeviceId,
|
|
IsOnline = d.IsOnline ? "在线" : "离线",
|
|
IpAddress = d.IpAddress,
|
|
Port = d.Port,
|
|
ExtraData = d.ExtraDataJson,
|
|
Enable = "启用",
|
|
LastSyncTime = DateTime.Now,
|
|
CreateDate = DateTime.Now
|
|
};
|
|
db.Insertable(entity).ExecuteCommand();
|
|
}
|
|
else
|
|
{
|
|
var entity = db.Queryable<base_device>().InSingle(existingId);
|
|
if (entity != null)
|
|
{
|
|
entity.IsOnline = d.IsOnline ? "在线" : "离线";
|
|
entity.IsParent = d.IsParent ? "是" : "否";
|
|
entity.ParentDeviceId = parentDeviceId ?? entity.ParentDeviceId;
|
|
entity.IpAddress = d.IpAddress;
|
|
entity.Port = d.Port;
|
|
entity.ExtraData = d.ExtraDataJson ?? entity.ExtraData;
|
|
entity.LastSyncTime = DateTime.Now;
|
|
db.Updateable(entity).ExecuteCommand();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|