|
|
|
@ -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; |
|
|
|
|
|
|
|
|
@ -23,11 +24,19 @@ public class AlgorithmConfigVM : BaseViewModel |
|
|
|
|
public ICommand BeautifyJsonCommand { get; } |
|
|
|
|
public ICommand UglifyJsonCommand { get; } |
|
|
|
|
|
|
|
|
|
public List<AgileJsonConfigEntity> AgileJsonConfigEntities { get; set; } |
|
|
|
|
private ObservableCollection<AgileJsonConfigEntity> _agileJsonConfigEntities; |
|
|
|
|
public ObservableCollection<AgileJsonConfigEntity> AgileJsonConfigEntities |
|
|
|
|
{ |
|
|
|
|
get => _agileJsonConfigEntities; |
|
|
|
|
set |
|
|
|
|
{ |
|
|
|
|
_agileJsonConfigEntities = value; |
|
|
|
|
OnPropertyChanged(nameof(AgileJsonConfigEntities)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public List<string> JsonKeysBak = new List<string>(); |
|
|
|
|
|
|
|
|
|
public List<Param> ModeList { get; set; } |
|
|
|
|
public List<string> _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<AgileJsonConfigEntity>(); |
|
|
|
|
AgileJsonConfigEntities.Add(new AgileJsonConfigEntity(this, JsonKeysBak) { |
|
|
|
|
Mode = 0, |
|
|
|
|
Spec = "p8-p8", |
|
|
|
|
_jsonKeysBak = GetNestedKeys(JObject.Parse(AlgorithmConfigJson)); |
|
|
|
|
InitAgileJsonConfig(); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void InitAgileJsonConfig() |
|
|
|
|
{ |
|
|
|
|
AgileJsonConfigEntities = new ObservableCollection<AgileJsonConfigEntity>(); |
|
|
|
|
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<AgileJsonConfigEntity>(); |
|
|
|
|
var insertSql = new List<string>(); |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 初始化算法数据 |
|
|
|
@ -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,32 +173,10 @@ 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<int, List<string>> SpecOptions = new() |
|
|
|
|
{ |
|
|
|
|
[0] = new List<string> { "p8-p8" }, |
|
|
|
|
[1] = new List<string> { |
|
|
|
|
"p8-p8", "p8-p8-s1", "p8-p8-s2", |
|
|
|
|
"p8-p8-s3", "p8-p8-s4" |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
public List<string> GetSpecOptions(int mode) |
|
|
|
|
{ |
|
|
|
|
return SpecOptions.TryGetValue(mode, out var options) |
|
|
|
|
? options |
|
|
|
|
: new List<string>(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private List<string> GetNestedKeys(JToken token, string prefix = "") |
|
|
|
|
{ |
|
|
|
|
var keys = new List<string>(); |
|
|
|
@ -177,8 +193,6 @@ public class AlgorithmConfigVM : BaseViewModel |
|
|
|
|
} |
|
|
|
|
return keys; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// 保存数据 |
|
|
|
|
/// </summary> |
|
|
|
@ -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<Param> GetRunModelList() |
|
|
|
|
{ |
|
|
|
|
return new List<Param> |
|
|
|
|
{ |
|
|
|
|
new Param() |
|
|
|
|
{ |
|
|
|
|
Value = "0", |
|
|
|
|
Name = "实验室模式" |
|
|
|
|
}, |
|
|
|
|
new Param() |
|
|
|
|
{ |
|
|
|
|
Value = "1", |
|
|
|
|
Name = "工厂模式" |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public class Param |
|
|
|
|
{ |
|
|
|
|
public string Value { get; set; } |
|
|
|
|
public string Name { get; set; } |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|