You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lj_plc/Actions/ChemicalWeighing/Mesnac.Action.ChemicalWeighing/Alarm/PmtAlarm/ImportAction.cs

146 lines
7.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
namespace Mesnac.Action.ChemicalWeighing.Alarm.PmtAlarm
{
/// <summary>
/// 导入报警参数业务
/// </summary>
public class ImportAction: ChemicalWeighingAction, IAction
{
private string caption = StringParser.Parse(ResourceService.GetString("Mesnac_Dialog_Caption")); //提示
public void Run(RuntimeParameter runtime)
{
string msg = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg")); //导入操作会覆盖当前所有报警信息!是否继续?
if (MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{
return;
}
base.RunIni(runtime); //必须要调用的
ICSharpCode.Core.LoggingService<ImportAction>.Debug("报警参数-导入...");
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
ICSharpCode.Core.LoggingService<ImportAction>.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("Alarm_PLC") ||
!ds.Tables[0].Columns.Contains("Alarm_Block") ||
!ds.Tables[0].Columns.Contains("Alarm_Word") ||
!ds.Tables[0].Columns.Contains("Alarm_Bit") ||
!ds.Tables[0].Columns.Contains("Alarm_Cn_Info") ||
!ds.Tables[0].Columns.Contains("Alarm_Other_Info"))
{
string msg4 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg4")); //导入的Excel表格式不正确请检查后重试
MessageBox.Show(msg4);
return;
}
string del = "truncate table Pmt_Alarm";
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = del;
dbHelper.ExecuteNonQuery();
//truncate table PmtAlarm
if (ds != null && ds.Tables.Count > 0)
{
string sqlstr = @"insert into Pmt_Alarm(Equip_Code, Alarm_PLC, Alarm_Block, Alarm_Word, Alarm_Bit, Alarm_Cn_Info, Alarm_Other_Info, Plc_Type)
values (@EquipCode, @AlarmPLC, @AlarmBlock, @AlarmAddress, @AlarmBit, @AlarmName, @AlarmPosition, @PlcType)";
foreach (DataRow row in ds.Tables[0].Rows)
{
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@EquipCode", row["Equip_Code"]);
dbHelper.AddParameter("@AlarmPLC", row["Alarm_PLC"]);
dbHelper.AddParameter("@AlarmBlock", row["Alarm_Block"]);
dbHelper.AddParameter("@AlarmAddress", row["Alarm_Word"]);
dbHelper.AddParameter("@AlarmBit", row["Alarm_Bit"]);
//dbHelper.AddParameter("@AlarmCode", row["AlarmCode"]);
dbHelper.AddParameter("@AlarmName", row["Alarm_Cn_Info"]);
dbHelper.AddParameter("@AlarmPosition", row["Alarm_Other_Info"]);
dbHelper.AddParameter("@PlcType", 4);
//dbHelper.AddParameter("@Remark", row["Remark"]);
//dbHelper.AddParameter("@RecordTime", Mesnac.Basic.DataProcessor.RowValue(row, "RecordTime", DateTime.Now));
//dbHelper.AddParameter("@RecordUser", Mesnac.Basic.DataProcessor.RowValue(row, "RecordUser", Mesnac.Basic.UserInfo.Instance.UserName));
//dbHelper.AddParameter("@DataSource", Mesnac.Basic.DataProcessor.RowValue(row, "DataSource", "2")); //2代表来自外部文件
dbHelper.ExecuteNonQuery();
}
string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg1")); //从Excel导入报警参数数据完毕!
ICSharpCode.Core.LoggingService<ImportAction>.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<ImportAction>.Warn(msg2);
#region 记录操作日志
base.DBLog(msg2);
#endregion
MessageBox.Show(String.Format(msg2), Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
catch (Exception ex)
{
string msg3 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Alarm_PmtAlarm_ImportAction_msg3")); //从Excel导入报警参数数据失败:{0}!
msg3 = String.Format(msg3, ex.Message);
ICSharpCode.Core.LoggingService<ImportAction>.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<ImportAction>.Warn("报警参数-导入-缺少报警参数DataGridView控件...");
runtime.IsReturn = false;
return;
}
DataTable table = dbHelper.GetDataTableBySql(dgPmtAlarmInfo.BaseControl.ActionDataSource);
dgPmtAlarmInfo.BaseControl.BindDataSource = table;
}
}
}
}