using FLY.OBJComponents.Client; using FLY.OBJComponents.Common; using FLY.OBJComponents.IService; using FLY.OBJComponents.Server.Model; using GalaSoft.MvvmLight.Command; using Misc; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using Unity; namespace FLY.Thick.Base.UI { /// /// Page_Flow.xaml 的交互逻辑 /// public partial class PgErrorAllTable : Page { PgErrorAllTableVm viewModel; /// /// /// public PgErrorAllTable() { InitializeComponent(); } [InjectionMethod] public void Init( IWarningSystem2Service warning, ParamDictionary paramDictionary) { //界面........................................ viewModel = new PgErrorAllTableVm(); viewModel.Init(warning, paramDictionary); this.DataContext = viewModel; } } public class PgErrorAllTableVm : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; /// /// 当前页面最后一行Id /// public long Id { get; protected set; } = -1; /// /// 最新的Id /// public long LastId { get; protected set; } = -1; private int windowSize = 30; /// /// 一页显示的行数 /// public int WindowSize { get { return windowSize; } set { if (value < 10) value = 30; if (windowSize != value) { windowSize = value; } } } /// /// 当前就是最新的视图 /// public bool IsNewest => Id == LastId; /// /// 数据加载中 /// public bool IsLoading { get; protected set; } /// /// 通过时间查找数据 /// public bool IsSearchByTime { get; set; } /// /// 查找时间 /// public DateTime SearchTime { get; set; } /// /// 查找Id /// public long SearchId { get; set; } public ObservableCollection Values { get; } = new ObservableCollection(); #region Cmd public RelayCommand PreViewCmd { get; } public RelayCommand NextViewCmd { get; } public RelayCommand ToNewestCmd { get; } public RelayCommand SearchCmd { get; } #endregion IWarningSystem2Service warning; ParamDictionary paramDictionary; public PgErrorAllTableVm() { PreViewCmd = new RelayCommand(PreView); NextViewCmd = new RelayCommand(NextView); ToNewestCmd = new RelayCommand(ToNewest); SearchCmd = new RelayCommand(Search); } public void Init( IWarningSystem2Service warning, ParamDictionary paramDictionary ) { this.warning = warning; this.paramDictionary = paramDictionary; //窗口显示数据条数 this.paramDictionary.SetBinding(this, nameof(WindowSize), 30); this.warning.PropertyChanged += BulkDB_PropertyChanged; this.PropertyChanged += CtrlTableViewModel_PropertyChanged; Misc.BindingOperations.SetBinding(this.warning, nameof(this.warning.LastId), this, nameof(LastId)); ToNewest(); } private void BulkDB_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == nameof(FObjBase.FObjServiceClient.IsConnected)) { if ((this.warning as FObjBase.FObjServiceClient).IsConnected) { ToNewest(); } else { IsLoading = false; } } } private void CtrlTableViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) { } bool IsConnected { get { return (this.warning as FObjBase.FObjServiceClient).IsConnected; } } void GetTrendReponse(object asyncContext, object retData) { IsLoading = false; var reponse = retData as Pack_GetTrendReponse; //向Values 后面添加数据 if (reponse.Values != null && reponse.Values.Count > 0) { //从尾向前排的!!!! //现在把数据反转,Id从小排到大 reponse.Values.Reverse(); Values.Clear(); for (int i = 0; i < reponse.Values.Count(); i++) { Values.Add(reponse.Values[i]); } //限制Values长度 int remove_cnt = Values.Count() - WindowSize; for (int i = 0; i < remove_cnt; i++) Values.RemoveAt(0); Id = reponse.Values.Last().ID; SearchId = reponse.Values.Last().ID; SearchTime = reponse.Values.Last().Time; } else { Values.Clear(); //没有任何数据 Id = -1; } } void Search() { if (IsSearchByTime) { Search(SearchTime); } else { Search(SearchId); } } /// /// WHERE ID 小于 lastId AND ID % graphparam.Interval == 0 ORDER BY ID DESC LIMIT graphparam.Len /// /// void Search(long id) { if (id <= 0) return; if (!IsConnected) return; if (IsLoading) return; IsLoading = true; //TODO,计算,是否有重叠的数据 warning.GetTrend( new Pack_GetTrendRequest() { Id = id, Interval = 1, Count = WindowSize }, GetTrendReponse, this); } /// /// WHERE Time 小于 lastTime AND ID % graphparam.Interval == 0 ORDER BY ID DESC LIMIT graphparam.Len /// /// void Search(DateTime time) { if (!IsConnected) return; if (IsLoading) return; IsLoading = true; //TODO,计算,是否有重叠的数据 warning.GetTrend( new Pack_GetTrendRequest() { Interval = 1, Count = WindowSize, IsSearchByTime = true, Time = time }, GetTrendReponse, this); } /// /// 下一页 /// void PreView() { Search(Id - WindowSize * 1); } /// /// 上一页 /// void NextView() { Search(Id + WindowSize * 1); } /// /// 显示最新数据 /// void ToNewest() { if (!IsConnected) return; if (IsLoading) return; IsLoading = true; warning.GetTrend( new Pack_GetTrendRequest() { Id = 0, Interval = 1, Count = WindowSize }, GetTrendReponse, this); } } public class PgErrorAllTableVmUt : PgErrorAllTableVm { public PgErrorAllTableVmUt() { DateTime time = DateTime.Now; Random random = new Random(); int len = 120; List bulkdb = new List(); for (int i = 0; i < len; i++) { var error = new FlyData_WarningHistory(); error.ID = i; error.Time = time + TimeSpan.FromSeconds(i * 7); error.ErrCode = i % 10; error.Description = $"hhah {i}"; error.State = i % 3 == 0 ? ERR_STATE.ON : ERR_STATE.OFF; bulkdb.Add(error); } WindowSize = 30; SearchId = 70; LastId = bulkdb.Last().ID; IsLoading = true; int startIndex = (int)(SearchId - WindowSize); int size = WindowSize; var values = bulkdb.Skip(startIndex).Take(size); foreach (var val in values) Values.Add(val); Id = Values.Last().ID; SearchId = Values.Last().ID; SearchTime = Values.Last().Time; } } }