using MaterialDesignThemes.Wpf; using Prism.Commands; using Prism.Events; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using log4net; namespace DM_Weight.ViewModels { public class BindingChannelDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime { public static BindingChannelDialogViewModel vm; public string Title => "库位绑定"; public event Action RequestClose; private int _drawerNo = 0; 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); } IEventAggregator _eventAggregator; PortUtil _portUtil; //ScreenUtil _screenUtil; public BindingChannelDialogViewModel(IEventAggregator eventAggregator, PortUtil portUtil//, ScreenUtil screenUtil ) { _eventAggregator = eventAggregator; _portUtil = portUtil; //_screenUtil = screenUtil; vm = this; } public int DrawerNo { get => _drawerNo; set { SetProperty(ref _drawerNo, value); GetChannelsByDrawerNo(); } } private DrugInfo? _drugInfo; public DrugInfo? DrugInfo { get => _drugInfo; set { SetProperty(ref _drugInfo, value); if (_drugInfo != null) { //DrugManuNos = _drugInfo.DrugManuNos; DrugManuNos = SqlSugarHelper.Db.Queryable().Where(m => m.DrugId == _drugInfo.DrugId.ToString()).ToList(); } } } private List? _drugInfos; public List? DrugInfos { get => _drugInfos; set => SetProperty(ref _drugInfos, value); } private DrugManuNo? _drugManuNo; public DrugManuNo? DrugManuNo { get => _drugManuNo; set => SetProperty(ref _drugManuNo, value); } private List? _drugManuNos; public List? DrugManuNos { get => _drugManuNos; set => SetProperty(ref _drugManuNos, value); } private List? _channels; public List? Channels { get => _channels; set => SetProperty(ref _channels, value); } private int _totalCount = 0; public int TotalCount { get => _totalCount; set => SetProperty(ref _totalCount, value); } private int _pageNum = 1; public int PageNum { get => _pageNum; set { SetProperty(ref _pageNum, value); GetChannelsByDrawerNo(); } } private int _pageCount = 1; public int PageCount { get => _pageCount; set { SetProperty(ref _pageCount, value); } } private int _pageSize = 9; public int PageSize { get => _pageSize; set { SetProperty(ref _pageSize, value); } } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { _eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); } private void GetAllDrugInfos() { var list = SqlSugarHelper.Db.Queryable().Includes(di => di.DrugManuNos).OrderBy(di => di.DrugId).ToList(); DrugInfos = list; } private void GetChannelsByDrawerNo() { Channels?.Clear(); int totalCount = 0; var list = SqlSugarHelper.Db.Queryable() .Includes(cs => cs.DrugInfo) .WhereIF(DrawerNo > 0, cs => cs.DrawerNo == DrawerNo) .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")) .OrderBy(cs => cs.DrawerNo) .OrderBy(cs => cs.ColNo) .ToPageList(PageNum, PageSize, ref totalCount); Channels = list; TotalCount = totalCount; } public void OnDialogOpened(IDialogParameters parameters) { if (parameters.ContainsKey("DrawerNo")) { DrawerNo = parameters.GetValue("DrawerNo"); } GetAllDrugInfos(); GetChannelsByDrawerNo(); _eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); } public DelegateCommand Query { get => new DelegateCommand(() => { GetChannelsByDrawerNo(); }); } public DelegateCommand BindingDrug { get => new DelegateCommand(async () => { var SelectChannels = Channels.FindAll(item => item.IsSelected && item.Quantity == 0); if(SelectChannels is null || SelectChannels.Count <= 0) { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("未选中库位或库位还存在药品无法绑定"); return; } if (SelectChannels.All(it => it.DrawerType != 1)) { if (SelectChannels.Count == 1) { if (DrugInfo != null) { int count = SqlSugarHelper.Db.Queryable().Where(cs => cs.DrugId.Equals(DrugInfo.DrugId.ToString())).Where(cs => cs.DrawerType != 1).Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).Count(); if (count == 0) { var item = SelectChannels[0]; if (item.DrugId == null || !DrugInfo.DrugId.ToString().Equals(item.DrugId)) { item.PosNo = 0; } item.DrugId = DrugInfo.DrugId.ToString(); SqlSugarHelper.Db.Updateable(item).UpdateColumns(it => new { it.DrugId, it.PosNo }).ExecuteCommand(); GetChannelsByDrawerNo(); if (item.BoardType == 5) { _portUtil.WindowName = "BindingChannelDialog"; // 向显示屏写入库位信息 _portUtil.WriteChannelInfo(1, DrugInfo.DrugName, item.DrawerNo, item.ColNo); await Task.Delay(200); _portUtil.WriteChannelInfo(2, DrugInfo.DrugSpec, item.DrawerNo, item.ColNo); await Task.Delay(200); _portUtil.WriteChannelInfo(8, DrugInfo.Manufactory, item.DrawerNo, item.ColNo); await Task.Delay(200); _portUtil.ShowContent(item.DrawerNo, item.ColNo); } //_screenUtil.SetStockInfo(item, 1); if(item.BoardType == 35) { _portUtil.WindowName = "BindingChannelDialog"; await _portUtil.WriteChannelInfoMethod(1,DrugInfo.DrugName, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(3, DrugInfo.DrugSpec, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(4, DrugInfo.Manufactory, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(5, item.EffDate, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(6, item.ManuNo, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.ShowContentMethod(item.DrawerNo, item.ColNo); } } else { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue($"药品【{DrugInfo.DrugName}】已经绑定了回收库位不可再次绑定"); } } else { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("请选择库位需要绑定的药品信息"); } } else { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("每种药品只能绑定一个回收库位"); } } else { if (DrugInfo != null && DrugManuNo != null) { var c = SelectChannels.Count; if (c > 0) { SelectChannels.ForEach(async (item) => { if (item.DrugId == null || !DrugInfo.DrugId.ToString().Equals(item.DrugId)) { item.PosNo = 0; } if(DrugManuNo.EffDate!=null&& DrugManuNo.EffDate.Length>=10) { DrugManuNo.EffDate= DrugManuNo.EffDate.Substring(0,10); } item.DrugId = DrugInfo.DrugId.ToString(); item.ManuNo = DrugManuNo.ManuNo; item.DrugInfo = DrugInfo; item.EffDate = String.Format("{0:yyyy-MM-dd}", DrugManuNo.EffDate.Length>=10? DrugManuNo.EffDate.Substring(0,10): DrugManuNo.EffDate); SqlSugarHelper.Db.Updateable(item).UpdateColumns(it => new { it.DrugId, it.ManuNo, it.EffDate, it.PosNo }).ExecuteCommand(); if (item.BoardType == 5) { _portUtil.WindowName = "BindingChannelDialog"; // 向显示屏写入库位信息 await _portUtil.WriteChannelInfo(1, DrugInfo.DrugName, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfo(2, DrugInfo.DrugSpec, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfo(8, DrugInfo.Manufactory, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfo(6, DrugManuNo.ManuNo, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfo(5, String.Format("{0:yyyy-MM-dd}", DrugManuNo.EffDate.Length>=10? DrugManuNo.EffDate.Substring(0,10): DrugManuNo.EffDate), item.DrawerNo, item.ColNo); await Task.Delay(200); _portUtil.ShowContent(item.DrawerNo, item.ColNo); } if (item.BoardType == 35) { _portUtil.WindowName = "BindingChannelDialog"; await _portUtil.WriteChannelInfoMethod(1, DrugInfo.DrugName, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(3, DrugInfo.DrugSpec, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(4, DrugInfo.Manufactory, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(5, item.EffDate, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.WriteChannelInfoMethod(6, item.ManuNo, item.DrawerNo, item.ColNo); await Task.Delay(200); await _portUtil.ShowContentMethod(item.DrawerNo, item.ColNo); } //_screenUtil.SetStockInfo(item, 1); }); GetChannelsByDrawerNo(); } else { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("所选库位中无可绑定库位【库位还存在药品】"); } } else { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("请选择库位需要绑定的药品及批次信息"); } } }); } public DelegateCommand RemoveBinding { get => new DelegateCommand(async () => { var SelectChannels = Channels.FindAll(item => item.IsSelected && item.Quantity == 0); var c = SelectChannels.Count; if (c > 0) { SelectChannels.ForEach(async item => { item.DrugId = null; item.ManuNo = null; item.EffDate = null; item.DrugInfo = null; SqlSugarHelper.Db.Updateable(item).UpdateColumns(it => new { it.DrugId, it.ManuNo, it.EffDate }).ExecuteCommand(); if (item.BoardType == 5) { // 清除显示屏库位信息 _portUtil.ClearContent(item.DrawerNo, item.ColNo); await Task.Delay(200); _portUtil.ShowContent(item.DrawerNo, item.ColNo); } if(item.BoardType == 35) { _portUtil.ClearContentMethod(item.DrawerNo, item.ColNo); await Task.Delay(300); _portUtil.ShowContentMethod(item.DrawerNo,item.ColNo); } //_screenUtil.SetStockInfo(item, 1); }); GetChannelsByDrawerNo(); } else { SnackbarBackground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#b71c1c")); SnackbarMessageQueue.Enqueue("所选库位中无可解绑库位【库位还存在药品】"); } }); } public DelegateCommand BtnCloseCommand { get => new DelegateCommand(() => { //DialogParameters parameters = new DialogParameters(); //parameters.Add("",); // 关闭当前窗口 RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel)); }); } public bool KeepAlive => false; string message = string.Empty; private void DoMyPrismEvent(DeviceMsg msg) { if (msg.WindowName == "BindingChannelDialog") { switch (msg.EventType) { case EventType.OPENERROR: AlertMsg alertMsg = new AlertMsg { Message = msg.Message, Type = MsgType.ERROR, }; if (message != msg.Message) { message = msg.Message; _eventAggregator.GetEvent().Publish(alertMsg); } break; } } } public void UpdateComboBoxItems(string text) { string str = @"SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit, d.manufactory,d.max_stock,CONCAT(drug_name,' ',drug_spec)as drug_name_spec FROM `drug_info` d"; if(string.IsNullOrEmpty(text)) { DrugInfos = SqlSugarHelper.Db.SqlQueryable(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList(); return; } if(DrugInfos!=null) { DrugInfos.Clear(); } DrugInfos = SqlSugarHelper.Db.SqlQueryable(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList(); } } }