Пример #1
0
        public override void OnNavigatedTo(INavigationParameters parameters)
        {
            base.OnNavigatedTo(parameters);

            try
            {
                if (parameters.ContainsKey("Bill"))
                {
                    if (ReferencePage.Equals("AllocationBillPage"))
                    {
                        this.ShowGift = false;
                        parameters.TryGetValue("Bill", out AllocationBillModel bill);
                        if (bill != null)
                        {
                            this.Bill = bill;
                        }
                    }
                }

                if (ReferencePage == "InventoryOPBillPage")
                {
                    ShowStockQty = false;
                    ShowGift     = false;
                }

                if (ReferencePage == "PurchaseOrderBillPage")
                {
                    ShowStockQty = true;
                    ShowGift     = false;
                }

                //盘点单
                if (ReferencePage == "InventoryOPBillPage")
                {
                    ProductSeries.ToList().ForEach(p =>
                    {
                        TempProductSeries.Add(p);
                    });
                }
            }
            catch (Exception ex)
            {
                Crashes.TrackError(ex);
            }
        }
Пример #2
0
        public SelectProductPageViewModel(INavigationService navigationService,
                                          IProductService productService,
                                          IDialogService dialogService) : base(navigationService, dialogService)
        {
            Title = "选择商品";

            _navigationService = navigationService;
            _dialogService     = dialogService;
            _productService    = productService;

            //只显库存商品
            this.WhenAnyValue(x => x.UsableQuantity)
            .Skip(1)
            .Subscribe(s =>
            {
                ((ICommand)Load)?.Execute(null);
                this.ForceRefresh = true;
            }).DisposeWith(DeactivateWith);

            //我的收藏
            this.WhenAnyValue(x => x.UsableFavorite)
            .Skip(1)
            .Subscribe(s =>
            {
                ((ICommand)Load)?.Execute(null);
                this.ForceRefresh = true;
            }).DisposeWith(DeactivateWith);

            //搜索
            this.WhenAnyValue(x => x.Filter.SerchKey)
            .Where(s => !string.IsNullOrEmpty(s))
            .Select(s => s)
            .Throttle(TimeSpan.FromSeconds(2), RxApp.MainThreadScheduler)
            .Subscribe(s =>
            {
                ((ICommand)SerchCommand)?.Execute(s);
            }).DisposeWith(DeactivateWith);

            this.SerchCommand = ReactiveCommand.Create <string>(e =>
            {
                if (string.IsNullOrEmpty(Filter.SerchKey))
                {
                    this.Alert("请输入关键字!");
                    return;
                }
                ((ICommand)Load)?.Execute(null);
            });

            //Load
            this.Load = ReactiveCommand.Create(async() =>
            {
                try
                {
                    ItemTreshold   = 1;
                    DataVewEnable  = false;
                    NullViewEnable = true;

                    var items = await GetProductsPage(0, 50);
                    if (items != null && items.Any())
                    {
                        //清除列表
                        ProductSeries?.Clear();
                        foreach (var item in items)
                        {
                            if (ProductSeries.Count(s => s.ProductId == item.ProductId) == 0)
                            {
                                ProductSeries.Add(item);
                            }
                        }
                    }
                }
                catch (Exception ex) { Crashes.TrackError(ex); }
                finally
                {
                    DataVewEnable  = true;
                    NullViewEnable = false;
                }
            });

            //以增量方式加载数据
            this.ItemTresholdReachedCommand = ReactiveCommand.Create(async() =>
            {
                using (var dig = UserDialogs.Instance.Loading("加载中..."))
                {
                    try
                    {
                        int pageIdex         = ProductSeries.Count / 50;
                        var items            = await GetProductsPage(pageIdex, PageSize);
                        var previousLastItem = ProductSeries.Last();
                        if (items != null)
                        {
                            foreach (var item in items)
                            {
                                if (ProductSeries.Count(s => s.ProductId == item.ProductId) == 0)
                                {
                                    ProductSeries.Add(item);
                                }
                            }

                            if (items.Count() == 0 || items.Count() == ProductSeries.Count)
                            {
                                ItemTreshold = -1;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Crashes.TrackError(ex);
                        ItemTreshold = -1;
                    }
                }
            }, this.WhenAny(x => x.ProductSeries, x => x.GetValue().Count > 0));


            //初始类别
            this.InitCatagory = ReactiveCommand.CreateFromTask(async() =>
            {
                try
                {
                    var result = await _productService.GetAllCategoriesAsync(true, new System.Threading.CancellationToken());
                    if (result != null && result.Any())
                    {
                        var categories = result.ToList();
                        if (categories != null && categories.Any())
                        {
                            foreach (var op in categories)
                            {
                                op.SelectedCommand = ReactiveCommand.Create <int>(r =>
                                {
                                    op.Selected = !op.Selected;
                                });
                            }
                            this.BindCategories = new ObservableCollection <CategoryModel>(categories);
                        }
                    }
                }
                catch (Exception ex) { Crashes.TrackError(ex); }
            });
            //选择类别
            this.WhenAnyValue(x => x.Selecter).Throttle(TimeSpan.FromMilliseconds(500))
            .Skip(1)
            .Where(x => x != null)
            .SubOnMainThread(x =>
            {
                if (x != null)
                {
                    Filter.CatagoryIds = new int[] { x.Id };
                    this.ForceRefresh  = true;

                    foreach (var op in this.BindCategories)
                    {
                        op.Selected = false;
                    }

                    x.Selected = !x.Selected;

                    ((ICommand)Load)?.Execute(null);
                }
                Selecter = null;
            })
            .DisposeWith(DeactivateWith);


            this.WhenAnyValue(x => x.ReferencePage)
            .Where(x => !string.IsNullOrEmpty(x))
            .Subscribe(x =>
            {
                this.ShowStockQty = !(x.Equals("InventoryOPBillPage") || x.Equals("InventoryBillPage"));
            }).DisposeWith(DeactivateWith);

            //选择礼品
            this.SelectGiftsCommand = ReactiveCommand.Create(async() =>
            {
                await this.NavigateAsync("SelectGiftsPage", ("WareHouse", WareHouse), ("Terminaler", this.Terminal));
            });
            //保存选择商品
            this.SubmitDataCommand = ReactiveCommand.CreateFromTask <object>(async e =>
            {
                if (string.IsNullOrEmpty(ReferencePage))
                {
                    await _navigationService.GoBackAsync();
                    return;
                }

                var products = ProductSeries.Select(p => p).Where(p => p.Selected == true).ToList();
                if (products.Count == 0)
                {
                    this.Alert("请选择商品项目");
                    return;
                }

                if (ReferencePage == nameof(InventoryReportPage))
                {
                    //添加上报商品
                    await this.NavigateAsync(nameof(AddReportProductPage),
                                             ("Reference", ReferencePage),
                                             ("Products", products));
                }
                else if (ReferencePage == nameof(AllocationBillPage))
                {
                    if (this.Bill == null)
                    {
                        this.Alert("单据不能关联,请确保参数");
                        return;
                    }

                    //添加调拨商品
                    await this.NavigateAsync(nameof(AddAllocationProductPage),
                                             ("Bill", Bill),
                                             ("Reference", ReferencePage),
                                             ("Products", products));
                }
                else if (ReferencePage == nameof(BackStockBillPage))
                {
                    //添加回库调拨单商品
                    await this.NavigateAsync(nameof(AddBackStockBillPage),
                                             ("Reference", ReferencePage),
                                             ("Products", products));
                }
                else if (ReferencePage == nameof(TrackAllocationBillPage))
                {
                    //添加回库调拨单商品
                    await this.NavigateAsync(nameof(AddAllocationProductPage),
                                             ("Reference", ReferencePage),
                                             ("Products", products));
                }
                else if (ReferencePage == nameof(CostContractBillPage))
                {
                    //添加合同商品
                    await this.NavigateAsync(nameof(AddCostContractProductPage),
                                             ("Reference", ReferencePage),
                                             ("Products", products));
                }
                else if (ReferencePage == nameof(InventoryOPBillPage) || ReferencePage == nameof(InventoryBillPage))
                {
                    //追加商品
                    if (TempProductSeries.Any())
                    {
                        var temps = TempProductSeries.ToList();
                        foreach (var tp in temps)
                        {
                            if (products.Where(s => s.ProductId == tp.ProductId).Count() == 0)
                            {
                                products.Add(tp);
                            }
                        }
                    }

                    //添加盘点商品
                    await this.NavigateAsync(nameof(AddInventoryProductPage),
                                             ("Reference", ReferencePage),
                                             ("WareHouse", WareHouse),
                                             ("Products", products));
                }
                else if (ReferencePage == nameof(FilterPage))
                {
                    //过滤
                    await _navigationService.GoBackAsync(
                        ("Filter", Filter),
                        ("Reference", ReferencePage),
                        ("Products", products));
                }
                else if (ReferencePage == nameof(PurchaseOrderBillPage))
                {
                    //添加采购商品
                    await this.NavigateAsync(nameof(AddPurchaseProductPage),
                                             ("WareHouse", WareHouse),
                                             ("Reference", ReferencePage),
                                             ("Products", products)
                                             );
                }
                else if (ReferencePage == nameof(ExchangeBillPage))
                {
                    //添加换货商品
                    await this.NavigateAsync(nameof(AddExchangeProductPage),
                                             ("WareHouse", WareHouse),
                                             ("Reference", ReferencePage),
                                             ("Products", products)
                                             );
                }
                else
                {
                    //添加单据商品
                    await this.NavigateAsync($"{nameof(AddProductPage)}",
                                             ("WareHouse", WareHouse),
                                             ("Reference", ReferencePage),
                                             ("Products", products)
                                             );
                }
            });
            //收藏
            this.FavoriteCommand = ReactiveCommand.Create <ProductModel>(p =>
            {
                try
                {
                    var ps = Settings.FavoriteProducts;
                    this.Filter.SerchKey = "";
                    if (!p.Favorited)
                    {
                        if (!ps.Select(s => s.ProductId).Contains(p.ProductId))
                        {
                            p.Favorited = true;
                            ps.Add(p);
                            Settings.FavoriteProducts = ps;
                            _dialogService.ShortAlert("收藏成功!");
                        }
                    }
                    else
                    {
                        var cur = ps.Where(s => s.ProductId == p.ProductId).FirstOrDefault();
                        if (cur != null)
                        {
                            cur.Favorited = false;
                            p.Favorited   = false;
                            ps.Remove(cur);
                            Settings.FavoriteProducts = ps;
                            _dialogService.ShortAlert("收藏已移除!");
                        }
                    }

                    if (UsableFavorite)
                    {
                        if (ProductSeries != null && ProductSeries.Any())
                        {
                            var reload = ProductSeries.Where(s => s.Favorited == true).ToList();
                            if (reload != null && reload.Any())
                            {
                                this.ProductSeries = new ObservableCollection <ProductModel>(reload);
                            }
                            else
                            {
                                this.UsableFavorite = false;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Crashes.TrackError(ex);
                }
            });

            this.BindBusyCommand(Load);

            this.Load.ThrownExceptions.Subscribe(ex => { Debug.Print(ex.StackTrace); }).DisposeWith(this.DeactivateWith);
            this.ItemTresholdReachedCommand.ThrownExceptions.Subscribe(ex => { Debug.Print(ex.StackTrace); }).DisposeWith(this.DeactivateWith);
            this.InitCatagory.ThrownExceptions.Subscribe(ex => { Debug.Print(ex.StackTrace); }).DisposeWith(this.DeactivateWith);
            this.SelectGiftsCommand.ThrownExceptions.Subscribe(ex => { Debug.Print(ex.StackTrace); }).DisposeWith(this.DeactivateWith);
            this.SubmitDataCommand.ThrownExceptions.Subscribe(ex => { Debug.Print(ex.StackTrace); }).DisposeWith(this.DeactivateWith);
            this.FavoriteCommand.ThrownExceptions.Subscribe(ex => { Debug.Print(ex.StackTrace); }).DisposeWith(this.DeactivateWith);
        }