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.

563 lines
20 KiB
C#

1 year ago
using DevExpress.Office.Utils;
using ICSharpCode.Core;
using Mesnac.Action.ChemicalWeighing.Entity;
using Mesnac.Action.ChemicalWeighing.Entity.material;
using Mesnac.Action.ChemicalWeighing.Entity.station;
using Mesnac.Action.ChemicalWeighing.XlPlcHelper;
using Mesnac.Controls.Default;
using Mesnac.Equips.BaseInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Mesnac.Action.ChemicalWeighing.Station
{
public partial class FrmStation : Form
{
#region 字段定义
private ActionType _actionType = ActionType.Add; //操作类型0-为添加1-为修改
private string _curStationID =string.Empty;
private xl_station xlstation = null;
private List<xl_station_sub> xlstationList; //待写入数据库的工位信息List
#endregion
#region 属性定义
public xl_station xl_Recipe { get => xl_Recipe; set => xl_Recipe = value; } //确定后要插入到数据库中供Action使用
#endregion
#region 构造方法
public FrmStation()
{
InitializeComponent();
}
/// <summary>
/// 构造方法
/// </summary>
/// <param name="actionType">操作类型0-为添加1-为修改</param>
public FrmStation(ActionType actionType)
{
InitializeComponent();
this._actionType = actionType;
xlstation=new xl_station() { ID=Guid.NewGuid().ToString()};
xlstationList = new List<xl_station_sub>();
}
/// <summary>
/// 构造方法
/// </summary>
/// <param name="actionType">操作类型0-为添加1-为修改</param>
public FrmStation(ActionType actionType, xl_station station)
{
InitializeComponent();
this._actionType = actionType;
xlstation = StationHelper.GetStation(station.ID);
xlstationList = StationHelper.GetStationSubList(station.ID);
}
#endregion
#region 方法定义
/// <summary>
/// 初始化界面文本
/// </summary>
public void InitUI()
{
if (this._actionType == ActionType.Add)
{
}
else if (this._actionType == ActionType.Modify)
{
txtStationName.Text = xlstation.StationName;
}
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)
{
//先添加好空行
if (xlstationList != null && xlstationList.Count > 0)
{
for (int i = 0; i < xlstationList.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 = xlstationList[i].StationSubName;
}
else if (j == 1)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = xlstationList[i].Station_Weight_Medium;
}
else if (j == 2)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = xlstationList[i].Station_Weight_Low;
}
else if (j == 3)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = xlstationList[i].Station_Weight_Advance;
}
else if (j == 4)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = xlstationList[i].Station_Speed_Hight;
}
else if (j == 5)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = xlstationList[i].Station_Speed_Medium;
}
else if (j == 6)
{
this.DGVMaterialSet.Rows[i].Cells[j].Value = xlstationList[i].Station_Speed_Low;
}
}
}
}
}
}
/// <summary>
/// 填充工位详情信息List
/// </summary>
public void GetPmtWeightList()
{
xlstationList.Clear();
for (int i = 0; i < DGVMaterialSet.Rows.Count; i++)
{
xl_station_sub xlstationsub = new xl_station_sub();
for (int j = 0; j < DGVMaterialSet.ColumnCount; j++)
{
if (j == 0)
{
xlstationsub.StationSubName = DGVMaterialSet.Rows[i].Cells[j].Value.ToString();
}
else if (j == 1)
{
xlstationsub.Station_Weight_Medium = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
else if (j == 2)
{
xlstationsub.Station_Weight_Low = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
else if (j == 3)
{
xlstationsub.Station_Weight_Advance = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
else if (j == 4)
{
xlstationsub.Station_Speed_Hight = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
else if (j == 5)
{
xlstationsub.Station_Speed_Medium = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
else if (j == 6)
{
xlstationsub.Station_Speed_Low = Convert.ToDouble(DGVMaterialSet.Rows[i].Cells[j].Value.ToString());
}
}
xlstationList.Add(xlstationsub);
}
}
/// <summary>
/// 向数据库中插入新工位数据
/// </summary>
public void NewStationAdd()
{
xlstation.StationName= this.txtStationName.Text;
xlstation.End_datetime=DateTime.Now;
_curStationID = StationHelper.InsertStation(xlstation);
}
#region 工位信息插入数据库
/// <summary>
/// 工位信息插入数据库
/// </summary>
public void NewSubAdd()
{
if (xlstationList != null && xlstationList.Count > 0 && _curStationID != null)
{
foreach (xl_station_sub tempStationSub in xlstationList)
{
tempStationSub.MainId = _curStationID;
StationHelper.InsertStationSub(tempStationSub);
PlcPlanHelper.WriteDeviceParam(tempStationSub);//更新PLC
}
}
}
#endregion
/// <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.InitUI();
this.InitData();
}
private void FrmPlan_Activated(object sender, EventArgs e)
{
if (this._actionType == ActionType.Modify)
{
this.txtStationName.Focus();
}
else
{
this.txtStationName.Focus();
}
}
private void btnOk_Click(object sender, EventArgs e)
{
#region 信息验证
if (String.IsNullOrEmpty(this.txtStationName.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;
}
}
#endregion
#region 计算TotalWeight和TotalError
//GetTotalWeightErrorValue();
#endregion
#region 填充物料信息List
GetPmtWeightList();
#endregion
#region 配方及物料信息写入数据库
if (_actionType == ActionType.Add)
{
this.NewStationAdd(); //新工位数据插入数据库
this.NewSubAdd(); //工位信息插入数据库
}
else if (_actionType == ActionType.Modify)
{
//this.NewRecipeAdd(); //更新配方数据到数据库
//this.WeighDelByRecipeID(pmt_Recipe.ID); //清空配方对应的物料信息
this.UpdateStation(); //配方对应的物料信息重新插入数据库
}
#endregion
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
private void UpdateStation()
{
try
{
xlstation.StationName = this.txtStationName.Text;
StationHelper.UpdateStationData(xlstation);
List<xl_station_sub> list = new List<xl_station_sub>();
foreach (DataGridViewRow rows in DGVMaterialSet.Rows)
{
xl_station_sub stu = new xl_station_sub();
stu.MainId = xlstation.ID;
stu.StationSubName = rows.Cells[0].Value.ToString();
stu.Station_Weight_Medium = Convert.ToDouble(rows.Cells[1].Value.ToString());
stu.Station_Weight_Low = Convert.ToDouble(rows.Cells[2].Value.ToString());
stu.Station_Weight_Advance = Convert.ToDouble(rows.Cells[3].Value.ToString());
stu.Station_Speed_Hight = Convert.ToDouble(rows.Cells[4].Value.ToString());
stu.Station_Speed_Medium = Convert.ToDouble(rows.Cells[5].Value.ToString());
stu.Station_Speed_Low = Convert.ToDouble(rows.Cells[6].Value.ToString());
stu.CreateDateTime = DateTime.Now;
list.Add(stu);
}
StationHelper.DelStationSubData(xlstation.ID);
foreach (xl_station_sub tempStationSub in list)
{
tempStationSub.MainId = xlstation.ID;
StationHelper.InsertStationSub(tempStationSub);
PlcPlanHelper.WriteDeviceParam(tempStationSub);//更新PLC
}
}
catch (Exception)
{
throw;
}
}
private void DGVMaterialSet_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
this.DGVMaterialSet.Refresh();
int newRowNum = this.DGVMaterialSet.RowCount;
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
private void cmbRecipeMaterial_SelectedIndexChanged(object sender, EventArgs e)
{
//this.InitRecipeVersionData();
//this.InitMaterialData();
}
#region 初始化选中的配方版本数据
/// <summary>
/// 初始化选中的配方版本数据
/// </summary>
//public void InitRecipeVersionData()
//{
// if (this.cmbRecipeMaterial.SelectedItem != null)
// {
// SimplePmtRecipe recipe = this.cmbRecipeMaterial.SelectedItem as SimplePmtRecipe;
// if (recipe != null)
// {
// List<string> recipeVersionList = TechnicalHelper.GetRecipeVersionList(recipe.ID);
// this.cmbRecipeVersion.DataSource = recipeVersionList;
// }
// }
//}
#endregion
#region 初始化选中的物料数据
/// <summary>
/// 初始化选中的配方版本数据
/// </summary>
//public void InitMaterialData()
//{
// if (this.cmbRecipeMaterial.SelectedItem != null)
// {
// SimplePmtRecipe recipe = this.cmbRecipeMaterial.SelectedItem as SimplePmtRecipe;
// List<Material> materialList = StationHelper.GetMaterialList(recipe.ID);
// //this.cmbMaterial.Clear();
// //foreach (var r in materialList)
// //{
// // this.cmbMaterial.Items.Add(r);
// // cmbMaterial.DisplayMember = "Material_name";
// // cmbMaterial.ValueMember = "Material_ID";
// //}
// this.cmbMaterial.DataSource = materialList;
// this.cmbMaterial.DisplayMember = "Material_name";
// this.cmbMaterial.ValueMember = "Material_ID";
// }
//}
#endregion
}
/// <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;
}
}
public class DataGridViewEditTextBoxColumn: DataGridViewTextBoxColumn
{
public DataGridViewEditTextBoxColumn()
{
DataGridViewComboEditBoxCell obj = new DataGridViewComboEditBoxCell();
this.CellTemplate = obj;
}
}
}