using HighWayIot.Repository.domain;
using HighWayIot.Repository.service;
using HighWayIot.Winform.Business;
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
{
///
/// 配方服务类实例
///
private ZxRecipeService zxRecipeService = ZxRecipeService.Instance;
///
/// 称重服务类实例
///
private ZxWeightService zxWeightService = ZxWeightService.Instance;
///
/// 配方服务类实例
///
private ZxMaterialService zxMaterialService = ZxMaterialService.Instance;
///
/// 配方字段服务类实例
///
private ZxRecipeParaService zxRecipeParaService = ZxRecipeParaService.Instance;
///
/// 工位配方字段服务类实例
///
private ZxRecipePositionParaService zxRecipePositionParaService = ZxRecipePositionParaService.Instance;
///
/// 配方字段实例
///
private ZxRecipeParaEntity zxRecipeParaEntity = new ZxRecipeParaEntity();
///
/// 工位配方字段实例
///
private ZxRecipePositionParaEntity zxRecipePositionParaEntity = new ZxRecipePositionParaEntity();
///
/// 配方字段实例剪切板
///
private ZxRecipeParaEntity zxRecipeParaEntityCut;
///
/// 称重DataGridView数据源
///
private List weightDataSourceEntities;
///
/// 配方列表
///
private List RecipeLists;
///
/// 称量列表
///
private 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)
{
string s = RecipeDataGridView.CurrentRow.Cells["RecipeCode"].Value.ToString();
//if (MessageBox.Show($"确定要保存配方编号为 [{s}] 的配方的参数?", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
//{
// return;
//}
ZxRecipePositionParaEntity entity = GetParaValue();
var paraData = zxRecipeParaService.GetRecipeParaInfoByRecipeCode(zxRecipeParaEntity.RecipeCode);
int flag = GetSelectIndex();
//没有就插入
if (paraData.Count == 0)
{
if (zxRecipeParaService.InsertRecipeParaInfo(zxRecipeParaEntity))
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败!");
}
}
//有就更改
else if (paraData.Count == 1)
{
zxRecipeParaEntity.Id = paraData[0].Id;
if (zxRecipeParaService.UpdateRecipeParaInfo(zxRecipeParaEntity))
{
MessageBox.Show("保存成功!");
}
else
{
MessageBox.Show("保存失败!");
}
}
else
{
MessageBox.Show("存在多条未删除的相同配方参数!请检查数据库。");
return;
}
}
///
/// 参数复制
///
///
///
private void CopyParaButton_Click(object sender, EventArgs e)
{
//zxRecipeParaEntityCut = GetParaValue();
NowCopyLabel.Text = RecipeDataGridView.CurrentRow.Cells["RecipeCode"].Value.ToString();
}
///
/// 参数粘贴
///
///
///
private void PasteParaButton_Click(object sender, EventArgs e)
{
if (zxRecipeParaEntityCut == null)
{
MessageBox.Show("剪切板为空!");
return;
}
//SetParaValue(zxRecipeParaEntityCut);
}
///
/// 参数清空
///
///
///
private void ClearCutBoradButton_Click(object sender, EventArgs e)
{
zxRecipeParaEntityCut = null;
NowCopyLabel.Text = "N/A";
if (MessageBox.Show("是否要清空前端数据", "", MessageBoxButtons.YesNo) == DialogResult.OK)
{
//SetParaValue(new ZxRecipeParaEntity());
}
}
///
/// 所选配方内容更改事件
///
///
///
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;
//设置参数
var paraData = zxRecipeParaService.GetRecipeParaInfoByRecipeCode(s);
//没有就全0
if (paraData.Count == 0)
{
//SetParaValue(new ZxRecipeParaEntity());
}
//有就显示
else if (paraData.Count == 1)
{
//SetParaValue(paraData[0]);
}
//有多条设为第一条
else
{
MessageBox.Show("存在多条未删除的相同配方字段信息!将设置为第一条,请检查数据库。");
//SetParaValue(paraData[0]);
}
}
///
/// 称重数据关联物料数据
///
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,
});
}
}
///
/// 设置显示的配方字段值
///
///
private void SetParaValue(ZxRecipePositionParaEntity entity)
{
E1TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E1);
E2TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E2);
E3TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E3);
E4TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E4);
E5TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E5);
E6TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E6);
E7TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E7);
E8TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E8);
E9TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E9);
E10TextBox.Text = GeneralUtils.IntEmptyOrToString(entity.E10);
}
///
/// 获取界面上的配方参数
///
/// 获取到的配方参数值
private ZxRecipePositionParaEntity GetParaValue()
{
ZxRecipePositionParaEntity entity = new ZxRecipePositionParaEntity()
{
E1 = GeneralUtils.StringNullOrToInt(E1TextBox.Text),
E2 = GeneralUtils.StringNullOrToInt(E2TextBox.Text),
E3 = GeneralUtils.StringNullOrToInt(E3TextBox.Text),
E4 = GeneralUtils.StringNullOrToInt(E4TextBox.Text),
E5 = GeneralUtils.StringNullOrToInt(E5TextBox.Text),
E6 = GeneralUtils.StringNullOrToInt(E6TextBox.Text),
E7 = GeneralUtils.StringNullOrToInt(E7TextBox.Text),
E8 = GeneralUtils.StringNullOrToInt(E8TextBox.Text),
E9 = GeneralUtils.StringNullOrToInt(E9TextBox.Text),
E10 = GeneralUtils.StringNullOrToInt(E10TextBox.Text)
};
return entity;
}
///
/// 工位改变
///
///
///
private void PositionRadioBoxChange(object sender, EventArgs e)
{
RadioButton s = sender as RadioButton;
if (s.Checked)
{
int flag = GetSelectIndex();
switch (flag)
{
case 1:
SetParaViewById(zxRecipeParaEntity.P1);
break;
case 2:
SetParaViewById(zxRecipeParaEntity.P2);
break;
case 31:
SetParaViewById(zxRecipeParaEntity.P31);
break;
case 32:
SetParaViewById(zxRecipeParaEntity.P32);
break;
case 41:
SetParaViewById(zxRecipeParaEntity.P41);
break;
case 42:
SetParaViewById(zxRecipeParaEntity.P42);
break;
case 51:
SetParaViewById(zxRecipeParaEntity.P51);
break;
case 52:
SetParaViewById(zxRecipeParaEntity.P52);
break;
case 0:
SetParaViewById(0);
break;
default:
SetParaViewById(0);
break;
}
}
}
private void SetParaViewById(int? id)
{
zxRecipePositionParaEntity = zxRecipePositionParaService.GetRecipePositionParaInfoById(id ?? 0);
SetParaValue(zxRecipePositionParaEntity);
}
///
/// 获取选择的工位包边
///
/// 选的不对劲就返回0
private int GetSelectIndex()
{
if (Position1RadioButton.Checked)
{
return 1;
}
if (Position2RadioButton.Checked)
{
return 2;
}
if (Position3RadioButton.Checked)
{
if (BodyRadioButton.Checked)
{
return 31;
}
if (SideRadioButton.Checked)
{
return 32;
}
}
if (Position4RadioButton.Checked)
{
if (BodyRadioButton.Checked)
{
return 41;
}
if (SideRadioButton.Checked)
{
return 42;
}
}
if (Position5RadioButton.Checked)
{
if (BodyRadioButton.Checked)
{
return 51;
}
if (SideRadioButton.Checked)
{
return 52;
}
}
return 0;
}
}
}