using Common.Logging; using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.select; using DM_Weight.util; 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.Text; using System.Threading; using System.Threading.Tasks; namespace DM_Weight.ViewModels { public class RemoveDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime { private readonly ILog logger = LogManager.GetLogger(typeof(RemoveDialogViewModel)); public string Title => "药品移出药箱"; public event Action RequestClose; private static readonly DateTime Jan1st1970 = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); IEventAggregator _eventAggregator; private ChannelStock _csStock; public ChannelStock _ChannelStock { get { return _csStock; } set { SetProperty(ref _csStock, value); } } SocketHelper _socketHelper; public RemoveDialogViewModel(IEventAggregator eventAggregator, SocketHelper socketHelper) { _eventAggregator = eventAggregator; _socketHelper = socketHelper; } private int _status = 0; public int Status { get => _status; set => SetProperty(ref _status, value); } static List StaticSelects = new List() { new OrderTakeSelect{Name="一号手术间",Code="1"}, new OrderTakeSelect{Name="二号手术间",Code="2"}, new OrderTakeSelect{Name="三号手术间",Code="3"}, new OrderTakeSelect{Name="四号手术间",Code="4"}, new OrderTakeSelect{Name="五号手术间",Code="5"}, new OrderTakeSelect{Name="六号手术间",Code="6"}, new OrderTakeSelect{Name="七号手术间",Code="7"}, new OrderTakeSelect{Name="八号手术间",Code="8"}, new OrderTakeSelect{Name="九号手术间",Code="9"}, new OrderTakeSelect{Name="十号手术间",Code="10"}, new OrderTakeSelect{Name="十一号手术间",Code="11"}, new OrderTakeSelect{Name="十二号手术间",Code="12"}, new OrderTakeSelect{Name="十三号手术间",Code="13"}, new OrderTakeSelect{Name="十四号手术间",Code="14"}, new OrderTakeSelect{Name="十五号手术间",Code="15"}, new OrderTakeSelect{Name="十六号手术间",Code="16"}, new OrderTakeSelect{Name="十七号手术间",Code="17"}, new OrderTakeSelect{Name="十八号手术间",Code="18"}, }; private List _selects = StaticSelects; public List Selects { get { return _selects; } set { SetProperty(ref _selects, value); } } private OrderTakeSelect _selectedItem = StaticSelects[0]; public OrderTakeSelect SelectedItem { get => _selectedItem; set => SetProperty(ref _selectedItem, value); } //去掉移出的药箱号 private OrderTakeSelect _removeItem = new(); public OrderTakeSelect RemoveItem { get => _removeItem; set => SetProperty(ref _removeItem, value); } private int _removeQuantity; public int RemoveQuantity { get => _removeQuantity; set { if (value < 0) { throw new ArgumentException("移入数量不能是负数"); } if (value > _ChannelStock.Quantity) { throw new ArgumentException("移入数量超出库存数"); } SetProperty(ref _removeQuantity, value); } } public bool CanCloseDialog() { return Status == 0; } public void OnDialogClosed() { Selects.Insert(_ChannelStock.DrawerNo - 1, RemoveItem); } public void OnDialogOpened(IDialogParameters parameters) { ChannelStock _csStock = parameters.GetValue("csStock"); _ChannelStock = _csStock; RemoveItem = Selects[_ChannelStock.DrawerNo - 1]; Selects.Remove(RemoveItem); RemoveQuantity = _ChannelStock.Quantity; RequestData(); } public void RequestData() { } //开药盒 public DelegateCommand OpenDrawer { get => new DelegateCommand(async () => { if (SelectedItem != null) { await OpenBox(); } else { AlertMsg alertMsg = new AlertMsg { Message = "请选择要移入的药箱!", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } }, () => Status == 0); } private async Task OpenBox() { if (_socketHelper.OpenStatus) { _socketHelper.speechSynthesizer.SpeakAsyncCancelAll(); _socketHelper.speechSynthesizer.Resume(); _socketHelper.SpeakAsync("请关闭手术间后再打开"); return; } MachineRecord machineRecord = new MachineRecord(); machineRecord.MachineId = "DM5"; machineRecord.DrawerNo = Convert.ToInt32(SelectedItem.Code); machineRecord.Operator = HomeWindowViewModel.Operator?.Id; machineRecord.OperationTime = DateTime.Now; machineRecord.Type = 55; machineRecord.InvoiceId = $"打开{Convert.ToInt32(SelectedItem.Code)}号手术间"; if (Convert.ToInt32(SelectedItem.Code) > 0) { RequestData(); Status = 1; _socketHelper.speechSynthesizer.SpeakAsyncCancelAll(); _socketHelper.speechSynthesizer.Resume(); _socketHelper.SpeakAsync($"正在打开{SelectedItem.Code}号手术间"); logger.Info($"正在打开{SelectedItem.Code}号手术间"); //记录开药箱日志 SqlSugarHelper.Db.Insertable(machineRecord).ExecuteCommand(); try { _socketHelper.SendMessage(new MyBaseMessage() { lockNo = (short)(Convert.ToInt32(SelectedItem.Code) - 1) }); _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}"); return; } _socketHelper.OpenStatus = true; //记录药箱打开时间 ChannelList channelList = SqlSugarHelper.Db.Queryable().Where(cl => cl.MachineId == "DM5" && cl.DrawerNo == _ChannelStock.DrawerNo).First(); if (channelList != null && (channelList.EffDate is null || Convert.ToDateTime(channelList.EffDate).ToString("yyyy-MM-dd") != DateTime.Now.ToString("yyyy-MM-dd"))) { channelList.EffDate = DateTime.Now.ToString(); SqlSugarHelper.Db.Updateable(channelList).UpdateColumns(it => new { it.EffDate }).ExecuteCommand(); } Thread.Sleep(200); int iException = 0; await new PromiseUtil().taskAsyncLoop(500, 0, async (options, next, stop) => { try { if (_socketHelper.OpenStatus) { _socketHelper.SendMessage(new MyBaseMessage() { lockNo = 0x33, functionCode = 4, delay = 2 }); if (_socketHelper.OpenStatus) { next(); } else { _socketHelper.IsMultiThread = false; _socketHelper.dateTime = DateTime.Now; Status = 0; stop(); } } else { _socketHelper.IsMultiThread = false; _socketHelper.dateTime = DateTime.Now; Status = 0; stop(); } iException = 0; } catch (Exception ex) { iException++; if (iException >= 3) { _socketHelper.OpenStatus = false; } AlertMsg alertMsg = new AlertMsg { Message = $"网口连接异常,正在重试{ex.Message}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); logger.Info($"网口连接异常,正在重试{ex.Message}"); next(); } }); } } private bool _isFinishClick = false; public bool IsFinishClick { get => _isFinishClick; set => SetProperty(ref _isFinishClick, value); } // 完成按钮 public DelegateCommand FinishCommand { get => new DelegateCommand(() => { try { IsFinishClick = true; var f = SqlSugarHelper.Db.UseTran(() => { //查询要移入的药箱中是否有该药品 List RemoveChannelStockList = SqlSugarHelper.Db.Queryable() .Where(cs => cs.MachineId.Equals(_ChannelStock.MachineId)) .Where(cs => cs.DrawerNo == Convert.ToInt32(SelectedItem.Code)) .Where(cs => cs.DrugId.Equals(_ChannelStock.DrugId)) //.Where(cs => cs.ManuNo.Equals(_ChannelStock.ManuNo)) .ToList(); ChannelList channelList = SqlSugarHelper.Db.Queryable() .Where(cl => cl.MachineId.Equals(_ChannelStock.MachineId)) .Where(cs => cs.DrawerNo == Convert.ToInt32(SelectedItem.Code)) .Where(cs => cs.DrugId.Equals(_ChannelStock.DrugId)).First(); if (RemoveChannelStockList != null&& RemoveChannelStockList.Count>0&& channelList!=null&&!string.IsNullOrEmpty(channelList.Id)) { //查询药品总数(移入后是否超基数) int sumQuantity = RemoveChannelStockList.Sum(rs => rs.Quantity); if (sumQuantity + RemoveQuantity > channelList.BaseQuantity) { AlertMsg alertMsg = new AlertMsg { Message = "移入数量超出药箱基数,请重新选择", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); return false; } //减当前药箱的数量,添加需要加药数量 _ChannelStock.Quantity = _ChannelStock.Quantity - RemoveQuantity; _ChannelStock.NeedNum = RemoveQuantity; SqlSugarHelper.Db.Updateable(_ChannelStock).UpdateColumns(it => new { it.Quantity,it.NeedNum }).ExecuteCommand(); string csId = Guid.NewGuid().ToString(); //查询要移动的药箱是否有该批次 ChannelStock removeChannelStock = RemoveChannelStockList.Where(it => it.ManuNo == _ChannelStock.ManuNo).FirstOrDefault(); //有该药品且有该批次 if (removeChannelStock != null) { csId = removeChannelStock.Id; removeChannelStock.Quantity = removeChannelStock.Quantity + RemoveQuantity; SqlSugarHelper.Db.Updateable(removeChannelStock).UpdateColumns(it => new { it.Quantity }).ExecuteCommand(); } else { //有该药品但没有该批次 int iInsertResult= SqlSugarHelper.Db.Insertable(new ChannelStock() { MachineId = _ChannelStock.MachineId, DrawerNo = Convert.ToInt32(SelectedItem.Code), ColNo = _ChannelStock.ColNo, DrugId = _ChannelStock.DrugId, ManuNo = _ChannelStock.ManuNo, EffDate=_ChannelStock.EffDate, Quantity = RemoveQuantity, DrawerType = 1, Chnguid = RemoveChannelStockList[0].Chnguid, CheckQuantity= RemoveChannelStockList[0].CheckQuantity, Id = csId, }).ExecuteCommand(); if (iInsertResult > 0) { //删除没有批次的数据 SqlSugarHelper.Db.Deleteable(RemoveChannelStockList.Where(cs => cs.ManuNo == null)).ExecuteCommand(); } else { AlertMsg alertMsg = new AlertMsg { Message = "移入失败", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); return false; } } // 保存数据 药品移出记录 int iInsertRecord= SqlSugarHelper.Db.Insertable(new MachineRecord() { MachineId = _ChannelStock.MachineId, DrawerNo = _ChannelStock.DrawerNo, ColNo = _ChannelStock.ColNo, DrugId = _ChannelStock.DrugId, ManuNo = _ChannelStock.ManuNo, EffDate = !String.IsNullOrEmpty(_ChannelStock.EffDate) ? DateTime.ParseExact(_ChannelStock.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, Operator = HomeWindowViewModel.Operator?.Id, OperationTime = DateTime.Now, Quantity = RemoveQuantity, Type = 21, InvoiceId = _ChannelStock.Id, DepartmentId = csId //要移入的药箱的channelStock的id }).ExecuteCommand(); if(iInsertRecord<=0) { AlertMsg alertMsg = new AlertMsg { Message = "添加记录失败", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); return false; } } else { //没有绑定该药品 AlertMsg alertMsg = new AlertMsg { Message = "药箱没有绑定该药品,请先绑定", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); return false; } return true; }); if (f.Data) { AlertMsg alertMsg = new AlertMsg { Message = "操作完成,库存已更新", Type = MsgType.SUCCESS, }; _eventAggregator.GetEvent().Publish(alertMsg); } if (!f.IsSuccess) { AlertMsg alertMsg = new AlertMsg { Message = "库存更新失败!", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } Status = 0; IsFinishClick = false; RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); } catch (Exception ex) { AlertMsg alertMsg = new AlertMsg { Message = $"操作异常!{ex.Message}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } }, () => !IsFinishClick && RemoveQuantity > 0).ObservesProperty(() => IsFinishClick).ObservesProperty(() => RemoveQuantity); } public long CurrentTimeMillis() { return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds; } // 取消按钮 public DelegateCommand CancleCommand { get => new DelegateCommand(() => { IsFinishClick = false; Status = 0; // 关闭当前窗口 RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); } public DelegateCommand BtnCloseCommand { get => new DelegateCommand(() => { // 关闭当前窗口 RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }, () => Status == 0).ObservesProperty(() => Status); } public bool KeepAlive => false; } }