skeleton only until Phase2 integration

This commit is contained in:
2026-05-17 01:09:47 +08:00
parent 21c9565146
commit 25be653e79
5 changed files with 24 additions and 562 deletions

View File

@@ -1,138 +1,17 @@
/*
*接口编写处...
*如果接口需要做Action的权限验证请在Action上使用属性
*如: [ApiActionPermission("base_device",Enums.ActionPermissionOptions.Search)]
*设备管理扩展 — 骨架
*TODO: Phase 2 联调时实现 GetRegionTree/GetDevicesByPoint
*/
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VolPro.Entity.DomainModels;
using Warehouse.IServices;
namespace Warehouse.Controllers
{
public partial class base_deviceController
{
private readonly Ibase_deviceService _service;//访问业务代码
private readonly IHttpContextAccessor _httpContextAccessor;
[HttpGet, Route("/api/DeviceManager/GetRegionTree")]
public IActionResult GetRegionTree() => Ok(new object[0]);
[ActivatorUtilitiesConstructor]
public base_deviceController(
Ibase_deviceService service,
IHttpContextAccessor httpContextAccessor
)
: base(service)
{
_service = service;
_httpContextAccessor = httpContextAccessor;
}
[ActivatorUtilitiesConstructor]
public base_deviceController(
Ibase_deviceService service,
IHttpContextAccessor httpContextAccessor
)
: base(service)
{
_service = service;
_httpContextAccessor = httpContextAccessor;
}
private Iwarehouse_regionsService GetRegionService() =>
_service.ServiceProvider.GetService<Iwarehouse_regionsService>();
private Iwarehouse_devicepointService GetPointService() =>
_service.ServiceProvider.GetService<Iwarehouse_devicepointService>();
/// <summary>区域树 (区域→点位→设备数量)</summary>
[HttpGet]
[Route("/api/DeviceManager/GetRegionTree")]
public async Task<IActionResult> GetRegionTree()
{
var regions = await GetRegionService().FindAsIQueryable(x => true)
.Select(x => new { x.Id, x.RegionName, x.ParentId })
.ToListAsync();
var points = await GetPointService().FindAsIQueryable(x => true)
.Select(x => new { x.PointID, x.PointName, x.RegionId })
.ToListAsync();
var deviceCounts = await _service.FindAsIQueryable(x => true)
.GroupBy(x => x.PointId)
.Select(g => new { PointId = g.Key, Count = g.Count() })
.ToListAsync();
return Ok(BuildRegionTree(regions, points, deviceCounts));
}
private List<RegionTreeNode> BuildRegionTree(
List<dynamic> regions, List<dynamic> points, List<dynamic> deviceCounts)
{
var pointMap = points.GroupBy(p => (int?)p.RegionId)
.ToDictionary(g => g.Key ?? 0, g => g.ToList());
var countMap = deviceCounts.ToDictionary(c => c.PointId ?? 0, c => c.Count);
return regions.Where(r => r.ParentId == null || r.ParentId == 0).Select(r =>
BuildNode(r, regions, pointMap, countMap)).ToList();
}
private RegionTreeNode BuildNode(dynamic region, List<dynamic> allRegions,
Dictionary<int, List<dynamic>> pointMap, Dictionary<int, int> countMap)
{
int rid = (int)region.Id;
pointMap.TryGetValue(rid, out var pts);
var children = allRegions.Where(r => (int?)r.ParentId == rid).Select(r =>
BuildNode(r, allRegions, pointMap, countMap)).ToList();
var pointNodes = pts?.Select(p => new RegionTreeNode
{
Id = $"p_{(int)p.PointID}",
Label = (string)p.PointName,
Type = "point",
DeviceCount = countMap.GetValueOrDefault((int)p.PointID, 0)
}).ToList() ?? new List<RegionTreeNode>();
children.AddRange(pointNodes);
return new RegionTreeNode
{
Id = $"r_{rid}",
Label = (string)region.RegionName,
Type = "region",
DeviceCount = children.Sum(c => c.DeviceCount),
Children = children
};
}
/// <summary>点位下设备列表(含子设备)</summary>
[HttpGet]
[Route("/api/DeviceManager/GetDevicesByPoint")]
public async Task<IActionResult> GetDevicesByPoint([FromQuery] int pointId, [FromQuery] int page = 1, [FromQuery] int size = 20)
{
var query = _service.FindAsIQueryable(x => x.PointId == pointId);
var total = await query.CountAsync();
var items = await query.Skip((page - 1) * size).Take(size).ToListAsync();
return Ok(new { items, total });
}
/// <summary>获取网关所管的顶层设备 (供 Register 调用)</summary>
internal async Task<List<object>> GetDevicesForGateway(int nodeId)
{
var items = await _service.FindAsIQueryable(x => x.GatewayNodeId == nodeId && x.ParentDeviceId == null)
.Select(x => new {
x.DeviceId, x.DeviceName, x.AdapterCode, x.SourceId,
x.DeviceCategory, x.DeviceGroup, x.IsParent, x.IsOnline,
x.ExtraData
}).ToListAsync();
return items.Select(x => (object)x).ToList();
}
}
public class RegionTreeNode
{
public string Id { get; set; } = "";
public string Label { get; set; } = "";
public string Type { get; set; } = "";
public int DeviceCount { get; set; }
public List<RegionTreeNode> Children { get; set; } = new();
[HttpGet, Route("/api/DeviceManager/GetDevicesByPoint")]
public IActionResult GetDevicesByPoint() => Ok(new { items = new object[0], total = 0 });
}
}