#region
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
#endregion
namespace CSFrameworkV5.Library.CommonClass
{
///
/// 快速命令处理程序
///
public class CommandHandler
{
private static List _AllCommands = null;
///
/// 系统定义的所有命令
///
private static List AllCommands
{
get
{
if (_AllCommands == null)
{
//模拟命令表(从数据库获取)
var dt = new DataTable("sys_Commands");
dt.Columns.Add("CommandCode",
typeof(string)); //命令,如:PO,PO1,PO2,SO,HELP,LK
dt.Columns.Add("CommandName",
typeof(string)); //命令名称,PO-打开采购订单
dt.Columns.Add("CommandType",
typeof(string)); //命令类型:OpenForm-打开窗体,Add,Search,Other
dt.Columns.Add("FormNamespace", typeof(string)); //窗体的命名空间
dt.Columns.Add("MenuName",
typeof(string)); //打开此窗体关联的菜单名称(模块主窗体内定义)
dt.Rows.Add("PO", "打开采购订单", "OpenForm",
"CSFrameworkV5.DemoPurchaseModule.frmPO", "menuItemPO");
dt.Rows.Add("PO1", "采购订单-新增", "Add",
"CSFrameworkV5.DemoPurchaseModule.frmPO", "menuItemPO");
dt.Rows.Add("PO2", "采购订单-搜索", "Search",
"CSFrameworkV5.DemoPurchaseModule.frmPO", "menuItemPO");
dt.Rows.Add("SO", "打开销售订单", "OpenForm",
"CSFrameworkV5.DemoSalesModule.frmSO",
"menuSalesOrder");
dt.Rows.Add("HELP", "打开帮助文档", "Other", "", "");
dt.Rows.Add("LK", "锁定主界面", "Other", "", "");
dt.AcceptChanges();
//将DataTable转换为对象列表
var list = new ModelConverter().FillModel(dt);
return list;
}
return _AllCommands;
}
}
///
/// 执行命令
///
/// 主窗体实例
/// 命令文本
///
public static object Execute(IMdiForm instance, string cmdText)
{
//匹配命令字符,返回对象实例
var cmd = AllCommands.Find(delegate(QuickCommand p)
{
return p.Match(cmdText);
});
if (cmd == null)
throw new Exception("无效的命令!参考:PO,PO1,PO2,SO,LK,HELP");
//界面操作通用处理方法
if (cmd.IsFormAction) return instance.OpenModuleForm(cmd);
//锁定用户界面命令
if (cmd.CommandCode == "LK")
return frmLock.ExecuteLock(instance as Form);
//打开帮助文档
if (cmd.CommandCode == "HELP") HelpDoc.HelpAllFile(HelpDoc.CSHelp);
//
//其它命令
//
return null;
}
}
///
/// 命令类型
///
public enum QuickCommandType
{
OpenForm,
Add,
Search,
Other
}
///
/// 命令对象
///
public class QuickCommand
{
public string Action { get; set; }
public string CommandCode { get; set; }
public string CommandName { get; set; }
public string CommandType { get; set; }
public string FormNamespace { get; set; }
public bool IsFormAction =>
CommandType == QuickCommandType.OpenForm.ToStringEx()
|| CommandType == QuickCommandType.Search.ToStringEx()
|| CommandType == QuickCommandType.Add.ToStringEx();
public string MenuName { get; set; }
public virtual object Execute(object sender)
{
return null;
}
public bool Match(string cmdText)
{
return CommandCode.ToLower() == cmdText.ToLower();
}
}
}