using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Configuration; using System.Drawing.Printing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using DM_Weight.Models; using DM_Weight.select; using DM_Weight.util; using DM_Weight.msg; using Prism.Events; namespace DM_Weight.ViewModels { public class OrderTakeDrugWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime { 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(OpenOrderDialog); public OrderTakeDrugWindowViewModel(IDialogService DialogService, IEventAggregator eventAggregator) { _dialogService = DialogService; _eventAggregator = eventAggregator; } public static List StaticOrderTakeSelects = new() { new OrderTakeSelect { Code = "OrderNo", Name = "处方号" }, new OrderTakeSelect { Code = "PatientId", Name = "患者编号" } }; private List _orderTakeSelects = StaticOrderTakeSelects; public List OrderTakeSelects { get { return _orderTakeSelects; } set { SetProperty(ref _orderTakeSelects, value); } } private OrderTakeSelect _selectedItem = StaticOrderTakeSelects[0]; /// /// 查询条件 查询字段 /// public OrderTakeSelect SelectedItem { get { return _selectedItem; } set { SetProperty(ref _selectedItem, value); RequestData(); } } private OrderInfo? _selectedOrder; public OrderInfo? SelectedOrder { get { return _selectedOrder; } set { SetProperty(ref _selectedOrder, value); //OpenOrderDialog(); } } private string _orderDate = DateTime.Now.ToString("yyyy-MM-dd"); /// /// 查询条件 处方日期 /// 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; /// /// 查询条件 查询字段值 /// public string? SearchValue { get { return _searchValue; } set { SetProperty(ref _searchValue, value); RequestData(); } } private List _orderInfos = new(); public List OrderInfos { get { return _orderInfos; } set { SetProperty(ref _orderInfos, value); } } public bool KeepAlive => false; public async void OpenOrderDialog() { if (SelectedOrder != null && SelectedOrder.DmStatus == 0 && SelectedOrder.CancelFlag == 0 && SelectedOrder.HisDispFlag == 0) { // 此处延时1毫秒,等待页面渲染 await Task.Delay(TimeSpan.FromMilliseconds(1)); DialogParameters dialogParameters = new DialogParameters(); dialogParameters.Add("orderInfo", SelectedOrder); DialogServiceExtensions.ShowDialogHost(_dialogService, "OrderTakeDialog", dialogParameters, DoDialogResult, "RootDialog"); } } private void DoDialogResult(IDialogResult dialogResult) { // 委托 被动执行 被子窗口执行 // dialogResult 第一方面可以拿到任意参数 第二方面 可判断关闭状态 //if(dialogResult.Result == ButtonResult.OK) //{ SelectedOrder = null; RequestData(); //} //MessageBox.Show("返回值:" + dialogResult.Result.ToString()); } //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作 public void ConfirmNavigationRequest(NavigationContext navigationContext, Action continuationCallback) { continuationCallback(true); } public DelegateCommand QueryCommand { get => new DelegateCommand(() => { RequestData(); }); } public void RequestData() { OrderInfos.Clear(); int totalCount = 0; //string SearchValue = null; //if (SearchValue != null) //{ // strSearchValue = SearchValue.Trim().Replace("\r", ""); //} List queryData = SqlSugarHelper.Db.Queryable() .InnerJoin((oi, od) => oi.OrderNo == od.OrderNo) .InnerJoin(SqlSugarHelper.Db.Queryable().Where(cs => cs.DrawerType == 1).Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM1")).GroupBy(cs => cs.DrugId), (oi, od, t) => od.DrugId == t.DrugId) .WhereIF(OrderDate != null, oi => oi.OrderDate.ToString("yyyy-MM-dd") == OrderDate) .WhereIF(!String.IsNullOrEmpty(SearchValue) && SelectedItem.Code.Equals("OrderNo"), oi => oi.OrderNo == SearchValue) .WhereIF(!String.IsNullOrEmpty(SearchValue) && SelectedItem.Code.Equals("PatientId"), oi => oi.PatientId == SearchValue) .WhereIF(!String.IsNullOrEmpty(ConfigurationManager.AppSettings["storage"]), oi => oi.Pharmacy == ConfigurationManager.AppSettings["storage"]) .Where(oi => oi.DmStatus == 0) .Where(oi => oi.HisDispFlag == 0) .Where(oi => oi.CancelFlag == 0) .Where(oi=>oi.Pharmacy.Equals(ConfigurationManager.AppSettings["storage"] ?? "")) .GroupBy(oi => oi.OrderDate) .Select(oi => oi) .ToPageList(PageNum, PageSize, ref totalCount); //.ToList(); OrderInfos = queryData; TotalCount = totalCount; PageCount = (int)Math.Ceiling((double)TotalCount / PageSize); } //接收导航传过来的参数 现在是在此处初始化了表格数据 public void OnNavigatedTo(NavigationContext navigationContext) { _eventAggregator.GetEvent().Subscribe(DoMyPrismEvent); RequestData(); } //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建 public bool IsNavigationTarget(NavigationContext navigationContext) { return true; } //这个方法用于拦截请求 public void OnNavigatedFrom(NavigationContext navigationContext) { // 取消消息订阅 _eventAggregator.GetEvent().Unsubscribe(DoMyPrismEvent); } void DoMyPrismEvent(DeviceMsg msg) { if (msg.EventType == EventType.CODESCAN) { SearchValue = msg.Code; } } } }