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().CheckPermission(ERole.Mantainer); } public DelegateCommand CellEditEndingCommand => new DelegateCommand((x) => { Regex regex = new Regex(selectedItem.CheckRegular); if (!regex.IsMatch(selectedItem.ParamValue)) { string oldValue = _unityContainer.Resolve().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 paraList = new ObservableCollection(); public ObservableCollection ParamList { get => paraList; set => SetProperty(ref paraList, value); } public DelegateCommand SaveCommand => new DelegateCommand((x) => { var sysSetupService = _unityContainer.Resolve(); 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().SetBatteryCodeLen(); _unityContainer.Resolve().AddLog($@"修改参数", E_LogType.Operate.ToString()); }); private void ShowInfo() { ParamList.Clear(); var sysSetupService = _unityContainer.Resolve(); sysSetupService.GetShowParam().ForEach(item => ParamList.Add(item)); } void INavigationAware.OnNavigatedTo(NavigationContext navigationContext) { } bool INavigationAware.IsNavigationTarget(NavigationContext navigationContext) { return false; } void INavigationAware.OnNavigatedFrom(NavigationContext navigationContext) { } } }