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 { internal class AddToJiaoJieNewWindowViewModel : 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); //_eventAggregator.GetEvent().Unsubscribe(SetIsSelected); } private List? channelStocks; public List? ChannelStocks { get => channelStocks; set => SetProperty(ref channelStocks, value); } private ChannelStock _channelStock; public ChannelStock _ChannelStock { get => _channelStock; set => SetProperty(ref _channelStock, value); } private List? _channelStocksList; public List? _ChannelStocksList { get => _channelStocksList; set => SetProperty(ref _channelStocksList, value); } private List? _channelLists; public List? _ChannelLists { get => _channelLists; set => SetProperty(ref _channelLists, value); } private ChannelList _channelList; public ChannelList _ChannelList { get => _channelList; set => SetProperty(ref _channelList, value); } public void OnNavigatedTo(NavigationContext navigationContext) { RequestData(); } private PortUtil _portUtil; IEventAggregator _eventAggregator; IDialogService _dialogService; public AddToJiaoJieNewWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService DialogService) { _portUtil = portUtil; _eventAggregator = eventAggregator; _dialogService = DialogService; } private void RequestData() { ChannelStocks?.Clear(); _ChannelLists?.Clear(); //1)查询channel_stock所有要补药的药箱 ChannelStocks = SqlSugarHelper.Db.Queryable() .Includes(cs => cs.ChannelLst) .Includes(cs => cs.DrugInfo) .Where(cs => cs.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5")) .OrderBy(cs => cs.Chnguid) .OrderBy(cs => cs.DrawerNo) .ToList(); ChannelStocks = ChannelStocks.GroupBy(it => new { it.DrawerNo, it.DrugId }) .Select(it => { var ret = it.First(); ret.Quantity = it.Sum(itx => itx.Quantity); //ret.NeedNum = ret.BaseQuantity-it.Sum(itx => itx.Quantity); return ret; }).Where(it => it.BaseQuantity > it.Quantity) .ToList(); if (ChannelStocks != null && ChannelStocks.Count > 0) { ChannelStocks.ForEach(cs => cs.AddQuantity =cs.BaseQuantity- cs.Quantity); //2)查询channel_list将1)中查询的添加到channel_list的channel_stock里供页面呈现显示 List DrawerNoList = ChannelStocks.Select(cs => cs.DrawerNo).Distinct().ToList(); List channelLists = new List(); for (int i = 0; i < DrawerNoList.Count; i++) { var channelList = SqlSugarHelper.Db.Queryable() .Where(cl => cl.MachineId == (ConfigurationManager.AppSettings["jj_machineId"] ?? "DM5") && cl.DrawerNo == DrawerNoList[i]) .OrderBy(cl => cl.Id) .OrderBy(cl => cl.DrawerNo) .First(); if (channelList.channelStocks == null) { channelList.channelStocks = new List(); } channelList.channelStocks.AddRange(ChannelStocks.Where(cs => cs.DrawerNo == DrawerNoList[i]).ToList()); if (channelList != null) { //if (channelList.channelStocks[0].AddToJJNum>0) // channelList.State=1; //表示有补药 channelLists.Add(channelList); } } _ChannelLists = channelLists; } else { _ChannelLists?.Clear(); _ChannelLists = new List(); } } 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 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 () => { csList = _ChannelList.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("_ChannelStock", 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) { RequestData(); } private DelegateCommand _rowSelected; public DelegateCommand RowSelected => _rowSelected ??= new DelegateCommand(OpenOrderDialog); public async void OpenOrderDialog() { if (_ChannelList != null && _ChannelList.channelStocks != null && _ChannelList.channelStocks.Any(cs => cs.State == 0)) { // 此处延时1毫秒,等待页面渲染 await Task.Delay(TimeSpan.FromMilliseconds(1)); //选中药箱号下的所有药品id DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("_ChannelStock", _ChannelList.channelStocks.Where(cs => cs.State == 0).ToList()); DialogServiceExtensions.ShowDialogHost(_dialogService, "AddToJiaoJieDialog", dialogParameters, DoDialogResult, "RootDialog"); } if (_ChannelList != null) { AlertMsg alertMsg = new AlertMsg { Message = $"药品已取出,待入库", Type = MsgType.ERROR, }; _eventAggregator.GetEvent().Publish(alertMsg); } } } }