using CSUST.Data.Expr; using JLHHJSvr.Com.Model; using JLHHJSvr.LJException; using JLHHJSvr.LJFramework.Tools; using NPOI.SS.Formula.Functions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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) { AddFormulaItem(new FormulaItem() { formula_name = name, formula = formula }); } 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}"); } } } }