feat: 网关管理模块 + RBAC权限认证 + 组织架构 + 审计日志

- 网关管理:GatewayEntity/Service/Controller CRUD + 测试连接;设备 GatewayId 外键关联
- JWT 认证:登录签发 Token(权限编码写入 Claims)、RequirePermission 权限过滤器、CurrentUser 上下文
- RBAC:用户/角色/权限实体与 CRUD,预定义 4 角色 + 6 权限种子(admin/admin123)
- 组织架构:sys_org 固定层级树(公司/实验室/部门/班组白夜班),层级校验,用户挂 OrgId/岗位/技能标签
- 数据权限:角色 DataScope(全部/本组织及下级),用户列表按组织子树过滤
- 防锁死保护:禁止删/禁自己,保证至少一名活跃管理员,角色摘除 user:manage 前校验
- 审计日志:AuditRecorder 接入设备增删改/指令下发/登录登出/组织变更,AuditController 查询
- 设备指令下发按网关表取连接参数;设备列表支持 gatewayId/productId 筛选

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-10 17:31:39 +08:00
co-authored by Claude Sonnet 4.6
parent 3059befcf5
commit 6aca0297bb
48 changed files with 3583 additions and 52 deletions
+58 -1
View File
@@ -3,11 +3,13 @@ using Model.Dto.Config;
using Model.Entity.Config;
using Model.Mapper;
using ORM;
using Service.Interface;
using Service.Interface.Config;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace Service.Implement.Config
@@ -17,10 +19,17 @@ namespace Service.Implement.Config
/// </summary>
public class DeviceService : IDeviceService
{
private readonly IAuditRecorder _audit;
public DeviceService(IAuditRecorder audit)
{
_audit = audit;
}
/// <summary>
/// 分页查询设备列表(支持关键字搜索编号/名称/类型,可按所属产品筛选)
/// </summary>
public async Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword, long? productId = null)
public async Task<Result<List<IotDeviceDto>>> GetPagedAsync(int pageIndex, int pageSize, RefAsync<int> total, string? keyword, long? productId = null, long? gatewayId = null)
{
try
{
@@ -29,11 +38,13 @@ namespace Service.Implement.Config
.WhereIF(!string.IsNullOrWhiteSpace(keyword),
x => x.Code.Contains(keyword!) || x.Name.Contains(keyword!) || x.DeviceType.Contains(keyword!))
.WhereIF(productId.HasValue && productId.Value > 0, x => x.ProductId == productId!.Value)
.WhereIF(gatewayId.HasValue && gatewayId.Value > 0, x => x.GatewayId == gatewayId!.Value)
.OrderBy(x => x.CreateTime, OrderByType.Desc)
.ToPageListAsync(pageIndex, pageSize, total);
var dtos = list.ToDtoList();
await FillProductNames(dtos);
await FillGatewayNames(dtos);
return Result<List<IotDeviceDto>>.Success(dtos);
}
catch (Exception ex)
@@ -57,6 +68,7 @@ namespace Service.Implement.Config
var dto = entity.ToDto();
await FillProductNames(new List<IotDeviceDto> { dto });
await FillGatewayNames(new List<IotDeviceDto> { dto });
return Result<IotDeviceDto>.Success(dto);
}
catch (Exception ex)
@@ -87,6 +99,10 @@ namespace Service.Implement.Config
// 初始离线
entity.OnlineStatus = IotDeviceOnlineStatusEnum.Offline;
await SqlSugarContext.DbContext.Insertable(entity).ExecuteCommandAsync();
await _audit.RecordAsync("Create", "IotDevice", entity.Id, entity.Id,
null, JsonSerializer.Serialize(new { entity.Code, entity.Name, entity.DeviceType, entity.GatewayId, entity.SlaveId }),
description: $"新增设备 {entity.Code}{entity.Name}");
return Result.Success();
}
catch (Exception ex)
@@ -112,9 +128,18 @@ namespace Service.Implement.Config
if (exists)
return Result.Error($"设备编号【{entity.Code}】已存在");
// 操作前值(审计):先查旧记录
var oldEntity = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
.Where(x => x.Id == entity.Id).FirstAsync();
await SqlSugarContext.DbContext.Updateable(entity)
.IgnoreColumns(x => new { x.CreateTime, x.IsDel, x.OnlineStatus, x.LastCollectTime, x.LastError })
.ExecuteCommandAsync();
await _audit.RecordAsync("Update", "IotDevice", entity.Id, entity.Id,
oldValue: oldEntity == null ? null : JsonSerializer.Serialize(new { oldEntity.Code, oldEntity.Name, oldEntity.DeviceType, oldEntity.GatewayId, oldEntity.SlaveId }),
newValue: JsonSerializer.Serialize(new { entity.Code, entity.Name, entity.DeviceType, entity.GatewayId, entity.SlaveId }),
description: $"修改设备 {entity.Code}{entity.Name}");
return Result.Success();
}
catch (Exception ex)
@@ -133,10 +158,17 @@ namespace Service.Implement.Config
try
{
var oldEntity = await SqlSugarContext.DbContext.Queryable<IotDeviceEntity>()
.Where(x => x.Id == id && x.IsDel == 0).FirstAsync();
await SqlSugarContext.DbContext.Updateable<IotDeviceEntity>()
.SetColumns(x => x.IsDel == 1)
.Where(x => x.Id == id)
.ExecuteCommandAsync();
await _audit.RecordAsync("Delete", "IotDevice", id, id,
oldValue: oldEntity == null ? null : JsonSerializer.Serialize(new { oldEntity.Code, oldEntity.Name }),
description: $"删除设备 {oldEntity?.Code}{oldEntity?.Name}");
return Result.Success();
}
catch (Exception ex)
@@ -165,6 +197,31 @@ namespace Service.Implement.Config
}
}
/// <summary>
/// 给设备 DTO 填充所属网关名称(GatewayName 展示用)
/// </summary>
private async Task FillGatewayNames(List<IotDeviceDto> dtos)
{
if (dtos == null || dtos.Count == 0)
return;
var ids = dtos.Where(x => long.TryParse(x.GatewayId, out var gid) && gid > 0)
.Select(x => long.Parse(x.GatewayId!)).Distinct().ToList();
if (ids.Count == 0)
return;
var gateways = await SqlSugarContext.DbContext.Queryable<GatewayEntity>()
.Where(x => ids.Contains(x.Id) && x.IsDel == 0)
.ToListAsync();
var map = gateways.ToDictionary(g => g.Id, g => $"{g.Code}({g.Name})");
foreach (var d in dtos)
{
if (long.TryParse(d.GatewayId, out var gid) && map.TryGetValue(gid, out var name))
d.GatewayName = name;
}
}
/// <summary>
/// 给设备 DTO 填充所属产品的型号/名称(ProductName 展示用)
/// </summary>