using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using MaterialDesignThemes.Wpf; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using System; using System.Collections; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; namespace DM_Weight.ViewModels { public class SettingBoxWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime { private SolidColorBrush _colorBrush; public SolidColorBrush SnackbarBackground { get => _colorBrush; set => SetProperty(ref _colorBrush, value); } private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3)); public ISnackbarMessageQueue SnackbarMessageQueue { get => _snackbarMessageQueue; set => SetProperty(ref _snackbarMessageQueue, value); } private int _drawerNo = 0; public int DrawerNo { get => _drawerNo; set => SetProperty(ref _drawerNo, value); } private bool _isEnable = true; public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); } private int _status = 0; public int Status { get => _status; set { SetProperty(ref _status, value); } } //选择用户 控件的Visibility值 private object _userStatus = Visibility.Collapsed; public object UserStatus { get => _userStatus; set => SetProperty(ref _userStatus, value); } public static List StaticBoxType = new() { new BoxType() { TypeValue = 1, TypeName = "公共手术间" }, new BoxType() { TypeValue = 0, TypeName = "归属药师" } }; //药箱类型 private List _boxTypes = StaticBoxType; public List BoxTypeList { get => _boxTypes; set => SetProperty(ref _boxTypes, value); } private BoxType _boxTypeSelectedItem = StaticBoxType[0]; public BoxType BoxTypeSelectedItem { get => _boxTypeSelectedItem; set { if (value != null && value.TypeValue == 1) { //药箱为公共药箱,把药师信息隐藏 UserStatus = Visibility.Collapsed; } else { //药箱为药师药箱 UserStatus = Visibility.Visible; } SetProperty(ref _boxTypeSelectedItem, value); } } //static List StaticUserList = SqlSugarHelper.Db.Queryable().Where(us => us.MachineId == "DM3").Select(us => new BindUserList() { UserId =Convert.ToInt32(us.UserBarcode), UserName = us.Nickname }).ToList(); //药师 private List _userLists;// = StaticUserList; public List UserLists { get => _userLists; set => SetProperty(ref _userLists, value); } private BindUserList _userListSelectedItem;// = _userLists[0]; public BindUserList UserListSelectedItem { get => _userListSelectedItem; set => SetProperty(ref _userListSelectedItem, value); } private PortUtil _portUtil; IEventAggregator _eventAggregator; SocketHelper _socketHelper; public SettingBoxWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, SocketHelper socketHelper) { _portUtil = portUtil; _eventAggregator = eventAggregator; BindUserList(); _socketHelper = socketHelper; } private void BindUserList() { if (_userLists == null) { _userLists = SqlSugarHelper.Db.Queryable().Where(us => us.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM5")).Select(us => new BindUserList() { UserId = us.UserBarcode, UserName = us.Nickname }).ToList(); _userListSelectedItem = _userLists[0]; } } public bool KeepAlive => false; public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { // 取消消息订阅 //_eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); } private void BindBoxTypeList() { BoxTypeList.Clear(); BoxTypeList = new List() { new BoxType() { TypeValue = 0, TypeName = "归属药师" }, new BoxType() { TypeValue = 1, TypeName = "公共手术间" } }; } public void OnNavigatedTo(NavigationContext navigationContext) { //_eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); } //点击药箱 public DelegateCommand UpdateDrawerNo { get => new DelegateCommand((DrawerNo) => { UserListSelectedItem = null; this.DrawerNo = Convert.ToInt32(DrawerNo); ChannelList cnl = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == "DM5" && cl.DrawerNo.ToString() == DrawerNo+1).First(); if (cnl != null && cnl.DrawerType != null) { BoxType currentBoxType = StaticBoxType.Where(sbt => sbt.TypeValue == cnl.DrawerType).FirstOrDefault(); if (currentBoxType != null) { BoxTypeSelectedItem = currentBoxType; if (BoxTypeSelectedItem.TypeValue == 0) { //药箱归属药师,把药师信息显示出来 UserStatus = Visibility.Visible; if (!string.IsNullOrEmpty(cnl.BelongUser)) { UserListSelectedItem = _userLists.Where(sul => sul.UserId!=null&&sul.UserId.ToString() == cnl.BelongUser).FirstOrDefault(); } if (UserListSelectedItem == null) { if (UserLists.Where(us => us.UserName == "请选择药师").Count() <= 0) { BindUserList nullBind = new BindUserList(); //nullBind.UserId = null; nullBind.UserName = "请选择药师"; _userLists.Add(nullBind); } UserListSelectedItem = UserLists.Where(us => us.UserName == "请选择药师").FirstOrDefault(); } } else { //药箱为公共药箱,把药师信息隐藏 UserStatus = Visibility.Collapsed; } } } } ); } //打开药箱 public DelegateCommand OpenBox { get => new DelegateCommand(() => { if (DrawerNo > 0) { if (_socketHelper.OpenStatus) { _socketHelper.speechSynthesizer.SpeakAsyncCancelAll(); _socketHelper.speechSynthesizer.Resume(); _socketHelper.SpeakAsync("请关闭手术间后再打开"); return; } //if (ModbusHelper.BoxOperate) //{ // Task.Factory.StartNew(() => { ModbusHelper.SpeakAsync("请关闭药箱后再打开"); }); // return; //} IsEnable = false; Status = 1; //_portUtil.SpeakAsync("正在打开药箱"); //_portUtil.DrawerNo = DrawerNo; //_portUtil.OpenBox(); _portUtil.SpeakAsync($"正在打开{DrawerNo}号手术间");//记录开药箱日志 SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = "DM5", DrawerNo = DrawerNo, Operator = HomeWindowViewModel.Operator?.Id, OperationTime = DateTime.Now, Type = 55, InvoiceId = $"打开{DrawerNo}号手术间", }).ExecuteCommand(); //bool bFlag = ModbusHelper.GetInstance().OpenBoxDoor(DrawerNo - 1); _socketHelper.SendMessage(new MyBaseMessage() { lockNo = (short)(DrawerNo - 1) }); _socketHelper.OpenStatus = true; int i = 10; Thread.Sleep(200); new PromiseUtil().taskAsyncLoop(500, 0, async (options, next, stop) => { i--; if (_socketHelper.OpenStatus) { //bool[] boolsl = ModbusHelper.GetInstance().GetAllBoxState(); _socketHelper.SendMessage(new MyBaseMessage() { lockNo = 0x33, functionCode = 4, delay = 2 }); //ModbusHelper.SpeakAsync($"i为{i};状态为:{_socketHelper.OpenStatus}"); //bool state = Array.TrueForAll(boolsl, b => b == false); if (_socketHelper.OpenStatus) { //if (i == 0) //{ // ModbusHelper.SpeakAsync("请及时关闭药箱"); // i = 10; //} next(); } else { stop(); } } else { stop(); } }); // if (bFlag) { IsEnable = true; Status = 0; } //Task.Factory.StartNew(async () => //{ // bool loop = true; // while (loop) // { // await Task.Delay(1000); // bool[] boolsl = ModbusHelper.GetInstance().GetAllBoxState(); // bool state = Array.TrueForAll(boolsl, b => b == false); // if (state) // { // loop = false; // ModbusHelper.BoxOperate = false; // } // else // { // ModbusHelper.BoxOperate = true; // //ModbusHelper.SpeakAsync("药箱已打开,请及时关闭"); // } // } //}); } }); } public DelegateCommand SaveSetting { get => new DelegateCommand(() => { if (DrawerNo >= 0) { ChannelList channelList = SqlSugarHelper.Db.Queryable().Where(cs => cs.DrawerNo == (DrawerNo+1) && cs.MachineId == "DM5").First(); if (BoxTypeSelectedItem != null) { int iUpdate = 0; if (BoxTypeSelectedItem.TypeValue == 0 && UserListSelectedItem != null) { //药箱 归属药师 iUpdate = SqlSugarHelper.Db.Updateable(new ChannelList() { BelongUser = UserListSelectedItem.UserId.ToString(), DrawerType = BoxTypeSelectedItem.TypeValue, Id = channelList.Id }).UpdateColumns(cl => new { cl.BelongUser, cl.DrawerType }).ExecuteCommand(); } else { //药箱属于公共药箱 iUpdate = SqlSugarHelper.Db.Updateable(new ChannelList() { DrawerType = BoxTypeSelectedItem.TypeValue, Id = channelList.Id }).UpdateColumns(cl => new { cl.DrawerType }).ExecuteCommand(); } if (iUpdate > 0) { //提示请选择药箱 AlertMsg alertMsg = new AlertMsg { Message = $"保存完成", Type = MsgType.INFO }; _eventAggregator.GetEvent().Publish(alertMsg); } else { //提示请选择药箱 AlertMsg alertMsg = new AlertMsg { Message = $"保存失败", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } } } else { //提示请选择药箱 AlertMsg alertMsg = new AlertMsg { Message = $"未选择手术间,请先选择手术间", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } }); } public DelegateCommand Query { get => new DelegateCommand(() => { BindUserList(); }); } } /// /// 药箱类型 /// public class BoxType { public int TypeValue { get; set; } public string TypeName { get; set; } } public class BindUserList { public string UserId { get; set; } public string UserName { get; set; } } }