827 lines
		
	
	
		
			36 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			827 lines
		
	
	
		
			36 KiB
		
	
	
	
		
			C#
		
	
	
	
using DM_Weight.Models;
 | 
						||
using DM_Weight.msg;
 | 
						||
using DM_Weight.Port;
 | 
						||
using DM_Weight.Report;
 | 
						||
using DM_Weight.select;
 | 
						||
using DM_Weight.util;
 | 
						||
using log4net;
 | 
						||
using log4net.Repository.Hierarchy;
 | 
						||
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;
 | 
						||
using System.Threading.Tasks;
 | 
						||
using System.Windows.Data;
 | 
						||
 | 
						||
namespace DM_Weight.ViewModels
 | 
						||
{
 | 
						||
    public class CheckStockNewWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
 | 
						||
    {
 | 
						||
        public static List<UseFor> SelectUseList = new()
 | 
						||
        {
 | 
						||
 | 
						||
            new UseFor { UseId = 96, UseName = "不入账册" },
 | 
						||
            new UseFor{UseId=0,UseName="入账册"}
 | 
						||
        };
 | 
						||
        private List<UseFor> _selectsUseFor = SelectUseList;
 | 
						||
 | 
						||
        public List<UseFor> SelectsUseFor
 | 
						||
        {
 | 
						||
            get { return _selectsUseFor; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selectsUseFor, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
        private UseFor _selectedItemUseFor = SelectUseList[0];
 | 
						||
        /// <summary>
 | 
						||
        /// 取药或加药用途字段
 | 
						||
        /// </summary>
 | 
						||
        public UseFor SelectedItemUseFor
 | 
						||
        {
 | 
						||
            get { return _selectedItemUseFor; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selectedItemUseFor, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
        //public static CheckStockNewWindowViewModel vm;
 | 
						||
        private readonly ILog logger = LogManager.GetLogger(typeof(CheckStockNewWindowViewModel));
 | 
						||
        public string drugId;
 | 
						||
        //public string Stock;
 | 
						||
        private static readonly DateTime Jan1st1970 = new DateTime
 | 
						||
    (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 | 
						||
        public static List<OrderTakeSelect> StaticSelects = new()
 | 
						||
        {
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "DrugName",
 | 
						||
                Name = "药品名称"
 | 
						||
            },
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "PyCode",
 | 
						||
                Name = "拼音码"
 | 
						||
            },
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "DrugBarcode",
 | 
						||
                Name = "药品条码"
 | 
						||
            },
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "DrugId",
 | 
						||
                Name = "药品编码"
 | 
						||
            }
 | 
						||
        };
 | 
						||
 | 
						||
        private List<OrderTakeSelect> _selects = StaticSelects;
 | 
						||
 | 
						||
        public List<OrderTakeSelect> Selects
 | 
						||
        {
 | 
						||
            get { return _selects; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selects, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private OrderTakeSelect _selectedItem = StaticSelects[0];
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件 查询字段
 | 
						||
        /// </summary>
 | 
						||
        public OrderTakeSelect SelectedItem
 | 
						||
        {
 | 
						||
            get { return _selectedItem; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selectedItem, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private string? _searchValue;
 | 
						||
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件 查询字段值
 | 
						||
        /// </summary>
 | 
						||
        public string? SearchValue
 | 
						||
        {
 | 
						||
            get { return _searchValue; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _searchValue, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
        //private DateTime? _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
 | 
						||
 | 
						||
        //public DateTime? StartDate
 | 
						||
        //{
 | 
						||
        //    get => _startDate;
 | 
						||
        //    set
 | 
						||
        //    {
 | 
						||
        //        if (value != null)
 | 
						||
        //        {
 | 
						||
        //            SetProperty(ref _startDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0));
 | 
						||
        //        }
 | 
						||
        //        else
 | 
						||
        //        {
 | 
						||
        //            SetProperty(ref _startDate, value);
 | 
						||
        //        }
 | 
						||
        //        RequestData();
 | 
						||
        //    }
 | 
						||
        //}
 | 
						||
 | 
						||
        //private DateTime? _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
 | 
						||
 | 
						||
        //public DateTime? EndDate
 | 
						||
        //{
 | 
						||
        //    get => _endDate;
 | 
						||
        //    set
 | 
						||
        //    {
 | 
						||
        //        if (value != null)
 | 
						||
        //        {
 | 
						||
        //            TimeSpan ershisi = new TimeSpan(23, 59, 59);
 | 
						||
        //            SetProperty(ref _endDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0, 23, 59, 59));
 | 
						||
        //        }
 | 
						||
        //        else
 | 
						||
        //        {
 | 
						||
        //            SetProperty(ref _endDate, value);
 | 
						||
        //        }
 | 
						||
 | 
						||
        //        RequestData();
 | 
						||
        //    }
 | 
						||
        //}
 | 
						||
 | 
						||
        IDialogService _dialogService;
 | 
						||
        IEventAggregator _eventAggregator;
 | 
						||
        ////private SqlSugarScope SqlSugarHelper.Db;
 | 
						||
        private PortUtil _portUtil;
 | 
						||
        private int _status;
 | 
						||
 | 
						||
        public int Status { get => _status; set => SetProperty(ref _status, value); }
 | 
						||
 | 
						||
        private int _drawerNo = 1;
 | 
						||
 | 
						||
        public int DrawerNo
 | 
						||
        {
 | 
						||
            get => _drawerNo;
 | 
						||
            set => SetProperty(ref _drawerNo, value);
 | 
						||
        }
 | 
						||
 | 
						||
        public long CurrentTimeMillis()
 | 
						||
        {
 | 
						||
            return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
 | 
						||
        }
 | 
						||
 | 
						||
        public CheckStockNewWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator, IDialogService dialogService)
 | 
						||
        {
 | 
						||
            _dialogService = dialogService;
 | 
						||
            ////this.SqlSugarHelper.Db = sqlSugarScope;
 | 
						||
            _portUtil = portUtil;
 | 
						||
            _portUtil = portUtil;
 | 
						||
            _eventAggregator = eventAggregator;
 | 
						||
            //vm= this;
 | 
						||
        }
 | 
						||
        void DoMyPrismEvent(DeviceMsg msg)
 | 
						||
        {
 | 
						||
 | 
						||
            if (msg.WindowName == "CheckStockNewWindow")
 | 
						||
            {
 | 
						||
                IGrouping<int, ChannelStock> grouping = enumerator.Current;
 | 
						||
                int DrawerNo = grouping.Key;// Convert.ToInt32(grouping.Key.Substring(0, grouping.Key.IndexOf('-')));
 | 
						||
                List<ChannelStock> channelStocks = grouping.ToList();
 | 
						||
 | 
						||
                switch (msg.EventType)
 | 
						||
                {
 | 
						||
                    // 抽屉打开
 | 
						||
                    case EventType.DRAWEROPEN:
 | 
						||
 | 
						||
 | 
						||
                        if (Status == 1)
 | 
						||
                        {
 | 
						||
                            if (channelStocks[0].process == 1)
 | 
						||
                            {
 | 
						||
                                channelStocks.ForEach(it => it.process = 2);
 | 
						||
                            }
 | 
						||
                            //Status = 2;
 | 
						||
                        }
 | 
						||
 | 
						||
                        break;
 | 
						||
                    // 抽屉关闭
 | 
						||
                    case EventType.DRAWERCLOSE:
 | 
						||
                        if (Status == 1)
 | 
						||
                        {
 | 
						||
                            if (channelStocks[0].process == 2)
 | 
						||
                            {
 | 
						||
                                channelStocks.ForEach(it => it.process = 3);
 | 
						||
                            }
 | 
						||
                            IGrouping<int, ChannelStock> groupingBefore = enumerator.Current;
 | 
						||
                            int DrawerNoBefore = groupingBefore.Key; //Convert.ToInt32(groupingBefore.Key.Substring(0, groupingBefore.Key.IndexOf('-')));
 | 
						||
                            if (enumerator.MoveNext())
 | 
						||
                            {
 | 
						||
                                IGrouping<int, ChannelStock> groupingAfter = enumerator.Current;
 | 
						||
                                int DrawerNoAfter = groupingAfter.Key;//Convert.ToInt32(groupingAfter.Key.Substring(0, groupingAfter.Key.IndexOf('-')));
 | 
						||
                                //if (DrawerNoBefore < 9 && DrawerNoAfter > 8)
 | 
						||
                                //{
 | 
						||
                                //    Thread.Sleep(50);
 | 
						||
                                //}
 | 
						||
 | 
						||
                                logger.Info($"抽屉号DrawerNoBefore【{DrawerNoBefore}】抽屉号DrawerNoAfter【{DrawerNoAfter}】");
 | 
						||
                                if (DrawerNoAfter == 17 || DrawerNoBefore == 17)
 | 
						||
                                {
 | 
						||
                                    //if (DrawerNoBefore == 17)
 | 
						||
                                    {
 | 
						||
                                        int sleepMilliseconds = Convert.ToInt32(ConfigurationManager.AppSettings["CheckSleepMilliseconds"] ?? "500");
 | 
						||
                                        Thread.Sleep(sleepMilliseconds);
 | 
						||
                                    }
 | 
						||
                                    //else
 | 
						||
                                    //{
 | 
						||
                                    //    Thread.Sleep(500);
 | 
						||
                                    //}
 | 
						||
                                }
 | 
						||
                                else
 | 
						||
                                {
 | 
						||
                                    //Thread.Sleep(80);
 | 
						||
                                    Thread.Sleep(500);
 | 
						||
                                }
 | 
						||
                                //logger.Info($"抽屉号DrawerNoBefore【{DrawerNoBefore}】抽屉号DrawerNoAfter【{DrawerNoAfter}】");
 | 
						||
                                OpenOneByOne();
 | 
						||
                            }
 | 
						||
                            // 已经全部取出
 | 
						||
                            else
 | 
						||
                            {
 | 
						||
                                Status = 3;
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                        break;
 | 
						||
                    // 数量变化
 | 
						||
                    case EventType.UPDATEQUANTITY:
 | 
						||
                        if (Status == 1)
 | 
						||
                        {
 | 
						||
                            logger.Info($"抽屉【{DrawerNo}】库位药品数量【{msg.Quantitys}】");
 | 
						||
                        }
 | 
						||
                        break;
 | 
						||
                    // 打开失败
 | 
						||
                    case EventType.OPENERROR:
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = msg.Message,
 | 
						||
                            Type = MsgType.ERROR,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                        Status = 0;
 | 
						||
                        break;
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
        }
 | 
						||
        private ChannelStock? _selectedChannel;
 | 
						||
 | 
						||
        public ChannelStock? SelectedChannel
 | 
						||
        {
 | 
						||
            get => _selectedChannel;
 | 
						||
            set => SetProperty(ref _selectedChannel, value);
 | 
						||
        }
 | 
						||
 | 
						||
        private List<ChannelStock>? _channelStocks = new();
 | 
						||
 | 
						||
        public List<ChannelStock>? ChannelStocks
 | 
						||
        {
 | 
						||
            get => _channelStocks;
 | 
						||
            set => SetProperty(ref _channelStocks, value);
 | 
						||
        }
 | 
						||
 | 
						||
        private IEnumerable<IGrouping<int, ChannelStock>> enumerable;
 | 
						||
        private IEnumerator<IGrouping<int, ChannelStock>> enumerator;
 | 
						||
 | 
						||
        public DelegateCommand BindingChannelDialog
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                DialogParameters dialogParameters = new DialogParameters();
 | 
						||
                DialogServiceExtensions.ShowDialogHost(_dialogService, "BindingChannelDialog", dialogParameters, DoDialogResult, "RootDialog");
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        public DelegateCommand QueryCommand
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                RequestData();
 | 
						||
            });
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 盘点
 | 
						||
        /// </summary>
 | 
						||
        public DelegateCommand OpenDrawer
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                if (Status == 0)
 | 
						||
                {
 | 
						||
                    enumerable = ChannelStocks.GroupBy(cs => cs.DrawerNo, cs => cs);
 | 
						||
                    //enumerable = ChannelStocks.GroupBy(cs => cs.Location, cs => cs);
 | 
						||
                    enumerator = enumerable.GetEnumerator();
 | 
						||
                    enumerator.MoveNext();
 | 
						||
                    Status = 1;
 | 
						||
                    OpenOneByOne();
 | 
						||
                }
 | 
						||
            });
 | 
						||
        }
 | 
						||
        private void OpenOneByOne()
 | 
						||
        {
 | 
						||
            IGrouping<int, ChannelStock> grouping = enumerator.Current;
 | 
						||
            //int DrawerNo =Convert.ToInt32(grouping.Key.Substring(0,grouping.Key.IndexOf('-')));
 | 
						||
            int DrawerNo = grouping.Key;
 | 
						||
 | 
						||
            if (DrawerNo <= 8)
 | 
						||
            {
 | 
						||
                if (App.CurrentFaUserList.Role.FirstEightChecked == "0")
 | 
						||
                {
 | 
						||
                    NoAuthority();
 | 
						||
                }
 | 
						||
            }
 | 
						||
            if (DrawerNo > 8 && DrawerNo != 17)
 | 
						||
            {
 | 
						||
                if (App.CurrentFaUserList.Role.LastEightChecked == "0")
 | 
						||
                {
 | 
						||
                    NoAuthority();
 | 
						||
                }
 | 
						||
            }
 | 
						||
            if (DrawerNo == 17)
 | 
						||
            {
 | 
						||
                if (App.CurrentFaUserList.Role.StorageBoxChecked == "0")
 | 
						||
                {
 | 
						||
                    NoAuthority();
 | 
						||
                }
 | 
						||
            }
 | 
						||
            List<ChannelStock> Stocks = grouping.ToList();
 | 
						||
            Stocks.ForEach(it => it.process = 1);
 | 
						||
            _portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
 | 
						||
            List<ChannelStock> singleChannels = new List<ChannelStock>();
 | 
						||
            for (int i = 0; i < Stocks.Count; i++)
 | 
						||
            {
 | 
						||
                ChannelStock copy = TransExpV2<ChannelStock, ChannelStock>.Trans(Stocks[i]);
 | 
						||
                singleChannels.Add(copy);
 | 
						||
            }
 | 
						||
            singleChannels = singleChannels.GroupBy(it => new
 | 
						||
            {
 | 
						||
                it.DrawerNo,
 | 
						||
                it.ColNo
 | 
						||
            }).Select(it =>
 | 
						||
            {
 | 
						||
                var ret = it.First();
 | 
						||
                //ret.Quantity = it.Sum(itx => itx.Quantity);
 | 
						||
                //ret.AddQuantity = it.Sum(itx => itx.AddQuantity);
 | 
						||
                return ret;
 | 
						||
            }).ToList().FindAll(it => it.BoardType != 1);
 | 
						||
 | 
						||
            // 发送取药数量
 | 
						||
            //singleChannels.ForEach(it =>
 | 
						||
            //{
 | 
						||
            //    try
 | 
						||
            //    {
 | 
						||
 | 
						||
            //        _portUtil.TakeQuantity(DrawerNo, it.ColNo, it.TakeQuantity, it.Quantity - it.TakeQuantity);
 | 
						||
            //    }
 | 
						||
            //    catch (Exception ex)
 | 
						||
            //    {
 | 
						||
            //        AlertMsg alertMsg = new AlertMsg
 | 
						||
            //        {
 | 
						||
            //            Message = $"打开抽屉异常{ex.Message}",
 | 
						||
            //            Type = MsgType.ERROR,
 | 
						||
            //        };
 | 
						||
            //        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
            //    }
 | 
						||
            //});
 | 
						||
            _portUtil.WindowName = "CheckStockNewWindow";
 | 
						||
            _portUtil.Operate = true;
 | 
						||
            _portUtil.BoardType = singleChannels.Count > 0 ? singleChannels[0].BoardType : 1;
 | 
						||
            _portUtil.ColNos = singleChannels.Select(it => it.ColNo).ToArray();
 | 
						||
            //_portUtil.Stocks = singleChannels.Select(it => it.Quantity).ToArray();
 | 
						||
            _portUtil.DrawerNo = DrawerNo;
 | 
						||
            _portUtil.Start();
 | 
						||
        }
 | 
						||
 | 
						||
        private bool _isFinishClick = false;
 | 
						||
 | 
						||
        // 完成按钮
 | 
						||
        public DelegateCommand TakeFinish
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                _isFinishClick = true;
 | 
						||
                //List<ChannelStock> record = ChannelStocks.FindAll(it => it.Quantity != it.CheckQuantity).ToList();
 | 
						||
                List<ChannelStock> record = ChannelStocks;
 | 
						||
                if (record.Count > 0)
 | 
						||
                {
 | 
						||
                    string InvoiceId = "CHECK_" + CurrentTimeMillis();
 | 
						||
                    var f = SqlSugarHelper.Db.UseTran(() =>
 | 
						||
                    {
 | 
						||
                        for (int i = 0; i < record.Count; i++)
 | 
						||
                        {
 | 
						||
                            ChannelStock it = record[i];
 | 
						||
 | 
						||
 | 
						||
                            // 更新数据 库存信息
 | 
						||
                            SqlSugarHelper.Db.Updateable(new ChannelStock()
 | 
						||
                            {
 | 
						||
                                Quantity = it.CheckQuantity,
 | 
						||
                                ManuNo = it.ManuNo,
 | 
						||
                                EffDate = it.EffDate,
 | 
						||
                                Id = it.Id,
 | 
						||
                            }).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate }).ExecuteCommand();
 | 
						||
                            // 获取更新完库存后的药品库存
 | 
						||
                            List<ChannelStock> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
                                .Where(cs => cs.MachineId.Equals(it.MachineId))
 | 
						||
                                .Where(cs => cs.DrugId.Equals(it.DrugId))
 | 
						||
                                .Where(cs => cs.DrawerType == 1)
 | 
						||
                                .ToList();
 | 
						||
 | 
						||
                            //盘点时不记录盘点库存为0且盘点后库存仍为0的记录(该批次可能不再用了)
 | 
						||
                            if (!it.Quantity.Equals(0) || !it.CheckQuantity.Equals(0))
 | 
						||
                            {
 | 
						||
                                // 保存数据 盘点记录
 | 
						||
                                SqlSugarHelper.Db.Insertable(new MachineRecord()
 | 
						||
                                {
 | 
						||
                                    MachineId = it.MachineId,
 | 
						||
                                    DrawerNo = it.DrawerNo,
 | 
						||
                                    ColNo = it.ColNo,
 | 
						||
                                    DrugId = it.DrugId,
 | 
						||
                                    ManuNo = it.ManuNo,
 | 
						||
                                    EffDate = !String.IsNullOrEmpty(it.EffDate) ? DateTime.ParseExact(it.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                                    Operator = HomeWindowViewModel.Operator?.Id,
 | 
						||
                                    Reviewer = HomeWindowViewModel.Reviewer?.Id,
 | 
						||
                                    OperationTime = DateTime.Now,
 | 
						||
                                    Quantity = it.CheckQuantity - it.Quantity,
 | 
						||
                                    Type = SelectedItemUseFor.UseId == 0 ? 4 : SelectedItemUseFor.UseId,
 | 
						||
                                    InvoiceId = InvoiceId,
 | 
						||
                                    StockQuantity = nowChannels.Sum(it => it.Quantity),
 | 
						||
                                    CheckQuantity = it.CheckQuantity,
 | 
						||
                                    ManunoQuantity = nowChannels.FindAll(it2 => it2.ManuNo == it.ManuNo).Sum(it => it.Quantity)
 | 
						||
                                }).ExecuteCommand();
 | 
						||
 | 
						||
                                logger.Info($"库存盘点->库位【{it.DrawerNo}-{it.ColNo}】药品【{it.DrugInfo.DrugName}】盘点前库存【{it.Quantity}】,更改后【{it.CheckQuantity}】");
 | 
						||
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
 | 
						||
                        //Task.Factory.StartNew(()=> SaveCheckRecord());
 | 
						||
                        string machine_id = (ConfigurationManager.AppSettings["machineId"] ?? "DM1");
 | 
						||
                        string strSql = $@"SELECT cl.`row_no` AS rowNo,cl.`col_no` AS colNo,cl.`quantity` AS quantity,cl.`manu_no` AS manuNo,cl.`eff_date` AS effDate,
 | 
						||
                          di.`drug_name` AS drugName,di.`drug_spec` AS drugSpec,di.`pack_unit` AS packUnit,di.`manufactory` AS manufactory,di.`max_stock` maxStock,
 | 
						||
                          cl.`drug_id` AS drugId,cl.`machine_id` AS MachineId  FROM channel_stock cl INNER JOIN drug_info di ON di.`drug_id` = cl.`drug_id` WHERE cl.`machine_id` =  '{machine_id}' AND cl.`drawer_type` = 1 ";
 | 
						||
                        if (App.SingleModel)
 | 
						||
                        {
 | 
						||
                            strSql += " AND ROW_NO<=8";
 | 
						||
                        }
 | 
						||
                        else
 | 
						||
                        {
 | 
						||
                            strSql += " AND ROW_NO>8";
 | 
						||
                        }
 | 
						||
                        strSql += " ORDER BY cl.`drug_id`";
 | 
						||
                        List<CheckRecordStock> checkRecordStockList = SqlSugarHelper.Db.SqlQueryable<dynamic>(strSql)
 | 
						||
                                                                           .AddParameters(new
 | 
						||
                                                                           {
 | 
						||
                                                                               machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1"
 | 
						||
                                                                           })
 | 
						||
                                                                          .Select(it => new CheckRecordStock())
 | 
						||
                                                                          .ToList();
 | 
						||
 | 
						||
                        logger.Info("已完成-查询channel_stock关联drug_info表信息查询");
 | 
						||
 | 
						||
                        List<CheckRecordStock> insertList = new List<CheckRecordStock>();
 | 
						||
 | 
						||
                        if (checkRecordStockList != null && checkRecordStockList.Count > 0)
 | 
						||
                        {
 | 
						||
                            for (int i = 0; i < checkRecordStockList.Count; i++)
 | 
						||
                            {
 | 
						||
                                if (!(insertList.Where(it => it.drugId == checkRecordStockList[i].drugId && it.manuNo == checkRecordStockList[i].manuNo).Count() > 0))
 | 
						||
                                {
 | 
						||
                                    insertList.Add(checkRecordStockList[i]);
 | 
						||
                                }
 | 
						||
                            }
 | 
						||
                            for (int j = 0; j < insertList.Count; j++)
 | 
						||
                            {
 | 
						||
 | 
						||
                                //盘点时不记录盘点库存为0且盘点后库存仍为0的记录(该批次可能不再用了)
 | 
						||
                                if (!insertList[j].quantity.Equals(0))
 | 
						||
                                {
 | 
						||
                                    CheckRecordStock cStock = insertList[j];
 | 
						||
                                    // 保存数据 盘点后药品总库存及批次库存数
 | 
						||
                                    SqlSugarHelper.Db.Insertable(new CheckRecordStock()
 | 
						||
                                    {
 | 
						||
                                        rowNo = cStock.rowNo,
 | 
						||
                                        colNo = cStock.colNo,
 | 
						||
                                        ManuQuantity = checkRecordStockList.Where(it => it.drugId == cStock.drugId && it.manuNo == cStock.manuNo).Sum(it => it.quantity),
 | 
						||
                                        manuNo = cStock.manuNo,
 | 
						||
                                        effDate = cStock.effDate,
 | 
						||
                                        drugName = cStock.drugName,
 | 
						||
                                        manufactory = cStock.manufactory,
 | 
						||
                                        drugSpec = cStock.drugSpec,
 | 
						||
                                        packUnit = cStock.packUnit,
 | 
						||
                                        maxStock = cStock.maxStock,
 | 
						||
                                        drugId = cStock.drugId,
 | 
						||
                                        MachineId = cStock.MachineId,
 | 
						||
                                        quantity = checkRecordStockList.Where(it => it.drugId == cStock.drugId).Sum(it => it.quantity),
 | 
						||
                                        optdate = DateTime.Now.ToString(),
 | 
						||
                                        operatorUser = HomeWindowViewModel.Operator?.Id.ToString(),
 | 
						||
                                        reviewerUser = HomeWindowViewModel.Reviewer?.Id.ToString()
 | 
						||
                                    }).ExecuteCommand();
 | 
						||
                                }
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                        logger.Info("已完成-保存数据 盘点后药品总库存及批次库存数");
 | 
						||
                        return true;
 | 
						||
                    });
 | 
						||
                    if (f.Data)
 | 
						||
                    {
 | 
						||
                        // 更新屏显库存
 | 
						||
                        //List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 1);
 | 
						||
                        List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType == 5&&it.CheckQuantity>0).OrderBy(it=>it.EffDate)
 | 
						||
                        .GroupBy(it => new
 | 
						||
                        {
 | 
						||
                            it.DrawerNo,
 | 
						||
                            it.ColNo
 | 
						||
                        }).Select(it =>
 | 
						||
                        {
 | 
						||
                            var ret = it.First();
 | 
						||
                            ret.CheckQuantity = it.Sum(itx => itx.CheckQuantity);
 | 
						||
                            //ret.AddQuantity = it.Sum(itx => itx.AddQuantity);
 | 
						||
                            return ret;
 | 
						||
                        }).ToList();
 | 
						||
                        //if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 5)
 | 
						||
                        {
 | 
						||
                            singleChannels.ForEach(it =>
 | 
						||
                            {
 | 
						||
                                //将库位多批次的总库存数更新标签
 | 
						||
                                _portUtil.WriteChannelInfo(6, it.EffDate == null ? "" : it.EffDate, it.DrawerNo, it.ColNo);
 | 
						||
                                Thread.Sleep(200);
 | 
						||
                                _portUtil.WriteChannelInfo(5, it.ManuNo, it.DrawerNo, it.ColNo);
 | 
						||
                                Thread.Sleep(200);
 | 
						||
                                // 将库位多批次的总库存数更新标签
 | 
						||
                                _portUtil.WriteQuantity(it.DrawerNo, it.ColNo, it.CheckQuantity);
 | 
						||
                                Thread.Sleep(200);
 | 
						||
                                _portUtil.ShowContent(it.DrawerNo, it.ColNo);
 | 
						||
                                Thread.Sleep(200);
 | 
						||
 | 
						||
                            });
 | 
						||
                        }
 | 
						||
                        logger.Info("已完成-更新标签");
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "抽屉盘点完成,库存已更新",
 | 
						||
                            Type = MsgType.SUCCESS,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
 | 
						||
                        RequestData();
 | 
						||
                    }
 | 
						||
                    if (!f.IsSuccess)
 | 
						||
                    {
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "抽屉盘点完成,库存更新失败!",
 | 
						||
                            Type = MsgType.ERROR,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    }
 | 
						||
                    Status = 0;
 | 
						||
                    _isFinishClick = false;
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    _isFinishClick = false;
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "盘点完成,库存无改变",
 | 
						||
                        Type = MsgType.ERROR
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    Status = 0;
 | 
						||
                    _isFinishClick = false;
 | 
						||
                }
 | 
						||
 | 
						||
 | 
						||
            }, () => Status == 3 && !_isFinishClick).ObservesProperty(() => Status);
 | 
						||
        }
 | 
						||
        public void SaveCheckRecord()
 | 
						||
        {
 | 
						||
            string machine_id = (ConfigurationManager.AppSettings["machineId"] ?? "DM1");
 | 
						||
            string strSql = $@"SELECT cl.`row_no` AS rowNo,cl.`col_no` AS colNo,cl.`quantity` AS quantity,cl.`manu_no` AS manuNo,cl.`eff_date` AS effDate,
 | 
						||
                          di.`drug_name` AS drugName,di.`drug_spec` AS drugSpec,di.`pack_unit` AS packUnit,di.`manufactory` AS manufactory,di.`max_stock` maxStock,
 | 
						||
                          cl.`drug_id` AS drugId,cl.`machine_id` AS MachineId  FROM channel_stock cl INNER JOIN drug_info di ON di.`drug_id` = cl.`drug_id` WHERE cl.`machine_id` =  '{machine_id}' AND cl.`drawer_type` = 1 ORDER BY cl.`drug_id`";
 | 
						||
            List<CheckRecordStock> checkRecordStockList = SqlSugarHelper.Db.SqlQueryable<dynamic>(strSql)
 | 
						||
                                                               .AddParameters(new
 | 
						||
                                                               {
 | 
						||
                                                                   machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1"
 | 
						||
                                                               })
 | 
						||
                                                              .Select(it => new CheckRecordStock())
 | 
						||
                                                              .ToList();
 | 
						||
 | 
						||
            logger.Info("已完成-查询channel_stock关联drug_info表信息查询");
 | 
						||
 | 
						||
            List<CheckRecordStock> insertList = new List<CheckRecordStock>();
 | 
						||
 | 
						||
            if (checkRecordStockList != null && checkRecordStockList.Count > 0)
 | 
						||
            {
 | 
						||
                for (int i = 0; i < checkRecordStockList.Count; i++)
 | 
						||
                {
 | 
						||
                    if (!(insertList.Where(it => it.drugId == checkRecordStockList[i].drugId && it.manuNo == checkRecordStockList[i].manuNo).Count() > 0))
 | 
						||
                    {
 | 
						||
                        insertList.Add(checkRecordStockList[i]);
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                for (int j = 0; j < insertList.Count; j++)
 | 
						||
                {
 | 
						||
 | 
						||
 | 
						||
                    CheckRecordStock cStock = insertList[j];
 | 
						||
                    // 保存数据 盘点后药品总库存及批次库存数
 | 
						||
                    SqlSugarHelper.Db.Insertable(new CheckRecordStock()
 | 
						||
                    {
 | 
						||
                        rowNo = cStock.rowNo,
 | 
						||
                        colNo = cStock.colNo,
 | 
						||
                        ManuQuantity = checkRecordStockList.Where(it => it.drugId == cStock.drugId && it.manuNo == cStock.manuNo).Sum(it => it.quantity),
 | 
						||
                        manuNo = cStock.manuNo,
 | 
						||
                        effDate = cStock.effDate,
 | 
						||
                        drugName = cStock.drugName,
 | 
						||
                        manufactory = cStock.manufactory,
 | 
						||
                        drugSpec = cStock.drugSpec,
 | 
						||
                        packUnit = cStock.packUnit,
 | 
						||
                        maxStock = cStock.maxStock,
 | 
						||
                        drugId = cStock.drugId,
 | 
						||
                        MachineId = cStock.MachineId,
 | 
						||
                        quantity = checkRecordStockList.Where(it => it.drugId == cStock.drugId).Sum(it => it.quantity),
 | 
						||
                        optdate = DateTime.Now.ToString(),
 | 
						||
                        operatorUser = HomeWindowViewModel.Operator?.Id.ToString(),
 | 
						||
                        reviewerUser = HomeWindowViewModel.Reviewer?.Id.ToString()
 | 
						||
                    }).ExecuteCommand();
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
 | 
						||
            logger.Info("已完成-插入check_stock表信息");
 | 
						||
        }
 | 
						||
        // 取消按钮
 | 
						||
        public DelegateCommand CancleTake
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                _portUtil.ResetData();
 | 
						||
                Status = 0;
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
        public bool KeepAlive => false;
 | 
						||
 | 
						||
        private void DoDialogResult(IDialogResult dialogResult)
 | 
						||
        {
 | 
						||
            // 委托   被动执行     被子窗口执行
 | 
						||
            // dialogResult  第一方面可以拿到任意参数   第二方面   可判断关闭状态
 | 
						||
            //if (dialogResult.Result == ButtonResult.OK)
 | 
						||
            //{
 | 
						||
            RequestData();
 | 
						||
            //}
 | 
						||
            //MessageBox.Show("返回值:" + dialogResult.Result.ToString());
 | 
						||
        }
 | 
						||
        ///// <summary>
 | 
						||
        ///// 导出账册
 | 
						||
        ///// </summary>
 | 
						||
        //public DelegateCommand DownloadAccountBook
 | 
						||
        //{
 | 
						||
        //    get => new DelegateCommand(() =>
 | 
						||
        //    {
 | 
						||
        //        if (!string.IsNullOrEmpty(Stock))
 | 
						||
        //        {
 | 
						||
        //            //根据库位信息查询出药品id
 | 
						||
        //            string[] strStock = Stock.Split('-');
 | 
						||
        //            if (strStock.Length >= 2)
 | 
						||
        //            {
 | 
						||
        //                //DrugId = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerNo == Convert.ToInt32(strStock[0]) && cs.ColNo == Convert.ToInt32(strStock[1])).Select(cs => cs.DrugId).ToString();
 | 
						||
        //                var list = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerNo == Convert.ToInt32(strStock[0]) && cs.ColNo == Convert.ToInt32(strStock[1]) && cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).ToList();
 | 
						||
        //                if (list != null && list.Count > 0)
 | 
						||
        //                {
 | 
						||
        //                    drugId = list[0].DrugId;
 | 
						||
        //                }
 | 
						||
        //            }
 | 
						||
        //        }
 | 
						||
        //        GridReportUtil.PrintReportAccountBook(StartDate, EndDate, drugId ?? "");
 | 
						||
        //    });
 | 
						||
        //}
 | 
						||
        //public DelegateCommand DownLoadCommond
 | 
						||
        //{
 | 
						||
        //    get => new DelegateCommand(() =>
 | 
						||
        //    {
 | 
						||
        //        GridReportUtil.PrintReportStockNew(StartDate, EndDate);
 | 
						||
        //    });
 | 
						||
        //}
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
        //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作
 | 
						||
        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
 | 
						||
        {
 | 
						||
            continuationCallback(true);
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        public void RequestData()
 | 
						||
        {
 | 
						||
            ChannelStocks.Clear();
 | 
						||
 | 
						||
            ChannelStocks.Clear();
 | 
						||
 | 
						||
            List<ChannelStock> q = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
                .Includes<DrugInfo>(cs => cs.DrugInfo)
 | 
						||
                .InnerJoin<ChannelList>((cs, cl) => cs.Chnguid == cl.Id && cs.DrugId == cl.DrugId)
 | 
						||
                .Where(cs => cs.DrawerType == 1)
 | 
						||
                .Where(cs => cs.DrugId != null)
 | 
						||
                .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
 | 
						||
                .WhereIF(App.SingleModel, cs => cs.DrawerNo <= 8) //单人登录开前8个抽屉,双人登录开后8个抽屉
 | 
						||
                .WhereIF(!(App.SingleModel), cs => cs.DrawerNo > 8)
 | 
						||
                .WhereIF(!String.IsNullOrEmpty(SearchValue), cs => cs.DrugInfo.DrugName == SearchValue)
 | 
						||
                .OrderBy(cs => cs.DrawerNo)
 | 
						||
                //.OrderByDescending(cs => cs.DrawerNo)
 | 
						||
                .OrderBy(cs => cs.ColNo)
 | 
						||
                //.OrderBy(cs => cs.DrugId)
 | 
						||
                .ToList();
 | 
						||
            ChannelStocks = q.Select(it => { it.CheckQuantity = it.Quantity; return it; }).ToList();
 | 
						||
            ChannelStocks = q;
 | 
						||
 | 
						||
 | 
						||
        }
 | 
						||
 | 
						||
        //接收导航传过来的参数  现在是在此处初始化了表格数据
 | 
						||
        public void OnNavigatedTo(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            _eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
 | 
						||
            Task.Factory.StartNew(() => { RequestData(); });
 | 
						||
            //RequestData();
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
 | 
						||
        public bool IsNavigationTarget(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            return true;
 | 
						||
        }
 | 
						||
 | 
						||
        //这个方法用于拦截请求
 | 
						||
        public void OnNavigatedFrom(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            // 取消消息订阅
 | 
						||
            _eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
 | 
						||
        }
 | 
						||
 | 
						||
        //无权限,进入下一个抽屉
 | 
						||
        private void NoAuthority()
 | 
						||
        {
 | 
						||
 | 
						||
            IGrouping<int, ChannelStock> groupingBefore = enumerator.Current;
 | 
						||
            int DrawerNoBefore = groupingBefore.Key; //Convert.ToInt32(groupingBefore.Key.Substring(0, groupingBefore.Key.IndexOf('-')));
 | 
						||
            if (enumerator.MoveNext())
 | 
						||
            {
 | 
						||
                IGrouping<int, ChannelStock> groupingAfter = enumerator.Current;
 | 
						||
                int DrawerNoAfter = groupingAfter.Key;//Convert.ToInt32(groupingAfter.Key.Substring(0, groupingAfter.Key.IndexOf('-')));
 | 
						||
                                                      //if (DrawerNoBefore < 9 && DrawerNoAfter > 8)
 | 
						||
                                                      //{
 | 
						||
                                                      //    Thread.Sleep(50);
 | 
						||
                                                      //}
 | 
						||
 | 
						||
                logger.Info($"抽屉号DrawerNoBefore【{DrawerNoBefore}】抽屉号DrawerNoAfter【{DrawerNoAfter}】");
 | 
						||
                if (DrawerNoAfter == 17 || DrawerNoBefore == 17)
 | 
						||
                {
 | 
						||
                    //if (DrawerNoBefore == 17)
 | 
						||
                    {
 | 
						||
                        int sleepMilliseconds = Convert.ToInt32(ConfigurationManager.AppSettings["CheckSleepMilliseconds"] ?? "500");
 | 
						||
                        Thread.Sleep(sleepMilliseconds);
 | 
						||
                    }
 | 
						||
                    //else
 | 
						||
                    //{
 | 
						||
                    //    Thread.Sleep(500);
 | 
						||
                    //}
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    //Thread.Sleep(80);
 | 
						||
                    Thread.Sleep(500);
 | 
						||
                }
 | 
						||
                OpenOneByOne();
 | 
						||
            }
 | 
						||
        }
 | 
						||
    }
 | 
						||
} |