123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- using MeterVision.Config;
- using MeterVision.db;
- using MeterVision.Dlg;
- using MeterVision.Helper;
- using MeterVision.model;
- using MeterVision.Stand;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading;
- 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;
- namespace MeterVision.Station
- {
- /// <summary>
- /// UCStationGrid.xaml 的交互逻辑
- /// </summary>
- public partial class UCStationGrid : UserControl, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
- public BulkObservableCollection<StationItem> StationItemList { get; set; }
- //修改站点参数配置命令
- //public event Action<StationItem> Station_Config;
- //修改站点事件
- //public static Action<StationItem> OnUpdateItem;
- public event Action<StationItem> OnStationItemChange;
- private StationItem _selectedStationItem;
- public StationItem SelectedStationItem
- {
- get => _selectedStationItem;
- set
- {
- if(_selectedStationItem != value)
- {
- _selectedStationItem = value;
- OnPropertyChanged(nameof(SelectedStationItem));
- OnStationItemChange?.Invoke(value);
- }
- }
- }
- //要查找的站点ID
- private string _findStationId;
- private string _standId;
- private MarkFindType _markFindType;
- public async void ChangeFindAsync(string stationId, MarkFindType markFindType, string standId)
- {
- await ChangeFind(stationId, markFindType, standId);
- }
- public async Task ChangeFind(string stationId, MarkFindType markFindType,string standId)
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- _findStationId = stationId;
- _standId = standId;
- _markFindType = markFindType;
- StationPage.InitDefaulValue();
- await LoadStationItemList(true);
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
-
- public CfginiItem mConfigItem { get; set; }
- public PageModel StationPage { get; set; }
- private int _totalRecords;
- public int TotalRecords
- {
- get => _totalRecords;
- set
- {
- if(_totalRecords != value)
- {
- _totalRecords = value;
- OnPropertyChanged(nameof(TotalRecords));
- }
- }
- }
- public UCStationGrid()
- {
- InitializeComponent();
- StationItemList = new BulkObservableCollection<StationItem>();
- dgStation.ItemsSource = StationItemList;
- mConfigItem = CfginiItem.GetConfigItem();
- StationPage = new PageModel
- {
- PageSize = mConfigItem.PageSize2, //mConfigItem.PatchPageSize,
- PageNumber = 1
- };
- //mConfigItem.OnPatchPageSizeChanged += MConfigItem_OnPatchPageSizeChanged;
- mConfigItem.OnPageSize2Changed += MConfigItem_OnPageSize2Changed;
- this.DataContext = this;
- }
- private async void MConfigItem_OnPageSize2Changed(int pageSize2)
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- StationPage.InitDefaulValue(pageSize2);
- await LoadStationItemList(true);
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
- public void ChangeMarkCount()
- {
- if(SelectedStationItem == null)
- {
- return;
- }
- int iMarkCount = DBStation.GetMarkCount(SelectedStationItem.Id);
- SelectedStationItem.MarkCount = iMarkCount;
- }
- private async Task LoadStationItemList(bool scrollTop)
- {
- StationItemList.Clear();
-
- if(_findStationId == null)
- {
- return;
- }
- try
- {
- var result = await Task.Run(() =>
- {
- //return DBStation.GetPagedTStations(StationPage.PageNumber, StationPage.PageSize,
- return DBStation.GetPagedVStations(StationPage.PageNumber, StationPage.PageSize,
- _findStationId, _markFindType,_standId);
- });
- //更新分页信息
- TotalRecords = result.Item1;
- StationPage.PageCount = result.Item2;
- List<TStation> stationList = result.Item3;
- //在主线中更新数据
- //Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- //{
- //int index = 0;
- //foreach(TStation station in stationList)
- //{
- // index++;
- // StationItem item = new StationItem(station);
- // item.Index = index + ((StationPage.PageNumber - 1) * StationPage.PageSize);
- // StationItemList.Add(item);
- //}
- var stationItems = stationList.Select((station, i) => new StationItem(station)
- {
- Index = (StationPage.PageNumber - 1) * StationPage.PageSize + i + 1
- }).ToList();
- StationItemList.AddRange(stationItems);
- //StationItemList.Re
- //SelectedStationItem = null;
- if(StationItemList.Count > 0 && scrollTop)
- {
- //await Task.Delay(100);
- await Dispatcher.BeginInvoke(new Action(() =>
- {
- var scrollViewer = GetScrollViewer(dgStation);
- scrollViewer?.ScrollToTop();
- //dgStation.ScrollIntoView(dgStation.Items[0]);
- }));
- }
- //}));
- }
- catch(Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow,
- $"b加载数据是发生错误:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- //if(StationItemList.Count > 0)
- //{
- // SelectedStationItem = StationItemList[0];
- //}
- //else
- //{
- // SelectedStationItem = null;
- //}
- }
- private ScrollViewer _scrollViewer;
- private ScrollViewer GetScrollViewer(DependencyObject obj)
- {
- if (_scrollViewer != null)
- {
- return _scrollViewer;
- }
- if (obj is ScrollViewer)
- {
- _scrollViewer = (ScrollViewer)obj;
- return obj as ScrollViewer;
- }
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
- {
- var child = VisualTreeHelper.GetChild(obj, i);
- var scrollViewer = GetScrollViewer(child);
- if (scrollViewer != null)
- {
- _scrollViewer = scrollViewer;
- return scrollViewer;
- }
- }
- return null;
- }
- private void MiUpdate_Click(object sender, RoutedEventArgs e)
- {
- if (SelectedStationItem == null) return;
- EditStationDlg2 dialog = new EditStationDlg2(SelectedStationItem)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- if (dialog.ShowDialog() == true)
- {
- SelectedStationItem = dialog.mStationItem;
- }
- //Station_Config?.Invoke(SelectedStationItem);
- }
- private void MiDelete_Click(object sender, RoutedEventArgs e)
- {
- if (SelectedStationItem == null) return;
- //获取当前行绑定的数据项
- DeleteSelectedItem(SelectedStationItem);
- }
- private static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
- {
- var parent = VisualTreeHelper.GetParent(dependencyObject);
- if (parent == null) return null;
- var parentT = parent as T;
- return parentT ?? FindAncestor<T>(parent);
- }
- private void BtnUpdateStation_Click(object sender, RoutedEventArgs e)
- {
- Button button = sender as Button;
- if (button == null) return;
- DataGridRow row = FindAncestor<DataGridRow>(button);
- if (row == null) return;
- StationItem stationItem = row.Item as StationItem;
- EditStationDlg2 dialog = new EditStationDlg2(stationItem)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- if (dialog.ShowDialog() == true)
- {
- stationItem = dialog.mStationItem;
- }
- //Station_Config?.Invoke(SelectedStationItem);
- }
- private void BtnDeleteStation_Click(object sender, RoutedEventArgs e)
- {
- Button button = sender as Button;
- if (button == null) return;
- DataGridRow row = FindAncestor<DataGridRow>(button);
- if (row == null) return;
- StationItem stationItem = row.Item as StationItem;
- DeleteSelectedItem(stationItem);
- }
- private async void DeleteSelectedItem(StationItem selectedItem)
- {
- if (selectedItem == null) return;
- MessageBoxResult result = MessageBox.Show("确定要删除此条目吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
- if (result != MessageBoxResult.Yes) return;
- string titleInfo = "正在删除,请稍后...";
- WaitWindow waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- bool deleteSuccess = false;
- await Task.Run(() =>
- {
- deleteSuccess = DBStation.DeleteStationAndStandDetails(selectedItem.StandId, selectedItem.StationId);
- //DBStation.DeleteTStation(selectedItem.StationId);
- });
- if (deleteSuccess)
- {
- //await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- //{
- // 从数据源中移除该条目
- StationItemList.Remove(selectedItem);
- SelectedStationItem = null;
- //}));
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"删除失败:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- waitWindow.Close();
- }
- }
- public async void ClearAllStation()
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- //清空提示
- MessageBoxResult result = MessageBox.Show("确定要清空所有数据吗?", "确认清空", MessageBoxButton.YesNo, MessageBoxImage.Question);
- if (result != MessageBoxResult.Yes) return;
- string titleInfo = "正在清空数据,请稍后...";
- WaitWindow waitWindow = new WaitWindow(titleInfo)
- {
- Owner = Application.Current.MainWindow,
- WindowStartupLocation = WindowStartupLocation.CenterOwner
- };
- waitWindow.Show();
- try
- {
- bool blClearAll = false;
- await Task.Run(() =>
- {
- blClearAll = DBStation.ClearTStationTable();
- });
- if (blClearAll)
- {
- StationPage.InitDefaulValue();
- await LoadStationItemList(true);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(Application.Current.MainWindow, $"清空失败:{ex.Message}", "错误",
- MessageBoxButton.OK, MessageBoxImage.Error);
- }
- finally
- {
- waitWindow.Close();
- }
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
- public async Task NextPage()
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- if (StationPage.PageNumber < StationPage.PageCount)
- {
- StationPage.PageNumber += 1;
- await LoadStationItemList(true);
- }
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
- public async Task PrePage()
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- if (StationPage.PageNumber > 1)
- {
- StationPage.PageNumber -= 1;
- await LoadStationItemList(true);
- }
- }
- catch{}
- finally
- {
- _semaphore.Release();
- }
- }
- public async Task FirstPage()
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- if (StationPage.PageNumber > 1)
- {
- StationPage.PageNumber = 1;
- await LoadStationItemList(true);
- }
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
- public async Task LastPage()
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- if (StationPage.PageNumber < StationPage.PageCount)
- {
- StationPage.PageNumber = StationPage.PageCount;
- await LoadStationItemList(true);
- }
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
- public async Task SpeciPage(int pageNumber)
- {
- if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
- try
- {
- if (pageNumber != StationPage.PageNumber &&
- pageNumber > 0 && pageNumber <= StationPage.PageCount)
- {
- StationPage.PageNumber = pageNumber;
- await LoadStationItemList(true);
- }
- }
- catch { }
- finally
- {
- _semaphore.Release();
- }
- }
- //---------------------------------------------------------------------
- }
- }
|