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.

774 lines
24 KiB
C#

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
{
/// <summary>
/// 操作
/// </summary>
public class BaseAction
{
private static bool _isFirstRun = true; //是否首次执行
private static NetTypes _netType = NetTypes.Local; //持久保持上辅机版本信息(单机、网络)
#region RunIni
/// <summary>
/// 接口方法给Sender复制
/// </summary>
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<Control> GetAllForm()
{
List<Control> Result = new List<Control>();
List<Mesnac.PlugIn.View.IWorkbenchWindow> 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;
}
/// <summary>
/// 获取RunSchema配置文件中配置项的值
/// </summary>
/// <param name="key">配置型</param>
/// <param name="defaultValue">获取不到时的默认值</param>
/// <returns>返回对应配置项的值</returns>
public string GetConfigValue(string key, string defaultValue)
{
return Mesnac.Basic.RunSchema.Instance.GetConfigValue(key, defaultValue);
}
#endregion
#region GetAllComponents
private List<Component> getFormComponents(Control form)
{
List<Component> Result = new List<Component>();
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<Component> getAllFormAllComponents()
{
List<Component> Result = new List<Component>();
List<Control> formList = GetAllForm();
foreach (Control form in formList)
{
List<Component> clist = getFormComponents(form);
for (int i = 0; i < clist.Count; i++)
{
Result.Add(clist[i]);
}
}
return Result;
}
protected List<Component> GetThisFormAllComponents()
{
List<Component> Result = new List<Component>();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormComponents(form);
}
return Result;
}
protected List<Component> GetAllFormAllComponents()
{
List<Component> Result = new List<Component>();
List<Component> clist = new List<Component>();
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<Component> GetThisOrAllFormAllComponents()
{
List<Component> Result = new List<Component>();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormComponents(form);
}
else
{
Result = getAllFormAllComponents();
}
return Result;
}
public List<Component> GetAllComponents()
{
return GetThisFormAllComponents();
}
#endregion
#region GetAllControls
private void getFormAllControls(Control control, ref List<Control> all)
{
all.Add(control);
foreach (Control child in control.Controls)
{
getFormAllControls(child, ref all);
}
}
private List<Control> getFormAllControls(Control frm)
{
List<Control> all = new List<Control>();
if (frm != null)
{
all.Add(frm);
foreach (Control control in frm.Controls)
{
getFormAllControls(control, ref all);
}
}
List<Component> 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<Control> getAllFormAllControls(Control _this)
{
List<Control> Result = new List<Control>();
List<Control> formList = GetAllForm();
foreach (Control form in formList)
{
if (form.Equals(_this))
{
continue;
}
List<Control> clist = getFormAllControls(form);
for (int i = 0; i < clist.Count; i++)
{
Result.Add(clist[i]);
}
}
return Result;
}
protected List<Control> GetThisFormAllControls()
{
List<Control> Result = new List<Control>();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormAllControls(form);
}
return Result;
}
protected List<Control> GetAllFormAllControls()
{
List<Control> Result = new List<Control>();
List<Control> clist = new List<Control>();
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<Control> GetThisOrAllFormAllControls()
{
List<Control> Result = new List<Control>();
Control form = GetThisOwer();
if (form != null)
{
Result = getFormAllControls(form);
}
else
{
Result = getAllFormAllControls(form);
}
return Result;
}
public List<Control> 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<Control> GetAllFormControlById(string id)
{
List<Control> Result = new List<Control>();
foreach (Control control in GetAllFormAllControls())
{
if (control.Name.Equals(id, StringComparison.CurrentCultureIgnoreCase))
{
Result.Add(control);
}
}
return Result;
}
protected List<Control> GetThisOrAllFormControlById(string id)
{
List<Control> Result = new List<Control>();
Control thisControl = GetThisFormControlById(id);
if (thisControl != null)
{
Result.Add(thisControl);
return Result;
}
List<Control> clist = GetAllFormControlById(id);
foreach (Control c in clist)
{
Result.Add(c);
}
return Result;
}
public List<Control> GetControlsById(string id)
{
return GetThisOrAllFormControlById(id);
}
public Control GetControlById(string id)
{
return GetThisFormControlById(id);
}
#endregion
#region GetTControls
private List<T> getTControls<T>(List<Control> clist)
{
List<T> Result = new List<T>();
foreach (object control in clist)
{
if (control is T)
{
Result.Add((T)control);
}
}
return Result;
}
protected List<T> GetThisFormAllTControls<T>()
{
return getTControls<T>(GetThisFormAllControls());
}
protected List<T> GetAllFormAllTControls<T>()
{
return getTControls<T>(GetAllFormAllControls());
}
protected List<T> GetThisOrAllFormAllTControls<T>()
{
List<T> Result = new List<T>();
Result = GetThisFormAllTControls<T>();
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllTControls<T>();
}
public List<T> GetTControls<T>()
{
return GetThisFormAllTControls<T>();
}
#endregion
#region GetAllMCControls
private List<IBaseControl> getAllMCControls(List<Control> clist)
{
List<IBaseControl> Result = new List<IBaseControl>();
foreach (Control control in clist)
{
if (control is IBaseControl)
{
Result.Add(control as IBaseControl);
}
}
return Result;
}
protected List<IBaseControl> GetThisFormAllMCControls()
{
return getAllMCControls(GetThisFormAllControls());
}
protected List<IBaseControl> GetAllFormAllMCControls()
{
return getAllMCControls(GetAllFormAllControls());
}
protected List<IBaseControl> GetThisOrAllFormAllMCControls()
{
List<IBaseControl> Result = GetThisFormAllMCControls();
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllMCControls();
}
public List<IBaseControl> GetAllMCControls()
{
return GetThisFormAllMCControls();
}
#endregion
#region GetMCControlByKey
private List<IBaseControl> getMCControlByKey(string key, List<IBaseControl> clist)
{
List<IBaseControl> Result = new List<IBaseControl>();
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<IBaseControl> GetThisFormMCControlByKey(string key)
{
return getMCControlByKey(key, GetThisFormAllMCControls());
}
protected List<IBaseControl> GetAllFormMCControlByKey(string key)
{
return getMCControlByKey(key, GetAllFormAllMCControls());
}
protected List<IBaseControl> GetThisOrAllFormMCControlByKey(string key)
{
List<IBaseControl> Result = GetThisFormMCControlByKey(key);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormMCControlByKey(key);
}
public List<IBaseControl> GetMCControlByKey(string key)
{
return GetThisFormMCControlByKey(key);
}
#endregion
#region GetAllMCControlsByOption
private List<IBaseControl> getAllMCControlsByOption(DbOptionTypes dbopttype, List<IBaseControl> clist)
{
List<IBaseControl> Result = new List<IBaseControl>();
foreach (IBaseControl control in clist)
{
if (control.DbOptionType == dbopttype)
{
Result.Add(control);
}
}
return Result;
}
protected List<IBaseControl> GetThisFormAllMCControlsByOption(DbOptionTypes dbopttype)
{
return getAllMCControlsByOption(dbopttype, GetThisFormAllMCControls());
}
protected List<IBaseControl> GetAllFormAllMCControlsByOption(DbOptionTypes dbopttype)
{
return getAllMCControlsByOption(dbopttype, GetAllFormAllMCControls());
}
protected List<IBaseControl> GetThisOrAllFormMCControlsByOption(DbOptionTypes dbopttype)
{
List<IBaseControl> Result = GetThisFormAllMCControlsByOption(dbopttype);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllMCControlsByOption(dbopttype);
}
public List<IBaseControl> GetAllMCControlsByOption(DbOptionTypes dbopttype)
{
return GetThisFormAllMCControlsByOption(dbopttype);
}
#endregion
#region GetAllMCPurviewControls
/// <summary>
/// 获取所有软控权限控件
/// </summary>
/// <returns></returns>
public List<IPurviewControl> GetAllMCPurviewControls()
{
List<IPurviewControl> Result = new List<IPurviewControl>();
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
/// <summary>
/// 显示界面提示重载方法
/// </summary>
/// <param name="msg"></param>
/// <param name="title"></param>
/// <param name="buttons"></param>
/// <param name="icon"></param>
public void ShowMsg(string msg, string caption, MessageBoxButtons buttons, MessageBoxIcon icon)
{
List<IBaseControl> 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;
}
}
}
/// <summary>
/// 显示界面提示
/// </summary>
/// <param name="msg"></param>
public void ShowMsg(string msg)
{
ShowMsg(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.None);
}
#endregion
#region Language
/// <summary>
/// 获取语言信息
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
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 通用方法、属性封装
/// <summary>
/// 单击版、网络版枚举类型0为单机版1为网络版
/// </summary>
public enum NetTypes
{
Local = 0, Net = 1
}
/// <summary>
/// 获取当前系统是单机版还是网络版0为单机版1为网络版
/// </summary>
public NetTypes NetType
{
get
{
return BaseAction._netType;
}
}
/// <summary>
/// 获取当前系统原始版本0为单机版1为网络版
/// </summary>
public NetTypes OriginalNetType
{
get
{
string result = this.GetConfigValue("NetType", "0");
if (result == "0")
{
return NetTypes.Local;
}
else
{
return NetTypes.Net;
}
}
}
/// <summary>
/// 获取当前系统机台号
/// </summary>
public string CurrEquipCode
{
get
{
string result = this.GetConfigValue("EquipCode", String.Empty);
return result;
}
}
/// <summary>
/// 获取是否有胶料称客户端标志01
/// </summary>
public int FarControlType
{
get
{
int result = Convert.ToInt32(this.GetConfigValue("FarControlType", "0"));
return result;
}
}
/// <summary>
/// 设置全局版本信息
/// </summary>
/// <param name="netType"></param>
public void SetGlobalNetType(NetTypes netType)
{
BaseAction._netType = netType;
}
#endregion
#endregion
}
}