V2 Controller扩展: A1-A4网关API + GetRegionTree + GetDevicesByPoint
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
/*
|
||||
*接口编写处...
|
||||
*如果接口需要做Action的权限验证,请在Action上使用属性
|
||||
*如: [ApiActionPermission("base_device",Enums.ActionPermissionOptions.Search)]
|
||||
*设备管理扩展 — 区域树 + 点位设备列表
|
||||
*所有改动在 Partial 目录,不破坏框架可升级性
|
||||
*/
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
@@ -11,23 +10,103 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using VolPro.Entity.DomainModels;
|
||||
using Warehouse.IServices;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Warehouse.Controllers
|
||||
{
|
||||
public partial class base_deviceController
|
||||
{
|
||||
private readonly Ibase_deviceService _service;//访问业务代码
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
[ActivatorUtilitiesConstructor]
|
||||
public base_deviceController(
|
||||
Ibase_deviceService service,
|
||||
IHttpContextAccessor httpContextAccessor
|
||||
)
|
||||
: base(service)
|
||||
/// <summary>
|
||||
/// 获取区域→点位→设备树。
|
||||
/// 用于管理端左侧树形控件展示层级结构。
|
||||
/// 格式: [{ id, label, type:"region", children: [{ id, label, type:"point", deviceCount }] }]
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[Route("/api/DeviceManager/GetRegionTree")]
|
||||
public async Task<IActionResult> GetRegionTree()
|
||||
{
|
||||
_service = service;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
// 获取区域和点位服务
|
||||
var regionSvcType = _service.GetType().Assembly.GetType("Warehouse.Services.warehouse_regionsService");
|
||||
var pointSvcType = _service.GetType().Assembly.GetType("Warehouse.Services.warehouse_devicepointService");
|
||||
|
||||
var regionSvc = regionSvcType?.GetProperty("Instance")?.GetValue(null) as IService<warehouse_regions>;
|
||||
var pointSvc = pointSvcType?.GetProperty("Instance")?.GetValue(null) as IService<warehouse_devicepoint>;
|
||||
|
||||
// 查所有区域
|
||||
var regions = regionSvc != null
|
||||
? await regionSvc.FindAsIQueryable(x => true).ToListAsync()
|
||||
: new List<warehouse_regions>();
|
||||
|
||||
// 查所有点位
|
||||
var points = pointSvc != null
|
||||
? await pointSvc.FindAsIQueryable(x => true).ToListAsync()
|
||||
: new List<warehouse_devicepoint>();
|
||||
|
||||
// 统计每个点位下的设备数量
|
||||
var deviceCounts = new Dictionary<int, int>();
|
||||
var allDevices = await _service.FindAsIQueryable(x => true)
|
||||
.Where(x => x.PointId != null)
|
||||
.GroupBy(x => x.PointId!.Value)
|
||||
.Select(g => new { PointId = g.Key, Count = g.Count() })
|
||||
.ToListAsync();
|
||||
foreach (var g in allDevices)
|
||||
deviceCounts[g.PointId] = g.Count;
|
||||
|
||||
// 构建树形结构
|
||||
var tree = new List<object>();
|
||||
foreach (var region in regions)
|
||||
{
|
||||
var regionChildren = points
|
||||
.Where(p => p.RegionId == region.Id)
|
||||
.Select(p => new
|
||||
{
|
||||
id = $"p_{p.PointID}",
|
||||
label = p.PointName ?? $"点位{p.PointID}",
|
||||
type = "point",
|
||||
deviceCount = deviceCounts.TryGetValue(p.PointID, out var c) ? c : 0
|
||||
})
|
||||
.ToList<object>();
|
||||
|
||||
tree.Add(new
|
||||
{
|
||||
id = $"r_{region.Id}",
|
||||
label = region.RegionName ?? $"区域{region.Id}",
|
||||
type = "region",
|
||||
deviceCount = regionChildren.Count,
|
||||
children = regionChildren
|
||||
});
|
||||
}
|
||||
|
||||
return Ok(tree);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定点位下的设备列表(含子设备)。
|
||||
/// 支持分页参数 page 和 size。
|
||||
/// </summary>
|
||||
[HttpGet]
|
||||
[Route("/api/DeviceManager/GetDevicesByPoint")]
|
||||
public async Task<IActionResult> GetDevicesByPoint(int pointId, int page = 1, 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)
|
||||
.OrderBy(x => x.DeviceId)
|
||||
.Select(x => new
|
||||
{
|
||||
x.DeviceId, x.DeviceName, x.AdapterCode, x.SourceId,
|
||||
x.DeviceCategory, x.DeviceGroup, x.IsParent,
|
||||
x.ParentDeviceId, x.IsOnline, x.IpAddress, x.Port,
|
||||
x.Location, x.ExtraData, x.LastSyncTime,
|
||||
x.MapModelId, x.MapModelScale, x.MapModelRotation, x.Enable
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
return Ok(new { items, total });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user