459 lines
19 KiB
C#
459 lines
19 KiB
C#
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 DM_Weight.msg;
|
||
using MaterialDesignThemes.Wpf;
|
||
using System.Windows.Media;
|
||
using DM_Weight.Common;
|
||
|
||
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 bool ISTakeReturn;
|
||
|
||
private int loginMode = Convert.ToInt32(CommonClass.ReadAppSetting("loginMode")?.ToString() ?? "1");
|
||
//获取可取药还药的时间段
|
||
//string[] timeSpan = CommonClass.ReadAppSetting("TakeReturnTime").Split('-');
|
||
//string weeksSpan = CommonClass.ReadAppSetting("TakeReturnWeek");
|
||
public bool MultiLogin
|
||
{
|
||
get => loginMode == 2;
|
||
}
|
||
|
||
private PremissionDm? _selectedMenu;
|
||
|
||
private PremissionDm? _selectedChildMenu;
|
||
|
||
private List<PremissionDm>? _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
|
||
{
|
||
if (value.PremissionPath.Equals("ReturnDrugWindow") || value.PremissionPath.Equals("ReturnDrugWindow2"))
|
||
{
|
||
//if (!ISTakeReturn)
|
||
//{
|
||
// return;
|
||
//}
|
||
if (ConfigurationManager.AppSettings["returnDrugMode"].ToString().Equals("1"))
|
||
{
|
||
_regionManager.RequestNavigate("ContentRegion", "ReturnDrugWindow2");
|
||
}
|
||
else
|
||
{
|
||
_regionManager.RequestNavigate("ContentRegion", "ReturnDrugWindow");
|
||
}
|
||
}
|
||
else
|
||
{
|
||
//if (value.PremissionPath.Equals("OrderTakeDrugWindow") && !ISTakeReturn)
|
||
//{
|
||
// return;
|
||
//}
|
||
//else
|
||
{
|
||
_regionManager.RequestNavigate("ContentRegion", value.PremissionPath);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
SetProperty(ref _selectedChildMenu, value);
|
||
}
|
||
}
|
||
}
|
||
|
||
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);
|
||
}
|
||
}
|
||
|
||
private DelegateCommand<ListBox> _selectionCommon;
|
||
public DelegateCommand<ListBox> SelectionCommon
|
||
{
|
||
get => _selectionCommon ?? (_selectionCommon = new DelegateCommand<ListBox>(SelectionMethod));
|
||
}
|
||
private void SelectionMethod(ListBox viewName)
|
||
{
|
||
logger.Info($"用户【{Operator?.Nickname}】进入菜单点击事件");
|
||
|
||
//CheckTakeReturnTime();
|
||
//PremissionDm premissionDm = viewName.SelectedItem as PremissionDm;
|
||
//if ((premissionDm.PremissionName == "取药" || premissionDm.PremissionName == "还药") && !ISTakeReturn)
|
||
//{
|
||
// //AlertMsg alertMsg = new AlertMsg
|
||
// //{
|
||
// // Message = $"当前时间不可操作{premissionDm.PremissionName}",
|
||
// // Type = MsgType.ERROR,
|
||
// //};
|
||
// //_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
// SnackbarMessageQueue.Enqueue($"当前时间不可操作{premissionDm.PremissionName}");
|
||
// viewName.SelectedItem = SelectedMenu;
|
||
// return;
|
||
//}
|
||
|
||
SelectedMenu = viewName.SelectedItem as PremissionDm;
|
||
if (SelectedMenu.PremissionName == "退出")
|
||
{
|
||
//logger.Info($"用户【{Operator?.Nickname}】退出登录");
|
||
logger.Info($"用户【{Operator?.Nickname}】退出登录;SelectionMethod{SelectedMenu.PremissionName}");
|
||
Operator = null;
|
||
Reviewer = null;
|
||
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
||
}
|
||
else
|
||
{
|
||
SelectedChildMenu = SelectedMenu.Children[0];
|
||
}
|
||
|
||
logger.Info($"用户【{Operator?.Nickname}】结束菜单点击事件");
|
||
}
|
||
|
||
public List<PremissionDm> 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); }
|
||
public bool KeepAlive => false;
|
||
private PortUtil _portUtil;
|
||
//IEventAggregator _eventAggregator;
|
||
public HomeWindowViewModel(IRegionManager iRegionManager, PortUtil portUtil, IDialogService dialogService, IUnityContainer container)
|
||
{
|
||
_portUtil = portUtil;
|
||
_regionManager = iRegionManager;
|
||
_dialogService = dialogService;
|
||
_container = container;
|
||
//_eventAggregator = eventAggregator;
|
||
}
|
||
|
||
public DelegateCommand<string> OpenFingerDialog
|
||
{
|
||
get => new DelegateCommand<string>((string Type) =>
|
||
{
|
||
DialogParameters dialogParameters = new DialogParameters();
|
||
dialogParameters.Add("User", Type.Equals("Operator") ? Operator : Reviewer);
|
||
DialogServiceExtensions.ShowDialogHost(_dialogService, "FingerprintDialog", dialogParameters, DoDialogResult, "RootDialog");
|
||
});
|
||
}
|
||
//退出
|
||
public DelegateCommand QuitCommand
|
||
{
|
||
get => new DelegateCommand(QuitAction);
|
||
}
|
||
private void QuitAction()
|
||
{
|
||
logger.Info($"用户【{Operator?.Nickname}】退出登录;{UserList.Nickname}");
|
||
Operator = null;
|
||
Reviewer = null;
|
||
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
||
}
|
||
|
||
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<string> OpenEditPasswordDialog
|
||
{
|
||
get => new DelegateCommand<string>((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 void FindDrawerCount()
|
||
{
|
||
int count = SqlSugarHelper.Db.Queryable<ChannelStock>().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();
|
||
|
||
Is16Drawer = count == 16;
|
||
}
|
||
|
||
//这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作
|
||
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
|
||
{
|
||
continuationCallback(true);
|
||
}
|
||
|
||
private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));
|
||
public ISnackbarMessageQueue SnackbarMessageQueue
|
||
{
|
||
get => _snackbarMessageQueue;
|
||
set => SetProperty(ref _snackbarMessageQueue, value);
|
||
}
|
||
private SolidColorBrush _colorBrush=new SolidColorBrush((Color) ColorConverter.ConvertFromString("#b71c1c"));
|
||
|
||
public SolidColorBrush SnackbarBackground
|
||
{
|
||
get => _colorBrush;
|
||
set => SetProperty(ref _colorBrush, value);
|
||
}
|
||
//接收导航传过来的参数
|
||
public void OnNavigatedTo(NavigationContext navigationContext)
|
||
{
|
||
//CheckTakeReturnTime();
|
||
////是否可操作取药还药定时
|
||
//System.Timers.Timer TakeReturnTimer = new System.Timers.Timer();
|
||
//TakeReturnTimer.Interval = 1000;
|
||
//TakeReturnTimer.Elapsed += (sender, e) =>
|
||
//{
|
||
// // 当前时间不可操作取药、还药
|
||
// if (!CheckTakeReturnTime())
|
||
// {
|
||
// if (SelectedMenu.PremissionName == "取药" || SelectedMenu.PremissionName == "还药")
|
||
// {
|
||
// SnackbarMessageQueue.Enqueue($"当前时间不可操作{SelectedMenu.PremissionName}");
|
||
// //SelectedMenu = ISTakeReturn ? UserList.Role.Permissions[0] : UserList.Role.Permissions[1];
|
||
// //SelectedChildMenu = SelectedMenu.Children[0];
|
||
// //_regionManager.RequestNavigate("ContentRegion", SelectedMenu.Children[0].PremissionPath);
|
||
// //PremissionDmList = UserList.Role.Permissions;
|
||
|
||
// List<PremissionDm> premissions = UserList.Role.Permissions;
|
||
|
||
// SelectedMenu = ISTakeReturn ? premissions[0] : premissions[1];
|
||
// SelectedChildMenu = SelectedMenu.Children[0];
|
||
// System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
|
||
// {
|
||
// _regionManager.RequestNavigate("ContentRegion", "SelfAddWindow");
|
||
// }));
|
||
// }
|
||
// }
|
||
//};
|
||
//TakeReturnTimer.Start();
|
||
|
||
//_portUtil.dateTime = DateTime.Now;
|
||
//取出user
|
||
UserList = navigationContext.Parameters.GetValue<UserList>("operator");
|
||
Operator = UserList;
|
||
logger.Info($"发药人【{Operator.Nickname}】登录");
|
||
if (navigationContext.Parameters.ContainsKey("reviewer"))
|
||
{
|
||
UserList2 = navigationContext.Parameters.GetValue<UserList>("reviewer");
|
||
Reviewer = UserList2;
|
||
logger.Info($"审核人【{Reviewer.Nickname}】登录");
|
||
}
|
||
|
||
List<PremissionDm> premissions = UserList.Role.Permissions;
|
||
|
||
//SqlSugarHelper.Db.SqlQueryable<PremissionDm>(sql)
|
||
//.ToTree(pd => pd.Children, pd => pd.ParentId, 0);
|
||
|
||
PremissionDmList = premissions;
|
||
//SelectedMenu = ISTakeReturn ? premissions[0] : premissions[1];
|
||
SelectedMenu = premissions[0];
|
||
SelectedChildMenu = SelectedMenu.Children[0];
|
||
|
||
|
||
FindDrawerCount();
|
||
|
||
int autoExit = Convert.ToInt32(ConfigurationManager.AppSettings["autoExit"] ?? "0");
|
||
if (autoExit > 0)
|
||
{
|
||
Timer timer = new Timer();
|
||
timer.Interval = 1000;
|
||
timer.Elapsed += (sender, e) =>
|
||
{
|
||
// 串口无人操作
|
||
if (!_portUtil.Operate)
|
||
{
|
||
// 30秒内无人操作鼠标键盘
|
||
if ((DateTime.Now - _portUtil.dateTime).TotalSeconds > autoExit && CheckComputerFreeState.GetLastInputTime() > autoExit)
|
||
{
|
||
logger.Info($"设备30秒内无人操作,用户【{Operator?.Nickname}】自动退出登录");
|
||
Operator = null;
|
||
Reviewer = null;
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
_regionManager.RequestNavigate("MainRegion", "LoginWindow");
|
||
timer.Stop();
|
||
});
|
||
}
|
||
}
|
||
};
|
||
timer.Start();
|
||
}
|
||
}
|
||
///// <summary>
|
||
///// 检查是否在可取药、还药操作时间段
|
||
///// </summary>
|
||
//private bool CheckTakeReturnTime()
|
||
//{
|
||
// ISTakeReturn = true;
|
||
// //查询该时间段能否取药跟还药
|
||
// DateTime dtNow = DateTime.Now;
|
||
// string weekNow = DateTime.Today.DayOfWeek.ToString("d");
|
||
// string[] timeSpan = CommonClass.ReadAppSetting("TakeReturnTime").Split('-');
|
||
// string weeksSpan = CommonClass.ReadAppSetting("TakeReturnWeek");
|
||
// if (timeSpan != null && timeSpan.Length == 2&& weeksSpan.IndexOf(weekNow)>=0)
|
||
// {
|
||
// bool bStart = DateTime.TryParse(timeSpan[0], out DateTime startTime);
|
||
// bool bEnd = DateTime.TryParse(timeSpan[1], out DateTime endTime);
|
||
// if (bStart && bEnd)
|
||
// {
|
||
// if (endTime < startTime)
|
||
// {
|
||
// endTime = endTime.AddDays(1);
|
||
// }
|
||
// if (dtNow >= startTime && dtNow <= endTime)
|
||
// {
|
||
// ISTakeReturn = true;
|
||
// }
|
||
// else
|
||
// {
|
||
// ISTakeReturn = false;
|
||
// //AlertMsg alertMsg = new AlertMsg
|
||
// //{
|
||
// // Message = $"当前时间不可操作{SelectedMenu.PremissionName}",
|
||
// // Type = MsgType.ERROR,
|
||
// //};
|
||
// //_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
||
// //return;
|
||
// }
|
||
// }
|
||
// }
|
||
// return ISTakeReturn;
|
||
//}
|
||
//每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
|
||
public bool IsNavigationTarget(NavigationContext navigationContext)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//这个方法用于拦截请求
|
||
public void OnNavigatedFrom(NavigationContext navigationContext)
|
||
{
|
||
|
||
}
|
||
}
|
||
}
|