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; using Lskj.Control.Model; using Lskj.Model; using Lskj.Business.Impl; using Lskj.Data; using Lskj.Util; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid.Columns; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Views.Base; using Lskj.Business; using System.Text.RegularExpressions; using System.Diagnostics; using Lskj.Core; namespace Lskj.Control { public partial class FrmBaseSelect : BaseForm { string ParmaryKey; /// /// 库存数量字段、输入数量字段、结余数量字段、记录唯一值标识 /// public string KcAmountField, AmountField, JYAmountField, RecordPrimaryField; DataTable QueryTable = new DataTable(); DataTable GridColumns = new DataTable(); MyControl SearchObj = new MyControl(); // 传入参数 MyControl ControlObj = null; GridControlEx ParentGridControl; DynamicBaseSelectModel Model; public DataRow SelectRowItem = null; public DataRow[] FpRows = null; // 已分配数据行 public DataRow[] FpOtherRows = null; // 已分配其他数据行 // 输出参数 public DataRow[] SelectedRows = null; /// /// 系统模块实体对象 /// /// The system model. public ModuleModel SysModel { get; private set; } /// /// 是否点击下一个 /// public bool isNextOne = false; public FrmBaseSelect() : this(null, null, null) { } public FrmBaseSelect(DynamicBaseSelectModel model, MyControl controlObj, GridControlEx gridControl) { InitializeComponent(); this.Model = model; this.ControlObj = controlObj; this.ParentGridControl = gridControl; this.gcMain.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown; this.gcMain.GridView.CellValueChanging += new DevExpress.XtraGrid.Views.Base.CellValueChangedEventHandler(OnGridViewCellValueChanging); //this.gcMain.GridView.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(GridView_RowStyle); this.btnNextData.Enabled = false; this.lblFP.TextChanged += new EventHandler(onLblFPextChanged); this.gcMain.RefreshTotal = false; } private void onLblFPextChanged(object sender, EventArgs e) { if (Convert.ToDouble(this.lblFP.Text)== 0) { this.btnNextData.Enabled = false; } else { this.btnNextData.Enabled = true; } } /// /// 说明:计算剩余数量 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.Single. public double CalcYfpAmount() { DataTable table = this.gcMain.GridControl.DataSource as DataTable; double total = 0.0; DataRow[] rowItems = table.Rows.Cast().Where(x => x.RowState == DataRowState.Modified).ToArray(); DataRow rowItem = this.gcMain.GridView.GetFocusedDataRow(); if (rowItems != null && rowItems.Length > 0) { foreach (DataRow item in rowItems) { if (item == rowItem) continue; total += Convert.ToDouble(item[AmountField] + ""); } } return total; } /// /// 说明:获取差量 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// System.Double. public double GetSyAmount() { return Convert.ToDouble(lblSY.Text); } /// /// 说明:初始化 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The instance containing the event data. protected override void OnLoad(EventArgs e) { WaitForm.ShowForm(); try { DataRow modelRow = MainImpl.GetSystemdllTab(this.Model.ModuleCode); if (modelRow == null) { MessageUtil.Show("模块编号[" + this.Model.ModuleCode + "],配置错误!\r\n该模块信息未找到!"); this.Close(); } this.lblTotal.Text = Model.AmountValue; this.SysModel = new ModuleModel(modelRow); this.ParmaryKey = BaseImpl.GetBasePrimaryKey(this.Model.ModuleCode); this.QueryTable = BaseModuleImpl.GetCustomQueryFields(this.SysModel.CondKey); this.GridColumns = BaseModuleImpl.GetBaseGridColumns(this.Model.ModuleCode); //List paramList = ReplaceHelper.GetParamFields(this.SysModel.MenuSql); //foreach (string item in paramList) //{ // if (item.Contains("{#")) // { // string fieldName = item.Replace("{#", "").Replace("}", ""); // ControlModel controlModel = this.ControlObj.ControlModels.Find(x => x.FieldName.Equals(fieldName, StringComparison.OrdinalIgnoreCase)); // if (string.IsNullOrEmpty(this.ControlObj.GetControlValue(controlModel))) // { // MessageUtil.Show("请先录入[" + controlModel.LabelText + "]!"); // this.Close(); // break; // } // } // else if (item.Contains("{")) // { // string fieldName = item.Replace("{", "").Replace("}", ""); // if (string.IsNullOrEmpty(this.SelectRowItem[fieldName] + "")) // { // MessageUtil.Show("请先录入[" + this.ParentGridControl.GridView.Columns[fieldName].Caption + "]!"); // this.Close(); // break; // } // } //} string searchSQL = this.ControlObj.ReplaceParentControlValue(this.SysModel.MenuSql); this.SearchObj = new MyControl(ReplaceHelper.ReplaceRowParam(SelectRowItem, searchSQL), gcMain, null, null, null); this.PlSearch.Height = this.SearchObj.InitSearchControl(QueryTable, this.PlSearch); if (this.QueryTable == null || this.QueryTable.Rows.Count > 0) { this.PlSearch.Visible = false; } if (this.GridColumns != null && this.GridColumns.Rows.Count > 0) { DataRow[] rows = this.GridColumns.Select("tm_tagid=1"); if (rows == null || rows.Length == 0) { MessageUtil.Show("未配置数量字段,基础档案库存数量字段标识设置为1!"); this.Close(); } else { this.KcAmountField = rows[0]["fieldname"] + ""; } rows = this.GridColumns.Select("tm_tagid=2"); if (rows == null || rows.Length == 0) { MessageUtil.Show("未配置数量字段,基础档案数量字段标识设置为2,可虚拟字段!"); this.Close(); } else { this.AmountField = rows[0]["fieldname"] + ""; } rows = this.GridColumns.Select("tm_tagid=3"); if (rows == null || rows.Length == 0) { MessageUtil.Show("未配置数量字段,基础档案数量字段标识设置为3,可虚拟字段!"); this.Close(); } else { this.JYAmountField = rows[0]["fieldname"] + ""; } rows = this.GridColumns.Select("tm_tagid=4"); if (rows == null || rows.Length == 0) { MessageUtil.Show("未配置唯一值字段,基础档案数量字段标识设置为4!"); this.Close(); } else { this.RecordPrimaryField = rows[0]["fieldname"] + ""; } } if (string.IsNullOrEmpty(KcAmountField)) { this.lblTotal.Text = this.lblSY.Text = this.lblFP.Text = "0"; this.btnFP.Enabled = this.btnCancel.Enabled = false; } else { this.lblSY.Text = this.lblTotal.Text = Convert.ToDouble(SelectRowItem[AmountField] + "") + ""; this.lblFP.Text = "0"; } this.gcMain.SetEditColumns(this.GridColumns, GridCustomColumnStruct.BaseMainGridView + this.SysModel.FormKey); InitCheckEdit(); this.gcMain.SetGridRowColors(BaseModuleImpl.GetBaseGridRowColors(this.Model.ModuleCode)); this.gcMain.SetGridRightMenus(BaseModuleImpl.GetBaseGridRightMenus(this.Model.ModuleCode), this.Model, OnGridViewRightCallBack, this.SearchObj, null); this.SearchObj.SearchLastGrid(); // if (this.FpOtherRows != null && this.FpOtherRows.Length > 0) { DataTable table = this.gcMain.GridControl.DataSource as DataTable; foreach (DataRow item in this.FpOtherRows) { DataRow rowItem = table.Rows.Cast().FirstOrDefault(x => x[RecordPrimaryField] + "" == item[RecordPrimaryField] + ""); if (rowItem != null) { rowItem[KcAmountField] = Convert.ToDouble(rowItem[KcAmountField]) - Convert.ToDouble(item[AmountField]); } } double total = Convert.ToDouble(this.lblTotal.Text); double ypfAmount = CalcYfpAmount(); this.lblFP.Text = ypfAmount + ""; this.lblSY.Text = (total - ypfAmount) + ""; } if (this.FpRows != null && this.FpRows.Length > 0) { DataTable table = this.gcMain.GridControl.DataSource as DataTable; foreach (DataRow item in this.FpRows) { DataRow rowItem = table.Rows.Cast().FirstOrDefault(x => x[RecordPrimaryField] + "" == item[RecordPrimaryField] + ""); if (rowItem != null) { rowItem[AmountField] = Convert.ToDouble(item[AmountField]); rowItem[JYAmountField] = Convert.ToDouble(rowItem[KcAmountField]) - Convert.ToDouble(rowItem[AmountField]); } } double total = Convert.ToDouble(this.lblTotal.Text); double ypfAmount = CalcYfpAmount(); this.lblFP.Text = ypfAmount + ""; this.lblSY.Text = (total - ypfAmount) + ""; } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); LogHelper.Instance.WriteError(ex, ResourceKeys.BaseModule); } Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this); WaitForm.HideForm(); } /// /// 说明:创建CheckBoxEdit /// 创建人:龚宇超 /// 创建日期:2017-08-21 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The grid column. public void InitCheckEdit() { GridColumn gridColumn = new GridColumn(); RepositoryItemCheckEdit checkEdit = new RepositoryItemCheckEdit(); checkEdit.ValueChecked = true; checkEdit.ValueUnchecked = false; gridColumn.ColumnEdit = checkEdit; gridColumn.Caption = "直接分配"; gridColumn.FieldName = "checkEdit"; gridColumn.Name = "checkEdit"; gridColumn.Visible = true; gridColumn.VisibleIndex = 0; gridColumn.ColumnEdit = checkEdit; checkEdit.CheckedChanged += new EventHandler(OnCheckedChanged); this.gcMain.GridControl.RepositoryItems.Add(checkEdit); this.gcMain.GridView.Columns.Add(gridColumn); } /// /// 选择状态改变触发事件 /// /// /// private void OnCheckedChanged(object sender, EventArgs e) { try { int rowIndex = this.gcMain.GridView.GetFocusedDataSourceRowIndex(); double total = Convert.ToDouble(this.lblTotal.Text); double totalCopy = total - CalcOldAmount(); double fpAmount = 0.0; CheckEdit checkEdit = sender as CheckEdit; // 分配规则,根据当前行自动分配直到分完为止 DataRow item = this.gcMain.GridView.GetFocusedDataRow(); double kcAmount = Convert.ToDouble(item[KcAmountField] + ""); if (checkEdit.Checked) { double check = Convert.ToDouble(item[AmountField] + ""); if (check == 0.0) { if (kcAmount >= totalCopy) { item[AmountField] = totalCopy; totalCopy = 0; } else { if (fpAmount + kcAmount > totalCopy) { // 最后一次分配 item[AmountField] = kcAmount - (totalCopy - fpAmount); } else { item[AmountField] = kcAmount; } } } } else { item[AmountField] = 0; } fpAmount = Convert.ToDouble(item[AmountField]); this.gcMain.OnGridViewCellValueChanged(this.gcMain, new CellValueChangedEventArgs(rowIndex, this.gcMain.GridView.Columns[AmountField], fpAmount)); item[JYAmountField] = Convert.ToDouble(item[KcAmountField]) - Convert.ToDouble(item[AmountField]); double ypfAmount = CalcOldAmount(); this.lblFP.Text = ypfAmount + ""; this.lblSY.Text = (total - ypfAmount) + ""; } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新 /// 创建人:龚宇超 /// 创建日期:2017-11-02 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. public double CalcOldAmount() { DataTable table = this.gcMain.GridControl.DataSource as DataTable; double total = 0.0; DataRow[] rowItems = table.Rows.Cast().Where(x => x.RowState == DataRowState.Modified).ToArray(); //DataRow rowItem = this.gcMain.GridView.GetFocusedDataRow(); if (rowItems != null && rowItems.Length > 0) { foreach (DataRow item in rowItems) { //if (item == rowItem) continue; total += Convert.ToDouble(item[AmountField] + ""); } } return total; } /// /// 说明:右键菜单执行后刷新数据,刷新规则按照上一次查询条件刷新 /// 创建人:龚宇超 /// 创建日期:2017-11-02 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. protected void OnGridViewRightCallBack(object sender, EventArgs e) { try { this.SearchObj.SearchLastGrid(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:确定 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void OnBtnOKClick(object sender, EventArgs e) { try { this.isNextOne = false; List listRows = new List(); DataTable table = this.gcMain.gridControl.DataSource as DataTable; foreach (DataRow item in table.Rows) { if (item.RowState == DataRowState.Modified && !string.IsNullOrEmpty(item[AmountField] + "")&& Convert.ToDouble(item[AmountField] + "") != 0) { listRows.Add(item); } } this.SelectedRows = listRows.ToArray(); this.DialogResult = System.Windows.Forms.DialogResult.OK; // this.SelectRowItem = null; this.Close(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show("确定出错!" + Message); } } //点击下一个 private void OnTheNextClick(object sender, EventArgs e) { try { List listRows = new List(); DataTable table = this.gcMain.gridControl.DataSource as DataTable; foreach (DataRow item in table.Rows) { if (item.RowState == DataRowState.Modified && !string.IsNullOrEmpty(item[AmountField] + "") && Convert.ToDouble(item[AmountField] + "") > 0) { listRows.Add(item); } } this.SelectedRows = listRows.ToArray(); this.isNextOne = true; this.DialogResult = System.Windows.Forms.DialogResult.OK; } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show("确定出错!" + Message); } } /// /// 说明:取消 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void OnBtnCancelClick(object sender, EventArgs e) { this.Close(); } /// /// 说明:自动分配 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. private void OnBtnFPClick(object sender, EventArgs e) { // 分配规则,根据当前行自动分配直到分完为止 try { DataTable table = this.gcMain.GridControl.DataSource as DataTable; if (table == null || table.Rows.Count == 0) return; double total = Convert.ToDouble(this.lblTotal.Text); double totalCopy = total - CalcOldAmount(); double fpAmount = 0.0; int rowIndex = this.gcMain.GridView.GetFocusedDataSourceRowIndex(); DataRow item = null; for (int i = rowIndex; i < table.Rows.Count; i++) { item = table.Rows[i]; double kcAmount = Convert.ToDouble(item[KcAmountField] + ""); double check = Convert.ToDouble(item[AmountField] + ""); totalCopy = total - CalcOldAmount(); if (check == 0.0) { if (kcAmount >= totalCopy) { item[AmountField] = totalCopy; totalCopy = 0; } else { //if (fpAmount + kcAmount > total) //{ // // 最后一次分配 // item[AmountField] = kcAmount - (total - fpAmount); //} //else //{ item[AmountField] = kcAmount; //} } } fpAmount = Convert.ToDouble(item[AmountField]); item[JYAmountField] = Convert.ToDouble(item[KcAmountField]) - Convert.ToDouble(item[AmountField]); this.gcMain.GridView.SelectRowHandler(i); this.gcMain.OnGridViewCellValueChanged(this.gcMain, new CellValueChangedEventArgs(i, this.gcMain.GridView.Columns[AmountField], fpAmount)); } //double ypfAmount = CalcYfpAmount() + (item == null ? 0 : Convert.ToDouble(item[AmountField])); double ypfAmount = CalcOldAmount(); this.lblFP.Text = ypfAmount + ""; this.lblSY.Text = (total - ypfAmount) + ""; } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show("自动分配出错!" + Message); } } /// /// 说明:数量字段发生变化时 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. void OnGridViewCellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) { try { if (e.Column.Name == this.AmountField) { double total = Convert.ToDouble(lblTotal.Text); double yfpAmount = CalcYfpAmount(); double fpAmount; if (string.IsNullOrWhiteSpace(e.Value.ToString())) { this.gcMain.GridView.SetRowCellValue(e.RowHandle, e.Column, 0); fpAmount = 0; } else{ // 计算剩余数量 fpAmount = Convert.ToDouble(e.Value + ""); } // 验证是否超出单行总数 DataRow rowItem = this.gcMain.GridView.GetFocusedDataRow(); double kcAmount = Convert.ToDouble(rowItem[KcAmountField] + ""); if (fpAmount > kcAmount) { fpAmount = kcAmount; this.gcMain.GridView.SetRowCellValue(e.RowHandle, e.Column, fpAmount); } // 验证是否超总数 if (yfpAmount + fpAmount > total) { fpAmount = total - yfpAmount; this.gcMain.GridView.SetRowCellValue(e.RowHandle, e.Column, fpAmount); } rowItem[JYAmountField] = Convert.ToDouble(rowItem[KcAmountField]) - fpAmount; fpAmount = yfpAmount + fpAmount; this.lblFP.Text = fpAmount + ""; this.lblSY.Text = (total - fpAmount) + ""; } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:行颜色 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void GridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e) { try { DataRow rowItem = this.gcMain.GridView.GetDataRow(e.RowHandle); double total = Convert.ToDouble(rowItem[KcAmountField]); double fpAmount = Convert.ToDouble(rowItem[AmountField]); if (fpAmount > 0) { e.Appearance.BackColor = total - fpAmount <= 0 ? Color.Green : Color.Red; } } catch (Exception) { } } } }