using BBWY.Client.APIServices; using BBWY.Client.Models; using BBWY.Common.Models; using GalaSoft.MvvmLight.Command; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace BBWY.Client.ViewModels { public class ShopSettingViewModel : BaseVM, IDenpendency { private bool isLoading; private GlobalContext globalContext; private ShopService shopService; private PurchaseAccount purchaseAccount; public PurchaseAccount PurchaseAccount { get => purchaseAccount; set { Set(ref purchaseAccount, value); } } public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } } public ICommand SaveCommand { get; set; } public ShopSettingViewModel(GlobalContext globalContext, ShopService shopService) { this.globalContext = globalContext; this.shopService = shopService; SaveCommand = new RelayCommand(Save); } protected override void Load() { this.PurchaseAccount = globalContext.User.Shop.PurchaseAccountList == null || globalContext.User.Shop.PurchaseAccountList.Count() == 0 ? new PurchaseAccount() : globalContext.User.Shop.PurchaseAccountList[0].Clone() as PurchaseAccount; } protected override void Unload() { this.PurchaseAccount = null; } private void Save() { IsLoading = true; Task.Factory.StartNew(() => shopService.SaveShopSetting(globalContext.User.Shop.ShopId, "", 0, PurchaseAccount)).ContinueWith(r => { IsLoading = false; var response = r.Result; if (!response.Success) { App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "保存店铺设置")); return; } if (globalContext.User.Shop.PurchaseAccountList == null) globalContext.User.Shop.PurchaseAccountList = new List(); globalContext.User.Shop.PurchaseAccountList.Clear(); PurchaseAccount.Id = response.Data; globalContext.User.Shop.PurchaseAccountList.Add(PurchaseAccount); }); } } }