using RS.BLL;
using RS.Common;
using RS.Model;
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;
using MorClient.UserFrmFrame;
using Sunny.UI;
using static System.Windows.Forms.AxHost;
using RS.Module.Common;
namespace MorClient
{
public partial class Formulabatch : UserControl
{
///
/// 能否新增
///
bool canAdd = true;
///
/// 鼠标所在行数
///
static int rowLocate = -1;
bool isUpdate = false;
DataGridViewHandle gridHdl = new DataGridViewHandle();
DataTable dtResult = new DataTable();
public static bool isClose = false;
BRecipeWoekManage bRecipeWoekManage = new BRecipeWoekManage();
static RecipeWork recipeWorkObj = new RecipeWork();
public Formulabatch()
{
InitializeComponent();
CreateNewRecipe();
}
public void IniLoadCobData(string str, UIComboBox uIComboBox)
{
int length = 0;
List listString = bRecipeWoekManage.GetWorkMachineRecipeScrInfo(str, out length);
for (int i = 0; i < length; i++)
{
uIComboBox.Items.Add(listString[i]);
}
}
public List DataGridViewIniLoadCobData(string str/*, UIComboBox uIComboBox*/)
{
int length = 0;
List listString = bRecipeWoekManage.GetWorkMachineRecipeScrInfo(str, out length);
return listString;
}
#region 按钮
///
/// 新增
///
///
///
private void uiButton1_Click_1(object sender, EventArgs e)
{
if (PublicParams.currUserInfo.userGroup == "Operator")
{
MessageBox.Show("权限不足");
return;
}
if (canAdd)
{
canAdd = false;
isUpdate = true;
recipeWorkObj.workRecipeName = null;
recipeWorkObj.scrOneRecipeName = null;
recipeWorkObj.scrTwoRecipeName = null;
recipeWorkObj.scrTreRecipeName = null;
recipeWorkObj.scrFouRecipeName = null;
recipeWorkObj.workCreator = null;
recipeWorkObj.workTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
dtResult.Rows.Add(null, null, null, null, null, null, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
dataGridView.DataSource = dtResult;
gridHdl._dbgrid = dataGridView;
gridHdl.GenerateBaseDataGridView();
//保存完设置只读
dataGridView.ReadOnly = false;
rowLocate = dataGridView.Rows.Count - 1;
}
}
///
/// 删除
///
///
///
private void uiButton4_Click(object sender, EventArgs e)
{
if (PublicParams.currUserInfo.userGroup == "Operator")
{
MessageBox.Show("权限不足");
return;
}
if (rowLocate != -1)
{
if (dataGridView.DataSource != null && dataGridView.Rows.Count > 0)
{
DialogResult dgRst = MessageBox.Show(this, "您确认删除配方吗?", "警告", MessageBoxButtons.OKCancel);
if (dgRst == DialogResult.OK)
{
if (rowLocate >= 0 && rowLocate < dataGridView.RowCount)
{
//数据库删除
DataGridViewRow dataGridViewRow = dataGridView.Rows[rowLocate] as DataGridViewRow;
string recipeName = dataGridViewRow.Cells[0].Value.ToString();
if (recipeName == publicProgram.scrP1 || recipeName == publicProgram.scrP2)
{
MessageBox.Show("配方已选择,禁止删除");
return;
}
bool result = bRecipeWoekManage.DelWorkRecipeInfo(recipeName);
//页面删除
dataGridView.Rows.RemoveAt(rowLocate);
if (result)
{
//MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
else
{
MessageBox.Show("请选择要删除的行");
}
}
}
else
{
MessageBox.Show("表中无数据,请先添加数据");
}
}
else
{
MessageBox.Show("请先选择删除的配方");
}
rowLocate = -1;
}
///
/// 修改
///
///
///
private void uiButton3_Click_1(object sender, EventArgs e)
{
if (PublicParams.currUserInfo.userGroup == "Operator")
{
MessageBox.Show("权限不足");
return;
}
if (dataGridView.DataSource != null)
{
if (dataGridView.RowCount > 0)
{
try
{
// 获取第一个选定单元格
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
// 获取包含该单元格的行
DataGridViewRow selectedRow = selectedCell.OwningRow;
// 获取行索引
int rowIndex = selectedRow.Index;
dataGridView.ReadOnly = false;
isUpdate = true;
for (int i = 0; i < dataGridView.RowCount; i++)
{
if (i != rowIndex)
dataGridView.Rows[i].ReadOnly = true;
else
dataGridView.Rows[i].ReadOnly = false;
}
}
catch
{
MessageBox.Show("请选择配方");
}
}
}
}
///
/// 保存
///
///
///
private void uiButton5_Click(object sender, EventArgs e)
{
if (PublicParams.currUserInfo.userGroup == "Operator")
{
MessageBox.Show("权限不足");
return;
}
if (isUpdate)
{
// 假设你的DataGridView控件名为dataGridView1
if (dataGridView.SelectedCells.Count > 0)
{
// 获取第一个选定单元格
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;
RecipeWork recipeWork = new RecipeWork();
recipeWork.workRecipeName = dataGridViewRow.Cells[0].Value.ToString();
recipeWork.scrOneRecipeName = dataGridViewRow.Cells[1].Value.ToString();
recipeWork.scrTwoRecipeName = dataGridViewRow.Cells[2].Value.ToString();
recipeWork.scrTreRecipeName = dataGridViewRow.Cells[3].Value.ToString();
recipeWork.scrFouRecipeName = dataGridViewRow.Cells[4].Value.ToString();
recipeWork.workCreator = dataGridViewRow.Cells[5].Value.ToString();
recipeWork.workTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//dataGridViewRow.Cells[6].Value.ToString();
if (!canAdd && rowLocate == dataGridView.RowCount - 1)
{
if (!bRecipeWoekManage.whetherExist(recipeWork.workRecipeName))
{
bool result = bRecipeWoekManage.SavePecipeWork(recipeWork);
if (result)
{
canAdd = true;
MessageBox.Show("保存成功");
GetDataSource();
}
else
{
MessageBox.Show("保存失败");
}
}
else
{
MessageBox.Show("配方名重复");
}
}
else
{
//不用dtresult是因为不知道什么原因这个数据和控件中数据源一样了
DataTable dataTable = bRecipeWoekManage.GetRecipeWork();
string old_work_recipe_name = dataTable.Rows[rowIndex]["work_recipe_name"].ToString();
if (old_work_recipe_name == publicProgram.scrP1 || old_work_recipe_name == publicProgram.scrP2)
{
MessageBox.Show("作业配方已选择,不能更改名称");
dataGridView.Rows[rowIndex].Cells[0].Value = old_work_recipe_name;
}
else
{
bool result = bRecipeWoekManage.UpdateWorkRecipe(recipeWork, old_work_recipe_name);
if (result)
{
canAdd = true;
MessageBox.Show("保存成功");
GetDataSource();
}
else
{
MessageBox.Show("保存失败");
}
}
}
}
else
{
MessageBox.Show("请选择要修改的行");
}
}
}
else
{
MessageBox.Show("请选择修改的数据");
}
dataGridView.ReadOnly = true;
}
#endregion
#region DataGridView
///
/// 加载数据
///
/// 配方名
public void CreateNewRecipe()
{
#region
DataGridViewTextBoxColumn textBoxColumn1 = new DataGridViewTextBoxColumn();
DataGridViewComboBoxColumn comboBoxColumn2 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn comboBoxColumn3 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn comboBoxColumn4 = new DataGridViewComboBoxColumn();
DataGridViewComboBoxColumn comboBoxColumn5 = new DataGridViewComboBoxColumn();
DataGridViewTextBoxColumn textBoxColumn5 = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn textBoxColumn6 = new DataGridViewTextBoxColumn();
// 如果需要绑定到数据源的属性,请设置此属性
textBoxColumn1.DataPropertyName = "work_recipe_name";
comboBoxColumn2.DataPropertyName = "scr_one_recipe_name";
comboBoxColumn3.DataPropertyName = "scr_two_recipe_name";
comboBoxColumn4.DataPropertyName = "scr_tre_recipe_name";
comboBoxColumn5.DataPropertyName = "scr_fou_recipe_name";
textBoxColumn5.DataPropertyName = "work_createtor";
textBoxColumn6.DataPropertyName = "work_time";
// 将列添加到 DataGridView 的 Columns 集合中
dataGridView.Columns.Add(textBoxColumn1);
dataGridView.Columns.Add(comboBoxColumn2);
dataGridView.Columns.Add(comboBoxColumn3);
dataGridView.Columns.Add(comboBoxColumn4);
dataGridView.Columns.Add(comboBoxColumn5);
dataGridView.Columns.Add(textBoxColumn5);
dataGridView.Columns.Add(textBoxColumn6);
List stingScr1 = DataGridViewIniLoadCobData("SCR1"/*, cobScr1*/);
List stingScr2 = DataGridViewIniLoadCobData("SCR2"/*, cobScr2*/);
List stingScr3 = DataGridViewIniLoadCobData("SCR3"/*, cobScr3*/);
List stingScr4 = DataGridViewIniLoadCobData("SCR4"/*, cobScr4*/);
stingScr1.Add("");
stingScr2.Add("");
stingScr3.Add("");
stingScr4.Add("");
comboBoxColumn2.DataSource = stingScr1;
comboBoxColumn3.DataSource = stingScr2;
comboBoxColumn4.DataSource = stingScr3;
comboBoxColumn5.DataSource = stingScr4;
//设置显示列名称
dataGridView.Columns[0].HeaderText = "作业配方名称";
dataGridView.Columns[1].HeaderText = "SCR1配方名称";
dataGridView.Columns[2].HeaderText = "SCR2配方名称";
dataGridView.Columns[3].HeaderText = "SCR3配方名称";
dataGridView.Columns[4].HeaderText = "SCR4配方名称";
dataGridView.Columns[5].HeaderText = "创建人";
dataGridView.Columns[6].HeaderText = "创建时间";
#endregion
gridHdl._dbgrid = dataGridView;
gridHdl.GenerateBaseDataGridView();
foreach (DataGridViewColumn column in dataGridView.Columns)
{
column.SortMode = DataGridViewColumnSortMode.NotSortable;
}
dataGridView.ReadOnly = true;
}
///
/// 数据源 根据配方名 查询所有步骤
///
///
public void GetDataSource()
{
dtResult = bRecipeWoekManage.GetRecipeWork();
dataGridView.DataSource = dtResult;
}
#endregion DataGridView
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// 假设你的DataGridView控件名为dataGridView1
if (dataGridView.SelectedCells.Count > 0)
{
// 获取第一个选定单元格
DataGridViewCell selectedCell = dataGridView.SelectedCells[0];
// 获取包含该单元格的行
DataGridViewRow selectedRow = selectedCell.OwningRow;
// 获取行索引
rowLocate = selectedRow.Index;
}
}
}
}