2352b79793
SVN-Revision: r1137
521 lines
20 KiB
C#
521 lines
20 KiB
C#
/*
|
||
* 存储过程提示类
|
||
* 执行存储过程报错(return -1 时)
|
||
* 把对应执行的存储过程(exec xxx )记录到剪切板中
|
||
*/
|
||
using Lskj.Core;
|
||
using Lskj.Model;
|
||
using Lskj.Util;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.Common;
|
||
using System.Data.SqlClient;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Windows.Forms;
|
||
|
||
namespace Lskj.Business.Impl
|
||
{
|
||
public sealed class SqlStoredProcedurepPrompt
|
||
{
|
||
|
||
|
||
//基础模块(ModuleGridEx)保存
|
||
public static void GenerateSaveBasePanelScript(BaseSaveModel model)
|
||
{
|
||
try
|
||
{
|
||
// 根据版本选择存储过程名称
|
||
string procedureName = model.NewVer == 0 ? "P_BaseSave" : "p_BaseSave70";
|
||
|
||
// 构建参数列表(根据ComfirmFlag选择对应参数集)
|
||
List<string> parameters = new List<string>
|
||
{
|
||
// 基础参数(两个参数集共有的部分)
|
||
$"@baseSql = N'{EscapeSqlString(model.BaseSql)}'",
|
||
$"@saveType = {Convert.ToInt32(model.BaseSaveType)}",
|
||
$"@modid = N'{EscapeSqlString(model.MenuCode)}'",
|
||
$"@tablename = N'{EscapeSqlString(model.TableName)}'",
|
||
$"@keyfield = N'{EscapeSqlString(model.KeyField)}'",
|
||
$"@keyvalue = N'{EscapeSqlString(model.FieldValue)}'",
|
||
$"@Operatorid = {ERPInfo.Instance.UserId}",
|
||
$"@operatorName = N'{EscapeSqlString(ERPInfo.Instance.UserName)}'",
|
||
"@msg = @msg OUTPUT"
|
||
};
|
||
|
||
// 如果确认标志不为0,添加@comfirmFlag参数(对应param2)
|
||
if (model.ComfirmFlag != 0)
|
||
{
|
||
parameters.Add($"@comfirmFlag = {model.ComfirmFlag}");
|
||
}
|
||
|
||
// 构建完整的执行语句
|
||
string script = $@"DECLARE @msg NVARCHAR(4000), @return INT;
|
||
EXEC @return = {procedureName} {string.Join(", ", parameters)};
|
||
SELECT @return AS 'return', @msg AS msg;";
|
||
|
||
if(ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// pubadd(ModulePanelEx)或审核界面中点击 保存
|
||
/// </summary>
|
||
/// <param name="model"></param>
|
||
/// <param name="storedProcedureValue"></param>
|
||
public static void GenerateBaseSaveProcedureScript(BaseSaveModel model, string storedProcedureValue = "0")
|
||
{
|
||
try
|
||
{
|
||
// 根据版本选择存储过程名称
|
||
string procedureName = model.NewVer == 0 ? "P_BaseSave" : "p_BaseSave70";
|
||
|
||
// 处理确认标志
|
||
if (storedProcedureValue.Equals("1"))
|
||
model.ComfirmFlag = 1;
|
||
|
||
// 构建参数列表(保持顺序并统一使用@name=value格式)
|
||
List<string> parameters = new List<string>
|
||
{
|
||
// 处理baseSql中的单引号转义
|
||
$"@baseSql = N'{EscapeSqlString(model.BaseSql)}'",
|
||
$"@saveType = {Convert.ToInt32(model.BaseSaveType)}",
|
||
$"@modid = N'{EscapeSqlString(model.MenuCode)}'",
|
||
$"@tablename = N'{EscapeSqlString(model.TableName)}'",
|
||
$"@keyfield = N'{EscapeSqlString(model.KeyField)}'",
|
||
$"@keyvalue = N'{EscapeSqlString(model.FieldValue)}'",
|
||
$"@Operatorid = {ERPInfo.Instance.UserId}",
|
||
$"@operatorName = N'{EscapeSqlString(ERPInfo.Instance.UserName)}'"
|
||
};
|
||
|
||
// 添加确认标志参数(如果需要)
|
||
if (model.ComfirmFlag != 0)
|
||
{
|
||
parameters.Add($"@comfirmFlag = {model.ComfirmFlag}");
|
||
}
|
||
|
||
// 添加输出参数
|
||
parameters.Add("@msg = @msg OUTPUT");
|
||
|
||
// 构建完整的执行语句
|
||
string script = $@"DECLARE @msg NVARCHAR(4000), @return INT;
|
||
EXEC @return = {procedureName} {string.Join(", ", parameters)}
|
||
SELECT @return AS 'return', @msg AS msg;";
|
||
|
||
if (ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// pubadd(ModulePanelEx)点击 提交
|
||
/// </summary>
|
||
/// <param name="menuCode"></param>
|
||
/// <param name="parmaryKey"></param>
|
||
/// <param name="comfirmType"></param>
|
||
public static void GenerateBaseApplyScript(string menuCode, string parmaryKey, int comfirmType)
|
||
{
|
||
try
|
||
{
|
||
// 构建参数列表,统一使用@name=value格式
|
||
List<string> parameters = new List<string>
|
||
{
|
||
$"@typeCode = N'{EscapeSqlString(menuCode)}'",
|
||
$"@billDocumentId = N'{EscapeSqlString(parmaryKey)}'",
|
||
$"@Operatorid = {ERPInfo.Instance.UserId}",
|
||
$"@operatorName = N'{EscapeSqlString(ERPInfo.Instance.UserName)}'",
|
||
$"@comfirmType = {comfirmType}",
|
||
"@msg = @msg OUTPUT"
|
||
};
|
||
|
||
// 构建完整的执行语句
|
||
string script = $@"DECLARE @msg NVARCHAR(4000), @return INT;
|
||
EXEC @return = p_baseApply {string.Join(", ", parameters)}
|
||
SELECT @return AS 'return', @msg AS msg;";
|
||
|
||
if (ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
//单据保存
|
||
public static void GenerateBillSaveScript(string masterSql, string detailSql, string detailTable, string billNo,
|
||
string detailGuid, string billSeq, int billWay, int comfirm,
|
||
int auditFlag = 0, int newVer = 0)
|
||
{
|
||
try
|
||
{
|
||
// 根据版本选择存储过程名称
|
||
string procedureName = newVer == 0 ? "P_BillSavePr_3" : "P_BillSavePr70";
|
||
|
||
// 构建参数列表(保持与原方法一致的顺序和格式)
|
||
List<string> parameters = new List<string>
|
||
{
|
||
// 处理主SQL语句中的单引号转义
|
||
$"@Sql = N'{EscapeSqlString(masterSql)}'",
|
||
$"@billdocument_id = N'{EscapeSqlString(billNo)}'",
|
||
$"@tmpstr = N'{EscapeSqlString(detailGuid)}'",
|
||
$"@Operatorid = {ERPInfo.Instance.UserId}",
|
||
$"@Fbilltagid = {billWay}",
|
||
$"@operateway = N'{EscapeSqlString(billSeq)}'",
|
||
"@msg = @msg OUTPUT",
|
||
$"@auditFlag = {auditFlag}",
|
||
$"@comfirmFlag = {comfirm}"
|
||
};
|
||
|
||
// 构建完整的执行语句(包含临时表清理语句)
|
||
string script = "--保存明细到临时表\r\n" +detailSql+ "\r\n" + $@"DECLARE @msg NVARCHAR(4000), @return INT;
|
||
-- 执行单据保存存储过程
|
||
EXEC @return = {procedureName} {string.Join(", ", parameters)};
|
||
SELECT @return AS 'return', @msg AS msg;
|
||
-- 清理临时表
|
||
DELETE {EscapeSqlString(detailTable)}_temp WHERE sysstr = N'{EscapeSqlString(detailGuid)}';";
|
||
|
||
if (ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
//提交单据
|
||
public static void GenerateBillApplyExecutionScript(string typeCode, string billNo, string hintMsg, int comfirm, int comfirmFlag = 0)
|
||
{
|
||
try
|
||
{
|
||
// 模拟原方法中检查存储过程是否包含@confirmFlag参数的逻辑
|
||
bool hasConfirmFlag = CheckIfProcedureHasParameter("p_billApply", "@confirmFlag");
|
||
|
||
// 构建参数列表,保持与原方法一致的顺序
|
||
List<string> parameters = new List<string>
|
||
{
|
||
$"@typeCode = N'{EscapeSqlString(typeCode)}'",
|
||
$"@billDocumentId = N'{EscapeSqlString(billNo)}'",
|
||
$"@Operatorid = {ERPInfo.Instance.UserId}",
|
||
$"@operatorName = N'{EscapeSqlString(ERPInfo.Instance.UserName)}'",
|
||
$"@comfirmType = {comfirm}",
|
||
$"@msg = @msg OUTPUT"
|
||
};
|
||
|
||
// 如果存储过程包含@confirmFlag参数,则添加该参数
|
||
if (hasConfirmFlag)
|
||
{
|
||
parameters.Add($"@confirmFlag = {comfirmFlag}");
|
||
}
|
||
|
||
// 构建完整的存储过程执行语句
|
||
string executionScript = $@"DECLARE @msg NVARCHAR(4000), @return INT;
|
||
EXEC @return = p_billApply {string.Join(", ", parameters)};
|
||
SELECT @return AS 'return', @msg AS msg;";
|
||
|
||
if (ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(executionScript);
|
||
LogHelper.Instance.WriteLog(executionScript);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
//审核(单表审核和单据审核)
|
||
public static void GenerateAuditExecutionScript(
|
||
DynamicModel sysModel,
|
||
string stepCode,
|
||
string billDocumentId,
|
||
string storedName,
|
||
string accractFlag,
|
||
string remindSelect,
|
||
string remarkText = "",
|
||
string remindOpers = "",
|
||
string confirmOpers = "",
|
||
bool ret = false,
|
||
int returnd = 0,
|
||
int selectConfirmFlag = 0,
|
||
bool isStepOver = false)
|
||
{
|
||
try
|
||
{
|
||
bool newAudit = true;
|
||
string[] newParameter = new string[] { "@nextSelectStepCode", "@nextSelectStepOper", "@selectConfirmFlag" };
|
||
if (!BaseAuditImpl.isParamComplete(storedName, newParameter)) newAudit = false;
|
||
|
||
// 构建参数列表
|
||
List<string> parameters = new List<string>
|
||
{
|
||
// 基础参数
|
||
$"@typeCode = N'{EscapeSqlString(sysModel.ModuleCode)}'",
|
||
$"@stepCode = {(string.IsNullOrEmpty(stepCode) ? 0 : Convert.ToInt32(stepCode))}",
|
||
$"@billDocumentId = N'{EscapeSqlString(billDocumentId)}'",
|
||
$"@operatorId = {sysModel.UserId}",
|
||
$"@operatorName = N'{EscapeSqlString(sysModel.UserName)}'",
|
||
$@"@auditAdvice = N'{EscapeSqlString(accractFlag == "F" ?
|
||
remarkText : (isStepOver ? $"由[{sysModel.UserName}]强制退回,意见:{remarkText}" : remarkText))}'",
|
||
$"@Direction = N'{EscapeSqlString(accractFlag)}'",
|
||
"@msg = @msg OUTPUT"
|
||
};
|
||
// 添加新审核相关参数
|
||
if (newAudit)
|
||
{
|
||
parameters.Add($"@selectConfirmFlag = {selectConfirmFlag}");
|
||
parameters.Add("@nextSelectStepCode = @nextSelectStepCode OUTPUT");
|
||
parameters.Add("@nextSelectStepOper = @nextSelectStepOper OUTPUT");
|
||
}
|
||
|
||
// 添加提醒相关参数
|
||
if (remindSelect == "1")
|
||
{
|
||
parameters.Add($"@hint_opers = N'{EscapeSqlString(remindOpers)}'");
|
||
parameters.Add($"@comfirm_opers = N'{EscapeSqlString(confirmOpers)}'");
|
||
}
|
||
|
||
// 添加返回步骤参数
|
||
if (ret)
|
||
{
|
||
parameters.Add($"@backStepCode = {returnd}");
|
||
}
|
||
|
||
|
||
// 构建完整执行语句(包含参数声明和结果查询)
|
||
string script = $@"DECLARE
|
||
@msg {(ERPInfo.Instance.ChangeParameterType ? "NVARCHAR(6000)" : "VARCHAR(3000)")},
|
||
@nextSelectStepCode VARCHAR(3000),
|
||
@nextSelectStepOper VARCHAR(3000),
|
||
@return INT;
|
||
EXEC @return = {storedName}
|
||
{string.Join(",\n ", parameters)};
|
||
SELECT
|
||
'return' = @return,
|
||
'msg' = @msg;";
|
||
|
||
// 复制到剪贴板
|
||
if (ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
public static void GenerateAuditExecutionScript(
|
||
DynamicModel sysModel,
|
||
string stepCode,
|
||
string billDocumentId,
|
||
string storedName,
|
||
string accractFlag,
|
||
string remindSelect,
|
||
string remarkText = "",
|
||
string remindOpers = "",
|
||
string confirmOpers = "",
|
||
bool ret = false,
|
||
int returnd = 0,
|
||
int selectConfirmFlag = 0,
|
||
bool isStepOver = false,
|
||
int comfirm = 0)
|
||
{
|
||
try
|
||
{
|
||
// 检查新审核参数是否存在
|
||
bool newAudit = true;
|
||
string[] newParameter = new string[] { "@nextSelectStepCode", "@nextSelectStepOper", "@selectConfirmFlag" };
|
||
if (!BaseAuditImpl.isParamComplete(storedName, newParameter))
|
||
newAudit = false;
|
||
|
||
// 检查存储过程是否包含@confirmFlag参数(模拟原方法syscolumns查询逻辑)
|
||
bool hasConfirmFlag = CheckIfProcedureHasParameter(storedName, "@confirmFlag");
|
||
|
||
// 构建参数列表
|
||
List<string> parameters = new List<string>
|
||
{
|
||
// 基础参数
|
||
$"@typeCode = N'{EscapeSqlString(sysModel.ModuleCode)}'",
|
||
$"@stepCode = {(string.IsNullOrEmpty(stepCode) ? 0 : Convert.ToInt32(stepCode))}",
|
||
$"@billDocumentId = N'{EscapeSqlString(billDocumentId)}'",
|
||
$"@operatorId = {sysModel.UserId}",
|
||
$"@operatorName = N'{EscapeSqlString(sysModel.UserName)}'",
|
||
$@"@auditAdvice = N'{EscapeSqlString(accractFlag == "F" ?
|
||
remarkText : (isStepOver ? $"由[{sysModel.UserName}]强制退回,意见:{remarkText}" : remarkText))}'",
|
||
$"@Direction = N'{EscapeSqlString(accractFlag)}'",
|
||
"@msg = @msg OUTPUT"
|
||
};
|
||
|
||
// 添加新审核相关参数
|
||
if (newAudit)
|
||
{
|
||
parameters.Add($"@selectConfirmFlag = {selectConfirmFlag}");
|
||
parameters.Add("@nextSelectStepCode = @nextSelectStepCode OUTPUT");
|
||
parameters.Add("@nextSelectStepOper = @nextSelectStepOper OUTPUT");
|
||
}
|
||
|
||
// 添加提醒相关参数
|
||
if (remindSelect == "1")
|
||
{
|
||
parameters.Add($"@hint_opers = N'{EscapeSqlString(remindOpers)}'");
|
||
parameters.Add($"@comfirm_opers = N'{EscapeSqlString(confirmOpers)}'");
|
||
}
|
||
|
||
// 添加返回步骤参数
|
||
if (ret)
|
||
{
|
||
parameters.Add($"@backStepCode = {returnd}");
|
||
}
|
||
|
||
// 新增:如果存储过程包含@confirmFlag参数,则添加该参数
|
||
if (hasConfirmFlag)
|
||
{
|
||
parameters.Add($"@confirmFlag = {comfirm}");
|
||
}
|
||
|
||
// 构建完整执行语句
|
||
string script = $@"DECLARE
|
||
@msg {(ERPInfo.Instance.ChangeParameterType ? "NVARCHAR(6000)" : "VARCHAR(3000)")},
|
||
@nextSelectStepCode VARCHAR(3000),
|
||
@nextSelectStepOper VARCHAR(3000),
|
||
@return INT;
|
||
|
||
EXEC @return = {storedName}
|
||
{string.Join(",\n ", parameters)};
|
||
|
||
SELECT
|
||
'return' = @return,
|
||
'nextSelectStepCode' = @nextSelectStepCode,
|
||
'nextSelectStepOper' = @nextSelectStepOper,
|
||
'msg' = @msg;";
|
||
|
||
// 复制到剪贴板并日志记录
|
||
if (ERPInfo.Instance.UserName.Equals("管理员"))
|
||
Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
//右键执行存储过程
|
||
public static void GenerateProcedureExecutionScript(string Action, List<string> paramList,bool isFirstFlag = false,string comfirmFlag = "0")
|
||
{
|
||
try
|
||
{
|
||
if (string.IsNullOrWhiteSpace(Action)) return;
|
||
// 解析存储过程名称和参数
|
||
List<string> parameters = ReplaceHelper.GetParamFields(Action);
|
||
string[] paramStr = parameters[0].Replace("{", "").Replace("}", "").Split(',');
|
||
string procedureName = Action.Replace(parameters[0], "").Trim();
|
||
|
||
// 构建参数列表
|
||
List<string> sqlParameters = new List<string>();
|
||
SqlParameter pMsg = new SqlParameter("@msg", SqlDbType.VarChar, 2000);
|
||
SqlParameter pComfirmFlag = new SqlParameter("@comfirmFlag", SqlDbType.Int);
|
||
|
||
for (int i = 0; i < paramStr.Length; i++)
|
||
{
|
||
string paramName = paramStr[i].Trim();
|
||
if (paramName.Equals("@msg", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
// 处理@msg参数
|
||
pMsg.Direction = ParameterDirection.InputOutput;
|
||
pMsg.Value = string.IsNullOrEmpty(paramList[i]) ? "" : paramList[i];
|
||
sqlParameters.Add("@msg = @msg OUTPUT");
|
||
}
|
||
else if (paramName.Equals("@comfirmFlag", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
// 处理@comfirmFlag参数
|
||
pComfirmFlag.Value = comfirmFlag;
|
||
sqlParameters.Add($"@{paramName} = {comfirmFlag}");
|
||
}
|
||
else
|
||
{
|
||
// 处理普通参数
|
||
sqlParameters.Add($"{paramName} = N'{EscapeSqlString(paramList[i])}'");
|
||
}
|
||
}
|
||
// 构建完整执行语句
|
||
string script = $@"DECLARE @returnValue INT, @msg VARCHAR(2000);
|
||
EXEC @returnValue = {procedureName}
|
||
{string.Join(", ", sqlParameters)};
|
||
SELECT 'return' = @returnValue, 'msg' = @msg;";
|
||
|
||
// 复制到剪贴板
|
||
if (ERPInfo.Instance.UserName.Equals("管理员")) Clipboard.SetText(script);
|
||
LogHelper.Instance.WriteLog(script);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 转义SQL字符串中的单引号,确保生成的语句语法正确
|
||
/// </summary>
|
||
private static string EscapeSqlString(string input)
|
||
{
|
||
if (string.IsNullOrEmpty(input))
|
||
return string.Empty;
|
||
|
||
// 将单引号替换为两个单引号,适应SQL Server的字符串格式
|
||
return input.Replace("'", "''");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查存储过程是否包含指定参数(模拟原方法中的syscolumns查询)
|
||
/// </summary>
|
||
/// <param name="procedureName">存储过程名称</param>
|
||
/// <param name="parameterName">参数名称</param>
|
||
/// <returns>如果存在返回true,否则返回false</returns>
|
||
private static bool CheckIfProcedureHasParameter(string procedureName, string parameterName)
|
||
{
|
||
DataTable dt = BaseImpl.GetDataTableResult(string.Format("select * from syscolumns where id =object_id('{0}') and name='{1}'", procedureName, parameterName));
|
||
if (dt != null && dt.Rows.Count > 0)
|
||
{
|
||
return true;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
//单据保存(达梦)
|
||
public static void GenerateBillSaveScript_DM(string detailSql, string Dmsql, string detailTable, string detailGuid)
|
||
{
|
||
try
|
||
{
|
||
string script |