using KGIS.Framework.ThreadManager; using KGIS.Framework.Utils; using KGIS.Framework.Utils.Helper; using Kingo.PluginServiceInterface; using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace Kingo.Plugin.DataCheck { public abstract class DataCheckHelper : BaseNotifyProperty, IStartCheck, ICheckStatistic { public string ActiveTitle { get; set; } public string DataCheckName { get; set; } public virtual List CheckGroup { get; set; } public abstract int ErrorNum { get; } public abstract int WaringNum { get; } public abstract int ExceptionNum { get; } public EnumCheckState CheckState { get { if (CheckGroup.FirstOrDefault(f => f.CheckState == EnumCheckState.检查中) != null) return EnumCheckState.检查中; List all = CheckGroup.FindAll(f => f.CheckState == EnumCheckState.检查完成); List all2 = CheckGroup.FindAll(f => f.IsChecked); if (all != null && all2 != null && all.Count == all2.Count) { return EnumCheckState.检查完成; } return EnumCheckState.未检查; } } public List CheckResults { get { return CheckGroup.SelectMany(s => s.CheckResults).ToList(); } } public void StartCheck(object pParm) { foreach (var group in CheckGroup) { if (group.IsChecked) group.StartCheck(pParm); } } public void StartCheckForThread(object pParam) { try { bool ischeck = false; foreach (var item in CheckGroup) { var checkCount = item.CheckRules.Where(x => x.IsChecked == true).ToList(); if (checkCount.Count > 0) { ischeck = true; break; } } if (ischeck) { foreach (var group in CheckGroup) { if (!group.IsChecked) continue; var checkCount = group.CheckRules.Where(x => x.IsChecked == true).ToList(); if (checkCount.Count == 0) continue; ActiveTitle = $"正在进行【{group.GroupName}】数据检查……"; base.NotifyProperty(this, "ActiveTitle"); group.StartCheckForThread(pParam); ActiveTitle = $"【{group.GroupName}】检查完成."; base.NotifyProperty(this, "ActiveTitle"); } MessageHelper.ShowTips(ActiveTitle); } else { ActiveTitle = "请选择检查规则."; base.NotifyProperty(this, "ActiveTitle"); } } catch (Exception ex) { LogAPI.Debug(ex.Message); } } /// /// 加载视图数据 /// /// public void StartLoadData(object pParm) { foreach (var group in CheckGroup) { group.StartLoadData(pParm); } } public void StartRepair(object pParm) { bool ischeck = false; foreach (var item in CheckGroup) { var checkCount = item.CheckRules.Where(x => x.IsChecked == true).ToList(); if (checkCount.Count > 0) { ischeck = true; break; } } if (ischeck) { foreach (var group in CheckGroup) { if (group.IsChecked) { var checkCount = group.CheckRules.Where(x => x.IsChecked == true).ToList(); if (checkCount.Count == 0) continue; ActiveTitle = "正在进行数据修复……"; base.NotifyProperty(this, "ActiveTitle"); group.StartRepair(pParm); } } ActiveTitle = "数据修复完成."; base.NotifyProperty(this, "ActiveTitle"); } else { ActiveTitle = "请选择检查规则."; base.NotifyProperty(this, "ActiveTitle"); } } public void StartSingleRepair(object pParm) { foreach (var group in CheckGroup) { group.StartRepair(pParm); } } } public interface IStartCheck : IDataRepair { void StartCheck(object pParm); void StartCheckForThread(object pParam); } public interface IDataRepair { void StartRepair(object pParm); void StartSingleRepair(object pParm); } /// /// 检查统计接口 /// public interface ICheckStatistic { int ErrorNum { get; } int WaringNum { get; } int ExceptionNum { get; } List CheckResults { get; } } /// /// 规则接口 /// public interface IRule : ICheckStatistic, IStartCheck { string RuleName { get; set; } string RuleDesc { get; set; } bool IsChecked { get; set; } EnumCheckState CheckState { get; set; } } /// /// 检查规则分组 /// public abstract class DataCheckRuleGroup : BaseNotifyProperty, ICheckStatistic, IStartCheck { private bool _IsChecked = true; public bool IsChecked { get => _IsChecked; set { _IsChecked = value; base.NotifyProperty(this, "IsChecked"); } } public virtual string GroupName { get; set; } public virtual string GroupDesc { get; set; } public List CheckRules { get; set; } public DataCheckRule CurrentCheckingRule { get; set; } public int ErrorNum { get { lock (CheckRules) { return CheckRules.Sum(s => s.ErrorNum); } } } public int WaringNum { get { return CheckRules.Sum(s => s.WaringNum); } } public int ExceptionNum { get { return CheckRules.Sum(s => s.ExceptionNum); } } public EnumCheckState CheckState { get { if (CheckRules.FirstOrDefault(f => f.CheckState == EnumCheckState.检查中) != null) return EnumCheckState.检查中; List all = CheckRules.FindAll(f => f.CheckState == EnumCheckState.检查完成); List all2 = CheckRules.FindAll(f => f.IsChecked); if (all != null && all2 != null && all.Count == all2.Count) { return EnumCheckState.检查完成; } return EnumCheckState.未检查; } } public List CheckResults { get { return CheckRules.SelectMany(s => s.CheckResults).ToList(); } } public DataCheckRuleGroup() { CheckRules = new List(); } public void StartCheck(object pParm) { foreach (var item in CheckRules) { if (!item.IsChecked) continue; item.CheckResults.Clear(); item.StartCheck(pParm); UpdateInfo(null); base.NotifyProperty(this, "CheckResults"); } } public void StartLoadData(object pParm) { //foreach (var item in CheckRules) //{ // item.CheckResults.Clear(); // item.StartLoadData(pParm); // //UpdateInfo(null); // //base.NotifyProperty(this, "CheckResults"); //} foreach (var item in CheckRules) { //if (!item.IsChecked) continue; //if (CurrentCheckingRule == null) // CurrentCheckingRule = item; item.CheckResults.Clear(); ThreadManager2.QueueUserWorkItem(new System.Threading.WaitCallback(item.StartLoadData), item, new System.Threading.WaitCallback(UpdateInfo)); //CurrentCheckingRule = item; //base.NotifyProperty(this, "CurrentCheckingRule"); //UpdateInfo(item); } } public abstract void UpdateInfo(object obj); public void StartRepair(object pParm) { foreach (var item in CheckRules) { item.CheckResults.Clear(); item.StartRepair(pParm); UpdateInfo(null); base.NotifyProperty(this, "CheckResults"); } } public void StartSingleRepair(object pParm) { //foreach (var item in CheckRules) //{ // item.CheckResults.Clear(); // item.StartRepair(pParm); // UpdateInfo(null); // //base.NotifyProperty(this, "CheckResults"); //} } public void StartCheckForThread(object pParam) { try { foreach (var item in CheckRules) { if (!item.IsChecked) continue; item.CheckResults.Clear(); UpdateInfo(null); item.StartCheck(pParam); UpdateInfo(null); //ThreadManager2.QueueUserWorkItem(new System.Threading.WaitCallback(item.StartCheck), item, new System.Threading.WaitCallback(UpdateInfo)); } } catch (Exception ex) { LogAPI.Debug(ex.Message); } } } /// /// 数据检查规则 /// public abstract class DataCheckRule : BaseNotifyProperty, IRule { #region 属性 public virtual string RuleName { get; set; } public virtual string RuleDesc { get; set; } //public bool IsChecked { get; set; } private bool _IsChecked = true; public bool IsChecked { get => _IsChecked; set { _IsChecked = value; base.NotifyProperty(this, "IsChecked"); } } /// /// 错误个数 /// public int ErrorNum { get { lock (this) { return CheckResults.Count(c => c.ErrorType == EnumErrorType.错误); } } } /// /// 警告个数 /// public int WaringNum { get { lock (CheckResults) { return CheckResults.Count(c => c.ErrorType == EnumErrorType.警告); } } } /// /// 例外个数 /// public int ExceptionNum { get { lock (CheckResults) { return CheckResults.Count(c => c.ErrorType == EnumErrorType.例外); } } } public List CheckResults { get; } public EnumCheckState CheckState { get; set; } #endregion public DataCheckRule() { CheckResults = new List(); IsChecked = true; CheckState = EnumCheckState.未检查; } public abstract void StartCheck(object pParm); public abstract void StartCheckForThread(object pParam); public abstract void StartLoadData(object pParm); public abstract void StartRepair(object pParm); public abstract void StartSingleRepair(object pParm); } public enum EnumCheckState { 未检查 = 0, 等待检查 = 1, 检查中 = 2, 检查完成 = 3, 检查失败 = 4 } public class BaseNotifyProperty : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void NotifyProperty(object obj, string PropertyName) { PropertyChanged?.Invoke(obj, new PropertyChangedEventArgs(PropertyName)); } } }