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.
172 lines
6.1 KiB
172 lines
6.1 KiB
using HandyControl.Controls; |
|
using System.Data; |
|
using System.Windows.Controls; |
|
using System.Windows.Input; |
|
using SparkClient.Model.Entity; |
|
using SparkClient.Model.Helper; |
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip; |
|
using System.Text.RegularExpressions; |
|
using Microsoft.Data.Sqlite; |
|
using SparkClient.ViewModel.Configuration.SettingsPages; |
|
|
|
namespace SparkClient.ViewModel.Configuration; |
|
|
|
public class CutConfigVM: BaseViewModel |
|
{ |
|
public ICommand AddRowsCommand { get; } |
|
public ICommand SaveCutConfigDataCommand { get; } |
|
public ICommand DelCutRowCommand { get; } |
|
|
|
private bool _isEnabled; |
|
public bool IsEnabled { get { return _isEnabled; } set { _isEnabled = value; OnPropertyChanged(nameof(IsEnabled)); } } |
|
public DataTable _cutterInfos; |
|
public DataTable CutterInfos { get { return _cutterInfos; } set { _cutterInfos = value; OnPropertyChanged("CutterInfos"); } } |
|
|
|
private int RowNo = 0; |
|
public CutConfigVM() |
|
{ |
|
AddRowsCommand = new RelayCommand(AddRows); |
|
|
|
SaveCutConfigDataCommand = new RelayCommand(SaveCutConfigData); |
|
|
|
DelCutRowCommand = new RelayCommand(DelCutRow); |
|
// 初始化VIEW层参数 |
|
// 初始化VIEW层Command |
|
this.InitCutConfigData(null); |
|
IsEnabledByRole(); |
|
} |
|
|
|
|
|
/// <summary> |
|
/// 初始化切工仪数据 |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void InitCutConfigData(object param) |
|
{ |
|
string sql = @"SELECT ITEM_NAME as ItemName , KEY AS Key, VALUE AS Value, GUID FROM CUTTER_CONFIG"; |
|
DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); |
|
if (dataTable!=null) { |
|
dataTable.Columns.Add(new DataColumn("RowNo", typeof(int))); |
|
foreach (DataRow row in dataTable.Rows) |
|
{ |
|
row["RowNo"] = RowNo++; |
|
} |
|
CutterInfos = dataTable; |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// 添加行 |
|
/// </summary> |
|
/// <param name="row">行数</param> |
|
public void AddRows(object row) |
|
{ |
|
CutterInfos.Rows.Add("", "", "",Guid.NewGuid(), RowNo++); |
|
} |
|
|
|
/// <summary> |
|
/// 保存数据 |
|
/// </summary> |
|
/// <param name="param"></param> |
|
public void SaveCutConfigData(object param) |
|
{ |
|
int count = CutterInfos.AsEnumerable().Where(r => r["Key"].ToString() != "").GroupBy(r => new { key = r.Field<string>("Key") }).Count(); |
|
if (count < CutterInfos.AsEnumerable().Where(r => r["Key"].ToString() != "").Count()) |
|
{ |
|
Growl.Error("Key有重复数据"); |
|
return; |
|
} |
|
string sql = @"SELECT ITEM_NAME as ItemName , KEY AS Key, VALUE AS Value, GUID FROM CUTTER_CONFIG"; |
|
DataTable dataTable = DataBaseHelper.ExecuteQuery(sql); |
|
int insertCount = 0; |
|
foreach (DataRow row in CutterInfos.Rows) |
|
{ |
|
if (row["Key"].ToString() == "") |
|
{ |
|
continue; |
|
} |
|
DataRow? removerow = dataTable.AsEnumerable().Where(r => r["GUID"].ToString() == row["GUID"].ToString()).FirstOrDefault(); |
|
if (removerow != null) |
|
{ |
|
CutterConfigEntity entity = new CutterConfigEntity() |
|
{ |
|
ItemName = row["ItemName"].ToString() ?? "", |
|
Key = row["Key"].ToString() ?? "", |
|
Value = row["Value"].ToString() ?? "", |
|
GUID = Guid.Parse(row["GUID"].ToString()??"") |
|
}; |
|
sql = entity.GenerateUpdateSQL(); |
|
SqliteParameter[] sqliteParameters = { |
|
new SqliteParameter("@ITEM_NAME", row["ItemName"].ToString()), |
|
new SqliteParameter("@KEY", row["Key"].ToString()), |
|
new SqliteParameter("@VALUE", row["Value"].ToString()), |
|
new SqliteParameter("@GUID", row["GUID"].ToString()), |
|
}; |
|
insertCount += DataBaseHelper.ExecuteNonQuery(sql, sqliteParameters); |
|
dataTable.Rows.Remove(removerow); |
|
} |
|
else |
|
{ |
|
CutterConfigEntity entity = new CutterConfigEntity() |
|
{ |
|
ItemName = row["ItemName"].ToString() ?? "", |
|
Key = row["Key"].ToString() ?? "", |
|
Value = row["Value"].ToString() ?? "" |
|
}; |
|
sql = entity.GenerateInsertSQL(); |
|
SqliteParameter[] sqliteParameters = { |
|
new SqliteParameter("@ItemName", row["ItemName"].ToString()), |
|
new SqliteParameter("@Key", row["Key"].ToString()), |
|
new SqliteParameter("@Value", row["Value"].ToString()), |
|
new SqliteParameter("@GUID", row["GUID"].ToString()), |
|
}; |
|
insertCount += DataBaseHelper.ExecuteNonQuery(sql, sqliteParameters); |
|
} |
|
} |
|
foreach (DataRow row in dataTable.Rows) |
|
{ |
|
Guid.TryParse(row["GUID"].ToString(), out Guid result); |
|
CutterConfigEntity entity = new CutterConfigEntity() |
|
{ |
|
ItemName = row["ItemName"].ToString() ?? "", |
|
Key = row["Key"].ToString() ?? "", |
|
Value = row["Value"].ToString() ?? "", |
|
GUID = result |
|
}; |
|
sql = entity.GenerateDeleteSQL(); |
|
insertCount += DataBaseHelper.ExecuteNonQuery(sql); |
|
} |
|
if (insertCount>=0) |
|
{ |
|
Growl.Info("保存成功"); |
|
} |
|
else |
|
{ |
|
Growl.Error("保存失败"); |
|
} |
|
} |
|
private void IsEnabledByRole() |
|
{ |
|
string PERMISSIONS = Settings.SelectValueById("PERMISSIONS"); |
|
if ("admin".Equals(PERMISSIONS)) |
|
{ |
|
IsEnabled = true; |
|
} |
|
else |
|
{ |
|
IsEnabled = false; |
|
} |
|
} |
|
/// <summary> |
|
/// 删除一行数据 |
|
/// </summary> |
|
/// <param name="row">行</param> |
|
public void DelCutRow(object row) |
|
{ |
|
DataRowView? a = row as DataRowView; |
|
if (a != null) |
|
{ |
|
CutterInfos.Rows.Remove(a.Row); |
|
} |
|
} |
|
} |