139 lines
5.5 KiB
C#
139 lines
5.5 KiB
C#
/*
|
||
*接口编写处...
|
||
*如果接口需要做Action的权限验证,请在Action上使用属性
|
||
*如: [ApiActionPermission("base_device",Enums.ActionPermissionOptions.Search)]
|
||
*/
|
||
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;
|
||
|
||
[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.RegionsId, x.RegionsName, x.ParentId })
|
||
.ToListAsync();
|
||
var points = await GetPointService().FindAsIQueryable(x => true)
|
||
.Select(x => new { x.DevicePointId, x.DevicePointName, 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.RegionsId;
|
||
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.DevicePointId}",
|
||
Label = (string)p.DevicePointName,
|
||
Type = "point",
|
||
DeviceCount = countMap.GetValueOrDefault((int)p.DevicePointId, 0)
|
||
}).ToList() ?? new List<RegionTreeNode>();
|
||
|
||
children.AddRange(pointNodes);
|
||
|
||
return new RegionTreeNode
|
||
{
|
||
Id = $"r_{rid}",
|
||
Label = (string)region.RegionsName,
|
||
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();
|
||
}
|
||
}
|