namespace GDNXFD.Alert.Config.Views
{
using GalaSoft.MvvmLight.Command;
using GDNXFD.Alert.Config.Helpers;
using GDNXFD.Alert.Config.ViewModel;
using GDNXFD.Data;
using GDNXFD.Data.Repositories;
using Model;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
///
/// Interaction logic for TravelList.xaml
///
public partial class DIList : UserControl
{
private List pageList;
private PaginationConfig pageConfig;
string station = string.Empty;
string rank = string.Empty;
string category = string.Empty;
string tagDesc = string.Empty;
string status = string.Empty;
private List _statusList;
private List _rankList;
private List _stationList;
bool isLoading = true;
///
/// Travel List constructor
///
public DIList()
{
InitializeComponent();
pageConfig = new PaginationConfig(0, 54);
InitializeLists();
NavigateToPage(1);
isLoading = false;
}
private void InitializeLists()
{
_statusList = new List { "升压站", "光伏" };
_rankList = new List { "高", "中高" ,"中","中低","低"};
_stationList = new List { "麻黄山风场", "牛首山风场", "青山风场", "石板泉风场", "香山风场", "大武口光伏电站", "平罗光伏电站", "宣和光伏电站", "马场湖光伏电站" };
var statusViewSource = ((CollectionViewSource)(FindResource("statusViewSource")));
statusViewSource.Source = _statusList;
var rankViewSource = ((CollectionViewSource)(FindResource("rankViewSource")));
rankViewSource.Source = _rankList;
var stationViewSource = ((CollectionViewSource)(FindResource("stationViewSource")));
stationViewSource.Source = _stationList;
}
private void TextBoxKeyboard_LostFocus(object sender, System.Windows.RoutedEventArgs e)
{
TabtipHelper tabtipHelper = TabtipHelper.Instance;
tabtipHelper.TryKillTabtipProcess();
}
private void btnRefresh_Click(object sender, System.Windows.RoutedEventArgs e)
{
NavigateToPage(1);
}
private void btnNew_Click(object sender, System.Windows.RoutedEventArgs e)
{
dgDIList.CanUserAddRows = true;
}
private void txtFilter_TextChanged(object sender, TextChangedEventArgs e)
{
tagDesc = txtFilter.Text.Trim();
}
private void cmbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbCategory.SelectedItem as ComboBoxItem;
if (item == null || item.Tag == null)
category = string.Empty;
else
category = item.Tag.ToString();
}
private void cmbRank_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbRank.SelectedItem as ComboBoxItem;
if (item == null || item.Tag == null)
rank = string.Empty;
else
rank = item.Tag.ToString();
}
private void cmbStation_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbStation.SelectedItem as ComboBoxItem;
if (item == null || item.Tag == null)
station = string.Empty;
else
station = item.Tag.ToString();
}
private void cmbConfirmStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbConfirmStatus.SelectedItem as ComboBoxItem;
if (item == null || item.Tag == null)
status = string.Empty;
else
status = item.Tag.ToString();
}
private void chkChanged(object sender, RoutedEventArgs e)
{
if (isLoading == true)
return;
var chk = e.OriginalSource as CheckBox;
if (chk == null)
return;
var model = chk.DataContext as IFixBJTestingPointDI;
if (model == null)
return;
if (ValidateModel(model) == true)
{
IFixBJTestingPointDI obj = AlertSnapRepository.GetIFixBjModel(model.PointKey);
if (obj != null)
{
if (string.IsNullOrEmpty(model.Id) || obj.Id != model.Id)
{
MessageBox.Show("测点已存在: " + obj.PointKey);
return;
}
if (obj.Enabled == model.Enabled)
return;
AlertSnapRepository.UpdateIFixBj(model);
}
}
}
#region 分页操作
private void PrePage_Click(object sender, RoutedEventArgs e)
{
if (this.pageConfig.CanGoPrevious)
{
this.pageConfig.NumberOfPageSelected = this.pageConfig.NumberOfPageSelected - 1;
NavigateToPage(this.pageConfig.NumberOfPageSelected);
}
}
private void NextPage_Click(object sender, RoutedEventArgs e)
{
if (this.pageConfig.CanGoNext)
{
this.pageConfig.NumberOfPageSelected = this.pageConfig.NumberOfPageSelected + 1;
NavigateToPage(this.pageConfig.NumberOfPageSelected);
}
}
private void FirstPage_Click(object sender, RoutedEventArgs e)
{
this.pageConfig.NumberOfPageSelected = 1;
NavigateToPage(1);
}
private void LastPage_Click(object sender, RoutedEventArgs e)
{
this.pageConfig.NumberOfPageSelected = this.pageConfig.NumberOfPages;
NavigateToPage(this.pageConfig.NumberOfPages);
}
public List GetPagesList()
{
var pagesList = new List();
pagesList.Add(new PageItem((1).ToString(), true, true));
for (int i = 2; i <= 5; i++)
{
pagesList.Add(new PageItem((i).ToString(), false, true));
}
return pagesList;
}
private List CalculatePaginator()
{
var pages = new List();
int firstPage = (int)Math.Floor((double)this.pageConfig.NumberOfPageSelected / 12) * 12;
if (firstPage > 0)
firstPage = firstPage - 2;
for (int i = firstPage; i < (firstPage + Math.Min(12, this.pageConfig.NumberOfPages)) && (i < this.pageConfig.NumberOfPages); i++)
{
var isSelected = false;
if (this.pageConfig.NumberOfPageSelected == i + 1)
isSelected = true;
PageItem page = new PageItem((i + 1).ToString(), isSelected, true);
page.PageSelected += PageSelectedChanged;
pages.Add(page);
}
if (this.pageConfig.NumberOfPages > this.pageConfig.PageSize && int.Parse(pages[pages.Count - 1].Page) != this.pageConfig.NumberOfPages)
{
pages.Add(new PageItem("...", false, false));
var lastPage = new PageItem(this.pageConfig.NumberOfPages.ToString(), false, true);
pages.Add(lastPage);
lastPage.PageSelected += PageSelectedChanged;
}
return pages;
}
private void PageSelectedChanged(object sender, EventArgs args)
{
this.pageConfig.PageSelected = (PageItem)sender;
NavigateToPage(this.pageConfig.NumberOfPageSelected);
}
private void ResetPaginator()
{
if (this.pageList != null)
{
foreach (var page in this.pageList)
{
page.PageSelected -= PageSelectedChanged;
}
}
}
private void NavigateToPage(int pageToNavigate)
{
isLoading = true;
try
{
ResetPaginator();
// 1.查询数据绑定到listview
string filter = CreateFilter();
int total = 0;
var alerts = AlertSnapRepository.GetIFixBjPage(filter, pageToNavigate, pageConfig.PageSize, ref total);
this.dgDIList.ItemsSource = alerts;
// 2.构造分页器绑定到PageList
pageConfig.ItemsCounted = total;
pageList = CalculatePaginator();
this.icPages.ItemsSource = pageList;
}
catch (Exception ex)
{
//logger.Error("查询历史报警时发生异常:" + ex.Message);
}
isLoading = false;
}
private string CreateFilter()
{
return String.Format("{0},{1},{2},{3},{4}",
station, rank, category, status, tagDesc);
}
#endregion
private void dgDIList_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count == 0) return;
var currentCell = e.AddedCells[0];
string header = (string)currentCell.Column.Header;
if (currentCell.Column == dgDIList.Columns[0]
|| currentCell.Column == dgDIList.Columns[3]
|| currentCell.Column == dgDIList.Columns[4])
{
dgDIList.BeginEdit();
}
}
private void dgDIList_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
var model = e.Row.Item as IFixBJTestingPointDI;
if (ValidateModel(model) == true)
{
IFixBJTestingPointDI obj = AlertSnapRepository.GetIFixBjModel(model.PointKey);
if (obj != null)
{
if (string.IsNullOrEmpty(model.Id) || obj.Id != model.Id)
{
MessageBox.Show("测点已存在: " + obj.PointKey);
return;
}
AlertSnapRepository.UpdateIFixBj(model);
}
else
{
AlertSnapRepository.AddIFixBj(model);
}
}
//dgDIList.CanUserAddRows = false;
return;
}
private bool ValidateModel(IFixBJTestingPointDI model)
{
if (model == null)
return false;
if (string.IsNullOrWhiteSpace(model.PointKey) ||
string.IsNullOrWhiteSpace(model.Description) ||
string.IsNullOrWhiteSpace(model.StationId))
return false;
if (string.IsNullOrWhiteSpace(model.LevelId))
model.LevelId = "MEDIUM";
if (string.IsNullOrWhiteSpace(model.Area1))
model.Area1 = "SYZ";
return true;
}
}
}