using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace AI.Models.SpecialMessages
{
///
/// 表格数据预览行 - 一行单元格
///
public class TableDataRow : INotifyPropertyChanged
{
/// 该行单元格(与列顺序一致)
public ObservableCollection Cells { get; } = new ObservableCollection();
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string propertyName) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
///
/// 表格数据展示特殊消息 - 用于导入数据等在聊天流中做数据预览
///
public class TableDataMessage : ISpecialMessage, INotifyPropertyChanged
{
private string _title = "数据预览";
private int _totalRowCount;
private int _maxPreviewRows = 50;
/// 消息唯一标识符
public string Id { get; set; } = Guid.NewGuid().ToString();
/// 类型名称
public string TypeName => "Table";
/// 不需要实时更新
public bool IsLive => false;
/// 标题(如“数据预览”)
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
/// 列名(表头)
public ObservableCollection ColumnNames { get; } = new ObservableCollection();
/// 预览行数据
public ObservableCollection Rows { get; } = new ObservableCollection();
/// 数据总行数(用于显示“共 N 行,仅显示前 M 行”)
public int TotalRowCount
{
get => _totalRowCount;
set => SetProperty(ref _totalRowCount, value);
}
/// 最多预览行数
public int MaxPreviewRows
{
get => _maxPreviewRows;
set => SetProperty(ref _maxPreviewRows, value);
}
/// 是否有多余行未显示
public bool HasMoreRows => TotalRowCount > Rows.Count;
/// 摘要文案(如“共 100 行,仅显示前 50 行”)
public string SummaryText => $"共 {TotalRowCount} 行,仅显示前 {Rows.Count} 行";
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetProperty(ref T field, T value, [CallerMemberName] string? propertyName = null)
{
if (EqualityComparer.Default.Equals(field, value))
return false;
field = value;
OnPropertyChanged(propertyName);
if (propertyName == nameof(TotalRowCount) || propertyName == nameof(MaxPreviewRows))
{
OnPropertyChanged(nameof(HasMoreRows));
OnPropertyChanged(nameof(SummaryText));
}
return true;
}
/// 从列名与行数据批量填充(每行为字符串数组,与列顺序一致)
public void SetData(IEnumerable columnNames, IEnumerable> rows, int? totalRowCount = null)
{
ColumnNames.Clear();
foreach (var c in columnNames)
ColumnNames.Add(c ?? string.Empty);
Rows.Clear();
foreach (var row in rows)
{
var r = new TableDataRow();
foreach (var cell in row)
r.Cells.Add(cell?.ToString() ?? string.Empty);
Rows.Add(r);
}
TotalRowCount = totalRowCount ?? Rows.Count;
OnPropertyChanged(nameof(HasMoreRows));
OnPropertyChanged(nameof(SummaryText));
}
}
}