2024-10-11 09:26:14 +00:00
using FujianEarthquake.Common ;
using FujianEarthquake.DataAccess ;
using FujianEarthquake.Models ;
using FujianEarthquake.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 ;
namespace FujianEarthquake.ViewModels
{
public class JunctionBoxStatusDataViewModel : 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 ( ) ; }
}
2024-10-12 09:37:29 +00:00
private int junctionBoxStatusTotalPage ;
2024-10-11 09:26:14 +00:00
2024-10-12 09:37:29 +00:00
public int JunctionBoxStatusTotalPage
2024-10-11 09:26:14 +00:00
{
2024-10-12 09:37:29 +00:00
get { return junctionBoxStatusTotalPage ; }
set { junctionBoxStatusTotalPage = value ; this . DoNotify ( ) ; }
}
private int junctionBoxStatusNowPage ;
public int JunctionBoxStatusNowPage
{
get { return junctionBoxStatusNowPage ; }
set { junctionBoxStatusNowPage = value ; this . DoNotify ( ) ; }
2024-10-11 09:26:14 +00:00
}
#endregion
private ObservableCollection < JunctionBoxMonitorModel > junctionBoxStatusDataList = new ObservableCollection < JunctionBoxMonitorModel > ( ) ;
public ObservableCollection < JunctionBoxMonitorModel > JunctionBoxStatusDataList
{
get { return junctionBoxStatusDataList ; }
set { junctionBoxStatusDataList = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < JunctionBoxMonitorModel > totalJunctionBoxStatusDataList = new ObservableCollection < JunctionBoxMonitorModel > ( ) ;
public ObservableCollection < JunctionBoxMonitorModel > TotalJunctionBoxStatusDataList
{
get { return totalJunctionBoxStatusDataList ; }
set { totalJunctionBoxStatusDataList = value ; this . DoNotify ( ) ; }
}
public static DialogViewModel vm ;
#region CommandBase Define
public CommandBase ForwordJunctionBoxStatusDataCommand { get ; set ; } //读取日志信息
public CommandBase NextJunctionBoxStatusDataCommand { get ; set ; } //读取日志信息
public CommandBase RefreshDataCommand { get ; set ; }
public CommandBase DownloadDataCommand { get ; set ; }
2024-10-12 09:37:29 +00:00
2024-11-01 08:05:11 +00:00
public CommandBase SearchJunctionBoxStatusDataCommand { get ; set ; }
2024-10-12 09:37:29 +00:00
public CommandBase ResetSearchCommand { get ; set ; }
2024-10-11 09:26:14 +00:00
#endregion
int station_id = 1 ;
public JunctionBoxStatusDataViewModel ( 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 . ForwordJunctionBoxStatusDataCommand = new CommandBase ( ) ;
this . ForwordJunctionBoxStatusDataCommand . DoExcute = new Action < object > ( ForwordJunctionBoxStatusData ) ;
this . ForwordJunctionBoxStatusDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . NextJunctionBoxStatusDataCommand = new CommandBase ( ) ;
this . NextJunctionBoxStatusDataCommand . DoExcute = new Action < object > ( NextJunctionBoxStatusData ) ;
this . NextJunctionBoxStatusDataCommand . 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 ) ;
2024-11-01 08:05:11 +00:00
this . SearchJunctionBoxStatusDataCommand = new CommandBase ( ) ;
this . SearchJunctionBoxStatusDataCommand . DoExcute = new Action < object > ( SearchJunctionBoxStatusData ) ;
this . SearchJunctionBoxStatusDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
2024-10-12 09:37:29 +00:00
this . ResetSearchCommand = new CommandBase ( ) ;
this . ResetSearchCommand . DoExcute = new Action < object > ( ResetSearch ) ;
this . ResetSearchCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
#endregion
2024-10-11 09:26:14 +00:00
}
#region RefreshData
public void RefreshData ( object o )
{
2024-10-12 09:37:29 +00:00
this . EndDateTime = DateTime . Now ;
this . StartDateTime = DateTime . Now . AddDays ( - 1 ) ;
2024-10-11 09:26:14 +00:00
//设置当前页为1
PageIndex = 1 ;
TotalJunctionBoxStatusDataList . Clear ( ) ;
JunctionBoxStatusDataList . Clear ( ) ;
2024-10-12 09:37:29 +00:00
string sql = String . Format ( "select * from underjuncbox_monitor where StationID = {0} ORDER by id desc limit 100" , station_id ) ;
2024-10-11 09:26:14 +00:00
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
JunctionBoxMonitorModel junctionBoxMonitorModel = new JunctionBoxMonitorModel ( ) ;
junctionBoxMonitorModel . Index = index + + ;
junctionBoxMonitorModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
junctionBoxMonitorModel . Seis1_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Voltage" ] ) ;
junctionBoxMonitorModel . Seis1_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Current" ] ) ;
junctionBoxMonitorModel . Seis2_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Voltage" ] ) ;
junctionBoxMonitorModel . Seis2_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Current" ] ) ;
junctionBoxMonitorModel . Video_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Video_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Video_Voltage" ] ) ;
junctionBoxMonitorModel . Video_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Video_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Video_Current" ] ) ;
junctionBoxMonitorModel . Out_Voltage12_Reserved2 = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Out_Voltage12_Reserved2" ] . ToString ( ) ) ? "0" : dataReader [ "Out_Voltage12_Reserved2" ] ) ;
TotalJunctionBoxStatusDataList . Add ( junctionBoxMonitorModel ) ;
}
dataReader . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
JunctionBoxStatusDataList = new ObservableCollection < JunctionBoxMonitorModel > ( ) ;
JunctionBoxStatusDataList = TotalJunctionBoxStatusDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
JunctionBoxStatusDataList = new ObservableCollection < JunctionBoxMonitorModel > ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxStatusDataList . Add ( TotalJunctionBoxStatusDataList [ i ] ) ;
}
}
2024-10-12 09:37:29 +00:00
JunctionBoxStatusTotalPage = TotalPage ;
JunctionBoxStatusNowPage = PageIndex ;
2024-10-11 09:26:14 +00:00
2024-10-12 09:37:29 +00:00
//sql = $"insert into log_record(StationID,RecordTime,Device_Name,Operation_Type,Record) values('{station_id}','{DateTime.Now}','海底地震监测基站-接驳盒','数据刷新','刷新接驳盒监测数据');";
//DBHelper.ExecuteNonQuery(sql, 1);
2024-10-11 09:26:14 +00:00
}
#endregion
#region DownloadData
2024-10-12 09:37:29 +00:00
2024-10-11 09:26:14 +00:00
public void DownloadData ( object o )
{
string junctionBoxStatusFolder = Tools . GetAppSetting ( "JunctionBoxFolder" ) ;
string savePath = CSVDownload . CreateFile ( junctionBoxStatusFolder , "JunctionBoxStatusData_" + DateTime . Now . ToString ( "yyyyMMddhhMMss" ) , "csv" ) ;
2024-10-12 09:37:29 +00:00
string DownloadDataMsg = "" ;
2024-10-11 09:26:14 +00:00
bool result = CSVDownload . SaveJunctionBoxStatusDataToCSVFile ( TotalJunctionBoxStatusDataList , savePath ) ;
if ( result )
{
DownloadDataMsg = "下载数据成功!" ;
}
else
{
DownloadDataMsg = "下载数据失败!" ;
}
2024-10-12 09:37:29 +00:00
string record = "下载接驳盒状态数据," + DownloadDataMsg ;
2024-10-11 09:26:14 +00:00
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 ForwordJunctionBoxStatusData ( object o )
{
if ( PageIndex = = 1 )
return ;
try
{
PageIndex - = 1 ;
JunctionBoxStatusDataList . Clear ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxStatusDataList . Add ( TotalJunctionBoxStatusDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
2024-10-12 09:37:29 +00:00
JunctionBoxStatusTotalPage = TotalPage ;
JunctionBoxStatusNowPage = PageIndex ;
2024-10-11 09:26:14 +00:00
}
catch
{
TotalJunctionBoxStatusDataList . Clear ( ) ;
JunctionBoxStatusDataList . Clear ( ) ;
}
}
public void NextJunctionBoxStatusData ( object o )
{
if ( PageIndex = = TotalPage )
return ;
try
{
PageIndex + = 1 ;
JunctionBoxStatusDataList . Clear ( ) ;
for ( int i = 0 ; i < ( PageIndex = = TotalPage ? ( RecordCount - ( PageIndex - 1 ) * 10 ) : 10 ) ; i + + )
{
JunctionBoxStatusDataList . Add ( TotalJunctionBoxStatusDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
2024-10-12 09:37:29 +00:00
JunctionBoxStatusTotalPage = TotalPage ;
JunctionBoxStatusNowPage = PageIndex ;
2024-10-11 09:26:14 +00:00
}
catch
{
TotalJunctionBoxStatusDataList . Clear ( ) ;
JunctionBoxStatusDataList . Clear ( ) ;
}
}
2024-10-12 09:37:29 +00:00
#endregion
#region SerachData
2024-11-01 08:05:11 +00:00
public void SearchJunctionBoxStatusData ( object o )
2024-10-12 09:37:29 +00:00
{
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 ;
TotalJunctionBoxStatusDataList . Clear ( ) ;
JunctionBoxStatusDataList . Clear ( ) ;
string sql = String . Format ( "select * from underjuncbox_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 ( ) )
{
JunctionBoxMonitorModel junctionBoxMonitorModel = new JunctionBoxMonitorModel ( ) ;
junctionBoxMonitorModel . Index = index + + ;
junctionBoxMonitorModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
junctionBoxMonitorModel . Seis1_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Voltage" ] ) ;
junctionBoxMonitorModel . Seis1_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis1_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis1_Current" ] ) ;
junctionBoxMonitorModel . Seis2_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Voltage" ] ) ;
junctionBoxMonitorModel . Seis2_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2_Current" ] ) ;
junctionBoxMonitorModel . Video_Voltage = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Video_Voltage" ] . ToString ( ) ) ? "0" : dataReader [ "Video_Voltage" ] ) ;
junctionBoxMonitorModel . Video_Current = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Video_Current" ] . ToString ( ) ) ? "0" : dataReader [ "Video_Current" ] ) ;
junctionBoxMonitorModel . Out_Voltage12_Reserved2 = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Out_Voltage12_Reserved2" ] . ToString ( ) ) ? "0" : dataReader [ "Out_Voltage12_Reserved2" ] ) ;
TotalJunctionBoxStatusDataList . Add ( junctionBoxMonitorModel ) ;
}
dataReader . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
JunctionBoxStatusDataList = new ObservableCollection < JunctionBoxMonitorModel > ( ) ;
JunctionBoxStatusDataList = TotalJunctionBoxStatusDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
JunctionBoxStatusDataList = new ObservableCollection < JunctionBoxMonitorModel > ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxStatusDataList . Add ( TotalJunctionBoxStatusDataList [ i ] ) ;
}
}
JunctionBoxStatusTotalPage = TotalPage ;
JunctionBoxStatusNowPage = 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
2024-10-11 09:26:14 +00:00
}
}