FormulaCheckExcutor.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using System.Text.RegularExpressions;
  7. using DirectService.Tools;
  8. using JLHHJSvr.BLL;
  9. using JLHHJSvr.Com;
  10. using JLHHJSvr.Com.Model;
  11. using JLHHJSvr.LJFramework.Tools;
  12. using LJLib.DAL.SQL;
  13. using LJLib.Net.SPI.Server;
  14. using Newtonsoft.Json.Linq;
  15. namespace JLHHJSvr.Excutor
  16. {
  17. internal sealed class FormulaCheckExcutor : ExcutorBase<FormulaCheckRequest, FormulaCheckResponse>
  18. {
  19. protected override void ExcuteInternal(FormulaCheckRequest request, object state, FormulaCheckResponse rslt)
  20. {
  21. var tokendata = BllHelper.GetToken(request.token);
  22. if (tokendata == null)
  23. {
  24. rslt.ErrMsg = "会话已经中断,请重新登录";
  25. return;
  26. }
  27. if(string.IsNullOrEmpty(request.formula))
  28. {
  29. rslt.ErrMsg = "公式为空,请检查!";
  30. return;
  31. }
  32. var replacements = new Dictionary<string, decimal>();
  33. var formula = request.formula.Replace("(", "(")
  34. .Replace(")", ")")
  35. .Replace(",", ",")
  36. .Replace("。", ".")
  37. .Replace(":", ":")
  38. .Replace(";", ";")
  39. .Replace("“", "\"")
  40. .Replace("”", "\"")
  41. .Replace("‘", "'")
  42. .Replace("’", "'");
  43. // 定义正则表达式模式,匹配包含【】的内容
  44. string pattern = @"【(.*?)】";
  45. Regex regex = new Regex(pattern);
  46. MatchCollection matches = regex.Matches(formula);
  47. foreach (Match match in matches)
  48. {
  49. if (!replacements.ContainsKey(match.Value))
  50. {
  51. replacements.Add(match.Value, 999M);
  52. }
  53. }
  54. // 相关变量会默认代入999,主要验证运算逻辑是否合理即可。避免除数为0即可
  55. foreach (var item in replacements)
  56. {
  57. formula = formula.Replace(item.Key, "(" + item.Value + ")");
  58. }
  59. if (LJExprParser.CheckFormula(formula,out var arg_msg))
  60. {
  61. rslt.ErrMsg = $"解析表达式{request.formula}失败:{arg_msg}";
  62. }
  63. }
  64. }
  65. }