using LogoForceTestApp.Modules.MainModule.Enums; using LogoForceTestApp.Modules.MainModule.Models; using LogoForceTestApp.Core; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System.Windows.Media; using LogoForceTestApp.Modules.MainModule.Views; using System; using HandyControl.Controls; using Repository.Entiies; using Repository; using Team.Utility; using System.Reflection; namespace LogoForceTestApp.ViewModels { public class MainWindowViewModel : BindableBase { private string _title = "LogoForceTestApp"; private readonly IRegionManager _regionManager; private readonly IEventAggregator _eventAggregator; private readonly IRepository _repository; private readonly IWritableOptions _appSettings; public string Title { get { return _title; } set { SetProperty(ref _title, value); } } private RunResultState _runResultState; public RunResultState Jugement { get { return _runResultState; } set { SetProperty(ref _runResultState, value); } } public string LoginMessage { get; set; } public DelegateCommand LogCommand { get; set; } public DelegateCommand DataCommand { get; set; } public DelegateCommand GoMainPageCommand { get; set; } public DelegateCommand LoginCommand { get; set; } public DelegateCommand SettingCommand { get; set; } public DelegateCommand OperationLogPageCommand { get; set; } public DelegateCommand Tracing { get; set; } public DelegateCommand SignInCommand { get; set; } public Brush LoinBrush { get; set; } public string LineName { get; set; } public MainWindowViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IRepository repository,IWritableOptions appSettings) { var version=Assembly.GetExecutingAssembly().GetName().Version; Title = Title+"-" + version.ToString(); DataCommand = new DelegateCommand(GoDataPage); GoMainPageCommand = new DelegateCommand(GoMainPage); LogCommand = new DelegateCommand(GoLogPage); LoginCommand = new DelegateCommand(Login); SettingCommand = new DelegateCommand(GoSettingPage); OperationLogPageCommand = new DelegateCommand(GoOperationLogPage); Tracing = new DelegateCommand(GoQualityTracing); SignInCommand = new DelegateCommand(GoSignInPage); _regionManager = regionManager; _eventAggregator = eventAggregator; _repository = repository; _appSettings = appSettings; LineName = _appSettings.Value.MachineName; if (string.IsNullOrWhiteSpace(_appSettings.Value.Process)) { _appSettings.Update(c => { c.Process = "FI_PP"; }); } if (string.IsNullOrWhiteSpace(_appSettings.Value.Builds)) { _appSettings.Update(c => { c.Builds = "RAMP"; }); } if (string.IsNullOrWhiteSpace(_appSettings.Value.Project)) { _appSettings.Update(c => { c.Project = "X1930"; }); } if (string.IsNullOrWhiteSpace(_appSettings.Value.Part)) { _appSettings.Update(c => { c.Part = "TC"; }); } if (string.IsNullOrWhiteSpace(_appSettings.Value.INSPECTION_PROGRAM_NAME)) { _appSettings.Update(c => { c.INSPECTION_PROGRAM_NAME = "FS_Color_Gloss"; }); } if (string.IsNullOrWhiteSpace(_appSettings.Value.AIM_UNIQUE)) { _appSettings.Update(c => { c.AIM_UNIQUE = "V1.0"; }); } if (string.IsNullOrWhiteSpace(_appSettings.Value.AimName)) { _appSettings.Update(c => { c.AimName = "L1-1"; }); } _eventAggregator.GetEvent().Subscribe(c => { Jugement = c; }); UpdateLoginMessage(); _eventAggregator.GetEvent().Subscribe(result => { UpdateLoginMessage(); }); } private void GoSignInPage() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = nameof(SignInPage), UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(SignInPage)); paramLog.Content = "导航成功"; _repository.Add(paramLog); } private async void GoQualityTracing() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = nameof(LoginPage), UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; try { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { paramLog.Content = "权限不足失败"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); return; } paramLog.Content = "导航成功"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(QualityTracing)); } finally { await _repository.AddAsync(paramLog); } } private async void GoOperationLogPage() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = nameof(OperationLogPage), UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; try { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { paramLog.Content = "权限不足失败"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); return; } _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(OperationLogPage)); paramLog.Content = "导航成功"; } finally { await _repository.AddAsync(paramLog); } } private async void GoSettingPage() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = "SettingPage", UserName=string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)?"游客": CurrentUser.CurrentUserName }; try { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { paramLog.Content = "无权限,跳转登录界面"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); return; } if (CurrentUser.CurrentUserName == Role.Operator.ToString()) { paramLog.Content = "权限不足"; Growl.Info($"权限不足,请切换工程师权限"); return; } _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(ParamPage)); paramLog.Content = "导航成功"; } finally { await _repository.AddAsync(paramLog); } } private void UpdateLoginMessage() { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { LoginMessage = "未登录"; LoinBrush = Brushes.Red; } else { LoginMessage = $"{CurrentUser.CurrentUserName}"; LoinBrush = Brushes.Blue; } } private void Login() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = "LoginPage", UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); paramLog.Content = "导航成功"; _repository.Add(paramLog); } private async void GoLogPage() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = "LoginPage", UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; try { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { paramLog.Content = "权限不足失败"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); return; } _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LogPage)); paramLog.Content = "导航成功"; } finally { await _repository.AddAsync(paramLog); } } private async void GoMainPage() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = "MainPage", UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; try { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { paramLog.Content = "权限不足失败"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); return; } _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(MainPage)); paramLog.Content = "导航成功"; } finally { await _repository.AddAsync(paramLog); } } private async void GoDataPage() { var paramLog = new ParamLog { Id = Guid.NewGuid(), OperateAction = OperateAction.Navigate, Occured = DateTime.UtcNow, OperateObject = nameof(LoginPage), UserName = string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName) ? "游客" : CurrentUser.CurrentUserName }; try { if (string.IsNullOrWhiteSpace(CurrentUser.CurrentUserName)) { paramLog.Content = "权限不足失败"; _regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(LoginPage)); return; } paramLog.Content = "导航成功"; _regionManager.RequestNavigate(RegionNames.ContentRegion, "DataPage"); } finally { await _repository.AddAsync(paramLog); } } } }