有积压时上报循环每秒检查一次(空闲仍 30 秒保活),保证「最后一次扫描 + 5 秒」就能上报;时间被刷新则重新计时并再次上报。 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
283 lines
10 KiB
C#
283 lines
10 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Input;
|
||
using System.Windows.Threading;
|
||
using YuncangTrace.LocalService.Services;
|
||
using YuncangTrace.LocalService.Services.Scan;
|
||
|
||
namespace YuncangTrace.LocalService.Views;
|
||
|
||
/// <summary>
|
||
/// 「扫码记录」页签(对应 AndroidScanCenter 的「通道回放」):
|
||
/// 看本机落了哪些条码、**服务器确认了没有**。
|
||
///
|
||
/// 记录先写本地库(`scan-records.json`),上报成功(服务器 2xx)才标成「已上传」,
|
||
/// 所以这里也是断网期间「有没有丢码」的唯一凭证。
|
||
/// </summary>
|
||
public partial class ScanRecordsView : UserControl
|
||
{
|
||
private readonly ScanService _scan;
|
||
private readonly ObservableCollection<ScanRecordItem> _rows = new();
|
||
|
||
/// <summary>上传状态/条数会自己变(后台重试),定时刷一下界面</summary>
|
||
private readonly DispatcherTimer _timer = new() { Interval = TimeSpan.FromSeconds(1) };
|
||
|
||
private bool _suppressFilterChange;
|
||
private bool _suppressPageSizeChange;
|
||
|
||
/// <summary>当前页码(0 起)</summary>
|
||
private int _page;
|
||
|
||
/// <summary>每页条数(默认 200:本地每通道最多 5000 条,全渲染会卡)</summary>
|
||
private int _pageSize = 200;
|
||
|
||
/// <summary>当前页签名(用来判断「要不要重建列表」,避免每秒刷新把滚动/选中冲掉)</summary>
|
||
private string _pageSignature = string.Empty;
|
||
|
||
/// <summary>下拉框选项:全部 + 每个通道</summary>
|
||
public sealed class ChannelOption
|
||
{
|
||
public string? ChannelId { get; init; }
|
||
public string Label { get; init; } = string.Empty;
|
||
}
|
||
|
||
public ScanRecordsView(ScanService scan)
|
||
{
|
||
InitializeComponent();
|
||
|
||
_scan = scan;
|
||
RecordList.ItemsSource = _rows;
|
||
|
||
// 默认 200 条/页;直接设 SelectedIndex 会触发 SelectionChanged,
|
||
// 那时 _scan 刚赋好、但其它控件不一定就绪 → 先抑制一下
|
||
_suppressPageSizeChange = true;
|
||
PageSizeBox.SelectedIndex = 1;
|
||
_suppressPageSizeChange = false;
|
||
|
||
// 与扫码通道页一致:**不随页签切换停用功能**,事件与定时刷新一直挂着
|
||
_scan.Changed += OnStoreChanged;
|
||
ScanRecordStore.Changed += OnStoreChanged;
|
||
_timer.Start();
|
||
|
||
Loaded += (_, _) =>
|
||
{
|
||
RefreshFilters();
|
||
Refresh();
|
||
};
|
||
|
||
_timer.Tick += (_, _) => Refresh();
|
||
}
|
||
|
||
// 事件只用来同步通道下拉(通道增删),列表本身交给定时器 ——
|
||
// 连续扫码时 Changed 会一条一触发,每次都重建 500 行没必要
|
||
private void OnStoreChanged() => Dispatcher.Invoke(RefreshFilters);
|
||
|
||
/// <summary>通道下拉:新增/删除通道后跟着变(保持当前选中)</summary>
|
||
private void RefreshFilters()
|
||
{
|
||
var current = (ChannelFilter.SelectedItem as ChannelOption)?.ChannelId;
|
||
|
||
var options = new List<ChannelOption> { new() { ChannelId = null, Label = "全部通道" } };
|
||
options.AddRange(_scan.Channels
|
||
.OrderBy(item => item.Number)
|
||
.Select(item => new ChannelOption
|
||
{
|
||
ChannelId = item.ChannelId,
|
||
Label = $"{item.Number} · {item.DisplayName}",
|
||
}));
|
||
|
||
_suppressFilterChange = true;
|
||
ChannelFilter.ItemsSource = options;
|
||
ChannelFilter.SelectedItem = options.FirstOrDefault(item => item.ChannelId == current) ?? options[0];
|
||
_suppressFilterChange = false;
|
||
}
|
||
|
||
private void ChannelFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
if (_suppressFilterChange) return;
|
||
_page = 0; // 换通道回到第一页
|
||
_pageSignature = string.Empty;
|
||
Refresh();
|
||
}
|
||
|
||
/// <summary>重画列表 + 状态行(按页取,不再一次塞 500 条)</summary>
|
||
private void Refresh()
|
||
{
|
||
var selected = (ChannelFilter.SelectedItem as ChannelOption)?.ChannelId;
|
||
|
||
var (items, total) = ScanRecordStore.Page(selected, _page, _pageSize);
|
||
var pages = Math.Max((total + _pageSize - 1) / _pageSize, 1);
|
||
|
||
// 页码越界(清空/删除后)→ 回到最后一页
|
||
if (_page >= pages)
|
||
{
|
||
_page = pages - 1;
|
||
(items, total) = ScanRecordStore.Page(selected, _page, _pageSize);
|
||
}
|
||
|
||
// 只有「这一页的内容真的变了」才重建,避免 1 秒定时器把滚动位置和选中项冲掉
|
||
var signature = $"{selected}|{_page}|{_pageSize}|{total}|{items.Count}|"
|
||
+ (items.Count == 0 ? "0" : $"{items[0].Id}-{items[^1].Id}-{items.Count(item => item.Uploaded)}");
|
||
|
||
if (signature != _pageSignature)
|
||
{
|
||
_pageSignature = signature;
|
||
|
||
var selectedId = (RecordList.SelectedItem as ScanRecordItem)?.Id;
|
||
|
||
_rows.Clear();
|
||
foreach (var item in items) _rows.Add(item);
|
||
|
||
// 尽量保持选中项(同一页时)
|
||
if (selectedId is not null)
|
||
{
|
||
var again = _rows.FirstOrDefault(item => item.Id == selectedId);
|
||
if (again is not null) RecordList.SelectedItem = again;
|
||
}
|
||
}
|
||
|
||
PageText.Text = $"第 {_page + 1} / {pages} 页 · 共 {total} 条";
|
||
|
||
FirstButton.IsEnabled = _page > 0;
|
||
PrevButton.IsEnabled = _page > 0;
|
||
NextButton.IsEnabled = _page < pages - 1;
|
||
LastButton.IsEnabled = _page < pages - 1;
|
||
|
||
var scope = selected is null
|
||
? "全部通道"
|
||
: _scan.Channels.FirstOrDefault(channel => channel.ChannelId == selected)?.DisplayName ?? selected;
|
||
|
||
var (allTotal, pending) = ScanRecordStore.Stats();
|
||
var ready = ScanRecordStore.ReadyCount();
|
||
var status = $"{scope}:共 {total} 条 · 全部 {allTotal} 条 · 待上传 {pending} 条";
|
||
if (pending > ready) status += $"({pending - ready} 条静默中,满 5 秒自动上报)";
|
||
|
||
if (IsAgentMode)
|
||
{
|
||
var agent = AgentFiles.LoadStatus();
|
||
if (!string.IsNullOrWhiteSpace(agent?.UploadStatus)) status += $" · {agent.UploadStatus}";
|
||
if (agent?.Running == false) status += " · 后台服务未运行";
|
||
}
|
||
else if (!string.IsNullOrWhiteSpace(_scan.UploadStatus))
|
||
{
|
||
status += $" · {_scan.UploadStatus}";
|
||
}
|
||
|
||
StatusText.Text = status;
|
||
UploadButton.IsEnabled = pending > 0;
|
||
}
|
||
|
||
// ---------------- 分页 ----------------
|
||
|
||
private void First_Click(object sender, RoutedEventArgs e) => GoToPage(0);
|
||
|
||
private void Prev_Click(object sender, RoutedEventArgs e) => GoToPage(_page - 1);
|
||
|
||
private void Next_Click(object sender, RoutedEventArgs e) => GoToPage(_page + 1);
|
||
|
||
private void Last_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var selected = (ChannelFilter.SelectedItem as ChannelOption)?.ChannelId;
|
||
var (_, total) = ScanRecordStore.Page(selected, 0, _pageSize);
|
||
var pages = Math.Max((total + _pageSize - 1) / _pageSize, 1);
|
||
GoToPage(pages - 1);
|
||
}
|
||
|
||
private void Jump_Click(object sender, RoutedEventArgs e) => JumpToInput();
|
||
|
||
private void JumpBox_KeyDown(object sender, KeyEventArgs e)
|
||
{
|
||
if (e.Key == Key.Enter) JumpToInput();
|
||
}
|
||
|
||
/// <summary>跳页:没在范围里的值由 Refresh 里的越界回退处理</summary>
|
||
private void JumpToInput()
|
||
{
|
||
var text = (JumpBox.Text ?? string.Empty).Trim();
|
||
JumpBox.Text = string.Empty;
|
||
|
||
if (!int.TryParse(text, out var number) || number <= 0) return;
|
||
GoToPage(number - 1);
|
||
}
|
||
|
||
private void GoToPage(int page)
|
||
{
|
||
if (page < 0) return;
|
||
_page = page;
|
||
_pageSignature = string.Empty; // 强制重建
|
||
Refresh();
|
||
}
|
||
|
||
private void PageSizeBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||
{
|
||
if (_suppressPageSizeChange) return;
|
||
if (PageSizeBox.SelectedItem is not ComboBoxItem item) return;
|
||
if (!int.TryParse(item.Content?.ToString(), out var size) || size <= 0) return;
|
||
|
||
_pageSize = size;
|
||
_page = 0;
|
||
_pageSignature = string.Empty;
|
||
Refresh();
|
||
}
|
||
|
||
/// <summary>当前是否「后台服务收码」模式(本进程只展示,写入靠服务)</summary>
|
||
private static bool IsAgentMode => AgentPaths.MachineMode && !AgentPaths.IsServiceProcess;
|
||
|
||
private void Upload_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
if (ScanRecordStore.PendingCount() == 0)
|
||
{
|
||
StatusText.Text = "没有待上传的记录";
|
||
return;
|
||
}
|
||
|
||
if (IsAgentMode)
|
||
{
|
||
AgentFiles.QueueCommand(command => command.TriggerUploadAt = DateTime.UtcNow);
|
||
StatusText.Text = "已通知后台服务上传,稍等…";
|
||
return;
|
||
}
|
||
|
||
if (ScanRecordStore.ReadyCount() == 0)
|
||
{
|
||
StatusText.Text = "记录还在 5 秒静默期(等最后一次扫描后再上报),稍后会自动上传";
|
||
return;
|
||
}
|
||
|
||
_scan.TriggerUpload();
|
||
StatusText.Text = "已请求上传,稍等…";
|
||
}
|
||
|
||
private void Clear_Click(object sender, RoutedEventArgs e)
|
||
{
|
||
var selected = (ChannelFilter.SelectedItem as ChannelOption)?.ChannelId;
|
||
var scope = selected is null ? "全部通道" : $"通道 {selected}";
|
||
var pending = ScanRecordStore.PendingCount();
|
||
|
||
var hint = pending > 0 ? $"\n\n注意:还有 {pending} 条没上传,清掉就找不回来了。" : string.Empty;
|
||
if (MessageBox.Show(Window.GetWindow(this)!, $"确定清空{scope}的扫码记录?{hint}", "清空扫码记录",
|
||
MessageBoxButton.YesNo, MessageBoxImage.Warning) != MessageBoxResult.Yes) return;
|
||
|
||
if (IsAgentMode)
|
||
{
|
||
AgentFiles.QueueCommand(command =>
|
||
{
|
||
command.ClearRecordsAt = DateTime.UtcNow;
|
||
command.ClearChannelId = selected;
|
||
});
|
||
StatusText.Text = "已通知后台服务清空,稍等…";
|
||
return;
|
||
}
|
||
|
||
var removed = ScanRecordStore.Clear(selected);
|
||
StatusText.Text = $"已清空 {removed} 条";
|
||
_page = 0;
|
||
_pageSignature = string.Empty;
|
||
Refresh();
|
||
}
|
||
}
|