using formula_manage.UserClass; using System; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Reflection; using System.Windows.Threading; 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 cdk_time; //注册到期时间 public string cdk_page = "Hidden"; //注册到期页面状态 public string cdk_pageT; //注册到期页面关闭时间 int cdk_pageT_T = -1;////注册到期页面关闭时间计算 public string Sys_Time //通知UI控件参数改变 { get { return sys_Time; } set { sys_Time = value; OnPropertyChanged("Sys_Time"); } } public string Cdk_time //通知UI控件参数改变 { get { return cdk_time; } set { cdk_time = value; OnPropertyChanged("Cdk_time"); } } public string Cdk_page //通知UI控件参数改变 { get { return cdk_page; } set { cdk_page = value; OnPropertyChanged("Cdk_page"); } } public string Cdk_pageT //通知UI控件参数改变 { get { return cdk_pageT; } set { cdk_pageT = value; OnPropertyChanged("Cdk_pageT"); } } string TEXT_SQLIP; string TEXT_SQLNAME; string TEXT_SQMOD; string TEXT_SQLUSER; string TEXT_SQLPASWOR; string Connstr_SC; string SN_ID; string SN_KEY; private void CDkey_() { try { string id_1 = SN_ID.Substring(5, 1) + SN_ID.Substring(0, 1) + SN_ID.Substring(4, 1) + SN_ID.Substring(1, 1) + SN_ID.Substring(3, 1) + SN_ID.Substring(2, 1); if ((id_1 == SN_KEY.Substring(0, 6)) && (SN_KEY.Length == 16)) { if (CRCcheck16.ToCRC16(CRCcheck16.StringToHexByte(SN_KEY.Substring(0, 12)), true) == SN_KEY.Substring(12, 4)) //校验key的crc校验值 { string Y = (2255 - StrToInt.To16Convert10(SN_KEY.Substring(6, 2))).ToString(); string M = (255 - StrToInt.To16Convert10(SN_KEY.Substring(8, 2))).ToString(); string D = (255 - StrToInt.To16Convert10(SN_KEY.Substring(10, 2))).ToString(); string YY = DateTime.Now.ToString("yyyy"); string MM = DateTime.Now.ToString("MM"); string DD = DateTime.Now.ToString("dd"); Cdk_time = "注册到期时间: " + Y + "/" + M + "/" + D; if (string.Compare(Y, YY) < 0) { if (string.Compare(M, MM) < 0) { if (string.Compare(D, DD) < 0) { Cdk_page = "Visible"; //cdk_pageT_T = -1; //防止触发倒计时 } else if ((int.Parse(D) - int.Parse(DD)) <= 10)//提前三天提醒 { Cdk_page = "Visible"; cdk_pageT_T = 180; //页面关闭倒计时30秒 } } } } else { Cdk_page = "Visible"; Cdk_time = "许可证安全校验错误请重新激活"; } } else { Cdk_page = "Visible"; Cdk_time = "计算机信息变更当前许可证失效"; } } catch (Exception) { Cdk_page = "Visible"; Cdk_time = "许可证安全校验错误请重新激活"; } } 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"); SN_ID = Configini.IniReadvalue("SN", "SN2"); SN_KEY = Configini.IniReadvalue("SN", "SN1"); 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("请求信息失败"); UserClass.LogGing.LogGingDATA("DISPENLINK = ConnectFailed"); return; } } public MainWindowViewModel() { CountDown(); Sql_(); CDkey_(); 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 timer1s = new DispatcherTimer//初始化循环,每0.5秒调用一次Tick { Interval = TimeSpan.FromSeconds(1) }; timer1s.Tick += Tick_Event_1S; timer1s.Start(); DispatcherTimer timer1h = new DispatcherTimer//初始化循环,每1h调用一次Tick { Interval = TimeSpan.FromSeconds(3600) }; timer1h.Tick += Tick_Event_1h; timer1h.Start(); //设置定时器 // disTimer.Tick += new EventHandler(DisTimer_Tick);//每一秒执行的方法 // disTimer.Interval = new TimeSpan(10000000); //时间间隔为一秒。 // disTimer.Start();//计时开始 } void Tick_Event_1S(object sender, EventArgs e)//Tick_Event周期执行事件 { Sys_Time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); if (cdk_pageT_T > 0) //提示注册画面关闭倒计时 { cdk_pageT_T--; Cdk_pageT = cdk_pageT_T.ToString(); if (cdk_pageT_T == 1) Cdk_page = "Hidden"; } } void Tick_Event_1h(object sender, EventArgs e)//Tick_Event周期执行事件 { CDkey_(); } } 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; } } }