293 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			293 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
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 SqlSugar;
 | 
						||
using System;
 | 
						||
using System.Collections.Generic;
 | 
						||
using System.ComponentModel;
 | 
						||
using System.Configuration;
 | 
						||
using System.Linq;
 | 
						||
using System.Text;
 | 
						||
using System.Threading.Channels;
 | 
						||
using System.Threading.Tasks;
 | 
						||
using System.Windows.Data;
 | 
						||
 | 
						||
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<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
 | 
						||
            //_eventAggregator.GetEvent<IsSelectedEvent>().Unsubscribe(SetIsSelected);
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        private List<ChannelStock>? channelStocks;
 | 
						||
        public List<ChannelStock>? ChannelStocks
 | 
						||
        {
 | 
						||
            get => channelStocks;
 | 
						||
            set => SetProperty(ref channelStocks, value);
 | 
						||
        }
 | 
						||
 | 
						||
        private ChannelStock _channelStock;
 | 
						||
        public ChannelStock _ChannelStock
 | 
						||
        {
 | 
						||
            get => _channelStock;
 | 
						||
            set => SetProperty(ref _channelStock, value);
 | 
						||
        }
 | 
						||
 | 
						||
        private List<ChannelList>? _channelLists;
 | 
						||
        public List<ChannelList>? _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)
 | 
						||
        {
 | 
						||
            //_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
 | 
						||
            //_eventAggregator.GetEvent<IsSelectedEvent>().Subscribe(SetIsSelected);
 | 
						||
            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?.Clear();
 | 
						||
            _ChannelLists?.Clear();
 | 
						||
            //1)查询channel_stock所有要补药的药箱
 | 
						||
            ChannelStocks = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
              .Includes<ChannelList>(cs => cs.ChannelLst)
 | 
						||
              .Includes<DrugInfo>(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 = it.Sum(itx => itx.NeedNum);
 | 
						||
                    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<int> DrawerNoList = ChannelStocks.Select(cs => cs.DrawerNo).Distinct().OrderBy(cs=>cs).ToList(); 
 | 
						||
                List<ChannelList> channelLists = new List<ChannelList>();
 | 
						||
                for (int i = 0; i < DrawerNoList.Count; i++)
 | 
						||
                {
 | 
						||
                    var channelList = SqlSugarHelper.Db.Queryable<ChannelList>()
 | 
						||
                        .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<ChannelStock>();
 | 
						||
                    }
 | 
						||
                    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<ChannelList>();
 | 
						||
            }
 | 
						||
 | 
						||
        }
 | 
						||
        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<int> 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<ChannelStock> csList = new List<ChannelStock>();
 | 
						||
        //取药 弹出出药列表
 | 
						||
        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 = _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);
 | 
						||
                    //List<string> drugIdLst = ChannelStocks.FindAll(cs => cs.DrawerNo == _ChannelStock.DrawerNo).Select(cs => cs.DrugId).ToList();
 | 
						||
                    //dialogParameters.Add("drugIdLst", drugIdLst);
 | 
						||
                    DialogServiceExtensions.ShowDialogHost(_dialogService, "AddToJiaoJieDialog", dialogParameters, DoDialogResult, "RootDialog");
 | 
						||
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = $"未选择药品,请先点选药箱号",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    _portUtil.Operate = false;
 | 
						||
                }
 | 
						||
            });
 | 
						||
        }
 | 
						||
        private void DoDialogResult(IDialogResult dialogResult)
 | 
						||
        {
 | 
						||
            // 委托   被动执行     被子窗口执行
 | 
						||
            // dialogResult  第一方面可以拿到任意参数   第二方面   可判断关闭状态
 | 
						||
            //if(dialogResult.Result == ButtonResult.OK)
 | 
						||
            //{
 | 
						||
            RequestData();
 | 
						||
            //}
 | 
						||
            //MessageBox.Show("返回值:" + dialogResult.Result.ToString());
 | 
						||
        }
 | 
						||
        //int AddToJiaoJieClickNum = 1;
 | 
						||
        ////设置选中药箱的复选框状态
 | 
						||
        //private void SetIsSelected(ChannelStock channelStock)
 | 
						||
        //{
 | 
						||
        //    if (channelStock != null)
 | 
						||
        //    {
 | 
						||
        //        //channelStock.ChannelLst.IsSelected = !channelStock.ChannelLst.IsSelected;
 | 
						||
        //        if (channelStock.ChannelLst.State == 0)
 | 
						||
        //        {
 | 
						||
        //            channelStock.ChannelLst.IsSelected = !channelStock.ChannelLst.IsSelected;
 | 
						||
        //        }
 | 
						||
        //        else
 | 
						||
        //        {
 | 
						||
        //            return;
 | 
						||
        //        }
 | 
						||
        //        if (_ChannelList.channelStocks != null && _ChannelList.channelStocks != null)
 | 
						||
        //        {
 | 
						||
        //            _ChannelList.channelStocks = _ChannelList.channelStocks.Select(x =>
 | 
						||
        //            {
 | 
						||
        //                for (int i = 0; i < _ChannelList.channelStocks.Count; i++)
 | 
						||
        //                {
 | 
						||
        //                    if (_ChannelList.channelStocks[i].DrawerNo == channelStock.DrawerNo)
 | 
						||
        //                    {
 | 
						||
        //                        _ChannelList.channelStocks[i].ChannelLst = channelStock.ChannelLst;
 | 
						||
        //                    }
 | 
						||
        //                    else
 | 
						||
        //                    {
 | 
						||
        //                        _ChannelList.channelStocks[i].ChannelLst.IsSelected = false;
 | 
						||
        //                    }
 | 
						||
        //                }
 | 
						||
        //                return x;
 | 
						||
        //            }).ToList();
 | 
						||
        //        }
 | 
						||
        //        ICollectionView vw = CollectionViewSource.GetDefaultView(_ChannelList.channelStocks);
 | 
						||
        //        vw.GroupDescriptions.Add(new PropertyGroupDescription("ChannelLst"));
 | 
						||
        //    }
 | 
						||
        //}
 | 
						||
 | 
						||
 | 
						||
        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<SnackbarEvent>().Publish(alertMsg);
 | 
						||
            }
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |