using Module.Plc.ModbusTcp.Tool;
using RS.BLL;
using RS.Common;
using RS.Model;
using RS.Module.Common;
using Sunny.UI;
using System;
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 MorClient.UserFrmFrame
{
public partial class FrmFlushingFormula : Form
{
///
/// 鼠标所在行数
///
int rowLocate = -1;
///
/// 能否新增
///
bool canAdd = true;
///
/// 能否修改
///
bool isUpdate = false;
///
/// SCR1-4
///
int state = 0;
///
/// 获取数据
///
DataTable dtResult = new DataTable();
DataGridViewHandle gridHdl = new DataGridViewHandle();
BRecipeWoekManage bRecipeWoekManage = new BRecipeWoekManage();
public FrmFlushingFormula(int STATE)
{
InitializeComponent();
CreateNewRecipe();
state = STATE;
}
///
/// 取消
///
///
///
private void uiButton3_Click(object sender, EventArgs e)
{
this.Close();
}
///
/// 新增
///
///
///
private void uiButton1_Click(object sender, EventArgs e)
{
if (canAdd)
{
canAdd = false;
isUpdate = true;
FlushRecipeArm flushRecipeArm = new FlushRecipeArm();
flushRecipeArm.flush_recipe_name = "";
flushRecipeArm.flush_location_one = Common.commonClass_scr2_flush.F_Position_M8;
flushRecipeArm.flush_location_two = Common.commonClass_scr2_flush.F_Position_M8;
flushRecipeArm.flush_speed = 0;
flushRecipeArm.flush_type = state.ToString();
if (dtResult.Rows.Count > 0)
dtResult.Rows.Add(flushRecipeArm.flush_recipe_name, flushRecipeArm.flush_location_one, flushRecipeArm.flush_location_two, flushRecipeArm.flush_speed);
else
dtResult.Rows.Add(flushRecipeArm.flush_location_one, flushRecipeArm.flush_location_two, flushRecipeArm.flush_speed, flushRecipeArm.flush_recipe_name,flushRecipeArm.flush_type);
dataGridView.DataSource = dtResult;
gridHdl._dbgrid = dataGridView;
gridHdl.GenerateBaseDataGridView();
dataGridView.ReadOnly = false;
rowLocate = dataGridView.Rows.Count;
}
else
{
MessageBox.Show("请先保存再新增");
dataGridView.ReadOnly = true;
}
}
///
/// 删除
///
///
///
private void uiButton2_Click(object sender, EventArgs e)
{
if (dataGridView.DataSource != null && dataGridView.Rows.Count > 0)
{
DialogResult dgRst = MessageBox.Show(this, "您确认删除配方吗?", "警告", MessageBoxButtons.OKCancel);
if (dgRst == DialogResult.OK)
{
try
{
// 获取第一个选定单元格
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
// 获取包含该单元格的行
DataGridViewRow selectedRow = selectedCell.OwningRow;
// 获取行索引
int rowIndex = selectedRow.Index;
if (rowIndex >= 0 && rowIndex < dataGridView.RowCount)
{
if (!canAdd && rowLocate == dataGridView.RowCount)
{
dataGridView.Rows.RemoveAt(rowIndex);
rowLocate = 0;
}
else
{
DataTable data = bRecipeWoekManage.GetFluRecipeScr(state);
string oldRecipeName = data.Rows[rowIndex][0].ToString();
if (!bRecipeWoekManage.whetherDelete("SCR" + state, "flushing_arm", oldRecipeName))
{
//数据库删除
DataGridViewRow dataGridViewRow = dataGridView.Rows[rowIndex] as DataGridViewRow;
//string recipeName = dataGridViewRow.Cells[0].Value.ToString();
bool result = bRecipeWoekManage.DeleteFlushRecipeArmInfo(oldRecipeName);
//页面删除
dataGridView.Rows.RemoveAt(rowIndex);
if (result)
{
//MessageBox.Show("删除成功");
GetDataSource();
}
else
{
MessageBox.Show("删除失败");
}
}
else
{
MessageBox.Show("当前配方已选择,禁止删除");
}
}
}
else
{
MessageBox.Show("请选择要删除的行");
}
}
catch
{
MessageBox.Show("请选择配方");
}
}
}
else
{
MessageBox.Show("表中无数据,请先添加数据");
}
}
///
/// 修改
///
///
///
private void uiButton4_Click(object sender, EventArgs e)
{
dataGridView.ReadOnly = false;
if (dataGridView.DataSource != null && dataGridView.Rows.Count > 0)
{
DialogResult dgRst = MessageBox.Show(this, "您确认修改配方吗?", "警告", MessageBoxButtons.OKCancel);
if (dgRst == DialogResult.OK)
{
isUpdate = true;
}
}
else
{
MessageBox.Show("表中无数据,请先添加数据");
}
}
///
/// 保存
///
///
///
private void uiButton5_Click(object sender, EventArgs e)
{
if (isUpdate)
{
// 假设你的DataGridView控件名为dataGridView1
if (dataGridView.SelectedCells.Count > 0)
{
try
{
// 获取第一个选定单元格
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
// 获取包含该单元格的行
DataGridViewRow selectedRow = selectedCell.OwningRow;
// 获取行索引
int rowIndex = selectedRow.Index;
if (rowIndex >= 0 && rowIndex < dataGridView.RowCount)
{
List strings = new List();
//判断有没有重复的
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
string flush_recipe_name = dataGridView.Rows[i].Cells[0].Value.ToString();
if (!string.IsNullOrEmpty(flush_recipe_name))
{
if (!strings.Contains(flush_recipe_name))
{
strings.Add(flush_recipe_name);
}
else
{
MessageBox.Show("配方名重复");
return;
}
}
else
{
MessageBox.Show("配方名不能为空");
return;
}
}
DataGridViewRow dataGridViewRow = dataGridView.Rows[rowIndex] as DataGridViewRow;
FlushRecipeArm flushRecipeArm = new FlushRecipeArm();
flushRecipeArm.flush_recipe_name = dataGridViewRow.Cells[0].Value.ToString().Replace(" ", "");
flushRecipeArm.flush_location_one = Convert.ToSingle(dataGridViewRow.Cells[1].Value.ToString());
flushRecipeArm.flush_location_two = Convert.ToSingle(dataGridViewRow.Cells[2].Value.ToString());
flushRecipeArm.flush_speed = Convert.ToSingle(dataGridViewRow.Cells[3].Value.ToString());
flushRecipeArm.flush_type = state.ToString();
if (float.TryParse(dataGridViewRow.Cells[1].Value.ToString(), out float scrub_position_one) && float.TryParse(dataGridViewRow.Cells[2].Value.ToString(), out float scrub_position_two))
{
if (!canAdd && rowLocate == dataGridView.RowCount)
{
//这里是编辑新增的
if (!bRecipeWoekManage.whetherRepeat(flushRecipeArm.flush_recipe_name, flushRecipeArm.flush_type))//不重复
{
bool result = bRecipeWoekManage.SaveFlushRecipeArmInfo(flushRecipeArm);
if (result)
{
canAdd = true;
MessageBox.Show("保存成功");
GetDataSource();
}
else
{
MessageBox.Show("保存失败");
}
}
else
{
MessageBox.Show("配方名重复");
}
}
else
{
//不用dtresult是因为不知道什么原因这个数据和控件中数据源一样了
DataTable dataTable = bRecipeWoekManage.GetFluRecipeScr(state);
string old_flush_recipe_name = dataTable.Rows[rowIndex]["flush_recipe_name"].ToString();
if (!bRecipeWoekManage.whetherDelete("SCR" + state, "flushing_arm", old_flush_recipe_name))
{
bool result = bRecipeWoekManage.UpdateFlushRecipeArm(flushRecipeArm, old_flush_recipe_name);
if (result)
{
canAdd = true;
MessageBox.Show("保存成功");
GetDataSource();
}
else
{
MessageBox.Show("保存失败");
}
}
else
{
string msg = "保存成功";
if (flushRecipeArm.flush_recipe_name != old_flush_recipe_name)
msg = "名字已还原,数据保存成功";
flushRecipeArm.flush_recipe_name = old_flush_recipe_name;
bool result = bRecipeWoekManage.UpdateFlushRecipeArm(flushRecipeArm, old_flush_recipe_name);
if (result)
{
canAdd = true;
MessageBox.Show(msg);
GetDataSource();
}
else
{
MessageBox.Show("保存失败");
}
}
}
}
else
{
MessageBox.Show("位置1或位置2数据无效");
}
}
else
{
MessageBox.Show("请选择要修改的行");
}
}
catch
{
MessageBox.Show("请选择单元格");
}
}
else
{
// 如果没有选定任何单元格,则显示消息
MessageBox.Show("没有选定任何单元格。");
}
}
else
{
MessageBox.Show("请先点击修改");
}
dataGridView.ReadOnly = true;
}
#region DataGridView
///
/// 加载数据
///
/// 配方名
public void CreateNewRecipe()
{
#region
DataGridViewTextBoxColumn textBoxColumn1 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn textBoxColumn2 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn textBoxColumn3 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn textBoxColumn4 = new DataGridViewTextBoxColumn();
// 如果需要绑定到数据源的属性,请设置此属性
textBoxColumn1.DataPropertyName = "flush_recipe_name";
textBoxColumn2.DataPropertyName = "flush_location_one";
textBoxColumn3.DataPropertyName = "flush_location_two";
textBoxColumn4.DataPropertyName = "flush_speed";
// 显示为带有一位位小数的浮点数
textBoxColumn2.DefaultCellStyle.Format = "N2";
textBoxColumn3.DefaultCellStyle.Format = "N2";
textBoxColumn4.DefaultCellStyle.Format = "N2";
// 将列添加到 DataGridView 的 Columns 集合中
dataGridView.Columns.Add(textBoxColumn1);
dataGridView.Columns.Add(textBoxColumn2);
dataGridView.Columns.Add(textBoxColumn3);
dataGridView.Columns.Add(textBoxColumn4);
//设置显示列名称
dataGridView.Columns[0].HeaderText = "配方名称";
dataGridView.Columns[1].HeaderText = "位置1";
dataGridView.Columns[2].HeaderText = "位置2";
dataGridView.Columns[3].HeaderText = "速度";
#endregion
gridHdl._dbgrid = dataGridView;
gridHdl.GenerateBaseDataGridView();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
}
///
/// 数据源 根据配方名 查询所有步骤
///
///
public void GetDataSource()
{
dtResult = bRecipeWoekManage.GetFluRecipeScr(state);
if (dtResult == null || dtResult.Rows.Count <= 0)
{
dtResult=new DataTable();
dtResult.Columns.Add("flush_location_one", typeof(double));
dtResult.Columns.Add("flush_location_two", typeof(double));
dtResult.Columns.Add("flush_speed", typeof(double));
dtResult.Columns.Add("flush_recipe_name", typeof(string));
dtResult.Columns.Add("flush_type", typeof(string));
}
dataGridView.DataSource = dtResult;
}
#endregion DataGridView
private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
private void FrmFlushingFormula_Load(object sender, EventArgs e)
{
if (PublicParams.currUserInfo.userGroup == "Operator")
{
this.Enabled = false;
}
else
{
this.Enabled = true;
}
}
}
}