UCStationGrid.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. using MeterVision.Config;
  2. using MeterVision.db;
  3. using MeterVision.Dlg;
  4. using MeterVision.Helper;
  5. using MeterVision.model;
  6. using MeterVision.Stand;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Windows;
  16. using System.Windows.Controls;
  17. using System.Windows.Data;
  18. using System.Windows.Documents;
  19. using System.Windows.Input;
  20. using System.Windows.Media;
  21. using System.Windows.Media.Imaging;
  22. using System.Windows.Navigation;
  23. using System.Windows.Shapes;
  24. namespace MeterVision.Station
  25. {
  26. /// <summary>
  27. /// UCStationGrid.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class UCStationGrid : UserControl, INotifyPropertyChanged
  30. {
  31. public event PropertyChangedEventHandler PropertyChanged;
  32. protected void OnPropertyChanged(string propertyName)
  33. {
  34. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  35. }
  36. private SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
  37. public BulkObservableCollection<StationItem> StationItemList { get; set; }
  38. //修改站点参数配置命令
  39. //public event Action<StationItem> Station_Config;
  40. //修改站点事件
  41. //public static Action<StationItem> OnUpdateItem;
  42. public event Action<StationItem> OnStationItemChange;
  43. private StationItem _selectedStationItem;
  44. public StationItem SelectedStationItem
  45. {
  46. get => _selectedStationItem;
  47. set
  48. {
  49. if(_selectedStationItem != value)
  50. {
  51. _selectedStationItem = value;
  52. OnPropertyChanged(nameof(SelectedStationItem));
  53. OnStationItemChange?.Invoke(value);
  54. }
  55. }
  56. }
  57. //要查找的站点ID
  58. private string _findStationId;
  59. private string _standId;
  60. private MarkFindType _markFindType;
  61. public async void ChangeFindAsync(string stationId, MarkFindType markFindType, string standId)
  62. {
  63. await ChangeFind(stationId, markFindType, standId);
  64. }
  65. public async Task ChangeFind(string stationId, MarkFindType markFindType,string standId)
  66. {
  67. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  68. try
  69. {
  70. _findStationId = stationId;
  71. _standId = standId;
  72. _markFindType = markFindType;
  73. StationPage.InitDefaulValue();
  74. await LoadStationItemList(true);
  75. }
  76. catch { }
  77. finally
  78. {
  79. _semaphore.Release();
  80. }
  81. }
  82. public CfginiItem mConfigItem { get; set; }
  83. public PageModel StationPage { get; set; }
  84. private int _totalRecords;
  85. public int TotalRecords
  86. {
  87. get => _totalRecords;
  88. set
  89. {
  90. if(_totalRecords != value)
  91. {
  92. _totalRecords = value;
  93. OnPropertyChanged(nameof(TotalRecords));
  94. }
  95. }
  96. }
  97. public UCStationGrid()
  98. {
  99. InitializeComponent();
  100. StationItemList = new BulkObservableCollection<StationItem>();
  101. dgStation.ItemsSource = StationItemList;
  102. mConfigItem = CfginiItem.GetConfigItem();
  103. StationPage = new PageModel
  104. {
  105. PageSize = mConfigItem.PageSize2, //mConfigItem.PatchPageSize,
  106. PageNumber = 1
  107. };
  108. //mConfigItem.OnPatchPageSizeChanged += MConfigItem_OnPatchPageSizeChanged;
  109. mConfigItem.OnPageSize2Changed += MConfigItem_OnPageSize2Changed;
  110. this.DataContext = this;
  111. }
  112. private async void MConfigItem_OnPageSize2Changed(int pageSize2)
  113. {
  114. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  115. try
  116. {
  117. StationPage.InitDefaulValue(pageSize2);
  118. await LoadStationItemList(true);
  119. }
  120. catch { }
  121. finally
  122. {
  123. _semaphore.Release();
  124. }
  125. }
  126. public void ChangeMarkCount()
  127. {
  128. if(SelectedStationItem == null)
  129. {
  130. return;
  131. }
  132. int iMarkCount = DBStation.GetMarkCount(SelectedStationItem.Id);
  133. SelectedStationItem.MarkCount = iMarkCount;
  134. }
  135. private async Task LoadStationItemList(bool scrollTop)
  136. {
  137. StationItemList.Clear();
  138. if(_findStationId == null)
  139. {
  140. return;
  141. }
  142. try
  143. {
  144. var result = await Task.Run(() =>
  145. {
  146. //return DBStation.GetPagedTStations(StationPage.PageNumber, StationPage.PageSize,
  147. return DBStation.GetPagedVStations(StationPage.PageNumber, StationPage.PageSize,
  148. _findStationId, _markFindType,_standId);
  149. });
  150. //更新分页信息
  151. TotalRecords = result.Item1;
  152. StationPage.PageCount = result.Item2;
  153. List<TStation> stationList = result.Item3;
  154. //在主线中更新数据
  155. //Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  156. //{
  157. //int index = 0;
  158. //foreach(TStation station in stationList)
  159. //{
  160. // index++;
  161. // StationItem item = new StationItem(station);
  162. // item.Index = index + ((StationPage.PageNumber - 1) * StationPage.PageSize);
  163. // StationItemList.Add(item);
  164. //}
  165. var stationItems = stationList.Select((station, i) => new StationItem(station)
  166. {
  167. Index = (StationPage.PageNumber - 1) * StationPage.PageSize + i + 1
  168. }).ToList();
  169. StationItemList.AddRange(stationItems);
  170. //StationItemList.Re
  171. //SelectedStationItem = null;
  172. if(StationItemList.Count > 0 && scrollTop)
  173. {
  174. //await Task.Delay(100);
  175. await Dispatcher.BeginInvoke(new Action(() =>
  176. {
  177. var scrollViewer = GetScrollViewer(dgStation);
  178. scrollViewer?.ScrollToTop();
  179. //dgStation.ScrollIntoView(dgStation.Items[0]);
  180. }));
  181. }
  182. //}));
  183. }
  184. catch(Exception ex)
  185. {
  186. MessageBox.Show(Application.Current.MainWindow,
  187. $"b加载数据是发生错误:{ex.Message}", "错误",
  188. MessageBoxButton.OK, MessageBoxImage.Error);
  189. }
  190. //if(StationItemList.Count > 0)
  191. //{
  192. // SelectedStationItem = StationItemList[0];
  193. //}
  194. //else
  195. //{
  196. // SelectedStationItem = null;
  197. //}
  198. }
  199. private ScrollViewer _scrollViewer;
  200. private ScrollViewer GetScrollViewer(DependencyObject obj)
  201. {
  202. if (_scrollViewer != null)
  203. {
  204. return _scrollViewer;
  205. }
  206. if (obj is ScrollViewer)
  207. {
  208. _scrollViewer = (ScrollViewer)obj;
  209. return obj as ScrollViewer;
  210. }
  211. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
  212. {
  213. var child = VisualTreeHelper.GetChild(obj, i);
  214. var scrollViewer = GetScrollViewer(child);
  215. if (scrollViewer != null)
  216. {
  217. _scrollViewer = scrollViewer;
  218. return scrollViewer;
  219. }
  220. }
  221. return null;
  222. }
  223. private void MiUpdate_Click(object sender, RoutedEventArgs e)
  224. {
  225. if (SelectedStationItem == null) return;
  226. EditStationDlg2 dialog = new EditStationDlg2(SelectedStationItem)
  227. {
  228. Owner = Application.Current.MainWindow,
  229. WindowStartupLocation = WindowStartupLocation.CenterOwner
  230. };
  231. if (dialog.ShowDialog() == true)
  232. {
  233. SelectedStationItem = dialog.mStationItem;
  234. }
  235. //Station_Config?.Invoke(SelectedStationItem);
  236. }
  237. private void MiDelete_Click(object sender, RoutedEventArgs e)
  238. {
  239. if (SelectedStationItem == null) return;
  240. //获取当前行绑定的数据项
  241. DeleteSelectedItem(SelectedStationItem);
  242. }
  243. private static T FindAncestor<T>(DependencyObject dependencyObject) where T : DependencyObject
  244. {
  245. var parent = VisualTreeHelper.GetParent(dependencyObject);
  246. if (parent == null) return null;
  247. var parentT = parent as T;
  248. return parentT ?? FindAncestor<T>(parent);
  249. }
  250. private void BtnUpdateStation_Click(object sender, RoutedEventArgs e)
  251. {
  252. Button button = sender as Button;
  253. if (button == null) return;
  254. DataGridRow row = FindAncestor<DataGridRow>(button);
  255. if (row == null) return;
  256. StationItem stationItem = row.Item as StationItem;
  257. EditStationDlg2 dialog = new EditStationDlg2(stationItem)
  258. {
  259. Owner = Application.Current.MainWindow,
  260. WindowStartupLocation = WindowStartupLocation.CenterOwner
  261. };
  262. if (dialog.ShowDialog() == true)
  263. {
  264. stationItem = dialog.mStationItem;
  265. }
  266. //Station_Config?.Invoke(SelectedStationItem);
  267. }
  268. private void BtnDeleteStation_Click(object sender, RoutedEventArgs e)
  269. {
  270. Button button = sender as Button;
  271. if (button == null) return;
  272. DataGridRow row = FindAncestor<DataGridRow>(button);
  273. if (row == null) return;
  274. StationItem stationItem = row.Item as StationItem;
  275. DeleteSelectedItem(stationItem);
  276. }
  277. private async void DeleteSelectedItem(StationItem selectedItem)
  278. {
  279. if (selectedItem == null) return;
  280. MessageBoxResult result = MessageBox.Show("确定要删除此条目吗?", "确认删除", MessageBoxButton.YesNo, MessageBoxImage.Question);
  281. if (result != MessageBoxResult.Yes) return;
  282. string titleInfo = "正在删除,请稍后...";
  283. WaitWindow waitWindow = new WaitWindow(titleInfo)
  284. {
  285. Owner = Application.Current.MainWindow,
  286. WindowStartupLocation = WindowStartupLocation.CenterOwner
  287. };
  288. waitWindow.Show();
  289. try
  290. {
  291. bool deleteSuccess = false;
  292. await Task.Run(() =>
  293. {
  294. deleteSuccess = DBStation.DeleteStationAndStandDetails(selectedItem.StandId, selectedItem.StationId);
  295. //DBStation.DeleteTStation(selectedItem.StationId);
  296. });
  297. if (deleteSuccess)
  298. {
  299. //await Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  300. //{
  301. // 从数据源中移除该条目
  302. StationItemList.Remove(selectedItem);
  303. SelectedStationItem = null;
  304. //}));
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. MessageBox.Show(Application.Current.MainWindow, $"删除失败:{ex.Message}", "错误",
  310. MessageBoxButton.OK, MessageBoxImage.Error);
  311. }
  312. finally
  313. {
  314. waitWindow.Close();
  315. }
  316. }
  317. public async void ClearAllStation()
  318. {
  319. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  320. try
  321. {
  322. //清空提示
  323. MessageBoxResult result = MessageBox.Show("确定要清空所有数据吗?", "确认清空", MessageBoxButton.YesNo, MessageBoxImage.Question);
  324. if (result != MessageBoxResult.Yes) return;
  325. string titleInfo = "正在清空数据,请稍后...";
  326. WaitWindow waitWindow = new WaitWindow(titleInfo)
  327. {
  328. Owner = Application.Current.MainWindow,
  329. WindowStartupLocation = WindowStartupLocation.CenterOwner
  330. };
  331. waitWindow.Show();
  332. try
  333. {
  334. bool blClearAll = false;
  335. await Task.Run(() =>
  336. {
  337. blClearAll = DBStation.ClearTStationTable();
  338. });
  339. if (blClearAll)
  340. {
  341. StationPage.InitDefaulValue();
  342. await LoadStationItemList(true);
  343. }
  344. }
  345. catch (Exception ex)
  346. {
  347. MessageBox.Show(Application.Current.MainWindow, $"清空失败:{ex.Message}", "错误",
  348. MessageBoxButton.OK, MessageBoxImage.Error);
  349. }
  350. finally
  351. {
  352. waitWindow.Close();
  353. }
  354. }
  355. catch { }
  356. finally
  357. {
  358. _semaphore.Release();
  359. }
  360. }
  361. public async Task NextPage()
  362. {
  363. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  364. try
  365. {
  366. if (StationPage.PageNumber < StationPage.PageCount)
  367. {
  368. StationPage.PageNumber += 1;
  369. await LoadStationItemList(true);
  370. }
  371. }
  372. catch { }
  373. finally
  374. {
  375. _semaphore.Release();
  376. }
  377. }
  378. public async Task PrePage()
  379. {
  380. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  381. try
  382. {
  383. if (StationPage.PageNumber > 1)
  384. {
  385. StationPage.PageNumber -= 1;
  386. await LoadStationItemList(true);
  387. }
  388. }
  389. catch{}
  390. finally
  391. {
  392. _semaphore.Release();
  393. }
  394. }
  395. public async Task FirstPage()
  396. {
  397. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  398. try
  399. {
  400. if (StationPage.PageNumber > 1)
  401. {
  402. StationPage.PageNumber = 1;
  403. await LoadStationItemList(true);
  404. }
  405. }
  406. catch { }
  407. finally
  408. {
  409. _semaphore.Release();
  410. }
  411. }
  412. public async Task LastPage()
  413. {
  414. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  415. try
  416. {
  417. if (StationPage.PageNumber < StationPage.PageCount)
  418. {
  419. StationPage.PageNumber = StationPage.PageCount;
  420. await LoadStationItemList(true);
  421. }
  422. }
  423. catch { }
  424. finally
  425. {
  426. _semaphore.Release();
  427. }
  428. }
  429. public async Task SpeciPage(int pageNumber)
  430. {
  431. if (!await _semaphore.WaitAsync(0)) return; // 防止重复调用
  432. try
  433. {
  434. if (pageNumber != StationPage.PageNumber &&
  435. pageNumber > 0 && pageNumber <= StationPage.PageCount)
  436. {
  437. StationPage.PageNumber = pageNumber;
  438. await LoadStationItemList(true);
  439. }
  440. }
  441. catch { }
  442. finally
  443. {
  444. _semaphore.Release();
  445. }
  446. }
  447. //---------------------------------------------------------------------
  448. }
  449. }