123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using CSUST.Data.Expr;
- using JLHHJSvr.BLL;
- using JLHHJSvr.LJException;
- using JLHHJSvr.LJFramework.Tools;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace JLHHJSvr.Helper
- {
- internal class CalculateHepler : HelperBase
- {
- public static string ConvertToEnglishSymbols(string input)
- {
- input = input.Replace("(", "(")
- .Replace(")", ")")
- .Replace(",", ",")
- .Replace("。", ".")
- .Replace(":", ":")
- .Replace(";", ";")
- .Replace("“", "\"")
- .Replace("”", "\"")
- .Replace("‘", "'")
- .Replace("’", "'");
- return input;
- }
- private TData CalculateVarFormula(string varname)
- {
- varname = $"【{varname}】";
- if (!formula_replacements.ContainsKey(varname)) return null;
- return Calculate(varname);
- }
- private void FormatExpression(ref string expression)
- {
- ConvertToEnglishSymbols(expression);
- // 定义正则表达式模式,匹配包含【】的内容
- string pattern = @"【(.*?)】";
- // 创建正则表达式对象
- Regex regex = new Regex(pattern);
- foreach (var replacement in formula_replacements)
- {
- expression = expression.Replace(replacement.Key, replacement.Value);
- }
- // 使用正则表达式匹配输入字符串
- MatchCollection matches = regex.Matches(expression);
- var new_replacements = new Dictionary<string, object>();
- foreach (Match match in matches)
- {
- if (replacements.TryGetValue(match.Value, out object value2))
- {
- new_replacements[match.Value] = value2;
- }
- }
- foreach (var replacement in new_replacements)
- {
- expression = expression.Replace(replacement.Key, Convert.ToString(replacement.Value));
- }
- }
- private TData Calculate(string expression, string name = "")
- {
- FormatExpression(ref expression);
- try
- {
- return LJExprParser.Parse(expression).Result;
- }
- catch (Exception ex)
- {
- throw new LJCommonException($"计算{name}公式错误!expression: {expression},exception: {ex.Message}");
- }
- }
- private object SqlCalculate(string expression, string name = "")
- {
- FormatExpression(ref expression);
- try
- {
- cmd.CommandText = $@"{expression}";
- cmd.Parameters.Clear();
- return cmd.ExecuteScalar();
- }
- catch (Exception ex)
- {
- throw new LJCommonException($"计算{name}公式错误!expression: {expression},exception: {ex.Message}");
- }
- }
- }
- }
|