using HMI.Core.Entity;
|
using HMI.Core.Manager;
|
using HMI.Core.Util;
|
using HMI.LogUtil;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.IO;
|
using System.Linq;
|
using System.Text.RegularExpressions;
|
|
namespace HMI.ParameterManage.Model
|
{
|
public class XmlModel : NotifyBase
|
{
|
public XmlModel(string baseFolder)
|
{
|
}
|
|
private ObservableCollection<ProductData> products;
|
|
public ObservableCollection<ProductData> Products
|
{
|
get { return products; }
|
set { Set(ref products, value); }
|
}
|
|
private ObservableCollection<ParameterData> parameters;
|
|
public ObservableCollection<ParameterData> Parameters
|
{
|
get { return parameters; }
|
set { Set(ref parameters, value); }
|
}
|
|
private ProductData selectedProduct;
|
|
public ProductData SelectedProduct
|
{
|
get { return selectedProduct; }
|
set
|
{
|
Set(ref selectedProduct, value);
|
LoadParameters();
|
}
|
}
|
|
private ParameterData selectedParameter;
|
|
public ParameterData SelectedParameter
|
{
|
get { return selectedParameter; }
|
set
|
{
|
Set(ref selectedParameter, value);
|
}
|
}
|
|
public List<string> groups { get; private set; }
|
|
public bool ValidateProductName(object value, out string msg)
|
{
|
msg = null;
|
string name = value.ToString();
|
if (Products == null) return true;
|
var temp = Products.FirstOrDefault(p => p.Name == name);
|
if (temp != null && temp != SelectedProduct)
|
{
|
msg = this.lang("App.MustBeUnique", this.lang("Pm.Product.Name"));
|
return false;
|
}
|
return true;
|
}
|
|
public bool ValidateParameterName(object value, out string msg)
|
{
|
string pattern = @"^[\p{L}_][\p{L}\p{N}_]*$";
|
msg = null;
|
string name = value.ToString();
|
|
if (!Regex.IsMatch(name, pattern))
|
{
|
msg = "参数名不能以数字开头";
|
return false;
|
}
|
|
if (Products == null) return true;
|
var temp = Parameters.FirstOrDefault(p => p.Name == name);
|
if (temp != null && temp != SelectedParameter)
|
{
|
msg = this.lang("App.MustBeUnique", this.lang("Pm.Product.Name"));
|
return false;
|
}
|
return true;
|
}
|
|
public void LoadProducts()
|
{
|
ObservableCollection<ProductData> products = ProductManager.GetProducts<ObservableCollection<ProductData>>();
|
products = products ?? new ObservableCollection<ProductData>();
|
Products = products;
|
}
|
|
public void LoadParameters()
|
{
|
ObservableCollection<ParameterData> parameters = null;
|
if (SelectedProduct != null)
|
{
|
parameters = ProductManager.GetParametersByProductName<ObservableCollection<ParameterData>>(SelectedProduct.Name);
|
}
|
parameters = parameters ?? new ObservableCollection<ParameterData>();
|
Parameters = parameters;
|
groups = Parameters.Where(p => !string.IsNullOrEmpty(p.PGroup)).Select(p => p.PGroup).Distinct().ToList();
|
}
|
|
public void SaveOrUpdateData(ProductData data)
|
{
|
int index = Products.IndexOf(data);
|
if (index > -1)
|
{
|
UpdateProduct(index, data);
|
}
|
else
|
{
|
InsertProduct(data);
|
}
|
}
|
|
public void SaveOrUpdateParameter(ParameterData data)
|
{
|
int index = Parameters.IndexOf(data);
|
if (index > -1)
|
{
|
UpdateParameter(index, data);
|
}
|
else
|
{
|
InsertParameter(data);
|
LogHelper.Info("HMI", $"[{data.ProductName}] Add New Parameter {data.Name}. Value: {data.Value}", out _);
|
}
|
}
|
|
#region 产品操作
|
/// <summary>
|
/// 新增产品
|
/// </summary>
|
/// <param name="data"></param>
|
public void InsertProduct(ProductData data)
|
{
|
var temp = XmlHelper.DeepClone(Products);
|
temp.Add(data);
|
SaveProduct(temp);
|
if (!string.IsNullOrEmpty(data.ParentId))
|
{
|
var parameters =
|
ProductManager.GetParametersByProductName<List<ParameterData>>(data.ParentId);
|
foreach (var p in parameters)
|
{
|
p.ProductName = data.Name;
|
}
|
SaveParameter(data.Name, parameters);
|
}
|
}
|
|
/// <summary>
|
/// 更新产品
|
/// </summary>
|
/// <param name="index"></param>
|
/// <param name="data"></param>
|
public void UpdateProduct(int index, ProductData data)
|
{
|
var temp = XmlHelper.DeepClone(Products);
|
temp[index] = data;
|
SaveProduct(temp);
|
}
|
|
/// <summary>
|
/// 删除产品
|
/// </summary>
|
/// <param name="data"></param>
|
public void DeleteProduct(ProductData data)
|
{
|
int index = Products.IndexOf(data);
|
if (index > -1)
|
{
|
var temp = Products.Where(p => p != data).ToList();
|
SaveProduct(temp);
|
}
|
}
|
|
/// <summary>
|
/// 批量删除产品
|
/// </summary>
|
public void DeleteProducts()
|
{
|
var temp = Products.Where(p => !p.IsSelected).ToList();
|
SaveProduct(temp);
|
}
|
|
/// <summary>
|
/// 持久化产品数据
|
/// </summary>
|
/// <param name="datas"></param>
|
private void SaveProduct(IList<ProductData> datas)
|
{
|
XmlHelper.SerializeToXml(ProductManager.GetDataFilePath(), datas);
|
}
|
#endregion
|
|
#region 产品参数操作
|
/// <summary>
|
/// 新增参数
|
/// </summary>
|
/// <param name="data"></param>
|
public void InsertParameter(ParameterData data)
|
{
|
var temp = XmlHelper.DeepClone(Parameters);
|
temp.Add(data);
|
SaveParameter(temp);
|
}
|
|
/// <summary>
|
/// 更新参数
|
/// </summary>
|
/// <param name="index"></param>
|
/// <param name="data"></param>
|
public void UpdateParameter(int index, ParameterData data)
|
{
|
var temp = XmlHelper.DeepClone(Parameters);
|
var old = temp[index];
|
temp[index] = data;
|
SaveParameter(temp);
|
if (old.Value != data.Value)
|
{
|
LogHelper.Info("HMI", $"Update Parameter {data.Name}. Value: {old.Value} => {data.Value}", out _);
|
}
|
}
|
|
/// <summary>
|
/// 删除参数
|
/// </summary>
|
/// <param name="data"></param>
|
public void DeleteParameter(ParameterData data)
|
{
|
int index = Parameters.IndexOf(data);
|
if (index > -1)
|
{
|
var temp = Parameters.Where(p => p != data).ToList();
|
SaveParameter(temp);
|
LoggerHelper.Info("Product Manage", $"User: {UserInfo.User.Name} deleted Product parameter:{data.Name} in Product:{data.ProductName}");
|
}
|
}
|
|
/// <summary>
|
/// 批量删除参数
|
/// </summary>
|
public void DeleteParameters()
|
{
|
var needDelet = Parameters.Where(p => p.IsSelected).ToList();
|
if (needDelet.Count > 0)
|
{
|
var first = needDelet[0];
|
var names = needDelet.Select(e => e.Name).ToList();
|
|
var temp = Parameters.Where(p => !p.IsSelected).ToList();
|
SaveParameter(temp);
|
LoggerHelper.Info("Product Manage", $"User: {UserInfo.User.Name} deleted Product parameters:{string.Join(",", names)} in Product:{first.ProductName}");
|
}
|
}
|
|
/// <summary>
|
/// 持久化参数数据
|
/// </summary>
|
/// <param name="datas"></param>
|
private void SaveParameter(IList<ParameterData> datas)
|
{
|
string fileName = ProductManager.GetParamFolderByProductName(SelectedProduct.Name);
|
XmlHelper.SerializeToXml(fileName, datas);
|
}
|
|
private void SaveParameter(string product, IList<ParameterData> datas)
|
{
|
string fileName = ProductManager.GetParamFolderByProductName(product);
|
XmlHelper.SerializeToXml(fileName, datas);
|
}
|
#endregion
|
}
|
}
|