You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
161 lines
5.2 KiB
161 lines
5.2 KiB
using System.Data; |
|
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; |
|
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(); |
|
} |
|
|
|
/// <summary> |
|
/// 初始化算法数据 |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void InitAlgorithmData(object param) |
|
{ |
|
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(); |
|
} |
|
} |
|
|
|
|
|
/// <summary> |
|
/// 保存数据 |
|
/// </summary> |
|
/// <param name="param"></param> |
|
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); |
|
} |
|
} |
|
private void IsEnabledByRole (){ |
|
string PERMISSIONS = Settings.SelectValueById("PERMISSIONS"); |
|
if ("admin".Equals(PERMISSIONS)) |
|
{ |
|
IsEnabled = true; |
|
} |
|
else |
|
{ |
|
IsEnabled = false; |
|
} |
|
} |
|
/// <summary> |
|
/// 美化JSON |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void BeautifyJson(object param) |
|
{ |
|
try |
|
{ |
|
AlgorithmConfigJson = JToken.Parse(AlgorithmConfigJson).ToString(); |
|
} |
|
catch (Exception ex) |
|
{ |
|
Growl.ErrorGlobal(ex.Message); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 压缩JSON |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void UglifyJson(object param) |
|
{ |
|
AlgorithmConfigJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(AlgorithmConfigJson)); |
|
} |
|
|
|
|
|
} |