using HighWayIot.Repository.domain; using HighWayIot.Repository.service; using HighWayIot.Winform.UserControlPages.RecipeConfigPages; using HighWayIot.Winform.UserControlPages.SysConfigPages; using Models; using System; using System.Collections; 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 HighWayIot.Winform.UserControlPages { public partial class RecipeConfigPage : UserControl { /// /// 配方服务类实例 /// ZxRecipeService zxRecipeService = ZxRecipeService.Instance; /// /// 称重服务类实例 /// ZxWeightService zxWeightService = ZxWeightService.Instance; /// /// 配方服务类实例 /// ZxMaterialService zxMaterialService = ZxMaterialService.Instance; /// /// 配方字段服务类实例 /// ZxRecipeParaService zxRecipeParaService = ZxRecipeParaService.Instance; /// /// 称重DataGridView数据源 /// List weightDataSourceEntities; /// /// 配方列表 /// List RecipeLists; /// /// 称量列表 /// List WeightLists; public RecipeConfigPage() { InitializeComponent(); Init(); } private void Init() { RecipeDataGridView.AutoGenerateColumns = false; WeightDataGridView.AutoGenerateColumns = false; RecipeLists = zxRecipeService.GetRecipeInfos(); RecipeDataGridView.DataSource = null; RecipeDataGridView.DataSource = RecipeLists; } /// /// 添加配方信息 /// /// /// private void AddRecipeButton_Click(object sender, EventArgs e) { AddRecipeForm form = new AddRecipeForm(); form.ShowDialog(); RecipeLists = zxRecipeService.GetRecipeInfos(); RecipeDataGridView.DataSource = null; RecipeDataGridView.DataSource = RecipeLists; } /// /// 删除配方信息 /// /// /// private void DeleteRecipeButton_Click(object sender, EventArgs e) { int a = RecipeDataGridView.CurrentRow.Index; string s = RecipeDataGridView.Rows[a].Cells["RecipeCode"].Value.ToString(); int id; try { id = int.Parse(RecipeDataGridView.Rows[a].Cells["RId"].Value.ToString()); } catch(Exception ex) { MessageBox.Show("ID转换发生错误"); return; } if (MessageBox.Show($"确定要删除编号为 [{s}] 的配方信息?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } if (zxWeightService.GetWeightInfos(s).Count > 0) { if (MessageBox.Show("是否要删除其关联的所有称量信息?", "确认", MessageBoxButtons.OKCancel) == DialogResult.OK) { if (zxWeightService.DeleteWeightInfoByRecipeCode(s)) { MessageBox.Show("称量信息删除成功"); } else { MessageBox.Show("称量信息删除失败!请检查数据库连接情况"); } } } if (zxRecipeService.DeleteRecipeInfoById(id)) { MessageBox.Show("配方信息删除成功!"); } else { MessageBox.Show("配方信息删除失败!请检查数据库连接情况"); } RecipeLists = zxRecipeService.GetRecipeInfos(); RecipeDataGridView.DataSource = null; RecipeDataGridView.DataSource = RecipeLists; } /// /// 修改配方信息 /// /// /// private void UpdateRecipeButton_Click(object sender, EventArgs e) { DataGridViewRow nowRow = RecipeDataGridView.CurrentRow; if(MessageBox.Show($"确认要更改编号为 [{nowRow.Cells["RecipeCode"].Value.ToString()}] 配方的数据吗", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } ZxRecipeEntity entity = new ZxRecipeEntity(); try { entity.Id = int.Parse(nowRow.Cells["RId"].Value.ToString().Trim()); entity.RecipeCode = nowRow.Cells["RecipeCode"].Value.ToString().Trim(); entity.RecipeName = nowRow.Cells["RecipeName"].Value.ToString().Trim(); entity.RecipeSpecCode = nowRow.Cells["RecipeSpecCode"].Value.ToString().Trim(); entity.RecipeSpecName = nowRow.Cells["RecipeSpecName"].Value.ToString().Trim(); entity.SizeKind = int.Parse(nowRow.Cells["SizeKind"].Value.ToString().Trim()); entity.FixedWidth = decimal.Parse(nowRow.Cells["FixedWidth"].Value.ToString().Trim()); entity.WeightError = int.Parse(nowRow.Cells["WeightError"].Value.ToString().Trim()); entity.IsUse = bool.Parse(nowRow.Cells["IsUse"].Value.ToString().Trim()); entity.IsDeleted = false; } catch (Exception ex) { MessageBox.Show("数据格式错误!"); return; } if (zxRecipeService.UpdateRecipeInfo(entity)) { MessageBox.Show("配方更新成功!"); } else { MessageBox.Show("配方更新失败!"); } RecipeLists = zxRecipeService.GetRecipeInfos(); RecipeDataGridView.DataSource = null; RecipeDataGridView.DataSource = RecipeLists; } /// /// 刷新配方信息 /// /// /// private void RefreshRecipeButton_Click(object sender, EventArgs e) { RecipeLists = zxRecipeService.GetRecipeInfos(); RecipeDataGridView.DataSource = null; RecipeDataGridView.DataSource = RecipeLists; } /// /// 添加称量信息 /// /// /// private void AddWeightButton_Click(object sender, EventArgs e) { if (RecipeDataGridView.CurrentRow == null) { MessageBox.Show("请先选择一条配方!"); return; } int a = RecipeDataGridView.CurrentRow.Index; string s = RecipeDataGridView.Rows[a].Cells["RecipeCode"].Value.ToString(); AddWeightForm form = new AddWeightForm(s); form.ShowDialog(); WeightLists = zxWeightService.GetWeightInfos(s); WeightToDataSource(); WeightDataGridView.DataSource = null; WeightDataGridView.DataSource = weightDataSourceEntities; } /// /// 删除称量信息 /// /// /// private void DeleteWeightButton_Click(object sender, EventArgs e) { int a = WeightDataGridView.CurrentRow.Index; int b = RecipeDataGridView.CurrentRow.Index; string s1 = RecipeDataGridView.Rows[b].Cells["RecipeCode"].Value.ToString(); string s2 = WeightDataGridView.Rows[a].Cells["MaterialCode"].Value.ToString(); int id; try { id = int.Parse(WeightDataGridView.Rows[a].Cells["Id"].Value.ToString()); } catch (Exception ex) { MessageBox.Show("ID转换发生错误"); return; } if (MessageBox.Show($"确定要删除配方编号为 [{s1}] 物料编码为 [{s2}] 的称重信息?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } if (zxWeightService.DeleteWeightInfoById(id)) { MessageBox.Show("配方信息删除成功!"); } else { MessageBox.Show("配方信息删除失败!请检查数据库连接情况"); } WeightLists = zxWeightService.GetWeightInfos(s1); WeightToDataSource(); WeightDataGridView.DataSource = null; WeightDataGridView.DataSource = weightDataSourceEntities; } /// /// 修改称量信息 /// /// /// private void UpdateWeightButton_Click(object sender, EventArgs e) { DataGridViewRow nowRow = WeightDataGridView.CurrentRow; int a = RecipeDataGridView.CurrentRow.Index; string s = RecipeDataGridView.Rows[a].Cells["RecipeCode"].Value.ToString(); if (MessageBox.Show($"确认要更改配方编号为 [{s}] 物料编号为 [{nowRow.Cells["MaterialCode"].Value.ToString()}] 的数据吗" , "确认" , MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return; } ZxWeightEntity entity = new ZxWeightEntity(); try { entity.Id = int.Parse(nowRow.Cells["Id"].Value.ToString().Trim()); entity.RecipeCode = s; entity.MaterialCode = nowRow.Cells["MaterialCode"].Value.ToString().Trim(); entity.SetThickness = decimal.Parse(nowRow.Cells["SetThickness"].Value.ToString().Trim()); entity.SetWidth = decimal.Parse(nowRow.Cells["SetWidth"].Value.ToString().Trim()); entity.SetLayer = int.Parse(nowRow.Cells["SetLayer"].Value.ToString().Trim()); entity.SetWeight = decimal.Parse(nowRow.Cells["SetWeight"].Value.ToString().Trim()); entity.SetError = decimal.Parse(nowRow.Cells["SetError"].Value.ToString().Trim()); entity.IsUse = bool.Parse(nowRow.Cells["WeightIsUse"].Value.ToString().Trim()); entity.IsDeleted = false; } catch (Exception ex) { MessageBox.Show("数据格式错误!"); return; } if (zxWeightService.UpdateWeightInfo(entity)) { MessageBox.Show("称量信息更新成功!"); } else { MessageBox.Show("称量信息更新失败!"); } WeightLists = zxWeightService.GetWeightInfos(s); WeightToDataSource(); WeightDataGridView.DataSource = null; WeightDataGridView.DataSource = weightDataSourceEntities; } /// /// 刷新称量信息 /// /// /// private void RefreshWeightButton_Click(object sender, EventArgs e) { int a = RecipeDataGridView.CurrentRow.Index; string s = RecipeDataGridView.Rows[a].Cells["RecipeCode"].Value.ToString(); WeightLists = zxWeightService.GetWeightInfos(s); WeightToDataSource(); WeightDataGridView.DataSource = null; WeightDataGridView.DataSource = weightDataSourceEntities; } /// /// 参数保存 /// /// /// private void SaveParaButton_Click(object sender, EventArgs e) { } /// /// 参数复制 /// /// /// private void CopyParaButton_Click(object sender, EventArgs e) { } /// /// 参数粘贴 /// /// /// private void PasteParaButton_Click(object sender, EventArgs e) { } /// /// 参数清空 /// /// /// private void ClearCutBoradButton_Click(object sender, EventArgs e) { } /// /// 所选配方内容更改事件 /// /// /// private void RecipeDataGridView_SelectionChanged(object sender, EventArgs e) { int a = RecipeDataGridView.CurrentRow.Index; string s = RecipeDataGridView.Rows[a].Cells["RecipeCode"].Value.ToString(); WeightLists = zxWeightService.GetWeightInfos(s); WeightToDataSource(); WeightDataGridView.DataSource = null; WeightDataGridView.DataSource = weightDataSourceEntities; } /// /// 称重数据关联物料数据 /// private void WeightToDataSource() { weightDataSourceEntities = new List(); foreach (var item in WeightLists) { var entitys = zxMaterialService.GetEntityByMaterialCode(item.MaterialCode); if (entitys == null && entitys.Count == 0) { return; } if (entitys.Count > 1) { MessageBox.Show("请检查是否有多条物料编号相同的可用物料"); return; } ZxMaterialEntity material = entitys[0]; weightDataSourceEntities.Add(new WeightDataSourceEntity() { Id = item.Id, RecipeCode = item.RecipeCode, MaterialCode = item.MaterialCode, MaterialName = material.MaterialName, MaterialType = material.MaterialType, MaterialChildType = material.ChildType, SetThickness = item.SetThickness, SetWidth = item.SetWidth, SetLayer = item.SetLayer, SetWeight = item.SetWeight, SetError = item.SetError, IsUse = item.IsUse, }); } } /// /// 所选单条Weight dataSource转为实体类 /// //private ZxWeightEntity DataSourceToWeight() //{ // int a = WeightDataGridView.CurrentRow.Index; // foreach //} } }