using System; using System.Collections.Generic; using System.Threading.Channels; using System.Threading.Tasks; using ClassNotify.Shared.Models; namespace ClassNotify.Server.Services; /// /// 诊断包创建结果包装 /// public class SupportBundleCreationResult { public bool IsSuccess { get; set; } public int StatusCode { get; set; } = 202; public SupportBundleCreatedDto? Dto { get; set; } public string? ErrorCode { get; set; } public string? ErrorMessage { get; set; } public string? ExistingBundleId { get; set; } public static SupportBundleCreationResult Accepted(SupportBundleCreatedDto dto) => new() { IsSuccess = true, StatusCode = 202, Dto = dto }; public static SupportBundleCreationResult Conflict(string errorCode, string existingBundleId, string message) => new() { IsSuccess = false, StatusCode = 409, ErrorCode = errorCode, ExistingBundleId = existingBundleId, ErrorMessage = message }; public static SupportBundleCreationResult Forbidden(string message) => new() { IsSuccess = false, StatusCode = 403, ErrorCode = "Forbidden", ErrorMessage = message }; public static SupportBundleCreationResult NotFound(string message) => new() { IsSuccess = false, StatusCode = 404, ErrorCode = "DeviceNotFound", ErrorMessage = message }; public static SupportBundleCreationResult BadRequest(string message) => new() { IsSuccess = false, StatusCode = 400, ErrorCode = "BadRequest", ErrorMessage = message }; } /// /// 诊断包通用查询包装 /// public class SupportBundleQueryResult { public bool IsSuccess { get; set; } public int StatusCode { get; set; } = 200; public T? Data { get; set; } public string? ErrorCode { get; set; } public string? ErrorMessage { get; set; } public static SupportBundleQueryResult Ok(T data) => new() { IsSuccess = true, StatusCode = 200, Data = data }; public static SupportBundleQueryResult Forbidden(string message) => new() { IsSuccess = false, StatusCode = 403, ErrorCode = "Forbidden", ErrorMessage = message }; public static SupportBundleQueryResult NotFound(string message) => new() { IsSuccess = false, StatusCode = 404, ErrorCode = SupportBundleErrorCodes.SupportBundleNotFound, ErrorMessage = message }; } /// /// 诊断包下载准备结果 /// public class SupportBundleDownloadResult { public bool IsSuccess { get; set; } public int StatusCode { get; set; } = 200; public string? PhysicalPath { get; set; } public string? DownloadFileName { get; set; } public string? ErrorCode { get; set; } public string? ErrorMessage { get; set; } public static SupportBundleDownloadResult Success(string physicalPath, string downloadFileName) => new() { IsSuccess = true, StatusCode = 200, PhysicalPath = physicalPath, DownloadFileName = downloadFileName }; public static SupportBundleDownloadResult Forbidden(string message) => new() { IsSuccess = false, StatusCode = 403, ErrorCode = "Forbidden", ErrorMessage = message }; public static SupportBundleDownloadResult NotFound(string message) => new() { IsSuccess = false, StatusCode = 404, ErrorCode = SupportBundleErrorCodes.SupportBundleNotFound, ErrorMessage = message }; public static SupportBundleDownloadResult Conflict(string errorCode, string message) => new() { IsSuccess = false, StatusCode = 409, ErrorCode = errorCode, ErrorMessage = message }; public static SupportBundleDownloadResult Gone(string errorCode, string message) => new() { IsSuccess = false, StatusCode = 410, ErrorCode = errorCode, ErrorMessage = message }; public static SupportBundleDownloadResult BadRequest(string errorCode, string message) => new() { IsSuccess = false, StatusCode = 400, ErrorCode = errorCode, ErrorMessage = message }; public static SupportBundleDownloadResult ServerError(string errorCode, string message) => new() { IsSuccess = false, StatusCode = 500, ErrorCode = errorCode, ErrorMessage = message }; } /// /// 诊断包内部全量字段详情查询 DTO (保留兼容) /// public class SupportBundleDetailDto { public string SupportBundleId { get; set; } = string.Empty; public string DeviceId { get; set; } = string.Empty; public string TenantId { get; set; } = string.Empty; public string CorrelationId { get; set; } = string.Empty; public string RequestedByUserId { get; set; } = string.Empty; public string Status { get; set; } = string.Empty; public DateTimeOffset CreatedAtUtc { get; set; } public DateTimeOffset UpdatedAtUtc { get; set; } public DateTimeOffset DeadlineAtUtc { get; set; } public DateTimeOffset? DispatchedAtUtc { get; set; } public DateTimeOffset? CompletedAtUtc { get; set; } public DateTimeOffset? ExpiresAtUtc { get; set; } public int DispatchAttemptCount { get; set; } public string? StoragePath { get; set; } public long? SizeBytes { get; set; } public string? Sha256 { get; set; } public string? ErrorCode { get; set; } } /// /// 设备技术诊断包服务接口 (P2.5 RC1-3 Support Bundle Phase 1, 2, 3) /// public interface ISupportBundleService { /// /// 请求生成技术诊断包 (严格 RBAC、幂等性、单 Active 检查、事务与审计一致性落库) /// Task CreateSupportBundleAsync( string deviceId, CreateSupportBundleRequest request, string actorUserId, string actorRole, string? actorTenantId, string actorIp, bool simulateAuditFailure, CancellationToken ct); /// /// 获取技术诊断包详情 (内部兼容) /// Task GetSupportBundleAsync(string bundleId, string actorRole, string? actorTenantId, CancellationToken ct); /// /// 管理员查询技术诊断包状态 (P3-1: 严格 RBAC,杜绝暴露内部物理 StoragePath) /// Task> GetSupportBundleStatusAsync(string bundleId, string actorRole, string? actorTenantId, CancellationToken ct); /// /// 获取设备最近诊断包列表 (P3-11: 页面刷新恢复,最多 20 条) /// Task>> GetDeviceSupportBundlesAsync(string deviceId, int take, string actorRole, string? actorTenantId, CancellationToken ct); /// /// 准备管理员安全下载 (P3-2 ~ P3-5, P3-8, P3-9) /// Task PrepareDownloadAsync(string bundleId, string actorRole, string? actorTenantId, string actorUser, string actorIp, CancellationToken ct); } /// /// 诊断包调度唤醒通知器 /// public interface ISupportBundleDispatchNotifier { void Notify(); Task WaitAsync(CancellationToken ct); } /// /// 诊断包调度唤醒通知器实现 /// public sealed class SupportBundleDispatchNotifier : ISupportBundleDispatchNotifier { private readonly Channel _channel = Channel.CreateBounded(new BoundedChannelOptions(1) { FullMode = BoundedChannelFullMode.DropOldest }); public void Notify() { _channel.Writer.TryWrite(1); } public async Task WaitAsync(CancellationToken ct) { await _channel.Reader.WaitToReadAsync(ct); while (_channel.Reader.TryRead(out _)) { } } }