using ComponentFactory.Krypton.Toolkit; using NXWMS.Client.Code.Converter; using NXWMS.Client.Code.Extends; using NXWMS.Client.Code.Models; using NXWMS.Client.Model.AppModels.Condition.Rule; using NXWMS.Client.Model.AppModels.Result; using NXWMS.Client.Model.AppModels.Result.Rule; using NXWMS.Client.Model.CoreModels; using NXWMS.Client.String.Enums; using NXWMS.Commons; using NXWMS.Forms.Balance; using NXWMS.Services; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Web.UI.WebControls; using System.Windows.Forms; using static NXWMS.Client.FrmCustom.ColumnControl; namespace NXWMS.Forms.Rule.frmRuleChild { public partial class frmOperateRule : Form { /// /// 客户端字段排序列表 /// private List _tableFieldOrderResult; /// /// 规则编码 /// public string _ruleCode; /// /// 界面功能类型 /// public EnumFunctionType _FunctionType; /// /// 规则类型 /// public RuleType _RuleType; /// /// 界面功能类型 /// public enum EnumFunctionType { Add, Edit, } /// /// 主 /// public RuleResult _MainInfo; /// /// 子 /// public List _DetailList; /// public frmOperateRule() { InitializeComponent(); this.Load += new System.EventHandler(this.frmOperatePutAwayRule_Load); } private void frmOperatePutAwayRule_Load(object sender, EventArgs e) { InitData(); InitControl(); } private void InitData() { _tableFieldOrderResult = new List(); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "#SELECTED", FieldDesc = "选择", IsReadOnly = true, Width = 100, ColumnType = DataGridViewColumnType.CheckBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "RULE_DTL_ID", FieldDesc = "明细ID", IsReadOnly = false, ColumnType = DataGridViewColumnType.TextBox, IsShow = false, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "RULE_ITEM_CODE", FieldDesc = "规则代码", IsReadOnly = false, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "RULE_ITEM_DESC", FieldDesc = "规则描述", IsReadOnly = false, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "ParamFlagName", FieldDesc = "参数标识", IsReadOnly = true, ColumnType = DataGridViewColumnType.ComboBox, Data = ModelsConvert.ToDataTable( new List() .GetFieldValueIdList() ), ConditionField = "Name", ResultField = "Id" }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "RULE_ITEM_PARAM1", FieldDesc = "规则参数1", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "RULE_ITEM_PARAM2", FieldDesc = "规则参数2", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "RULE_ITEM_PARAM3", FieldDesc = "规则参数3", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "UsedFlagName", FieldDesc = "使用标识", IsReadOnly = false, ColumnType = DataGridViewColumnType.CheckBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "CreateName", FieldDesc = "创建人", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "CREATE_TIME", FieldDesc = "创建时间", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "UpdateName", FieldDesc = "更新人", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "UPDATE_TIME", FieldDesc = "更新时间", IsReadOnly = true, ColumnType = DataGridViewColumnType.TextBox, }); switch (_RuleType) { //订单分配规则 case RuleType.OrderDistribution: kryptonGroupBoxMain.Text = "订单分配规则主表"; kryptonGroupBoxDetail.Text = "订单分配规则明细"; kryptonLabelCode.Text = "订单分配规则编码"; kryptonLabelName.Text = "订单分配规则名称"; //不同结果类型 switch (_FunctionType) { case EnumFunctionType.Add: this.Text = "订单分配规则-新增"; txtRuleCode.Text = _ruleCode; txtRuleCode.Enabled = true; break; case EnumFunctionType.Edit: this.Text = "订单分配规则-编辑"; txtRuleCode.Text = _ruleCode; txtRuleCode.Enabled = false; break; } break; //上架单规则 case RuleType.PutAway: kryptonGroupBoxMain.Text = "上架单规则主表"; kryptonGroupBoxDetail.Text = "上架单规则明细"; kryptonLabelCode.Text = "上架单规则编码"; kryptonLabelName.Text = "上架单规则名称"; //不同结果类型 switch (_FunctionType) { case EnumFunctionType.Add: this.Text = "上架单规则-新增"; txtRuleCode.Text = _ruleCode; txtRuleCode.Enabled = true; break; case EnumFunctionType.Edit: this.Text = "上架单规则-编辑"; txtRuleCode.Text = _ruleCode; txtRuleCode.Enabled = false; break; } break; } } /// /// 控件获取值 /// /// /// /// /// public string ControlGetValueEvent(DataGridViewCell dataGridViewCell, object inData, OperationDataType operationDataType) { if (inData == null) { return string.Empty; } if (string.IsNullOrWhiteSpace(inData.ToString())) { return string.Empty; } //MessageBox.Show(inData.ToString()); return ""; } private void InitControl() { CheckForIllegalCrossThreadCalls = false; dataGridView.CellClick += new DataGridViewCellEventHandler(dataGridView_CellClick); dataGridView.CellValidating += new DataGridViewCellValidatingEventHandler(this.dataGridView_CellValidating); //dataGridView.DataError += new DataGridViewDataErrorEventHandler(this.dataGridView_DataError); //dataGridView.NewRowNeeded += new DataGridViewRowEventHandler(this.dataGridView_NewRowNeeded); dataGridView.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; dataGridView.RowValidating += new DataGridViewCellCancelEventHandler(this.dataGridView_RowValidating); dataGridView.RowHeadersWidth = 200; dataGridView.VirtualMode = false; dataGridView.CausesValidation = true; dataGridView.ShowCellErrors = true; dataGridView.ShowCellToolTips = true; dataGridView.ShowRowErrors = true; dataGridView.ShowEditingIcon = true; dataGridView.ShowCellToolTips = true; dataGridView.BuildDataGridView(_tableFieldOrderResult); //columnControlCode.ControlGetValueEvent += new ControlGetValueHandler(ControlGetValueEvent); //不同结果类型 switch (_RuleType) { //订单分配规则 case RuleType.OrderDistribution: switch (_FunctionType) { case EnumFunctionType.Add: break; case EnumFunctionType.Edit: break; } break; //上架单规则 case RuleType.PutAway: switch (_FunctionType) { case EnumFunctionType.Add: break; case EnumFunctionType.Edit: break; } break; } if (_FunctionType != EnumFunctionType.Add) { //获取对应盘点单数据 var mainResult = RuleServices.putAwayRuleService.GetList(new PutAwayRuleSearchCondition { RuleCode=_ruleCode }); if (mainResult.Status == OperateStatus.Success) { _MainInfo = mainResult.Data.RowData.FirstOrDefault(); txtRuleCode.Text = _MainInfo.RULE_CODE; txtRuleName.Text = _MainInfo.RULE_NAME.ToString(); chkUse.Checked = _MainInfo.USED_FLAG.GetObjectToBoolean(); richDescibe.Text = _MainInfo.DESCRIBE; } else { KryptonMessageBox.Show($"操作失败!\r\n{mainResult.Message}", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } //获取对应盘点子数据 var detailResult = RuleServices.putAwayRuleService.GetDetailList(new PutAwayRuleDetailSearchCondition { RuleId= _MainInfo.RULE_ID, }); if (detailResult.Status == OperateStatus.Success) { _DetailList = detailResult.Data.ToList(); dataGridView.Columns.Clear(); var addRows = detailResult.Data.ToList(); dataGridView.SetDataGridViewData(_tableFieldOrderResult,ModelsConvert.ToDataTable(addRows)); } else { KryptonMessageBox.Show($"操作失败!\r\n{detailResult.Message}", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } /// /// 选择行索引列表 /// private List _CheckRowIndexList = new List(); /// /// 数据试图 /// /// /// private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0 && e.ColumnIndex == 0) { if (this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null) { if (!_CheckRowIndexList.Where(s => s == e.RowIndex).Any()) { _CheckRowIndexList.Add(e.RowIndex); } this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 1; } else if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1") { if (_CheckRowIndexList.Where(s => s == e.RowIndex).Any()) { _CheckRowIndexList.Remove(e.RowIndex); } this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 0; } else { if (!_CheckRowIndexList.Where(s => s == e.RowIndex).Any()) { _CheckRowIndexList.Add(e.RowIndex); } this.dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = 1; } } } private void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { var dataGrid = sender as DataGridView; var checkResult = IsCheckSuccess(dataGrid, e.RowIndex, e.ColumnIndex, e.FormattedValue); var columnName = dataGrid.Columns[e.ColumnIndex].Name; if (columnName == "ParamFlagName") { //如果是参数 if (e.FormattedValue.ToString() == (RuleParamFlag.Have.Description()).ToString()) { dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM1"].ReadOnly = false; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM2"].ReadOnly = false; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM3"].ReadOnly = false; } else { dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM1"].Value = ""; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM2"].Value = ""; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM3"].Value = ""; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM1"].ReadOnly = true; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM2"].ReadOnly = true; dataGrid.Rows[e.RowIndex].Cells["RULE_ITEM_PARAM3"].ReadOnly = true; } } if(columnName == "UsedFlagName") { if (dataGrid.Rows[e.RowIndex].Cells["UsedFlagName"].Value == null) { dataGrid.Rows[e.RowIndex].Cells["UsedFlagName"].Value = 0; } } } private void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e) { var dataGrid = sender as DataGridView; if (!dataGrid.Rows[dataGrid.Rows.Count - 1].IsNewRow) { foreach (DataGridViewColumn item in dataGrid.Columns) { if (!IsCheckSuccess(dataGrid, e.RowIndex, item.Index, dataGrid.Rows[e.RowIndex].Cells[item.Name].FormattedValue.ToString())) { e.Cancel = true; break; } } } } /// /// 验证 /// /// /// /// /// /// private bool IsCheckSuccess(DataGridView dataGrid, int rowIndex, int columnIndex, object cellValue) { var columnName = dataGrid.Columns[columnIndex].Name; var headerText = dataGrid.Columns[columnIndex].HeaderText; var value = cellValue.ToString(); var errMessage = string.Empty; dataGridView.Rows[rowIndex].Cells[columnIndex].ErrorText = ""; dataGridView.Rows[rowIndex].Cells[columnIndex].ToolTipText = ""; dataGridView.Rows[rowIndex].HeaderCell.Value = ""; dataGridView.Rows[rowIndex].HeaderCell.Tag = ""; //空值验证 if (columnName == "ParamFlagName" || columnName == "ParamFlagName" || columnName == "UsedFlagName") { if (string.IsNullOrWhiteSpace(value)) { var tagModel = dataGridView.Columns[columnName].Tag as TableFieldOrderResult; switch (tagModel.ColumnType) { case DataGridViewColumnType.ComboBox: errMessage = "请选择" + headerText; break; case DataGridViewColumnType.DateTimePicker: errMessage = "请选择" + headerText; break; default: errMessage = "请输入" + headerText; break; } dataGridView.Rows[rowIndex].Cells[columnIndex].ErrorText = errMessage; dataGridView.Rows[rowIndex].Cells[columnIndex].ToolTipText = errMessage; dataGridView.Rows[rowIndex].HeaderCell.Value = errMessage; dataGridView.Rows[rowIndex].HeaderCell.Tag = errMessage; dataGridView.Rows[rowIndex].HeaderCell.Style.ForeColor = Color.Red; return false; } } //逻辑验证 //重复编码 if (columnName == "RULE_ITEM_CODE") { foreach (DataGridViewRow item in dataGrid.Rows) { if (item.Cells["RULE_ITEM_CODE"].FormattedValue.ToString() == value && item.Index != rowIndex) { errMessage = "规则编码不可重复!"; dataGridView.Rows[rowIndex].Cells[columnIndex].ErrorText = errMessage; dataGridView.Rows[rowIndex].Cells[columnIndex].ToolTipText = errMessage; dataGridView.Rows[rowIndex].HeaderCell.Value = errMessage; dataGridView.Rows[rowIndex].HeaderCell.Style.ForeColor = Color.Red; return false; } } } //逻辑验证 return true; } private void dataGridView_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (e.Button == MouseButtons.Right) { contextMenuStrip1.Show(MousePosition.X, MousePosition.Y); } } private void 删除选中行ToolStripMenuItem_Click(object sender, EventArgs e) { if (_CheckRowIndexList.Count == 0) { KryptonMessageBox.Show($"请选择数据进行删除!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (KryptonMessageBox.Show($"确认删除选中数据?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { switch (_FunctionType) { case EnumFunctionType.Add: for (int i = 0; i < _CheckRowIndexList.Count; i++) { if (dataGridView.Rows[_CheckRowIndexList[i]].Cells[0].FormattedValue.ToString() == "1") { dataGridView.Rows.RemoveAt(i); i--; } else { continue; } } dataGridView.DataSource = null; dataGridView.BuildDataGridView(_tableFieldOrderResult); break; case EnumFunctionType.Edit: for (int i = 0; i < _CheckRowIndexList.Count; i++) { if (dataGridView.Rows[_CheckRowIndexList[i]].Cells[0].FormattedValue.ToString() == "1") { dataGridView.Rows.RemoveAt(i); i--; } else { continue; } } break; } } _CheckRowIndexList = new List(); } private void 全部删除ToolStripMenuItem_Click(object sender, EventArgs e) { if (KryptonMessageBox.Show($"确认删除所有数据?", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { switch (_FunctionType) { case EnumFunctionType.Add: dataGridView.Rows.Clear(); break; case EnumFunctionType.Edit: dataGridView.Rows.Clear(); break; } _CheckRowIndexList = new List(); } } private void btnConfirm_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtRuleCode.Text)) { KryptonMessageBox.Show($"请输入规则编码!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(txtRuleName.Text)) { KryptonMessageBox.Show($"请输入规则名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } for (int i = 0; i < dataGridView.RowCount; i++) { dataGridView_RowValidating(dataGridView, new DataGridViewCellCancelEventArgs(0, i)); } if (!dataGridView.IsCheckHeaderValue()) { KryptonMessageBox.Show($"请检查不符合要求数据!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } var index = 0; foreach(DataGridViewRow item in dataGridView.Rows) { if (!item.IsNewRow) { index++; } } if (index == 0) { KryptonMessageBox.Show($"请添加至少一条明细记录!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } switch (_RuleType) { case RuleType.PutAway: var itemList = new List(); for (int i = 0; i < dataGridView.Rows.Count; i++) { if (!dataGridView.Rows[i].IsNewRow) { itemList.Add(new PutAwayRuleDetailCondition { RuleItemDescribe = dataGridView.Rows[i].Cells["RULE_ITEM_DESC"].GetObjectToString(), IsUsed = dataGridView.Rows[i].Cells["UsedFlagName"].Value.GetObjectToBoolean("已启用"), ParamFlag = dataGridView.Rows[i].Cells["ParamFlagName"].GetObjectToString() == "有参数" ? (int)RuleParamFlag.Have : (int)RuleParamFlag.NotHave, RuleItemCode = dataGridView.Rows[i].Cells["RULE_ITEM_CODE"].Value.GetObjectToString(), RuleItemId = dataGridView.Rows[i].Cells["RULE_DTL_ID"].Value.GetObjectToInt(), RuleItemParam1 = dataGridView.Rows[i].Cells["RULE_ITEM_PARAM1"].Value.GetObjectToString(), RuleItemParam2 = dataGridView.Rows[i].Cells["RULE_ITEM_PARAM2"].Value.GetObjectToString(), RuleItemParam3 = dataGridView.Rows[i].Cells["RULE_ITEM_PARAM3"].Value.GetObjectToString(), }); } } switch (_FunctionType) { case EnumFunctionType.Add: var addEntity = new PutAwayRuleCondition { OperationUserId = AppConfig.UserLoginResult.UserInfo.UserId, DetailList = itemList, RuleDescribe = richDescibe.Text, IsUsed = chkUse.Checked, RuleCode = txtRuleCode.Text, RuleName = txtRuleName.Text, }; var addResult = RuleServices.putAwayRuleService.Add(addEntity); if (addResult.Status == OperateStatus.Success) { KryptonMessageBox.Show($"新增规则成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); dataGridView.ClearSelection(); this.DialogResult = DialogResult.Yes; } else { KryptonMessageBox.Show($"操作失败!\r\n{addResult.Message}", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } break; case EnumFunctionType.Edit: var editEntity = new PutAwayRuleCondition { OperationUserId = AppConfig.UserLoginResult.UserInfo.UserId, DetailList = itemList, RuleDescribe = richDescibe.Text, IsUsed = chkUse.Checked, RuleCode = txtRuleCode.Text, RuleName = txtRuleName.Text, }; var editResult = RuleServices.putAwayRuleService.Edit(editEntity); if (editResult.Status == OperateStatus.Success) { KryptonMessageBox.Show($"编辑规则成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); dataGridView.ClearSelection(); this.DialogResult = DialogResult.Yes; } else { KryptonMessageBox.Show($"操作失败!\r\n{editResult.Message}", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } break; } break; case RuleType.OrderDistribution: break; } } private void btnCancel_Click(object sender, EventArgs e) { if (KryptonMessageBox.Show($"确认退出操作? \r\n退出后,当前所有操作数据全部清空!", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { this.DialogResult = DialogResult.No; } } } }