diff --git a/DM_Weight/App.xaml.cs b/DM_Weight/App.xaml.cs index 759d43c..bc5c79a 100644 --- a/DM_Weight/App.xaml.cs +++ b/DM_Weight/App.xaml.cs @@ -121,6 +121,15 @@ namespace DM_Weight #region 取药 + + //交接柜补药 + containerRegistry.RegisterForNavigation(); + //交接柜补药页面弹窗 + containerRegistry.RegisterDialog(); + containerRegistry.RegisterForNavigation(); + + + // 处方取药页面 containerRegistry.RegisterForNavigation(); // 处方取药模态框 diff --git a/DM_Weight/Converter/OpenBoxConverter.cs b/DM_Weight/Converter/OpenBoxConverter.cs new file mode 100644 index 0000000..75beeb7 --- /dev/null +++ b/DM_Weight/Converter/OpenBoxConverter.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Data; + +namespace DM_Weight.Converter +{ + internal class OpenBoxConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + int status = int.Parse(value.ToString()); + if(status == 3||status==35) + { + return Visibility.Visible; + } + else + { + return Visibility.Collapsed; + } + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/DM_Weight/Models/ChannelList.cs b/DM_Weight/Models/ChannelList.cs index 81f1e4a..9e332df 100644 --- a/DM_Weight/Models/ChannelList.cs +++ b/DM_Weight/Models/ChannelList.cs @@ -196,5 +196,11 @@ namespace DM_Weight.Models private string _drugSpec; [SugarColumn(ColumnName = "drug_manu_no")] public string DrugSpec { get=> _drugSpec;set=>SetProperty(ref _drugSpec, value); } + + [SugarColumn(ColumnName = "yh_no")] + public string BelongUser + { + get; set; + } } } diff --git a/DM_Weight/Models/ChannelStock.cs b/DM_Weight/Models/ChannelStock.cs index fe2462c..cd1f769 100644 --- a/DM_Weight/Models/ChannelStock.cs +++ b/DM_Weight/Models/ChannelStock.cs @@ -72,7 +72,12 @@ namespace DM_Weight.Models /// [SugarColumn(ColumnName = "quantity")] public int Quantity { get; set; } - + /// + /// + /// 交接柜中库存基数 + /// + [SugarColumn(ColumnName = "check_quantity")] + public int BaseQuantity { get; set; } /// /// /// 默认值: 1 @@ -98,11 +103,18 @@ namespace DM_Weight.Models [SugarColumn(IsIgnore = true)] public InOutInvoice Invoice { get; set; } + //[SugarColumn(IsIgnore = true)] + //public string Location + //{ + // get => DrawerNo + "-" + ColNo; + //} + [SugarColumn(IsIgnore = true)] public string Location { - get => DrawerNo + "-" + ColNo; + get => ColNo == 0 ? DrawerNo + "号交接柜" : DrawerNo + "-" + ColNo; } + private int _addQuantity = 0; [SugarColumn(IsIgnore = true)] public int AddQuantity @@ -187,5 +199,12 @@ namespace DM_Weight.Models [SugarColumn(IsIgnore = true)] public DrugPleaseClaim PleaseClaim { get; set; } + //dm_machine_record表id值 + [SugarColumn(IsIgnore = true)] + public int? MachineRecordId { get; set; } + + [SugarColumn(IsIgnore = true)] + public string OrderNos { get; set; } + } } \ No newline at end of file diff --git a/DM_Weight/Models/DrugBase.cs b/DM_Weight/Models/DrugBase.cs index 694c073..6d79200 100644 --- a/DM_Weight/Models/DrugBase.cs +++ b/DM_Weight/Models/DrugBase.cs @@ -17,9 +17,9 @@ namespace DM_Weight.Models private int _baseId = 0; [SugarColumn(ColumnName = "id", IsPrimaryKey = true)] public int BaseId { get=> _baseId; set { SetProperty(ref _baseId, value); } } - private long _drugId = 0; + private string _drugId = "0"; [SugarColumn(ColumnName = "drugid")] - public long DrugId { get => _drugId; set { SetProperty(ref _drugId, value); } } + public string DrugId { get => _drugId; set { SetProperty(ref _drugId, value); } } private string _machineId = ""; [SugarColumn(ColumnName = "machine_id")] public string MachineId { get => _machineId; set { SetProperty(ref _machineId, value); } } diff --git a/DM_Weight/Models/DrugInfo.cs b/DM_Weight/Models/DrugInfo.cs index ed5ee52..efeac45 100644 --- a/DM_Weight/Models/DrugInfo.cs +++ b/DM_Weight/Models/DrugInfo.cs @@ -20,7 +20,7 @@ namespace DM_Weight.Models /// ҩƷID /// [SugarColumn(ColumnName = "drug_id", IsPrimaryKey = true)] - public long DrugId { get; set; } + public string DrugId { get; set; } /// /// ƴ /// diff --git a/DM_Weight/Models/RejectionReport.cs b/DM_Weight/Models/RejectionReport.cs new file mode 100644 index 0000000..f01cda4 --- /dev/null +++ b/DM_Weight/Models/RejectionReport.cs @@ -0,0 +1,104 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DM_Weight.Models +{ + [SugarTable("rejection_report")] + public class RejectionReport + { + /// + /// 主键 + /// + [SugarColumn(ColumnName = "id", IsPrimaryKey = true)] + public int Id { get; set; } + /// + /// 发药时间 + /// + [SugarColumn(ColumnName = "SendDate")] + public DateTime SendDate { get; set; } + /// + /// 发药者 + /// + [SugarColumn(ColumnName = "SendUser")] + public string SendUser { get; set; } + /// + /// 领药者 + /// + [SugarColumn(ColumnName = "ReceiveUser")] + public string ReceiveUser { get; set; } + /// + /// 实发数 + /// + [SugarColumn(ColumnName = "RealNum")] + public int RealNum { get; set; } + /// + /// 实物数 + /// + [SugarColumn(ColumnName = "InfactNum")] + public int InfactNum { get; set; } + /// + /// 空安瓿 + /// + [SugarColumn(ColumnName = "EmptyNum")] + public int EmptyNum { get; set; } + /// + /// 还药时间 + /// + [SugarColumn(ColumnName = "ReturnTime")] + public string ReturnTime { get; set; } + /// + /// 还药者 + /// + [SugarColumn(ColumnName = "ReturnUser")] + public string ReturnUser { get; set; } + /// + /// 接收者 + /// + [SugarColumn(ColumnName = "ReturnReceiveUser")] + public string ReturnReceiveUser { get; set; } + /// + /// 补充者 + /// + [SugarColumn(ColumnName = "AddUser")] + public string AddUser { get; set; } + /// + /// 核对者 + /// + [SugarColumn(ColumnName = "AddCheckUser")] + public string AddCheckUser { get; set; } + /// + /// 操作时间 + /// + [SugarColumn(ColumnName = "OperationTime")] + public DateTime OperationTime { get; set; } + + + [SugarColumn(ColumnName = "DrugId")] + public string DrugId { get; set; } + + /// + /// 药品名称 + /// + [SugarColumn(ColumnName = "DrugName")] + public string DrugName { get; set; } + /// + /// 药品规格 + /// + [SugarColumn(ColumnName = "DrugSpec")] + public string DrugSpec { get; set; } + /// + /// 总基数 + /// + [SugarColumn(ColumnName = "BaseNum")] + public string BaseNum { get; set; } + /// + /// 药箱号 + /// + [SugarColumn(ColumnName = "DrawerNo")] + public int DrawerNo { get; set; } + } +} diff --git a/DM_Weight/Port/PortUtil.cs b/DM_Weight/Port/PortUtil.cs index 8bdc39a..e54d8d0 100644 --- a/DM_Weight/Port/PortUtil.cs +++ b/DM_Weight/Port/PortUtil.cs @@ -129,6 +129,107 @@ namespace DM_Weight.Port ColNos = new int[] { }; } + #region 开抽屉 + /// + /// 打开抽屉 + /// + /// + /// + public async Task OpenAllDrawer() + { + byte[] buffer = await OpenDrawer(); + int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray(); + + logger.Info($"OpenDrawer{string.Join(",", r)}"); + if (DrawerState(r)) + { + statue = 1; + // 返回消息 抽屉已经打开 + _eventAggregator.GetEvent().Publish(new util.DeviceMsg() + { + EventType = util.EventType.DRAWEROPEN, + WindowName = WindowName, + }); + logger.Info($"抽屉【{DrawerNo}】打开成功"); + // 查询抽屉状态 + await Task.Delay(200); + } + else + { + string _WindowName = WindowName; + // 重新初始化数据 + ResetData(); + // 返回消息 抽屉打开失败 + _eventAggregator.GetEvent().Publish(new util.DeviceMsg() + { + EventType = util.EventType.OPENERROR, + WindowName = WindowName, + Message = $"抽屉【{DrawerNo}】打开失败" + }); + logger.Info($"抽屉【{DrawerNo}】打开失败"); + } + } + /// + /// 查询抽屉状态 + /// + /// + /// + public async Task GetAllDrawerLockState() + { + try + { + + if (Operate) + { + // 查询抽屉状态 + byte[] buffer = await CheckDrawerStatus(); + + int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray(); + + logger.Info($"GetAllDrawerLockState{string.Join(",", r)}"); + int[] subArray = new int[8]; + for (int i = 0; i < 8; i++) + { + subArray[i] = r[2 + i]; + } + bool state = subArray.All(r => r > 0); + if (state) + { + //抽屉全部关闭 + + string _WindowName = WindowName; + // 重新初始化数据 + ResetData(); + // 返回消息 抽屉已经关闭 + _eventAggregator.GetEvent().Publish(new util.DeviceMsg() + { + EventType = util.EventType.DRAWERCLOSE, + WindowName = _WindowName, + }); + logger.Info($"抽屉【{DrawerNo}】已关闭"); + } + else + { + // 继续监听抽屉状态 + await Task.Delay(200); + GetAllDrawerLockState(); + } + + } + + } + catch (Exception ex) + { + _eventAggregator.GetEvent().Publish(new util.DeviceMsg() + { + EventType = util.EventType.OPENERROR, + WindowName = WindowName, + Message = $"GetDrawerStatus异常:{ex.Message}" + }); + logger.Info($"GetDrawerStatus异常:{ex.Message}"); + } + } + #endregion public async void Start() { diff --git a/DM_Weight/ViewModels/AddToJiaoJieDialogViewModel.cs b/DM_Weight/ViewModels/AddToJiaoJieDialogViewModel.cs new file mode 100644 index 0000000..764df77 --- /dev/null +++ b/DM_Weight/ViewModels/AddToJiaoJieDialogViewModel.cs @@ -0,0 +1,532 @@ +using DM_Weight.Models; +using DM_Weight.msg; +using DM_Weight.Port; +using DM_Weight.util; +using log4net; +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 AddToJiaoJieDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime + { + private readonly ILog logger = LogManager.GetLogger(typeof(OrderTakeDialogViewModel)); + + + private List _channelStocks = new List(); + + public List ChannelStocks + { + get => _channelStocks; + set => SetProperty(ref _channelStocks, value); + } + /// + /// 交接柜的库位信息 + /// + private List _jiaojie_channelStocks; + + public List Jiaojie_ChannelStocks + { + get => _jiaojie_channelStocks; + set => SetProperty(ref _jiaojie_channelStocks, value); + } + /// + /// 交接柜的库位信息 + /// + private ChannelStock _jiaojei_cs; + + public ChannelStock Jiaojie_cs + { + get => _jiaojei_cs; + set => SetProperty(ref _jiaojei_cs, value); + } + private static readonly DateTime Jan1st1970 = new DateTime + (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + private IEnumerable> enumerable; + private IEnumerator> enumerator; + + + public string Title => "交接柜补药"; + + public bool KeepAlive => false; + + public event Action RequestClose; + + public bool CanCloseDialog() + { + return Status == 0; + } + + public void OnDialogClosed() + { + // 取消消息订阅 + _eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); + } + + public void OnDialogOpened(IDialogParameters parameters) + { + _eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); + Jiaojie_ChannelStocks = parameters.GetValue>("ChannelStocks"); + + for (int i = 0; i < Jiaojie_ChannelStocks.Count; i++) + { + ChannelStock copy = TransExpV2.Trans(Jiaojie_ChannelStocks[i]); + ChannelStocks.Add(copy); + } + ChannelStocks = Jiaojie_ChannelStocks.GroupBy(cs => cs.DrugId).Select(g => new + { + DrugId = g.Key, + AddQuantity = g.Sum(s => s.AddQuantity) + }).Select(cs => new ChannelStock() { DrugId = cs.DrugId, AddQuantity = cs.AddQuantity }).ToList(); + RequestData(); + } + + private PortUtil _portUtil; + IEventAggregator _eventAggregator; + IDialogService _dialogService; + public AddToJiaoJieDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService DialogService) + { + _dialogService = DialogService; + _portUtil = portUtil; + _eventAggregator = eventAggregator; + } + void DoMyPrismEvent(DeviceMsg msg) + { + + if (msg.WindowName == "OrderTakeDrugWindow") + { + IGrouping grouping = enumerator.Current; + int DrawerNo = grouping.Key; + List channelStocks = grouping.ToList(); + + switch (msg.EventType) + { + // 抽屉打开 + case EventType.DRAWEROPEN: + + + if (Status == 1) + { + if (channelStocks[0].process == 1) + { + channelStocks.ForEach(it => it.process = 2); + } + } + //是冰箱抽屉则开冰箱抽屉时发送延迟报警指令 + CheckIsFridgeOpen(); + break; + // 抽屉关闭 + case EventType.DRAWERCLOSE: + if (Status == 1) + { + if (channelStocks[0].process == 2) + { + channelStocks.ForEach(it => it.process = 3); + } + IGrouping groupingBefore = enumerator.Current; + int DrawerNoBefore = groupingBefore.Key; + if (enumerator.MoveNext()) + { + IGrouping groupingAfter = enumerator.Current; + int DrawerNoAfter = groupingAfter.Key; + if (DrawerNoBefore < 9 && DrawerNoAfter > 8) + { + Thread.Sleep(50); + } + OpenOneByOne(); + } + // 已经全部取出 + else + { + Status = 3; + } + } + //是冰箱抽屉则开冰箱抽屉时发送延迟报警指令 + CheckIsFridgeClose(); + break; + // 数量变化 + case EventType.UPDATEQUANTITY: + if (Status == 1) + { + logger.Info($"抽屉【{DrawerNo}】库位取药数量【{msg.Quantitys}】"); + } + break; + // 打开失败 + case EventType.OPENERROR: + AlertMsg alertMsg = new AlertMsg + { + Message = msg.Message, + Type = MsgType.ERROR, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + Status = 0; + break; + } + } + + } + + private int _status = 0; + + public int Status + { + get => _status; set => SetProperty(ref _status, value); + } + public async void RequestData() + { + List channelStocks = new List(); + List msg = new List(); + for (int i = 0; i < ChannelStocks.Count; i++) + { + List HasQChannels = SqlSugarHelper.Db.Queryable() + .Includes(cs => cs.DrugInfo) + .Where(cs => cs.Quantity > 0) + .Where(cs => cs.DrawerType == 1) + .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3")) + .Where(cs => cs.DrugId == ChannelStocks[i].DrugId) + .OrderBy(cs => cs.EffDate) + .OrderBy(cs => cs.DrawerNo) + .ToList(); + if (HasQChannels == null || HasQChannels.Count <= 0) + { + msg.Add($"有药品未绑定,请先绑定"); + continue; + } + int total = HasQChannels.Sum(it => it.Quantity); + int TakeQ = ChannelStocks[i].AddQuantity; + // 说明数量足够 + if (total >= TakeQ) + { + for (int j = 0; TakeQ > 0; j++) + { + ChannelStock stock = HasQChannels[j]; + if (TakeQ > stock.Quantity) + { + stock.TakeQuantity = stock.Quantity; + channelStocks.Add(stock); + TakeQ -= stock.Quantity; + } + else + { + stock.TakeQuantity = TakeQ; + channelStocks.Add(stock); + TakeQ = 0; + } + } + } + else + { + msg.Add($"药品【{HasQChannels[0].DrugInfo.DrugName}】库存不足,应取【{TakeQ}】库存【{total}】"); + } + + } + if (msg.Count > 0) + { + RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); + //MessageBox.Show(string.Join("\n", msg)); + DialogParameters dialogParameters = new DialogParameters(); + dialogParameters.Add("msgInfo", msg); + DialogServiceExtensions.ShowDialogHost(_dialogService, "ShowMessageDialog", dialogParameters, "RootDialog"); + + } + else + { + channelStocks.Sort((a, b) => + { + if ((a.DrawerNo - b.DrawerNo) == 0) + { + return a.ColNo - b.ColNo; + } + return a.DrawerNo - b.DrawerNo; + }); + ChannelStocks = channelStocks; + + + } + } + public DelegateCommand OpenDrawer + { + get => new DelegateCommand(async () => + { + if (Status == 0) + { + if (HomeWindowViewModel.Operator.Role != null && HomeWindowViewModel.Operator.Role.RoleName != "管理员") + { + //查看当前用户是否有所在药品抽屉的权限;1-2层所有人能开,其他6层管理员才能开 + bool bDrawer = ChannelStocks.Select(it => it.DrawerNo).Where(n => n > 2).Any(); + if (bDrawer) + { + AlertMsg alertMsg = new AlertMsg + { + Message = "当前用户没有打开抽屉的权限!", + Type = MsgType.ERROR, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + return; + } + + } + enumerable = ChannelStocks.GroupBy(cs => cs.DrawerNo, cs => cs); + enumerator = enumerable.GetEnumerator(); + enumerator.MoveNext(); + Status = 1; + OpenOneByOne(); + } + + }); + } + + private void OpenOneByOne() + { + IGrouping grouping = enumerator.Current; + int DrawerNo = grouping.Key; + List channelStocks = grouping.ToList(); + channelStocks.ForEach(it => it.process = 1); + _portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉"); + + + List singleChannels = channelStocks.FindAll(it => it.BoardType != 1); + + // 发送取药数量 + singleChannels.ForEach(it => + { + try + { + + _portUtil.TakeQuantity(DrawerNo, it.ColNo, it.TakeQuantity, it.Quantity - it.TakeQuantity); + } + catch (Exception ex) + { + AlertMsg alertMsg = new AlertMsg + { + Message = $"打开抽屉异常{ex.Message}", + Type = MsgType.ERROR, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + } + }); + + _portUtil.WindowName = "OrderTakeDrugWindow"; + _portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1; + _portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray(); + _portUtil.DrawerNo = DrawerNo; + _portUtil.Start(); + } + + private bool _isFinishClick = false; + // 完成按钮 + public DelegateCommand TakeFinish + { + get => new DelegateCommand(() => + { + if (!_isFinishClick) + { + _isFinishClick = true; + List record = ChannelStocks.FindAll(it => it.TakeQuantity > 0).ToList(); + if (record.Count > 0) + { + string InvoiceId = "AddJiaoJie_" + CurrentTimeMillis(); + var f = SqlSugarHelper.Db.UseTran(() => + { + for (int i = 0; i < record.Count; i++) + { + ChannelStock it = record[i]; + // 更新数据 库存信息 + SqlSugarHelper.Db.Updateable(new ChannelStock() + { + Quantity = it.Quantity - it.TakeQuantity, + ManuNo = it.ManuNo, + EffDate = it.EffDate, + Id = it.Id, + }).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate }).ExecuteCommand(); + + //更新 交接柜 库存信息 + List jiaojie = Jiaojie_ChannelStocks.Where(cs => cs.DrugId == it.DrugId).ToList(); + if (jiaojie != null && jiaojie.Count > 0) + { + for (int j = 0; j < jiaojie.Count; j++) + { + // 更新数据 交接柜 库存信息 + ChannelStock jiaojie_it = jiaojie[j]; + SqlSugarHelper.Db.Updateable(new ChannelStock() + { + Quantity = jiaojie_it.BaseQuantity, + //ManuNo = it.ManuNo, + //EffDate = it.EffDate, + Id = jiaojie_it.Id, + }).UpdateColumns(jiaojie_it => new { jiaojie_it.Quantity }).ExecuteCommand(); + } + } + + + // 保存数据 出库记录 + SqlSugarHelper.Db.Insertable(new MachineRecord() + { + MachineId = it.MachineId, + DrawerNo = it.DrawerNo, + ColNo = it.ColNo, + DrugId = it.DrugId, + ManuNo = it.ManuNo, + EffDate = !String.IsNullOrEmpty(it.EffDate) ? DateTime.ParseExact(it.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, + Operator = HomeWindowViewModel.Operator?.Id, + Reviewer = HomeWindowViewModel.Reviewer?.Id, + OperationTime = DateTime.Now, + Quantity = it.TakeQuantity, + Type = 2, + Status = 2,//给交接柜补药不用还空瓶,等真正用了以后再还空瓶,所以先把状态置为2 + InvoiceId = InvoiceId + }).ExecuteCommand(); + } + + //保存注射剂报表信息 + for (int j = 0; j < Jiaojie_ChannelStocks.Count; j++) + { + ChannelStock jStock = Jiaojie_ChannelStocks[j]; + if (jStock.DrugInfo.Dosage == "注射剂") + { + int totalBaseNum = SqlSugarHelper.Db.Queryable(). + Where(db => db.DrugId == jStock.DrugId && db.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM3")).Select(db => db.BaseQuantity).First(); + + string belognUser = SqlSugarHelper.Db.Queryable(). + Where(cl => cl.DrawerNo == jStock.DrawerNo && cl.MachineId == jStock.MachineId).Select(cl => cl.BelongUser).First(); + DrugInfo di = SqlSugarHelper.Db.Queryable().Where(di => di.DrugId == jStock.DrugId).First(); + SqlSugarHelper.Db.Insertable(new RejectionReport() + { + SendDate = DateTime.Parse(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm")), + SendUser = HomeWindowViewModel.Operator?.Nickname, + ReceiveUser = belognUser,//Jiaojie_ChannelStocks + DrugId = jStock.DrugId, + DrugName = di.DrugName, + DrugSpec = di.DrugSpec, + BaseNum = totalBaseNum + "支",//总基数 + DrawerNo = jStock.DrawerNo + }).ExecuteCommand(); + } + } + return true; + }); + if (f.Data) + { + // 更新屏显库存 + List singleChannels = record.FindAll(it => it.BoardType != 5); + if (singleChannels.Count > 0) + { + singleChannels.ForEach(it => + { + _portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.Quantity - it.TakeQuantity); + }); + } + 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.Cancel)); + RequestClose?.Invoke(new DialogResult(ButtonResult.OK)); + } + else + { + _isFinishClick = false; + AlertMsg alertMsg = new AlertMsg + { + Message = "没有填写取药数量", + Type = MsgType.ERROR + }; + _eventAggregator.GetEvent().Publish(alertMsg); + } + + } + + }); + } + + public long CurrentTimeMillis() + { + return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds; + } + + // 取消按钮 + public DelegateCommand CancleTake + { + get => new DelegateCommand(() => + { + _portUtil.ResetData(); + Status = 0; + }); + } + //检查是否是冰箱抽屉(冰箱抽屉打开时需要发送冰箱延迟报警的指令) + public async Task CheckIsFridgeOpen() + { + //if (ChannelStocks != null && ChannelStocks.Count > 0) + //{ + // if (_portUtil.BoardType == (Int32)BoardTypeEnum.fridge) + // { + // _portUtil.FridgeOperate = true; + // //发送冰箱延迟报警的指令 + // await _portUtil.FridgeDelayWarm(); + // _portUtil.FridgeOperate = false; + // } + //} + } + //检查是否是冰箱抽屉(冰箱抽屉关闭时需要查询冰箱温度如温度不在范围则发送冰箱延迟报警的指令) + public async Task CheckIsFridgeClose() + { + //if (ChannelStocks != null && ChannelStocks.Count > 0) + //{ + // if (_portUtil.BoardType == (Int32)BoardTypeEnum.fridge) + // { + // string[] iTempertureRange = ConfigurationManager.AppSettings["temperatureRange"].Split('-'); + // //发送查询冰箱温度的指令 + // float temperature = await _portUtil.GetFridgeTemperature(); + // if (temperature > Convert.ToSingle(iTempertureRange[0]) && temperature < Convert.ToSingle(iTempertureRange[1])) + // { + // _portUtil.FridgeOperate = true; + // //发送冰箱延迟报警的指令 + // await _portUtil.FridgeDelayWarm(); + // _portUtil.FridgeOperate = false; + // } + // } + //} + } + + public DelegateCommand BtnCloseCommand + { + get => new DelegateCommand(() => + { + if (Status != 0) + { + _portUtil.ResetData(); + Status = 0; + } + //DialogParameters parameters = new DialogParameters(); + //parameters.Add("",); + // 关闭当前窗口 + RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); + }); + } + } +} diff --git a/DM_Weight/ViewModels/AddToJiaoJieWindowViewModel.cs b/DM_Weight/ViewModels/AddToJiaoJieWindowViewModel.cs new file mode 100644 index 0000000..b54c49c --- /dev/null +++ b/DM_Weight/ViewModels/AddToJiaoJieWindowViewModel.cs @@ -0,0 +1,454 @@ +using DM_Weight.Models; +using DM_Weight.msg; +using DM_Weight.Port; +using DM_Weight.util; +using log4net; +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.Tasks; + +namespace DM_Weight.ViewModels +{ + public class AddToJiaoJieWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime + { + private readonly ILog logger = LogManager.GetLogger(typeof(AddToJiaoJieWindowViewModel)); + private static readonly DateTime Jan1st1970 = new DateTime + (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + public long CurrentTimeMillis() + { + return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds; + } + public bool KeepAlive => false; + public bool IsNavigationTarget(NavigationContext navigationContext) + { + return true; + } + + public void OnNavigatedFrom(NavigationContext navigationContext) + { + // 取消消息订阅 + _eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); + } + + + private List channelStocks; + public List ChannelStocks + { + get => channelStocks; + set => SetProperty(ref channelStocks, value); + } + + + + public void OnNavigatedTo(NavigationContext navigationContext) + { + _eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); + RequestData(); + } + private PortUtil _portUtil; + IEventAggregator _eventAggregator; + IDialogService _dialogService; + public AddToJiaoJieWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService DialogService) + { + _portUtil = portUtil; + _eventAggregator = eventAggregator; + _dialogService = DialogService; + } + private void RequestData() + { + ChannelStocks = SqlSugarHelper.Db.Queryable() + .Includes(cs => cs.ChannelLst) + .Includes(cs => cs.DrugInfo) + .Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5") && cs.BaseQuantity > cs.Quantity) + .OrderBy(cs => cs.Chnguid) + .OrderBy(cs => cs.DrawerNo) + .ToList(); + ChannelStocks.ForEach(cs => cs.AddQuantity = cs.BaseQuantity - cs.Quantity); + } + private int _status = 0; + + public int Status { get => _status; set => SetProperty(ref _status, value); } + + private bool _isEnable = true; + public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); } + + private List iDrawerNoLst + { get; set; } + private int CurrentNum { get; set; } + + //刷新 + public DelegateCommand QueryCommand + { + get => new DelegateCommand(() => RequestData()); + } + //一键补药 + public DelegateCommand OpenDragCommand + { + get => new DelegateCommand(() => + { + try + { + Status = 1; + IsEnable = false; + + var varDrawerNO = SqlSugarHelper.Db.Queryable() + .Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM3")) + .GroupBy(cs => cs.DrawerNo).Select(DrawerNo => DrawerNo).ToList(); + + iDrawerNoLst = varDrawerNO.Select(item => item.DrawerNo).ToList(); + CurrentNum = 0; + _portUtil.SpeakAsync($"正在打开 {iDrawerNoLst[CurrentNum]} 号抽屉"); + + + _portUtil.WindowName = "AddToJiaoJieWindow"; + _portUtil.Operate = true; + //_portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1; + //_portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray(); + _portUtil.DrawerNo = iDrawerNoLst[CurrentNum]; + _portUtil.OpenAllDrawer(); + + } + catch (Exception ex) + { + + AlertMsg alertMsg = new AlertMsg + { + Message = $"补药异常{ex.Message}", + Type = MsgType.ERROR, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + logger.Info($"AddToJiaoJieWindowViewModel异常:{ex.Message}"); + _portUtil.Operate = false; + } + + }); + } + void DoMyPrismEvent(DeviceMsg msg) + { + if (msg.WindowName == "AddToJiaoJieWindow") + { + switch (msg.EventType) + { + // 抽屉打开 + case EventType.DRAWEROPEN: + if (Status == 1) + { + Status = 2; + } + CurrentNum += 1; + if (CurrentNum < iDrawerNoLst.Count) + { + _portUtil.WindowName = "AddToJiaoJieWindow"; + _portUtil.Operate = true; + _portUtil.DrawerNo = iDrawerNoLst[CurrentNum]; + _portUtil.OpenAllDrawer(); + } + else + { + _portUtil.GetAllDrawerLockState(); + } + break; + // 抽屉关闭 + case EventType.DRAWERCLOSE: + if (Status == 2) + { + Status = 3; + } + _portUtil.Operate = false; + IsEnable = true; + CurrentNum = 0; + + break; + // 数量变化 + case EventType.UPDATEQUANTITY: + if (Status == 2) + { + ChannelStocks.ForEach(it => it.AddQuantity = msg.Quantitys[it.ColNo - 1]); + } + break; + // 打开失败 + case EventType.OPENERROR: + AlertMsg alertMsg = new AlertMsg + { + Message = msg.Message, + Type = MsgType.ERROR, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + Status = 0; + _portUtil.Operate = false; + IsEnable = false; + CurrentNum = 0; + break; + } + } + + } + //完成按钮 + public DelegateCommand AddFinish + { + get => new DelegateCommand(() => + { + if (ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).Count <= 0) + { + + AlertMsg alertMsg = new AlertMsg + { + Message = "请选择药箱", + Type = MsgType.SUCCESS, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + return; + } + List jiaojieStocks = new List(); + List cs = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList(); + for (int i = 0; i < cs.Count; i++) + { + ChannelStock copy = TransExpV2.Trans(cs[i]); + jiaojieStocks.Add(copy); + } + csList = jiaojieStocks.FindAll(cs => cs.ChannelLst.IsSelected).GroupBy(cs => cs.DrugId).Select(g => new + { + DrugId = g.Key, + AddQuantity = g.Sum(s => s.AddQuantity) + }).Select(cs => new ChannelStock() { DrugId = cs.DrugId, AddQuantity = cs.AddQuantity }).ToList(); + + List channelStocks = new List(); + List msg = new List(); + for (int i = 0; i < csList.Count; i++) + { + List HasQChannels = SqlSugarHelper.Db.Queryable() + .Includes(cs => cs.DrugInfo) + .Where(cs => cs.Quantity > 0) + .Where(cs => cs.DrawerType == 1) + .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3")) + .Where(cs => cs.DrugId == csList[i].DrugId) + .OrderBy(cs => cs.EffDate) + .OrderBy(cs => cs.DrawerNo) + .ToList(); + int total = HasQChannels.Sum(it => it.Quantity); + int TakeQ = csList[i].AddQuantity; + // 说明数量足够 + if (total >= TakeQ) + { + for (int j = 0; TakeQ > 0; j++) + { + ChannelStock stock = HasQChannels[j]; + if (TakeQ > stock.Quantity) + { + stock.TakeQuantity = stock.Quantity; + channelStocks.Add(stock); + TakeQ -= stock.Quantity; + } + else + { + stock.TakeQuantity = TakeQ; + channelStocks.Add(stock); + TakeQ = 0; + } + } + } + else + { + msg.Add($"药品【{ChannelStocks[i].DrugInfo.DrugName}】库存不足,应取【{TakeQ}】库存【{total}】"); + } + + } + if (msg.Count > 0) + { + DialogParameters dialogParameters = new DialogParameters(); + dialogParameters.Add("msgInfo", msg); + DialogServiceExtensions.ShowDialogHost(_dialogService, "ShowMessageDialog", dialogParameters, "RootDialog"); + return; + + } + else + { + channelStocks.Sort((a, b) => + { + if ((a.DrawerNo - b.DrawerNo) == 0) + { + return a.ColNo - b.ColNo; + } + return a.DrawerNo - b.DrawerNo; + }); + } + List record = channelStocks.FindAll(it => it.TakeQuantity > 0).ToList(); + if (record.Count > 0) + { + string InvoiceId = "AddJiaoJie_" + CurrentTimeMillis(); + var f = SqlSugarHelper.Db.UseTran(() => + { + for (int i = 0; i < record.Count; i++) + { + ChannelStock it = record[i]; + // 更新数据 库存信息 + SqlSugarHelper.Db.Updateable(new ChannelStock() + { + Quantity = it.Quantity - it.TakeQuantity, + ManuNo = it.ManuNo, + EffDate = it.EffDate, + Id = it.Id, + }).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate }).ExecuteCommand(); + + //更新 交接柜 库存信息 + List jiaojie = jiaojieStocks.Where(cs => cs.DrugId == it.DrugId).ToList(); + if (jiaojie != null && jiaojie.Count > 0) + { + for (int j = 0; j < jiaojie.Count; j++) + { + // 更新数据 交接柜 库存信息 + ChannelStock jiaojie_it = jiaojie[j]; + SqlSugarHelper.Db.Updateable(new ChannelStock() + { + Quantity = jiaojie_it.BaseQuantity, + //ManuNo = it.ManuNo, + //EffDate = it.EffDate, + Id = jiaojie_it.Id, + }).UpdateColumns(jiaojie_it => new { jiaojie_it.Quantity }).ExecuteCommand(); + } + } + + + // 保存数据 出库记录 + SqlSugarHelper.Db.Insertable(new MachineRecord() + { + MachineId = it.MachineId, + DrawerNo = it.DrawerNo, + ColNo = it.ColNo, + DrugId = it.DrugId, + ManuNo = it.ManuNo, + EffDate = !String.IsNullOrEmpty(it.EffDate) ? DateTime.ParseExact(it.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, + Operator = HomeWindowViewModel.Operator?.Id, + Reviewer = HomeWindowViewModel.Reviewer?.Id, + OperationTime = DateTime.Now, + Quantity = it.TakeQuantity, + Type = 2, + InvoiceId = InvoiceId + }).ExecuteCommand(); + } + return true; + }); + if (f.Data) + { + // 更新屏显库存 + List singleChannels = record.FindAll(it => it.BoardType != 5); + if (singleChannels.Count > 0) + { + singleChannels.ForEach(it => + { + _portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.Quantity - it.TakeQuantity); + }); + } + 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); + } + } + else + { + AlertMsg alertMsg = new AlertMsg + { + Message = "补药数量有误", + Type = MsgType.ERROR + }; + _eventAggregator.GetEvent().Publish(alertMsg); + } + }); + } + + //取消 + public DelegateCommand CancleAdd + { + get => new DelegateCommand(() => + { + _portUtil.ResetData(); + Status = 0; + IsEnable = true; + CurrentNum = 0; + }); + } + + + public DelegateCommand RejectReport_Download + { + get => new DelegateCommand(() => { + //GridReportUtil.RejectionReport(""); + }); + } + public DelegateCommand Account_Download + { + get => new DelegateCommand(() => + { + //GridReportUtil.AccountNewReport(); + }); + } + + private List csList = new List(); + //取药 弹出出药列表 + public DelegateCommand TakeDrugCommand + { + get => new DelegateCommand(async () => + { + //选中channelStock.channel_list的isSelected则选中channelStock的isSelected + //var o= ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList(); + + //csList = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).GroupBy(cs => cs.DrugId).Select(g => new { + // DrugId = g.Key, AddQuantity = g.Sum(s => s.AddQuantity) + //}).Select(cs=>new ChannelStock() { DrugId=cs.DrugId,AddQuantity=cs.AddQuantity }).ToList(); + + csList = ChannelStocks.FindAll(cs => cs.ChannelLst.IsSelected).ToList(); + + if (csList != null && csList.Count > 0) + { + // 此处延时1毫秒,等待页面渲染 + await Task.Delay(TimeSpan.FromMilliseconds(1)); + DialogParameters dialogParameters = new DialogParameters(); + dialogParameters.Add("ChannelStocks", csList); + DialogServiceExtensions.ShowDialogHost(_dialogService, "AddToJiaoJieDialog", dialogParameters, DoDialogResult, "RootDialog"); + + } + else + { + AlertMsg alertMsg = new AlertMsg + { + Message = $"未选择药品,请先勾选要药箱号", + Type = MsgType.ERROR, + }; + _eventAggregator.GetEvent().Publish(alertMsg); + _portUtil.Operate = false; + } + }); + } + private void DoDialogResult(IDialogResult dialogResult) + { + // 委托 被动执行 被子窗口执行 + // dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态 + //if(dialogResult.Result == ButtonResult.OK) + //{ + RequestData(); + //} + //MessageBox.Show("返回值:" + dialogResult.Result.ToString()); + } + } +} diff --git a/DM_Weight/ViewModels/DrugListWindowViewModel.cs b/DM_Weight/ViewModels/DrugListWindowViewModel.cs index 3bd2e25..fce0cc8 100644 --- a/DM_Weight/ViewModels/DrugListWindowViewModel.cs +++ b/DM_Weight/ViewModels/DrugListWindowViewModel.cs @@ -199,7 +199,7 @@ namespace DM_Weight.ViewModels //查询最大药品id并+1赋值给新增药品 - long maxDrugId = SqlSugarHelper.Db.Queryable().Max(it => it.DrugId); + string maxDrugId = SqlSugarHelper.Db.Queryable().Max(it => it.DrugId); SelectedDrug.DrugId = maxDrugId + 1; var f = SqlSugarHelper.Db.UseTran(() => { @@ -246,7 +246,7 @@ namespace DM_Weight.ViewModels _eventAggregator.GetEvent().Publish(alertMsg); return; } - }, () => SelectedDrug.DrugId <= 0).ObservesProperty(() => SelectedDrug); + }, () => SelectedDrug.DrugId == null).ObservesProperty(() => SelectedDrug); } public DelegateCommand EditDrugCommand @@ -306,7 +306,7 @@ namespace DM_Weight.ViewModels _eventAggregator.GetEvent().Publish(alertMsg); } RequestData(); - }, () => SelectedDrug.DrugId > 0).ObservesProperty(() => SelectedDrug); + }, () => SelectedDrug.DrugId !=null).ObservesProperty(() => SelectedDrug); } public DelegateCommand AddManuCommand @@ -322,7 +322,7 @@ namespace DM_Weight.ViewModels GetManuNos(); } - }, () => SelectedDrug.DrugId > 0).ObservesProperty(() => SelectedDrug); + }, () => SelectedDrug.DrugId !=null).ObservesProperty(() => SelectedDrug); } public DelegateCommand EditManuCommand diff --git a/DM_Weight/ViewModels/RoleManagerWindowViewModel.cs b/DM_Weight/ViewModels/RoleManagerWindowViewModel.cs index b64c3e7..18097d2 100644 --- a/DM_Weight/ViewModels/RoleManagerWindowViewModel.cs +++ b/DM_Weight/ViewModels/RoleManagerWindowViewModel.cs @@ -165,11 +165,17 @@ namespace DM_Weight.ViewModels PremissionImage = "/Images/TbQyao.png", }; ObservableCollection quyaoChild = new ObservableCollection(); + //PremissionDm quyao1 = new PremissionDm + //{ + // Id = 11, + // PremissionName = "处方取药", + // PremissionPath = "OrderTakeDrugWindow", + //}; PremissionDm quyao1 = new PremissionDm { Id = 11, - PremissionName = "处方取药", - PremissionPath = "OrderTakeDrugWindow", + PremissionName = "交接柜补药", + PremissionPath = "AddToJiaoJieWindow", }; PremissionDm quyao2 = new PremissionDm { diff --git a/DM_Weight/Views/AddToJiaoJieWindow.xaml b/DM_Weight/Views/AddToJiaoJieWindow.xaml new file mode 100644 index 0000000..4a16e67 --- /dev/null +++ b/DM_Weight/Views/AddToJiaoJieWindow.xaml @@ -0,0 +1,173 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/DM_Weight/Views/AddToJiaoJieWindow.xaml.cs b/DM_Weight/Views/AddToJiaoJieWindow.xaml.cs new file mode 100644 index 0000000..8f1b46f --- /dev/null +++ b/DM_Weight/Views/AddToJiaoJieWindow.xaml.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace DM_Weight.Views +{ + /// + /// AddToJiaoJieWindow.xaml 的交互逻辑 + /// + public partial class AddToJiaoJieWindow : UserControl + { + public AddToJiaoJieWindow() + { + InitializeComponent(); + } + } +} diff --git a/DM_Weight/Views/Dialog/AddToJiaoJieDialog.xaml b/DM_Weight/Views/Dialog/AddToJiaoJieDialog.xaml new file mode 100644 index 0000000..6057632 --- /dev/null +++ b/DM_Weight/Views/Dialog/AddToJiaoJieDialog.xaml @@ -0,0 +1,152 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +