using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IStation.Model
{
public partial class MonitorFluctRecord
{
///
/// 验证
///
public bool Verify()
{
if (this.TimeArray == null || this.TimeArray.Count < 1)
return default;
if (this.MaxValues == null || this.MaxValues.Count < 1)
return default;
if (this.MinValues == null || this.MinValues.Count < 1)
return default;
if ((this.TimeArray.Count == this.MaxValues.Count) && (this.MaxValues.Count == this.MinValues.Count))
return true;
return default;
}
///
/// 获取最小值
///
public double? GetMinValue(DateTime time)
{
if (!Verify())
return default;
var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
var index = this.TimeArray.IndexOf(seconds);
if (index < 0)
return default;
return this.MinValues[index];
}
///
/// 获取大约最小值
///
public double? GetRoundMinValue(DateTime time)
{
if (!Verify())
return default;
var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
var index = this.TimeArray.IndexOf(seconds);
if (index < 0)
{
var smallerIndex = this.TimeArray.FindLastIndex(x => x < seconds);
var biggerIndex = this.TimeArray.FindIndex(x => x > seconds);
if (smallerIndex > -1)
{
//大一点和小一点的都存在
if (biggerIndex > -1)
{
var smaller = this.MinValues[smallerIndex];
var bigger = this.MinValues[biggerIndex];
var value = smaller + (bigger - smaller) / (this.TimeArray[biggerIndex] - this.TimeArray[smallerIndex]) * (seconds - this.TimeArray[smallerIndex]);
return value;
}
else//小一点的存在 大一点的不存在
{
var smaller = this.MinValues[smallerIndex];
return smaller;
}
}
else
{
//小一点的不存在 大一点的存在
if (biggerIndex > -1)
{
var bigger = this.MinValues[biggerIndex];
return bigger;
}
else//小一点的和大一点的都不存在
{
return default;
}
}
}
return this.MinValues[index];
}
///
/// 获取最大值
///
public double? GetMaxValue(DateTime time)
{
if (!Verify())
return default;
var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
var index = this.TimeArray.IndexOf(seconds);
if (index < 0)
return default;
return this.MaxValues[index];
}
///
/// 获取大约最大值
///
public double? GetRoundMaxValue(DateTime time)
{
if (!Verify())
return default;
var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
var index = this.TimeArray.IndexOf(seconds);
if (index < 0)
{
var smallerIndex = this.TimeArray.FindLastIndex(x => x < seconds);
var biggerIndex = this.TimeArray.FindIndex(x => x > seconds);
if (smallerIndex > -1)
{
//大一点和小一点的都存在
if (biggerIndex > -1)
{
var smaller = this.MaxValues[smallerIndex];
var bigger = this.MaxValues[biggerIndex];
var value = smaller + (bigger - smaller) / (this.TimeArray[biggerIndex] - this.TimeArray[smallerIndex]) * (seconds - this.TimeArray[smallerIndex]);
return value;
}
else//小一点的存在 大一点的不存在
{
var smaller = this.MaxValues[smallerIndex];
return smaller;
}
}
else
{
//小一点的不存在 大一点的存在
if (biggerIndex > -1)
{
var bigger = this.MaxValues[biggerIndex];
return bigger;
}
else//小一点的和大一点的都不存在
{
return default;
}
}
}
return this.MaxValues[index];
}
///
/// 设置值
///
public void SetValue(DateTime time, double value)
{
if (this.TimeArray == null)
this.TimeArray = new List();
if (this.MaxValues == null)
this.MaxValues = new List();
if (this.MinValues == null)
this.MinValues = new List();
var seconds = (int)Math.Floor(time.TimeOfDay.TotalSeconds);
var index = this.TimeArray.IndexOf(seconds);
if (index < 0)
{
var biggerIndex = this.TimeArray.FindIndex(x => x > seconds);
if (biggerIndex < 0)
{
this.TimeArray.Add(seconds);
this.MaxValues.Add(value);
this.MinValues.Add(value);
}
else
{
this.TimeArray.Insert(biggerIndex, seconds);
this.MaxValues.Insert(biggerIndex, value);
this.MinValues.Insert(biggerIndex, value);
}
}
else
{
if (this.MaxValues[index] < value)
this.MaxValues[index] = value;
if (this.MinValues[index] > value)
this.MinValues[index] = value;
}
}
}
}