810 lines
		
	
	
		
			37 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			810 lines
		
	
	
		
			37 KiB
		
	
	
	
		
			C#
		
	
	
	
using DM_Weight.Models;
 | 
						||
using DM_Weight.msg;
 | 
						||
using DM_Weight.Port;
 | 
						||
using DM_Weight.select;
 | 
						||
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;
 | 
						||
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.Documents;
 | 
						||
 | 
						||
namespace DM_Weight.ViewModels
 | 
						||
{
 | 
						||
    public class InvoiceInNewWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
 | 
						||
    {
 | 
						||
        private readonly ILog logger = LogManager.GetLogger(typeof(InvoiceInWindowViewModel));
 | 
						||
        private int _pageNum = 1;
 | 
						||
        public int PageNum
 | 
						||
        {
 | 
						||
            get => _pageNum;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _pageNum, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _pageCount = 1;
 | 
						||
        public int PageCount
 | 
						||
        {
 | 
						||
            get => _pageCount;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _pageCount, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _pageSize = 8;
 | 
						||
        public int PageSize
 | 
						||
        {
 | 
						||
            get => _pageSize;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _pageSize, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _totalCount = 0;
 | 
						||
        public int TotalCount
 | 
						||
        {
 | 
						||
            get => _totalCount;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _totalCount, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
        private int _status = 0;
 | 
						||
 | 
						||
        public int Status { get => _status; set => SetProperty(ref _status, value); }
 | 
						||
 | 
						||
        IDialogService _dialogService;
 | 
						||
 | 
						||
 | 
						||
        IEventAggregator _eventAggregator;
 | 
						||
 | 
						||
        private DelegateCommand _rowSelected;
 | 
						||
 | 
						||
        public DelegateCommand RowSelected => _rowSelected ??= new DelegateCommand(GetChannelByInvoice);
 | 
						||
        ////private SqlSugarScope SqlSugarHelper.Db;
 | 
						||
        private PortUtil _portUtil;
 | 
						||
        public InvoiceInNewWindowViewModel(PortUtil portUtil, IDialogService DialogService, IEventAggregator eventAggregator)
 | 
						||
        {
 | 
						||
            _portUtil = portUtil;
 | 
						||
            _dialogService = DialogService;
 | 
						||
            _eventAggregator = eventAggregator;
 | 
						||
            ////this.SqlSugarHelper.Db = sqlSugarScope;
 | 
						||
        }
 | 
						||
        void DoMyPrismEvent(DeviceMsg msg)
 | 
						||
        {
 | 
						||
 | 
						||
            if (msg.WindowName.Equals(WindowName))
 | 
						||
            {
 | 
						||
                IGrouping<int, ChannelStock> grouping = enumerator.Current;
 | 
						||
                int DrawerNo = grouping.Key;
 | 
						||
                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);
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
 | 
						||
                        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;
 | 
						||
                            if (enumerator.MoveNext())
 | 
						||
                            {
 | 
						||
                                IGrouping<int, ChannelStock> groupingAfter = enumerator.Current;
 | 
						||
                                int DrawerNoAfter = groupingAfter.Key;
 | 
						||
                                if (DrawerNoBefore < 9 && DrawerNoAfter > 8)
 | 
						||
                                {
 | 
						||
                                    Thread.Sleep(50);
 | 
						||
                                }
 | 
						||
                                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;
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
        }
 | 
						||
 | 
						||
        public static List<OrderTakeSelect> StaticOrderTakeSelects = new()
 | 
						||
        {
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "invoiceNo",
 | 
						||
                Name = "凭证单号"
 | 
						||
            }
 | 
						||
        };
 | 
						||
 | 
						||
        private List<OrderTakeSelect> _orderTakeSelects = StaticOrderTakeSelects;
 | 
						||
 | 
						||
        public List<OrderTakeSelect> OrderTakeSelects
 | 
						||
        {
 | 
						||
            get { return _orderTakeSelects; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _orderTakeSelects, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private OrderTakeSelect _selectedItem = StaticOrderTakeSelects[0];
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件 查询字段
 | 
						||
        /// </summary>
 | 
						||
        public OrderTakeSelect SelectedItem
 | 
						||
        {
 | 
						||
            get { return _selectedItem; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selectedItem, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private Invoice? _selectedInvoice;
 | 
						||
 | 
						||
        public Invoice? SelectedInvoice
 | 
						||
        {
 | 
						||
            get { return _selectedInvoice; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selectedInvoice, value);
 | 
						||
 | 
						||
                //OpenOrderDialog();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        private string _orderDate = DateTime.Now.ToString("yyyy-MM-dd");
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件  处方日期
 | 
						||
        /// </summary>
 | 
						||
        public string OrderDate
 | 
						||
        {
 | 
						||
            get { return _orderDate; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                if (!String.IsNullOrEmpty(value))
 | 
						||
                {
 | 
						||
                    SetProperty(ref _orderDate, DateTime.Parse(value).ToString("yyyy-MM-dd"));
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    SetProperty(ref _orderDate, value);
 | 
						||
                }
 | 
						||
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private string? _searchValue;
 | 
						||
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件 查询字段值
 | 
						||
        /// </summary>
 | 
						||
        public string? SearchValue
 | 
						||
        {
 | 
						||
            get { return _searchValue; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _searchValue, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private List<Invoice> _invoices = new();
 | 
						||
 | 
						||
        public List<Invoice> Invoices { get { return _invoices; } set { SetProperty(ref _invoices, value); } }
 | 
						||
 | 
						||
 | 
						||
        private List<InOutInvoice> _inOutInvoices = new();
 | 
						||
 | 
						||
        public List<InOutInvoice> InOutInvoices { get { return _inOutInvoices; } set { SetProperty(ref _inOutInvoices, value); } }
 | 
						||
 | 
						||
 | 
						||
        private ObservableCollection<ChannelList>? _channelLsts = new();
 | 
						||
 | 
						||
        public ObservableCollection<ChannelList>? ChannelLsts
 | 
						||
        {
 | 
						||
            get => _channelLsts;
 | 
						||
            set => SetProperty(ref _channelLsts, value);
 | 
						||
        }
 | 
						||
        private List<ChannelStock> _channelStocks = new List<ChannelStock>();
 | 
						||
        public List<ChannelStock> ChannelStocks
 | 
						||
        {
 | 
						||
            get => _channelStocks;
 | 
						||
            set => SetProperty(ref _channelStocks, value);
 | 
						||
        }
 | 
						||
        private IEnumerable<IGrouping<int, ChannelStock>> enumerable;
 | 
						||
        private IEnumerator<IGrouping<int, ChannelStock>> enumerator;
 | 
						||
        private string WindowName = "InvoiceAddWindow";
 | 
						||
        //private List<ChannelStock> _channelStocks = new();
 | 
						||
 | 
						||
        //public List<ChannelStock> ChannelStocks { get { return _channelStocks; } set { SetProperty(ref _channelStocks, value); } }
 | 
						||
 | 
						||
 | 
						||
 | 
						||
        private List<ChannelStock> _addChannels = new();
 | 
						||
 | 
						||
        public List<ChannelStock> AddChannels { get { return _addChannels; } set { SetProperty(ref _addChannels, value); } }
 | 
						||
        //左侧点击事件
 | 
						||
        public void GetChannelByInvoice()
 | 
						||
        {
 | 
						||
            //ChannelStocks.Clear();
 | 
						||
            ChannelLsts.Clear();
 | 
						||
            InOutInvoices.Clear();
 | 
						||
            List<ChannelStock> iChannelStock = new List<ChannelStock>();
 | 
						||
            if (SelectedInvoice != null)
 | 
						||
            {
 | 
						||
                //先查询有几种药
 | 
						||
                string strSql = @"SELECT SUM(IF(di.small_unit=io.units,io.quantity,io.quantity*di.convert_ratio)) AS SumQuantity, COUNT(io.ID) AS CountNum,io.INVOICE_NO AS InvoiceNo,io.drug_id AS DrugId,io.QUANTITY AS quantity,io.drug_manu_no AS drugManuNo FROM IN_OUT_INVOICE io inner join drug_info di on io.drug_id=di.drug_id WHERE io.INVOICE_NO=@INVOICE_NO  GROUP BY io.INVOICE_NO,io.DRUG_ID";
 | 
						||
 | 
						||
                var invoices = SqlSugarHelper.Db.SqlQueryable<InOutInvoice>(strSql)
 | 
						||
                                                    .AddParameters(new
 | 
						||
                                                    {
 | 
						||
                                                        INVOICE_NO = SelectedInvoice.InvoiceNo
 | 
						||
                                                    })
 | 
						||
                                                    .Select(it => new InOutInvoice())
 | 
						||
                                                    .Select("*").ToList();
 | 
						||
                for (int i = 0; i < invoices.Count; i++)
 | 
						||
                {
 | 
						||
                    //查询药品是否绑定
 | 
						||
                    List<ChannelList> channelL = SqlSugarHelper.Db.Queryable<ChannelList>()
 | 
						||
                                                 .Includes(cl => cl.Drug, di => di.DrugManuNos)
 | 
						||
                                                 //.Includes(cl => cl.channelStocks, dr => dr.DrugInfo, d => d.DrugManuNos)
 | 
						||
                                                 .Where(cl => cl.DrugId == invoices[i].DrugId && cl.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
 | 
						||
                                                 .Where(cl=>cl.DrawerNo>2&&cl.DrawerType==1)  //调拨只能往周转库调住院药房3~16为周转库
 | 
						||
                                                 .ToList();
 | 
						||
                    ChannelList channelLst = channelL.Count > 0 ? channelL[0] : null;
 | 
						||
                    if (channelLst == null || channelLst.Id is null)
 | 
						||
                    {
 | 
						||
                        //药品未绑定库位,需要先绑药
 | 
						||
                        //AlertMsg alertMsg = new AlertMsg
 | 
						||
                        //{
 | 
						||
                        //    Message = "药品未绑定库位,请先绑定库位",
 | 
						||
                        //    Type = MsgType.ERROR,
 | 
						||
                        //};
 | 
						||
                        //_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                        continue;
 | 
						||
                    }
 | 
						||
                    //ChannelStock stock = new ChannelStock();
 | 
						||
                    //查询每种药有多少个批次
 | 
						||
                    var invoicesManuNo = SqlSugarHelper.Db.Queryable<InOutInvoice>().Includes<DrugInfo>(oi=>oi.DrugInfo).InnerJoin<DrugInfo>((oi,di)=>oi.DrugId==di.DrugId)
 | 
						||
                               .Where(oi => oi.InvoiceNo == invoices[i].InvoiceNo && oi.DrugId == invoices[i].DrugId && oi.Status == 2 && oi.Type == 2 && oi.CancelFlag == 0).ToList();
 | 
						||
                    for (int j = 0; j < invoicesManuNo.Count; j++)
 | 
						||
                    {
 | 
						||
                        //查询是否有库存
 | 
						||
                        List<ChannelStock> stockList = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
                             .Where(cs => cs.ManuNo == invoicesManuNo[j].DrugManuNo && cs.DrugId == invoicesManuNo[j].DrugId && cs.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
 | 
						||
                             .Where(cs=>cs.DrawerNo>2)
 | 
						||
                             .ToList();
 | 
						||
                        //stock = stockList.Count > 0 ? stockList[0] : new ChannelStock();
 | 
						||
                        List<DrugManuNo> manuNoList = SqlSugarHelper.Db.Queryable<DrugManuNo>().Where(dm => dm.ManuNo == invoicesManuNo[j].DrugManuNo && dm.DrugId == invoicesManuNo[j].DrugId).ToList();
 | 
						||
                        if (stockList == null || stockList.Count <= 0)
 | 
						||
                        {
 | 
						||
                            if (manuNoList == null || manuNoList.Count <= 0)
 | 
						||
                            {
 | 
						||
                                //药品批次不存在
 | 
						||
                                AlertMsg alertMsg = new AlertMsg
 | 
						||
                                {
 | 
						||
                                    Message = $"药品批次{invoicesManuNo[j].DrugManuNo}不存在,请核对药品批次信息!",
 | 
						||
                                    Type = MsgType.ERROR,
 | 
						||
                                };
 | 
						||
                                _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                                continue;
 | 
						||
                            }
 | 
						||
                            ChannelStock stock = new ChannelStock();
 | 
						||
                            //没有库存写入一条数据
 | 
						||
                            stock.MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
 | 
						||
                            stock.DrawerNo = channelLst.DrawerNo;
 | 
						||
                            stock.BoardType = channelLst.BoardType;
 | 
						||
                            stock.DrawerType = channelLst.DrawerType;
 | 
						||
                            stock.ColNo = channelLst.ColNo;
 | 
						||
                            stock.DrugId = channelLst.DrugId;
 | 
						||
                            stock.ManuNo = invoicesManuNo[j].DrugManuNo;
 | 
						||
                            stock.EffDate = manuNoList[0].EffDate;
 | 
						||
                            stock.Chnguid = channelLst.Id;
 | 
						||
                            stockList.Add(stock);
 | 
						||
                        }
 | 
						||
                        //stock.AddQuantity = invoicesManuNo[j].quantity;
 | 
						||
                        //stockList.ForEach(s => s.AddQuantity = invoicesManuNo[j].quantity);
 | 
						||
                        stockList.GroupBy(x => x.ManuNo)
 | 
						||
                           .Select(it =>
 | 
						||
                           {
 | 
						||
                               var ret = it.First();
 | 
						||
                               ret.AddQuantity = invoicesManuNo[j].Units== invoicesManuNo[j].DrugInfo.SmallUnit? invoicesManuNo[j].quantity: invoicesManuNo[j].quantity* invoicesManuNo[j].DrugInfo.ConvertRatio;
 | 
						||
                               return ret;
 | 
						||
                           })
 | 
						||
                           .ToList();
 | 
						||
 | 
						||
                        if (channelLst.channelStocks == null)
 | 
						||
                        {
 | 
						||
                            channelLst.channelStocks = new List<ChannelStock>();
 | 
						||
                        }
 | 
						||
                        channelLst.channelStocks.AddRange(stockList);
 | 
						||
                    }
 | 
						||
 | 
						||
                    InOutInvoice copy = TransExpV2<InOutInvoice, InOutInvoice>.Trans(invoices[i]);
 | 
						||
                    InOutInvoices.Add(copy);
 | 
						||
                    ChannelLsts.Add(channelLst);
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand OpenInvoiceAdd
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                bool flag = true;
 | 
						||
                ChannelStocks.Clear();
 | 
						||
                foreach (ChannelList lst in ChannelLsts)
 | 
						||
                {
 | 
						||
                    ChannelStocks.AddRange(lst.channelStocks);
 | 
						||
                }
 | 
						||
                AddChannels = ChannelStocks.FindAll(it => it.AddQuantity != 0);
 | 
						||
 | 
						||
                enumerable = AddChannels.GroupBy(cs => cs.DrawerNo, cs => cs);
 | 
						||
                enumerator = enumerable.GetEnumerator();
 | 
						||
                OpenDrawer(AddChannels);
 | 
						||
            }, () => SelectedInvoice != null).ObservesProperty(() => SelectedInvoice);
 | 
						||
        }
 | 
						||
 | 
						||
        private void OpenDrawer(List<ChannelStock> AddChannels)
 | 
						||
        {
 | 
						||
            if (Status == 0)
 | 
						||
            {
 | 
						||
                if (AddChannels == null || AddChannels.Count <= 0)
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "请输入入库数量",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    return;
 | 
						||
                }
 | 
						||
                int totalNum = AddChannels.Sum(add => add.AddQuantity);
 | 
						||
                if (totalNum != SelectedInvoice.Quantity)
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "各批次添加数量要与调拨单药品总数一致!",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    return;
 | 
						||
                }
 | 
						||
                enumerator.MoveNext();
 | 
						||
                Status = 1;
 | 
						||
                OpenOneByOne();
 | 
						||
            }
 | 
						||
        }
 | 
						||
        private async void OpenOneByOne()
 | 
						||
        {
 | 
						||
            IGrouping<int, ChannelStock> grouping = enumerator.Current;
 | 
						||
            int DrawerNo = grouping.Key;
 | 
						||
            List<ChannelStock> channelStocks = grouping.ToList();
 | 
						||
            channelStocks.ForEach(it => it.process = 1);
 | 
						||
            _portUtil.SpeakAsync("正在打开" + DrawerNo + "号抽屉");
 | 
						||
            List<ChannelStock> singleChannels = new List<ChannelStock>();
 | 
						||
            for (int i = 0; i < channelStocks.Count; i++)
 | 
						||
            {
 | 
						||
                ChannelStock copy = TransExpV2<ChannelStock, ChannelStock>.Trans(channelStocks[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);
 | 
						||
 | 
						||
            _portUtil.WindowName = WindowName;
 | 
						||
            _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;
 | 
						||
 | 
						||
            if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 5)
 | 
						||
            {
 | 
						||
                List<ChannelStock> ChannelLst = new List<ChannelStock>();
 | 
						||
                for (int i = 0; i < channelStocks.Count; i++)
 | 
						||
                {
 | 
						||
                    ChannelStock copy = TransExpV2<ChannelStock, ChannelStock>.Trans(channelStocks[i]);
 | 
						||
                    singleChannels.Add(copy);
 | 
						||
                }
 | 
						||
                ChannelLst = ChannelLst.Where(it => it.BoardType != 1)
 | 
						||
                                                                        .GroupBy(it => 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();
 | 
						||
                // 发送加药数量
 | 
						||
                singleChannels.ForEach(it =>
 | 
						||
                {
 | 
						||
                    _portUtil.TakeQuantity(DrawerNo, it.ColNo, it.AddQuantity, it.Quantity + it.AddQuantity);
 | 
						||
                });
 | 
						||
            }
 | 
						||
 | 
						||
            if ((singleChannels.Count > 0 ? singleChannels[0].BoardType : 1) == 6)
 | 
						||
            {
 | 
						||
                for (int i = 0; i < singleChannels.Count; i++)
 | 
						||
                {
 | 
						||
                    ChannelStock it = singleChannels[i];
 | 
						||
                    _portUtil.ClearCount(it.DrawerNo, it.ColNo);
 | 
						||
                    await Task.Delay(50);
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
            _portUtil.Start();
 | 
						||
        }
 | 
						||
        private bool _isFinishClick = false;
 | 
						||
        // 完成按钮
 | 
						||
        public DelegateCommand TakeFinish
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(async () =>
 | 
						||
            {
 | 
						||
                if (!_isFinishClick)
 | 
						||
                {
 | 
						||
                    _isFinishClick = true;
 | 
						||
                    List<ChannelStock> record = ChannelStocks.ToList();
 | 
						||
                    string InvoiceId = SelectedInvoice.InvoiceNo;
 | 
						||
                    var f = SqlSugarHelper.Db.UseTran(() =>
 | 
						||
                    {
 | 
						||
                        SqlSugarHelper.Db.Updateable(new InOutInvoice()
 | 
						||
                        {
 | 
						||
                            Status = 1,
 | 
						||
                            InvoiceNo = SelectedInvoice.InvoiceNo
 | 
						||
                        }).UpdateColumns(it => new { it.Status }).WhereColumns(it => new { it.InvoiceNo }).ExecuteCommand();
 | 
						||
 | 
						||
                        for (int i = 0; i < record.Count; i++)
 | 
						||
                        {
 | 
						||
 | 
						||
 | 
						||
                            ChannelStock it = record[i];
 | 
						||
                            if(it.AddQuantity<=0)
 | 
						||
                            {
 | 
						||
                                continue;
 | 
						||
                            }
 | 
						||
                            if (it.BoardType == 6 && it.PosNo == 0)
 | 
						||
                            {
 | 
						||
                                _portUtil.SetNumCount(it.DrawerNo, it.ColNo, it.AddQuantity);
 | 
						||
                            }
 | 
						||
                            if (it.Id != null)
 | 
						||
                            {
 | 
						||
                                // 更新数据 库存信息
 | 
						||
                                SqlSugarHelper.Db.Updateable(new ChannelStock()
 | 
						||
                                {
 | 
						||
                                    Quantity = it.Quantity + it.AddQuantity,
 | 
						||
                                    ManuNo = it.ManuNo,
 | 
						||
                                    EffDate = it.EffDate,
 | 
						||
                                    Id = it.Id,
 | 
						||
                                }).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate }).ExecuteCommand();
 | 
						||
                            }
 | 
						||
                            else
 | 
						||
                            {
 | 
						||
                                //如果批号重复则不让添加
 | 
						||
                                List<ChannelStock> csCount = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerNo == it.DrawerNo && cs.ColNo == it.ColNo && cs.ManuNo == it.ManuNo && cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1") && cs.Quantity > 0).ToList();
 | 
						||
                                if (csCount.Count > 0)
 | 
						||
                                {
 | 
						||
                                    //repeatList.Add(it.ManuNo);
 | 
						||
                                    //stockRepeats.Add(it);
 | 
						||
                                    continue;
 | 
						||
                                }
 | 
						||
                                //查询现有库位中是否有库存为0的记录,如果有直接update
 | 
						||
                                ChannelStock recordHistory = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerNo == it.DrawerNo && cs.ColNo == it.ColNo && cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1") && cs.Quantity <= 0).First();
 | 
						||
                                if (recordHistory != null && recordHistory.Id != null)
 | 
						||
                                {
 | 
						||
                                    SqlSugarHelper.Db.Updateable<ChannelStock>().SetColumns(item => new ChannelStock()
 | 
						||
                                    {
 | 
						||
                                        Quantity = it.AddQuantity,
 | 
						||
                                        ManuNo = it.ManuNo,
 | 
						||
                                        EffDate = it.EffDate
 | 
						||
                                    }).Where(item => item.Id == recordHistory.Id).ExecuteCommand();
 | 
						||
                                }
 | 
						||
                                else
 | 
						||
                                {
 | 
						||
                                    // 更新数据 库存信息
 | 
						||
                                    SqlSugarHelper.Db.Insertable(new ChannelStock()
 | 
						||
                                    {
 | 
						||
                                        Quantity = it.AddQuantity,
 | 
						||
                                        Chnguid = it.Chnguid,
 | 
						||
                                        ManuNo = it.ManuNo,
 | 
						||
                                        EffDate = it.EffDate,
 | 
						||
                                        DrawerNo = it.DrawerNo,
 | 
						||
                                        ColNo = it.ColNo,
 | 
						||
                                        DrugId = it.DrugId,
 | 
						||
                                        DrawerType = it.DrawerType,
 | 
						||
                                        BoardType = it.BoardType,
 | 
						||
                                        Id = Guid.NewGuid().ToString(),
 | 
						||
                                        MachineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1"
 | 
						||
                                    }).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();
 | 
						||
 | 
						||
                            // 保存数据 出/入库记录
 | 
						||
                            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.AddQuantity,
 | 
						||
                                Type = 1,
 | 
						||
                                InvoiceId = InvoiceId,
 | 
						||
                                StockQuantity = nowChannels.Sum(it => it.Quantity),
 | 
						||
                                ManunoQuantity = nowChannels.FindAll(it2 => it2.ManuNo == it.ManuNo).Sum(it => it.Quantity),
 | 
						||
                                SupplierDept = SelectedInvoice.PharmacyName1 ?? ConfigurationManager.AppSettings["supplierDept"].ToString(),
 | 
						||
                                ReceiveDept = SelectedInvoice.PharmacyName2 ?? ConfigurationManager.AppSettings["receiveDept"].ToString()
 | 
						||
                            }).ExecuteCommand();
 | 
						||
                        }
 | 
						||
                        if (ChannelLsts != null && ChannelLsts.Count > 0)
 | 
						||
                        {
 | 
						||
                            for (int i = 0; i < ChannelLsts.Count; i++)
 | 
						||
                            {
 | 
						||
                                //根据ChannelLsts中的库位,删除该库位库存为0的记录
 | 
						||
                                SqlSugarHelper.Db.Deleteable<ChannelStock>()
 | 
						||
                                .Where(cs => cs.Quantity <= 0 && cs.DrugId == ChannelLsts[i].DrugId && cs.MachineId == ChannelLsts[i].MachineId).ExecuteCommand();
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                        return true;
 | 
						||
                    });
 | 
						||
                    if (f.Data)
 | 
						||
                    {
 | 
						||
                        // 更新屏显库存
 | 
						||
                        //List<ChannelStock> singleChannels = record.FindAll(it => it.BoardType != 1);
 | 
						||
                        List<ChannelStock> singleChannels = record.Where(it => it.BoardType != 1&&it.AddQuantity>0)
 | 
						||
                                                                        .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();
 | 
						||
                        if (singleChannels != null && singleChannels.Count > 0)
 | 
						||
                        {
 | 
						||
                            for (int i = 0; i < singleChannels.Count; i++)
 | 
						||
                            {
 | 
						||
                                if (singleChannels[i].BoardType == 5)
 | 
						||
                                {
 | 
						||
                                    List<ChannelStock> channelStockEffDate = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
                                                                        .Where(cs => cs.MachineId == singleChannels[i].MachineId)
 | 
						||
                                                                        .Where(cs => cs.DrawerNo == singleChannels[i].DrawerNo)
 | 
						||
                                                                        .Where(cs => cs.ColNo == singleChannels[i].ColNo&&cs.Quantity>0)
 | 
						||
                                                                        .OrderBy(cs => cs.EffDate).ToList();
 | 
						||
                                    int totalQuantity = channelStockEffDate.Sum(it => it.Quantity);
 | 
						||
 | 
						||
                                    //将库位多批次的总库存数更新标签
 | 
						||
                                    _portUtil.WriteQuantity(channelStockEffDate[0].DrawerNo, channelStockEffDate[0].ColNo, totalQuantity);
 | 
						||
                                    Thread.Sleep(200);
 | 
						||
                                    _portUtil.WriteChannelInfo(6, channelStockEffDate[0].EffDate == null ? "" : channelStockEffDate[0].EffDate, channelStockEffDate[0].DrawerNo, channelStockEffDate[0].ColNo);
 | 
						||
                                    Thread.Sleep(200);
 | 
						||
                                    _portUtil.WriteChannelInfo(5, channelStockEffDate[0].ManuNo, channelStockEffDate[0].DrawerNo, channelStockEffDate[0].ColNo);
 | 
						||
                                    Thread.Sleep(200);
 | 
						||
                                    _portUtil.ShowContent(channelStockEffDate[0].DrawerNo, channelStockEffDate[0].ColNo);
 | 
						||
                                }
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
                        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;
 | 
						||
 | 
						||
                }
 | 
						||
                RequestData();
 | 
						||
            });
 | 
						||
        }
 | 
						||
        // 取消按钮
 | 
						||
        public DelegateCommand CancleTake
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                RequestData();
 | 
						||
                _portUtil.ResetData();
 | 
						||
                Status = 0;
 | 
						||
            });
 | 
						||
        }
 | 
						||
        public async void OpenOrderDialog()
 | 
						||
        {
 | 
						||
            //if (SelectedInvoice != null && SelectedInvoice.Status == 0)
 | 
						||
            //{
 | 
						||
            //    // 此处延时1毫秒,等待页面渲染
 | 
						||
            //    await Task.Delay(TimeSpan.FromMilliseconds(1));
 | 
						||
            //    DialogParameters dialogParameters = new DialogParameters();
 | 
						||
            //    dialogParameters.Add("invoice", SelectedInvoice);
 | 
						||
            //    dialogParameters.Add("ChannelStocks", AddChannels);
 | 
						||
            //    DialogServiceExtensions.ShowDialogHost(_dialogService, "InvoiceAddDialog", dialogParameters, DoDialogResult, "RootDialog");
 | 
						||
            //}
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand QueryCommand
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                RequestData();
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
        public bool KeepAlive => true;
 | 
						||
 | 
						||
        private void DoDialogResult(IDialogResult dialogResult)
 | 
						||
        {
 | 
						||
            // 委托   被动执行     被子窗口执行
 | 
						||
            // dialogResult  第一方面可以拿到任意参数   第二方面   可判断关闭状态
 | 
						||
            //if (dialogResult.Result == ButtonResult.OK)
 | 
						||
            {
 | 
						||
                SelectedInvoice = null;
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
            //MessageBox.Show("返回值:" + dialogResult.Result.ToString());
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作
 | 
						||
        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
 | 
						||
        {
 | 
						||
            continuationCallback(true);
 | 
						||
        }
 | 
						||
 | 
						||
        public void RequestData()
 | 
						||
        {
 | 
						||
            Invoices.Clear();
 | 
						||
            int totalCount = 0;
 | 
						||
 | 
						||
            var sb = new StringBuilder();
 | 
						||
            //sb.Append("select i.invoice_no as InvoiceNo, i.invoice_date as InvoiceDate, COUNT(i.id) as `Count`, SUM(i.quantity) as quantity, p1.pharmacy_name as PharmacyName1, p2.pharmacy_name as PharmacyName2 from in_out_invoice i");
 | 
						||
            sb.Append(" SELECT count(1) as Count, i.InvoiceNo, i.InvoiceDate, sum(i.quantity) as quantity, p1.pharmacy_name as PharmacyName1, p2.pharmacy_name PharmacyName2 from ");
 | 
						||
            sb.Append(" (SELECT io.drug_id,io.in_pharmacy_id,io.out_pharmacy_id, io.invoice_no as InvoiceNo, DATE_FORMAT(io.Invoice_Date,'%Y-%m-%d')  as InvoiceDate, SUM(IF(di.small_unit=io.units,io.quantity,io.quantity*di.convert_ratio)) as quantity  ");
 | 
						||
            sb.Append(" FROM in_out_invoice io inner join drug_info di on io.drug_id=di.drug_id where io.status=@Status  and io.type=@type  and io.cancel_flag=@CancelFlag GROUP BY io.invoice_no,io.drug_id) i ");
 | 
						||
            sb.Append(" inner join ( select c.drug_id as drug_id from channel_list c where c.machine_id = '" + (ConfigurationManager.AppSettings["machineId"] ?? "DM1") + "' group by c.drug_id ) di on di.drug_id = i.drug_id");
 | 
						||
            sb.Append(" left join pharmacy_info p1 on p1.pharmacy = i.in_pharmacy_id");
 | 
						||
            sb.Append(" left join pharmacy_info p2 on p2.pharmacy = i.out_pharmacy_id where 1=1");
 | 
						||
            //sb.Append(" where i.status=@Status ");
 | 
						||
            //sb.Append(" and i.type!=@type ");
 | 
						||
            //sb.Append(" and i.cancel_flag=@CancelFlag ");
 | 
						||
            if (OrderDate != null)
 | 
						||
            {
 | 
						||
                sb.Append(" and i.InvoiceDate = @CreateTime ");
 | 
						||
            }
 | 
						||
            if (!String.IsNullOrEmpty(SearchValue))
 | 
						||
            {
 | 
						||
                sb.Append(" and i.InvoiceNo = @InvoiceNo ");
 | 
						||
            }
 | 
						||
            if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["storage"]))
 | 
						||
            {
 | 
						||
                sb.Append(" and i.in_pharmacy_id = @OutPharmacyId ");
 | 
						||
            }
 | 
						||
            //sb.Append(" group by i.invoice_no");
 | 
						||
            sb.Append(" GROUP BY  i.InvoiceNo  order by i.InvoiceDate ");
 | 
						||
            Invoices = SqlSugarHelper.Db.SqlQueryable<dynamic>(sb.ToString())
 | 
						||
                .AddParameters(new
 | 
						||
                {
 | 
						||
                    Status = 2,
 | 
						||
                    type = 2,
 | 
						||
                    CancelFlag = 0,
 | 
						||
                    CreateTime = OrderDate,
 | 
						||
                    InvoiceNo = SearchValue,
 | 
						||
                    OutPharmacyId = ConfigurationManager.AppSettings["storage"]
 | 
						||
                })
 | 
						||
                .Select(it => new Invoice())
 | 
						||
                .Select("*")
 | 
						||
                .ToPageList(PageNum, PageSize, ref totalCount);
 | 
						||
 | 
						||
            TotalCount = totalCount;
 | 
						||
            PageCount = (int)Math.Ceiling((double)TotalCount / PageSize);
 | 
						||
        }
 | 
						||
        //接收导航传过来的参数  现在是在此处初始化了表格数据
 | 
						||
        public void OnNavigatedTo(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            _eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
 | 
						||
            RequestData();
 | 
						||
        }
 | 
						||
 | 
						||
        //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
 | 
						||
        public bool IsNavigationTarget(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            return true;
 | 
						||
        }
 | 
						||
 | 
						||
        //这个方法用于拦截请求
 | 
						||
        public void OnNavigatedFrom(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            _eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |