using log4net; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using DM_Weight.Models; using DM_Weight.Port; using DM_Weight.util; using DM_Weight.Views; using System.Timers; using Unity; using System.Windows.Threading; using Newtonsoft.Json.Linq; using System.Threading; using DM_Weight.msg; using log4net.Core; using DM_Weight.Services; namespace DM_Weight.ViewModels { public class HomeWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime { private readonly ILog logger = LogManager.GetLogger(typeof(HomeWindowViewModel)); private readonly IDialogService _dialogService; private UserList? _userList; private UserList? _userList2; private int loginMode = Convert.ToInt32(ConfigurationManager.AppSettings["loginMode"]?.ToString() ?? "1"); public bool MultiLogin { get => loginMode == 2; } private PremissionDm? _selectedMenu; private PremissionDm? _selectedChildMenu; private List? _premissionDmList; public PremissionDm? SelectedChildMenu { get { return _selectedChildMenu; } set { //if (!_portUtil.Operate) //{ //if (value != null) //{ // if (value.PremissionPath.Equals("TakeRecordWindow")) // { // //定义传参变量 // NavigationParameters keys = new NavigationParameters(); // //添加参数,键值对格式 // keys.Add("Type", 2); // _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); // } // else if (value.PremissionPath.Equals("AddRecordWindow")) // { // //定义传参变量 // NavigationParameters keys = new NavigationParameters(); // //添加参数,键值对格式 // keys.Add("Type", 1); // _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); // } // else if (value.PremissionPath.Equals("ReturnRecordWindow")) // { // //定义传参变量 // NavigationParameters keys = new NavigationParameters(); // //添加参数,键值对格式 // keys.Add("Type", 3); // _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); // } // else if (value.PremissionPath.Equals("RetrunEmptyRecordWindow")) // { // //定义传参变量 // NavigationParameters keys = new NavigationParameters(); // //添加参数,键值对格式 // keys.Add("Type", 3); // _regionManager.RequestNavigate("ContentRegion", value.PremissionPath, keys); // } // else if (value.PremissionPath.Equals("CheckRecordWindow")) // { // //定义传参变量 // NavigationParameters keys = new NavigationParameters(); // //添加参数,键值对格式 // keys.Add("Type", 4); // _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); // } // else // { // _regionManager.RequestNavigate("ContentRegion", value.PremissionPath); // } //} SetProperty(ref _selectedChildMenu, value); //} } } private void SelectChildNavigate(PremissionDm SelectedChildMenu) { if (!_portUtil.Operate) { if (SelectedChildMenu != null) { if (SelectedChildMenu.PremissionPath.Equals("TakeRecordWindow")) { //定义传参变量 NavigationParameters keys = new NavigationParameters(); //添加参数,键值对格式 keys.Add("Type", 2); _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); } else if (SelectedChildMenu.PremissionPath.Equals("AddRecordWindow")) { //定义传参变量 NavigationParameters keys = new NavigationParameters(); //添加参数,键值对格式 keys.Add("Type", 1); _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); } else if (SelectedChildMenu.PremissionPath.Equals("ReturnRecordWindow")) { //定义传参变量 NavigationParameters keys = new NavigationParameters(); //添加参数,键值对格式 keys.Add("Type", 3); _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); } else if (SelectedChildMenu.PremissionPath.Equals("RetrunEmptyRecordWindow")) { //定义传参变量 NavigationParameters keys = new NavigationParameters(); //添加参数,键值对格式 keys.Add("Type", 3); _regionManager.RequestNavigate("ContentRegion", SelectedChildMenu.PremissionPath, keys); } else if (SelectedChildMenu.PremissionPath.Equals("CheckRecordWindow")) { //定义传参变量 NavigationParameters keys = new NavigationParameters(); //添加参数,键值对格式 keys.Add("Type", 4); _regionManager.RequestNavigate("ContentRegion", "MachineRecordWindow", keys); } else { _regionManager.RequestNavigate("ContentRegion", SelectedChildMenu.PremissionPath); } } } } public PremissionDm? SelectedMenu { get { return _selectedMenu; } set { //if (value != null) //{ // if (value.PremissionName == "退出") // { // logger.Info($"用户【{Operator?.Nickname}】退出登录"); // Operator = null; // Reviewer = null; // _regionManager.RequestNavigate("MainRegion", "LoginWindow"); // } // else // { // SelectedChildMenu = value.Children[0]; // } //} SetProperty(ref _selectedMenu, value); } } #region 父菜单点击 private DelegateCommand _selectionCommon; public DelegateCommand SelectionCommon { get => _selectionCommon ?? (_selectionCommon = new DelegateCommand(SelectionMethod)); } private void SelectionMethod() { logger.Info("开始进入父菜单"); //SelectedMenu = viewName.SelectedItem as PremissionDm; if (SelectedMenu!=null&&SelectedMenu.PremissionName == "退出") { logger.Info($"用户【{Operator?.Nickname}】退出登录"); Operator = null; Reviewer = null; _regionManager.RequestNavigate("MainRegion", "LoginWindow"); } else { //SelectedMenu.Children = SelectedMenu.Children; //SelectedChildMenu = SelectedMenu.Children[0]; if (!_portUtil.Operate) { _regionManager.RequestNavigate("ContentRegion", SelectedMenu.Children[0].PremissionPath); } } logger.Info("结束父菜单"); } #endregion #region 子菜单点击 private DelegateCommand _selectionChildCommon; public DelegateCommand SelectionChildCommon { get => _selectionChildCommon ?? (_selectionChildCommon = new DelegateCommand(SelectionChildMethod)); } private void SelectionChildMethod() { SelectChildNavigate(SelectedChildMenu); } #endregion public List PremissionDmList { get { return _premissionDmList; } set { SetProperty(ref _premissionDmList, value); } } public UserList UserList { get { return _userList; } set { SetProperty(ref _userList, value); } } public UserList UserList2 { get { return _userList2; } set { SetProperty(ref _userList2, value); } } public static UserList? Operator; public static UserList? Reviewer; IRegionManager _regionManager; IUnityContainer _container; private bool _is16Drawer; public bool Is16Drawer { get => _is16Drawer; set => SetProperty(ref _is16Drawer, value); } private PortUtil _portUtil; //private SqlSugarScope SqlSugarHelper.Db; IEventAggregator _eventAggregator; SqlHelperService _sqlHelperService; public HomeWindowViewModel(IRegionManager iRegionManager, PortUtil portUtil, IDialogService dialogService, IUnityContainer container, SqlSugarScope sqlSugarScope, IEventAggregator eventAggregator,SqlHelperService sqlHelperService) { _portUtil = portUtil; _regionManager = iRegionManager; _dialogService = dialogService; _container = container; //this.SqlSugarHelper.Db = sqlSugarScope; this._eventAggregator = eventAggregator; _sqlHelperService= sqlHelperService; } public DelegateCommand OpenFingerDialog { get => new DelegateCommand((string Type) => { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("User", Type.Equals("Operator") ? Operator : Reviewer); DialogServiceExtensions.ShowDialogHost(_dialogService, "FingerprintDialog", dialogParameters, DoDialogResult, "RootDialog"); }); } public DelegateCommand OpenRecoverCommand { get => new DelegateCommand(async () => { try { _portUtil.WindowName = "HomeWindow"; _portUtil.Operate = true; await _portUtil.OpenStorage(); _portUtil.ResetData(); } catch (Exception ex) { logger.Info($"OpenRecoverCommand异常{ex.Message}"); } }); } public DelegateCommand OpenEditPasswordDialog { get => new DelegateCommand((string Type) => { DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("EditPass", true); dialogParameters.Add("User", Type.Equals("Operator") ? Operator : Reviewer); DialogServiceExtensions.ShowDialogHost(_dialogService, "EditUserDialog", dialogParameters, DoDialogResult, "RootDialog"); }); } private void DoDialogResult(IDialogResult dialogResult) { // 委托 被动执行 被子窗口执行 // dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态 if (dialogResult.Result == ButtonResult.OK) { } } //交接班 public DelegateCommand ShiftsCommand { get => new DelegateCommand(ShiftsAction); } public bool KeepAlive => false; private void ShiftsAction() { _regionManager.RequestNavigate("ContentRegion", "ChangeShiftsWindow"); } public void FindDrawerCount() { //int count = SqlSugarHelper.Db.Queryable().Where(cs => cs.DrawerType != 3) // .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count(); int count = _sqlHelperService.FindDrawerCount(); Is16Drawer = count == 16; } //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } //接收导航传过来的参数 public void OnNavigatedTo(NavigationContext navigationContext) { _portUtil.dateTime = DateTime.Now; //取出user UserList = navigationContext.Parameters.GetValue("operator"); Operator = UserList; logger.Info($"发药人【{Operator.Nickname}】登录"); if (navigationContext.Parameters.ContainsKey("reviewer")) { UserList2 = navigationContext.Parameters.GetValue("reviewer"); Reviewer = UserList2; logger.Info($"审核人【{Reviewer.Nickname}】登录"); } List premissions = UserList.Role.Permissions; if (premissions.Count <= 0) { Operator = null; Reviewer = null; Application.Current.Dispatcher.Invoke(() => { _regionManager.RequestNavigate("MainRegion", "LoginWindow"); }); AlertMsg alertMsg = new AlertMsg { Message = "用户还未设置权限,请联系管理员", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); return; } //SqlSugarHelper.Db.SqlQueryable(sql) //.ToTree(pd => pd.Children, pd => pd.ParentId, 0); PremissionDmList = premissions; SelectedMenu = premissions[0]; SelectedChildMenu = premissions[0].Children[0]; _regionManager.RequestNavigate("ContentRegion", premissions[0].Children[0].PremissionPath); //FindDrawerCount(); int autoExit = Convert.ToInt32(ConfigurationManager.AppSettings["autoExit"] ?? "0"); if (autoExit > 0) { System.Timers.Timer timer = new System.Timers.Timer(); timer.Interval = 1000; timer.Elapsed += (sender, e) => { // 串口无人操作 if (!_portUtil.Operate) { // 30秒内无人操作鼠标键盘 if ((DateTime.Now - _portUtil.dateTime).TotalSeconds > autoExit && CheckComputerFreeState.GetLastInputTime() > autoExit) { if (Operator is null) { timer.Stop(); } else { logger.Info($"设备{autoExit}秒内无人操作,用户【{Operator?.Nickname}】自动退出登录"); Operator = null; Reviewer = null; timer.Stop(); Application.Current.Dispatcher.Invoke(() => { _regionManager.RequestNavigate("MainRegion", "LoginWindow"); timer.Stop(); }); } } } }; timer.Start(); } } //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建 public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } //这个方法用于拦截请求 public void OnNavigatedFrom(NavigationContext navigationContext) { } //取药 private DelegateCommand _takeCommand; public DelegateCommand TakeCommand { get => new DelegateCommand(() => { TakeMethod(); }); } //=>_takeCommand??= new DelegateCommand(TakeMethod); private void TakeMethod() { SelectedMenu = PremissionDmList[0]; if (SelectedMenu.PremissionName == "退出") { logger.Info($"用户【{Operator?.Nickname}】退出登录"); Operator = null; Reviewer = null; _regionManager.RequestNavigate("MainRegion", "LoginWindow"); } else { //SelectedMenu.Children = SelectedMenu.Children; SelectedChildMenu = SelectedMenu.Children[0]; } } //加药 private DelegateCommand _addCommand; public DelegateCommand AddCommand => _addCommand ??= new DelegateCommand(AddMethod); private void AddMethod() { SelectedMenu = PremissionDmList[1]; if (SelectedMenu.PremissionName == "退出") { logger.Info($"用户【{Operator?.Nickname}】退出登录"); Operator = null; Reviewer = null; _regionManager.RequestNavigate("MainRegion", "LoginWindow"); } else { //SelectedMenu.Children = SelectedMenu.Children; SelectedChildMenu = SelectedMenu.Children[0]; } } //还药 private DelegateCommand _returnCommand; public DelegateCommand ReturnCommand => _returnCommand ??= new DelegateCommand(ReturnMethod); private void ReturnMethod() { SelectedMenu = PremissionDmList[2]; if (SelectedMenu.PremissionName == "退出") { logger.Info($"用户【{Operator?.Nickname}】退出登录"); Operator = null; Reviewer = null; _regionManager.RequestNavigate("MainRegion", "LoginWindow"); } else { //SelectedMenu.Children = SelectedMenu.Children; SelectedChildMenu = SelectedMenu.Children[0]; } } private DelegateCommand _stockCommand; public DelegateCommand StockCommand => _stockCommand ??= new DelegateCommand(StockMethod); private void StockMethod() { SelectedMenu = PremissionDmList[3]; if (SelectedMenu.PremissionName == "退出") { logger.Info($"用户【{Operator?.Nickname}】退出登录"); Operator = null; Reviewer = null; _regionManager.RequestNavigate("MainRegion", "LoginWindow"); } else { //SelectedMenu.Children = SelectedMenu.Children; SelectedChildMenu = SelectedMenu.Children[0]; } } private DelegateCommand _settingCommand; public DelegateCommand SettingCommand => _settingCommand ??= new DelegateCommand(SettingMethod); private void SettingMethod() { SelectedMenu = PremissionDmList[4]; if (SelectedMenu.PremissionName == "退出") { logger.Info($"用户【{Operator?.Nickname}】退出登录"); Operator = null; Reviewer = null; _regionManager.RequestNavigate("MainRegion", "LoginWindow"); } else { //SelectedMenu.Children = SelectedMenu.Children; SelectedChildMenu = SelectedMenu.Children[0]; } } } }