步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.8 KiB

3 years ago
using BBWY.Client.APIServices;
using BBWY.Client.Models;
using BBWY.Client.Views.Setting;
3 years ago
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 isValidated;
3 years ago
private bool isLoading;
private GlobalContext globalContext;
private ShopService shopService;
private PurchaseAccount purchaseAccount;
private string managePwd;
private decimal platformCommissionRatio;
3 years ago
private int panelIndex;
public ICommand SaveCommand { get; set; }
public ICommand SetPanelIndexCommand { get; set; }
3 years ago
public PurchaseAccount PurchaseAccount { get => purchaseAccount; set { Set(ref purchaseAccount, value); } }
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
3 years ago
public string ManagePwd { get => managePwd; set { Set(ref managePwd, value); } }
public decimal PlatformCommissionRatio { get => platformCommissionRatio; set { Set(ref platformCommissionRatio, value); } }
3 years ago
3 years ago
public int PanelIndex { get => panelIndex; set { Set(ref panelIndex, value); } }
public IList<int> SafeDayList { get; set; }
3 years ago
public ShopSettingViewModel(GlobalContext globalContext, ShopService shopService)
{
3 years ago
PanelIndex = 0;
3 years ago
this.globalContext = globalContext;
this.shopService = shopService;
SaveCommand = new RelayCommand(Save);
3 years ago
SetPanelIndexCommand = new RelayCommand<int>(i =>
{
PanelIndex = i;
});
SafeDayList = new List<int>() { 0, 14, 21, 28 };
3 years ago
}
protected override void Load()
{
IsLoading = false;
if (!string.IsNullOrEmpty(globalContext.User.Shop.ManagePwd))
{
var validatePwd = new ValidateManagePwd(globalContext.User.Shop.ManagePwd);
if (validatePwd.ShowDialog() != true)
return;
}
isValidated = true;
this.ManagePwd = globalContext.User.Shop.ManagePwd;
3 years ago
this.PlatformCommissionRatio = (globalContext.User.Shop.PlatformCommissionRatio ?? 0.05M) * 100;
3 years ago
this.PurchaseAccount = globalContext.User.Shop.PurchaseAccountList == null || globalContext.User.Shop.PurchaseAccountList.Count() == 0 ?
new PurchaseAccount() :
globalContext.User.Shop.PurchaseAccountList[0].Clone() as PurchaseAccount;
3 years ago
}
protected override void Unload()
{
this.PurchaseAccount = null;
3 years ago
this.PlatformCommissionRatio = 0M;
this.ManagePwd = string.Empty;
this.isValidated = false;
3 years ago
}
private void Save()
{
if (!isValidated)
{
MessageBox.Show("店铺密码验证未通过", "保存店铺设置");
return;
}
if (string.IsNullOrEmpty(PurchaseAccount.AppKey) ||
3 years ago
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();
3 years ago
var p = PlatformCommissionRatio / 100;
3 years ago
IsLoading = true;
3 years ago
Task.Factory.StartNew(() => shopService.SaveShopSetting(globalContext.User.Shop.ShopId, ManagePwd, p, PurchaseAccount)).ContinueWith(r =>
3 years ago
{
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<PurchaseAccount>();
globalContext.User.Shop.PurchaseAccountList.Clear();
PurchaseAccount.Id = response.Data;
globalContext.User.Shop.PurchaseAccountList.Add(PurchaseAccount);
globalContext.User.Shop.PlatformCommissionRatio = p;
globalContext.User.Shop.ManagePwd = this.ManagePwd;
3 years ago
});
}
}
}