FormulaHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 
  2. using MuchInfo.Chart.Data.Models;
  3. using MuchInfo.Chart.DataAdapter;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. namespace MuchInfo.Chart.WPF.Helpers
  7. {
  8. public class FormulaHelper
  9. {
  10. /// <summary>
  11. /// 创建 指标菜单
  12. /// </summary>
  13. /// <param name="fileName">(flieName为空时取Main指标所在的指标)</param>
  14. /// <param name="indicatorHandler">选择指标时处理函数</param>
  15. /// <returns></returns>
  16. public static MenuItem CreateIndicatorMenu(string fileName, RoutedEventHandler indicatorHandler)
  17. {
  18. IDataService iDataService = new DataService();
  19. var spaceModel = iDataService.LoadSpaceFromFile(fileName);
  20. if (spaceModel != null)
  21. {
  22. MenuItem modelItem = new MenuItem();
  23. modelItem.Header = spaceModel.SpaceName;
  24. modelItem.Tag = spaceModel;
  25. MenuByRecursion(modelItem, spaceModel, indicatorHandler);
  26. return modelItem;
  27. }
  28. return null;
  29. }
  30. /// <summary>
  31. /// 加载指标分类
  32. /// </summary>
  33. /// <param name="fileName">指标地址</param>
  34. /// <returns></returns>
  35. public static SpaceModel LoadIndicatorSpaceModel(string fileName)
  36. {
  37. IDataService iDataService = new DataService();
  38. return iDataService.LoadSpaceFromFile(fileName);
  39. }
  40. /// <summary>
  41. /// 递归生成指标
  42. /// </summary>
  43. /// <param name="parentItem">父菜单</param>
  44. /// <param name="spaceModel">转换父菜单的对象</param>
  45. /// /// <param name="indicatorHandler">选择指标时处理函数</param>
  46. private static void MenuByRecursion(MenuItem parentItem, SpaceModel spaceModel,
  47. RoutedEventHandler indicatorHandler)
  48. {
  49. foreach (var space in spaceModel.SpaceModels) //加载命名空间
  50. {
  51. var modelItem = new MenuItem();
  52. modelItem.Header = space.SpaceName;
  53. modelItem.Tag = space;
  54. parentItem.Items.Add(modelItem);
  55. MenuByRecursion(modelItem, space, indicatorHandler);
  56. }
  57. if (spaceModel.ProgramModels != null)
  58. {
  59. foreach (var program in spaceModel.ProgramModels)
  60. {
  61. var programItem = new MenuItem();
  62. programItem.Header = program.Name;
  63. programItem.Tag = program;
  64. programItem.Click += indicatorHandler;
  65. parentItem.Items.Add(programItem);
  66. //programItem.IsMainView = program.IsMainView;
  67. }
  68. }
  69. }
  70. /// <summary>
  71. /// 通过名称查找公式,找不到返回空
  72. /// </summary>
  73. /// <param name="Name">名称</param>
  74. /// <param name="filePath">公式路径</param>
  75. /// <returns></returns>
  76. public static FormulaModel GetFormulaByName(string Name, string filePath)
  77. {
  78. IDataService iDataService = new DataService();
  79. var spacel = iDataService.LoadSpaceFromFile(filePath);
  80. if (spacel != null)
  81. {
  82. var programModel = GetProgramModelByName(Name, spacel);
  83. if (!string.IsNullOrEmpty(programModel.Name))
  84. {
  85. return programModel.CovertToFormulaModel();
  86. }
  87. }
  88. return null;
  89. }
  90. /// <summary>
  91. /// 获取编辑公式
  92. /// </summary>
  93. /// <param name="programName">查找的公式名称</param>
  94. /// <param name="spaceModel">命名空间</param>
  95. /// <returns></returns>
  96. private static ProgramModel GetProgramModelByName(string programName, SpaceModel spaceModel)
  97. {
  98. //modified By:ouyang.hongbin
  99. //TODO;修正查找方法
  100. //foreach (var programModel in spaceModel.ProgramModels)
  101. //{
  102. // if (programName.Equals(programModel.Name))
  103. // {
  104. // return programModel;
  105. // }
  106. //}
  107. //foreach (var spaceItem in spaceModel.SpaceModels)
  108. //{
  109. // return GetProgramModelByName(programName, spaceItem);
  110. //}
  111. //return null;
  112. foreach (var spaceModelItem in spaceModel.SpaceModels)
  113. {
  114. foreach (var programModelsItem in spaceModelItem.ProgramModels)
  115. {
  116. if (programName.ToUpper().Equals(programModelsItem.Name.ToUpper()))
  117. {
  118. return programModelsItem;
  119. }
  120. }
  121. }
  122. return null;
  123. }
  124. }
  125. }