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.

585 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using Mesnac.Action.Base;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.Core;
using Mesnac.Action.ChemicalWeighing.Entity;
using Mesnac.Action.ChemicalWeighing.Technical;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe;
using System.Configuration;
using Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe.entity;
namespace Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe
{
public partial class FrmRecipe : Form
{
#region 字段定义
private ActionType _actionType = ActionType.Add; //操作类型0-为添加1-为修改
private string _recipeName = null;
private int _mixerNum = 0;
private int _groutBags = 0;
private string _remark = null;
private List<Base_MaterialInfo> pmt_Materials = new List<Base_MaterialInfo>(); //所有物料集合
private decimal _totalWeight = 0M;
private decimal _totalError = 0M;
private string _curRecipeID = null;
private List<Pmt_weigh> pmt_Weighs = null; //待写入数据库的配方物料信息List
private List<Base_RecipeMaterial> base_RecipeMaterials = null;
private Base_RecipeInfo modifyRecipeInfo = null;
#endregion
#region 属性定义
public string RecipeName { get => _recipeName; set => _recipeName = value; }
public int MixerNum { get => _mixerNum; set => _mixerNum = value; }
public int GroutBags { get => _groutBags; set => _groutBags = value; }
public string Remark { get => _remark; set => _remark = value; }
public List<Base_MaterialInfo> Pmt_Materials { get => pmt_Materials; set => pmt_Materials = value; }
public decimal TotalWeight { get => _totalWeight; set => _totalWeight = value; }
public decimal TotalError { get => _totalError; set => _totalError = value; }
public string CurRecipeID { get => _curRecipeID; set => _curRecipeID = value; }
#endregion
#region 构造方法
public FrmRecipe()
{
InitializeComponent();
}
/// <summary>
/// 构造方法
/// </summary>
/// <param name="actionType">操作类型0-为添加1-为修改</param>
public FrmRecipe(ActionType actionType)
{
InitializeComponent();
this._actionType = actionType;
base_RecipeMaterials = new List<Base_RecipeMaterial>();
}
/// <summary>
/// 修改构造方法
/// </summary>
/// <param name="actionType">操作类型0-为添加1-为修改</param>
public FrmRecipe(ActionType actionType,Base_RecipeInfo recipeInfo)
{
InitializeComponent();
this._actionType = actionType;
modifyRecipeInfo = recipeInfo;
base_RecipeMaterials = new List<Base_RecipeMaterial>();
}
#endregion
#region 方法定义
/// <summary>
/// 物料名称集合获取
/// </summary>
public void InitCombox()
{
pmt_Materials = Product.PptPlan.PlanHelper.GetAllPmt_material();
}
/// <summary>
/// 初始化界面文本
/// </summary>
public void InitUI()
{
if (this._actionType == ActionType.Add)
{
this.Text = "添加新配方";
txtRecipeName.Enabled = true;
}
else if (this._actionType == ActionType.Modify)
{
this.Text = "修改配方";
txtRecipeName.Text = modifyRecipeInfo.recipeName;
txtRecipeName.Enabled = false;
txtRecipeId.Text = modifyRecipeInfo.recipeId;
txtRecipeId.Enabled = false;
txtRecipeType.Text = modifyRecipeInfo.recipeType;
txtRecipeWeight.Text = modifyRecipeInfo.recipeWeight.ToString();
txtRemark.Text = modifyRecipeInfo.remark;
}
else if (this._actionType == ActionType.SaveAs)
{
this.Text = "另存配方";
//txtRecipeName.Text = modifyRecipeInfo.recipeName;
//txtRecipeId.Text = modifyRecipeInfo.recipeId;
txtRecipeType.Text = modifyRecipeInfo.recipeType;
txtRecipeWeight.Text = modifyRecipeInfo.recipeWeight.ToString();
txtRemark.Text = modifyRecipeInfo.remark;
}
this.btnOk.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnOK"));
this.btnCancel.Text = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_btnCancel"));
}
/// <summary>
/// 初始化配方信息
/// </summary>
public void InitData()
{
if (this._actionType == ActionType.Add)
{
this.DGVMaterialSet.Rows.Add();
}
if (this._actionType == ActionType.Modify || this._actionType == ActionType.SaveAs)
{
//根据配方编号获取关联物料信息
DataTable dataTable = RecipeHelper.GetRecipeMaterialInfo(modifyRecipeInfo.recipeId);
//先添加好空行
if (dataTable != null && dataTable.Rows.Count > 0)
{
for(int i = 0;i< dataTable.Rows.Count; i++)
{
this.DGVMaterialSet.Rows.Add();
//再将数据填入
for(int j = 0;j< this.DGVMaterialSet.ColumnCount; j++)
{
if(j == 0)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = dataTable.Rows[i][3].ToString();
}
else if(j == 1)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = dataTable.Rows[i][4].ToString();
}
else if (j == 2)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = dataTable.Rows[i][5].ToString();
}
}
}
}
}
}
/// <summary>
/// 填充物料信息List
/// </summary>
public void GetPmtWeightList()
{
base_RecipeMaterials.Clear();
for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
{
Base_RecipeMaterial baseRepiceMaterial = new Base_RecipeMaterial();
baseRepiceMaterial.recipeId = txtRecipeId.Text;
for (int j = 0; j < DGVMaterialSet.ColumnCount; j++)
{
if (j == 0)
{
//根据物料名称获取物料ID
string materialNameStr = DGVMaterialSet.Rows[i].Cells[j].Value.ToString();
baseRepiceMaterial.materialName = materialNameStr;
if (pmt_Materials.Exists(x => x.materialName == materialNameStr))
{
baseRepiceMaterial.materialId = GetMaterialID(materialNameStr);
}
}
else if (j == 1)
{
baseRepiceMaterial.materialWeight = Convert.ToDecimal(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
else if (j == 2)
{
baseRepiceMaterial.putTime = Convert.ToInt16(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
}
base_RecipeMaterials.Add(baseRepiceMaterial);
}
}
/// <summary>
/// 根据物料名称查找物料ID
/// </summary>
public string GetMaterialID(string materialName)
{
Base_MaterialInfo findPmt_Material = pmt_Materials.FindLast(x => x.materialName == materialName);
return findPmt_Material.materialId;
}
/// <summary>
/// 根据物料ID查找物料名称
/// </summary>
public string GetMaterialName(string materialID)
{
Base_MaterialInfo findPmt_Material = pmt_Materials.FindLast(x => x.materialId == materialID);
return findPmt_Material.materialName;
}
/// <summary>
/// 计算TotalWeight和TotalError
/// </summary>
public void GetTotalWeightErrorValue()
{
for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
{
for (int j = 1; j < DGVMaterialSet.ColumnCount; j++)
{
if (j == 1)
{
double tempd = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
_totalWeight = _totalWeight + (decimal)tempd;
}
if (j == 2)
{
double tempe = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
_totalError = _totalError + (decimal)tempe;
}
}
}
}
/// <summary>
/// 向数据库中插入新配方数据
/// </summary>
public void NewRecipeAdd()
{
Base_RecipeInfo base_RepiceInfo = new Base_RecipeInfo()
{
recipeName = txtRecipeName.Text,
recipeId = txtRecipeId.Text,
recipeType = txtRecipeType.Text,
recipeWeight = Convert.ToDecimal(txtRecipeWeight.Text),
recipeState = 0,
editUser = "admin",
editTime = DateTime.Now,
remark = txtRemark.Text
};
_curRecipeID = RecipeHelper.InsertRecipe(base_RepiceInfo);
}
/// <summary>
/// 配方物料信息插入数据库
/// </summary>
public void NewWeighAdd()
{
if(base_RecipeMaterials != null && base_RecipeMaterials.Count > 0)
{
foreach(Base_RecipeMaterial tempPmt_Weigh in base_RecipeMaterials)
{
tempPmt_Weigh.editUser = "admin";
RecipeHelper.InsertWeigh(tempPmt_Weigh);
}
}
}
/// <summary>
/// 配方物料信息删除
/// </summary>
public void WeighDelByRecipeID(string recipeID)
{
RecipeHelper.DelWeighByRecipeID(recipeID);
}
/// <summary>
/// 物料名称重复检查
/// </summary>
/// <returns></returns>
private bool DGVMaterialRepeatCheck()
{
bool checkResult = false;
if (DGVMaterialSet.Rows.Count == 1)
{
checkResult = false;
}
else
{
for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
{
if (DGVMaterialSet.Rows[i].Cells[0].Value == null)
{
return true;
}
string material1Name = DGVMaterialSet.Rows[i].Cells[0].Value.ToString();
for (int j = i + 1; j < DGVMaterialSet.Rows.Count; j++)
{
if (DGVMaterialSet.Rows[j].Cells[0].Value == null)
{
return true;
}
if (DGVMaterialSet.Rows[j].Cells[0].Value.ToString() == material1Name)
{
checkResult = true;
break;
}
}
if (checkResult)
{
break;
}
}
}
return checkResult;
}
#endregion
#region 事件处理
private void FrmPlan_Load(object sender, EventArgs e)
{
this.InitCombox();
this.InitUI();
this.InitData();
}
private void FrmPlan_Activated(object sender, EventArgs e)
{
if (this._actionType == ActionType.Modify)
{
this.txtRecipeId.Focus();
}
else
{
this.txtRecipeName.Focus();
}
}
private void btnOk_Click(object sender, EventArgs e)
{
#region 信息验证
if (String.IsNullOrEmpty(this.txtRecipeName.Text))
{
MessageBox.Show("请输入新配方的名称!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.txtRecipeId.Text))
{
MessageBox.Show("请输入新配方的编号!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (String.IsNullOrEmpty(this.txtRecipeType.Text))
{
MessageBox.Show("请输入新配方的类别!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (DGVMaterialSet == null)
{
MessageBox.Show("请设置配方的物料信息!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (DGVMaterialRepeatCheck())
{
MessageBox.Show("物料列表中存在重复物料或者空物料,请修改.", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
if (DGVMaterialSet != null && DGVMaterialSet.Rows.Count > 0)
{
bool errorFlag = false;
for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
{
for (int j = 0; j < DGVMaterialSet.ColumnCount; j++)
{
if (DGVMaterialSet.Rows[i].Cells[j].Value == null)
{
errorFlag = true;
break;
}
}
if (errorFlag)
{
break;
}
}
if (errorFlag)
{
MessageBox.Show("请将物料信息填写完整!或删除无用物料!", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
}
if (_actionType == ActionType.Add && RecipeHelper.IsExistsName(this.txtRecipeName.Text))
{
MessageBox.Show("新配方的名称重复!请使用其他名称", Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
#endregion
#region 计算TotalWeight和TotalError
//GetTotalWeightErrorValue();
#endregion
#region 填充物料信息List
GetPmtWeightList();
#endregion
#region 配方及物料信息写入数据库
if(_actionType == ActionType.Add)
{
this.NewRecipeAdd(); //新配方数据插入数据库
this.NewWeighAdd(); //配方对应的物料信息插入数据库
}
else if (_actionType == ActionType.Modify)
{
this.NewRecipeAdd(); //更新配方数据到数据库
this.WeighDelByRecipeID(txtRecipeId.Text); //清空配方对应的物料信息
this.NewWeighAdd(); //配方对应的物料信息重新插入数据库
}
else if(_actionType == ActionType.SaveAs)
{
this.NewRecipeAdd(); //新配方数据插入数据库
this.NewWeighAdd(); //配方对应的物料信息插入数据库
}
#endregion
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void DGVMaterialSet_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
this.DGVMaterialSet.Refresh();
int newRowNum = this.DGVMaterialSet.RowCount;
if (pmt_Materials != null && pmt_Materials.Count > 0)
{
//(this.DGVMaterialSet.Rows[newRowNum - 1].Cells[0] as DataGridViewComboEditBoxCell).DataSource = pmt_Materials;
foreach (Base_MaterialInfo pmt_Material in pmt_Materials)
{
(this.DGVMaterialSet.Rows[newRowNum - 1].Cells[0] as DataGridViewComboEditBoxCell).Items.Add(pmt_Material.materialName);
}
}
this.DGVMaterialSet.Rows[newRowNum - 1].HeaderCell.Value = "+";
this.DGVMaterialSet.CurrentCell = this.DGVMaterialSet[1, newRowNum - 1];
}
private void DGVMaterialSet_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
this.DGVMaterialSet.Rows.Add();
}
private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
{
this.DGVMaterialSet.Rows.Add();
}
private void toolStripMenuItemDel_Click(object sender, EventArgs e)
{
if(this.DGVMaterialSet.CurrentCell != null)
{
this.DGVMaterialSet.Rows.RemoveAt(this.DGVMaterialSet.CurrentRow.Index);
}
}
#endregion
/// <summary>
/// PLC读写测试
/// </summary>
//public void plcTest()
//{
// try
// {
// int[] res = null;
// var recipeId = Convert.ToInt32(this.txtRecipeId.Text.ToString());
// var writeResult = BasePlcHelper.Instance.PlcWriteByDataKey(BasePlcHelper.Instance.ChemicalWeighing_PLC_LoadingStatus, new object[] { recipeId });
// var readResult = BasePlcHelper.Instance.PlcRead(BasePlcHelper.Instance.ChemicalWeighing_PLC_LoadingStatus, out res);
// int info = res[0];
// this.plcTest1.Text = info.ToString();
// }
// catch (Exception ex)
// {
// Console.WriteLine(ex.Message);
// }
//}
}
/// <summary>
/// 自定义可编辑下拉框单元
/// </summary>
public class DataGridViewComboEditBoxCell : DataGridViewComboBoxCell
{
public override void InitializeEditingControl(int rowIndex, object initialFormattedValue,DataGridViewCellStyle dataGridViewCellStyle)
{
base.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle);
ComboBox comboBox = (ComboBox)base.DataGridView.EditingControl;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox.Validating += new CancelEventHandler(comboBox_Validating);
}
}
protected override object GetFormattedValue(object value, int rowIndex,ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter,TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context)
{
if (value != null && value.ToString().Trim() != string.Empty)
{
if (Items.IndexOf(value) == -1)// 如果下拉框中不存在填入的值,则添加到下拉框中
{
//Items.Add(value);
//// 添加到该列所有单元所绑定的下拉列表中
//DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)OwningColumn;
//col.Items.Add(value);
return null;
}
}
return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);
}
private void comboBox_Validating(object sender, CancelEventArgs e)
{
DataGridViewComboBoxEditingControl cbo = (DataGridViewComboBoxEditingControl)sender;
if (cbo.Text.Trim() == string.Empty)
return;
DataGridView grid = cbo.EditingControlDataGridView;
object value = cbo.Text;
if (cbo.Items.IndexOf(value) == -1)
{
DataGridViewComboBoxColumn cboCol = (DataGridViewComboBoxColumn)grid.Columns[grid.CurrentCell.ColumnIndex];
grid.CurrentCell.Value = value;
}
}
}
public class DataGridViewComboEditBoxColumn : DataGridViewComboBoxColumn
{
public DataGridViewComboEditBoxColumn()
{
DataGridViewComboEditBoxCell obj = new DataGridViewComboEditBoxCell();
this.CellTemplate = obj;
}
}
}