using DevExpress.DocumentServices.ServiceModel.DataContracts; using Mesnac.Action.ChemicalWeighing.BinManage; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Action.ChemicalWeighing.Entity.material; using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe; using Mesnac.Codd.Session; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing.Technical.XlRecipe { public partial class FrmRecipeMaterial : Form { private List pmt_Materials = new List(); //所有物料集合 private ActionType _actionType = ActionType.Add; //操作类型,0-为添加,1-为修改 string recipeId = string.Empty; string materialID = string.Empty; string weighID = string.Empty; private Entity.xl_recipe pmt_Recipe = null; private List pmt_Weighs = null; //待写入数据库的配方物料信息List private xl_weigh weigh = null; int binId = 0; string materialName = string.Empty; string binName=string.Empty; public FrmRecipeMaterial() { InitializeComponent(); pmt_Weighs = new List(); } public FrmRecipeMaterial(ActionType actionType, string selectRecipeID) { InitializeComponent(); _actionType = actionType; recipeId = selectRecipeID; pmt_Recipe = RecipeHelper.GetRecipeByName(selectRecipeID); weigh=new xl_weigh(); } public FrmRecipeMaterial(ActionType actionType,string selectRecipeID,string _weighID,string _materialName) { InitializeComponent(); weighID = _weighID; pmt_Recipe = RecipeHelper.GetRecipeByName(selectRecipeID); pmt_Weighs = Product.XlPlan.PlanHelper.Getxl_weighList(pmt_Recipe.ID); weigh= pmt_Weighs.FirstOrDefault(x => x.ID == weighID); binId = weigh.Bin_Serial; //binName = _materialName; materialName = _materialName; _actionType = actionType; } private void btnOk_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(this.txtRecipeName.Text)) { MessageBox.Show("请输入新配方的名称!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (String.IsNullOrEmpty(this.cmbMaterial.Text)) { MessageBox.Show("请选择料仓!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (String.IsNullOrEmpty(this.txtBin.Text)) { MessageBox.Show("请选择料仓!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (String.IsNullOrEmpty(this.txtWeight.Text)) { MessageBox.Show("请输入物料重量!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (String.IsNullOrEmpty(this.txtError.Text)) { MessageBox.Show("请输入物料误差!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (_actionType == ActionType.Add) { int binId =Convert.ToInt32( this.cmbMaterial.SelectedValue.ToString());//仓库ID if (TechnicalHelper.GetXlRecipeMaterial(recipeId, materialID,binId)) { MessageBox.Show("物料列表中存在重复物料,请修改.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } if (_actionType == ActionType.Modify) { if (TechnicalHelper.IsXlRecipeMaterial(pmt_Recipe.ID, weigh.Material_ID, weighID)) { MessageBox.Show("物料列表中存在重复物料,请修改.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } } if (_actionType == ActionType.Add) { this.NewWeighAdd(); //配方对应的物料信息插入数据库 } else if (_actionType == ActionType.Modify) { var wList= Product.XlPlan.PlanHelper.Getxl_weighList(pmt_Recipe.ID); UpdateWeighByRecipeID(weighID, binId, weigh.Material_ID, Convert.ToDecimal(this.txtWeight.Text), Convert.ToDecimal(this.txtError.Text)); //配方对应的物料信息重新插入数据库 decimal totalError = wList.Sum(d=>d.Set_Error); decimal totalWeigh = wList.Sum(d => d.Set_Weight); UpdateError(pmt_Recipe.ID, totalWeigh, totalError); } this.DialogResult = System.Windows.Forms.DialogResult.OK; } private void FrmRecipeMaterial_Load(object sender, EventArgs e) { InitCombox(); if (_actionType == ActionType.Modify) { var binM = pmt_Weighs.FirstOrDefault(d => d.ID == weighID); this.txtRecipeName.Text = pmt_Recipe.Recipe_Name; this.txtVersion.Text = pmt_Recipe.Version; //this.textRecipeCode.Text = pmt_Recipe.Recipe_Code; this.txtBin.Text = materialName; this.txtWeight.Text = binM.Set_Weight.ToString(); this.txtError.Text = binM.Set_Error.ToString(); } if (_actionType == ActionType.Add) { this.txtRecipeName.Text = pmt_Recipe.Recipe_Name; this.txtVersion.Text = pmt_Recipe.Version; //this.textRecipeCode.Text = pmt_Recipe.Recipe_Code; this.cmbMaterial.SelectedIndex = 0; } } /// /// 物料/工位名称集合获取 /// public void InitCombox() { var table = BinHelper.GetBinMaterial(); if (table != null) { //以下向下拉列表框中插入“请选择” DataRow dr = table.NewRow(); dr[0] = "0"; dr[1] = "请选择"; dr[2] = "请选择"; dr[3] = "请选择"; dr[4] = "请选择"; dr[5] = "0"; table.Rows.InsertAt(dr, 0); this.cmbMaterial.DataSource = table; cmbMaterial.DisplayMember = "Bin_Code"; cmbMaterial.ValueMember = "Bin_Serial";//仓库编码 } if (_actionType == ActionType.Modify) { cmbMaterial.SelectedValue = binId; } if (_actionType == ActionType.Add) { cmbMaterial.SelectedValue = 0; } } private void cmbMaterial_SelectedIndexChanged(object sender, EventArgs e) { if (cmbMaterial.SelectedIndex == -1) return; if (cmbMaterial.SelectedIndex == 0) { txtBin.Text = ""; return; } // string combobox1_value = this.cmbMaterial.Text;//仓库名称 string combobox1_index = this.cmbMaterial.SelectedValue.ToString(); ;//仓库ID binId =Convert.ToInt32(combobox1_index); var list = BinHelper.GetBinList(); if (list != null) { var bin = list.FirstOrDefault(d => d.Bin_Serial == binId); List materialList = BinHelper.GetXlMaterialList(); if(materialList != null) { var material = materialList.FirstOrDefault(d=>d.ID== bin.Material_ID); if (material!=null) { txtBin.Text = material.Material_name; materialID = material.ID; } } binName= bin.Bin_Name.ToString(); } } #region 配方物料信息删除 /// /// 配方物料信息删除 /// public void WeighDelByRecipeID(string recipeID) { RecipeHelper.DelWeighByRecipeID(recipeID); } #endregion /// /// 配方物料信息插入数据库 /// public void NewWeighAdd() { var weigh = Product.XlPlan.PlanHelper.Getxl_weighList(pmt_Recipe.ID); int max = 0; if (weigh.Count==0) { max = 1; } else { max= weigh.Max(d=>d.Weight_Id)+1; } var list=BinHelper.GetBinList(); var bin=list.FirstOrDefault(d=>d.Bin_Serial==binId); //var max= pmt_Weighs.Max(d=>d.Weight_Id)+1; InsertWeigh(recipeId, binId, max, bin.Material_ID,Convert.ToDecimal(this.txtWeight.Text), Convert.ToDecimal(this.txtError.Text)); var wList = Product.XlPlan.PlanHelper.Getxl_weighList(pmt_Recipe.ID); decimal totalError = wList.Sum(d => d.Set_Error); decimal totalWeigh = wList.Sum(d => d.Set_Weight); UpdateError(pmt_Recipe.ID, totalWeigh, totalError); } #region 保存物料信息 /// /// 保存配方的物料信息 /// /// 保存配方的物料信息 public static void InsertWeigh(string Recipe_ID,int Bin_Serial,int Weight_Id,string Material_ID, decimal Set_Weight, decimal Set_Error) { try { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.CommandType = CommandType.Text; string sqlstr = String.Empty; //添加新配方 sqlstr = @"insert into xl_weigh(Equip_Code, Recipe_ID,Bin_Serial, Weight_Id, Material_ID,Station, Set_Weight, Set_Error, Cpk_Error) values(@Equip_Code, @Recipe_ID,@Bin_Serial, @Weight_Id, @Material_ID,@Station, @Set_Weight, @Set_Error, @Cpk_Error)"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Equip_Code", "01"); dbHelper.AddParameter("@Recipe_ID", Recipe_ID); dbHelper.AddParameter("@Bin_Serial", Bin_Serial); dbHelper.AddParameter("@Weight_Id", Weight_Id); dbHelper.AddParameter("@Material_ID", Material_ID); dbHelper.AddParameter("@Station", "0"); dbHelper.AddParameter("@Set_Weight", Set_Weight); dbHelper.AddParameter("@Set_Error", Set_Error); dbHelper.AddParameter("@Cpk_Error", null); dbHelper.ExecuteNonQuery(); } catch (Exception ex) { throw; } } /// /// 删除配方的物料信息 /// /// 配方ID public static void DelWeighByRecipeID(string recipeID) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.CommandType = CommandType.Text; string sqlstr = String.Empty; //添加新配方 sqlstr = @"delete from xl_weigh where Recipe_ID = @Recipe_ID"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@Recipe_ID", recipeID); dbHelper.ExecuteNonQuery(); } #endregion #region 保存物料信息 /// /// 删除配方的物料信息 /// /// 配方ID public static void UpdateWeighByRecipeID(string Id,int BinSerial,string Material_ID,decimal Set_Weight,decimal Set_Error) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.CommandType = CommandType.Text; string sqlstr = String.Empty; //添加新配方 sqlstr = @" update xl_weigh set Bin_Serial=@BinSerial,Material_ID=@MaterialID,Set_Weight=@Set_Weight,Set_Error=@Set_Error where ID=@Id"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@BinSerial", BinSerial); dbHelper.AddParameter("@MaterialID", Material_ID); dbHelper.AddParameter("@Set_Weight", Set_Weight); dbHelper.AddParameter("@Set_Error", Set_Error); dbHelper.AddParameter("@Id", Id); dbHelper.ExecuteNonQuery(); } #endregion #region 更新配方误差 /// /// 删除配方的物料信息 /// /// 配方ID public static void UpdateError(string Id, decimal totalWeigh,decimal Set_Error) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.CommandType = CommandType.Text; string sqlstr = String.Empty; //添加新配方 sqlstr = @" update xl_recipe set Total_Weight=@TotalWeight,Total_Error=@Error where ID=@Id"; dbHelper.ClearParameter(); dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@TotalWeight", totalWeigh); dbHelper.AddParameter("@Error", Set_Error); dbHelper.AddParameter("@Id", Id); dbHelper.ExecuteNonQuery(); } #endregion } }