using formula_manage.Windows;
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.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using formula_manage.ViewModel;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
using static System.Net.Mime.MediaTypeNames;
using formula_manage.UserClass;
using System.Drawing;
using System.Diagnostics;
using System.Printing;
using System.Drawing.Printing;
using static System.Drawing.Printing.PrinterSettings;
using formula_manage.View;
using FastReport.DevComponents.AdvTree;
namespace formula_manage.Windows
{
///
/// Machine.xaml 的交互逻辑
///
public partial class RECIPE : Window
{
// 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 string INIPath = Convert.ToString(System.AppDomain.CurrentDomain.BaseDirectory) + "formula.ini"; //配置文件路径
string TEXT_SQLIP;
string TEXT_SQLNAME;
string TEXT_SQMOD;
string TEXT_SQLUSER;
string TEXT_SQLPASWOR;
string Connstr_SC;
int ID_N = 1;
public RECIPE()
{
DataContext = new RECIPEViewModel();
WindowStartupLocation = WindowStartupLocation.CenterScreen;
InitializeComponent();
RRODUCTdataTable.Columns.Add("ID", typeof(int));
RRODUCTdataTable.Columns.Add("DYELOT", typeof(string));
RRODUCTdataTable.Columns.Add("ReDye", typeof(int));
RRODUCTdataTable.Columns.Add("STEP", typeof(int));
RRODUCTdataTable.Columns.Add("PRODUCT_CODE", typeof(string));
RRODUCTdataTable.Columns.Add("CONC", typeof(float));
RRODUCTdataTable.Columns.Add("SHIFT", typeof(string));
RRODUCTdataTable.Columns.Add("PRODUCT_NAME", typeof(string));
RRODUCTdataTable.Columns.Add("TARGET_WT", typeof(float));
RRODUCTdataTable.Columns.Add("UNIT", typeof(string));
RRODUCTdataTable.Columns.Add("Process", typeof(string));
RRODUCTdataTable.Columns.Add("REMARK", typeof(string));
DataRow row = RRODUCTdataTable.NewRow(); //ID列
row["ID"] = ID_N;
row["UNIT"] = "g";
RRODUCTdataTable.Rows.Add(row);
Grid_RRODUCT.ItemsSource = RRODUCTdataTable.DefaultView;
}
private void RECIPE_Loaded(object sender, RoutedEventArgs e)//打开页面执行
{
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath);//生效配置读取
this.DatagridRECIPE.LoadingRow += new EventHandler(this.DataGridEquipment_LoadingRow);//生成序列号
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;
}
GridSql();
}
///
/// 生成序列号的方法
///
private void DataGridEquipment_LoadingRow(object sender, DataGridRowEventArgs e)
{
e.Row.Header = e.Row.GetIndex() + 1;
}
///
/// 查询数据库的方法
///
private async void GridSql()
{
string Dissolve_sql = "SELECT DissolveCode ,DissolveName ,REMARK ,MaterialType ,WeightMIN ,WeightMAX FROM [Dispensing].[dbo].[Dissolve]";//查询语句
SqlConnection conn_SC = new SqlConnection(Connstr_SC); //实例化
try
{
await conn_SC.OpenAsync(); //打开数据连接
SqlDataAdapter Stuff_data = new SqlDataAdapter(Dissolve_sql, Connstr_SC); //查询
DataTable dataTable = new DataTable(); //建立缓存
Stuff_data.Fill(dataTable); //查询结果存入缓存
conn_SC.Close(); //关闭连接
DatagridRECIPE.ItemsSource = dataTable.DefaultView; //数据加入表格
}
catch (Exception)
{
System.Windows.MessageBox.Show("请求信息失败,检查连接");
return;
}
}
private void RECIPE_MouseDoubleClick(object sender, MouseButtonEventArgs e)//数据表双击事件
{
int rownum = DatagridRECIPE.SelectedIndex;//获取鼠标选中行并定义变量
if (rownum != -1)//判断鼠标定位是否有效
{
}
}
private void Tb_KeyFloating(object sender, TextCompositionEventArgs e)//输入事件
{
//Regex re = new Regex("[^0-9.-]+");
Regex re = new Regex(@"^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$");// 非负浮点数
e.Handled = !re.IsMatch(e.Text);
}
private async void Button_Preservation(object sender, RoutedEventArgs e)//保存按钮事件
{
Regex re_number = new Regex(@"^[0-9]+(.[0-9]{1,2})?$");//校验用正则表达式有1~2位小数的正实数
Regex re_char = new Regex(@"^[A-Za-z0-9\s@()()/+!!_-]+$");//校验用正则表达式由数字,26个英文字母,空白字符和@()()/+!!_-组成的字符串
// string Dissolve_sql;
// int int_ion = 0;
}
private async void Button_Delete(object sender, RoutedEventArgs e)//删除按钮事件
{
string Mac_name = "是否删除【" + this.Code.Text + "】";//获取信息并拼接提示字符串
MessageBoxResult mac_name = System.Windows.MessageBox.Show(Mac_name, "提示", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.Yes);//弹窗提示是否删除目标
if (mac_name == MessageBoxResult.Yes)//判断是否删除原料
{
string Stuff_sql = string.Format("DELETE FROM [dbo].[Dissolve] WHERE Name ='" + Code.Text.ToString() + "'");
SqlConnection conn_SC = new SqlConnection(Connstr_SC); //实例化
try
{
await conn_SC.OpenAsync(); //打开数据连接
SqlCommand INSERT_cmd = new SqlCommand(Stuff_sql, conn_SC);
int sql_in = INSERT_cmd.ExecuteNonQuery(); //执行语句
conn_SC.Close(); //关闭连接
if (sql_in == 0)
{
System.Windows.MessageBox.Show("ERR.C0110-2:删除失败", "错误");//判断执行是否成功
}
else
{
GridSql();
}
}
catch (Exception)
{
System.Windows.MessageBox.Show("请求失败,检查连接");
return;
}
}
}
string Code_;
string Name_;
string Type_;
string Conc_;
double Weight_ = 0;
private void CP_PRODUCT_CODE_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)//原料代码输入事件
{
//double ang;
if (e.Key == Key.Enter)
{
System.Windows.Controls.ComboBox curComboBox = sender as System.Windows.Controls.ComboBox;
int grid_row = Grid_RRODUCT.SelectedIndex; //获取当前行
Code_ = curComboBox.Text;
DataRow[] r = MainWindowViewModel.STUFFdatatemp.Select("ProductCode ='" + Code_ + "'"); //查询判断原料代码是否有效
if (!r.Any()) //检查原料信息
{
System.Windows.MessageBox.Show("原料不存在");
curComboBox.Text = "";
return;
}
DataRow[] c = RRODUCTdataTable.Select("PRODUCT_CODE ='" + Code_ + "'");//查询判断原料代码是否重复
if (c.Any())
{
System.Windows.MessageBox.Show("重复原料");
return;
}
Name_ = r[0].ItemArray[1].ToString();
Type_ = r[0].ItemArray[2].ToString();
Conc_ = r[0].ItemArray[3].ToString();
if ((Type_ == "0") || (Type_ == "3"))
{
Type_ = "%";
}
if ((Type_ == "1") || (Type_ == "2"))
{
Type_ = "g/L";
}
if (grid_row == -1) grid_row = 0;
DataRow row = RRODUCTdataTable.Rows[grid_row]; //ID列
row.BeginEdit();
row["PRODUCT_CODE"] = Code_;
row["PRODUCT_NAME"] = Name_;
row["SHIFT"] = Type_;
row.EndEdit();
if (grid_row == (ID_N - 1))
{
PressKey.PressKeys(Keys.Right, false);
PressKey.PressKeys(Keys.Right, true);
PressKey.PressKeys(Keys.Tab, false);
PressKey.PressKeys(Keys.Tab, true);
}
else
{
PressKey.PressKeys(Keys.Right, false);
PressKey.PressKeys(Keys.Right, true);
PressKey.PressKeys(Keys.Up, false);
PressKey.PressKeys(Keys.Up, true);
PressKey.PressKeys(Keys.Tab, false);
PressKey.PressKeys(Keys.Tab, true);
}
}
DataGridHelper.SetRealTimeCommit(Grid_RRODUCT, true); //实时更新datagrid
}
private void CP_CONC_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)//目标饱和度输入事件
{
double ang;
double conc = 0;
System.Windows.Controls.TextBox curTextBox = sender as System.Windows.Controls.TextBox;
int grid_row = Grid_RRODUCT.SelectedIndex; //获取当前行
if (e.Key == Key.Enter)
{
if (!double.TryParse(curTextBox.Text, out ang))
{
System.Windows.MessageBox.Show("错误,重新输入");
curTextBox.Text = "";
return;
}
else
{
if (curTextBox.Text != "") conc = double.Parse(curTextBox.Text); //转换后判断数值输入是否异常
if (conc > 100)
{
System.Windows.MessageBox.Show("比例错误");
curTextBox.Text = "";
return;
}
else
{
double Weight_t = Weight_ * conc;
DataRow row = RRODUCTdataTable.Rows[grid_row]; //ID列
row.BeginEdit();
row["CONC"] = string.Format("{0:N6}", conc);
row["TARGET_WT"] = string.Format("{0:N3}", Weight_t);
row.EndEdit();
if (grid_row == (ID_N - 1))
{
PressKey.PressKeys(Keys.Left, false);
PressKey.PressKeys(Keys.Left, true);
PressKey.PressKeys(Keys.Tab, false);
PressKey.PressKeys(Keys.Tab, true);
DataRow rowadd = RRODUCTdataTable.NewRow(); //ID列
//rowadd = RRODUCTdataTable.NewRow();
ID_N++;
rowadd["ID"] = ID_N;
rowadd["UNIT"] = "g";
RRODUCTdataTable.Rows.Add(rowadd);
}
}
DataGridHelper.SetRealTimeCommit(Grid_RRODUCT, true); //实时更新datagrid
}
}
}
private void CP_PRODUCT_CODE_DropDownClosed(object sender, EventArgs e)//原料代码选择事件
{
//double ang;
System.Windows.Controls.ComboBox curComboBox = sender as System.Windows.Controls.ComboBox;
int grid_row = Grid_RRODUCT.SelectedIndex; //获取当前行
Code_ = curComboBox.Text;
DataRow[] r = MainWindowViewModel.STUFFdatatemp.Select("ProductCode ='" + Code_ + "'"); //查询判断原料代码是否有效
if (!r.Any()) //检查原料信息
{
System.Windows.MessageBox.Show("原料不存在");
curComboBox.Text = "";
return;
}
DataRow[] c = RRODUCTdataTable.Select("PRODUCT_CODE ='" + Code_ + "'");//查询判断原料代码是否重复
if (c.Any())
{
System.Windows.MessageBox.Show("重复原料");
return;
}
Name_ = r[0].ItemArray[1].ToString();
Type_ = r[0].ItemArray[2].ToString();
Conc_ = r[0].ItemArray[3].ToString();
if ((Type_ == "0") || (Type_ == "3"))
{
Type_ = "%";
}
if ((Type_ == "1") || (Type_ == "2"))
{
Type_ = "g/L";
}
if (grid_row == -1) grid_row = 0;
DataRow row = RRODUCTdataTable.Rows[grid_row]; //ID列
row.BeginEdit();
row["PRODUCT_CODE"] = Code_;
row["PRODUCT_NAME"] = Name_;
row["SHIFT"] = Type_;
row.EndEdit();
DataGridHelper.SetRealTimeCommit(Grid_RRODUCT, true); //实时更新datagrid
}
private void CP_PRODUCT_NAME_DropDownClosed(object sender, EventArgs e)//原料名选择事件
{
//double ang;
System.Windows.Controls.ComboBox curComboBox = sender as System.Windows.Controls.ComboBox;
int grid_row = Grid_RRODUCT.SelectedIndex; //获取当前行
if (curComboBox.Text != "")
{
Name_ = curComboBox.Text;
DataRow[] r = MainWindowViewModel.STUFFdatatemp.Select("ProductName ='" + Name_ + "'"); //查询判断原料代码是否有效
DataRow[] c = RRODUCTdataTable.Select("PRODUCT_NAME ='" + Name_ + "'");//查询判断原料代码是否重复
if (c.Any())
{
System.Windows.MessageBox.Show("重复原料");
return;
}
Code_ = r[0].ItemArray[0].ToString();
Type_ = r[0].ItemArray[2].ToString();
Conc_ = r[0].ItemArray[3].ToString();
if ((Type_ == "0") || (Type_ == "3"))
{
Type_ = "%";
}
if ((Type_ == "1") || (Type_ == "2"))
{
Type_ = "g/L";
}
if (grid_row == -1) grid_row = 0;
DataRow row = RRODUCTdataTable.Rows[grid_row]; //ID列
row.BeginEdit();
row["PRODUCT_CODE"] = Code_;
row["PRODUCT_NAME"] = Name_;
row["SHIFT"] = Type_;
row.EndEdit();
}
}
}
}