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.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows; using System.Windows.Threading; using Brush = System.Windows.Media.Brush; namespace JiangsuEarthquake.ViewModels { public class SystemMonitorDataViewModel : 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 int systemMonitorTotalPage; public int SystemMonitorTotalPage { get { return systemMonitorTotalPage; } set { systemMonitorTotalPage = value; this.DoNotify(); } } private int systemMonitorNowPage; public int SystemMonitorNowPage { get { return systemMonitorNowPage; } set { systemMonitorNowPage = value; this.DoNotify(); } } #endregion private ObservableCollection systemMonitorDataList = new ObservableCollection(); public ObservableCollection SystemMonitorDataList { get { return systemMonitorDataList; } set { systemMonitorDataList = value; this.DoNotify(); } } private ObservableCollection totalSystemMonitorDataList = new ObservableCollection(); public ObservableCollection TotalSystemMonitorDataList { get { return totalSystemMonitorDataList; } set { totalSystemMonitorDataList = value; this.DoNotify(); } } public static DialogViewModel vm; #region CommandBase Define public CommandBase ForwordSystemMonitorDataCommand { get; set; } //读取日志信息 public CommandBase NextSystemMonitorDataCommand { get; set; } //读取日志信息 public CommandBase RefreshDataCommand { get; set; } public CommandBase DownloadDataCommand { get; set; } public CommandBase SearchEnvironDataCommand { get; set; } public CommandBase ResetSearchCommand { get; set; } #endregion int station_id = 1; public SystemMonitorDataViewModel(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.ForwordSystemMonitorDataCommand = new CommandBase(); this.ForwordSystemMonitorDataCommand.DoExcute = new Action(ForwordSystemMonitorData); this.ForwordSystemMonitorDataCommand.DoCanExcute = new Func((o) => true); this.NextSystemMonitorDataCommand = new CommandBase(); this.NextSystemMonitorDataCommand.DoExcute = new Action(NextSystemMonitorData); this.NextSystemMonitorDataCommand.DoCanExcute = new Func((o) => true); this.RefreshDataCommand = new CommandBase(); this.RefreshDataCommand.DoExcute = new Action(RefreshData); this.RefreshDataCommand.DoCanExcute = new Func((o) => true); this.DownloadDataCommand = new CommandBase(); this.DownloadDataCommand.DoExcute = new Action(DownloadData); this.DownloadDataCommand.DoCanExcute = new Func((o) => true); this.SearchEnvironDataCommand = new CommandBase(); this.SearchEnvironDataCommand.DoExcute = new Action(SearchEnvironData); this.SearchEnvironDataCommand.DoCanExcute = new Func((o) => true); this.ResetSearchCommand = new CommandBase(); this.ResetSearchCommand.DoExcute = new Action(ResetSearch); this.ResetSearchCommand.DoCanExcute = new Func((o) => true); #endregion } #region RefreshData public void RefreshData(object o) { this.EndDateTime = DateTime.Now; this.StartDateTime = DateTime.Now.AddDays(-1); //设置当前页为1 PageIndex = 1; TotalSystemMonitorDataList.Clear(); SystemMonitorDataList.Clear(); string sql = String.Format("select * from juncbox_monitor where StationID = {0} ORDER by id desc limit 100", station_id); MySqlDataReader dataReader = DBHelper.ExecuteReader(sql, 1); int index = 1; while (dataReader.Read()) { BaseStationMonitorModel baseStationMonitorModel = new BaseStationMonitorModel(); baseStationMonitorModel.Index = index++; baseStationMonitorModel.RecordTime = Convert.ToDateTime(dataReader["RecordTime"]); baseStationMonitorModel.Seis1_Voltage = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis1_Voltage"].ToString()) ? "0" : dataReader["Seis1_Voltage"]); baseStationMonitorModel.Seis1_Current = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis1_Current"].ToString()) ? "0" : dataReader["Seis1_Current"]); baseStationMonitorModel.Seis2_Voltage = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis2_Voltage"].ToString()) ? "0" : dataReader["Seis2_Voltage"]); baseStationMonitorModel.Seis2_Current = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis2_Current"].ToString()) ? "0" : dataReader["Seis2_Current"]); baseStationMonitorModel.Elect_Current = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Elect_Current"].ToString()) ? "0" : dataReader["Elect_Current"]); baseStationMonitorModel.Out_Voltage12_Reserved1 = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Out_Voltage12_Reserved1"].ToString()) ? "0" : dataReader["Out_Voltage12_Reserved1"]); baseStationMonitorModel.Out_Voltage12_Reserved2 = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Out_Voltage12_Reserved2"].ToString()) ? "0" : dataReader["Out_Voltage12_Reserved2"]); baseStationMonitorModel.Reserved = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Reserved"].ToString()) ? "0" : dataReader["Reserved"]); TotalSystemMonitorDataList.Add(baseStationMonitorModel); } dataReader.Dispose(); RecordCount = index - 1; if (RecordCount <= 10) { TotalPage = 1; SystemMonitorDataList = new ObservableCollection(); SystemMonitorDataList = TotalSystemMonitorDataList; } else { TotalPage = (int)Math.Ceiling((double)RecordCount / 10); SystemMonitorDataList = new ObservableCollection(); for (int i = 0; i < 10; i++) { SystemMonitorDataList.Add(TotalSystemMonitorDataList[i]); } } SystemMonitorTotalPage = TotalPage; SystemMonitorNowPage = PageIndex; //sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-接驳盒','数据刷新','刷新接驳盒监测数据');"; //DBHelper.ExecuteNonQuery(sql, 1); } #endregion #region DownloadData public void DownloadData(object o) { string baseStationMonitorFolder = Tools.GetAppSetting("SystemMonitorDataFolder"); string savePath = CSVDownload.CreateFile(baseStationMonitorFolder, "SystemMonitorData_" + DateTime.Now.ToString("yyyyMMddhhMMss"), "csv"); string DownloadDataMsg; bool result = CSVDownload.SaveBaseStationMonitorDataToCSVFile(TotalSystemMonitorDataList, savePath); if (result) { DownloadDataMsg = "下载数据成功!"; } else { DownloadDataMsg = "下载数据失败!"; } string record = "下载接驳盒监测数据," + DownloadDataMsg; string sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-地震仪','数据下载','{record}');"; DBHelper.ExecuteNonQuery(sql, 1); } #endregion #region Page Switching public void ForwordSystemMonitorData(object o) { if (PageIndex == 1) return; try { PageIndex -= 1; SystemMonitorDataList.Clear(); for (int i = 0; i < 10; i++) { SystemMonitorDataList.Add(TotalSystemMonitorDataList[i + (PageIndex - 1) * 10]); } SystemMonitorTotalPage = TotalPage; SystemMonitorNowPage = PageIndex; } catch { TotalSystemMonitorDataList.Clear(); SystemMonitorDataList.Clear(); } } public void NextSystemMonitorData(object o) { if (PageIndex == TotalPage) return; try { PageIndex += 1; SystemMonitorDataList.Clear(); for (int i = 0; i < (PageIndex == TotalPage ? (RecordCount - (PageIndex - 1) * 10) : 10); i++) { SystemMonitorDataList.Add(TotalSystemMonitorDataList[i + (PageIndex - 1) * 10]); } SystemMonitorTotalPage = TotalPage; SystemMonitorNowPage = PageIndex; } catch { TotalSystemMonitorDataList.Clear(); SystemMonitorDataList.Clear(); } } #endregion #region SerachData public void SearchEnvironData(object o) { if (string.IsNullOrEmpty(EndDateTime.ToString()) || string.IsNullOrEmpty(StartDateTime.ToString())) { HandyControl.Controls.Dialog.Show(new TextDialog("请选择起始时间和结束时间!")); return; } if (EndDateTime < StartDateTime) { HandyControl.Controls.Dialog.Show(new TextDialog("起始时间大于结束时间,\n请重新输入!")); return; } string timeSearch = " and RecordTime BETWEEN '" + StartDateTime + "' and '" + EndDateTime + "' "; //设置当前页为1 PageIndex = 1; TotalSystemMonitorDataList.Clear(); SystemMonitorDataList.Clear(); string sql = String.Format("select * from juncbox_monitor 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()) { BaseStationMonitorModel baseStationMonitorModel = new BaseStationMonitorModel(); baseStationMonitorModel.Index = index++; baseStationMonitorModel.RecordTime = Convert.ToDateTime(dataReader["RecordTime"]); baseStationMonitorModel.Seis1_Voltage = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis1_Voltage"].ToString()) ? "0" : dataReader["Seis1_Voltage"]); baseStationMonitorModel.Seis1_Current = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis1_Current"].ToString()) ? "0" : dataReader["Seis1_Current"]); baseStationMonitorModel.Seis2_Voltage = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis2_Voltage"].ToString()) ? "0" : dataReader["Seis2_Voltage"]); baseStationMonitorModel.Seis2_Current = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Seis2_Current"].ToString()) ? "0" : dataReader["Seis2_Current"]); baseStationMonitorModel.Elect_Current = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Elect_Current"].ToString()) ? "0" : dataReader["Elect_Current"]); baseStationMonitorModel.Out_Voltage12_Reserved1 = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Out_Voltage12_Reserved1"].ToString()) ? "0" : dataReader["Out_Voltage12_Reserved1"]); baseStationMonitorModel.Out_Voltage12_Reserved2 = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Out_Voltage12_Reserved2"].ToString()) ? "0" : dataReader["Out_Voltage12_Reserved2"]); baseStationMonitorModel.Reserved = Convert.ToSingle(string.IsNullOrEmpty(dataReader["Reserved"].ToString()) ? "0" : dataReader["Reserved"]); TotalSystemMonitorDataList.Add(baseStationMonitorModel); } dataReader.Dispose(); RecordCount = index - 1; if (RecordCount <= 10) { TotalPage = 1; SystemMonitorDataList = new ObservableCollection(); SystemMonitorDataList = TotalSystemMonitorDataList; } else { TotalPage = (int)Math.Ceiling((double)RecordCount / 10); SystemMonitorDataList = new ObservableCollection(); for (int i = 0; i < 10; i++) { SystemMonitorDataList.Add(TotalSystemMonitorDataList[i]); } } SystemMonitorTotalPage = TotalPage; SystemMonitorNowPage = PageIndex; string record = "查询接驳盒监测数据历史数据,查询时间范围为:" + StartDateTime + "至" + EndDateTime + ",共查询到" + RecordCount + "条历史数据"; sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-接驳盒','数据查询','{record}');"; DBHelper.ExecuteNonQuery(sql, 1); } public void ResetSearch(object o) { RefreshData(null); } #endregion } }