2024-10-11 09:26:14 +00:00
using FujianEarthquake.Common ;
using FujianEarthquake.DataAccess ;
using FujianEarthquake.Views.UserControls ;
using FujianEarthquake.Models ;
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 ;
using System.Windows.Media ;
using System.Windows.Threading ;
namespace FujianEarthquake.ViewModels
{
public class JunctionBoxEnvironDataViewModel : 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 junctionBoxEnvironTotalPage ;
public int JunctionBoxEnvironTotalPage
{
get { return junctionBoxEnvironTotalPage ; }
set { junctionBoxEnvironTotalPage = value ; this . DoNotify ( ) ; }
}
private int junctionBoxEnvironNowPage ;
public int JunctionBoxEnvironNowPage
{
get { return junctionBoxEnvironNowPage ; }
set { junctionBoxEnvironNowPage = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < JunctionBoxEnvironModel > junctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
public ObservableCollection < JunctionBoxEnvironModel > JunctionBoxEnvironDataList
{
get { return junctionBoxEnvironDataList ; }
set { junctionBoxEnvironDataList = value ; this . DoNotify ( ) ; }
}
private ObservableCollection < JunctionBoxEnvironModel > totalJunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
public ObservableCollection < JunctionBoxEnvironModel > TotalJunctionBoxEnvironDataList
{
get { return totalJunctionBoxEnvironDataList ; }
set { totalJunctionBoxEnvironDataList = value ; this . DoNotify ( ) ; }
}
#endregion
#region Command Define
public CommandBase ForwordJunctionBoxEnvironCommand { get ; set ; }
public CommandBase NextJunctionBoxEnvironCommand { 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 static DialogViewModel vm ;
public JunctionBoxEnvironDataViewModel ( 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 . ForwordJunctionBoxEnvironCommand = new CommandBase ( ) ;
this . ForwordJunctionBoxEnvironCommand . DoExcute = new Action < object > ( ForwordJunctionBoxEnviron ) ;
this . ForwordJunctionBoxEnvironCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . NextJunctionBoxEnvironCommand = new CommandBase ( ) ;
this . NextJunctionBoxEnvironCommand . DoExcute = new Action < object > ( NextJunctionBoxEnviron ) ;
this . NextJunctionBoxEnvironCommand . 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 ) ;
this . SearchEnvironDataCommand = new CommandBase ( ) ;
this . SearchEnvironDataCommand . DoExcute = new Action < object > ( SearchEnvironData ) ;
this . SearchEnvironDataCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
this . ResetSearchCommand = new CommandBase ( ) ;
this . ResetSearchCommand . DoExcute = new Action < object > ( ResetSearch ) ;
this . ResetSearchCommand . DoCanExcute = new Func < object , bool > ( ( o ) = > true ) ;
#endregion
}
#region Page Switching
public void ForwordJunctionBoxEnviron ( object o )
{
if ( PageIndex = = 1 )
return ;
try
{
PageIndex - = 1 ;
JunctionBoxEnvironDataList . Clear ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
JunctionBoxEnvironTotalPage = TotalPage ;
JunctionBoxEnvironNowPage = PageIndex ;
}
catch
{
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
}
}
public void NextJunctionBoxEnviron ( object o )
{
if ( PageIndex = = TotalPage )
return ;
try
{
PageIndex + = 1 ;
JunctionBoxEnvironDataList . Clear ( ) ;
for ( int i = 0 ; i < ( PageIndex = = TotalPage ? ( RecordCount - ( PageIndex - 1 ) * 10 ) : 10 ) ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i + ( PageIndex - 1 ) * 10 ] ) ;
}
JunctionBoxEnvironTotalPage = TotalPage ;
JunctionBoxEnvironNowPage = PageIndex ;
}
catch
{
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
}
}
#endregion
#region RefreshData
public void RefreshData ( object o )
{
this . EndDateTime = DateTime . Now ;
this . StartDateTime = DateTime . Now . AddDays ( - 1 ) ;
//设置当前页为1
PageIndex = 1 ;
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
string sql = String . Format ( "select * from underjuncbox_env where StationID = {0} ORDER by id desc limit 100" , station_id ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
string sqlSub = String . Format ( "select * from underjuncbox_cavity_state where StationID = {0} ORDER by id desc limit 100" , station_id ) ;
MySqlDataReader dataReaderSub = DBHelper . ExecuteReader ( sqlSub , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
JunctionBoxEnvironModel junctionBoxEnvironModel = new JunctionBoxEnvironModel ( ) ;
junctionBoxEnvironModel . Index = index + + ;
junctionBoxEnvironModel . DataTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ;
junctionBoxEnvironModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
junctionBoxEnvironModel . Temperature = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Temperature" ] . ToString ( ) ) ? "0" : dataReader [ "Temperature" ] ) ;
junctionBoxEnvironModel . Humidity = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Humidity" ] . ToString ( ) ) ? "0" : dataReader [ "Humidity" ] ) ;
junctionBoxEnvironModel . AttitudeX = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeX" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeX" ] ) ;
junctionBoxEnvironModel . AttitudeY = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeY" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeY" ] ) ;
junctionBoxEnvironModel . AttitudeZ = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeZ" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeZ" ] ) ;
2024-11-01 08:05:11 +00:00
junctionBoxEnvironModel . Seis2_AttitudeX = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2AttitudeX" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2AttitudeX" ] ) ;
junctionBoxEnvironModel . Seis2_AttitudeY = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2AttitudeY" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2AttitudeY" ] ) ;
junctionBoxEnvironModel . Seis2_AttitudeZ = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2AttitudeZ" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2AttitudeZ" ] ) ;
2024-10-11 09:26:14 +00:00
if ( dataReaderSub . Read ( ) )
{
int leakState = Convert . ToInt32 ( string . IsNullOrEmpty ( dataReaderSub [ "Leakage" ] . ToString ( ) ) ? "2" : dataReaderSub [ "Leakage" ] ) ;
if ( leakState = = 1 )
{
junctionBoxEnvironModel . Leakage = "漏水" ;
}
else if ( leakState = = 0 )
{
junctionBoxEnvironModel . Leakage = "未漏水" ;
}
else
{
junctionBoxEnvironModel . Leakage = "未知" ;
}
}
else
{
junctionBoxEnvironModel . Leakage = "未知" ;
}
TotalJunctionBoxEnvironDataList . Add ( junctionBoxEnvironModel ) ;
}
dataReader . Dispose ( ) ;
dataReaderSub . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
JunctionBoxEnvironDataList = TotalJunctionBoxEnvironDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i ] ) ;
}
}
JunctionBoxEnvironTotalPage = TotalPage ;
JunctionBoxEnvironNowPage = PageIndex ;
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
public void DownloadData ( object o )
{
string DownloadDataMsg = "" ;
string junctionBoxEnvironFolder = Tools . GetAppSetting ( "JunctionBoxFolder" ) ;
string savePath = CSVDownload . CreateFile ( junctionBoxEnvironFolder , "JunctionBoxEnvironData_" + DateTime . Now . ToString ( "yyyyMMddhhMMss" ) , "csv" ) ;
bool result = CSVDownload . SaveJunctionBoxEnvironDataToCSVFile ( TotalJunctionBoxEnvironDataList , savePath ) ;
if ( result )
{
DownloadDataMsg = "下载数据成功!" ;
HandyControl . Controls . Dialog . Show ( new TextDialog ( "接驳盒环境数据下载成功!\n路径为: " + junctionBoxEnvironFolder ) ) ;
}
else
{
DownloadDataMsg = "下载数据失败!" ;
HandyControl . Controls . Dialog . Show ( new TextDialog ( "接驳盒环境数据下载失败!" ) ) ;
}
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 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 ;
TotalJunctionBoxEnvironDataList . Clear ( ) ;
JunctionBoxEnvironDataList . Clear ( ) ;
string sql = String . Format ( "select * from underjuncbox_env where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReader = DBHelper . ExecuteReader ( sql , 1 ) ;
string sqlSub = String . Format ( "select * from underjuncbox_cavity_state where StationID = {0} {1} ORDER by id desc limit 100" , station_id , timeSearch ) ;
MySqlDataReader dataReaderSub = DBHelper . ExecuteReader ( sqlSub , 1 ) ;
int index = 1 ;
while ( dataReader . Read ( ) )
{
JunctionBoxEnvironModel JunctionBoxEnvironModel = new JunctionBoxEnvironModel ( ) ;
JunctionBoxEnvironModel . Index = index + + ;
JunctionBoxEnvironModel . DataTime = Convert . ToDateTime ( dataReader [ "DataTime" ] ) ;
JunctionBoxEnvironModel . RecordTime = Convert . ToDateTime ( dataReader [ "RecordTime" ] ) ;
JunctionBoxEnvironModel . Temperature = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Temperature" ] . ToString ( ) ) ? "0" : dataReader [ "Temperature" ] ) ;
JunctionBoxEnvironModel . Humidity = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Humidity" ] . ToString ( ) ) ? "0" : dataReader [ "Humidity" ] ) ;
JunctionBoxEnvironModel . AttitudeX = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeX" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeX" ] ) ;
JunctionBoxEnvironModel . AttitudeY = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeY" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeY" ] ) ;
JunctionBoxEnvironModel . AttitudeZ = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "AttitudeZ" ] . ToString ( ) ) ? "0" : dataReader [ "AttitudeZ" ] ) ;
2024-11-01 08:05:11 +00:00
JunctionBoxEnvironModel . Seis2_AttitudeX = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2AttitudeX" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2AttitudeX" ] ) ;
JunctionBoxEnvironModel . Seis2_AttitudeY = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2AttitudeY" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2AttitudeY" ] ) ;
JunctionBoxEnvironModel . Seis2_AttitudeZ = Convert . ToSingle ( string . IsNullOrEmpty ( dataReader [ "Seis2AttitudeZ" ] . ToString ( ) ) ? "0" : dataReader [ "Seis2AttitudeZ" ] ) ;
2024-10-11 09:26:14 +00:00
if ( dataReaderSub . Read ( ) )
{
int leakState = Convert . ToInt32 ( string . IsNullOrEmpty ( dataReaderSub [ "Leakage" ] . ToString ( ) ) ? "2" : dataReaderSub [ "Leakage" ] ) ;
if ( leakState = = 1 )
{
JunctionBoxEnvironModel . Leakage = "漏水" ;
}
else if ( leakState = = 0 )
{
JunctionBoxEnvironModel . Leakage = "未漏水" ;
}
else
{
JunctionBoxEnvironModel . Leakage = "未知" ;
}
}
else
{
JunctionBoxEnvironModel . Leakage = "未知" ;
}
TotalJunctionBoxEnvironDataList . Add ( JunctionBoxEnvironModel ) ;
}
dataReader . Dispose ( ) ;
dataReaderSub . Dispose ( ) ;
RecordCount = index - 1 ;
if ( RecordCount < = 10 )
{
TotalPage = 1 ;
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
JunctionBoxEnvironDataList = TotalJunctionBoxEnvironDataList ;
}
else
{
TotalPage = ( int ) Math . Ceiling ( ( double ) RecordCount / 10 ) ;
JunctionBoxEnvironDataList = new ObservableCollection < JunctionBoxEnvironModel > ( ) ;
for ( int i = 0 ; i < 10 ; i + + )
{
JunctionBoxEnvironDataList . Add ( TotalJunctionBoxEnvironDataList [ i ] ) ;
}
}
JunctionBoxEnvironTotalPage = TotalPage ;
JunctionBoxEnvironNowPage = 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
}
}