using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Windows.Forms; using com.steering.mes.mcp.entity; using Core.Mes.Client.Comm.Control; using Core.Mes.Client.Comm.Format; using Core.Mes.Client.Comm.Tool; using Core.StlMes.Client.Mcp.Control.Entity; using Core.StlMes.Client.Mcp.Mch.Entity; using CoreFS.CA06; using Infragistics.Win; using Infragistics.Win.UltraWinGrid; using NPOI.SS.UserModel; namespace Core.StlMes.Client.Mcp.Mch.MchResult { public partial class MesureData : FrmBase { private string _judgeStove; private string _batchNo; private string _plineCode = ""; private string _heatPlanId; private string _userName; private PlnZyJgxCEntity _jgxPlan = null; public List list = new List(); public MesureData(string heatPlanId, string judgeStove, string batchNo,string PlineCode,OpeBase _ob, string userName,bool ShowAdd) { _judgeStove = judgeStove; _batchNo = batchNo; _heatPlanId = heatPlanId; _plineCode = PlineCode; _userName = userName; this.ob = _ob; InitializeComponent(); if (!ShowAdd) { uegAdd.Visible = false; } Query(); } public MesureData(PlnZyJgxCEntity JgxPlan, OpeBase _ob, string userName, bool ShowAdd, string CustomInfo) { _jgxPlan = JgxPlan; _judgeStove = JgxPlan.JudgeStoveNo; _batchNo = JgxPlan.BatchNo; _heatPlanId = JgxPlan.HeatPlanNo; if (!CustomInfo.Equals("NotPline")) { _plineCode = JgxPlan.PlineCode; } _userName = userName; this.ob = _ob; InitializeComponent(); if (!ShowAdd) { uegAdd.Visible = false; } Query(); } public MesureData(PlnZyJgxCEntity JgxPlan, OpeBase _ob, string userName, bool ShowAdd) { _jgxPlan = JgxPlan; _judgeStove = JgxPlan.JudgeStoveNo; _batchNo = JgxPlan.BatchNo; _heatPlanId = JgxPlan.HeatPlanNo; _plineCode = JgxPlan.PlineCode; _userName = userName; this.ob = _ob; InitializeComponent(); if (!ShowAdd) { uegAdd.Visible = false; } Query(); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); EntityHelper.ShowGridCaption(ugData.DisplayLayout.Bands[0]); ugData.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.False; ugData.DisplayLayout.Override.HeaderClickAction = HeaderClickAction.Default; EntityHelper.ShowGridCaption(ultraGrid1.DisplayLayout.Bands[0]); bindingSource1.DataSource = new List() { new RtdMeasurePosdataEntity() { MatNo = "1", ActLen = (decimal)13313, ActWeight = 529, ProBz = "甲", ProBc = "早" } }; } private void ugData_InitializeRow(object sender, Infragistics.Win.UltraWinGrid.InitializeRowEventArgs e) { RtdMeasurePosdataEntity entity = e.Row.ListObject as RtdMeasurePosdataEntity; if(entity==null) return; if(entity.Flag== "10") e.Row.Appearance.BackColor = Color.Gray; else if (entity.Rk != "1") e.Row.Appearance.BackColor = Color.Yellow; e.Row.Cells["MatNo"].EditorComponent = entity.Flag == "10" ? uteRecover : uteRemove; entity.WtType = entity.OraActWeight == null ? "实重" : "理重"; } private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e) { switch (e.Tool.Key) { case "Save": // ButtonTool Query(); // Place code here break; case "Show": // ButtonTool ShowData(); // Place code here break; case "Close": // ButtonTool this.DialogResult = DialogResult.Cancel; break; case "Turn": // ButtonTool Turn(); // Place code here break; case "Revert": // ButtonTool Revert(); // Place code here break; } } private void Query() { list = EntityHelper.GetData( "com.steering.mes.mcp.Mch.FrmMeasureResult.geRtdMeasurePosdataAll", new object[] { _judgeStove,_batchNo,_plineCode.ToString2(), "0" }, ob); rtdMeasurePosdataEntityBindingSource.DataSource = chkFlag.Checked ?list: list.Where(p => p.Flag == "00").ToList() ; var WasteList = list.Where(p => p.Flag == "10").ToList(); string message = "共" + list.Where(p=>p.Flag=="00").Select(p=>p.MatNo).Distinct().Count() + "支"; string messageW=" "; if(WasteList.Any()) messageW = " 已剔除" + WasteList.Count+ "条"; ugData.DisplayLayout.Bands[0].Summaries[0].DisplayFormat = message; ugData.DisplayLayout.Bands[0].Summaries[1].DisplayFormat = list.Where(p => p.Flag == "00" && p.Rk == "1").Sum(p => p.ActLen).ToString3(); ugData.DisplayLayout.Bands[0].Summaries[2].DisplayFormat = list.Where(p => p.Flag == "00" && p.Rk == "1").Sum(p => p.ActWeight).ToString3(); ugData.DisplayLayout.Bands[0].Summaries["Waste"].DisplayFormat = messageW; GridHelper.RefreshAndAutoSizeExceptColumns(ugData, "MatInfo" ); } private void Turn() { if(_jgxPlan==null) return; using (MesureDataPerWt mesureData = new MesureDataPerWt(ob,_jgxPlan)) { mesureData.ShowDialog(); if (mesureData.DialogResult == DialogResult.OK) { Query(); } } } private void Revert() { if (_jgxPlan == null) return; var ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.mcp.Mch.FrmMeasureResult"; ccp.MethodName = "Revert"; ccp.ServerParams = new object[] { _jgxPlan.JudgeStoveNo, _jgxPlan.BatchNo, _jgxPlan.PlineCode }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("操作成功")) { Query(); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } private void chkFlag_CheckedChanged(object sender, EventArgs e) { rtdMeasurePosdataEntityBindingSource.DataSource = chkFlag.Checked ?list: list.Where(p => p.Flag == "00").ToList() ; 按钮显示区域.Tools["Show"].SharedPropsInternal.Caption = chkFlag.Checked ? "隐藏已剔除" : "显示已剔除"; } private void ShowData() { chkFlag.Checked = !chkFlag.Checked; rtdMeasurePosdataEntityBindingSource.DataSource = chkFlag.Checked ? list : list.Where(p => p.Flag == "00").ToList(); 按钮显示区域.Tools["Show"].SharedPropsInternal.Caption = chkFlag.Checked ? "隐藏已剔除" : "显示已剔除"; } private void uteRemove_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e) { RtdMeasurePosdataEntity entity = ugData.ActiveRow.ListObject as RtdMeasurePosdataEntity; if (entity == null) return; if (entity.MatNoCx != null && !entity.MatNoCx.Equals("")) { MessageUtil.ShowTips("剔除失败,材料号不为空!!!"); return; } var ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.mcp.Mch.FrmMeasureResult"; ccp.MethodName = "DoRemove"; ccp.ServerParams = new object[] { _heatPlanId, entity.KeyId }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("成功")) { Query(); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } private void uteRecover_EditorButtonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e) { RtdMeasurePosdataEntity entity = ugData.ActiveRow.ListObject as RtdMeasurePosdataEntity; if (entity == null) return; var ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.mcp.Mch.FrmMeasureResult"; ccp.MethodName = "DoRecover"; ccp.ServerParams = new object[] { _heatPlanId, entity.KeyId }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { if (ccp.ReturnInfo.Equals("成功")) { Query(); } else { MessageUtil.ShowTips(ccp.ReturnInfo); } } } private void ultraButton1_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(utMatNo.Text)) { MessageBox.Show("请输入管号!"); return; } double len = 0; if (string.IsNullOrWhiteSpace(uteLen.Text) || !double.TryParse(uteLen.Text.Trim(),out len)) { MessageBox.Show("请输入正确的长度!"); return; } if (string.IsNullOrWhiteSpace(uteWt.Text) || !double.TryParse(uteWt.Text.Trim(), out len)) { MessageBox.Show("请输入正确的重量!"); return; } RtdMeasurePosdataEntity entity = new RtdMeasurePosdataEntity() { MatNo = utMatNo.Text, ActLen = decimal.Parse(uteLen.Text.Trim()), ActWeight = decimal.Parse(uteWt.Text.Trim()), MatInfo = uteMatInfo.Text, Flag = "00", JudgeStoveNo = _judgeStove, BatchNo = _batchNo, CreateUser = _userName, ProBc = uceBc.SelectedIndex<0?"":uceBc.SelectedItem.DataValue.ToString2(), ProBz = uceBz.SelectedIndex<0?"":uceBz.SelectedItem.DataValue.ToString2() }; var ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.mcp.Mch.FrmMeasureResult"; ccp.MethodName = "DoAddMeasureData"; ccp.ServerParams = new object[] { JSONFormat.Format(entity), _heatPlanId }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { MessageUtil.ShowTips(ccp.ReturnInfo); if (ccp.ReturnInfo.Equals("新增成功")) { Query(); } } } private void ubt_Click(object sender, EventArgs e) { GridHelper.ulGridToExcel( ultraGrid1 , "导入模板"); } private void btnExport_Click(object sender, EventArgs e) { List list = null; var openFileDialog = new OpenFileDialog { Filter = "测量点数据|*.xls;*.xlsx" }; if (openFileDialog.ShowDialog() == DialogResult.OK) { list = ReadExcelToRtdMeasurePosdataEntity(openFileDialog.FileName); } if (list == null) return; if (list.Count <= 0) { MessageBox.Show("Excel无数据"); return; } var ccp = new CoreClientParam(); ccp.ServerName = "com.steering.mes.mcp.Mch.FrmMeasureResult"; ccp.MethodName = "DoAddMeasureData"; ccp.ServerParams = new object[] { list.Select(JSONFormat.Format).ToList(), _heatPlanId }; ccp = ExecuteNonQuery(ccp, CoreInvokeType.Internal); if (ccp.ReturnCode != -1) { MessageUtil.ShowTips(ccp.ReturnInfo); if (ccp.ReturnInfo.Equals("新增成功")) { Query(); } } } public List ReadExcelToRtdMeasurePosdataEntity(string fileName) { //定义要返回的datatable对象 List RtdMeasurePosdataEntitys = new List(); try { if (!File.Exists(fileName)) { return null; } //根据指定路径读取文件 using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { //根据文件流创建excel数据结构 NPOI.SS.UserModel.IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(fs); //IWorkbook workbook = new HSSFWorkbook(fs); for (int k = 0; k < workbook.NumberOfSheets; k++) { NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(k); if (sheet != null) { //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = 1; i <= rowCount; ++i) { NPOI.SS.UserModel.IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null        if (row.Cells.Count < 3) { throw new Exception("无足够列"); } string bc ="", bz = ""; if (row.Cells.Count > 5) { bc = GetCellValue(row.Cells[4]); if (bc == "早") bc = "1"; if (bc == "中") bc = "2"; if (bc == "晚") bc = "3"; if (bc == "白") bc = "0"; if (bc != "" && bc != "1" && bc != "0" && bc != "2" && bc != "3") { throw new Exception("无法识别班次"); } bz = GetCellValue(row.Cells[5]); if (bz == "甲") bz = "1"; if (bz == "乙") bz = "2"; if (bz == "丙") bz = "3"; if (bz == "丁") bz = "4"; if (bz != "" && bz != "1" && bz != "4" && bz != "2" && bz != "3") { throw new Exception("无法识别班组"); } } RtdMeasurePosdataEntity entity = new RtdMeasurePosdataEntity() { MatNo = GetCellValue(row.Cells[0]), MatInfo= row.Cells.Count <4?"": GetCellValue(row.Cells[3]), Flag = "00", JudgeStoveNo = _judgeStove, BatchNo = _batchNo, CreateUser = _userName, ProBz = bz, ProBc = bc }; decimal actLen; decimal actWt; decimal.TryParse(GetCellValue(row.Cells[1]), out actLen); decimal.TryParse(GetCellValue(row.Cells[2]), out actWt); if (actLen == null || actLen <= 0) { throw new Exception("长度异常"); } if (actWt == null || actWt <= 0) { throw new Exception("重量异常"); } entity.ActLen = actLen; entity.ActWeight = actWt; RtdMeasurePosdataEntitys.Add(entity); } } } } return RtdMeasurePosdataEntitys; } catch (Exception ex) { MessageBox.Show("EXCEL格式错误:" + ex.Message); return null; } } private string GetCellValue(ICell cell) { string value=""; switch (cell.CellType) { case CellType.Blank: value = ""; break; case CellType.Numeric: short format = cell.CellStyle.DataFormat; value = cell.NumericCellValue.ToString2(); break; case CellType.String: value = cell.StringCellValue; break; } return value; } } }