20240301_JSEQ_upperpc/JiangsuEarthquakeJM/JiangsuEarthquake/ViewModels/AlarmRecordViewModel.cs
春风过客 d7f51483a7 新增功能:
1 将地震仪数据读取功能单独成一个页面;
2024-05-13 18:51:05 +08:00

400 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using JiangsuEarthquake.Common;
using JiangsuEarthquake.DataAccess;
using JiangsuEarthquake.Models;
using JiangsuEarthquake.Views.UserControls;
using MySql.Data.MySqlClient;
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.Media;
using System.Windows.Threading;
namespace JiangsuEarthquake.ViewModels
{
public class AlarmRecordViewModel : NotifyBase
{
#region Search Data
private DateTime _startDateTime;
public DateTime StartDateTime
{
get { return _startDateTime; }
set { Set(ref _startDateTime, value); }
}
private DateTime _endDateTime;
public DateTime EndDateTime
{
get { return _endDateTime; }
set { Set(ref _endDateTime, value); }
}
int PageIndex = 0;
private int recordCount;
public int RecordCount
{
get { return recordCount; }
set { recordCount = value; this.DoNotify(); }
}
private int totalPage;
public int TotalPage
{
get { return totalPage; }
set { totalPage = value; this.DoNotify(); }
}
private string alarmMsg;
public string AlarmMsg
{
get { return alarmMsg; }
set { alarmMsg = value; this.DoNotify(); }
}
#endregion
private ObservableCollection<AlarmRecordModel> alarmRecordList = new ObservableCollection<AlarmRecordModel>();
public ObservableCollection<AlarmRecordModel> AlarmRecordList
{
get { return alarmRecordList; }
set { alarmRecordList = value; this.DoNotify(); }
}
private ObservableCollection<AlarmRecordModel> totalAlarmRecordList = new ObservableCollection<AlarmRecordModel>();
public ObservableCollection<AlarmRecordModel> TotalAlarmRecordList
{
get { return totalAlarmRecordList; }
set { totalAlarmRecordList = value; this.DoNotify(); }
}
#region CommandBase Define
public CommandBase ForwordAlarmCommand { get; set; }
public CommandBase NextAlarmCommand { get; set; }
public CommandBase RefreshDataCommand { get; set; }
public CommandBase DownloadDataCommand { get; set; }
#endregion
int station_id = 1;
string timeSearch = "";
public DispatcherTimer timerDownloadDataMsgHidden = new DispatcherTimer();
#region Data Filtering
public static DialogViewModel vm;
private bool _isChecked;
public bool IsChecked
{
get { return _isChecked; }
set
{
_isChecked = value;
this.DoNotify();
if (IsChecked)
{
if (string.IsNullOrEmpty(EndDateTime.ToString()) || string.IsNullOrEmpty(StartDateTime.ToString()))
{
IsChecked = false;
return;
}
if (EndDateTime < StartDateTime)
{
HandyControl.Controls.Dialog.Show(new TextDialog("起始时间大于结束时间,\n请重新输入"));
IsChecked = false;
//this.EndDateTime = DateTime.Now;
//this.StartDateTime = DateTime.Now.AddDays(-1);
return;
}
timeSearch = " and DataTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' ";
}
else
{
timeSearch = "";
}
//设置当前页为1
PageIndex = 1;
TotalAlarmRecordList.Clear();
AlarmRecordList.Clear();
string sql = String.Format("select * from alarminfohis where StationID = {0} {1} ORDER by id desc limit 100", station_id, timeSearch);
MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1);
int index = 1;
while (dataReader.Read())
{
AlarmRecordModel alarmRecordModel = new AlarmRecordModel();
alarmRecordModel.Index = index++;
alarmRecordModel.DataTime = Convert.ToDateTime(dataReader["DataTime"]);
alarmRecordModel.ParaName = dataReader["ParaName"].ToString();
alarmRecordModel.ParaState = dataReader["ParaState"].ToString();
alarmRecordModel.ProcessingMethod = dataReader["ProcessingMethod"].ToString();
if (Convert.ToInt32(dataReader["UrgencyLevel"]) == 1)
{
alarmRecordModel.UrgencyLevel = (ImageSource)Application.Current.FindResource("CycleYellow");
}
else if (Convert.ToInt32(dataReader["UrgencyLevel"]) == 2)
{
alarmRecordModel.UrgencyLevel = (ImageSource)Application.Current.FindResource("CycleOrange");
}
else
{
alarmRecordModel.UrgencyLevel = (ImageSource)Application.Current.FindResource("CycleRed");
}
TotalAlarmRecordList.Add(alarmRecordModel);
}
dataReader.Dispose();
RecordCount = index - 1;
if (RecordCount <= 10)
{
TotalPage = 1;
AlarmRecordList = TotalAlarmRecordList;
}
else
{
TotalPage = (int)Math.Ceiling((double)RecordCount / 10);
for (int i = 0; i < 10; i++)
{
AlarmRecordList.Add(TotalAlarmRecordList[i]);
}
}
AlarmMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex);
}
}
#endregion
public AlarmRecordViewModel(int id)
{
// 默认查询1天内的日志
this.EndDateTime = DateTime.Now;
this.StartDateTime = DateTime.Now.AddDays(-1);
PageIndex = 1;
station_id = id;
vm = new DialogViewModel
{
Content = ""
};
#region Command Set
this.ForwordAlarmCommand = new CommandBase();
this.ForwordAlarmCommand.DoExcute = new Action<object>(ForwordAlarm);
this.ForwordAlarmCommand.DoCanExcute = new Func<object, bool>((o) => true);
this.NextAlarmCommand = new CommandBase();
this.NextAlarmCommand.DoExcute = new Action<object>(NextAlarm);
this.NextAlarmCommand.DoCanExcute = new Func<object, bool>((o) => true);
this.RefreshDataCommand = new CommandBase();
this.RefreshDataCommand.DoExcute = new Action<object>(RefreshData);
this.RefreshDataCommand.DoCanExcute = new Func<object, bool>((o) => true);
this.DownloadDataCommand = new CommandBase();
this.DownloadDataCommand.DoExcute = new Action<object>(DownloadData);
this.DownloadDataCommand.DoCanExcute = new Func<object, bool>((o) => true);
#endregion
timerDownloadDataMsgHidden.Interval = TimeSpan.FromSeconds(2);
timerDownloadDataMsgHidden.Tick += TimerDownloadDataMsgHidden_Tick;
}
#region RefreshData
public void RefreshData(object o)
{
if (IsChecked)
{
timeSearch = " and DataTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' ";
}
else
{
timeSearch = "";
}
//设置当前页为1
PageIndex = 1;
TotalAlarmRecordList.Clear();
AlarmRecordList.Clear();
string sql = String.Format("select * from alarminfohis where StationID = {0} {1} ORDER by id desc limit 100", station_id, timeSearch);
MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1);
int index = 1;
while (dataReader.Read())
{
AlarmRecordModel alarmRecordModel = new AlarmRecordModel();
alarmRecordModel.Index = index++;
alarmRecordModel.DataTime = Convert.ToDateTime(dataReader["DataTime"]);
alarmRecordModel.ParaName = dataReader["ParaName"].ToString();
alarmRecordModel.ParaState = dataReader["ParaState"].ToString();
alarmRecordModel.ProcessingMethod = dataReader["ProcessingMethod"].ToString();
if (Convert.ToInt32(dataReader["UrgencyLevel"]) == 1)
{
alarmRecordModel.UrgencyLevel = (ImageSource)Application.Current.FindResource("CycleRed");
}
else if (Convert.ToInt32(dataReader["UrgencyLevel"]) == 2)
{
alarmRecordModel.UrgencyLevel = (ImageSource)Application.Current.FindResource("CycleOrange");
}
else
{
alarmRecordModel.UrgencyLevel = (ImageSource)Application.Current.FindResource("CycleYellow");
}
TotalAlarmRecordList.Add(alarmRecordModel);
}
dataReader.Dispose();
RecordCount = index - 1;
if (RecordCount <= 10)
{
TotalPage = 1;
AlarmRecordList = TotalAlarmRecordList;
}
else
{
TotalPage = (int)Math.Ceiling((double)RecordCount / 10);
for (int i = 0; i < 10; i++)
{
AlarmRecordList.Add(TotalAlarmRecordList[i]);
}
}
AlarmMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex);
}
#endregion
#region DownloadData
private void TimerDownloadDataMsgHidden_Tick(object sender, EventArgs e)
{
DownloadDataMsgVisibility = Visibility.Hidden;
// 停止定时器
(sender as DispatcherTimer).Stop();
}
private string downloadDataMsg;
public string DownloadDataMsg
{
get { return downloadDataMsg; }
set { downloadDataMsg = value; this.DoNotify(); }
}
private Brush downloadDataMsgForeground;
public Brush DownloadDataMsgForeground
{
get { return downloadDataMsgForeground; }
set { downloadDataMsgForeground = value; this.DoNotify(); }
}
private bool downloadDataBtnIsEnabled = true;
public bool DownloadDataBtnIsEnabled
{
get { return downloadDataBtnIsEnabled; }
set { downloadDataBtnIsEnabled = value; this.DoNotify(); }
}
private Visibility downloadDataMsgVisibility = Visibility.Visible;
public Visibility DownloadDataMsgVisibility
{
get { return downloadDataMsgVisibility; }
set { downloadDataMsgVisibility = value; this.DoNotify(); }
}
public void DownloadData(object o)
{
DownloadDataBtnIsEnabled = false;
DownloadDataMsgVisibility = Visibility.Visible;
string baseStationFolder = Tools.GetAppSetting("AlarmRecordFolder");
string savePath = CSVDownload.CreateFile(baseStationFolder, "AlarmRecord_" + DateTime.Now.ToString("yyyyMMddhhMMss"), "csv");
bool result = CSVDownload.SaveAlarmRecordDataToCSVFile(TotalAlarmRecordList, savePath);
if (result)
{
DownloadDataMsg = "下载数据成功!";
DownloadDataMsgForeground = new SolidColorBrush(Colors.Green);
}
else
{
DownloadDataMsg = "下载数据失败!";
DownloadDataMsgForeground = new SolidColorBrush(Colors.Red);
}
timerDownloadDataMsgHidden.Start();
DownloadDataBtnIsEnabled = true;
}
#endregion
#region Page Switching
public void ForwordAlarm(object o)
{
if (PageIndex == 1)
return;
PageIndex -= 1;
AlarmRecordList.Clear();
for (int i = 0; i < 10; i++)
{
AlarmRecordList.Add(TotalAlarmRecordList[i + (PageIndex - 1) * 10]);
}
AlarmMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex);
}
public void NextAlarm(object o)
{
if (PageIndex == TotalPage)
return;
PageIndex += 1;
AlarmRecordList.Clear();
for (int i = 0; i < (PageIndex == TotalPage ? (RecordCount - (PageIndex - 1) * 10) : 10); i++)
{
AlarmRecordList.Add(TotalAlarmRecordList[i + (PageIndex - 1) * 10]);
}
AlarmMsg = string.Format("共计{0}页,当前第{1}页", TotalPage, PageIndex);
}
#endregion
}
}