DIList.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. namespace GDNXFD.Alert.Config.Views
  2. {
  3. using GalaSoft.MvvmLight.Command;
  4. using GDNXFD.Alert.Config.Helpers;
  5. using GDNXFD.Alert.Config.ViewModel;
  6. using GDNXFD.Data;
  7. using GDNXFD.Data.Repositories;
  8. using Model;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Data;
  14. /// <summary>
  15. /// Interaction logic for TravelList.xaml
  16. /// </summary>
  17. public partial class DIList : UserControl
  18. {
  19. private List<PageItem> pageList;
  20. private PaginationConfig pageConfig;
  21. string station = string.Empty;
  22. string rank = string.Empty;
  23. string category = string.Empty;
  24. string tagDesc = string.Empty;
  25. string status = string.Empty;
  26. private List<string> _statusList;
  27. private List<string> _rankList;
  28. private List<string> _stationList;
  29. bool isLoading = true;
  30. /// <summary>
  31. /// Travel List constructor
  32. /// </summary>
  33. public DIList()
  34. {
  35. InitializeComponent();
  36. pageConfig = new PaginationConfig(0, 54);
  37. InitializeLists();
  38. NavigateToPage(1);
  39. isLoading = false;
  40. }
  41. private void InitializeLists()
  42. {
  43. _statusList = new List<string> { "升压站", "光伏" };
  44. _rankList = new List<string> { "高", "中高" ,"中","中低","低"};
  45. _stationList = new List<string> { "麻黄山风场", "牛首山风场", "青山风场", "石板泉风场", "香山风场", "大武口光伏电站", "平罗光伏电站", "宣和光伏电站", "马场湖光伏电站" };
  46. var statusViewSource = ((CollectionViewSource)(FindResource("statusViewSource")));
  47. statusViewSource.Source = _statusList;
  48. var rankViewSource = ((CollectionViewSource)(FindResource("rankViewSource")));
  49. rankViewSource.Source = _rankList;
  50. var stationViewSource = ((CollectionViewSource)(FindResource("stationViewSource")));
  51. stationViewSource.Source = _stationList;
  52. }
  53. private void TextBoxKeyboard_LostFocus(object sender, System.Windows.RoutedEventArgs e)
  54. {
  55. TabtipHelper tabtipHelper = TabtipHelper.Instance;
  56. tabtipHelper.TryKillTabtipProcess();
  57. }
  58. private void btnRefresh_Click(object sender, System.Windows.RoutedEventArgs e)
  59. {
  60. NavigateToPage(1);
  61. }
  62. private void btnNew_Click(object sender, System.Windows.RoutedEventArgs e)
  63. {
  64. dgDIList.CanUserAddRows = true;
  65. }
  66. private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
  67. {
  68. tagDesc = txtFilter.Text.Trim();
  69. }
  70. private void cmbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
  71. {
  72. var item = cmbCategory.SelectedItem as ComboBoxItem;
  73. if (item == null || item.Tag == null)
  74. category = string.Empty;
  75. else
  76. category = item.Tag.ToString();
  77. }
  78. private void cmbRank_SelectionChanged(object sender, SelectionChangedEventArgs e)
  79. {
  80. var item = cmbRank.SelectedItem as ComboBoxItem;
  81. if (item == null || item.Tag == null)
  82. rank = string.Empty;
  83. else
  84. rank = item.Tag.ToString();
  85. }
  86. private void cmbStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
  87. {
  88. var item = cmbStation.SelectedItem as ComboBoxItem;
  89. if (item == null || item.Tag == null)
  90. station = string.Empty;
  91. else
  92. station = item.Tag.ToString();
  93. }
  94. private void cmbConfirmStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
  95. {
  96. var item = cmbConfirmStatus.SelectedItem as ComboBoxItem;
  97. if (item == null || item.Tag == null)
  98. status = string.Empty;
  99. else
  100. status = item.Tag.ToString();
  101. }
  102. private void chkChanged(object sender, RoutedEventArgs e)
  103. {
  104. if (isLoading == true)
  105. return;
  106. var chk = e.OriginalSource as CheckBox;
  107. if (chk == null)
  108. return;
  109. var model = chk.DataContext as IFixBJTestingPointDI;
  110. if (model == null)
  111. return;
  112. if (ValidateModel(model) == true)
  113. {
  114. IFixBJTestingPointDI obj = AlertSnapRepository.GetIFixBjModel(model.PointKey);
  115. if (obj != null)
  116. {
  117. if (string.IsNullOrEmpty(model.Id) || obj.Id != model.Id)
  118. {
  119. MessageBox.Show("测点已存在: " + obj.PointKey);
  120. return;
  121. }
  122. if (obj.Enabled == model.Enabled)
  123. return;
  124. AlertSnapRepository.UpdateIFixBj(model);
  125. }
  126. }
  127. }
  128. #region 分页操作
  129. private void PrePage_Click(object sender, RoutedEventArgs e)
  130. {
  131. if (this.pageConfig.CanGoPrevious)
  132. {
  133. this.pageConfig.NumberOfPageSelected = this.pageConfig.NumberOfPageSelected - 1;
  134. NavigateToPage(this.pageConfig.NumberOfPageSelected);
  135. }
  136. }
  137. private void NextPage_Click(object sender, RoutedEventArgs e)
  138. {
  139. if (this.pageConfig.CanGoNext)
  140. {
  141. this.pageConfig.NumberOfPageSelected = this.pageConfig.NumberOfPageSelected + 1;
  142. NavigateToPage(this.pageConfig.NumberOfPageSelected);
  143. }
  144. }
  145. private void FirstPage_Click(object sender, RoutedEventArgs e)
  146. {
  147. this.pageConfig.NumberOfPageSelected = 1;
  148. NavigateToPage(1);
  149. }
  150. private void LastPage_Click(object sender, RoutedEventArgs e)
  151. {
  152. this.pageConfig.NumberOfPageSelected = this.pageConfig.NumberOfPages;
  153. NavigateToPage(this.pageConfig.NumberOfPages);
  154. }
  155. public List<PageItem> GetPagesList()
  156. {
  157. var pagesList = new List<PageItem>();
  158. pagesList.Add(new PageItem((1).ToString(), true, true));
  159. for (int i = 2; i <= 5; i++)
  160. {
  161. pagesList.Add(new PageItem((i).ToString(), false, true));
  162. }
  163. return pagesList;
  164. }
  165. private List<PageItem> CalculatePaginator()
  166. {
  167. var pages = new List<PageItem>();
  168. int firstPage = (int)Math.Floor((double)this.pageConfig.NumberOfPageSelected / 12) * 12;
  169. if (firstPage > 0)
  170. firstPage = firstPage - 2;
  171. for (int i = firstPage; i < (firstPage + Math.Min(12, this.pageConfig.NumberOfPages)) && (i < this.pageConfig.NumberOfPages); i++)
  172. {
  173. var isSelected = false;
  174. if (this.pageConfig.NumberOfPageSelected == i + 1)
  175. isSelected = true;
  176. PageItem page = new PageItem((i + 1).ToString(), isSelected, true);
  177. page.PageSelected += PageSelectedChanged;
  178. pages.Add(page);
  179. }
  180. if (this.pageConfig.NumberOfPages > this.pageConfig.PageSize && int.Parse(pages[pages.Count - 1].Page) != this.pageConfig.NumberOfPages)
  181. {
  182. pages.Add(new PageItem("...", false, false));
  183. var lastPage = new PageItem(this.pageConfig.NumberOfPages.ToString(), false, true);
  184. pages.Add(lastPage);
  185. lastPage.PageSelected += PageSelectedChanged;
  186. }
  187. return pages;
  188. }
  189. private void PageSelectedChanged(object sender, EventArgs args)
  190. {
  191. this.pageConfig.PageSelected = (PageItem)sender;
  192. NavigateToPage(this.pageConfig.NumberOfPageSelected);
  193. }
  194. private void ResetPaginator()
  195. {
  196. if (this.pageList != null)
  197. {
  198. foreach (var page in this.pageList)
  199. {
  200. page.PageSelected -= PageSelectedChanged;
  201. }
  202. }
  203. }
  204. private void NavigateToPage(int pageToNavigate)
  205. {
  206. isLoading = true;
  207. try
  208. {
  209. ResetPaginator();
  210. // 1.查询数据绑定到listview
  211. string filter = CreateFilter();
  212. int total = 0;
  213. var alerts = AlertSnapRepository.GetIFixBjPage(filter, pageToNavigate, pageConfig.PageSize, ref total);
  214. this.dgDIList.ItemsSource = alerts;
  215. // 2.构造分页器绑定到PageList
  216. pageConfig.ItemsCounted = total;
  217. pageList = CalculatePaginator();
  218. this.icPages.ItemsSource = pageList;
  219. }
  220. catch (Exception ex)
  221. {
  222. //logger.Error("查询历史报警时发生异常:" + ex.Message);
  223. }
  224. isLoading = false;
  225. }
  226. private string CreateFilter()
  227. {
  228. return String.Format("{0},{1},{2},{3},{4}",
  229. station, rank, category, status, tagDesc);
  230. }
  231. #endregion
  232. private void dgDIList_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
  233. {
  234. if (e.AddedCells.Count == 0) return;
  235. var currentCell = e.AddedCells[0];
  236. string header = (string)currentCell.Column.Header;
  237. if (currentCell.Column == dgDIList.Columns[0]
  238. || currentCell.Column == dgDIList.Columns[3]
  239. || currentCell.Column == dgDIList.Columns[4])
  240. {
  241. dgDIList.BeginEdit();
  242. }
  243. }
  244. private void dgDIList_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
  245. {
  246. var model = e.Row.Item as IFixBJTestingPointDI;
  247. if (ValidateModel(model) == true)
  248. {
  249. IFixBJTestingPointDI obj = AlertSnapRepository.GetIFixBjModel(model.PointKey);
  250. if (obj != null)
  251. {
  252. if (string.IsNullOrEmpty(model.Id) || obj.Id != model.Id)
  253. {
  254. MessageBox.Show("测点已存在: " + obj.PointKey);
  255. return;
  256. }
  257. AlertSnapRepository.UpdateIFixBj(model);
  258. }
  259. else
  260. {
  261. AlertSnapRepository.AddIFixBj(model);
  262. }
  263. }
  264. //dgDIList.CanUserAddRows = false;
  265. return;
  266. }
  267. private bool ValidateModel(IFixBJTestingPointDI model)
  268. {
  269. if (model == null)
  270. return false;
  271. if (string.IsNullOrWhiteSpace(model.PointKey) ||
  272. string.IsNullOrWhiteSpace(model.Description) ||
  273. string.IsNullOrWhiteSpace(model.StationId))
  274. return false;
  275. if (string.IsNullOrWhiteSpace(model.LevelId))
  276. model.LevelId = "MEDIUM";
  277. if (string.IsNullOrWhiteSpace(model.Area1))
  278. model.Area1 = "SYZ";
  279. return true;
  280. }
  281. }
  282. }