using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 智能仓储WCS管理系统 { /// /// 警告信息容器 /// public class WarnInfoContainer { ConcurrentDictionary InfoAll = new ConcurrentDictionary(); /// /// 有新的日志加入或移除时,1,当前全部,2,加入的,3移除的 /// public Action, IEnumerable, IEnumerable> WarnInfoChanged; /// /// 添加或者更新信息 /// public void AddOrUpdate(IEnumerable warnInfos) { if (warnInfos == null || !warnInfos.Any()) return; List adds = new List(); List cles = new List(); var info1 = warnInfos.Where(o => o.WarnType == WarnInfoType.WhileWarn).GroupBy(o => o.Source); var info2 = warnInfos.Where(o => o.WarnType == WarnInfoType.AlwayWarn); DateTime dt = DateTime.Now; foreach (var addText in info1) { var yuan = InfoAll.Keys.Where(o => o.WarnType == WarnInfoType.WhileWarn && o.Source == addText.Key); var uni = yuan.Intersect(addText); var add = addText.Except(yuan).Except(uni); var cle = yuan.Except(addText).Except(uni); adds.AddRange(add); cles.AddRange(cle); foreach (var item in cle) { item.TimeTo = item.TimeTo.HasValue ? item.TimeTo : dt; if (!InfoAll.TryRemove(item, out string val)) InfoAll.TryRemove(item, out string val1); } foreach (var item in add) { item.Id = Guid.NewGuid().ToString("N"); item.TimeGo = item.TimeGo == new DateTime() ? dt : item.TimeGo; if (!InfoAll.TryAdd(item, null)) InfoAll.TryAdd(item, null); } } { var exc = info2.Except(InfoAll.Keys); foreach (var item in exc) { item.Id = Guid.NewGuid().ToString("N"); item.TimeGo = item.TimeGo == new DateTime() ? dt : item.TimeGo; if (!InfoAll.TryAdd(item, null)) InfoAll.TryAdd(item, null); adds.Add(item); } } if (adds.Any() || cles.Any()) WarnInfoChanged?.BeginInvoke(InfoAll.Keys.ToArray(), adds, cles, null, null); } /// /// 更新循环警告 /// public void UpdateWhileWarn(IEnumerable texts) { UpdateWhileWarn(string.Empty, texts); } /// /// 更新循环警告 /// public void UpdateWhileWarn(string source, IEnumerable texts) { DateTime dt = DateTime.Now; AddOrUpdate(texts.Select(o => new WarnInfoItem() { Source = source, Text = o, WarnType = WarnInfoType.WhileWarn, TimeGo = dt, TimeTo = null })); } /// /// 添加常驻警告 /// public void AddAlwayWarn(string text) { AddAlwayWarn(string.Empty, new string[] { text }); } /// /// 添加常驻警告 /// public void AddAlwayWarn(IEnumerable texts) { AddAlwayWarn(string.Empty, texts); } /// /// 添加常驻警告 /// > public void AddAlwayWarn(string source, string text) { AddAlwayWarn(source, new string[] { text }); } /// /// 添加常驻警告 /// > public void AddAlwayWarn(string source, IEnumerable texts) { DateTime dt = DateTime.Now; AddOrUpdate(texts.Select(o => new WarnInfoItem() { Source = source, Text = o, WarnType = WarnInfoType.AlwayWarn, TimeGo = dt, TimeTo = null })); } /// /// 移除警告信息 /// public void RemoveAll() { RemoveAll(null); } /// /// 移除警告信息 /// public void RemoveAll(WarnInfoType errType) { RemoveAll(null, errType); } /// /// 移除警告信息 /// public void RemoveAll(string source, WarnInfoType? errType = null) { if (InfoAll.IsEmpty) return; var clear = InfoAll.Keys .Where(o => (source == null ? true : o.Source == source) && (errType.HasValue ? o.WarnType == errType.Value : true)); if (clear == null || !clear.Any()) return; var dt = DateTime.Now; foreach (var item in clear) { item.TimeTo = item.TimeTo.HasValue ? item.TimeTo : dt; if (!InfoAll.TryRemove(item, out string val)) InfoAll.TryRemove(item, out string val1); } WarnInfoChanged?.BeginInvoke(InfoAll.Keys, new List(), clear, null, null); } } /// /// 警告信息 /// public class WarnInfoItem { /// /// 唯一编码 /// public string Id { get; set; } /// /// 来源 /// public string Source { get; set; } /// /// 文本信息 /// public string Text { get; set; } /// /// 级别(提示、警告、错误、致命) /// public string Level { get; set; } /// /// 解决方案 /// public string Solution { get; set; } /// /// 警告类型 /// public WarnInfoType WarnType { get; set; } /// /// 开始时间 /// public DateTime TimeGo { get; set; } /// /// 结束时间 /// public DateTime? TimeTo { get; set; } /// /// 自定义信息 /// public object Tag { get; set; } public override bool Equals(object obj) { if (obj is WarnInfoItem b) return (Source == b.Source && Text == b.Text && Level == b.Level && Solution == b.Solution && WarnType == b.WarnType); else return false; } public override int GetHashCode() { return (Source + Text + Level + Solution + WarnType.ToString()).GetHashCode(); } public override string ToString() { return $"{(string.IsNullOrEmpty(Source) ? "" : $"[{Source}]")}{(string.IsNullOrEmpty(Level) ? "" : $"[{Level}]")}{Text}{(string.IsNullOrEmpty(Solution) ? "" : $"({Solution})")}{(TimeTo.HasValue ? $"({(TimeTo.Value - TimeGo).TotalSeconds.ToString("0.00")}s)" : "")}"; } public static bool operator ==(WarnInfoItem a, WarnInfoItem b) { return a.Equals(b); } public static bool operator !=(WarnInfoItem a, WarnInfoItem b) { return !a.Equals(b); } } /// /// 警告信息类型 /// public enum WarnInfoType { /// /// 常驻错误。加入一次需要手动清除 /// AlwayWarn = 10, /// /// 循环错误。每次需要把全部的错误参入进来 /// WhileWarn = 20, } }