using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Xml; using System.Data; using System.IO; using ICSharpCode.Core; using Mesnac.Basic; using Mesnac.Equips; using Mesnac.Codd.Session; using Mesnac.Action.Base; using Mesnac.Controls.Base; namespace Mesnac.Action.ChemicalWeighing { /// /// 小料称量系统业务基类 /// public class ChemicalWeighingAction : DatabaseAction { private static bool _isFirstRun = true; //是否首次运行 #region RunIni /// /// 初始化 /// /// private void Ini(RuntimeParameter runtime) { if (_isFirstRun) { Global.PublicVar.Instance.ProjectWizardName = runtime.ProjectWizardName; _isFirstRun = false; } } /// /// 接口方法 /// /// public new void RunIni(RuntimeParameter runtime) { this.Ini(runtime); base.RunIni(runtime); //必须调用 } #endregion #region 日志方法 /// /// 数据库日志记录 /// /// 操作类别 /// 日志标题 /// 日志内容 public void DBLog(string operateType, string caption, string content) { BaseDataHelper.DBLog(this.FormGUID, this.FormText, this.FunctionGUID, this.FunctionText, String.Empty, operateType, caption, content); } /// /// 数据库日志记录 /// /// 日志内容 public void DBLog(string content) { BaseDataHelper.DBLog(this.FormGUID, this.FormText, this.FunctionGUID, this.FunctionText, String.Empty, this.FormText, this.FunctionText, content); } #endregion #region 辅助方法 #region 刷新自定义设备变量 /// /// 异步刷新自定义设备变量 /// public void RefreshCustomEquip() { try { ThreadPool.QueueUserWorkItem(new WaitCallback(this.ReadEquip)); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("异步刷新自定义设备变量失败:" + ex.Message); } } private void ReadEquip(Object stateInfo) { try { string equipName = base.GetConfigValue("Customer.EquipName", "FeedingCustomer"); //获取PLC设备名称 if (Mesnac.Equips.Factory.Instance.AllEquips.ContainsKey(equipName)) { Mesnac.Equips.Factory.Instance.AllEquips[equipName].Read(); } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("刷新自定义设备变量失败:" + ex.Message); } } #endregion #region 获取界面数据库控件值 /// /// 获取配方界面字段值 /// /// 界面字段的业务标识 /// 默认值 /// 返回string类型的配方界面字段值 public string GetValueByMCKey(string key, string defaultValue) { string result = defaultValue; DbMCControl control = null; List controls = base.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, key); foreach (DbMCControl c in controls) { if (c.BaseControl.DbOptionType == DbOptionTypes.Modify || c.BaseControl.DbOptionType == DbOptionTypes.QueryAndModify) { control = c; break; } } if (control != null) { if (control.BaseControl is CheckBox) { if ((control.BaseControl as CheckBox).Checked) { return "1"; } else { return "0"; } } else { object value = control.BaseControl.MCValue; if (value != null) { return value.ToString(); } } } return defaultValue; } /// /// 获取配方界面字段值 /// /// 界面字段的业务标识 /// 默认值 /// 返回int类型的配方界面字段值 public int GetValueByMCKey(string key, int defaultValue) { int result = defaultValue; string strResult = GetValueByMCKey(key, String.Empty); if (int.TryParse(strResult, out result)) { return result; } return defaultValue; } /// /// 获取配方界面字段值 /// /// 界面字段的业务标识 /// 默认值 /// 返回float格式的配方界面字段值 public float GetValueByMCKey(string key, float defaultValue) { float result = defaultValue; string strResult = GetValueByMCKey(key, String.Empty); if (float.TryParse(strResult, out result)) { return result; } return defaultValue; } /// /// 获取界面字段值 /// /// /// /// public decimal GetValueByMCKey(string key, decimal defaultValue) { decimal result = defaultValue; string strResult = GetValueByMCKey(key, String.Empty); if (decimal.TryParse(strResult, out result)) { return result; } return defaultValue; } /// /// 获取配方界面字段值 /// /// 界面字段的业务标识 /// 默认值 /// 返回DateTime格式的配方界面字段值 public DateTime GetValueByMCKey(string key, DateTime defaultValue) { DateTime result = defaultValue; string strResult = GetValueByMCKey(key, String.Empty); if (DateTime.TryParse(strResult, out result)) { return result; } return defaultValue; } #endregion #region 设置界面数据库控件值 /// /// 设置界面数据库控件值 /// /// 界面字段的业务标识 /// 要设置的值 /// 返回字符串格式的配方界面字段值 public void SetValueByMCKey(string key, object value) { DbMCControl control = null; List controls = base.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, key); foreach (DbMCControl c in controls) { if (c.BaseControl.DbOptionType == DbOptionTypes.Modify || c.BaseControl.DbOptionType == DbOptionTypes.QueryAndModify) { control = c; break; } } if (control != null) { control.BaseControl.MCValue = value; } } #endregion #region 设置GridViewRow背景色 public static void SetBackColor(DataGridView grid, string columnName, object filterValue, System.Drawing.Color c) { lock (String.Empty) { try { if (grid == null) { ICSharpCode.Core.LoggingService.Warn("设置GridViewRow背景色失败:网格控件为null"); return; } foreach (DataGridViewRow row in grid.Rows) { if (filterValue.Equals(row.Cells[columnName].Value)) { row.DefaultCellStyle.BackColor = c; } } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("设置GridViewRow背景色失败:" + ex.Message); } } } #endregion #endregion } }