using DM_Weight.Common; using DM_Weight.Models; using DM_Weight.msg; using DM_Weight.Port; using DM_Weight.util; using log4net; using NetTaste; 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.Collections.ObjectModel; using System.Configuration; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; namespace DM_Weight.ViewModels { public class BiaoDingWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime { private readonly ILog logger = LogManager.GetLogger(typeof(BiaoDingWindowViewModel)); private List _drawerNoList = new List(); /// /// 抽屉号按钮是否可用 /// private string _btnVisibil; public string BtnVisibil { get => _btnVisibil; set { SetProperty(ref _btnVisibil, value); } } //状态,抽屉下的库位有库存为0的则可标定,否则不可标定 private int _status=0; public int Status { get => _status; set => SetProperty(ref _status, value); } //凭证号 private string _pzh; public string PZH { get => _pzh; set { SetProperty(ref _pzh, value); } } private ObservableCollection? _channelStocks; public ObservableCollection? ChannelStocks { get => _channelStocks; set => SetProperty(ref _channelStocks, value); } private ChannelStock? _channelStock; public ChannelStock? CStock { get => _channelStock; set => SetProperty(ref _channelStock, value); } private static readonly DateTime Jan1st1970 = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private bool _openBoxVisibility = false; public bool OpenBoxVisibility { get => _openBoxVisibility; set => SetProperty(ref _openBoxVisibility, value); } private PortUtil _portUtil; IEventAggregator _eventAggregator; IDialogService _dialogService; public BiaoDingWindowViewModel(IDialogService DialogService, PortUtil portUtil, IEventAggregator eventAggregator) { _dialogService = DialogService; _portUtil = portUtil; _eventAggregator = eventAggregator; } private void DoDialogResult(IDialogResult dialogResult) { CStock = null; Status = 0; RequestData(); } private int _drawerNo = Convert.ToInt32(ConfigurationManager.AppSettings["WeightDrawerNumber"].Split(',')[0]); public int DrawerNo { get => _drawerNo; set => SetProperty(ref _drawerNo, value); } private bool _is8Drawer = true; public bool Is8Drawer { get => _is8Drawer; set => SetProperty(ref _is8Drawer, value); } private bool _is16Drawer = false; public bool Is16Drawer { get => _is16Drawer; set => SetProperty(ref _is16Drawer, value); } private bool _is17Drawer = false; public bool Is17Drawer { get => _is17Drawer; set => SetProperty(ref _is17Drawer, value); } public DelegateCommand UpdateDrawerNo { get => new DelegateCommand((DrawerNo) => { this.DrawerNo = Convert.ToInt32(DrawerNo); RequestData(); }, (DrawerNo) => Status == 0 ); } //打开抽屉 public DelegateCommand OpenDrawer { get => new DelegateCommand(async () => { try { if (Status == 0) { _portUtil.Operate = true; _portUtil.DrawerNo = DrawerNo; _portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉"); byte[] buffer = await _portUtil.OpenDrawer(); int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray(); logger.Info($"OpenDrawer{string.Join(",", r)}"); if (DrawerState(r)) { new PromiseUtil().taskAsyncLoop(200, 0, async (options, next, stop) => { Status = 1; // 查询抽屉状态 _portUtil.Operate = true; byte[] buffer = await _portUtil.CheckDrawerStatus(); int[] r = buffer.Select(it => Convert.ToInt32(it)).ToArray(); if (DrawerState(r)) { Status = 2; next(); } else { Status = 0; //关闭抽屉 logger.Info($"抽屉【{DrawerNo}】已关闭"); // 重新初始化数据 _portUtil.ResetData(); stop(); } }); } else { logger.Info($"抽屉【{DrawerNo}】打开失败"); // 重新初始化数据 _portUtil.ResetData(); AlertMsg alertMsg = new AlertMsg { Message = "抽屉打开失败", Type = MsgType.ERROR, }; // 返回消息 抽屉打开失败 _eventAggregator.GetEvent().Publish(alertMsg); } } } catch (Exception ex) { AlertMsg alertMsg = new AlertMsg { Message = $"操作异常{ex.Message}", Type = MsgType.ERROR, }; // 返回消息 抽屉打开失败 _eventAggregator.GetEvent().Publish(alertMsg); } }); } //开药盒 public DelegateCommand OpenBoxCommand { get => new DelegateCommand(async (param) => { try { if (Status >= 1) { //开药盒 logger.Info("打开药盒"); _portUtil.DrawerNo = DrawerNo; if (await _portUtil.OpenBoxByColNo(CStock.ColNo)) { // 此处延时1毫秒,等待页面渲染 await Task.Delay(TimeSpan.FromMilliseconds(1)); DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("channelStock", CStock); DialogServiceExtensions.ShowDialogHost(_dialogService, "BiaoDingDialog", dialogParameters, DoDialogResult, "RootDialog"); } else { AlertMsg alertMsg = new AlertMsg { Message = "打开药盒失败", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); await Task.Delay(200); } } else { //if (Status >= 2) //{ // AlertMsg alertMsg = new AlertMsg // { // Message = "请先关闭药盒后再打开", // Type = MsgType.ERROR, // }; // _eventAggregator.GetEvent().Publish(alertMsg); //} if(Status<1) { AlertMsg alertMsg = new AlertMsg { Message = "请先开抽屉后再打开药盒", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } } } catch(Exception ex) { AlertMsg alertMsg = new AlertMsg { Message = $"开药盒异常{ex.Message}", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } }); } //抽屉返回状态 private bool DrawerState(int[] r) { int index = DrawerNo > 8 ? DrawerNo - 7 : DrawerNo + 1; return r[index] == 0; } public bool KeepAlive => false; public void FindDrawerCount() { int count = SqlSugarHelper.Db.Queryable().Where(cs => cs.DrawerType != (Int32)DrawerTypeEnum.recyle) .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrawerNo).Select(cs => SqlFunc.AggregateCount(cs.DrawerNo)).Count(); Is8Drawer = count < 9; Is16Drawer = count >= 16; Is17Drawer = count > 16; } //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } public void RequestData() { List queryData = SqlSugarHelper.Db.Queryable() .Includes(cs => cs.DrugInfo, di => di.DrugManuNos) .Where(cs => cs.DrawerNo == DrawerNo) .Where(cs => cs.DrawerType == (Int32)DrawerTypeEnum.drawerTypeOne) .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")) .Where(cs => cs.DrugId != null) .OrderBy(cs => cs.ColNo) .ToList(); ChannelStocks = new ObservableCollection(queryData.Select(cs => { cs.drugManuNo = cs.DrugInfo.DrugManuNos.Find(it => it.ManuNo.Equals(cs.ManuNo)); return cs; }).ToList()); } //接收导航传过来的参数 现在是在此处初始化了表格数据 public void OnNavigatedTo(NavigationContext navigationContext) { FindDrawerCount(); RequestData(); } //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建 public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } //这个方法用于拦截请求 public void OnNavigatedFrom(NavigationContext navigationContext) { } } }