using System;
namespace IStation.Algorithm
{
#region ViewModel
///
/// 分析泵项
///
public class AnaItem
{
public AnaItem() { }
public string RunFlag { get; set; }
public int RunCount { get; set; }
public List PumpIds { get; set; }
public double Flow { get; set; }
public double Head { get; set; }
}
#endregion
///
/// 调度分析辅助类
///
public class SchedulingHelper
{
double _frequency_def = 50;
double _frequency_min = 25;
double _frequency_max = 50;
double _frequency_space = 0.1;//频率间隔
double _head_space = 0.1;//扬程间隔
#region RunFlag
string _falgFrePumpTag = "B";
string _falgFixPumpTag = "G";
string _falgSpaceMark = "_";
string GetRunFlag(List pumpItems)
{
var tableName = string.Empty;
var count = pumpItems.Count;
pumpItems = pumpItems.OrderBy(p => p.ID).ToList();
for (int i = 0; i < count; i++)
{
var pumpItem = pumpItems[i];
tableName += GetRunFlag(pumpItem);
if ((i + 1) != count)
{
tableName += _falgSpaceMark;
}
}
return tableName;
}
string GetRunFlag(AnaPumpItem pumpItem)
{
return (pumpItem.IsBp ? _falgFrePumpTag : _falgFixPumpTag) + pumpItem.ID;
}
string GetRunFlag(Dictionary dic)
{
var tableName = string.Empty;
var index = 0;
var count = dic.Count;
foreach (var item in dic)
{
tableName += GetRunFlag(item.Key, item.Value);
index++;
if (index != count)
{
tableName += _falgSpaceMark;
}
}
return tableName;
}
string GetRunFlag(long id, bool isBp)
{
return (isBp ? _falgFrePumpTag : _falgFixPumpTag) + id;
}
string GetRunFlag(List flags)
{
var tableName = string.Empty;
var count = flags.Count;
var index = 0;
foreach (var flag in flags)
{
tableName += flag;
index++;
if (index != count)
{
tableName += _falgSpaceMark;
}
}
return tableName;
}
#endregion
DAL.ScheduleCombine _dal = new DAL.ScheduleCombine();
DAL.ScheduleConclusion _dalScheduleConclusion = new DAL.ScheduleConclusion();
DAL.ScheduleAnaLog _dalAnaLog = new DAL.ScheduleAnaLog();
public class CurrentViewModel : Entity.ScheduleConclusion
{
public CurrentViewModel(Entity.ScheduleConclusion rhs) : base(rhs)
{
this.Flags = new List();
var flags = rhs.RunFlag.Split('_');
foreach (var item in flags)
{
if (int.TryParse(item.Substring(1), out int flag))
{
this.Flags.Add(flag);
}
}
}
public List Flags { get; set; }
}
public void Ana(List pumps, double tagetFlow, double tagetHead, List openPumpCombine)
{
var pumpIds = pumps.Select(x => x.ID);
var dic_pump = pumps.ToDictionary(x => x.ID, x => x.IsBp);
if (openPumpCombine != null && openPumpCombine.Any())
{
var allCombineList = PermutationAndCombination.GetCombination(pumpIds.ToArray(), 2);
var combineList = new List>();
foreach (var combine in allCombineList)
{
var pump1 = combine[0];
var pump2 = combine[1];
if (!openPumpCombine.Contains(pump1))
continue;
if (!openPumpCombine.Contains(pump2))
continue;
combineList.Add(combine.ToList());
}
if (combineList.Count < 1)
return;
var avgFlow = tagetFlow / 2;
var optimalConclusionList = new List();
foreach (var combine in combineList)
{
var dic = new Dictionary();
foreach (var pump in combine)
{
if (dic_pump.ContainsKey(pump))
{
dic.Add(pump, dic_pump[pump]);
}
}
var runFalg = GetRunFlag(dic);
var list = _dalScheduleConclusion.GetList(runFalg, avgFlow, tagetHead, 1);
if (list != null && list.Any())
{
var vmList = list.Select(x => new CurrentViewModel(x)).ToList();
optimalConclusionList.AddRange(vmList);
}
}
if (optimalConclusionList == null || !optimalConclusionList.Any())
{
return;
}
var optimalConclusionCombineList = PermutationAndCombination.GetCombination(optimalConclusionList.ToArray(), 2);
var combines = new List();
foreach (var arr in optimalConclusionCombineList)
{
var falgs = arr.SelectMany(x => x.Flags).ToList();
if (falgs.GroupBy(x => x).Where(x => x.Count() > 1).Count() > 0)
{
continue;
}
var combine = new Combine();
combine.Power = arr.Sum(x => x.Power);
combine.Head = arr.Average(x => x.Head);
combine.Flow = arr.Sum(x => x.Flow);
combine.RunFlagList = falgs;
combines.Add(combine);
}
var a = combines;
}
}
private class Combine
{
public double Flow { get; set; }
public double Head { get; set; }
public double Power { get; set; }
public List RunFlagList { get; set; }
}
///
/// 插入分析日志
///
private void InsertAnaLog(string info)
{
var entity = new Entity.ScheduleAnaLog(info);
_dalAnaLog.Insert(entity);
}
///
/// 判断表是否存在
///
public bool ExistTable(string runFlag)
{
return _dal.ExistTable(runFlag);
}
}
}