391 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			391 lines
		
	
	
		
			14 KiB
		
	
	
	
		
			C#
		
	
	
	
using DM_Weight.Models;
 | 
						||
using DM_Weight.msg;
 | 
						||
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.Generic;
 | 
						||
using System.Collections.ObjectModel;
 | 
						||
using System.ComponentModel;
 | 
						||
using System.Configuration;
 | 
						||
using System.Linq;
 | 
						||
using System.Linq.Expressions;
 | 
						||
using System.Text;
 | 
						||
using System.Threading.Tasks;
 | 
						||
using System.Windows.Data;
 | 
						||
using System.Windows.Markup;
 | 
						||
 | 
						||
namespace DM_Weight.ViewModels
 | 
						||
{
 | 
						||
    public class InvoiceInWindowViewModel : 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);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        IDialogService _dialogService;
 | 
						||
 | 
						||
 | 
						||
        IEventAggregator _eventAggregator;
 | 
						||
 | 
						||
        private DelegateCommand _rowSelected;
 | 
						||
 | 
						||
        public DelegateCommand RowSelected => _rowSelected ??= new DelegateCommand(GetChannelByInvoice);
 | 
						||
        //private SqlSugarScope SqlSugarHelper.Db;
 | 
						||
        public InvoiceInWindowViewModel(IDialogService DialogService, IEventAggregator eventAggregator)
 | 
						||
        {
 | 
						||
            _dialogService = DialogService;
 | 
						||
            _eventAggregator = eventAggregator;
 | 
						||
            //this.SqlSugarHelper.Db= sqlSugarScope;
 | 
						||
        }
 | 
						||
 | 
						||
        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 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();
 | 
						||
            InOutInvoices.Clear();
 | 
						||
            List<ChannelStock> i = new List<ChannelStock>();
 | 
						||
            if (SelectedInvoice != null)
 | 
						||
            {
 | 
						||
                var invoices = SqlSugarHelper.Db.Queryable<InOutInvoice>()
 | 
						||
                                .Includes<DrugInfo>(i => i.DrugInfo)
 | 
						||
                                .InnerJoin(SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.DrawerType == 1).Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrugId), (i, t) => i.DrugId == t.DrugId)
 | 
						||
                                .Where(i => i.InvoiceNo == SelectedInvoice.InvoiceNo)
 | 
						||
                                .ToList();
 | 
						||
                foreach (var invoice in invoices)
 | 
						||
                {
 | 
						||
                    List<ChannelStock> q = SqlSugarHelper.Db.Queryable<ChannelStock>()
 | 
						||
                    .Includes<DrugInfo>(cs => cs.DrugInfo)
 | 
						||
                    .WhereIF(!string.IsNullOrEmpty(invoice.DrugEffDate), cs => cs.EffDate.Equals(invoice.DrugEffDate))
 | 
						||
                    .WhereIF(!string.IsNullOrEmpty(invoice.DrugManuNo), cs => cs.ManuNo.Equals(invoice.DrugManuNo))
 | 
						||
                    .Where(cs => cs.DrugId == invoice.DrugId)
 | 
						||
                    .Where(cs => cs.DrawerType == 1)
 | 
						||
                    .Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1"))
 | 
						||
                    .OrderBy(cs => cs.DrugId)
 | 
						||
                    .OrderBy(cs => cs.DrawerNo)
 | 
						||
                    .OrderBy(cs => cs.ColNo)
 | 
						||
                    .ToList();
 | 
						||
 | 
						||
 | 
						||
                    
 | 
						||
 | 
						||
                    for (int j = 0; j < q.Count;j++)
 | 
						||
                    {
 | 
						||
                        ChannelStock item = q[j];
 | 
						||
                        if(j == 0)
 | 
						||
                        {
 | 
						||
                            item.AddQuantity = invoice.quantity;
 | 
						||
                        }
 | 
						||
                        item.Invoice = invoice;
 | 
						||
                        i.Add(item);
 | 
						||
                    }
 | 
						||
                    InOutInvoice copy = TransExpV2<InOutInvoice, InOutInvoice>.Trans(invoice);
 | 
						||
                    InOutInvoices.Add(copy);
 | 
						||
                }
 | 
						||
            }
 | 
						||
            ICollectionView vw = CollectionViewSource.GetDefaultView(i);
 | 
						||
            vw.GroupDescriptions.Add(new PropertyGroupDescription("Invoice"));
 | 
						||
            ChannelStocks = i;
 | 
						||
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand OpenInvoiceAdd
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                bool flag = true;
 | 
						||
                for (int i = 0; i < InOutInvoices.Count; i++)
 | 
						||
                {
 | 
						||
                    InOutInvoice invoices = InOutInvoices[i];
 | 
						||
                    if (invoices.quantity != ChannelStocks.FindAll(it => it.DrugId == invoices.DrugId).Sum(it => it.AddQuantity))
 | 
						||
                    {
 | 
						||
                        flag = false;
 | 
						||
                        break;
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                if (flag)
 | 
						||
                {
 | 
						||
                    AddChannels = ChannelStocks.FindAll(it => it.AddQuantity != 0);
 | 
						||
                    AddChannels.Sort((a, b) =>
 | 
						||
                    {
 | 
						||
                        if ((a.DrawerNo - b.DrawerNo) == 0)
 | 
						||
                        {
 | 
						||
                            return a.ColNo - b.ColNo;
 | 
						||
                        }
 | 
						||
                        return a.DrawerNo - b.DrawerNo;
 | 
						||
                    });
 | 
						||
                    OpenOrderDialog();
 | 
						||
                } else
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "库位添加数量小于应添加数量",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                }
 | 
						||
            },() => SelectedInvoice !=null).ObservesProperty(() => SelectedInvoice);
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        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  i.InvoiceNo, i.InvoiceDate, i.Count, i.quantity, p1.pharmacy_name as PharmacyName1, p2.pharmacy_name PharmacyName2 from ");
 | 
						||
            sb.Append(" (SELECT drug_id,in_pharmacy_id,out_pharmacy_id, invoice_no as InvoiceNo, invoice_date as InvoiceDate, COUNT(id) as `Count`, SUM(quantity) as quantity ");
 | 
						||
            sb.Append(" FROM in_out_invoice where status=@Status  and type!=@type  and cancel_flag=@CancelFlag GROUP BY invoice_no) i ");
 | 
						||
            sb.Append(" inner join ( select c.drug_id as drug_id from channel_stock 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_id = i.in_pharmacy_id");
 | 
						||
            sb.Append(" left join pharmacy_info p2 on p2.pharmacy_id = i.out_pharmacy_id");
 | 
						||
            //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(" order by i.InvoiceDate ");
 | 
						||
            Invoices = SqlSugarHelper.Db.SqlQueryable<dynamic>(sb.ToString())
 | 
						||
                .AddParameters(new
 | 
						||
                {
 | 
						||
                    Status = 0,
 | 
						||
                    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)
 | 
						||
        {
 | 
						||
            RequestData();
 | 
						||
        }
 | 
						||
 | 
						||
        //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
 | 
						||
        public bool IsNavigationTarget(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            return true;
 | 
						||
        }
 | 
						||
 | 
						||
        //这个方法用于拦截请求
 | 
						||
        public void OnNavigatedFrom(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |