using System.Data; using System.IO; using System.Text; using System.Windows.Input; using HandyControl.Controls; using log4net; using Microsoft.Data.Sqlite; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SparkClient.Model.Entity; using SparkClient.Model.Helper; using SparkClient.ViewModel.Configuration.SettingsPages; using MessageBox = SparkClient.Views.Dialog.MessageBox; using SparkClient.Model.Attributes; namespace SparkClient.ViewModel.Configuration; public class AlgorithmConfigVM : BaseViewModel { private static readonly ILog Logger = LogManager.GetLogger(typeof(AlgorithmConfigVM)); public ICommand SaveAlgorithmDataCommand { get; } public ICommand BeautifyJsonCommand { get; } public ICommand UglifyJsonCommand { get; } private bool _isEnabled; public bool IsEnabled { get { return _isEnabled; } set { _isEnabled = value; OnPropertyChanged(nameof(IsEnabled)); } } private string _AlgorithmConfigJson; public string AlgorithmConfigJson { get { return _AlgorithmConfigJson; } set { _AlgorithmConfigJson = value; OnPropertyChanged("AlgorithmConfigJson"); } } public AlgorithmConfigVM() { SaveAlgorithmDataCommand = new RelayCommand(SaveAlgorithmData); BeautifyJsonCommand = new RelayCommand(BeautifyJson); UglifyJsonCommand = new RelayCommand(UglifyJson); InitAlgorithmData(null); IsEnabledByRole(); } /// /// 初始化算法数据 /// /// [Log] public void InitAlgorithmData(object param) { 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) { foreach (DataRow row in dataTable.Rows) { sb.Append(row["json"].ToString()); } } if (sb.Length > 0) { AlgorithmConfigJson = JToken.Parse(sb.ToString()).ToString(); } } else { 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(); } } } catch (Exception ex) { new MessageBox().Show($"{MultilingualHelper.getString("ApplicationError")}{ex.Message}"); Logger.Error($"全局异常捕获:{ex.Message}", ex); } } /// /// 保存数据 /// /// [Log] public void SaveAlgorithmData(object param) { try { DataBaseHelper.BeginTransaction(); string temp = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(AlgorithmConfigJson)); string deleteSql = @"DELETE FROM ALGORITHM_CONFIG"; DataBaseHelper.ExecuteNonQuery(deleteSql); int order = 0; int insertCount = 0; while (temp.Length > 2000) { AlgorithmConfigEntity entity = new AlgorithmConfigEntity() { GUID = Guid.NewGuid(), JsonOrder = order++, Json = temp.Substring(0, 2000), }; temp = temp.Substring(2000); string sql = entity.GenerateInsertSQL(); SqliteParameter[] sqliteParameters = { new SqliteParameter("@Json", entity.Json), new SqliteParameter("@JsonOrder", entity.JsonOrder), new SqliteParameter("@GUID", entity.GUID), }; insertCount += DataBaseHelper.ExecuteNonQuery(sql, sqliteParameters); } if (temp.Length > 0) { AlgorithmConfigEntity entity = new AlgorithmConfigEntity() { GUID = Guid.NewGuid(), JsonOrder = order++, Json = temp }; string sql = entity.GenerateInsertSQL(); SqliteParameter[] sqliteParameters = { new SqliteParameter("@Json", entity.Json), new SqliteParameter("@JsonOrder", entity.JsonOrder), new SqliteParameter("@GUID", entity.GUID.ToString()), }; insertCount += DataBaseHelper.ExecuteNonQuery(sql, sqliteParameters); } if (insertCount >= 0) { Growl.Info(MultilingualHelper.getString("SaveSuccess")); DataBaseHelper.commit(); } else { Growl.Error(MultilingualHelper.getString("SaveFail")); DataBaseHelper.rollback(); } } catch (Exception ex) { Growl.Error(MultilingualHelper.getString("SaveFail")); Logger.Error($"全局异常捕获:{ex.Message}",ex); } } private void IsEnabledByRole (){ string PERMISSIONS = Settings.SelectValueById("PERMISSIONS"); if ("admin".Equals(PERMISSIONS)) { IsEnabled = true; } else { IsEnabled = false; } } /// /// 美化JSON /// /// [Log] public void BeautifyJson(object param) { try { AlgorithmConfigJson = JToken.Parse(AlgorithmConfigJson).ToString(); } catch (Exception ex) { Growl.ErrorGlobal(ex.Message); } } /// /// 压缩JSON /// /// [Log] public void UglifyJson(object param) { AlgorithmConfigJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(AlgorithmConfigJson)); } // 公共方法来获取 AlgorithmConfigJson [Log] public string GetAlgorithmConfigJson() { return AlgorithmConfigJson; } }