diff --git a/Model/Entity/AgileJsonConfigEntity.cs b/Model/Entity/AgileJsonConfigEntity.cs index 313a1af..764529b 100644 --- a/Model/Entity/AgileJsonConfigEntity.cs +++ b/Model/Entity/AgileJsonConfigEntity.cs @@ -6,34 +6,34 @@ namespace SparkClient.Model.Entity; public class AgileJsonConfigEntity { - private readonly AlgorithmConfigVM _vm; - private readonly List _keys; - public AgileJsonConfigEntity(AlgorithmConfigVM vm, List keys) + + public readonly List Keys; + public AgileJsonConfigEntity(List keys) { - _vm = vm; - _mode = 0; Shape = "圆形"; - Spec = _vm.GetSpecOptions(_mode).FirstOrDefault(); - _keys = keys; + Keys = keys; JsonKeys = keys; + ModeList = GetRunModelList(); + SpceList = GetRunSpecList(); + _mode = ModeList.FirstOrDefault(); + _spec = SpceList.FirstOrDefault(); } - private int _mode; - public int Mode + private Param _mode; + public Param Mode { get => _mode; set { if (_mode == value) return; _mode = value; - OnPropertyChanged(); - // 自动更新规格 - Spec = _vm.GetSpecOptions(value).FirstOrDefault(); + OnPropertyChanged(nameof(Mode)); + } } - private string _spec; - public string Spec + private Param _spec; + public Param Spec { get => _spec; set => SetField(ref _spec, value); @@ -43,7 +43,7 @@ public class AgileJsonConfigEntity public string Shape { get => _shape; - private set => SetField(ref _shape, value); + set => SetField(ref _shape, value); } @@ -54,7 +54,6 @@ public class AgileJsonConfigEntity set { SetField(ref _jsonKey, value); - FilterItems(value); } } @@ -73,16 +72,22 @@ public class AgileJsonConfigEntity set => SetField(ref _jsonKeys, value); } - private string GetDefaultSpec(int mode) + private List _modeList; + + public List ModeList { - return mode switch - { - 0 => "p8-p8", - 1 => "p8-p8-s1", - _ => null - }; + get => _modeList; + set => SetField(ref _modeList, value); + } + private List _specList; + + public List SpceList + { + get => _specList; + set => SetField(ref _specList, value); } + public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string name = null) { @@ -96,17 +101,57 @@ public class AgileJsonConfigEntity OnPropertyChanged(name); return true; } + public List GetRunModelList() + { + return new List + { + new Param() + { + Value = "0", + Name = "实验室模式" + }, + new Param() + { + Value = "1", + Name = "工厂模式" + } + }; + } - private void FilterItems(string key) + public List GetRunSpecList() { - JsonKeys.Clear(); - - foreach (var data in _keys) + return new List { - if (data.ToLower().Contains(key.ToLower())) + new Param() + { + Value = "P8-P8", + Name = "P8-P8" + }, + new Param() + { + Value = "P8-P8-S1", + Name = "P8-P8-S1" + }, + new Param() { - JsonKeys.Add(data); + Value = "P8-P8-S2", + Name = "P8-P8-S2" + }, + new Param() + { + Value = "P8-P8-S3", + Name = "P8-P8-S3" + }, + new Param() + { + Value = "P8-P8-S4", + Name = "P8-P8-S4" } - } + }; } +} +public class Param +{ + public string Value { get; set; } + public string Name { get; set; } } \ No newline at end of file diff --git a/Model/Extension/CommonExtension.cs b/Model/Extension/CommonExtension.cs index 88a6348..7fde3c8 100644 --- a/Model/Extension/CommonExtension.cs +++ b/Model/Extension/CommonExtension.cs @@ -17,4 +17,17 @@ public static class CommonExtension { return String.IsNullOrEmpty(str); } + + public static string toMD5Code(this object obj) + { + if (null == obj) + { + return Helper.Common.GenerateMd5Hash(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")); + } + else + { + var str = $"{DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff")}-{obj.GetHashCode()}-{Helper.Common.AesKey}"; + return Helper.Common.GenerateMd5Hash(str); + } + } } \ No newline at end of file diff --git a/SparkDB.db b/SparkDB.db index a4afb7d..5813df3 100644 Binary files a/SparkDB.db and b/SparkDB.db differ diff --git a/ViewModel/Configuration/AlgorithmConfigVM.cs b/ViewModel/Configuration/AlgorithmConfigVM.cs index 883ac9b..a4fdc71 100644 --- a/ViewModel/Configuration/AlgorithmConfigVM.cs +++ b/ViewModel/Configuration/AlgorithmConfigVM.cs @@ -13,6 +13,7 @@ using SparkClient.Model.Helper; using SparkClient.ViewModel.Configuration.SettingsPages; using MessageBox = SparkClient.Views.Dialog.MessageBox; using SparkClient.Model.Attributes; +using SparkClient.Model.Extension; namespace SparkClient.ViewModel.Configuration; @@ -22,12 +23,20 @@ public class AlgorithmConfigVM : BaseViewModel public ICommand SaveAlgorithmDataCommand { get; } public ICommand BeautifyJsonCommand { get; } public ICommand UglifyJsonCommand { get; } - - public List AgileJsonConfigEntities { get; set; } - - public List JsonKeysBak = new List(); - public List ModeList { get; set; } + private ObservableCollection _agileJsonConfigEntities; + public ObservableCollection AgileJsonConfigEntities + { + get => _agileJsonConfigEntities; + set + { + _agileJsonConfigEntities = value; + OnPropertyChanged(nameof(AgileJsonConfigEntities)); + } + } + + + public List _jsonKeysBak; private bool _isEnabled; public bool IsEnabled { get { return _isEnabled; } set { _isEnabled = value; OnPropertyChanged(nameof(IsEnabled)); } } @@ -41,16 +50,93 @@ public class AlgorithmConfigVM : BaseViewModel UglifyJsonCommand = new RelayCommand(UglifyJson); InitAlgorithmData(null); IsEnabledByRole(); - JsonKeysBak = GetNestedKeys(JObject.Parse(AlgorithmConfigJson)); - ModeList = GetRunModelList(); - AgileJsonConfigEntities = new List(); - AgileJsonConfigEntities.Add(new AgileJsonConfigEntity(this, JsonKeysBak) { - Mode = 0, - Spec = "p8-p8", - - }); + _jsonKeysBak = GetNestedKeys(JObject.Parse(AlgorithmConfigJson)); + InitAgileJsonConfig(); + } + private void InitAgileJsonConfig() + { + AgileJsonConfigEntities = new ObservableCollection(); + string sql = @"SELECT Mode, Shape, Spec, JsonKey, Value FROM AGILE_ALGORITHM_CONFIG"; + DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); + var baseData = new AgileJsonConfigEntity(_jsonKeysBak); + if (dataTable != null && dataTable.Rows.Count >= 0) + { + foreach (DataRow row in dataTable.Rows) + { + try + { + AgileJsonConfigEntities.Add(new AgileJsonConfigEntity(_jsonKeysBak) + { + Mode = baseData.ModeList.Find(e => e.Value.Equals(row["Mode"].ToSafeString())), + Spec = baseData.SpceList.Find(e => e.Value.Equals(row["Spec"].ToSafeString())), + Shape = row["Shape"].ToSafeString(), + JsonKey = row["JsonKey"].ToSafeString(), + Value = row["Value"].ToSafeString() + }); + } catch { } + } + } + } + private bool CheckAndSaveAgileJson() + { + var finalSaveData = new ObservableCollection(); + var insertSql = new List(); + foreach (var row in AgileJsonConfigEntities) + { + if (string.IsNullOrWhiteSpace(row.JsonKey) || string.IsNullOrWhiteSpace(row.Value) || + string.IsNullOrWhiteSpace(row.Shape)) + continue; + if (row.Mode == null || row.Spec == null) + continue; + insertSql.Add($"INSERT INTO AGILE_ALGORITHM_CONFIG (GUID, Mode, Spec, Shape, JsonKey, Value)" + + $@"VALUES ('{row.toMD5Code()}',{row.Mode.Value},'{row.Spec.Value}','{row.Shape}','{row.JsonKey}','{row.Value}');"); + finalSaveData.Add(row); + } + + AgileJsonConfigEntities = finalSaveData; + + var deleteSql = $"DELETE FROM AGILE_ALGORITHM_CONFIG;"; + + try + { + DataBaseHelper.BeginTransaction(); + DataBaseHelper.ExecuteNonQuery(deleteSql); + insertSql.ForEach(e => DataBaseHelper.ExecuteNonQuery(e)); + DataBaseHelper.commit(); + } + catch (Exception e) + { + Logger.Error($"灵活配置存储异常:{e.Message}\r\n {e.StackTrace}"); + new MessageBox().Show($"灵活配置存储异常:{e.Message}"); + DataBaseHelper.rollback(); + return false; + } + + return true; + } + + public string GetAlgorithmConfig(string param) + { + string sql = @"SELECT Mode, Shape, Spec, JsonKey, Value FROM AGILE_ALGORITHM_CONFIG"; + DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); + if (dataTable != null && dataTable.Rows.Count >= 0) + { + var jsonAlgorithm = JToken.Parse(_AlgorithmConfigJson); + foreach (DataRow row in dataTable.Rows) + { + //只对当前运行环境有效 + if (Common.RunMode == int.Parse(row["Mode"].ToSafeString())) + { + + } + } + return jsonAlgorithm.ToString(); + } + + return _AlgorithmConfigJson; + } /// /// 初始化算法数据 @@ -61,68 +147,20 @@ public class AlgorithmConfigVM : BaseViewModel { try { - if (param == null) + AlgorithmConfigJson = "{}"; + string sql = @"SELECT JSON as json FROM ALGORITHM_CONFIG ORDER BY JSON_ORDER"; + DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); + StringBuilder sb = new StringBuilder(); + if (dataTable != null) { - AlgorithmConfigJson = "{}"; - string sql = @"SELECT JSON as json FROM ALGORITHM_CONFIG ORDER BY JSON_ORDER"; - DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); - StringBuilder sb = new StringBuilder(); - if (dataTable != null) + foreach (DataRow row in dataTable.Rows) { - foreach (DataRow row in dataTable.Rows) - { - sb.Append(row["json"].ToString()); - } - } - if (sb.Length > 0) - { - AlgorithmConfigJson = JToken.Parse(sb.ToString()).ToString(); + sb.Append(row["json"].ToString()); } } - else + if (sb.Length > 0) { - string[] types = param.ToString().Split(" "); - string filename = string.Empty; - if (types.Last().Equals("S4")) - { - filename = "p8p8s4.config"; - }else if (types.Last().Equals("S3")) - { - filename = "p8p8s3.config"; - }else if (types.Last().Equals("S2")) - { - filename = "p8p8s2.config"; - }else if (types.Last().Equals("S1")) - { - filename = "p8p8s1.config"; - }else - { - filename = "p8p8.config"; - } - string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SparkCore", "config",filename); - if (!File.Exists(filePath)) - { - AlgorithmConfigJson = "{}"; - string sql = @"SELECT JSON as json FROM ALGORITHM_CONFIG ORDER BY JSON_ORDER"; - DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); - StringBuilder sb = new StringBuilder(); - if (dataTable != null) - { - foreach (DataRow row in dataTable.Rows) - { - sb.Append(row["json"].ToString()); - } - } - if (sb.Length > 0) - { - AlgorithmConfigJson = JToken.Parse(sb.ToString()).ToString(); - } - } - else - { - string content = File.ReadAllText(filePath); - AlgorithmConfigJson = JToken.Parse(content).ToString(); - } + AlgorithmConfigJson = JToken.Parse(sb.ToString()).ToString(); } } catch (Exception ex) @@ -135,31 +173,9 @@ public class AlgorithmConfigVM : BaseViewModel // 命令实现 public ICommand AddCommand => new RelayCommand((param) => { - AgileJsonConfigEntities.Add(new AgileJsonConfigEntity(this, JsonKeysBak) - { - Mode = 0, - Spec = "p8-p8", - - }); + AgileJsonConfigEntities.Add(new AgileJsonConfigEntity(_jsonKeysBak)); }); - - - private static readonly Dictionary> SpecOptions = new() - { - [0] = new List { "p8-p8" }, - [1] = new List { - "p8-p8", "p8-p8-s1", "p8-p8-s2", - "p8-p8-s3", "p8-p8-s4" - } - }; - - public List GetSpecOptions(int mode) - { - return SpecOptions.TryGetValue(mode, out var options) - ? options - : new List(); - } private List GetNestedKeys(JToken token, string prefix = "") { @@ -177,8 +193,6 @@ public class AlgorithmConfigVM : BaseViewModel } return keys; } - - /// /// 保存数据 /// @@ -188,6 +202,11 @@ public class AlgorithmConfigVM : BaseViewModel { try { + if (!CheckAndSaveAgileJson()) + { + return; + } + DataBaseHelper.BeginTransaction(); string temp = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(AlgorithmConfigJson)); string deleteSql = @"DELETE FROM ALGORITHM_CONFIG"; @@ -290,26 +309,6 @@ public class AlgorithmConfigVM : BaseViewModel return AlgorithmConfigJson; } - public List GetRunModelList() - { - return new List - { - new Param() - { - Value = "0", - Name = "实验室模式" - }, - new Param() - { - Value = "1", - Name = "工厂模式" - } - }; - } + } -public class Param -{ - public string Value { get; set; } - public string Name { get; set; } -} \ No newline at end of file diff --git a/Views/Configuration/AlgorithmConfigPage.xaml b/Views/Configuration/AlgorithmConfigPage.xaml index 3f198ee..b4b7fa3 100644 --- a/Views/Configuration/AlgorithmConfigPage.xaml +++ b/Views/Configuration/AlgorithmConfigPage.xaml @@ -88,8 +88,9 @@ - -