优化桌面端无用菜单和地磅数据可手动功能。

This commit is contained in:
geht
2026-05-18 15:55:11 +08:00
parent 5800b6b61c
commit c11f3104cb
18 changed files with 628 additions and 118 deletions

View File

@@ -10,8 +10,7 @@ using YY.Admin.Core.Services;
namespace YY.Admin.Services.Service.Warehouse;
/// <summary>
/// 仓库数据只读服务:启动时从后端拉取全量列表并缓存到磁盘,断网时回退本地缓存
/// 仅供其他模块的下拉筛选使用,不提供 CRUD。
/// 仓库数据只读服务:启动与重连时后台刷新远端写入缓存GetAllAsync 优先读缓存,避免库区等业务每次打开都全量拉仓库
/// </summary>
public class WarehouseService : IWarehouseService, ISingletonDependency
{
@@ -20,6 +19,7 @@ public class WarehouseService : IWarehouseService, ISingletonDependency
private readonly INetworkMonitor _networkMonitor;
private readonly ILoggerService _logger;
private readonly object _cacheLock = new();
private readonly SemaphoreSlim _emptyLoadGate = new(1, 1);
private readonly string _cacheFilePath;
private List<MesXslWarehouse> _localCache = new();
@@ -64,19 +64,44 @@ public class WarehouseService : IWarehouseService, ISingletonDependency
public async Task<List<MesXslWarehouse>> GetAllAsync(CancellationToken ct = default)
{
if (_networkMonitor.IsOnline)
lock (_cacheLock)
{
if (_localCache.Count > 0 || !_networkMonitor.IsOnline)
return _localCache.ToList();
}
// 缓存为空且在线:仅此时阻塞拉远端(首次安装或清缓存后)
await _emptyLoadGate.WaitAsync(ct).ConfigureAwait(false);
try
{
lock (_cacheLock)
{
if (_localCache.Count > 0)
return _localCache.ToList();
}
if (!_networkMonitor.IsOnline)
{
lock (_cacheLock)
return _localCache.ToList();
}
try
{
await RefreshFromRemoteAsync(ct).ConfigureAwait(false);
}
catch { }
catch
{
/* 登录前拉取失败等场景,仍返回当前缓存(可能仍为空) */
}
}
finally
{
_emptyLoadGate.Release();
}
lock (_cacheLock)
{
return _localCache.ToList();
}
}
private async Task RefreshFromRemoteAsync(CancellationToken ct)