using ICSharpCode.Core; using Mesnac.Action.Base; using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Codd.Session; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Mesnac.Action.ChemicalWeighing.Warehouse { public class ImportAction : ChemicalWeighingAction, IAction { private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption")); //提示 private DbMCControl _clientGridControl = null; private RuntimeParameter _runtime; public static event EventHandler OnSubRefresh; public void Run(RuntimeParameter runtime) { base.RunIni(runtime); this._runtime = runtime; DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Hw_WareHouse").FirstOrDefault(); if (clientGridControl == null || !(clientGridControl.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Error("{投料管理—添加物料}缺少管理控件..."); return; } this._clientGridControl = clientGridControl; DataGridView gridView = this._clientGridControl.BaseControl as DataGridView; if (gridView.SelectedRows.Count != 1) { string msg1_1 = StringParser.Parse("请选择一行要釜信息"); MessageBox.Show(msg1_1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); this._runtime.IsReturn = true; return; } string msg = "导入操作会覆盖当前所有信息!是否继续?"; //导入操作会覆盖当前所有信息!是否继续? if (MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } base.RunIni(runtime); //必须要调用的 ICSharpCode.Core.LoggingService.Debug("投料参数-导入..."); DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { ICSharpCode.Core.LoggingService.Error("获取本地数据连接失败..."); return; } OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "xls files(*.xls)|*.xls"; ofd.AddExtension = true; DialogResult result = ofd.ShowDialog(); if (result == DialogResult.OK) { try { string fileName = ofd.FileName; if (!String.IsNullOrEmpty(fileName)) { DataSet ds = Mesnac.Basic.DataToFileHandler.Instance.FromExcel(fileName); //判断导入Excel表格式的合法性 if (!ds.Tables[0].Columns.Contains("PId") || !ds.Tables[0].Columns.Contains("Material_Code") || !ds.Tables[0].Columns.Contains("MaterialID") || !ds.Tables[0].Columns.Contains("MaterialName") || !ds.Tables[0].Columns.Contains("SetWeight")) { string msg4 = "导入的Excel表格式不正确!请检查后重试!"; //导入的Excel表格式不正确!请检查后重试! MessageBox.Show(msg4); return; } var selectId = gridView.SelectedRows[0].Cells["ID"].Value.ToString(); if (string.IsNullOrEmpty(selectId)) { string msg4 = "请选择反应釜!"; //导入的Excel表格式不正确!请检查后重试! MessageBox.Show(msg4); return; } int Id = int.Parse(selectId); if (ds != null && ds.Tables.Count > 0) { WarehouseHelper.DeleteAllMaterial(Id); string sqlstr = @"insert into Hw_WareHouse_Sub(MainId,PId, MaterialID, MaterialName,Material_Code, SetWeight, CreateTime) values (@MainId,@PId, @MaterialID,@MaterialName,@Material_Code, @SetWeight, @CreateTime)"; foreach (DataRow row in ds.Tables[0].Rows) { dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; dbHelper.CommandText = sqlstr; dbHelper.AddParameter("@MainId", selectId); dbHelper.AddParameter("@PId", row["PId"]); dbHelper.AddParameter("@MaterialID", row["MaterialID"]); dbHelper.AddParameter("@MaterialName", row["MaterialName"]); dbHelper.AddParameter("@Material_Code", row["Material_Code"]); dbHelper.AddParameter("@SetWeight", row["SetWeight"]); dbHelper.AddParameter("@CreateTime",DateTime.Now); dbHelper.ExecuteNonQuery(); } string msg1 = "从Excel导入物料数据完毕!"; ICSharpCode.Core.LoggingService.Info(msg1); #region 记录操作日志 base.DBLog(msg1); #endregion MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg2")); //从Excel中导入报警参数数据失败:Excel中没有数据! ICSharpCode.Core.LoggingService.Warn(msg2); #region 记录操作日志 base.DBLog(msg2); #endregion MessageBox.Show(String.Format(msg2), Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning); } } if (OnSubRefresh != null) { OnSubRefresh(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty); } } catch (Exception ex) { string msg3 = "从Excel导入物料数据失败:{0}!"; //从Excel导入报警参数数据失败:{0}! msg3 = String.Format(msg3, ex.Message); ICSharpCode.Core.LoggingService.Error(msg3); #region 记录操作日志 base.DBLog(msg3); #endregion MessageBox.Show(msg3, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning); } //刷新DataGridView数据 DbMCControl dgPmtAlarmInfo = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_Alarm").FirstOrDefault(); if (dgPmtAlarmInfo == null || !(dgPmtAlarmInfo.BaseControl is DataGridView)) { ICSharpCode.Core.LoggingService.Warn("报警参数-导入-缺少报警参数DataGridView控件..."); runtime.IsReturn = false; return; } DataTable table = dbHelper.GetDataTableBySql(dgPmtAlarmInfo.BaseControl.ActionDataSource); dgPmtAlarmInfo.BaseControl.BindDataSource = table; } } } }