using HandyControl.Tools.Extension; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 智能仓储WCS管理系统 { /// /// 警告信息数据库 /// public static class WarnInfoDb { /// /// 是否启用 /// public static bool IsEnabled = false; public static SqlSugarScope db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = $"Data Source={Path.Combine(LocalFile.DataDir, "WarnInfo.db3")};Version=3;journal_mode=WAL;",//并发写加入:journal_mode=WAL; DbType = DbType.Sqlite,//[Sqlite]安装[System.Data.SQLite.Core]; IsAutoCloseConnection = true }, db => { db.Aop.OnError = ex => { Logs.Write($@"{nameof(WarnInfoDb)}{Environment.NewLine}SQL:{ex?.Sql}{Environment.NewLine}Parametres:{JsonConvert.SerializeObject(ex?.Parametres)}{Environment.NewLine}InnerException:{ex?.InnerException?.ToString()}{Environment.NewLine}Exception:{ex?.ToString()}{Environment.NewLine}", LogsType.DbErr); }; }); /// /// 初始化数据 /// public static void Ini() { //不存在创建数据库,存在不会创建 db.DbMaintenance.CreateDatabase(); //创建表根据实体类 db.CodeFirst.InitTables(typeof(WarnInfoItemDb)); IsEnabled = true; } /// /// 清除数据 /// /// 保留时间 /// 清理的数量 public static int Clear(TimeSpan time) { try { var dt = DateTime.Now.Date.AddDays(1) - time; return WarnInfoDb.db.Deleteable().Where(o => o.TimeGo < dt).ExecuteCommand(); } catch (Exception) { return 0; } } } /// /// 警告信息 /// [SugarTable("WarnInfoItem")] [SugarIndex("index_TimeGo", nameof(WarnInfoItemDb.TimeGo), OrderByType.Desc)] public class WarnInfoItemDb { [SugarColumn(IsPrimaryKey = true, IsIdentity = false)] public string Id { get; set; } /// /// 来源 /// [SugarColumn(IsNullable = true)] public string Source { get; set; } /// /// 文本信息 /// [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsNullable = true)] public string Text { get; set; } /// /// 级别(提示、警告、错误、致命) /// [SugarColumn(IsNullable = true)] public string Level { get; set; } /// /// 解决方案 /// [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString, IsNullable = true)] public string Solution { get; set; } /// /// 警告类型 /// public string WarnType { get; set; } /// /// 开始时间 /// public DateTime TimeGo { get; set; } /// /// 结束时间 /// [SugarColumn(IsNullable = true)] public DateTime? TimeTo { get; set; } /// /// 持续时间 /// [SugarColumn(IsIgnore = true)] public string DuraTime { get => TimeTo.HasValue ? $"{((int)(TimeTo.Value - TimeGo).TotalHours).ToString().PadLeft(2, '0')}:{(TimeTo.Value - TimeGo).Minutes.ToString().PadLeft(2, '0')}:{(TimeTo.Value - TimeGo).Seconds.ToString().PadLeft(2, '0')}" : ""; } public static List GetList(IEnumerable warnInfos) { return warnInfos.Select(o => new WarnInfoItemDb() { Id = o.Id, Source = o.Source, Text = o.Text, Level = o.Level, Solution = o.Solution, WarnType = o.WarnType == WarnInfoType.AlwayWarn ? "常驻错误" : "循环错误", TimeGo = o.TimeGo, TimeTo = o.TimeTo, }).ToList(); } } }