using MuchInfo.Chart.Data.Models;
using MuchInfo.Chart.DataAdapter;
using System.Windows;
using System.Windows.Controls;
namespace MuchInfo.Chart.WPF.Helpers
{
public class FormulaHelper
{
///
/// 创建 指标菜单
///
/// (flieName为空时取Main指标所在的指标)
/// 选择指标时处理函数
///
public static MenuItem CreateIndicatorMenu(string fileName, RoutedEventHandler indicatorHandler)
{
IDataService iDataService = new DataService();
var spaceModel = iDataService.LoadSpaceFromFile(fileName);
if (spaceModel != null)
{
MenuItem modelItem = new MenuItem();
modelItem.Header = spaceModel.SpaceName;
modelItem.Tag = spaceModel;
MenuByRecursion(modelItem, spaceModel, indicatorHandler);
return modelItem;
}
return null;
}
///
/// 加载指标分类
///
/// 指标地址
///
public static SpaceModel LoadIndicatorSpaceModel(string fileName)
{
IDataService iDataService = new DataService();
return iDataService.LoadSpaceFromFile(fileName);
}
///
/// 递归生成指标
///
/// 父菜单
/// 转换父菜单的对象
/// /// 选择指标时处理函数
private static void MenuByRecursion(MenuItem parentItem, SpaceModel spaceModel,
RoutedEventHandler indicatorHandler)
{
foreach (var space in spaceModel.SpaceModels) //加载命名空间
{
var modelItem = new MenuItem();
modelItem.Header = space.SpaceName;
modelItem.Tag = space;
parentItem.Items.Add(modelItem);
MenuByRecursion(modelItem, space, indicatorHandler);
}
if (spaceModel.ProgramModels != null)
{
foreach (var program in spaceModel.ProgramModels)
{
var programItem = new MenuItem();
programItem.Header = program.Name;
programItem.Tag = program;
programItem.Click += indicatorHandler;
parentItem.Items.Add(programItem);
//programItem.IsMainView = program.IsMainView;
}
}
}
///
/// 通过名称查找公式,找不到返回空
///
/// 名称
/// 公式路径
///
public static FormulaModel GetFormulaByName(string Name, string filePath)
{
IDataService iDataService = new DataService();
var spacel = iDataService.LoadSpaceFromFile(filePath);
if (spacel != null)
{
var programModel = GetProgramModelByName(Name, spacel);
if (!string.IsNullOrEmpty(programModel.Name))
{
return programModel.CovertToFormulaModel();
}
}
return null;
}
///
/// 获取编辑公式
///
/// 查找的公式名称
/// 命名空间
///
private static ProgramModel GetProgramModelByName(string programName, SpaceModel spaceModel)
{
//modified By:ouyang.hongbin
//TODO;修正查找方法
//foreach (var programModel in spaceModel.ProgramModels)
//{
// if (programName.Equals(programModel.Name))
// {
// return programModel;
// }
//}
//foreach (var spaceItem in spaceModel.SpaceModels)
//{
// return GetProgramModelByName(programName, spaceItem);
//}
//return null;
foreach (var spaceModelItem in spaceModel.SpaceModels)
{
foreach (var programModelsItem in spaceModelItem.ProgramModels)
{
if (programName.ToUpper().Equals(programModelsItem.Name.ToUpper()))
{
return programModelsItem;
}
}
}
return null;
}
}
}