/// <summary> /// 生成解析完成的提示信息 /// </summary> /// <param name="expvarDic"></param> /// <param name="item"></param> /// <param name="expLength"></param> /// <returns></returns> private static string[] GetPromptMsgs(IDictionary <string, string> expvarDic, AddinExpressionModel item) { string text = string.Empty; if (string.IsNullOrEmpty(item.Msg)) { return(null); } //提示信息变量替换 text = item.Msg.ToLower(); foreach (var dickey in expvarDic.Keys) { text = text.Replace(dickey, expvarDic[dickey]); } UnResolvedText msgText = new UnResolvedText { RawText = text, RowsType = EnumUIDataSourceType.All }; string[] msgs = AddinParameterUtils.ReplaceWithParameterValue(msgText); if (AddinParameterUtils.HasEmptyDataUIParameter(msgs)) { //有空的UI变量 return(msgs); } var parsedMsgs = EvalMsgExpressions(msgs); //解析出提示信息中的表达式 return(parsedMsgs); }
/// <summary> /// 计算表达式变量 /// </summary> /// <returns></returns> private static IDictionary <string, string> EvalExpVars(ISession session, IList <AddinExpressionVarModel> expVars) { string sqltext = string.Empty; object value = null; Dictionary <string, string> expvarDic = new Dictionary <string, string>(); foreach (var item in expVars) { LogHelper <ExpressionUtils> .Info("当前解析的表达式变量为:" + item.SqlText); UnResolvedText rawText = new UnResolvedText { RequestParam = item.FromDs, RowsType = item.RowsType, RawText = item.SqlText }; //解析完成的sql string[] sqls = AddinParameterUtils.ReplaceWithParameterValue(rawText); if (sqls.Length > 1) { throw new AddinException("表达式变量不支持多值"); } sqltext = sqls[0]; if (AddinParameterUtils.HasEmptyDataUIParameter(sqls)) { expvarDic.Add(item.VarName.ToLower(), UIParameter.NO_DATA); LogHelper <ExpressionUtils> .Info(item.VarName.ToLower() + " : " + UIParameter.NO_DATA); continue; } if (item.SqlOpType == EnumSqlOpType.Sql) { //SQL语句 value = SqlUtils.ExecuteScalar(session, sqltext); } else if (item.SqlOpType == EnumSqlOpType.Func) { //FUNC value = SqlUtils.ExecuteFunc(session, sqltext); } if (value != null) { //进行变量替换 expvarDic.Add(item.VarName.ToLower(), value.ToString()); //key是小写 } else { expvarDic.Add(item.VarName.ToLower(), "null"); LogHelper <ExpressionUtils> .Info(item.VarName.ToLower() + "表达式变量的值为:null"); } } return(expvarDic); }
public static bool Eval(ISession session, IMethodInvocation invocation, IList <AddinExpressionModel> exps, IList <AddinExpressionVarModel> expVars) { if (expVars == null) { return(false); } if (exps == null) { return(false); } bool rtn = false; //表达式变量计算 IDictionary <string, string> expvarDic = EvalExpVars(session, expVars); Parser parser = new Parser(); //表达式解析 //进行表达式的循环 foreach (var item in exps) { if (string.IsNullOrEmpty(item.ExpText)) { continue; } string text = item.ExpText.ToLower(); //先进行表达式变量替换 foreach (var dickey in expvarDic.Keys) { text = text.Replace(dickey, expvarDic[dickey]); } LogHelper <ExpressionUtils> .Info("当前执行的未替换变量的表达式为:" + text); UnResolvedText expressionText = new UnResolvedText { RawText = text, RowsType = EnumUIDataSourceType.All }; //生成表达式 string[] expressions = AddinParameterUtils.ReplaceWithParameterValue(expressionText); if (AddinParameterUtils.HasEmptyDataUIParameter(expressions)) { LogHelper <ExpressionUtils> .Info("表达式有空的UI参数:" + text); continue; } //生成表达式消息提示字符串 string[] msgs = GetPromptMsgs(expvarDic, item); if (!IsMatchMsg(expressions, msgs)) { throw new AddinException("解析后表达式数量为[" + expressions.Length + "]与提示信息的数量[" + msgs.Length + "]不匹配"); } for (int i = 0; i < expressions.Length; i++) { string expression = expressions[i]; LogHelper <ExpressionUtils> .Info("已替换变量参数的表达式:" + expression); //计算表达式 var expValue = parser.Evaluate(expression); rtn = expValue == null ? false : Convert.ToBoolean(expValue); if (!rtn) { var msg = GetMsg(msgs, i); throw new AddinException(msg); } } } //进行UI变量替换 return(rtn); }