using Mesnac.Controls.Base;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.ComponentModel;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Mesnac.Action.Base
{
///
/// 操作
///
public class BaseAction
{
private static bool _isFirstRun = true; //是否首次执行
private static NetTypes _netType = NetTypes.Local; //持久保持上辅机版本信息(单机、网络)
#region RunIni
///
/// 接口方法,给Sender复制
///
private RuntimeParameter _runtimeParameter { get; set; }
public void RunIni(RuntimeParameter runtime)
{
this._runtimeParameter = runtime;
if (_isFirstRun)
{
if (runtime != null)
{
LanguageService.Instance.ProjectWizardName = runtime.ProjectWizardName;
string result = this.GetConfigValue("NetType", "0");
if (result == "0")
{
BaseAction._netType = NetTypes.Local;
}
else
{
BaseAction._netType = NetTypes.Net;
}
}
_isFirstRun = false;
}
}
#endregion
#region GetForm
private Control getThisForm(Control control)
{
Form form = control.FindForm();
if (form != null)
{
return form;
}
return control;
}
public Control GetThisOwer()
{
if (this._runtimeParameter == null) return null;
if (this._runtimeParameter.Sender == null) return null;
try
{
if ((this._runtimeParameter.Sender is Control))
{
return getThisForm(this._runtimeParameter.Sender as Control);
}
if (this._runtimeParameter.Sender is ToolStripItem)
{
return getThisForm((this._runtimeParameter.Sender as ToolStripItem).Owner);
}
}
catch { };
if (this._runtimeParameter.BaseControl.MCRoot is Control)
{
return this._runtimeParameter.BaseControl.MCRoot as Control;
}
return null;
}
private List GetAllForm()
{
List Result = new List();
List windows = Mesnac.Gui.Workbench.WorkbenchSingleton.Workbench.GetCustomerViews();
foreach (Mesnac.PlugIn.View.IWorkbenchWindow win in windows)
{
Control form = win as Form;
if (form != null)
{
Result.Add(form);
}
}
return Result;
}
///
/// 获取RunSchema配置文件中配置项的值
///
/// 配置型
/// 获取不到时的默认值
/// 返回对应配置项的值
public string GetConfigValue(string key, string defaultValue)
{
return Mesnac.Basic.RunSchema.Instance.GetConfigValue(key, defaultValue);
}
#endregion
#region GetAllComponents
private List getFormComponents(Control form)
{
List Result = new List();
IContainer container = form.Container;
if (container == null)
{
return Result;
}
PropertyInfo pi = ((object)form).GetType().GetProperty("Container");
if (pi != null)
{
container = pi.GetValue(form, null) as IContainer;
}
foreach (Component component in container.Components)
{
Result.Add(component);
}
return Result;
}
private List getAllFormAllComponents()
{
List Result = new List();
List formList = GetAllForm();
foreach (Control form in formList)
{
List clist = getFormComponents(form);
for (int i = 0; i < clist.Count; i++)
{
Result.Add(clist[i]);
}
}
return Result;
}
protected List GetThisFormAllComponents()
{
List Result = new List();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormComponents(form);
}
return Result;
}
protected List GetAllFormAllComponents()
{
List Result = new List();
List clist = new List();
Control form = GetThisOwer();
if (form != null)
{
clist = getFormComponents(form);
}
foreach (Component c in clist)
{
Result.Add(c);
}
clist.Clear();
clist = getAllFormAllComponents();
foreach (Component c in clist)
{
Result.Add(c);
}
return Result;
}
protected List GetThisOrAllFormAllComponents()
{
List Result = new List();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormComponents(form);
}
else
{
Result = getAllFormAllComponents();
}
return Result;
}
public List GetAllComponents()
{
return GetThisFormAllComponents();
}
#endregion
#region GetAllControls
private void getFormAllControls(Control control, ref List all)
{
all.Add(control);
foreach (Control child in control.Controls)
{
getFormAllControls(child, ref all);
}
}
private List getFormAllControls(Control frm)
{
List all = new List();
if (frm != null)
{
all.Add(frm);
foreach (Control control in frm.Controls)
{
getFormAllControls(control, ref all);
}
}
List lst = getFormComponents(frm);
foreach (Control c in all)
{
if (c is IBaseControl)
{
IBaseControl control = c as IBaseControl;
if (control.IsDbControl&& (!string.IsNullOrWhiteSpace(control.MCDataSourceID)))
{
foreach (Component cm in lst)
{
MCDataSource datasource = cm as MCDataSource;
if (datasource != null)
{
if (cm.Site.Name.Equals(control.MCDataSourceID, StringComparison.CurrentCultureIgnoreCase))
{
control.MCDataSource = datasource;
break;
}
}
}
}
}
}
return all;
}
private List getAllFormAllControls(Control _this)
{
List Result = new List();
List formList = GetAllForm();
foreach (Control form in formList)
{
if (form.Equals(_this))
{
continue;
}
List clist = getFormAllControls(form);
for (int i = 0; i < clist.Count; i++)
{
Result.Add(clist[i]);
}
}
return Result;
}
protected List GetThisFormAllControls()
{
List Result = new List();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormAllControls(form);
}
return Result;
}
protected List GetAllFormAllControls()
{
List Result = new List();
List clist = new List();
Control form = GetThisOwer();
if (form != null)
{
clist = getFormAllControls(form);
}
foreach (Control c in clist)
{
Result.Add(c);
}
clist.Clear();
clist = getAllFormAllControls(form);
foreach (Control c in clist)
{
Result.Add(c);
}
return Result;
}
protected List GetThisOrAllFormAllControls()
{
List Result = new List();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormAllControls(form);
}
else
{
Result = getAllFormAllControls(form);
}
return Result;
}
public List GetAllControls()
{
return GetThisFormAllControls();
}
#endregion
#region GetControlById
protected Control GetThisFormControlById(string id)
{
Control Result = null;
foreach (Control control in GetThisFormAllControls())
{
if (control.Name.Equals(id, StringComparison.CurrentCultureIgnoreCase))
{
return control;
}
}
return Result;
}
protected List GetAllFormControlById(string id)
{
List Result = new List();
foreach (Control control in GetAllFormAllControls())
{
if (control.Name.Equals(id, StringComparison.CurrentCultureIgnoreCase))
{
Result.Add(control);
}
}
return Result;
}
protected List GetThisOrAllFormControlById(string id)
{
List Result = new List();
Control thisControl = GetThisFormControlById(id);
if (thisControl != null)
{
Result.Add(thisControl);
return Result;
}
List clist = GetAllFormControlById(id);
foreach (Control c in clist)
{
Result.Add(c);
}
return Result;
}
public List GetControlsById(string id)
{
return GetThisOrAllFormControlById(id);
}
public Control GetControlById(string id)
{
return GetThisFormControlById(id);
}
#endregion
#region GetTControls
private List getTControls(List clist)
{
List Result = new List();
foreach (object control in clist)
{
if (control is T)
{
Result.Add((T)control);
}
}
return Result;
}
protected List GetThisFormAllTControls()
{
return getTControls(GetThisFormAllControls());
}
protected List GetAllFormAllTControls()
{
return getTControls(GetAllFormAllControls());
}
protected List GetThisOrAllFormAllTControls()
{
List Result = new List();
Result = GetThisFormAllTControls();
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllTControls();
}
public List GetTControls()
{
return GetThisFormAllTControls();
}
#endregion
#region GetAllMCControls
private List getAllMCControls(List clist)
{
List Result = new List();
foreach (Control control in clist)
{
if (control is IBaseControl)
{
Result.Add(control as IBaseControl);
}
}
return Result;
}
protected List GetThisFormAllMCControls()
{
return getAllMCControls(GetThisFormAllControls());
}
protected List GetAllFormAllMCControls()
{
return getAllMCControls(GetAllFormAllControls());
}
protected List GetThisOrAllFormAllMCControls()
{
List Result = GetThisFormAllMCControls();
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllMCControls();
}
public List GetAllMCControls()
{
return GetThisFormAllMCControls();
}
#endregion
#region GetMCControlByKey
private List getMCControlByKey(string key, List clist)
{
List Result = new List();
if (this._runtimeParameter != null && this._runtimeParameter.Sender != null)
{
foreach (IBaseControl control in clist)
{
if (control.MCKey != null && control.MCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
Result.Add(control);
}
}
}
return Result;
}
protected List GetThisFormMCControlByKey(string key)
{
return getMCControlByKey(key, GetThisFormAllMCControls());
}
protected List GetAllFormMCControlByKey(string key)
{
return getMCControlByKey(key, GetAllFormAllMCControls());
}
protected List GetThisOrAllFormMCControlByKey(string key)
{
List Result = GetThisFormMCControlByKey(key);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormMCControlByKey(key);
}
public List GetMCControlByKey(string key)
{
return GetThisFormMCControlByKey(key);
}
#endregion
#region GetAllMCControlsByOption
private List getAllMCControlsByOption(DbOptionTypes dbopttype, List clist)
{
List Result = new List();
foreach (IBaseControl control in clist)
{
if (control.DbOptionType == dbopttype)
{
Result.Add(control);
}
}
return Result;
}
protected List GetThisFormAllMCControlsByOption(DbOptionTypes dbopttype)
{
return getAllMCControlsByOption(dbopttype, GetThisFormAllMCControls());
}
protected List GetAllFormAllMCControlsByOption(DbOptionTypes dbopttype)
{
return getAllMCControlsByOption(dbopttype, GetAllFormAllMCControls());
}
protected List GetThisOrAllFormMCControlsByOption(DbOptionTypes dbopttype)
{
List Result = GetThisFormAllMCControlsByOption(dbopttype);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllMCControlsByOption(dbopttype);
}
public List GetAllMCControlsByOption(DbOptionTypes dbopttype)
{
return GetThisFormAllMCControlsByOption(dbopttype);
}
#endregion
#region GetAllMCPurviewControls
///
/// 获取所有软控权限控件
///
///
public List GetAllMCPurviewControls()
{
List Result = new List();
foreach (Control control in GetThisFormAllControls())
{
if (control is IPurviewControl)
{
Result.Add(control as IPurviewControl);
}
}
return Result;
}
#endregion
#region 日志、提示等基本函数
#region 判断IBaseControl数据库操作类型
public bool DbOptionTypesIsQuery(IBaseControl baseControl)
{
return baseControl.DbOptionType == DbOptionTypes.Query || baseControl.DbOptionType == DbOptionTypes.QueryAndModify;
}
public bool DbOptionTypesIsModify(IBaseControl baseControl)
{
return baseControl.DbOptionType == DbOptionTypes.Modify || baseControl.DbOptionType == DbOptionTypes.QueryAndModify;
}
#endregion
#region ShowMsg
///
/// 显示界面提示重载方法
///
///
///
///
///
public void ShowMsg(string msg, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
List msgControls = this.GetThisFormMCControlByKey("ShowMsgControl");
if (msgControls.Count == 0)
{
if (!string.IsNullOrWhiteSpace(msg))
{
MessageBox.Show(msg, caption, buttons, icon);
}
}
else
{
foreach (IBaseControl control in msgControls)
{
control.MCValue = msg;
}
}
}
///
/// 显示界面提示
///
///
public void ShowMsg(string msg)
{
ShowMsg(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
}
#endregion
#region Language
///
/// 获取语言信息
///
///
///
public string Language(int i)
{
return Mesnac.Action.Base.LanguageService.Instance.Read(i);
}
#endregion
#region 重写Log4net方法
public void LogDebug(object message)
{
ICSharpCode.Core.LoggingService.Debug(message);
}
public void LogDebugFormatted(string format, params object[] args)
{
ICSharpCode.Core.LoggingService.DebugFormatted(format, args);
}
public void LogInfo(object message)
{
ICSharpCode.Core.LoggingService.Info(message);
}
public void LogInfoFormatted(string format, params object[] args)
{
ICSharpCode.Core.LoggingService.InfoFormatted(format, args);
}
public void LogWarn(object message)
{
ICSharpCode.Core.LoggingService.Warn(message);
}
public void LogWarn(object message, Exception exception)
{
ICSharpCode.Core.LoggingService.Warn(message, exception);
}
public void LogWarnFormatted(string format, params object[] args)
{
ICSharpCode.Core.LoggingService.WarnFormatted(format, args);
}
public void LogError(object message)
{
ICSharpCode.Core.LoggingService.Error(message);
}
public void LogError(object message, Exception exception)
{
ICSharpCode.Core.LoggingService.Error(message, exception);
}
public void LogErrorFormatted(string format, params object[] args)
{
ICSharpCode.Core.LoggingService.ErrorFormatted(format, args);
}
public void LogFatal(object message)
{
ICSharpCode.Core.LoggingService.Fatal(message);
}
public void LogFatal(object message, Exception exception)
{
ICSharpCode.Core.LoggingService.Fatal(message, exception);
}
public void LogFatalFormatted(string format, params object[] args)
{
ICSharpCode.Core.LoggingService.FatalFormatted(format, args);
}
public bool IsLogDebugEnabled
{
get
{
return ICSharpCode.Core.LoggingService.IsDebugEnabled;
}
}
public bool IsLogInfoEnabled
{
get
{
return ICSharpCode.Core.LoggingService.IsInfoEnabled;
}
}
public bool IsLogWarnEnabled
{
get
{
return ICSharpCode.Core.LoggingService.IsWarnEnabled;
}
}
public bool IsLogErrorEnabled
{
get
{
return ICSharpCode.Core.LoggingService.IsErrorEnabled;
}
}
public bool IsLogFatalEnabled
{
get
{
return ICSharpCode.Core.LoggingService.IsFatalEnabled;
}
}
#endregion
#region 通用方法、属性封装
///
/// 单击版、网络版枚举类型:0为单机版,1为网络版
///
public enum NetTypes
{
Local = 0, Net = 1
}
///
/// 获取当前系统是单机版还是网络版:0为单机版,1为网络版
///
public NetTypes NetType
{
get
{
return BaseAction._netType;
}
}
///
/// 获取当前系统原始版本:0为单机版,1为网络版
///
public NetTypes OriginalNetType
{
get
{
string result = this.GetConfigValue("NetType", "0");
if (result == "0")
{
return NetTypes.Local;
}
else
{
return NetTypes.Net;
}
}
}
///
/// 获取当前系统机台号
///
public string CurrEquipCode
{
get
{
string result = this.GetConfigValue("EquipCode", String.Empty);
return result;
}
}
///
/// 获取是否有胶料称客户端标志,0:无,1:有
///
public int FarControlType
{
get
{
int result = Convert.ToInt32(this.GetConfigValue("FarControlType", "0"));
return result;
}
}
///
/// 设置全局版本信息
///
///
public void SetGlobalNetType(NetTypes netType)
{
BaseAction._netType = netType;
}
#endregion
#endregion
}
}