401 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			401 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
using log4net;
 | 
						||
using log4net.Repository.Hierarchy;
 | 
						||
using Newtonsoft.Json.Linq;
 | 
						||
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.Speech.Synthesis;
 | 
						||
using System.Text;
 | 
						||
using System.Threading.Tasks;
 | 
						||
using DM_Weight.Models;
 | 
						||
using DM_Weight.msg;
 | 
						||
using DM_Weight.Port;
 | 
						||
using DM_Weight.util;
 | 
						||
using DM_Weight.Views;
 | 
						||
 | 
						||
namespace DM_Weight.ViewModels
 | 
						||
{
 | 
						||
    public class ReturnEmptyDialogViewModel : BindableBase, IDialogAware, IRegionMemberLifetime
 | 
						||
    {
 | 
						||
 | 
						||
        private readonly ILog logger = LogManager.GetLogger(typeof(ReturnEmptyDialogViewModel));
 | 
						||
        public string Title => "归还空瓶(记录)";
 | 
						||
 | 
						||
        public event Action<IDialogResult> RequestClose;
 | 
						||
 | 
						||
        private static readonly DateTime Jan1st1970 = new DateTime
 | 
						||
   (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 | 
						||
 | 
						||
        private string WindowName = "ReturnEmpty1Window";
 | 
						||
 | 
						||
        private PortUtil _portUtil;
 | 
						||
        IEventAggregator _eventAggregator;
 | 
						||
 | 
						||
 | 
						||
        public ReturnEmptyDialogViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
 | 
						||
        {
 | 
						||
            _portUtil = portUtil;
 | 
						||
            _eventAggregator = eventAggregator;
 | 
						||
        }
 | 
						||
 | 
						||
        void DoMyPrismEvent(DeviceMsg msg)
 | 
						||
        {
 | 
						||
 | 
						||
            if (msg.WindowName.Equals(WindowName))
 | 
						||
            {
 | 
						||
 | 
						||
                switch (msg.EventType)
 | 
						||
                {
 | 
						||
                    // 抽屉打开
 | 
						||
                    case EventType.DRAWEROPEN:
 | 
						||
                        if (Status == 1)
 | 
						||
                        {
 | 
						||
                            Status = 2;
 | 
						||
                        }
 | 
						||
 | 
						||
                        break;
 | 
						||
                    // 抽屉关闭
 | 
						||
                    case EventType.DRAWERCLOSE:
 | 
						||
                        if (Status == 2)
 | 
						||
                        {
 | 
						||
                            Status = 3;
 | 
						||
                            if(ChannelStock.DrawerType == 3)
 | 
						||
                            {
 | 
						||
                                ReturnQuantity = msg.Quantitys[0];
 | 
						||
                                logger.Info($"抽屉【{ChannelStock.DrawerNo}】回收库位药品数量增加【{msg.Quantitys[0]}】");
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                        break;
 | 
						||
                    // 数量变化
 | 
						||
                    case EventType.UPDATEQUANTITY:
 | 
						||
                        if (Status == 2)
 | 
						||
                        {
 | 
						||
                            ReturnQuantity = msg.Quantitys[0];
 | 
						||
                            logger.Info($"抽屉【{ChannelStock.DrawerNo}】回收库位药品数量增加【{msg.Quantitys[0]}】");
 | 
						||
                        }
 | 
						||
                        break;
 | 
						||
                    // 打开失败
 | 
						||
                    case EventType.OPENERROR:
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = msg.Message,
 | 
						||
                            Type = MsgType.ERROR,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                        Status = 0;
 | 
						||
                        break;
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
        }
 | 
						||
 | 
						||
        private int _returnQuantity;
 | 
						||
 | 
						||
        public int ReturnQuantity
 | 
						||
        {
 | 
						||
            get => _returnQuantity;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                if (value < 0)
 | 
						||
                {
 | 
						||
                    throw new ArgumentException("还药数量不能是负数");
 | 
						||
                }
 | 
						||
                //if (value > MachineRecord.CanReturnQuantity)
 | 
						||
                //{
 | 
						||
                //    throw new ArgumentException("还药数量超出");
 | 
						||
                //}
 | 
						||
                SetProperty(ref _returnQuantity, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _status = 0;
 | 
						||
 | 
						||
        public int Status
 | 
						||
        {
 | 
						||
            get => _status; set => SetProperty(ref _status, value);
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        private List<MachineRecord> _machineRecords = new();
 | 
						||
 | 
						||
        public List<MachineRecord> MachineRecords
 | 
						||
        {
 | 
						||
            get => _machineRecords;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _machineRecords, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
        private MachineRecord _machineRecord = new();
 | 
						||
 | 
						||
        public MachineRecord _MachineRecord
 | 
						||
        {
 | 
						||
            get => _machineRecord;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _machineRecord, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
        private ChannelStock _channelStock;
 | 
						||
 | 
						||
        public ChannelStock ChannelStock
 | 
						||
        {
 | 
						||
            get => _channelStock;
 | 
						||
            set => SetProperty(ref _channelStock, value);
 | 
						||
        }
 | 
						||
 | 
						||
       
 | 
						||
 | 
						||
        public bool CanCloseDialog()
 | 
						||
        {
 | 
						||
            return Status == 0;
 | 
						||
        }
 | 
						||
 | 
						||
        public void OnDialogClosed()
 | 
						||
        {
 | 
						||
            // 取消消息订阅
 | 
						||
            _eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
 | 
						||
        }
 | 
						||
 | 
						||
        public void OnDialogOpened(IDialogParameters parameters)
 | 
						||
        {
 | 
						||
            _eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
 | 
						||
 | 
						||
            ChannelStock _record = parameters.GetValue<ChannelStock>("channel");
 | 
						||
            ChannelStock = _record;
 | 
						||
 | 
						||
            RequestData();
 | 
						||
        }
 | 
						||
 | 
						||
        public void RequestData()
 | 
						||
        {
 | 
						||
            List<MachineRecord> queryData = SqlSugarHelper.Db.Queryable<MachineRecord>()
 | 
						||
                .Includes<UserList>(mr => mr.User)
 | 
						||
                .Where(mr => mr.DrugId == ChannelStock.DrugId)
 | 
						||
                .Where(mr => mr.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM3"))
 | 
						||
                .Where(mr => mr.Type == 2)
 | 
						||
                .Where(mr => mr.Status != 2)
 | 
						||
                .OrderByDescending(mr => mr.OperationTime)
 | 
						||
                .OrderBy(mr => mr.Id)
 | 
						||
                .ToList();
 | 
						||
            MachineRecords = queryData;
 | 
						||
        }
 | 
						||
        
 | 
						||
        public DelegateCommand RowSelected
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                if (_MachineRecord != null && _MachineRecord.Quantity > 0)
 | 
						||
                {
 | 
						||
                    MachineRecords = MachineRecords.Select(x =>
 | 
						||
                    {
 | 
						||
                        if (x.Id == _MachineRecord.Id)
 | 
						||
                        {
 | 
						||
                            x.IsSelected = !x.IsSelected;
 | 
						||
                        }
 | 
						||
                        return x;
 | 
						||
                    }).ToList();
 | 
						||
                    //DialogParameters dialogParameters = new DialogParameters();
 | 
						||
                    //dialogParameters.Add("channel", Channel);
 | 
						||
                    //DialogServiceExtensions.ShowDialogHost(_dialogService, "ReturnEmptyDialog", dialogParameters, DoDialogResult, "RootDialog");
 | 
						||
                }
 | 
						||
            });
 | 
						||
        }
 | 
						||
        public DelegateCommand OpenDrawer
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                //if (HomeWindowViewModel.Operator.Role != null && HomeWindowViewModel.Operator.Role.RoleName != "管理员")
 | 
						||
                //{
 | 
						||
                //    //查看当前用户是否有所在药品抽屉的权限;1-2层所有人能开,其他6层管理员才能开
 | 
						||
                //    if (ChannelStock.DrawerNo>3)
 | 
						||
                //    {
 | 
						||
                //        AlertMsg alertMsg = new AlertMsg
 | 
						||
                //        {
 | 
						||
                //            Message = "当前用户没有打开抽屉的权限!",
 | 
						||
                //            Type = MsgType.ERROR,
 | 
						||
                //        };
 | 
						||
                //        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                //        return;
 | 
						||
                //    }
 | 
						||
 | 
						||
                //}
 | 
						||
                if (ChannelStock != null)
 | 
						||
                {
 | 
						||
                    if (Status == 0)
 | 
						||
                    {
 | 
						||
                        if (ChannelStock.DrawerType == 3 && ChannelStock.BoardType == 1)
 | 
						||
                        {
 | 
						||
                            Status = 3 ;
 | 
						||
                        }
 | 
						||
                        else
 | 
						||
                        {
 | 
						||
                            Status = 1;
 | 
						||
                            _portUtil.SpeakAsync("正在打开" + ChannelStock.DrawerNo + "号抽屉");
 | 
						||
                            _portUtil.WindowName = WindowName;
 | 
						||
                            _portUtil.ColNos = new int[] { ChannelStock.ColNo };
 | 
						||
                            _portUtil.DrawerNo = ChannelStock.DrawerNo;
 | 
						||
                            _portUtil.DrawerType = ChannelStock.DrawerType;
 | 
						||
                            _portUtil.BoardType = ChannelStock.BoardType;
 | 
						||
                            _portUtil.Start();
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "请选择退还药品要放入的库位!",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                }
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        private bool _isFinishClick = false;
 | 
						||
 | 
						||
        public bool IsFinishClick { get => _isFinishClick; set => SetProperty(ref _isFinishClick, value); }
 | 
						||
        // 完成按钮
 | 
						||
        public DelegateCommand TakeFinish
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                IsFinishClick = true;
 | 
						||
                List<MachineRecord> records = MachineRecords.FindAll(it => it.IsSelected).ToList();
 | 
						||
                if (records.Count > 0 && records.Sum(it => it.Quantity - it.ReturnQuantity1)  == ReturnQuantity)
 | 
						||
                {
 | 
						||
                    string InvoiceId = "RETURN_" + CurrentTimeMillis();
 | 
						||
                    var f = SqlSugarHelper.Db.UseTran(() =>
 | 
						||
                    {
 | 
						||
 | 
						||
                        // 更新数据 库存信息
 | 
						||
                        SqlSugarHelper.Db.Updateable(new ChannelStock()
 | 
						||
                        {
 | 
						||
                            Quantity = ChannelStock.Quantity + ReturnQuantity,
 | 
						||
                            Id = ChannelStock.Id,
 | 
						||
                        }).UpdateColumns(it => new { it.Quantity }).ExecuteCommand();
 | 
						||
 | 
						||
                        // 获取更新完库存后的药品库存
 | 
						||
                        List<ChannelStock> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
                            .Where(cs => cs.MachineId.Equals(ChannelStock.MachineId))
 | 
						||
                            .Where(cs => cs.DrugId.Equals(ChannelStock.DrugId))
 | 
						||
                            .Where(cs => cs.DrawerType == 1)
 | 
						||
                            .ToList();
 | 
						||
 | 
						||
                        for (int i = 0; i < records.Count; i++)
 | 
						||
                        {
 | 
						||
                            MachineRecord _MachineRecord = records[i];
 | 
						||
                            // 更新数据 取药记录 设置还药数量、状态
 | 
						||
                            SqlSugarHelper.Db.Updateable(new MachineRecord()
 | 
						||
                            {
 | 
						||
                                ReturnQuantity2 = _MachineRecord.Quantity - _MachineRecord.ReturnQuantity1,
 | 
						||
                                Id = _MachineRecord.Id,
 | 
						||
                                Status = 2,
 | 
						||
                            }).UpdateColumns(it => new { it.ReturnQuantity2, it.Status }).ExecuteCommand();
 | 
						||
 | 
						||
                            // 保存数据 还药空瓶记录
 | 
						||
                            SqlSugarHelper.Db.Insertable(new MachineRecord()
 | 
						||
                            {
 | 
						||
                                MachineId = ChannelStock.MachineId,
 | 
						||
                                DrawerNo = ChannelStock.DrawerNo,
 | 
						||
                                ColNo = ChannelStock.ColNo,
 | 
						||
                                DrugId = ChannelStock.DrugId,
 | 
						||
                                ManuNo = ChannelStock.ManuNo,
 | 
						||
                                EffDate = !String.IsNullOrEmpty(ChannelStock.EffDate) ? DateTime.ParseExact(ChannelStock.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                                Operator = HomeWindowViewModel.Operator?.Id,
 | 
						||
                                Reviewer = HomeWindowViewModel.Reviewer?.Id,
 | 
						||
                                OperationTime = DateTime.Now,
 | 
						||
                                Quantity = _MachineRecord.Quantity, //ReturnQuantity,
 | 
						||
                                Type = 32,
 | 
						||
                                InvoiceId = InvoiceId,
 | 
						||
                                GetId = _MachineRecord.Id
 | 
						||
                                //,StockQuantity = nowChannels.Sum(it => it.Quantity)
 | 
						||
                            }).ExecuteCommand();
 | 
						||
                        }
 | 
						||
 | 
						||
                        return true;
 | 
						||
                    });
 | 
						||
                    if (f.Data)
 | 
						||
                    {
 | 
						||
                        // 更新屏显库存
 | 
						||
                        if (ChannelStock.BoardType == 5)
 | 
						||
                        {
 | 
						||
                            _portUtil.WriteQuantity(ChannelStock.DrawerNo, ChannelStock.ColNo, ChannelStock.Quantity + ReturnQuantity);
 | 
						||
                        }
 | 
						||
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "操作完成,库存已更新",
 | 
						||
                            Type = MsgType.SUCCESS,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    }
 | 
						||
                    if (!f.IsSuccess)
 | 
						||
                    {
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "库存更新失败!",
 | 
						||
                            Type = MsgType.ERROR,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    }
 | 
						||
                    Status = 0;
 | 
						||
                    IsFinishClick = false;
 | 
						||
                    RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
 | 
						||
                } else
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "选择的归还数目与实际数目不符",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    IsFinishClick = false;
 | 
						||
                }
 | 
						||
            }, () => !IsFinishClick && ReturnQuantity > 0).ObservesProperty(() => IsFinishClick).ObservesProperty(() => ReturnQuantity);
 | 
						||
        }
 | 
						||
 | 
						||
        public long CurrentTimeMillis()
 | 
						||
        {
 | 
						||
            return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
 | 
						||
        }
 | 
						||
 | 
						||
        // 取消按钮
 | 
						||
        public DelegateCommand CancleTake
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                IsFinishClick = false;
 | 
						||
                _portUtil.ResetData();
 | 
						||
                Status = 0;
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
        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));
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
        public bool KeepAlive => false;
 | 
						||
    }
 | 
						||
}
 |