using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Text.RegularExpressions ;
using System.Threading.Tasks ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Data ;
using System.Windows.Documents ;
using System.Windows.Input ;
using System.Windows.Media ;
using System.Windows.Media.Imaging ;
using System.Windows.Shapes ;
using GalaSoft.MvvmLight ;
using formula_manage.SQLModels ;
using System.Data.SqlClient ;
using System.Data ;
using System.Windows.Forms ;
using static System . Windows . Forms . VisualStyles . VisualStyleElement . Button ;
using Xceed.Wpf.AvalonDock.Themes ;
namespace formula_manage.Windows
{
/// <summary>
/// Stuff.xaml 的交互逻辑
/// </summary>
public partial class Stuff : Window
{
public string INIPath = Convert . ToString ( System . AppDomain . CurrentDomain . BaseDirectory ) + "formula.ini" ; //配置文件路径
int Button_ID = 0 ;
string TEXT_SQLIP ;
string TEXT_SQLNAME ;
string TEXT_SQMOD ;
string TEXT_SQLUSER ;
string TEXT_SQLPASWOR ;
string Connstr_SC ;
public Stuff ( )
{
WindowStartupLocation = WindowStartupLocation . CenterScreen ;
InitializeComponent ( ) ;
}
private async void Stuff_Loaded ( object sender , RoutedEventArgs e ) //打开页面执行
{
UserClass . IniFile . IniFiles Configini = new UserClass . IniFile . IniFiles ( INIPath ) ; //生效配置读取
this . DataGridStuff . LoadingRow + = new EventHandler < DataGridRowEventArgs > ( 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 ;
}
string Stuff_sql = "SELECT ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ,ProductIon_Type FROM [Dispensing].[dbo].[PRODUCT]" ; //查询语句
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
try
{
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlDataAdapter Stuff_data = new SqlDataAdapter ( Stuff_sql , Connstr_SC ) ; //查询
DataTable dataTable = new DataTable ( ) ; //建立缓存
Stuff_data . Fill ( dataTable ) ; //查询结果存入缓存
conn_SC . Close ( ) ; //关闭连接
DataGridStuff . ItemsSource = dataTable . DefaultView ; //数据加入表格
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求原料信息失败,检查连接" ) ;
return ;
}
}
/// <summary>
/// 生成序列号的方法
/// </summary>
private void DataGridEquipment_LoadingRow ( object sender , DataGridRowEventArgs e )
{
e . Row . Header = e . Row . GetIndex ( ) + 1 ;
}
private void Radiobutton ( object sender , EventArgs e )
{
System . Windows . Controls . RadioButton Radiobutton = ( sender as System . Windows . Controls . RadioButton ) ;
string radiobutton = Radiobutton . Content . ToString ( ) ;
if ( radiobutton = = "全部原料" )
{
GridSql ( 0 ) ;
Button_ID = 0 ;
}
else if ( radiobutton = = "染料" )
{
GridSql ( 1 ) ;
Button_ID = 1 ;
}
else if ( radiobutton = = "助剂" )
{
GridSql ( 2 ) ;
Button_ID = 2 ;
}
else if ( radiobutton = = "粉体助剂" )
{
GridSql ( 3 ) ;
Button_ID = 3 ;
}
else if ( radiobutton = = "液体染料" )
{
GridSql ( 4 ) ;
Button_ID = 4 ;
}
}
/// <summary>
/// 查询数据库的方法
/// </summary>
private async void GridSql ( int e )
{
string Stuff_sql = null ;
if ( e = = 0 ) Stuff_sql = "SELECT ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ,ProductIon_Type FROM [Dispensing].[dbo].[PRODUCT]" ; //查询语句
else if ( e = = 1 ) Stuff_sql = "SELECT ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ,ProductIon_Type FROM [Dispensing].[dbo].[PRODUCT] Where ProductType = '0' " ;
else if ( e = = 2 ) Stuff_sql = "SELECT ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ,ProductIon_Type FROM [Dispensing].[dbo].[PRODUCT] Where ProductType = '1' " ;
else if ( e = = 3 ) Stuff_sql = "SELECT ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ,ProductIon_Type FROM [Dispensing].[dbo].[PRODUCT] Where ProductType = '2' " ;
else if ( e = = 4 ) Stuff_sql = "SELECT ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ,ProductIon_Type FROM [Dispensing].[dbo].[PRODUCT] Where ProductType = '3' " ;
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
try
{
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlDataAdapter Stuff_data = new SqlDataAdapter ( Stuff_sql , Connstr_SC ) ; //查询
DataTable dataTable = new DataTable ( ) ; //建立缓存
Stuff_data . Fill ( dataTable ) ; //查询结果存入缓存
conn_SC . Close ( ) ; //关闭连接
DataGridStuff . ItemsSource = dataTable . DefaultView ; //数据加入表格
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求原料信息失败,检查连接" ) ;
return ;
}
}
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个英文字母,空白字符和@()()/+!!_-组成的字符串
int int_stuff_ProductType = 0 ;
int int_stuff_ion = 0 ;
int ColorCode_SQL ;
string Stuff_sql ;
string Product_Code = stuff_ProductCode . Text . ToString ( ) ;
string Product_Name = stuff_ProductName . Text . ToString ( ) ;
string _ Price = stuff_Price . Text . ToString ( ) ;
string _ SUPPLIER = stuff_SUPPLIER . Text . ToString ( ) ;
string _ Concentration = stuff_Concentration . Text . ToString ( ) ;
string _ GRAVITY = stuff_GRAVITY . Text . ToString ( ) ;
if ( this . stuff_ProductType . Text = = "染料" ) //原料类型0。染料,1助剂,2粉体
int_stuff_ProductType = 0 ;
else if ( this . stuff_ProductType . Text = = "助剂" )
int_stuff_ProductType = 1 ;
else if ( this . stuff_ProductType . Text = = "粉体助剂" )
int_stuff_ProductType = 2 ;
else if ( this . stuff_ProductType . Text = = "液体染料" )
int_stuff_ProductType = 3 ;
if ( stuff_ion . Text = = "分散" ) int_stuff_ion = 0 ;
else if ( stuff_ion . Text = = "酸性" ) int_stuff_ion = 1 ;
else if ( stuff_ion . Text = = "活性" ) int_stuff_ion = 2 ;
else if ( stuff_ion . Text = = "硫化" ) int_stuff_ion = 3 ;
else if ( stuff_ion . Text = = "阳离子" ) int_stuff_ion = 4 ;
else if ( stuff_ion . Text = = "酞菁" ) int_stuff_ion = 5 ;
else if ( stuff_ion . Text = = "直接" ) int_stuff_ion = 6 ;
if ( ( re_number . IsMatch ( this . stuff_Price . Text ) = = false ) & & ( stuff_Price . Text ! = "" ) )
System . Windows . MessageBox . Show ( "ERR:价格信息错误" , "错误" ) ; //检查价格输入信息
if ( re_char . IsMatch ( this . stuff_ProductCode . Text ) = = false )
System . Windows . MessageBox . Show ( "ERR:无效的原料信息" , "错误" ) ; //检查原料代码
else
if ( string . IsNullOrEmpty ( this . stuff_ProductName . Text ) )
System . Windows . MessageBox . Show ( "ERR:无效的原料名称" , "错误" ) ; //检查原料名称
else
if ( string . IsNullOrEmpty ( this . stuff_ProductType . Text ) )
System . Windows . MessageBox . Show ( "ERR:无效的原料类型" , "错误" ) ; //检查原料类型
else
if ( string . IsNullOrEmpty ( this . stuff_GRAVITY . Text ) )
System . Windows . MessageBox . Show ( "ERR:无效的比重" , "错误" ) ; //检查原料比重
else
if ( re_number . IsMatch ( this . stuff_GRAVITY . Text ) = = false )
System . Windows . MessageBox . Show ( "ERR:比重信息错误" , "错误" ) ; //检查比重输入信息
else
if ( re_number . IsMatch ( this . stuff_Concentration . Text ) = = false )
System . Windows . MessageBox . Show ( "ERR:浓度错误" , "错误" ) ; //检查浓度输入信息
else
if ( float . Parse ( this . stuff_GRAVITY . Text ) < 0 | | float . Parse ( this . stuff_GRAVITY . Text ) > 5 )
System . Windows . MessageBox . Show ( "ERR:比重错误" , "错误" ) ; //检查比重输入数值
else
if ( float . Parse ( this . stuff_Concentration . Text ) < 0 | | float . Parse ( this . stuff_Concentration . Text ) > 1 0 0 )
System . Windows . MessageBox . Show ( "ERR:浓度错误" , "错误" ) ; //检查浓度输入数值
else
{
try
{
if ( int_stuff_ProductType = = 0 | | int_stuff_ProductType = = 3 ) //判断是否为染料,非染料写空
{
string colorValue = stuff_Color . Background . ToString ( ) ; //获取色彩框背景色"#FFC1C2C3"
string StuffColor_R = string . Format ( "{0:X2}" , colorValue . Substring ( 3 , 2 ) ) ; //获取红色参数C1
string StuffColor_G = string . Format ( "{0:X2}" , colorValue . Substring ( 5 , 2 ) ) ; //获取绿色参数C2
string StuffColor_B = string . Format ( "{0:X2}" , colorValue . Substring ( 7 , 2 ) ) ; //获取蓝色参数C3
string StuffColor = StuffColor_B + StuffColor_G + StuffColor_R ; //合并16进制rgb参数
ColorCode_SQL = Convert . ToInt32 ( StuffColor , 1 6 ) ; //16进制转10进制
if ( ColorCode_SQL < 0 ) ColorCode_SQL = 1 6 7 7 7 2 1 5 ; //判断色彩数据是否有效
}
else
{
ColorCode_SQL = 1 6 7 7 7 2 1 5 ;
}
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ; //实例化
Stuff_sql = "SELECT count(*) FROM [Dispensing].[dbo].[PRODUCT] WHERE ProductCode = '" + Product_Code + " '" ; //查询语句
await conn_SC . OpenAsync ( ) ; //打开数据连
SqlCommand cmd = new SqlCommand ( Stuff_sql , conn_SC ) ; //查询记录数
int count = Convert . ToInt32 ( cmd . ExecuteScalar ( ) ) ; //显示记录数
conn_SC . Close ( ) ; //关闭连接
if ( count = = 0 )
{
Stuff_sql = string . Format ( "INSERT INTO[Dispensing].[dbo].[PRODUCT](ProductCode ,ProductName , Price , SUPPLIER ,ProductType ,Color ,Concentration , GRAVITY ) " +
"VALUES ('" + Product_Code + "','" + Product_Name + "','" + _ Price + "','" + _ SUPPLIER + "','" + int_stuff_ProductType + "','" + ColorCode_SQL + "','" + _ Concentration + "','" + _ GRAVITY + "')" ) ;
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlCommand INSERT_cmd = new SqlCommand ( Stuff_sql , conn_SC ) ;
int sql_in = INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
if ( stuff_ion . Text ! = "" ) Stuff_sql = string . Format ( "UPDATE [dbo].[PRODUCT] SET [ProductIon_Type] =" + int_stuff_ion + " Where ProductCode='" + Product_Code + "'" ) ;
SqlCommand Stuff_sql_cmd = new SqlCommand ( Stuff_sql , conn_SC ) ;
INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
conn_SC . Close ( ) ; //关闭连接
if ( sql_in = = 0 )
{
System . Windows . MessageBox . Show ( "添加失败" ) ;
}
else
{
if ( Button_ID = = 0 ) GridSql ( 0 ) ;
else if ( Button_ID = = 1 ) GridSql ( 1 ) ;
else if ( Button_ID = = 2 ) GridSql ( 2 ) ;
else if ( Button_ID = = 3 ) GridSql ( 3 ) ;
else if ( Button_ID = = 4 ) GridSql ( 4 ) ;
}
}
else
{
Stuff_sql = string . Format ( "UPDATE [dbo].[PRODUCT] SET ProductName='" + Product_Name + "',Price='" + _ Price + "',SUPPLIER='" + _ SUPPLIER + "',ProductType=" + int_stuff_ProductType + ",Color=" + ColorCode_SQL + ",Concentration=" + _ Concentration + ",GRAVITY=" + _ GRAVITY + " Where ProductCode='" + Product_Code + "'" ) ;
await conn_SC . OpenAsync ( ) ; //打开数据连接
SqlCommand INSERT_cmd = new SqlCommand ( Stuff_sql , conn_SC ) ;
int sql_in = INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
if ( stuff_ion . Text ! = "" ) Stuff_sql = string . Format ( "UPDATE [dbo].[PRODUCT] SET [ProductIon_Type] =" + int_stuff_ion + " Where ProductCode='" + Product_Code + "'" ) ;
SqlCommand Stuff_sql_cmd = new SqlCommand ( Stuff_sql , conn_SC ) ;
INSERT_cmd . ExecuteNonQuery ( ) ; //执行语句
conn_SC . Close ( ) ; //关闭连接
if ( sql_in = = 0 )
{
System . Windows . MessageBox . Show ( "修改失败" ) ;
}
else
{
if ( Button_ID = = 0 ) GridSql ( 0 ) ;
else if ( Button_ID = = 1 ) GridSql ( 1 ) ;
else if ( Button_ID = = 2 ) GridSql ( 2 ) ;
else if ( Button_ID = = 3 ) GridSql ( 3 ) ;
else if ( Button_ID = = 4 ) GridSql ( 4 ) ;
}
}
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求失败,检查连接" ) ;
return ;
}
}
}
private async void Button_Delete ( object sender , RoutedEventArgs e ) //删除按钮事件
{
string ShowProductName = "是否删除原料【" + this . stuff_ProductName . Text + "】" + this . stuff_ProductCode . Text ; //获取原料信息并拼接提示字符串
MessageBoxResult showProductName = System . Windows . MessageBox . Show ( ShowProductName , "提示" , MessageBoxButton . YesNo , MessageBoxImage . Warning , MessageBoxResult . Yes ) ; //弹窗提示是否删除目标原料
if ( showProductName = = MessageBoxResult . Yes ) //判断是否删除原料
{
string Stuff_sql = string . Format ( "DELETE FROM [dbo].[PRODUCT] WHERE ProductCode ='" + stuff_ProductCode . 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
{
if ( Button_ID = = 0 ) GridSql ( 0 ) ;
else if ( Button_ID = = 1 ) GridSql ( 1 ) ;
else if ( Button_ID = = 2 ) GridSql ( 2 ) ;
else if ( Button_ID = = 3 ) GridSql ( 3 ) ;
else if ( Button_ID = = 4 ) GridSql ( 4 ) ;
}
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "请求失败,检查连接" ) ;
return ;
}
}
}
private void DataGridStuff_MouseDoubleClick ( object sender , MouseButtonEventArgs e ) //数据表双击事件
{
int rownum = DataGridStuff . SelectedIndex ; //获取鼠标选中行并定义变量
if ( rownum ! = - 1 ) //判断鼠标定位是否有效
{
/*定位选中行及指定列单元格文本信息*/
string DataGridStuff_ProductCode = ( DataGridStuff . Columns [ 1 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位第0列,原料代码
string DataGridStuff_ProductName = ( DataGridStuff . Columns [ 2 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位第1列,原料名称
string DataGridStuff_Price = ( DataGridStuff . Columns [ 8 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位,价格
string DataGridstuff_SUPPLIER = ( DataGridStuff . Columns [ 9 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位,供应商
string DataGridStuff_ProductType = ( DataGridStuff . Columns [ 3 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位,类型 //string DataGridStuff_Color = (DataGridStuff.Columns[6].GetCellContent(DataGridStuff.Items[rownum])as TextBlock).Text;//定位,色彩
string DataGridStuff_Concentration = ( DataGridStuff . Columns [ 6 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位选中行单元格文本信息
string DataGridStuff_stuff_ion = ( DataGridStuff . Columns [ 4 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位选中行单元格文本信息并去除空白字符,浓度
string DataGridStuff_GRAVITY = ( DataGridStuff . Columns [ 7 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Text ; //定位,比重
/*stuff_Color.Background = (DataGridStuff.Columns[6].GetCellContent(DataGridStuff.Items[rownum]) as TextBlock).Background;//定位,色彩,背景色传递至色彩框背景色*/
/*填写信息至指定文本框*/
//product_temp.ProductCode = DataGridStuff_ProductCode;//写入sql实体缓存
stuff_ProductCode . Text = DataGridStuff_ProductCode . Trim ( ) ; //原料代码,去除空白字符
stuff_ProductName . Text = DataGridStuff_ProductName . Trim ( ) ; //原料名称,去除空白字符
stuff_Price . Text = DataGridStuff_Price ; //价格
stuff_ProductType . Text = DataGridStuff_ProductType ; //类型
stuff_ion . Text = DataGridStuff_stuff_ion ;
stuff_SUPPLIER . Text = DataGridstuff_SUPPLIER ; //供应商
/*stuff_Color.Text = DataGridStuff_Color;//色彩数据*/
stuff_Concentration . Text = DataGridStuff_Concentration ; //浓度
stuff_GRAVITY . Text = DataGridStuff_GRAVITY ; //比重
/ * if ( DataGridStuff_ProductType . ToString ( ) = = "0" ) stuff_ProductType . Text = "染料" ; //判断类型1-染料。2-助剂。3-粉体助剂。
if ( DataGridStuff_ProductType . ToString ( ) = = "1" ) stuff_ProductType . Text = "助剂" ;
if ( DataGridStuff_ProductType . ToString ( ) = = "2" ) stuff_ProductType . Text = "粉体助剂" ; * /
if ( ( DataGridStuff . Columns [ 5 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Background ! = null ) //定位选中行单元格,色彩,判断参数是否有效
{
stuff_Color . Background = ( DataGridStuff . Columns [ 5 ] . GetCellContent ( DataGridStuff . Items [ rownum ] ) as TextBlock ) . Background ; //定位选中行单元格,色彩,有效背景色传递至色彩框背景色
}
else
{
stuff_Color . Background = new SolidColorBrush ( System . Windows . Media . Color . FromArgb ( 2 5 5 , 2 5 5 , 2 5 5 , 2 5 5 ) ) ; //白色传递至色彩框背景色
}
}
}
private void Stuff_Color_MouseDoubleClick ( object sender , MouseButtonEventArgs e ) //色彩框双击事件
{
System . Windows . Forms . ColorDialog colorDialog = new System . Windows . Forms . ColorDialog ( ) ; //使用调色盘控件ColorDialog
if ( colorDialog . ShowDialog ( ) = = System . Windows . Forms . DialogResult . OK ) //打开调色盘
{
System . Drawing . Color DColor = colorDialog . Color ; ////获取选中色彩信息
//System.Windows.Media.Color MColor = new System.Windows.Media.Color();//转换
System . Windows . Media . Color MColor = System . Windows . Media . Color . FromArgb ( DColor . A , DColor . R , DColor . G , DColor . B ) ; //转换并配置ARGB参数
stuff_Color . Background = new SolidColorBrush ( MColor ) ; //ARGB参数输出至stuff_Color的背景色
//string colorCode = string.Format("{0:X2}", DColor.B) + string.Format("{0:X2}", DColor.G) + string.Format("{0:X2}", DColor.R);//反向十六进制RGB
//string colorCode = string.Format("{0:X2}", DColor.R) + string.Format("{0:X2}", DColor.G) + string.Format("{0:X2}", DColor.B);//正向十六进制RGB
//ColorCode_SQL = Convert.ToInt32(colorCode, 16);//十六进制RGB转存储数值
}
}
}
}