using ComponentFactory.Krypton.Toolkit; using NXWMS.Client.Code.Converter; using NXWMS.Client.Code.Extends; using NXWMS.Client.Code.Models; using NXWMS.Client.Interface.Rule; using NXWMS.Client.Model.AppModels.Condition.Rule; using NXWMS.Client.Model.AppModels.Result.Rule; using NXWMS.Client.Model.CoreModels; using NXWMS.Client.String.Enums; using NXWMS.Commons; 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.Windows.Forms; namespace NXWMS.Forms.Rule.frmRuleChild { public partial class frmOperateImportTemplate : Form { /// /// 客户端字段排序列表 /// private List _tableFieldOrderResult; /// /// 模版编码 /// public string _templateCode; /// /// 界面功能类型 /// public EnumFunctionType _FunctionType; /// /// 界面功能类型 /// public enum EnumFunctionType { Add, Edit, } /// /// 是否已加载完成 /// private bool _IsLoading = false; private ImportTemplateResult _MainInfo; private List _DetailList; public frmOperateImportTemplate() { InitializeComponent(); this.SetFormSizeLocation(1); this.Load += new System.EventHandler(this.frmOperateImportTemplate_Load); } private void frmOperateImportTemplate_Load(object sender, EventArgs e) { InitData(); InitControl(); //_IsLoading = true; } 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 = "IMPORT_TEMPLATE_DTL_ID", FieldDesc = "导入模版明细ID", IsReadOnly = false, ColumnType = DataGridViewColumnType.TextBox, IsShow = false, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "IMPORT_TEMPLATE_CODE", FieldDesc = "导入模版编码", IsReadOnly = false, Width = 200, ColumnType = DataGridViewColumnType.TextBox, IsShow = false, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "SOURCE_CODE", FieldDesc = "源结构编码", IsReadOnly = false, Width = 100, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "SOURCE_NAME", FieldDesc = "源结构名称", IsReadOnly = false, Width = 200, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "SourceTypeName", FieldDesc = "源结构类型", IsReadOnly = true, Width = 100, ColumnType = DataGridViewColumnType.ComboBox, Data = ModelsConvert.ToDataTable( new List().GetFieldValueIdList()), ConditionField = "Name", ResultField = "Id" }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "SOURCE_LENGTH", FieldDesc = "源结构长度", Width = 100, IsReadOnly = false, MaxValue=10000, MinValue=0, ColumnType = DataGridViewColumnType.NumericUpDown, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "SourceRequiredFlagName", FieldDesc = "源结构必填标志", IsReadOnly = true, Width = 150, ColumnType = DataGridViewColumnType.ComboBox, Data = ModelsConvert.ToDataTable( new List().GetFieldValueIdList()), ConditionField = "Name", ResultField = "Id" }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TARGET_CODE", FieldDesc = "目标结构编码", IsReadOnly = false, Width = 100, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TARGET_NAME", FieldDesc = "目标结构名称", IsReadOnly = false, Width = 200, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TargetTypeName", FieldDesc = "目标结构类型", IsReadOnly = true, Width = 100, ColumnType = DataGridViewColumnType.ComboBox, Data = ModelsConvert.ToDataTable( new List().GetFieldValueIdList()), ConditionField = "Name", ResultField = "Id" }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TARGET_LENGTH", FieldDesc = "目标结构长度", IsReadOnly = false, MaxValue = 10000, MinValue = 0, Width = 100, ColumnType = DataGridViewColumnType.NumericUpDown, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TargetRequiredFlagName", FieldDesc = "目标结构必填标志", IsReadOnly = true, Width = 150, ColumnType = DataGridViewColumnType.ComboBox, Data = ModelsConvert.ToDataTable( new List().GetFieldValueIdList()), ConditionField = "Name", ResultField = "Id" }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TargetRelationTypeName", FieldDesc = "目标关联类型编码", IsReadOnly = true, Width = 150, ColumnType = DataGridViewColumnType.ComboBox, Data = ModelsConvert.ToDataTable( new List().GetFieldValueIdList()), ConditionField = "Name", ResultField = "Id" }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "TARGET_RELATION_CONTENT", FieldDesc = "目标关联类型内容", IsReadOnly = false, Width = 300, ColumnType = DataGridViewColumnType.TextBox, }); _tableFieldOrderResult.Add(new TableFieldOrderResult { FieldName = "DESCRIBE", FieldDesc = "描述", IsReadOnly = false, Width = 300, 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 (_FunctionType) { case EnumFunctionType.Add: this.Text = "导入模版-新增"; txtTemplateCode.Text = _templateCode; txtTemplateCode.Enabled = true; break; case EnumFunctionType.Edit: this.Text = "导入模版-编辑"; txtTemplateCode.Text = _templateCode; txtTemplateCode.Enabled = false; break; } } private void InitControl() { CheckForIllegalCrossThreadCalls = false; dataGridView.CellClick += new DataGridViewCellEventHandler(dataGridView_CellClick); dataGridView.CellValidating += new DataGridViewCellValidatingEventHandler(this.dataGridView_CellValidating); dataGridView.DataError += delegate (object sender, DataGridViewDataErrorEventArgs e) { }; dataGridView.RowValidating += new DataGridViewCellCancelEventHandler(this.dataGridView_RowValidating); dataGridView.RowsAdded += new DataGridViewRowsAddedEventHandler(this.dataGridView_RowsAdded); 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 (_FunctionType) { case EnumFunctionType.Add: break; case EnumFunctionType.Edit: break; } var fieldList = new List().GetFieldValueCodeList(); cmbImportMode.DataSource = fieldList; cmbImportMode.DisplayMember = "Name"; cmbImportMode.ValueMember = "Code"; cmbImportMode.SelectedIndex = -1; fieldList = new List().GetFieldValueCodeList(); cmbSourceType.DataSource = fieldList; cmbSourceType.DisplayMember = "Name"; cmbSourceType.ValueMember = "Code"; cmbSourceType.SelectedIndex = -1; if (_FunctionType != EnumFunctionType.Add) { //获取对应盘点单数据 var mainResult = RuleServices.importTemplateService.GetList(new ImportTemplateSearchCondition { TemplateCode = _templateCode, }); if (mainResult.Status == OperateStatus.Success) { _MainInfo = mainResult.Data.RowData.FirstOrDefault(); txtTemplateCode.Text = _MainInfo.IMPORT_TEMPLATE_CODE; txtTemplateName.Text = _MainInfo.IMPORT_TEMPLATE_NAME; txtSourceCode.Text = _MainInfo.TARGET_OBJECT_CODE; txtSourceName.Text = _MainInfo.TARGET_OBJECT_NAME; cmbImportMode.Text = _MainInfo.ImportModeName; richDescibe.Text = _MainInfo.DESCRIBE; cmbSourceType.Text = _MainInfo.SourceObjTypeName; chkUse.Checked = _MainInfo.USED_FLAG.GetObjectToBoolean(); } else { KryptonMessageBox.Show($"操作失败!\r\n{mainResult.Message}", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } //获取对应子数据 var detailResult = RuleServices.importTemplateService.GetDetailList(new ImportTemplateDetailSearchCondition { TemplateCode = _MainInfo.IMPORT_TEMPLATE_CODE }); 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 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 List _CheckRowIndexList = new List(); 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 == "PARAM_FLAG") { //如果是参数 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 == "SOURCE_CODE" || columnName == "SOURCE_NAME" || columnName == "SourceTypeName" || columnName == "TARGET_CODE" || columnName == "TARGET_NAME" || columnName == "TargetTypeName" || columnName == "SourceRequiredFlagName" || columnName == "RequiredFlagName" || columnName == "TargetRelationTypeName") { 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 == "SOURCE_CODE") { foreach (DataGridViewRow item in dataGrid.Rows) { if (item.Cells["SOURCE_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; } } } if (columnName == "TARGET_CODE") { foreach (DataGridViewRow item in dataGrid.Rows) { if (item.Cells["TARGET_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 btnConfirm_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(txtTemplateCode.Text)) { KryptonMessageBox.Show($"请输入导入模版编号!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(txtTemplateName.Text)) { KryptonMessageBox.Show($"请输入导入模版名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(cmbImportMode.Text)) { KryptonMessageBox.Show($"请选择导入方式!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(txtSourceCode.Text)) { KryptonMessageBox.Show($"请输入源对象编码!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(txtSourceName.Text)) { KryptonMessageBox.Show($"请输入源对象名称!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } if (string.IsNullOrWhiteSpace(cmbSourceType.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; } var itemList = new List(); for (int i = 0; i < dataGridView.Rows.Count; i++) { if (!dataGridView.Rows[i].IsNewRow) { itemList.Add(new ImportTemplateDetailCondition { Describe = dataGridView.Rows[i].Cells["DESCRIBE"].Value.GetObjectToString(), IsUsed = dataGridView.Rows[i].Cells["UsedFlagName"].Value.GetObjectToBoolean("已启用"), SourceCode = dataGridView.Rows[i].Cells["SOURCE_CODE"].Value.GetObjectToString(), SourceLength = dataGridView.Rows[i].Cells["SOURCE_LENGTH"].Value.GetObjectToInt(), SourceName = dataGridView.Rows[i].Cells["SOURCE_NAME"].Value.GetObjectToString(), SourceRequiredFlag = dataGridView.Rows[i].Cells["SourceRequiredFlagName"].Value.GetEnumInt(), SourceType = dataGridView.Rows[i].Cells["SourceTypeName"].Value.GetEnumString(), TargetCode = dataGridView.Rows[i].Cells["TARGET_CODE"].Value.GetObjectToString(), TargetLength = dataGridView.Rows[i].Cells["TARGET_LENGTH"].Value.GetObjectToInt(), TargetName = dataGridView.Rows[i].Cells["TARGET_NAME"].Value.GetObjectToString(), TargetRelationContent = dataGridView.Rows[i].Cells["TARGET_RELATION_CONTENT"].Value.GetObjectToString(), TargetRelationType = dataGridView.Rows[i].Cells["TargetRelationTypeName"].Value.GetEnumString(), TargetRequiredFlag = dataGridView.Rows[i].Cells["TargetRequiredFlagName"].Value.GetEnumInt(), TargetType = dataGridView.Rows[i].Cells["TargetTypeName"].Value.GetEnumString(), }); } } switch (_FunctionType) { case EnumFunctionType.Add: var addEntity = new ImportTemplateCondition { OperationUserId = AppConfig.UserLoginResult.UserInfo.UserId, DetailList = itemList, Describe = richDescibe.Text, IsUsed = chkUse.Checked, ImportMode = cmbImportMode.SelectedValue.GetObjectToString(), SourceObjCode = txtSourceCode.Text, SourceObjName = txtSourceName.Text, SourceObjType = cmbImportMode.SelectedValue.GetObjectToString(), TemplateCode = txtTemplateCode.Text, TemplateName = txtTemplateName.Text, }; var addResult = RuleServices.importTemplateService.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 ImportTemplateCondition { OperationUserId = AppConfig.UserLoginResult.UserInfo.UserId, DetailList = itemList, IsUsed = chkUse.Checked, Describe = richDescibe.Text, ImportMode = cmbImportMode.SelectedValue.GetObjectToString(), SourceObjCode = txtSourceCode.Text, SourceObjName = txtSourceName.Text, SourceObjType = cmbImportMode.SelectedValue.GetObjectToString(), TemplateCode = txtTemplateCode.Text, TemplateName = txtTemplateName.Text, }; var editResult = RuleServices.importTemplateService.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; } } private void dataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) { if (_IsLoading) { dataGridView.Rows[e.RowIndex].Cells["SOURCE_TYPE"].Value = SourceType.String.Description(); dataGridView.Rows[e.RowIndex].Cells["TARGET_TYPE"].Value = SourceType.String.Description(); } } private void btnCancel_Click(object sender, EventArgs e) { if (KryptonMessageBox.Show($"确认退出操作? \r\n退出后,当前所有操作数据全部清空!", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { this.DialogResult = DialogResult.No; } } } }