using formula_manage.SQLModels;
using formula_manage.ViewModel;
using formula_manage.Windows;
using GalaSoft.MvvmLight;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using System.Xml.Linq;
namespace formula_manage.ViewModel
{
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
///
///
///
///
///
public class MainWindowViewModel : ViewModelBase
{
DataTable DissolvedataTable = new DataTable(); //建立Dissolve
DataTable STUFFdataTable = new DataTable(); //建立STUFF
DataTable MACHINEdataTable = new DataTable(); //建立Machine
DataTable RRODUCTdataTable = new DataTable(); //建立RRODUCT
DataTable RecipedataTable = new DataTable(); //建立Recipe
public static DataTable STUFFdatatemp = new DataTable(); //建立STUFF缓存
public static DataTable MACHINEdatatemp = new DataTable(); //建立Machine缓存
public string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini"; //配置文件路径
public string sys_Time; //显示系统时间
public string Sys_Time //通知UI控件参数改变
{
get { return sys_Time; }
set { sys_Time = value; OnPropertyChanged("Sys_Time"); }
}
string TEXT_SQLIP;
string TEXT_SQLNAME;
string TEXT_SQMOD;
string TEXT_SQLUSER;
string TEXT_SQLPASWOR;
string Connstr_SC;
private async void Sql_()
{
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath);//生效配置读取
TEXT_SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
TEXT_SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
TEXT_SQMOD = Configini.IniReadvalue("SQL_SERVER", "SQL3");
TEXT_SQLUSER = Configini.IniReadvalue("SQL_SERVER", "SQL4");
TEXT_SQLPASWOR = Configini.IniReadvalue("SQL_SERVER", "SQL5");
if (TEXT_SQMOD == "0") //判断连接方式
{
Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";Trusted_Connection=SSPI";
}
else
{
Connstr_SC = "server=" + TEXT_SQLIP + ";database=" + TEXT_SQLNAME + ";User ID=" + TEXT_SQLUSER + ";Password=" + TEXT_SQLPASWOR;
}
string Stuff_sql = "SELECT ProductCode ,ProductName ,ProductType ,Concentration FROM [dbo].[PRODUCT] order by ProductCode asc";//查询STUFF语句
string MAC_sql = "SELECT Name ,Capacity ,Volume FROM [dbo].[MACHINE] order by Name asc";//查询machine语句
string Dissolve_sql = "SELECT DissolveCode ,DissolveName ,MaterialType ,WeightMIN ,WeightMAX REMARK FROM [dbo].[Dissolve]";//查询语句
SqlConnection conn_SC = new SqlConnection(Connstr_SC); //实例化
try
{
await conn_SC.OpenAsync(); //打开数据连接
SqlDataAdapter Stuff_data = new SqlDataAdapter(Stuff_sql, Connstr_SC); //查询stuff
SqlDataAdapter Mac_data = new SqlDataAdapter(MAC_sql, Connstr_SC); //查询machine
SqlDataAdapter Dissolve_data = new SqlDataAdapter(Dissolve_sql, Connstr_SC); //查询Dissolve
Stuff_data.Fill(STUFFdataTable); //stuff查询结果存入缓存
Mac_data.Fill(MACHINEdataTable); //machine查询结果存入缓存
Dissolve_data.Fill(DissolvedataTable); //Dissolve_data查询结果存入缓存
conn_SC.Close(); //关闭连接
}
catch (Exception)
{
System.Windows.MessageBox.Show("请求原料信息失败,检查连接");
return;
}
}
public MainWindowViewModel()
{
CountDown();
Sql_();
stuff_Product = ToObservableCollection(STUFFdataTable); //stuff_Product表转换
mac_Machine = ToObservableCollection(MACHINEdataTable);
flow_Workflow = ToObservableCollection(DissolvedataTable);
STUFFdatatemp = STUFFdataTable;
MACHINEdatatemp = MACHINEdataTable;
}
public ObservableCollection stuff_Product { get; set; } //stuff_Product动态表实力化
public ObservableCollection mac_Machine { get; set; } //mac_Machine动态表实力化
public ObservableCollection flow_Workflow { get; set; } //Dissolve动态表实力化
public ObservableCollection ToObservableCollection(DataTable dt) where T : class, new() //DataTable FOR ObservableCollection转换器
{
Type t = typeof(T);
PropertyInfo[] propertys = t.GetProperties();
ObservableCollection lst = new ObservableCollection();
string typeName = string.Empty;
foreach (DataRow dr in dt.Rows)
{
T entity = new T();
foreach (PropertyInfo pi in propertys)
{
typeName = pi.Name;
if (dt.Columns.Contains(typeName))
{
if (!pi.CanWrite) continue;
object value = dr[typeName];
if (value == DBNull.Value) continue;
if (pi.PropertyType == typeof(string))
{
pi.SetValue(entity, value.ToString(), null);
}
else if (pi.PropertyType == typeof(int) || pi.PropertyType == typeof(int?))
{
pi.SetValue(entity, int.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(DateTime?) || pi.PropertyType == typeof(DateTime))
{
pi.SetValue(entity, DateTime.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(float))
{
pi.SetValue(entity, float.Parse(value.ToString()), null);
}
else if (pi.PropertyType == typeof(double))
{
pi.SetValue(entity, double.Parse(value.ToString()), null);
}
else
{
pi.SetValue(entity, value, null);
}
}
}
lst.Add(entity);
}
return lst;
} //DataTable FOR ObservableCollection转换器
///
/// 循环事件设定
///
public void CountDown()
{
DispatcherTimer timer = new DispatcherTimer//初始化循环,每0.5秒调用一次Tick_Event
{
Interval = TimeSpan.FromSeconds(0.5)
};
timer.Tick += Tick_Event;
timer.Start();
//设置定时器
// disTimer.Tick += new EventHandler(DisTimer_Tick);//每一秒执行的方法
// disTimer.Interval = new TimeSpan(10000000); //时间间隔为一秒。
// disTimer.Start();//计时开始
}
void Tick_Event(object sender, EventArgs e)//Tick_Event周期执行事件
{
Sys_Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
public class Product //stuff_Product
{
public string ProductCode { get; set; }
public string ProductName { get; set; }
public int ProductType { get; set; }
public int Concentration { get; set; }
public override string ToString()
{
return ProductCode;
}
}
public class Machine //mac
{
public string Name { get; set; }
public float Volume { get; set; }
public float Capacity { get; set; }
public override string ToString()
{
return Name;
}
}
public class Workflow //Dissolve
{
public string DissolveName { get; set; }
public int MaterialType { get; set; }
public int WeightMIN { get; set; }
public int WeightMAX { get; set; }
public override string ToString()
{
return DissolveName;
}
}
}