using BBWYB.Client.APIServices; using BBWYB.Client.Models; using BBWYB.Common.Models; using CommunityToolkit.Mvvm.Input; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace BBWYB.Client.ViewModels { public class ShopSettingViewModel : BaseVM, IDenpendency { private bool isLoading; private GlobalContext globalContext; private ShopService shopService; private PurchaseAccount purchaseAccount; private Platform purchasePlatform; public ICommand SaveCommand { get; set; } public bool IsLoading { get => isLoading; set { SetProperty(ref isLoading, value); } } public Platform PurchasePlatform { get => purchasePlatform; set { if (SetProperty(ref purchasePlatform, value)) { OnPurchasePlatformChanged(); } } } public PurchaseAccount PurchaseAccount { get => purchaseAccount; set { SetProperty(ref purchaseAccount, value); } } public ShopSettingViewModel(GlobalContext globalContext, ShopService shopService) { this.globalContext = globalContext; this.shopService = shopService; SaveCommand = new RelayCommand(Save); } private void OnPurchasePlatformChanged() { var pa = globalContext.User.Shop.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == PurchasePlatform); if (pa == null) { pa = new PurchaseAccount() { PurchasePlatformId = PurchasePlatform, }; globalContext.User.Shop.PurchaseAccountList.Add(pa); } this.PurchaseAccount = pa; } protected override void Load() { if (globalContext.User.Shop.PurchaseAccountList == null) globalContext.User.Shop.PurchaseAccountList = new List(); IsLoading = false; PurchasePlatform = Platform.阿里巴巴; } private void Save() { if (string.IsNullOrEmpty(PurchaseAccount.AppKey) || string.IsNullOrEmpty(PurchaseAccount.AppSecret) || string.IsNullOrEmpty(PurchaseAccount.AppToken) || string.IsNullOrEmpty(PurchaseAccount.AccountName)) { MessageBox.Show("采购信息不能为空", "保存店铺设置"); return; } PurchaseAccount.AppKey = PurchaseAccount.AppKey.Trim(); PurchaseAccount.AppSecret = PurchaseAccount.AppSecret.Trim(); PurchaseAccount.AppToken = PurchaseAccount.AppToken.Trim(); PurchaseAccount.AccountName = PurchaseAccount.AccountName.Trim(); IsLoading = true; Task.Factory.StartNew(() => shopService.SaveShopSetting(globalContext.User.Shop.ShopId, PurchaseAccount)).ContinueWith(r => { IsLoading = false; var response = r.Result; if (!response.Success) { App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "保存店铺设置")); return; } PurchaseAccount.Id = response.Data; }); } } }