using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.select; using DM_Weight.util; using log4net; using log4net.Repository.Hierarchy; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Media; namespace DM_Weight.ViewModels { public class CheckOrderNewWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime { private readonly ILog logger = LogManager.GetLogger(typeof(CheckOrderNewWindowViewModel)); // System.Timers.Timer CheckBoxStatusTimer; private int _pageNum = 1; public int PageNum { get => _pageNum; set { SetProperty(ref _pageNum, value); RequestData(); } } private int _pageCount = 1; public int PageCount { get => _pageCount; set { SetProperty(ref _pageCount, value); } } private int _pageSize = 8; public int PageSize { get => _pageSize; set { SetProperty(ref _pageSize, value); } } private int _totalCount = 0; public int TotalCount { get => _totalCount; set { SetProperty(ref _totalCount, value); } } //所选药箱号 private int _drawerNo = -1; public int DrawerNo { get => _drawerNo; set => SetProperty(ref _drawerNo, value); } private string _orderDate = DateTime.Now.ToString("yyyy-MM-dd"); /// /// 查询条件 处方日期 /// public string OrderDate { get { return _orderDate; } set { if (!String.IsNullOrEmpty(value)) { SetProperty(ref _orderDate, DateTime.Parse(value).ToString("yyyy-MM-dd")); } else { SetProperty(ref _orderDate, value); } RequestData(); } } 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); } } private List _ordersList = new List(); public List OrderInfoList { get => _ordersList; set => SetProperty(ref _ordersList, value); } private List? _totalDrugList = new List(); public List? TotalDrugList { get => _totalDrugList; set => SetProperty(ref _totalDrugList, value); } private OrderInfo _selectOrderInfo; public OrderInfo selectOrderInfo { get => _selectOrderInfo; set => SetProperty(ref _selectOrderInfo, value); } #region 18个药箱号的可用状态 private bool _IsEnabled1 = true; public bool IsEnabled1 { get => _IsEnabled1; set => SetProperty(ref _IsEnabled1, value); } private bool _IsEnabled2 = true; public bool IsEnabled2 { get => _IsEnabled2; set => SetProperty(ref _IsEnabled2, value); } private bool _IsEnabled3 = true; public bool IsEnabled3 { get => _IsEnabled3; set => SetProperty(ref _IsEnabled3, value); } private bool _IsEnabled4 = true; public bool IsEnabled4 { get => _IsEnabled4; set => SetProperty(ref _IsEnabled4, value); } private bool _IsEnabled5 = true; public bool IsEnabled5 { get => _IsEnabled5; set => SetProperty(ref _IsEnabled5, value); } private bool _IsEnabled6 = true; public bool IsEnabled6 { get => _IsEnabled6; set => SetProperty(ref _IsEnabled6, value); } private bool _IsEnabled7 = true; public bool IsEnabled7 { get => _IsEnabled7; set => SetProperty(ref _IsEnabled7, value); } private bool _IsEnabled8 = true; public bool IsEnabled8 { get => _IsEnabled8; set => SetProperty(ref _IsEnabled8, value); } private bool _IsEnabled9 = true; public bool IsEnabled9 { get => _IsEnabled9; set => SetProperty(ref _IsEnabled9, value); } private bool _IsEnabled10 = true; public bool IsEnabled10 { get => _IsEnabled10; set => SetProperty(ref _IsEnabled10, value); } private bool _IsEnabled11 = true; public bool IsEnabled11 { get => _IsEnabled11; set => SetProperty(ref _IsEnabled11, value); } private bool _IsEnabled12 = true; public bool IsEnabled12 { get => _IsEnabled12; set => SetProperty(ref _IsEnabled12, value); } private bool _IsEnabled13 = true; public bool IsEnabled13 { get => _IsEnabled13; set => SetProperty(ref _IsEnabled13, value); } private bool _IsEnabled14 = true; public bool IsEnabled14 { get => _IsEnabled14; set => SetProperty(ref _IsEnabled14, value); } private bool _IsEnabled15 = true; public bool IsEnabled15 { get => _IsEnabled15; set => SetProperty(ref _IsEnabled15, value); } private bool _IsEnabled16 = true; public bool IsEnabled16 { get => _IsEnabled16; set => SetProperty(ref _IsEnabled16, value); } private bool _IsEnabled17 = true; public bool IsEnabled17 { get => _IsEnabled17; set => SetProperty(ref _IsEnabled17, value); } private bool _IsEnabled18 = true; public bool IsEnabled18 { get => _IsEnabled18; set => SetProperty(ref _IsEnabled18, value); } #endregion #region 按钮颜色 private Brush _button1Color = Brushes.White; public Brush Button1Color { get => _button1Color; set => SetProperty(ref _button1Color, value); } private Brush _button2Color = Brushes.White; public Brush Button2Color { get => _button2Color; set => SetProperty(ref _button2Color, value); } private Brush _button3Color = Brushes.White; public Brush Button3Color { get => _button3Color; set => SetProperty(ref _button3Color, value); } private Brush _button4Color = Brushes.White; public Brush Button4Color { get => _button4Color; set => SetProperty(ref _button4Color, value); } private Brush _button5Color = Brushes.White; public Brush Button5Color { get => _button5Color; set => SetProperty(ref _button5Color, value); } private Brush _button6Color = Brushes.White; public Brush Button6Color { get => _button6Color; set => SetProperty(ref _button6Color, value); } private Brush _button7Color = Brushes.White; public Brush Button7Color { get => _button7Color; set => SetProperty(ref _button7Color, value); } private Brush _button8Color = Brushes.White; public Brush Button8Color { get => _button8Color; set => SetProperty(ref _button8Color, value); } private Brush _button9Color = Brushes.White; public Brush Button9Color { get => _button9Color; set => SetProperty(ref _button9Color, value); } private Brush _button10Color = Brushes.White; public Brush Button10Color { get => _button10Color; set => SetProperty(ref _button10Color, value); } private Brush _button11Color = Brushes.White; public Brush Button11Color { get => _button11Color; set => SetProperty(ref _button11Color, value); } private Brush _button12Color = Brushes.White; public Brush Button12Color { get => _button12Color; set => SetProperty(ref _button12Color, value); } private Brush _button13Color = Brushes.White; public Brush Button13Color { get => _button13Color; set => SetProperty(ref _button13Color, value); } private Brush _button14Color = Brushes.White; public Brush Button14Color { get => _button14Color; set => SetProperty(ref _button14Color, value); } private Brush _button15Color = Brushes.White; public Brush Button15Color { get => _button15Color; set => SetProperty(ref _button15Color, value); } private Brush _button16Color = Brushes.White; public Brush Button16Color { get => _button16Color; set => SetProperty(ref _button16Color, value); } private Brush _button17Color = Brushes.White; public Brush Button17Color { get => _button17Color; set => SetProperty(ref _button17Color, value); } private Brush _button18Color = Brushes.White; public Brush Button18Color { get => _button18Color; set => SetProperty(ref _button18Color, value); } #endregion IEventAggregator _eventAggregator; //private PortUtil _portUtil; SocketHelper _socketHelper; public CheckOrderNewWindowViewModel(IEventAggregator eventAggregator, SocketHelper socketHelper) { //_portUtil = portUtil; _eventAggregator = eventAggregator; _socketHelper = socketHelper; } private string? _searchValue; /// /// 查询条件 查询字段值 /// public string? SearchValue { get { return _searchValue; } set { SetProperty(ref _searchValue, value); RequestData(); } } void RequestData() { int totalCount = 0; if (DrawerNo >= 0) { //查询当前药箱归属的药师 ChannelList currentList = SqlSugarHelper.Db.Queryable().Where(cl => cl.DrawerNo == DrawerNo + 1 && cl.MachineId == "DM5").First();//.Select(cl => cl.BelongUser).First(); //string currentDrawerUser = currentList.BelongUser; OrderInfoList = SqlSugarHelper.Db.Queryable() .Includes(oi => oi._OrderDetail, od => od.DrugInfo) .Includes(oi => oi._OrderDetail, od => od.surgicalResidual) //.Includes(cl => cl.channelStocks, cs => cs.DrugInfo,di=>di.drugBase) //.InnerJoin((oi, od) => oi.OrderNo == od.OrderNo) //.InnerJoin((oi, od, di) => od.DrugId == di.DrugId.ToString()) .WhereIF(OrderDate != null, oi => oi.RecvDate.ToString("yyyy-MM-dd") == OrderDate) .WhereIF(!String.IsNullOrEmpty(ConfigurationManager.AppSettings["storage"]), oi => oi.Pharmacy == ConfigurationManager.AppSettings["storage"]) .Where(oi => oi.DmStatus == 1) .Where(oi => oi.HisDispFlag == 0) .Where(oi => oi.CancelFlag == 0) .Where(oi => oi.DrawerCode == DrawerNo + 1) .Where(oi => oi._OrderDetail.SetManuNo != null) .Where(oi => oi._OrderDetail.Quantity > 0) .OrderBy(oi => oi.OrderId) .ToPageList(PageNum, PageSize, ref totalCount); if (OrderInfoList != null && OrderInfoList.Count() > 0) { OrderInfoList.ForEach(oi => oi.ItemIsChecked = true); //OrderInfoList.ForEach(oi => oi.OrderDetailList.ForEach(od => od.DrugInfo = od.DrugInfo ?? new DrugInfo())); OrderInfoList.ForEach(oi => oi._OrderDetail.DrugInfo = oi._OrderDetail.DrugInfo ?? new DrugInfo()); //TotalDrugList = OrderInfoList.Where(oi => oi.ItemIsChecked).GroupBy(oi => oi.OrderDetailList.DrugInfo.DrugName).Select(oi => new TotalDrug { DrugName = oi.Key, TotalCount = oi.Sum(item => item.OrderDetailList.Quantity) }).ToList(); TotalDrugList = OrderInfoList.Where(oi => oi.ItemIsChecked).Select(OrderDetailList => OrderDetailList._OrderDetail).GroupBy(item => item.DrugInfo.DrugName).Select(group => new TotalDrug { DrugName = group.Key, TotalCount = group.Sum(item => item.Quantity) }).ToList(); } else { TotalDrugList = null; } TotalCount = totalCount; PageCount = (int)Math.Ceiling((double)TotalCount / PageSize); //if (Convert.ToDateTime(currentList.EffDate).ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")) if (currentList != null && currentList.EffDate != null) { switch (DrawerNo + 1) { case 1: _button1Color = Brushes.Yellow; break; case 2: _button2Color = Brushes.Yellow; break; case 3: _button3Color = Brushes.Yellow; break; case 4: _button4Color = Brushes.Yellow; break; case 5: _button5Color = Brushes.Yellow; break; case 6: _button6Color = Brushes.Yellow; break; case 7: _button7Color = Brushes.Yellow; break; case 8: _button8Color = Brushes.Yellow; break; case 9: _button9Color = Brushes.Yellow; break; case 10: _button10Color = Brushes.Yellow; break; case 11: _button11Color = Brushes.Yellow; break; case 12: _button12Color = Brushes.Yellow; break; case 13: _button13Color = Brushes.Yellow; break; case 14: _button14Color = Brushes.Yellow; break; case 15: _button15Color = Brushes.Yellow; break; case 16: _button16Color = Brushes.Yellow; break; case 17: _button17Color = Brushes.Yellow; break; case 18: _button18Color = Brushes.Yellow; break; default: break; } } } } public DelegateCommand RowSelected { get => new DelegateCommand(() => { if (selectOrderInfo != null) { OrderInfoList = OrderInfoList.Select(x => { if (x.OrderNo == selectOrderInfo.OrderNo) { x.ItemIsChecked = !x.ItemIsChecked; } return x; }).ToList(); if (OrderInfoList != null && OrderInfoList.Count() > 0) { //TotalDrugList = OrderInfoList.Where(oi => oi.ItemIsChecked).GroupBy(oi => oi._OrderDetail.DrugInfo.DrugName).Select(oi => new TotalDrug { DrugName = oi.Key, TotalCount = oi.Sum(item => item._OrderDetail.Quantity) }).ToList(); TotalDrugList = OrderInfoList.Where(oi => oi.ItemIsChecked).Select(OrderDetailList => OrderDetailList._OrderDetail).GroupBy(item => item.DrugInfo.DrugName).Select(group => new TotalDrug { DrugName = group.Key, TotalCount = group.Sum(item => item.Quantity) }).ToList(); } } }); } public DelegateCommand Query { get => new DelegateCommand(() => { RequestData(); }); } //确认 public DelegateCommand CheckOrder { get => new DelegateCommand(() => { if (DrawerNo == -1) { AlertMsg alertMsg = new AlertMsg { Message = "请先选择手术间", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); return; } CheckOrderAction(); }); } void CheckOrderAction() { var confirmData = OrderInfoList.Where(oi => oi.ItemIsChecked == true).ToList(); if (confirmData.Count > 0) { var f = SqlSugarHelper.Db.UseTran(() => { string empChannelStock = string.Empty; for (int i = 0; i < confirmData.Count; i++) { OrderInfo oi = confirmData[i]; //更新处方状态 if (oi.DmStatus == 1) { SqlSugarHelper.Db.Updateable(new OrderInfo() { DmStatus = 2, OrderNo = oi.OrderNo }).UpdateColumns(it => new { it.DmStatus }).WhereColumns(it => new { it.OrderNo }).ExecuteCommand(); SqlSugarHelper.Db.Insertable(new OrderFinish() { OrderNo = oi.OrderNo, PatientId = oi.PatientId, Pharmacy = oi.Pharmacy, State = 2, WinNo = DrawerNo + 1 + "号手术间", Operator = HomeWindowViewModel.Operator?.Nickname, }).ExecuteCommand(); } //Expression> updateExp = it => it.ManuNo == oi._OrderDetail.SetManuNo && it.EffDate == oi._OrderDetail.SetEffDate; //for (int j = 0; j < oi._OrderDetail.Count; j++) //{ // oi._OrderDetail = oi.OrderDetailList[j]; int drawerNo = DrawerNo + 1; ChannelStock cs = SqlSugarHelper.Db.Queryable() .Where(cs => cs.DrugId == oi._OrderDetail.DrugId && cs.ManuNo == oi._OrderDetail.SetManuNo // && cs.EffDate == oi._OrderDetail.SetEffDate && cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.DrawerNo == drawerNo && cs.Quantity >= oi._OrderDetail.Quantity).First(); if (cs == null) { empChannelStock += $"{oi.OrderNo},{oi._OrderDetail.DrugId},{oi._OrderDetail.SetManuNo};"; continue; } cs.Quantity = cs.Quantity - oi._OrderDetail.Quantity; cs.NeedNum = cs.NeedNum + oi._OrderDetail.Quantity; // 更新数据 库存信息 SqlSugarHelper.Db.Updateable(cs).UpdateColumns(it => new { it.Quantity, it.NeedNum }).ExecuteCommand(); // 获取更新完库存后的药品库存 List nowChannels = SqlSugarHelper.Db.Queryable() .Where(it => it.MachineId.Equals(cs.MachineId) || it.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"])) .Where(it => it.DrugId.Equals(cs.DrugId)) .Where(it => it.ManuNo.Equals(cs.ManuNo)) .Where(it => it.DrawerType == 1) .ToList(); if (cs != null) { SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = ConfigurationManager.AppSettings["dm_machineId"].ToString(), DrawerNo = cs.DrawerNo, ColNo = cs.ColNo, DrugId = cs.DrugId, ManuNo = cs.ManuNo, EffDate = !String.IsNullOrEmpty(cs.EffDate) ? DateTime.ParseExact(cs.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, Operator = HomeWindowViewModel.Operator?.Id, Reviewer = HomeWindowViewModel.Reviewer?.Id, OperationTime = DateTime.Now, Quantity = oi._OrderDetail.Quantity, Type = 2, InvoiceId = oi.OrderNo //, StockQuantity = nowChannels.Sum(it => it.Quantity) }).ExecuteCommand(); #region 记录 注射剂使用与交接记录报表 //查询发药时间 //MachineRecord SendMachineRecord = SqlSugarHelper.Db.Queryable().Where(mr => mr.DrawerNo == cs.DrawerNo && mr.Type == 2) //.OrderByDescending(mr => mr.OperationTime) //.First(); //string retUser = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cl.DrawerNo == cs.DrawerNo) //.Select(cl => cl.BelongUser).First(); RejectionReport rejectionReport = SqlSugarHelper.Db.Queryable().Where(rp => rp.DrugId == cs.DrugId && rp.DrawerNo == cs.DrawerNo).OrderByDescending(rp => rp.SendDate).First(); if (rejectionReport != null) { //发药信息 //RejectionReport rejectionReport = new RejectionReport(); //rejectionReport.SendDate = SendMachineRecord.OperationTime; //rejectionReport.SendUser = SendMachineRecord.Operator.ToString(); //rejectionReport.ReceiveUser = retUser; rejectionReport.RealNum = cs.BaseQuantity; //还药信息 rejectionReport.InfactNum = cs.BaseQuantity - oi._OrderDetail.Quantity; rejectionReport.EmptyNum = oi._OrderDetail.Quantity; rejectionReport.ReturnTime = DateTime.Now.ToString(); rejectionReport.ReturnUser = rejectionReport.SendUser; rejectionReport.ReturnReceiveUser = rejectionReport.ReceiveUser;// SendMachineRecord.Operator.ToString(); rejectionReport.DrugId = oi._OrderDetail.DrugId; rejectionReport.DrugName = oi._OrderDetail.DrugInfo.DrugName; rejectionReport.DrugSpec = oi._OrderDetail.DrugInfo.DrugSpec; rejectionReport.OperationTime = DateTime.Now; int iRejectionReport = SqlSugarHelper.Db.Updateable(rejectionReport).ExecuteCommand(); } #endregion //} //查询上一条账册中的空瓶数 AccountBookG2 accountBookEmpty = SqlSugarHelper.Db.Queryable() .Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"].ToString())) .Where(ab => ab.Type == 1 || ab.Type == 2) .Where(ab => ab.DrugId == oi._OrderDetail.DrugId) .Where(ab => ab.ManuNo == cs.ManuNo).OrderByDescending(ab => ab.Id).First(); //保存账册 int iInsertResult = SqlSugarHelper.Db.Insertable(new AccountBookG2() { DrugId = oi._OrderDetail.DrugId, Type = 2, Department = oi.DeptName, OrderNo = oi.OrderNo, ManuNo = cs.ManuNo, EffDate = cs.EffDate, OutQuantity = oi._OrderDetail.Quantity, UserId1 = HomeWindowViewModel.Operator?.Id, UserId2 = HomeWindowViewModel.Reviewer?.Id, MachineId = ConfigurationManager.AppSettings["dm_machineId"].ToString(), CreateDate = DateTime.Now.ToString("yyyy-MM-dd"), CreateTime = DateTime.Now, InvoiceNo = oi.OrderNo, ManuStock = nowChannels.Sum(it => it.Quantity), TotalStock = (accountBookEmpty != null ? (accountBookEmpty.TotalStock > 0 ? accountBookEmpty.TotalStock : 0) : 0) + oi._OrderDetail.Quantity, ShoushuJian= drawerNo.ToString() }).ExecuteCommand(); //修改凌晨生成的日结存与总结存数据 AccountBookG2 accountBookG2Day = SqlSugarHelper.Db.Queryable() .Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"].ToString())) .Where(ab => ab.Type == 3) .Where(ab => ab.DrugId == oi._OrderDetail.DrugId) .Where(ab => ab.ManuNo == cs.ManuNo) .Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First(); if (accountBookG2Day != null) { accountBookG2Day.ManuStock = accountBookG2Day.ManuStock - oi._OrderDetail.Quantity; SqlSugarHelper.Db.Updateable(accountBookG2Day).ExecuteCommand(); } else { //生成日结存时可能没有该库位的绑定信息,需要写入日结存 int iDayResult = SqlSugarHelper.Db.Insertable(new AccountBookG2() { DrugId = oi._OrderDetail.DrugId, Type = 3, ManuNo = cs.ManuNo, EffDate = cs.EffDate, YQuantity = 0, ManuStock = oi._OrderDetail.Quantity, TotalStock = oi._OrderDetail.Quantity, UserId1 = HomeWindowViewModel.Operator?.Id, UserId2 = HomeWindowViewModel.Reviewer?.Id, MachineId = ConfigurationManager.AppSettings["dm_machineId"].ToString(), CreateDate = DateTime.Now.ToString("yyyy-MM-dd"), InvoiceNo = "日结存" }).ExecuteCommand(); if (iDayResult <= 0) { logger.Info($"未写入日结存数据{oi._OrderDetail.DrugId}-{cs.ManuNo}-{cs.EffDate}-{cs.Quantity}"); } } //修改凌晨生成的日结存与总结存数据 AccountBookG2 accountBookG2Total = SqlSugarHelper.Db.Queryable() .Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"].ToString())) .Where(ab => ab.Type == 4) .Where(ab => ab.DrugId == oi._OrderDetail.DrugId) .Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First(); if (accountBookG2Total != null) { accountBookG2Total.TotalStock = accountBookG2Total.TotalStock - oi._OrderDetail.Quantity; SqlSugarHelper.Db.Updateable(accountBookG2Total).ExecuteCommand(); } else { //生成总结存时可能没有该库位的绑定信息,需要写入总结存 int iTotalResult = SqlSugarHelper.Db.Insertable(new AccountBookG2() { DrugId = oi._OrderDetail.DrugId, Type = 4, YQuantity = 0, ManuStock = oi._OrderDetail.Quantity, TotalStock = oi._OrderDetail.Quantity, UserId1 = HomeWindowViewModel.Operator?.Id, UserId2 = HomeWindowViewModel.Reviewer?.Id, MachineId = ConfigurationManager.AppSettings["dm_machineId"].ToString(), CreateDate = DateTime.Now.ToString("yyyy-MM-dd"), InvoiceNo = "总结存" }).ExecuteCommand(); if (iTotalResult <= 0) { logger.Info($"未写入总结存数据{oi._OrderDetail.DrugId}-{oi._OrderDetail.Quantity}"); } } } } if (!string.IsNullOrEmpty(empChannelStock)) { AlertMsg alertMsg = new AlertMsg { Message = $"所选单子中对应药品批次无效{empChannelStock}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); logger.Info($"所选单子对应药品批次无效{empChannelStock}"); throw new Exception("事务回滚"); } return true; }); if (f.Data) { RequestData(); AlertMsg alertMsg = new AlertMsg { Message = "所选单子已核对完成", Type = MsgType.SUCCESS, }; _eventAggregator.GetEvent().Publish(alertMsg); RequestData(); } if (!f.IsSuccess) { AlertMsg alertMsg = new AlertMsg { Message = "核对失败!", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } Status = 0; } else { AlertMsg alertMsg = new AlertMsg { Message = "请勾选要核对单数据", Type = MsgType.ERROR }; _eventAggregator.GetEvent().Publish(alertMsg); } } /// /// 选择药箱,打开药箱 /// public DelegateCommand UpdateDrawerNo { get => new DelegateCommand(OpenBoxAction); } public bool KeepAlive => false; public void OpenBoxAction(string strDrawerNo) { //if (ModbusHelper.BoxOperate) //{ // Task.Factory.StartNew(() => { ModbusHelper.SpeakAsync("请关闭药箱后再打开"); }); // return; //} if (!_socketHelper.OpenStatus) { _socketHelper.OpenStatus = true; DrawerNo = Convert.ToInt32(strDrawerNo); MachineRecord machineRecord = new MachineRecord(); machineRecord.MachineId = "DM5"; machineRecord.DrawerNo = DrawerNo + 1; machineRecord.Operator = HomeWindowViewModel.Operator?.Id; machineRecord.OperationTime = DateTime.Now; machineRecord.Type = 55; machineRecord.InvoiceId = $"打开{DrawerNo + 1}号手术间"; if (DrawerNo >= 0) { RequestData(); Status = 1; _socketHelper.speechSynthesizer.SpeakAsyncCancelAll(); _socketHelper.speechSynthesizer.Resume(); _socketHelper.SpeakAsync($"正在打开{DrawerNo + 1}号手术间"); //logger.Info($"正在打开{DrawerNo + 1}号药箱"); //记录开药箱日志 SqlSugarHelper.Db.Insertable(machineRecord).ExecuteCommand(); //ModbusHelper.GetInstance().OpenBoxDoor(DrawerNo); try { _socketHelper.SendMessage(new MyBaseMessage() { lockNo = (short)(DrawerNo) }); _socketHelper.dateTime = DateTime.Now; } catch (Exception ex) { AlertMsg alertMsg = new AlertMsg { Message = $"网口连接异常,正在重试{ex.Message}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); logger.Info($"网口连接异常,正在重试{ex.Message}"); _socketHelper.OpenStatus = false; return; } //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("药箱已打开,请及时关闭"); // } // } //}); int iException = 0; Thread.Sleep(200); new PromiseUtil().taskAsyncLoop(200, 0, async (options, next, stop) => { _socketHelper.IsMultiThread = true; try { 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) { logger.Info($"OpenStatus:{_socketHelper.OpenStatus}"); //if (i == 0) //{ // //ModbusHelper.SpeakAsync("请及时关闭药箱"); // i = 10; //} next(); } else { logger.Info($"OpenStatus:{_socketHelper.OpenStatus}"); _socketHelper.dateTime = DateTime.Now; _socketHelper.IsMultiThread = false; stop(); } } else { _socketHelper.dateTime = DateTime.Now; _socketHelper.IsMultiThread = false; stop(); } iException = 0; } catch (Exception ex) { iException++; if (iException >= 3) { _socketHelper.OpenStatus = false; } // _socketHelper.OpenStatus = false; AlertMsg alertMsg = new AlertMsg { Message = $"网口连接异常,正在重试{ex.Message}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); logger.Info($"网口连接异常,正在重试{ex.Message}"); next(); } }); //设置对应药箱按钮可用状态 //SetIsEnableStatus(DrawerNo, false); ChannelList iList = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == "DM5" && cl.DrawerNo == DrawerNo + 1).First(); if (iList != null) { iList.EffDate = null; SqlSugarHelper.Db.Updateable(iList).UpdateColumns(it => new { it.EffDate }).ExecuteCommand(); } } } else if (_socketHelper.OpenStatus) { _socketHelper.speechSynthesizer.SpeakAsyncCancelAll(); _socketHelper.speechSynthesizer.Resume(); _socketHelper.SpeakAsync("请关闭手术间后再打开"); return; } } private void SetIsEnableStatus(int drawerNO, bool status) { switch (drawerNO) { case 0: IsEnabled1 = status; break; case 1: IsEnabled2 = status; break; case 2: IsEnabled3 = status; break; case 3: IsEnabled4 = status; break; case 4: IsEnabled5 = status; break; case 5: IsEnabled6 = status; break; case 6: IsEnabled7 = status; break; case 7: IsEnabled8 = status; break; case 8: IsEnabled9 = status; break; case 9: IsEnabled10 = status; break; case 10: IsEnabled11 = status; break; case 11: IsEnabled12 = status; break; case 12: IsEnabled13 = status; break; case 13: IsEnabled14 = status; break; case 14: IsEnabled15 = status; break; case 15: IsEnabled16 = status; break; case 16: IsEnabled17 = status; break; case 17: IsEnabled18 = status; break; } } public void OnNavigatedTo(NavigationContext navigationContext) { logger.Info("进入OnNavigatedTo"); //RequestData(); NavigateToInfo(); logger.Info("结束RequestData"); } void NavigateToInfo() { List chlList = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == "DM5").ToList();//.Select(cl => cl.BelongUser).First(); if (chlList != null && chlList.Count > 0) { for (int i = 0; i < chlList.Count; i++) { ChannelList chl = chlList[i]; if (chl != null && chl.EffDate != null) { switch (chl.DrawerNo) { case 1: Button1Color = Brushes.Yellow; break; case 2: Button2Color = Brushes.Yellow; break; case 3: Button3Color = Brushes.Yellow; break; case 4: Button4Color = Brushes.Yellow; break; case 5: Button5Color = Brushes.Yellow; break; case 6: Button6Color = Brushes.Yellow; break; case 7: Button7Color = Brushes.Yellow; break; case 8: Button8Color = Brushes.Yellow; break; case 9: Button9Color = Brushes.Yellow; break; case 10: Button10Color = Brushes.Yellow; break; case 11: Button11Color = Brushes.Yellow; break; case 12: Button12Color = Brushes.Yellow; break; case 13: Button13Color = Brushes.Yellow; break; case 14: Button14Color = Brushes.Yellow; break; case 15: Button15Color = Brushes.Yellow; break; case 16: Button16Color = Brushes.Yellow; break; case 17: Button17Color = Brushes.Yellow; break; case 18: Button18Color = Brushes.Yellow; break; default: break; } } } } } public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } public void OnNavigatedFrom(NavigationContext navigationContext) { } //void DoMyPrismEvent(DeviceMsg msg) //{ // switch (msg.EventType) // { // // 药箱打开 // case EventType.DRAWEROPEN: // if (Status == 1) // { // Status = 2; // } // //记录开药箱日志 // SqlSugarHelper.Db.Insertable(new MachineRecord() // { // MachineId = "DM5", // DrawerNo = _portUtil.DrawerNo, // Operator = HomeWindowViewModel.Operator?.Id, // OperationTime = DateTime.Now, // Type = 55, // InvoiceId = $"打开{DrawerNo}号药箱", // }).ExecuteCommand(); // _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 (Status == 2) // { // Status = 3; // } // _portUtil.Operate = false; // break; // // 打开失败 // case EventType.OPENERROR: // AlertMsg alertMsg = new AlertMsg // { // Message = msg.Message, // Type = MsgType.ERROR // }; // _eventAggregator.GetEvent().Publish(alertMsg); // Status = 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 class TotalDrug { public string DrugName { get; set; } public int TotalCount { get; set; } } }