using FLY.Thick.Blowing.IService;
using FLY.Thick.Blowing.IService.IBulkDBServicePack;
using FObjBase;
using Misc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FLY.Thick.Blowing.Server.Model
{
///
/// 提供接口,让别人获取 整理好的 当前生产,或之前生产的数据。
/// 从LocalDb 获取数据, 或从 直接从数据获取数据
///
/// 与HistoryDb,成对。
/// HistoryDb 是写
/// BulkDb 是读
///
public class BulkDb : IBulkDbService
{
///
/// 本地数据库
///
public LocalDb localDb;
DbModel dbModel;
IShareDbService shareDb;
///
/// 数据库总数量,当它变化了,肯定有新数据
///
public int Count { get; set; }
///
/// 开始产生数据时间
///
public DateTime StartTime { get; private set; }
///
/// 最后产生数据时间
///
public DateTime EndTime { get; private set; }
///
/// 当前生产,已经结束,已经下辊了
///
public bool IsFinished { get; private set; }
///
///
///
public event PropertyChangedEventHandler PropertyChanged;
///
/// 动态数据推送
///
public event BulkDBTempFrameChangedEventHander TempFrameChanged;
///
///
///
public BulkDb()
{
}
///
/// 写入动态数据
///
///
///
public void SetTempFrame(int startIndex, double[] D)
{
TempFrameChanged?.Invoke(this, new BulkDBTempFrameChangedEventArgs() { StartIndex = startIndex, D = D });
}
///
///
///
///
///
///
public void Init(IShareDbService shareDb,
LocalDb localDb, DbModel dbModel)
{
this.localDb = localDb;
this.dbModel = dbModel;
this.shareDb = shareDb;
LocalDBLoad();
Misc.BindingOperations.SetBinding(localDb, "ProfileStartTime", this, "StartTime");
Misc.BindingOperations.SetBinding(localDb, "ProfileEndTime", this, "EndTime");
Misc.BindingOperations.SetBinding(localDb, "IsProfileFinished", this, "IsFinished");
shareDb.ScanDataChanged += ShareDB_ScanDataAdded;
}
private void ShareDB_ScanDataAdded(object sender, IService.IShareDBServicePack.ScanDataChangedEventArgs e)
{
Count++;
//await Task.Factory.StartNew(() =>
//{
// var n = dBModel.sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dBModel.TbScanData.TableName}");
// Count = System.Convert.ToInt32(n);
//});
}
///
/// 加载数据库中最后1次订单数据到 本地
///
void LocalDBLoad()
{
var reponse = dbModel.sqliteHelper.ExecuteScalar($"SELECT MAX(ID) FROM {dbModel.TbProfile.TableName}");
if (reponse is DBNull)
{
localDb.ProfileStartTime = DateTime.MinValue;
localDb.ProfileEndTime = DateTime.MinValue;
localDb.IsProfileFinished = true;
}
else
{
long maxid = System.Convert.ToInt64(reponse);
var profile = dbModel.TbProfile.Find($"WHERE ID = {maxid}").First();
localDb.CurrProfile = profile;
localDb.ProfileStartTime = profile.StartTime;
localDb.ProfileEndTime = profile.EndTime;
localDb.IsProfileFinished = profile.IsFinished;
}
var n = dbModel.sqliteHelper.ExecuteScalar($"SELECT COUNT(*) FROM {dbModel.TbScanData.TableName}");
Count = System.Convert.ToInt32(n);
}
///
/// 获取N幅数据,需要异步
///
///
///
///
public async void GetFrame(Pack_GetFrameRequest request, AsyncCBHandler AsyncDelegate, object AsyncContext)
{
Pack_GetFrameReponse reponse = new Pack_GetFrameReponse();
reponse.Request = request;
reponse.Values = new List();
if (request.Count > 10)//数量限制,避免死机
request.Count = 10;
await Task.Factory.StartNew(() =>
{
var db_scandatas = dbModel.TbScanData.Find($"ORDER BY ID LIMIT {request.Index},{request.Count}");
var lc_scanDatas = Lc_AutoMapperProfile.Mapper.Map, List>(db_scandatas);
reponse.Values.AddRange(lc_scanDatas);
});
AsyncDelegate(AsyncContext, reponse);
}
///
/// 获取纵向趋势图
///
///
///
///
public async void GetTrend(Pack_GetTrendRequest request, AsyncCBHandler AsyncDelegate, object AsyncContext)
{
//TODO 需要异步
Pack_GetTrendReponse reponse = new Pack_GetTrendReponse();
reponse.Request = request;
reponse.Values = new List();
if (request.Count > 400)//数量限制,避免死机
request.Count = 400;
await Task.Factory.StartNew(() =>
{
var db_scandatas = dbModel.TbScanData.Find($"ORDER BY ID LIMIT {request.Index},{request.Count}");
var lc_scanDatas = Lc_AutoMapperProfile.Mapper.Map, List>(db_scandatas);
reponse.Values.AddRange(lc_scanDatas.Select(sd =>
{
double avg = sd.Thicks.AverageNoNull();
double sigma = sd.Thicks.Sigma();
return new TrendValue()
{
Sigma = sigma,
Value = avg,
Time = sd.Time
};
}));
});
AsyncDelegate(AsyncContext, reponse);
}
///
/// 完成
///
public void Finish()
{
shareDb.FinishProfile();
}
}
}