113 lines
4.9 KiB
C#
113 lines
4.9 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text.Json;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace YuncangTrace.LocalService.Services;
|
||
|
||
/// <summary>一次查询的结果(HTTP 层和界面都复用)</summary>
|
||
/// <param name="Ok">是否查到</param>
|
||
/// <param name="StatusCode">HTTP 状态码(200 / 400 / 401 / 404 / 500)</param>
|
||
/// <param name="Message">失败原因</param>
|
||
/// <param name="Data">成功时的数据(**平铺**一个对象,前端按「项目/值」两列直接渲染)</param>
|
||
public sealed record QueryResult(bool Ok, int StatusCode, string Message, object? Data)
|
||
{
|
||
public static QueryResult Success(object data) => new(true, 200, "ok", data);
|
||
|
||
public static QueryResult BadRequest(string message) => new(false, 400, message, null);
|
||
|
||
public static QueryResult NotFound(string message) => new(false, 404, message, null);
|
||
|
||
public static QueryResult Failed(string message) => new(false, 500, message, null);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询结果的两副面孔
|
||
///
|
||
/// - <see cref="Http"/>:给网页 `/local` 页用的「项目 / 值」扁平字典;
|
||
/// - <see cref="Query"/>:给桌面端用的结构化对象(校验回放 / 拉流 / 下载都要它)。
|
||
/// </summary>
|
||
public sealed record PackQueryOutcome(QueryResult Http, PackQuery? Query);
|
||
|
||
/// <summary>
|
||
/// 按运单号查「回放计划」
|
||
///
|
||
/// 数据来自后端 `GET /api/web/pack-query?code=`(**范围限定当前主体**):
|
||
/// 后端用**扫码当时的工作台快照**算好「去哪个录像机的哪个通道、拉哪一段时间、画面怎么摆」,
|
||
/// 本机负责展示 + 真正去录像机上校验/拉流。
|
||
/// </summary>
|
||
public sealed class OrderQueryService
|
||
{
|
||
private readonly Func<ApiClient?>? _api;
|
||
private readonly Func<int>? _subjectId;
|
||
private readonly Func<string?>? _token;
|
||
|
||
/// <param name="apiProvider">后端客户端(登录后才有;未登录返回 null,查询会直接回「未登录」)</param>
|
||
/// <param name="subjectIdProvider">当前主体 id(登录后由设置里取)</param>
|
||
/// <param name="tokenProvider">当前用户令牌</param>
|
||
public OrderQueryService(Func<ApiClient?>? apiProvider = null, Func<int>? subjectIdProvider = null,
|
||
Func<string?>? tokenProvider = null)
|
||
{
|
||
_api = apiProvider;
|
||
_subjectId = subjectIdProvider;
|
||
_token = tokenProvider;
|
||
}
|
||
|
||
/// <summary>按运单号查回放计划(需要用户令牌 + 当前主体)</summary>
|
||
public async Task<PackQueryOutcome> QueryAsync(string? code, CancellationToken ct = default)
|
||
{
|
||
var value = (code ?? string.Empty).Trim();
|
||
if (value.Length == 0) return new PackQueryOutcome(QueryResult.BadRequest("缺少运单号"), null);
|
||
|
||
var api = _api?.Invoke();
|
||
var token = _token?.Invoke();
|
||
if (api is null || string.IsNullOrWhiteSpace(token))
|
||
{
|
||
return new PackQueryOutcome(QueryResult.Failed("本机软件未登录,无法查询"), null);
|
||
}
|
||
|
||
var subjectId = _subjectId?.Invoke() ?? 0;
|
||
if (subjectId <= 0) return new PackQueryOutcome(QueryResult.BadRequest("还没有选择主体,无法查询"), null);
|
||
|
||
JsonElement data;
|
||
try
|
||
{
|
||
data = await api.PackQueryAsync(token!, subjectId, value, ct).ConfigureAwait(false);
|
||
}
|
||
catch (ApiException ex)
|
||
{
|
||
// 401/403 就是登录态失效,别当成服务器错误
|
||
var status = ex.StatusCode is 401 or 403 ? 401 : 500;
|
||
return new PackQueryOutcome(new QueryResult(false, status, ex.Message, null), null);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return new PackQueryOutcome(QueryResult.Failed(ex.Message), null);
|
||
}
|
||
|
||
var query = PackQuery.Parse(value, data);
|
||
|
||
// ⚠️ 不要在这里打完整 JSON:一条记录几 KB,扫码密集时日志一天能到几十 MB。
|
||
// 只记结论,排障需要细节看后端日志 / 页面。
|
||
AppLog.Write($"[pack-query] {value} → found={query.Found} reason={query.Reason} "
|
||
+ $"records={query.Records.Count} streams={query.Streams.Count}");
|
||
|
||
if (!query.Found)
|
||
{
|
||
// reason:not-found(本主体没扫过)/ foreign(运单存在但属于别的主体)
|
||
var message = query.Reason == "foreign"
|
||
? "这个运单号存在,但属于其他主体,当前主体查不到"
|
||
: query.Hints.FirstOrDefault() ?? "没有查到该运单的扫码记录";
|
||
return new PackQueryOutcome(QueryResult.NotFound(message), null);
|
||
}
|
||
|
||
// 网页 /local 页吃的是「项目 / 值」(与桌面端共用同一份 ToRows,不会各说一套)
|
||
var rows = new Dictionary<string, object?>();
|
||
foreach (var row in query.ToRows()) rows[row.Key] = row.Value;
|
||
|
||
return new PackQueryOutcome(QueryResult.Success(rows), query);
|
||
}
|
||
}
|