using Lskj.Data.Api.BLL; using Lskj.Model; using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.Core { /// /// cs调用bs接口推送辅助类 /// public class ApiHelper { /// /// 接口传入参数模型 /// public Interface.Api.ModuleEventPms mep { get; private set; } /// /// bs接口调用类 /// public ApiHandler apiHandler { get; private set; } /// /// bs接口集合 /// public List apiHandlerModels { get; private set; } /// /// 接口执行返回值 /// public string apiResutMsg { get; private set; } /// /// 模块编号 /// public string moduleId; /// /// 主键名 /// public string idValue; /// /// 主键值 /// public string primaryKey; /// /// 操作表名 /// public string tabName; /// /// 右键id /// public int contextMenuId; /// /// 选中行数据 /// public Hashtable masterData; /// /// 执行方法名 /// private string execStr; /// /// 其他调用 /// /// /// /// /// /// /// public ApiHelper(string moduleId, string idValue, string primaryKey, string tabName, int contextMenuId, DataRow foucusRow = null) { this.idValue = idValue; this.moduleId = moduleId; this.tabName = tabName; this.primaryKey = primaryKey; this.contextMenuId = contextMenuId; this.masterData = GetHashTab(foucusRow); } /// /// 右键调用 /// /// /// /// public ApiHelper(string moduleId, int contextMenuId, DataRow foucusRow) { this.moduleId = moduleId; this.contextMenuId = contextMenuId; this.masterData = GetHashTab(foucusRow); } /// /// 接口推送 /// /// /// /// public void OnEvent(Interface.Api.OperateEvent operateEvent, Interface.Api.ActionType actionType, bool isSend = true) { execStr = GetExecStr(actionType); string apiMsg = ""; mep = new Interface.Api.ModuleEventPms(); bool isShowApiRtnMessage = false; try { #region Api属性值设置 mep.ModuleId = moduleId; mep.OperEvent = operateEvent; mep.IdValue = idValue; mep.ContextMenuId = contextMenuId; mep.MasterData = masterData; mep.AcType = actionType; mep.LoginId = ERPInfo.Instance.UserId; mep.LoginName = ERPInfo.Instance.UserName; mep.Password = ERPInfo.Instance.InPassWord; string Autogrowcolumn = SqlHelper.ExecuteScalar(string.Format("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='{0}' AND COLUMNPROPERTY(OBJECT_ID('{0}'),COLUMN_NAME,'IsIdentity')=1", this.tabName)) + ""; ;//自增长列 if (Autogrowcolumn.Equals(this.primaryKey, StringComparison.CurrentCultureIgnoreCase)) { if (!string.IsNullOrEmpty(Autogrowcolumn) && actionType == Interface.Api.ActionType.Add) mep.IdValue = GetAutoIdValue(tabName, Autogrowcolumn); } if (actionType == Interface.Api.ActionType.Add) mep.IdentityId = GetAutoIdValue(tabName, Autogrowcolumn);//新增获取自增字段 else { if (!string.IsNullOrEmpty(this.tabName) && !string.IsNullOrEmpty(this.primaryKey) && !string.IsNullOrEmpty(mep.IdValue)) { mep.IdentityId = GetPrimaryAutoId(Autogrowcolumn, this.primaryKey, this.idValue, this.tabName);//否则获取主键字段 } } try { string showMessageTagSql = "select top 1 * from p_systemtab"; DataTable dataTable = SqlHelper.ExecuteDataTable(showMessageTagSql); if (dataTable != null && dataTable.Rows.Count > 0 && dataTable.Columns.Contains("IsApiShowRtnMessage")) { isShowApiRtnMessage = (dataTable.Rows[0]["IsApiShowRtnMessage"] + "").Equals("1") ? true : false; } } catch (Exception) { isShowApiRtnMessage = false; } #endregion apiHandler = new Lskj.Data.Api.BLL.ApiHandler(); apiHandlerModels = new List(); apiHandler._dataImpl = new Data.Api.DataImpl(); apiHandler._dataImpl = new Lskj.Data.Api.DataImpl()//新建数据库连接 { dbOperator = new Lskj.Data.ADO.Template.DbTemplate(SqlHelper._connection.ConnectionString, "system.data.sqlclient") }; if (isSend)//是否发送数据 { apiHandler.GetEventApiSendData(mep, apiHandlerModels, apiHandler._dataImpl); apiHandler.SendEventApiData(apiHandlerModels, apiHandler._dataImpl); } else { apiHandler.GetEventApiSendData(mep, apiHandlerModels, apiHandler._dataImpl); } this.apiResutMsg = apiHandler.GetApiReturnMsg(apiHandlerModels); } catch (Exception ex) { //MessageBox.Show(ex.Message); //if (!isSend) // throw;//非执行状态抛出异常外部弹框 apiMsg += string.Format("{0}推送失败,原因:{1}\r\n", execStr, ex.Message); this.apiResutMsg = apiMsg; } finally { //bool allowShowMsg = false; //try //{ // allowShowMsg = apiHandlerModels[0].sendApis[0].AllowShowMsg; //} //catch (Exception) //{ //} //if (!allowShowMsg) //{ // this.apiResutMsg = "";//屏蔽弹出提示框 //} } } /// /// 未发送时执行调用 /// public void OnEvent() { string apiMsg = ""; try { if (apiHandler != null) { apiHandler.SendEventApiData(apiHandlerModels); this.apiResutMsg = apiHandler.GetApiReturnMsg(apiHandlerModels); } } catch (Exception ex) { apiMsg += string.Format("{0}推送失败,原因:{1}\r\n", execStr, ex.Message); this.apiResutMsg = apiMsg; } finally { this.apiResutMsg = "";//屏蔽弹出提示框 } } /// /// 判断是否配置接口数据 /// /// /// /// /// public static bool IsExecEventApi(Interface.Api.OperateEvent operateEvent, string moduleId, int contextMenuId) { try { string isExitApiTab = "select top 1 * from sysObjects where Id=OBJECT_ID(N'p_systemEventApiTab') and xtype='U'"; DataTable apiTab = SqlHelper.ExecuteDataTable(isExitApiTab); if (apiTab.Rows.Count > 0) { string sqlStr = ""; if (!string.IsNullOrEmpty(contextMenuId + "") && !(contextMenuId + "").Equals("0")) { sqlStr = string.Format("select * from p_systemEventApiTab where actionType=0 and charIndex(','+eType+',',',{0},')>0 and charIndex(',{1},',','+convert(varchar(8000),contextMenuId)+',')>0 order by orderValue", operateEvent, contextMenuId); } else { sqlStr = string.Format("select * from p_systemEventApiTab where actionType=0 and charIndex(','+eType+',',',{0},')>0 and charIndex(',{1},',','+convert(varchar(8000),dllcoid)+',')>0 order by orderValue", operateEvent, moduleId); } DataTable tbVal = SqlHelper.ExecuteDataTable(sqlStr); if (tbVal.Rows.Count > 0) return true; else return false; } else return false; } catch (Exception) { return false; } } /// /// 判断是否配置接口日志数据 /// /// /// /// /// public static bool IsExecEventApiLog() { try { string isExitApiTab = "select top 1 * from sysObjects where Id=OBJECT_ID(N'p_systemEventApiLogTab') and xtype='U'"; DataTable apiTab = SqlHelper.ExecuteDataTable(isExitApiTab); if (apiTab.Rows.Count > 0) return true; else return false; } catch (Exception) { return false; } } /// /// 获取对应主键值的自增长值 /// /// /// /// /// /// private string GetPrimaryAutoId(string autogrowcolumn, string keyField, string autogrowvalue, string tabName) { string value = ""; if (!string.IsNullOrEmpty(autogrowcolumn)) { string sql = "select {0} from {1} where {2}='{3}'"; sql = string.Format(sql, autogrowcolumn, tabName, keyField, autogrowvalue); value = SqlHelper.ExecuteScalar(sql) + ""; } return value; } #region 获取自增长主键值 /// /// 获取自增长主键值 /// /// /// /// private string GetAutoIdValue(string tabName, string autogrowcolumn) { string autogrowvalue = ""; if (!string.IsNullOrEmpty(autogrowcolumn)) autogrowvalue = SqlHelper.ExecuteScalar(string.Format("select ident_current('{0}')", tabName)) + ""; return autogrowvalue; } #endregion #region dataRow转hashTable /// /// dataRow转hashTable /// /// /// private Hashtable GetHashTab(DataRow row) { Hashtable hashtable = new Hashtable(); if (row != null) { foreach (DataColumn col in row.Table.Columns) { hashtable.Add(col.ColumnName.ToLower(), row[col] + ""); } } return hashtable; } #endregion /// /// 获取执行操作的中文意思 /// /// /// private string GetExecStr(Interface.Api.ActionType actionType) { string execStr = ""; switch (actionType) { case Interface.Api.ActionType.None: break; case Interface.Api.ActionType.Load: break; case Interface.Api.ActionType.Add: execStr = "新增"; break; case Interface.Api.ActionType.Update: execStr = "更新"; break; case Interface.Api.ActionType.Delete: execStr = "删除"; break; case Interface.Api.ActionType.Obsolete: break; case Interface.Api.ActionType.EscObsolete: break; case Interface.Api.ActionType.Submit: execStr = "提交审核"; break; case Interface.Api.ActionType.EscSubmit: break; case Interface.Api.ActionType.Audit: break; case Interface.Api.ActionType.AuditBack: break; case Interface.Api.ActionType.AuditClose: break; case Interface.Api.ActionType.AuditPress: break; case Interface.Api.ActionType.AuditPause: break; case Interface.Api.ActionType.AuditForward: break; case Interface.Api.ActionType.AuditHand: break; default: break; } return execStr; } } }