using JLHHJSvr.Com.Model; using JLHHJSvr.LJException; using JLHHJSvr.LJFramework.Tools; using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace JLHHJSvr.Tools { public class CalculateFormula { private HashSet _formula; private HashSet _const; public CalculateFormula() { _formula = new HashSet(); _const = new HashSet(); } public void AddFormulaItem(FormulaItem item) { if(!_formula.Add(item)) { _formula.Remove(item); _formula.Add(item); } } public void AddConstFormulaItem(FormulaItem item) { if (!_const.Add(item)) { _const.Remove(item); _const.Add(item); } } public void AddFormulaItem(string name,object value) { AddConstFormulaItem(new FormulaItem() { formula_name = name, value = value }); } public void AddFormulaItem(string name, string formula) { var formulaItem = new FormulaItem() { formula_name = name, formula = formula } AddFormulaItem(formulaItem); BuildFormulaGraph(formulaItem); } // 构建拓扑图 private void BuildFormulaGraph(FormulaItem formula) { formula.graph = new List(); // 定义正则表达式模式,匹配包含【】的内容 string pattern = @"【(.*?)】"; // 创建正则表达式对象 Regex regex = new Regex(pattern); // 使用正则表达式匹配输入字符串 MatchCollection matches = regex.Matches(formula.formula); foreach (Match match in matches) { formula.graph.Add(match.Value); } } private string ConvertToEnglishSymbols(string input) { input = input.Replace("(", "(") .Replace(")", ")") .Replace(",", ",") .Replace("。", ".") .Replace(":", ":") .Replace(";", ";") .Replace("“", "\"") .Replace("”", "\"") .Replace("‘", "'") .Replace("’", "'"); return input; } private void FormatExpression(FormulaItem formula) { formula.formula = ConvertToEnglishSymbols(formula.formula); } /// /// 公式计算 /// /// /// /// private void Calculate(FormulaItem formula) { FormatExpression(formula); try { formula.value = LJExprParser.Parse(formula.formula).Result; } catch (Exception ex) { throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}"); } } /// /// Sql公式计算 /// /// /// /// private void SqlCalculate(FormulaItem formula) { FormatExpression(formula); try { //cmd.CommandText = $@"{formula.formula}"; //cmd.Parameters.Clear(); //return cmd.ExecuteScalar(); } catch (Exception ex) { throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}"); } } } }