using FLY.OBJComponents.Server; using FLY.OBJComponents.Server.Model; using FLY.Thick.Blowing.IService; using FLY.Thick.Blowing.IService.IShareDBServicePack; using FObjBase; using SQLite; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FLY.Thick.Blowing.Server.Model { /// <summary> /// 数据库 写操作 /// </summary> public class HistoryDb : IShareDbService { /// <summary> /// 本地数据库 /// </summary> public LocalDb localDb; DbModel dbModel; OrgDbModel orgDbModel; public BufferError Errorbuffer; #region IDBShareService /// <summary> /// 产品改变事件 /// </summary> public event ProfileChangedEventHandler ProfileChanged; /// <summary> /// 扫描图改变事件 /// </summary> public event ScanDataChangedEventHandler ScanDataChanged; #endregion /// <summary> /// /// </summary> /// <param name="dBModel"></param> /// <param name="orgDBModel"></param> /// <param name="localDB"></param> public void Init(DbModel dBModel, OrgDbModel orgDBModel, LocalDb localDB) { this.dbModel = dBModel; this.orgDbModel = orgDBModel; this.localDb = localDB; Errorbuffer = new BufferError(); Errorbuffer.Init(dBModel.TbError); } /// <summary> /// 按时间删除数据库 /// </summary> /// <param name="month"></param> public void KeepDBSize(int month) { if (month <= 3) month = 3; DateTime del_time = DateTime.Now - TimeSpan.FromDays(month * 30); string det_time_str = del_time.ToStringOfSQLiteFieldType(); List<string> sqls = new List<string>(); sqls.Add( $"DELETE FROM {dbModel.TbProfile.TableName}" + $" WHERE EndTime < {det_time_str}"); sqls.Add( $"DELETE FROM {dbModel.TbScanData.TableName}" + $" WHERE Time < {det_time_str}"); sqls.Add( $"DELETE FROM {dbModel.TbError.TableName}" + $" WHERE Time < {det_time_str}"); sqls.Add( $"DELETE FROM {dbModel.TbTrendData.TableName}" + $" WHERE Time < {det_time_str}"); dbModel.sqliteHelper.QueryTran(sqls); } /// <summary> /// 按时间删除原始数据库 /// </summary> /// <param name="day"></param> public void KeepOrgDbSize(int day) { if (day <= 2) day = 2; DateTime del_time = DateTime.Now - TimeSpan.FromDays(day); string det_time_str = del_time.ToStringOfSQLiteFieldType(); List<string> sqls = new List<string>(); sqls.Add( $"DELETE FROM {orgDbModel.TbSign.TableName}" + $" WHERE Time < {det_time_str}"); sqls.Add( $"DELETE FROM {orgDbModel.TbRoll.TableName}" + $" WHERE Time < {det_time_str}"); orgDbModel.sqliteHelper.QueryTran(sqls); } /// <summary> /// 保存产品参数 /// </summary> public void AddProfile( Db_Profile profile ) { //把上一次结束掉 if ((localDb.CurrProfile != null) && localDb.IsProfileFinished == false) { FinishProfile(); } //添加 数据库必要的 field localDb.ProfileStartTime = DateTime.Now; localDb.ProfileEndTime = DateTime.Now; localDb.IsProfileFinished = false; profile.ID = dbModel.TbProfile.FreeID; profile.StartTime = localDb.ProfileStartTime; profile.EndTime = localDb.ProfileEndTime; //SQL List<string> sqls = new List<string>(); sqls.Add(SQLiteHelper.GetInsertCommandText(profile)); dbModel.sqliteHelper.QueryTranAsync(sqls); //本地数据备份 localDb.CurrProfile = profile; #region IDBShareService ProfileChanged?.Invoke(this, new ProfileChangedEventArgs() { isInsert = true, profile = profile }); #endregion } /// <summary> /// 修改产品参数 /// </summary> /// <param name="profile"></param> public void UpdateProfile( Db_Profile profile ) { //添加 数据库必要的 field profile.ID = localDb.CurrProfile.ID; profile.StartTime = localDb.ProfileStartTime; profile.EndTime = localDb.ProfileEndTime; //SQL List<string> sqls = new List<string>(); sqls.Add(SQLiteHelper.GetUpdateCommandText(profile, $"WHERE ID = {profile.ID}")); dbModel.sqliteHelper.QueryTranAsync(sqls); //本地数据备份 localDb.CurrProfile = profile; #region IDBShareService ProfileChanged?.Invoke(this, new ProfileChangedEventArgs() { isInsert = false, profile = profile }); #endregion } /// <summary> /// 下辊,结束当前产品生产 /// </summary> public void FinishProfile() { if (localDb.CurrProfile == null) return; //SQL dbModel.sqliteHelper.ExecuteNonQuery( $"UPDATE {dbModel.TbProfile.TableName} SET IsFinished = {true} WHERE ID = {localDb.CurrProfile.ID}"); localDb.IsProfileFinished = true; localDb.CurrProfile.IsFinished = true; #region IDBShareService ProfileChanged?.Invoke(this, new ProfileChangedEventArgs() { isInsert = false, profile = localDb.CurrProfile }); #endregion } /// <summary> /// 不需设置 DB_ScanData 内 ProfileID, 自动设置! /// </summary> public void AddScanData( Lc_ScanData scanData) { bool updateEnd = false; //添加 数据库必要的 field scanData.ID = dbModel.TbScanData.FreeID; if (localDb.ProfileEndTime < scanData.Time) { localDb.ProfileEndTime = scanData.Time; updateEnd = true; } //SQLs List<string> sqls = new List<string>(); var db_scandata = Lc_AutoMapperProfile.Mapper.Map<Db_ScanData>(scanData); sqls.Add(SQLiteHelper.GetInsertCommandText(db_scandata)); if (updateEnd) { string s_endtime = localDb.ProfileEndTime.ToStringOfSQLiteFieldType(); sqls.Add($"UPDATE {dbModel.TbProfile.TableName} SET EndTime={s_endtime} WHERE ID = {localDb.CurrProfile.ID}"); } dbModel.sqliteHelper.QueryTranAsync(sqls); //本地数据备份 localDb.CurrProfile.EndTime = localDb.ProfileEndTime; #region IDBShareService ScanDataChanged?.Invoke(this, new ScanDataChangedEventArgs() { isInsert = true, scandata = scanData }); if (updateEnd) { ProfileChanged?.Invoke(this, new ProfileChangedEventArgs() { isInsert = false, profile = localDb.CurrProfile }); } #endregion } /// <summary> /// 不需设置 DB_TrendData 内 ProfileID, 自动设置! /// </summary> public void AddTrendData( Lc_TrendData trendData) { bool updateEnd = false; //添加 数据库必要的 field trendData.ID = dbModel.TbTrendData.FreeID; if (localDb.ProfileEndTime < trendData.Time) { localDb.ProfileEndTime = trendData.Time; updateEnd = true; } //SQLs List<string> sqls = new List<string>(); var db_trenddata = Lc_AutoMapperProfile.Mapper.Map<Db_TrendData>(trendData); sqls.Add(SQLiteHelper.GetInsertCommandText(db_trenddata)); if (updateEnd) { string s_endtime = localDb.ProfileEndTime.ToStringOfSQLiteFieldType(); sqls.Add($"UPDATE {dbModel.TbProfile.TableName} SET EndTime={s_endtime} WHERE ID = {localDb.CurrProfile.ID}"); } dbModel.sqliteHelper.QueryTranAsync(sqls); //本地数据备份 localDb.CurrProfile.EndTime = localDb.ProfileEndTime; #region IDBShareService if (updateEnd) { ProfileChanged?.Invoke(this, new ProfileChangedEventArgs() { isInsert = false, profile = localDb.CurrProfile }); } #endregion } /// <summary> /// 需要通过 ID才能更新 /// </summary> public void UpdateScanData( Lc_ScanData scanData) { //添加 数据库必要的 field //SQLs List<string> sqls = new List<string>(); var db_scandata = Lc_AutoMapperProfile.Mapper.Map<Db_ScanData>(scanData); sqls.Add(SQLiteHelper.GetUpdateCommandText(db_scandata, $"WHERE ID = {scanData.ID}")); dbModel.sqliteHelper.QueryTranAsync(sqls); #region IDBShareService ScanDataChanged?.Invoke(this, new ScanDataChangedEventArgs() { isInsert = false, scandata = scanData }); #endregion } /// <summary> /// /// </summary> /// <param name="db_errorData"></param> public void AddErrorData( DB_Error db_errorData ) { //添加 数据库必要的 field db_errorData.ID = dbModel.TbError.FreeID; //SQLs List<string> sqls = new List<string>(); sqls.Add(SQLiteHelper.GetInsertCommandText(db_errorData)); dbModel.sqliteHelper.QueryTranAsync(sqls); } #region 原始数据 /// <summary> /// 记录 原始数据.转向信号 /// </summary> /// <param name="lc_Sign"></param> public void AddSignData( Lc_Sign lc_Sign ) { lc_Sign.ID = orgDbModel.TbSign.FreeID; //SQLs List<string> sqls = new List<string>(); var db_Sign = OrgLc_AutoMapperProfile.Mapper.Map<Db_Sign>(lc_Sign); sqls.Add(SQLiteHelper.GetInsertCommandText(db_Sign)); orgDbModel.sqliteHelper.QueryTranAsync(sqls); } /// <summary> /// 记录 原始数据.辊信号 /// </summary> /// <param name="lc_Roll"></param> public void AddRollData( Lc_Roll lc_Roll ) { lc_Roll.ID = orgDbModel.TbRoll.FreeID; //SQLs List<string> sqls = new List<string>(); var db_Roll = OrgLc_AutoMapperProfile.Mapper.Map<Db_Roll>(lc_Roll); sqls.Add(SQLiteHelper.GetInsertCommandText(db_Roll)); orgDbModel.sqliteHelper.QueryTranAsync(sqls); } #endregion #region IDBShareService /// <summary> /// /// </summary> /// <param name="asyncDelegate"></param> /// <param name="asyncContext"></param> public void GetProfile(AsyncCBHandler asyncDelegate, object asyncContext) { if (localDb.CurrProfile != null) { asyncDelegate.Invoke(asyncContext, localDb.CurrProfile); } else { asyncDelegate.Invoke(asyncContext, null); } } #endregion } }