using Core.Mes.Client.Comm.Tool; using Core.StlMes.Client.Qcm.BLL; using Core.StlMes.Client.Qcm.model; using CoreFS.CA06; using Infragistics.Win.UltraWinGrid; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Windows.Forms; namespace Core.StlMes.Client.Qcm { public partial class ComBaseWaterGroupFrm : FrmBase { private ComBaseWaterGroupBLL _waterGroupBLL; private List _waterGroupGradeList; public ComBaseWaterGroupFrm() { InitializeComponent(); EntityHelper.ShowGridCaption(ultraGrid1.DisplayLayout.Bands[0]); EntityHelper.ShowGridCaption(ultraGrid2.DisplayLayout.Bands[0]); } private void ComBaseWaterGroupFrm_Load(object sender, EventArgs e) { _waterGroupBLL = new ComBaseWaterGroupBLL(ob); QueryWaterGroup(); } public override void ToolBar_Click(object sender, string ToolbarKey) { base.ToolBar_Click(sender, ToolbarKey); switch (ToolbarKey) { case "Query": QueryWaterGroup(); break; case "Save": SaveWaterGroup(); break; case "MoveUp": MoveUp(); break; case "MoveDown": MoveDown(); break; case "Close": this.Close(); break; } } private void QueryWaterGroup() { this.comBaseWaterGroupEntityBindingSource.DataSource = _waterGroupBLL.QueryWaterGroup(); ultraGrid1.UpdateData(); } private void QueryWaterGroupGrade() { ComBaseWaterGroupEntity waterGroup = (ComBaseWaterGroupEntity)ultraGrid1.ActiveRow.ListObject; _waterGroupGradeList = _waterGroupBLL.QueryWaterGroupGrade(waterGroup.WaterGroupCode); this.comBaseWaterGroupEntityBindingSource1.DataSource = _waterGroupGradeList.AsQueryable() .OrderByDescending(a => a.Chk).ThenBy(a => a.GradeIndex, new NullMaxCompare()).ThenBy(a => a.Gradename); foreach (UltraGridRow row in ultraGrid2.Rows) { ComBaseWaterGroupEntity waterGroup2 = (ComBaseWaterGroupEntity)row.ListObject; if (waterGroup2.WaterGroupCode != "") { row.SetValue("Chk", "True"); } } ultraGrid2.UpdateData(); } /// /// 空认为是最大的值 /// private class NullMaxCompare : IComparer { #region IComparer 成员 public int Compare(decimal? x, decimal? y) { if (x == y) { return 0; } else if (x == null) { return Convert.ToInt32(99999 - y); } else if (y == null) { return Convert.ToInt32(x - 99999); } else { return Convert.ToInt32(x - y); } } #endregion } ///// ///// 空认为是最小的值 ///// //private class NullSmallCompare : IComparer //{ // #region IComparer 成员 // public int Compare(decimal? x, decimal? y) // { // if (x == y) // { // return 0; // } // else if (x == null) // { // return 0; // } // else if (y == null) // { // return 0; // } // else if (x > y) // { // return 1; // } // else // { // return -1; // } // } // #endregion //} private void SaveWaterGroup() { ultraGrid2.UpdateData(); if (ultraGrid1.ActiveRow == null) { MessageUtil.ShowWarning("请选择一行记录!"); return; } if (ultraGrid1.ActiveRow.GetValue("WaterGroup") == "") { MessageUtil.ShowWarning("请输入连浇群组名称!"); return; } var rows = ultraGrid2.Rows.AsQueryable().Where("CHK = 'True'"); var waterGroups = rows.Select(a => (ComBaseWaterGroupEntity)a.ListObject); if (rows.Count() == 0) { MessageUtil.ShowWarning("请选择记录!"); return; } string waterGroupDesc = ultraGrid1.ActiveRow.GetValue("WaterGroup"); string waterGroupCode = ultraGrid1.ActiveRow.GetValue("WaterGroupCode"); if (_waterGroupBLL.GetWaterGroupCnt(waterGroupDesc, waterGroupCode) > 0) { MessageUtil.ShowWarning("系统已连浇群组名称" + waterGroupDesc + "!"); return; } var groupRows = waterGroups.GroupBy(a => a.GradeIndex).Where(a => a.Count() >= 2); foreach (var row in groupRows) { MessageUtil.ShowWarning("钢种序号:" + row.Key + "重复!"); var repeatRow = rows.Where(a => a.GetValue("GradeIndex") == row.Key.ToString()); if (repeatRow.Count() > 0) { repeatRow.First().Cells["GradeIndex"].Activate(); } return; } List listWaterGroups = rows.Select(a => (ComBaseWaterGroupEntity)a.ListObject).ToList(); string gradeCodes = ""; foreach (ComBaseWaterGroupEntity waterGroup in listWaterGroups) { waterGroup.CreateName = UserInfo.GetUserName(); waterGroup.WaterGroupCode = ultraGrid1.ActiveRow.GetValue("WaterGroupCode"); waterGroup.WaterGroup = ultraGrid1.ActiveRow.GetValue("WaterGroup"); if (gradeCodes == "") { gradeCodes = waterGroup.Gradecode; } else { gradeCodes += "," + waterGroup.Gradecode; } } string gradeWaterGroup = ""; if (_waterGroupBLL.GetWaterGroupGradeCnt(gradeCodes, waterGroupCode, out gradeWaterGroup) > 0) { MessageUtil.ShowWarning("该连浇群组与连浇群组" + gradeWaterGroup + "的钢种组合重复!"); return; } if (MessageUtil.ShowYesNoAndQuestion("是否确认保存?") == System.Windows.Forms.DialogResult.No) { return; } waterGroupCode = _waterGroupBLL.SaveWaretGroupGrade(listWaterGroups); MessageUtil.ShowTips("保存成功!"); Relocate(waterGroupCode); } private void Relocate(string waterGroupCode) { QueryWaterGroup(); var row = ultraGrid1.Rows.Where(a => a.GetValue("waterGroupCode") == waterGroupCode).FirstOrDefault(); if (row != null) { row.Activate(); } } private void MoveUp() { ultraGrid2.BeginUpdate(); IQueryable rows = ultraGrid2.Rows.AsQueryable().Where( a => a.Activated || a.Selected); IQueryable queryableRows = _waterGroupGradeList.AsQueryable(); foreach (UltraGridRow row in rows) { if (row.Index == 0) continue; ComBaseWaterGroupEntity waterGroup = (ComBaseWaterGroupEntity)row.ListObject; ComBaseWaterGroupEntity[] preWaterGroups = ultraGrid2.Rows.AsQueryable().Select(a => (ComBaseWaterGroupEntity)a.ListObject) .Where(a => a.Chk && a.GradeIndex < waterGroup.GradeIndex).OrderByDescending(a => a.GradeIndex).ToArray(); if (preWaterGroups.Length > 0) { decimal? index = waterGroup.GradeIndex; waterGroup.GradeIndex = preWaterGroups[0].GradeIndex; preWaterGroups[0].GradeIndex = index; } else { continue; } } this.comBaseWaterGroupEntityBindingSource1.DataSource = queryableRows.OrderByDescending(a => a.Chk).ThenBy( a => a.GradeIndex, new NullMaxCompare()).ThenBy(a => a.Gradename); ultraGrid2.EndUpdate(); } private void MoveDown() { ultraGrid2.BeginUpdate(); IQueryable selectedRows = ultraGrid2.Rows.AsQueryable().Where( a => a.Activated || a.Selected).Select(a => (ComBaseWaterGroupEntity)a.ListObject).OrderByDescending(a => a.GradeIndex); IQueryable queryableRows = _waterGroupGradeList.AsQueryable(); foreach (ComBaseWaterGroupEntity waterGroup in selectedRows) { ComBaseWaterGroupEntity[] preWaterGroups = ultraGrid2.Rows.AsQueryable().Select(a => (ComBaseWaterGroupEntity)a.ListObject) .Where(a => a.Chk && a.GradeIndex > waterGroup.GradeIndex).OrderBy(a => a.GradeIndex).ToArray(); if (preWaterGroups.Length > 0) { decimal? index = waterGroup.GradeIndex; waterGroup.GradeIndex = preWaterGroups[0].GradeIndex; preWaterGroups[0].GradeIndex = index; } else { continue; } } this.comBaseWaterGroupEntityBindingSource1.DataSource = queryableRows.OrderByDescending(a => a.Chk).ThenBy( a => a.GradeIndex, new NullMaxCompare()).ThenBy(a => a.Gradename); ultraGrid2.EndUpdate(); } private void ultraGrid2_InitializeRow(object sender, InitializeRowEventArgs e) { } private void ultraGrid1_AfterRowActivate(object sender, EventArgs e) { try { this.Cursor = Cursors.WaitCursor; QueryWaterGroupGrade(); } finally { this.Cursor = Cursors.Default; } } private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e) { e.Layout.Override.TemplateAddRowPrompt = "点击新增一行记录!"; e.Layout.Override.TemplateAddRowAppearance.BackColor = Color.FromArgb(255, 255, 128); } private void ultraGrid1_CellChange(object sender, CellEventArgs e) { ultraGrid1.UpdateData(); } private void ultraGrid2_CellChange(object sender, CellEventArgs e) { ultraGrid2.UpdateData(); ComBaseWaterGroupEntity waterGroup = (ComBaseWaterGroupEntity)e.Cell.Row.ListObject; if (e.Cell.Column.Key == "Chk" && waterGroup.GradeIndex == null) { IQueryable queryableRows = _waterGroupGradeList.AsQueryable(); var waterGroups = queryableRows.Where(a => a.GradeIndex != null).OrderByDescending( a => a.GradeIndex, new NullMaxCompare()); if (waterGroups.Count() > 0) { waterGroup.GradeIndex = waterGroups.First().GradeIndex + 1; } else { waterGroup.GradeIndex = 1; } ultraGrid2.Refresh(); this.comBaseWaterGroupEntityBindingSource1.DataSource = queryableRows.OrderByDescending(a => a.Chk).ThenBy( a => a.GradeIndex, new NullMaxCompare()).ThenBy(a => a.Gradename); } } } }