CalculateFormula.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using JLHHJSvr.Com.Model;
  2. using JLHHJSvr.LJException;
  3. using JLHHJSvr.LJFramework.Tools;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text.RegularExpressions;
  8. namespace JLHHJSvr.Tools
  9. {
  10. public class CalculateFormula
  11. {
  12. private HashSet<FormulaItem> _formula;
  13. private HashSet<FormulaItem> _const;
  14. public CalculateFormula()
  15. {
  16. _formula = new HashSet<FormulaItem>();
  17. _const = new HashSet<FormulaItem>();
  18. }
  19. public void AddFormulaItem(FormulaItem item)
  20. {
  21. if(!_formula.Add(item))
  22. {
  23. _formula.Remove(item);
  24. _formula.Add(item);
  25. }
  26. }
  27. public void AddConstFormulaItem(FormulaItem item)
  28. {
  29. if (!_const.Add(item))
  30. {
  31. _const.Remove(item);
  32. _const.Add(item);
  33. }
  34. }
  35. public void AddFormulaItem(string name,object value)
  36. {
  37. AddConstFormulaItem(new FormulaItem() { formula_name = name, value = value });
  38. }
  39. public void AddFormulaItem(string name, string formula)
  40. {
  41. var formulaItem = new FormulaItem() { formula_name = name, formula = formula }
  42. AddFormulaItem(formulaItem);
  43. BuildFormulaGraph(formulaItem);
  44. }
  45. // 构建拓扑图
  46. private void BuildFormulaGraph(FormulaItem formula)
  47. {
  48. formula.graph = new List<string>();
  49. // 定义正则表达式模式,匹配包含【】的内容
  50. string pattern = @"【(.*?)】";
  51. // 创建正则表达式对象
  52. Regex regex = new Regex(pattern);
  53. // 使用正则表达式匹配输入字符串
  54. MatchCollection matches = regex.Matches(formula.formula);
  55. foreach (Match match in matches)
  56. {
  57. formula.graph.Add(match.Value);
  58. }
  59. }
  60. private string ConvertToEnglishSymbols(string input)
  61. {
  62. input = input.Replace("(", "(")
  63. .Replace(")", ")")
  64. .Replace(",", ",")
  65. .Replace("。", ".")
  66. .Replace(":", ":")
  67. .Replace(";", ";")
  68. .Replace("“", "\"")
  69. .Replace("”", "\"")
  70. .Replace("‘", "'")
  71. .Replace("’", "'");
  72. return input;
  73. }
  74. private void FormatExpression(FormulaItem formula)
  75. {
  76. formula.formula = ConvertToEnglishSymbols(formula.formula);
  77. }
  78. /// <summary>
  79. /// 公式计算
  80. /// </summary>
  81. /// <param name="expression"></param>
  82. /// <param name="name"></param>
  83. /// <returns></returns>
  84. private void Calculate(FormulaItem formula)
  85. {
  86. FormatExpression(formula);
  87. try
  88. {
  89. formula.value = LJExprParser.Parse(formula.formula).Result;
  90. }
  91. catch (Exception ex)
  92. {
  93. throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}");
  94. }
  95. }
  96. /// <summary>
  97. /// Sql公式计算
  98. /// </summary>
  99. /// <param name="expression"></param>
  100. /// <param name="name"></param>
  101. /// <returns></returns>
  102. private void SqlCalculate(FormulaItem formula)
  103. {
  104. FormatExpression(formula);
  105. try
  106. {
  107. //cmd.CommandText = $@"{formula.formula}";
  108. //cmd.Parameters.Clear();
  109. //return cmd.ExecuteScalar();
  110. }
  111. catch (Exception ex)
  112. {
  113. throw new LJCommonException($"计算{formula.formula_name}公式错误: {ex.Message}");
  114. }
  115. }
  116. }
  117. }