123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 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<FormulaItem> _formula;
- private HashSet<FormulaItem> _const;
- public CalculateFormula()
- {
- _formula = new HashSet<FormulaItem>();
- _const = new HashSet<FormulaItem>();
- }
- 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);
- }
- /// <summary>
- /// 公式计算
- /// </summary>
- /// <param name="expression"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- 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}");
- }
- }
- /// <summary>
- /// Sql公式计算
- /// </summary>
- /// <param name="expression"></param>
- /// <param name="name"></param>
- /// <returns></returns>
- 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}");
- }
- }
- }
- }
|