Files
6098/Cowain.Bake.UI/FactoryMaintenance/ViewModels/SysSetupViewModel.cs

103 lines
3.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
using Cowain.Bake.Model;
using System.Collections.ObjectModel;
using Cowain.Bake.BLL;
using System.Text.RegularExpressions;
using Prism.Mvvm;
using Prism.Commands;
using Cowain.Bake.Common.Core;
using Cowain.Bake.Common.Enums;
using Prism.Regions;
using Cowain.Bake.Common.Interface;
namespace Cowain.Bake.UI.FactoryMaintenance.ViewModels
{
public class SysSetupViewModel : BindableBase, INavigationAware
{
private readonly IUnityContainer _unityContainer;
private bool isAllowed;
public bool IsAllowed
{
get => isAllowed;
set => SetProperty(ref isAllowed, value);
}
private TSysSetup selectedItem;
public TSysSetup SelectedItem
{
get => selectedItem;
set => SetProperty(ref selectedItem, value);
}
public SysSetupViewModel(IUnityContainer unityContainer)
{
_unityContainer = unityContainer;
ShowInfo();
isAllowed = unityContainer.Resolve<UserService>().CheckPermission(ERole.Mantainer);
}
public DelegateCommand<object> CellEditEndingCommand => new DelegateCommand<object>((x) =>
{
Regex regex = new Regex(selectedItem.CheckRegular);
if (!regex.IsMatch(selectedItem.ParamValue))
{
string oldValue = _unityContainer.Resolve<SysSetupService>().GetValueByID(selectedItem.Id);
LogHelper.Instance.Error(selectedItem.ParamName + "的值:" + selectedItem.ParamValue + ",不正确,已回滚!", true);
var findSelect = ParamList.Where(item => item.Id == selectedItem.Id).FirstOrDefault();
findSelect.ParamValue = oldValue;
}
});
public ObservableCollection<TSysSetup> paraList = new ObservableCollection<TSysSetup>();
public ObservableCollection<TSysSetup> ParamList
{
get => paraList;
set => SetProperty(ref paraList, value);
}
public DelegateCommand<object> SaveCommand => new DelegateCommand<object>((x) =>
{
var sysSetupService = _unityContainer.Resolve<SysSetupService>();
foreach (var item in ParamList)
{
Regex reg = new Regex(item.CheckRegular);
if (reg.IsMatch(item.ParamValue))
{
sysSetupService.UpdateValue(item.Id , item.ParamValue);
}
else
{
LogHelper.Instance.Error(item.ParamName + "格式不正确,不保存。", true);
}
}
ShowInfo();
HandyControl.Controls.Growl.Success("保存成功");
_unityContainer.Resolve<ICommonFun>().SetBatteryCodeLen();
_unityContainer.Resolve<LogService>().AddLog($@"修改参数", E_LogType.Operate.ToString());
});
private void ShowInfo()
{
ParamList.Clear();
var sysSetupService = _unityContainer.Resolve<SysSetupService>();
sysSetupService.GetShowParam().ForEach(item => ParamList.Add(item));
}
void INavigationAware.OnNavigatedTo(NavigationContext navigationContext)
{
}
bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext)
{
return false;
}
void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext)
{
}
}
}