/****************************** * 说明:辅助工具操作类 * 创建人:龚宇超 * 创建日期:2017-12-20 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Windows.Forms; using Lskj.Util; using Lskj.Business.Impl; using System.Diagnostics; using Lskj.Model; using Lskj.Data; using System.IO; using System.Data.SqlClient; using Lskj.Business; using DevExpress.XtraGrid.Views.Grid; using Lskj.Core; namespace Lskj.Control.Model { /// /// 辅助工具操作类 /// public class CommonMenu { protected GridControlEx GridControlObj; protected GridView BaseGridView; private DynamicModel _model; /// /// 控件面板 /// private MyControl _controlObj; public bool issuccess = true; /// /// 最初账套版本 /// public DataRow AccountItem; /// /// Occurs when [apply before]. /// public event CommonToolApplyEventHandler ApplyBefore; public event CommonToolAfterDynamicLinkEventHandler DynamicLinkAfter; public event StoredProcedureAfterExecute ReturnStoredProcedureValue; /// /// Initializes a new instance of the class. /// /// 常用工具数据行. public CommonMenu(DynamicModel model, MyControl control, System.Windows.Forms.Control view = null) { this._model = model; this._controlObj = control; if (view != null) this.GridControlObj = view as GridControlEx; if (this.GridControlObj != null) { this.BaseGridView = this.GridControlObj.GridView; } } /// /// 说明:执行常用功能 /// 创建人:龚宇超 /// 创建日期:2017-12-20 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// 当前选中按钮绑定的数据源行 /// true if XXXX, false otherwise. public bool Apply(DataRow rowItem) { bool resultValue = false; if (rowItem == null || this._controlObj == null) return resultValue; GridRightMenuModel model = new GridRightMenuModel(rowItem); try { //if (!string.IsNullOrWhiteSpace(model.MenuCond)) //{ // try // { // string cond = ReplaceHelper.ReplaceUserInfo(model.MenuCond); // if (this._controlObj != null) // { // cond = _controlObj.ReplaceControlValue(model.MenuCond); // } // if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) // { // DataRow parentitem = BaseGridView.GetFocusedDataRow(); // cond = ReplaceHelper.ReplaceRowParam(parentitem, cond) + ""; // } // bool result = ReplaceHelper.EvalCond(cond); // if (!result) return false; // } // catch (Exception) // { // MessageUtil.Show(ResourceKeys.SetRightMenuCondFault); // } //} if (!string.IsNullOrEmpty(model.UnionZTid)) { string UnionZTid = ReplaceHelper.ReplaceRowParam(BaseGridView.GetFocusedDataRow(), model.UnionZTid); string condition = string.Format("where ShowName ='{0}'", ERPInfo.Instance.AccountBook); DataTable dt = MainImpl.GetLedgerList(condition); DataRow RT = BaseImpl.GetDataRowResult($"select * from p_sydbGroupTab where ID={UnionZTid}"); AccountItem = dt.Rows[0]; AccountItem["IP"] = DBConfig.Instance.ServerName; ToDBatching(RT, AccountItem); } if (!string.IsNullOrWhiteSpace(model.BeforeMsg)) { string prompt = this._controlObj.ReplaceControlValue(model.BeforeMsg); if (prompt.StartsWith("@") || prompt.StartsWith("!")) { prompt = BaseImpl.GetDefaultValue(prompt); } if (!string.IsNullOrWhiteSpace(prompt)) { DialogResult result = MessageUtil.Show(prompt, MessageBoxButtons.YesNo); // DialogResult result = MessageUtil.Show(this._controlObj.ReplaceControlValue(model.BeforeMsg), MessageBoxButtons.YesNo); if (result != DialogResult.Yes) { return resultValue; } } } ApplyArgs applyArgs = new ApplyArgs(); applyArgs.MemuModel = model; if (ApplyBefore != null) { ApplyBefore(applyArgs); } if (applyArgs.CancelFlag) return false; List paramList = HandleRightMenuParams(model.ParamList); string maintabFocusedRowJson = ""; if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) { maintabFocusedRowJson = BaseGridView.GetFocusedDataRow().ToJsonObject(); // 假设 ToJsonObject() 是将对象转换为 JSON 字符串的方法 } paramList.Add(maintabFocusedRowJson); string dllName = this._controlObj.ReplaceControlValue(model.DllName).Trim(); if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) { DataRow parentitem = BaseGridView.GetFocusedDataRow(); dllName = ReplaceHelper.ReplaceRowParam(parentitem, dllName).Trim(); } switch (model.ActionType) { case 1: // 执行存储过程 resultValue = ExecProcedure(model.Action, paramList); break; case 2: // 调用dll程序 resultValue = ExecDynamicLinkLibary(model, paramList, rowItem, dllName); break; case 3: //调用delphi程序 resultValue = ExecDelphiLibary(model.DllName, paramList); break; case 0: // 执行sql语句 resultValue = ExecSqlValue(model.Action); break; default: // 特殊处理程序 if (dllName.EndsWith(".exe") && !model.WipeExes.Contains(dllName.ToString())) { // 执行exe程序 WinHelper.WinExec(dllName, 1); } else if (dllName.StartsWith("www.") || dllName.StartsWith("http://") || dllName.StartsWith("https://") || dllName.StartsWith("ftp://") || dllName.StartsWith("file://")) { // 打开网页程序 Process.Start(dllName); } break; } } catch (Exception ex) { LogUtil.WriteError("执行常用功能出错!", ex); //MessageUtil.Show("执行常用功能出错!" + ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } finally { if (!string.IsNullOrEmpty(model.UnionZTid)) ToDBatching(AccountItem, AccountItem); } return true; } /// /// 说明:处理常用工具菜单10个扩展参数 /// 创建人:龚宇超 /// 创建日期:2017-10-30 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The parameter list. private List HandleRightMenuParams(List paramList) { List handleParamList = new List(); foreach (string item in paramList) { if (!string.IsNullOrWhiteSpace(item)) { string fieldValue = ReplaceHelper.ReplaceUserInfo(item); string firstChar = fieldValue.Substring(0, 1); switch (firstChar) { case "@": // sql处理 { if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) { DataRow parentitem = BaseGridView.GetFocusedDataRow(); fieldValue = ReplaceHelper.ReplaceRowParam(parentitem, item) + ""; } string value = MainImpl.GetResult(this._controlObj.ReplaceControlValue(fieldValue.Replace("@", ""))) + ""; handleParamList.Add(value); } break; case "{": // 字段特殊替换 { if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) { DataRow parentitem = BaseGridView.GetFocusedDataRow(); fieldValue = ReplaceHelper.ReplaceRowParam(parentitem, item) + ""; } handleParamList.Add(this._controlObj.ReplaceControlValue(fieldValue)); } break; case "#": // 需要传入的sql语句 handleParamList.Add(this._controlObj.ReplaceControlValue(fieldValue.Replace("#", ""))); break; case "[": //[FormTitle_格式数据 if (fieldValue.StartsWith("[FormTitle_")) { handleParamList.Add(this._controlObj.ReplaceControlValue(fieldValue.Replace("[FormTitle_", ""))); } else { handleParamList.Add(this._controlObj.ReplaceControlValue(fieldValue)); } break; default: if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) { DataRow parentitem = BaseGridView.GetFocusedDataRow(); fieldValue = ReplaceHelper.ReplaceRowParam(parentitem, item) + ""; } handleParamList.Add(this._controlObj.ReplaceControlValue(fieldValue)); break; } } else { handleParamList.Add(item); } } return handleParamList; } /// /// 说明:执行存储过程 /// 创建人:龚宇超 /// 创建日期:2017-10-31 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the proc. /// The parameter list. /// true if XXXX, false otherwise. private bool ExecProcedure(string procName, List paramList) { if (string.IsNullOrWhiteSpace(procName)) { MessageUtil.Show(ResourceKeys.OperFault + "未配置执行存储过程."); return false; } else { bool reaultValue = true; List procParams = ReplaceHelper.GetParamFields(procName); if (procParams.Count == 0) { MessageUtil.Show(ResourceKeys.OperFault + "存储过程配置错误."); return false; } string[] paramStr = procParams[0].Replace("{", "").Replace("}", "").Split(','); List sqlParamList = new List(); SqlParameter pMsg = new SqlParameter("@msg", SqlDbType.VarChar, 2000); for (int i = 0; i < paramStr.Length; i++) { string item = paramStr[i]; if (paramStr[i].Equals("@msg", StringComparison.OrdinalIgnoreCase)) { pMsg.Direction = ParameterDirection.InputOutput; pMsg.Value = ""; sqlParamList.Add(pMsg); } else { string paramStr1 = paramStr[i]; SqlParameter param = new SqlParameter(string.Format("{0}", paramStr1), SqlDbType.VarChar); param.Value = paramList[i]; sqlParamList.Add(param); } } SqlParameter returnValue = new SqlParameter("@return", SqlDbType.Int, 4); returnValue.Direction = ParameterDirection.ReturnValue; sqlParamList.Add(returnValue); int result = MainImpl.ExecProcedure(procName.Replace(procParams[0], ""), sqlParamList.ToArray()); int rValue = Convert.ToInt32(returnValue.Value); switch (rValue) { case -1: MessageUtil.Show(ResourceKeys.OperFault + "\n" + pMsg.Value); reaultValue = false; break; case 99: MessageUtil.Show(pMsg.Value + ""); break; case 9999: FrmPrompt frmPrompt = new FrmPrompt(pMsg.Value + ""); DialogResult dialogResult = frmPrompt.ShowDialog(); if (dialogResult == DialogResult.OK) { SqlParameter param = sqlParamList.FirstOrDefault(x => "@msg".Equals(x.ParameterName)); param.Value = frmPrompt.PromptMsg; this.ExecProcedure(procName, paramList); } break; default: if (ReturnStoredProcedureValue != null) { ReturnStoredProcedureValue(pMsg.Value + ""); } MessageUtil.Show(ResourceKeys.OperSuccess); break; } return reaultValue; } } /// /// 说明:执行sql语句 /// 创建人:龚宇超 /// 创建日期:2017-10-31 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The SQL value. /// The row data. private bool ExecSqlValue(string sqlValue) { if (string.IsNullOrWhiteSpace(sqlValue)) { MessageUtil.Show(ResourceKeys.OperFault + "未配置执行sql语句"); return false; } else { MainImpl.GetResult(this._controlObj.ReplaceControlValue(sqlValue)); MessageUtil.Show(ResourceKeys.OperSuccess); } return true; } /// /// 说明:执行动态链接库 /// 创建人:龚宇超 /// 创建日期:2017-11-01 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// true if XXXX, false otherwise. private bool ExecDynamicLinkLibary(GridRightMenuModel model, List paramList, DataRow rowItem, string dllname) { issuccess = true; if (string.IsNullOrWhiteSpace(dllname)) { MessageUtil.Show(ResourceKeys.OperFault + "未配置动态链接库."); return false; } else { switch (dllname.ToLower()) { case "iexplore.exe": // 调用ie浏览器 string url = (paramList[0] + Uri.EscapeUriString(paramList[1])).Replace("#", "%23"); Process.Start("iexplore.exe", url); break; case "lstest.exe": // 调用附件下载 StringBuilder dfile = new StringBuilder(); StringBuilder rfile = new StringBuilder(); int hwnd = 0; dfile.Append(paramList[0] + paramList[1]); rfile = DelphiHelper.DownloadAttach(dfile, hwnd); rfile.Clear(); rfile.Append(PubUtil.FileDownLoadTempPath + Path.GetFileName(dfile.ToString())); if (File.Exists(rfile.ToString())) { Process.Start(rfile.ToString(), ""); } break; default: List str = new List { //_model.FormText, model.MenuName, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, "3", _model.ModuleCode, paramList[0], paramList[1], paramList[2], paramList[3], paramList[4], paramList[5], paramList[6], paramList[7], paramList[8], paramList[9], paramList[10], paramList[11], paramList[12]//固定添加行数据json字符串 }; //string maintabFocusedRowJson = ""; //if (BaseGridView != null && BaseGridView.GetFocusedDataRow() != null) //{ // maintabFocusedRowJson = BaseGridView.GetFocusedDataRow().ToJsonObject(); // 假设 ToJsonObject() 是将对象转换为 JSON 字符串的方法 //} //str.Add(maintabFocusedRowJson); // 现在可以添加元素了,因为 str 是 List // // 假设你需要将 List 转回字符串数组,可以这样操作 string[] strArray = str.ToArray(); IForm form = FormHelper.LoadDllForm(dllname, strArray); if (form == null) { MessageUtil.Show(ResourceKeys.DyncmicDllNotFount); return false; } if (form.SubForm == null) { MessageUtil.Show(ResourceKeys.DyncmicDllInitFault); return false; } form.SubForm.WindowState = model.MaxWindow ? FormWindowState.Maximized : FormWindowState.Normal; form.SubForm.Text = model.MenuName; if (form.SubForm is BaseForm) { (form.SubForm as BaseForm).ParentView = this.BaseGridView; } form.SubForm.ShowDialog(); if (DynamicLinkAfter != null) { ApplyArgs applyArgs = new ApplyArgs(); applyArgs.MemuModel = model; if (form.SubForm.Tag != null && form.SubForm.Tag is FormDialogModel) { applyArgs.DialogModel = form.SubForm.Tag as FormDialogModel; } DynamicLinkAfter(this, applyArgs); } break; } } return true; } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2017-11-01 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// Name of the libary. /// The parameter list. /// true if XXXX, false otherwise. private bool ExecDelphiLibary(string libaryName, List paramList) { try { DelphiHelper.LoadDelphiDll(libaryName, paramList[0], paramList[1], paramList[2], paramList[3], Convert.ToInt32(paramList[4]), paramList[5]); return true; } catch (Exception ex) { //MessageUtil.Show(ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } return false; } /// /// 启动账套数据源刷新 /// protected void ToDBatching(DataRow RT, DataRow firstrow) { string serverIP = RT["IP"] + ""; string dbName = RT["DBName"] + ""; string dataBook = RT["ShowName"] + ""; bool isInternal = RT.Table.Columns.Contains("IsInternalNetwork") && !string.IsNullOrEmpty(RT["IsInternalNetwork"] + "") ? "1".Equals(RT["IsInternalNetwork"] + "") : false; DBConfig.Instance.DataBook = dataBook; DBConfig.Instance.ServerName = serverIP; DBConfig.Instance.DataBase = dbName; DBConfig.Instance.IsInternalNetwork = isInternal; if (!DBConfig.Instance.CreateConnection()) { ToDBatching(firstrow, firstrow); } } } }