using BBWY.Client.APIServices; using BBWY.Client.Models; using BBWY.Client.Views.Purchase; using BBWY.Common.Models; using GalaSoft.MvvmLight.Command; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace BBWY.Client.ViewModels { public class ChoosePurchaseSchemeViewModel : BaseVM, IDenpendency { private PurchaseService purchaseService; private GlobalContext globalContext; private BindingPurchaseProductViewModel bindingPurchaseProduct; public string SkuId { get; set; } public string SkuName { get; set; } public string OrderId { get; set; } public int ItemTotal { get; set; } public IList PurchaseSchemeList { get; set; } public ICommand DeleteCommand { get; set; } public ICommand EditCommand { get; set; } public ICommand PreviewPurchaseCommand { get; set; } public ChoosePurchaseSchemeViewModel(PurchaseService purchaseService, GlobalContext globalContext, BindingPurchaseProductViewModel bindingPurchaseProduct) { this.purchaseService = purchaseService; this.globalContext = globalContext; this.bindingPurchaseProduct = bindingPurchaseProduct; PurchaseSchemeList = new ObservableCollection(); DeleteCommand = new RelayCommand(Delete); EditCommand = new RelayCommand(Edit); PreviewPurchaseCommand = new RelayCommand(PreviewPurchase); } protected override void Load() { PurchaseSchemeList.Clear(); Task.Factory.StartNew(() => LoadPurchaseScheme(SkuId)); } protected override void Unload() { this.ItemTotal = 0; this.OrderId = this.SkuId = this.SkuName = string.Empty; PurchaseSchemeList.Clear(); } public void SetData(string orderId, string skuId, string skuName, int itemTotal) { this.OrderId = orderId; this.SkuId = skuId; this.SkuName = skuName; this.ItemTotal = itemTotal; } public void LoadPurchaseScheme(string skuId) { var purchaseSchemeResponse = purchaseService.GetPurchaseSchemeList(new string[] { SkuId }, globalContext.User.Shop.ShopId); if (!purchaseSchemeResponse.Success) { App.Current.Dispatcher.Invoke(() => MessageBox.Show(purchaseSchemeResponse.Msg, "获取采购方案")); return; } App.Current.Dispatcher.Invoke(() => { foreach (var apiModel in purchaseSchemeResponse.Data) PurchaseSchemeList.Add(PurchaseScheme.Convert(apiModel)); }); } public void Delete(PurchaseScheme purchaseScheme) { Task.Factory.StartNew(() => purchaseService.DeletePurchaseScheme(purchaseScheme.Id)).ContinueWith(r => { var response = r.Result; if (response.Success) App.Current.Dispatcher.Invoke(() => PurchaseSchemeList.Remove(purchaseScheme)); else App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "删除采购方案")); }); } public void Edit(PurchaseScheme purchaseScheme) { var skuList = new List() { new ProductSku(){ Id=purchaseScheme.SkuId,ProductId=purchaseScheme.ProductId, Title=SkuName} }; bindingPurchaseProduct.SetData(skuList, purchaseScheme.PurchaserId, purchaseScheme.PurchaserName); var bindingView = new Views.Ware.BindingPurchaseProduct(); var r = bindingView.ShowDialog(); if (r == true) Task.Factory.StartNew(() => LoadPurchaseScheme(SkuId)); } public void PreviewPurchase(PurchaseScheme purchaseScheme) { var p = new _1688Purchase(this.OrderId, this.ItemTotal, purchaseScheme); p.ShowDialog(); } } }