382 lines
14 KiB
C#
382 lines
14 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// cs调用bs接口推送辅助类
|
|
/// </summary>
|
|
public class ApiHelper
|
|
{
|
|
/// <summary>
|
|
/// 接口传入参数模型
|
|
/// </summary>
|
|
public Interface.Api.ModuleEventPms mep { get; private set; }
|
|
/// <summary>
|
|
/// bs接口调用类
|
|
/// </summary>
|
|
public ApiHandler apiHandler { get; private set; }
|
|
/// <summary>
|
|
/// bs接口集合
|
|
/// </summary>
|
|
public List<ApiHandlerModel> apiHandlerModels { get; private set; }
|
|
/// <summary>
|
|
/// 接口执行返回值
|
|
/// </summary>
|
|
public string apiResutMsg { get; private set; }
|
|
/// <summary>
|
|
/// 模块编号
|
|
/// </summary>
|
|
public string moduleId;
|
|
/// <summary>
|
|
/// 主键名
|
|
/// </summary>
|
|
public string idValue;
|
|
/// <summary>
|
|
/// 主键值
|
|
/// </summary>
|
|
public string primaryKey;
|
|
/// <summary>
|
|
/// 操作表名
|
|
/// </summary>
|
|
public string tabName;
|
|
/// <summary>
|
|
/// 右键id
|
|
/// </summary>
|
|
public int contextMenuId;
|
|
/// <summary>
|
|
/// 选中行数据
|
|
/// </summary>
|
|
public Hashtable masterData;
|
|
/// <summary>
|
|
/// 执行方法名
|
|
/// </summary>
|
|
private string execStr;
|
|
/// <summary>
|
|
/// 其他调用
|
|
/// </summary>
|
|
/// <param name="moduleId"></param>
|
|
/// <param name="idValue"></param>
|
|
/// <param name="primaryKey"></param>
|
|
/// <param name="tabName"></param>
|
|
/// <param name="contextMenuId"></param>
|
|
/// <param name="foucusRow"></param>
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// 右键调用
|
|
/// </summary>
|
|
/// <param name="moduleId"></param>
|
|
/// <param name="contextMenuId"></param>
|
|
/// <param name="foucusRow"></param>
|
|
public ApiHelper(string moduleId, int contextMenuId, DataRow foucusRow)
|
|
{
|
|
this.moduleId = moduleId;
|
|
this.contextMenuId = contextMenuId;
|
|
this.masterData = GetHashTab(foucusRow);
|
|
}
|
|
/// <summary>
|
|
/// 接口推送
|
|
/// </summary>
|
|
/// <param name="operateEvent"></param>
|
|
/// <param name="actionType"></param>
|
|
/// <param name="isSend"></param>
|
|
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<ApiHandlerModel>();
|
|
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 = "";//屏蔽弹出提示框
|
|
//}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 未发送时执行调用
|
|
/// </summary>
|
|
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 = "";//屏蔽弹出提示框
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 判断是否配置接口数据
|
|
/// </summary>
|
|
/// <param name="operateEvent"></param>
|
|
/// <param name="moduleId"></param>
|
|
/// <param name="contextMenuId"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 判断是否配置接口日志数据
|
|
/// </summary>
|
|
/// <param name="operateEvent"></param>
|
|
/// <param name="moduleId"></param>
|
|
/// <param name="contextMenuId"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取对应主键值的自增长值
|
|
/// </summary>
|
|
/// <param name="autogrowcolumn"></param>
|
|
/// <param name="keyField"></param>
|
|
/// <param name="autogrowvalue"></param>
|
|
/// <param name="tabName"></param>
|
|
/// <returns></returns>
|
|
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 获取自增长主键值
|
|
/// <summary>
|
|
/// 获取自增长主键值
|
|
/// </summary>
|
|
/// <param name="tabName"></param>
|
|
/// <param name="autogrowcolumn"></param>
|
|
/// <returns></returns>
|
|
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
|
|
/// <summary>
|
|
/// dataRow转hashTable
|
|
/// </summary>
|
|
/// <param name="row"></param>
|
|
/// <returns></returns>
|
|
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
|
|
/// <summary>
|
|
/// 获取执行操作的中文意思
|
|
/// </summary>
|
|
/// <param name="actionType"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|