using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using log4net.Repository.Hierarchy; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Threading; namespace DM_Weight.ViewModels { public class OpenBoxWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime { private int _drawerNo = -1; public int DrawerNo { get => _drawerNo; set => SetProperty(ref _drawerNo, value); } private int status = 0; public int Status { get => status; set => SetProperty(ref status, value); } //开名下药箱按钮的显示状态 private bool _selfEnable = true; public bool SelfEnable { get => _selfEnable; set => SetProperty(ref _selfEnable, value); } //开公共药箱按钮的显示状态 private bool _publicEnable = true; public bool PublicEnable { get => _publicEnable; set => SetProperty(ref _publicEnable, value); } //公共药箱状态 private int _publicStatus = 0; public int PublicStatus { get => _publicStatus;set=>SetProperty(ref _publicStatus, value); } //名下药箱状态 private int _selfStatus = 0; public int SelfStatus { get => _publicStatus;set=>SetProperty(ref _publicStatus,value); } //抽屉号列表 public static List iList = new List(); //第几个抽屉号 public static int iNumber = 1; private PortUtil _portUtil; IEventAggregator _eventAggregator; public OpenBoxWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator) { _portUtil = portUtil; _eventAggregator = eventAggregator; } public DelegateCommand OpenBoxDelegate { get => new DelegateCommand((DrawerNo) => { this.DrawerNo = Convert.ToInt32(DrawerNo); switch (this.DrawerNo) { case 0: PublicEnable = false; PublicStatus=1; break; case 1: SelfEnable = false; SelfStatus = 1; break; } SearchBox(); } ); } private void SearchBox() { iList = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == "DM5" && cl.DrawerType == this.DrawerNo) .WhereIF(this.DrawerNo==0,cl=> cl.BelongUser == HomeWindowViewModel.Operator.UserName) .Select(cl => cl.DrawerNo).ToList(); if (iList.Count > 0) { _portUtil.SpeakAsync("正在打开药箱"); _portUtil.DrawerNo = iList[iNumber]; iNumber++; _portUtil.OpenBox(); } } void DoMyPrismEvent(DeviceMsg msg) { switch (msg.EventType) { // 药箱打开 case EventType.DRAWEROPEN: //记录开药箱日志 SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = "DM5", DrawerNo = _portUtil.DrawerNo, Operator = HomeWindowViewModel.Operator?.Id, OperationTime = DateTime.Now, Type = 55, InvoiceId = "药箱打开", }).ExecuteCommand(); if (iNumber < iList.Count) { _portUtil.DrawerNo = iList[iNumber]; iNumber++; if(PublicStatus==1) { PublicStatus = 2; } else { SelfStatus = 2; } _portUtil.OpenBox(); } else { iNumber = 0; _portUtil.GetBoxStatus(); } break; // 药箱关闭 case EventType.DRAWERCLOSE: //记录药箱操作日志 SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = "DM5", DrawerNo = _portUtil.DrawerNo, Operator = HomeWindowViewModel.Operator?.Id, OperationTime = DateTime.Now, Type = 55, InvoiceId = "药箱关闭", }).ExecuteCommand(); if (PublicStatus == 2) { PublicStatus = 3; } else { SelfStatus = 3; } PublicEnable = true; SelfEnable = true; DrawerNo = -1; _portUtil.Operate = false; break; // 打开失败 case EventType.OPENERROR: AlertMsg alertMsg = new AlertMsg { Message = msg.Message, Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); PublicEnable = true; SelfEnable = true; DrawerNo = -1; PublicStatus = 0; SelfStatus = 0; //记录药箱操作日志 SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = "DM5", DrawerNo = _portUtil.DrawerNo, Operator = HomeWindowViewModel.Operator?.Id, OperationTime = DateTime.Now, Type = 55, InvoiceId = "药箱打开失败", }).ExecuteCommand(); _portUtil.Operate = false; break; } } public bool KeepAlive => false; public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { // 取消消息订阅 _eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); } public void OnNavigatedTo(NavigationContext navigationContext) { _eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); } } }