CalculateFormula.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using CSUST.Data.Expr;
  2. using JLHHJSvr.Com.Model;
  3. using JLHHJSvr.LJException;
  4. using JLHHJSvr.LJFramework.Tools;
  5. using NPOI.SS.Formula.Functions;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace JLHHJSvr.Tools
  12. {
  13. public class CalculateFormula
  14. {
  15. private HashSet<FormulaItem> _formula;
  16. private HashSet<FormulaItem> _const;
  17. public CalculateFormula()
  18. {
  19. _formula = new HashSet<FormulaItem>();
  20. _const = new HashSet<FormulaItem>();
  21. }
  22. public void AddFormulaItem(FormulaItem item)
  23. {
  24. if(!_formula.Add(item))
  25. {
  26. _formula.Remove(item);
  27. _formula.Add(item);
  28. }
  29. }
  30. public void AddConstFormulaItem(FormulaItem item)
  31. {
  32. if (!_const.Add(item))
  33. {
  34. _const.Remove(item);
  35. _const.Add(item);
  36. }
  37. }
  38. public void AddFormulaItem(string name,object value)
  39. {
  40. AddConstFormulaItem(new FormulaItem() { formula_name = name, value = value });
  41. }
  42. public void AddFormulaItem(string name, string formula)
  43. {
  44. AddFormulaItem(new FormulaItem() { formula_name = name, formula = formula });
  45. }
  46. private string ConvertToEnglishSymbols(string input)
  47. {
  48. input = input.Replace("(", "(")
  49. .Replace(")", ")")
  50. .Replace(",", ",")
  51. .Replace("。", ".")
  52. .Replace(":", ":")
  53. .Replace(";", ";")
  54. .Replace("“", "\"")
  55. .Replace("”", "\"")
  56. .Replace("‘", "'")
  57. .Replace("’", "'");
  58. return input;
  59. }
  60. private void FormatExpression(FormulaItem formula)
  61. {
  62. formula.formula = ConvertToEnglishSymbols(formula.formula);
  63. }
  64. /// <summary>
  65. /// 公式计算
  66. /// </summary>
  67. /// <param name="expression"></param>
  68. /// <param name="name"></param>
  69. /// <returns></returns>
  70. private void Calculate(FormulaItem formula)
  71. {
  72. FormatExpression(formula);
  73. try
  74. {
  75. formula.value = LJExprParser.Parse(formula.formula).Result;
  76. }
  77. catch (Exception ex)
  78. {
  79. throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}");
  80. }
  81. }
  82. /// <summary>
  83. /// Sql公式计算
  84. /// </summary>
  85. /// <param name="expression"></param>
  86. /// <param name="name"></param>
  87. /// <returns></returns>
  88. private void SqlCalculate(FormulaItem formula)
  89. {
  90. FormatExpression(formula);
  91. try
  92. {
  93. //cmd.CommandText = $@"{formula.formula}";
  94. //cmd.Parameters.Clear();
  95. //return cmd.ExecuteScalar();
  96. }
  97. catch (Exception ex)
  98. {
  99. throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}");
  100. }
  101. }
  102. }
  103. }