1
0
Fork 0
You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

605 lines
22 KiB
C#

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
{
/// <summary>
/// 配方服务类实例
/// </summary>
ZxRecipeService zxRecipeService = ZxRecipeService.Instance;
/// <summary>
/// 称重服务类实例
/// </summary>
ZxWeightService zxWeightService = ZxWeightService.Instance;
/// <summary>
/// 配方服务类实例
/// </summary>
ZxMaterialService zxMaterialService = ZxMaterialService.Instance;
/// <summary>
/// 配方字段服务类实例
/// </summary>
ZxRecipeParaService zxRecipeParaService = ZxRecipeParaService.Instance;
/// <summary>
/// 配方字段实例
/// </summary>
ZxRecipeParaEntity zxRecipeParaEntity = new ZxRecipeParaEntity();
/// <summary>
/// 配方字段实例剪切板
/// </summary>
ZxRecipeParaEntity zxRecipeParaEntityCut;
/// <summary>
/// 称重DataGridView数据源
/// </summary>
List<WeightDataSourceEntity> weightDataSourceEntities;
/// <summary>
/// 配方列表
/// </summary>
List<ZxRecipeEntity> RecipeLists;
/// <summary>
/// 称量列表
/// </summary>
List<ZxWeightEntity> WeightLists;
public RecipeConfigPage()
{
InitializeComponent();
Init();
}
private void Init()
{
RecipeDataGridView.AutoGenerateColumns = false;
WeightDataGridView.AutoGenerateColumns = false;
RecipeLists = zxRecipeService.GetRecipeInfos();
RecipeDataGridView.DataSource = null;
RecipeDataGridView.DataSource = RecipeLists;
}
/// <summary>
/// 添加配方信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddRecipeButton_Click(object sender, EventArgs e)
{
AddRecipeForm form = new AddRecipeForm();
form.ShowDialog();
RecipeLists = zxRecipeService.GetRecipeInfos();
RecipeDataGridView.DataSource = null;
RecipeDataGridView.DataSource = RecipeLists;
}
/// <summary>
/// 删除配方信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 修改配方信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 刷新配方信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RefreshRecipeButton_Click(object sender, EventArgs e)
{
RecipeLists = zxRecipeService.GetRecipeInfos();
RecipeDataGridView.DataSource = null;
RecipeDataGridView.DataSource = RecipeLists;
}
/// <summary>
/// 添加称量信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 删除称量信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 修改称量信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 刷新称量信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
/// <summary>
/// 参数保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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;
}
ZxRecipeParaEntity entity = GetParaValue();
entity.IsDeleted = false;
entity.RecipeCode = s;
zxRecipeParaEntity = entity;
var paraData = zxRecipeParaService.GetRecipeParaInfoByRecipeCode(zxRecipeParaEntity.RecipeCode);
//没有就插入
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;
}
}
/// <summary>
/// 参数复制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CopyParaButton_Click(object sender, EventArgs e)
{
zxRecipeParaEntityCut = GetParaValue();
NowCopyLabel.Text = RecipeDataGridView.CurrentRow.Cells["RecipeCode"].Value.ToString();
}
/// <summary>
/// 参数粘贴
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PasteParaButton_Click(object sender, EventArgs e)
{
if (zxRecipeParaEntityCut == null)
{
MessageBox.Show("剪切板为空!");
return;
}
SetParaValue(zxRecipeParaEntityCut);
}
/// <summary>
/// 参数清空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClearCutBoradButton_Click(object sender, EventArgs e)
{
zxRecipeParaEntityCut = null;
NowCopyLabel.Text = "N/A";
if (MessageBox.Show("是否要清空前端数据", "", MessageBoxButtons.YesNo) == DialogResult.OK)
{
SetParaValue(new ZxRecipeParaEntity());
}
}
/// <summary>
/// 所选配方内容更改事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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]);
}
}
/// <summary>
/// 称重数据关联物料数据
/// </summary>
private void WeightToDataSource()
{
weightDataSourceEntities = new List<WeightDataSourceEntity>();
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,
});
}
}
/// <summary>
/// 设置配方字段值
/// </summary>
/// <param name="entity"></param>
private void SetParaValue(ZxRecipeParaEntity entity)
{
zxRecipeParaEntity = entity;
E1P1TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E1P1);
E1P2TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E1P2);
E1P3TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E1P3);
E1P4TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E1P4);
E2P1TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E2P1);
E2P2TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E2P2);
E2P3TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E2P3);
E2P4TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E2P4);
E3P1TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P1);
E3P2TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P2);
E3P3TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P3);
E3P4TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P4);
E3P5CheckBox.Checked = zxRecipeParaEntity.E3P5 == 1;
E3P6TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P6);
E3P7TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P7);
E3P8TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P8);
E3P9TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P9);
E3P10TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P10);
E3P11TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P11);
E3P12TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P12);
E3P13TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P13);
E3P14TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P14);
E3P15TextBox.Text = GeneralUtils.IntEmptyOrToString(zxRecipeParaEntity.E3P15);
}
/// <summary>
/// 获取界面上的配方参数
/// </summary>
/// <returns>获取到的配方参数值</returns>
private ZxRecipeParaEntity GetParaValue()
{
ZxRecipeParaEntity entity = new ZxRecipeParaEntity()
{
E1P1 = GeneralUtils.StringNullOrToInt(E1P1TextBox.Text),
E1P2 = GeneralUtils.StringNullOrToInt(E1P2TextBox.Text),
E1P3 = GeneralUtils.StringNullOrToInt(E1P3TextBox.Text),
E1P4 = GeneralUtils.StringNullOrToInt(E1P4TextBox.Text),
E2P1 = GeneralUtils.StringNullOrToInt(E2P1TextBox.Text),
E2P2 = GeneralUtils.StringNullOrToInt(E2P2TextBox.Text),
E2P3 = GeneralUtils.StringNullOrToInt(E2P3TextBox.Text),
E2P4 = GeneralUtils.StringNullOrToInt(E2P4TextBox.Text),
E3P1 = GeneralUtils.StringNullOrToInt(E3P1TextBox.Text),
E3P2 = GeneralUtils.StringNullOrToInt(E3P2TextBox.Text),
E3P3 = GeneralUtils.StringNullOrToInt(E3P3TextBox.Text),
E3P4 = GeneralUtils.StringNullOrToInt(E3P4TextBox.Text),
E3P5 = E3P5CheckBox.Checked ? 1 : 0,
E3P6 = GeneralUtils.StringNullOrToInt(E3P6TextBox.Text),
E3P7 = GeneralUtils.StringNullOrToInt(E3P7TextBox.Text),
E3P8 = GeneralUtils.StringNullOrToInt(E3P8TextBox.Text),
E3P9 = GeneralUtils.StringNullOrToInt(E3P9TextBox.Text),
E3P10 = GeneralUtils.StringNullOrToInt(E3P10TextBox.Text),
E3P11 = GeneralUtils.StringNullOrToInt(E3P11TextBox.Text),
E3P12 = GeneralUtils.StringNullOrToInt(E3P12TextBox.Text),
E3P13 = GeneralUtils.StringNullOrToInt(E3P13TextBox.Text),
E3P14 = GeneralUtils.StringNullOrToInt(E3P14TextBox.Text),
E3P15 = GeneralUtils.StringNullOrToInt(E3P15TextBox.Text),
};
return entity;
}
}
}