using BBWY.Client.APIServices; using BBWY.Client.Models; using BBWY.Client.Views.BatchPurchase; using GalaSoft.MvvmLight.Command; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; namespace BBWY.Client.ViewModels { public class BatchPurchaseCreateNewOrderViewModel : BaseVM { private PurchaseProductAPIService purchaseProductAPIService; private bool isLoading; private decimal productAmount; private decimal freightAmount; private decimal totalAmount; private string contactName; private string address; private string mobile; private string province; private string city; private string county; private string town; private string prucahseRemark; private PurchaseOrderMode purchaseOrderMode = PurchaseOrderMode.代发; public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } } public decimal ProductAmount { get => productAmount; set { Set(ref productAmount, value); } } public decimal FreightAmount { get => freightAmount; set { Set(ref freightAmount, value); } } public decimal TotalAmount { get => totalAmount; set { Set(ref totalAmount, value); } } public string ContactName { get => contactName; set { Set(ref contactName, value); } } public string Address { get => address; set { Set(ref address, value); } } public string Mobile { get => mobile; set { Set(ref mobile, value); } } public string Province { get => province; set { Set(ref province, value); } } public string City { get => city; set { Set(ref city, value); } } public string County { get => county; set { Set(ref county, value); } } public string Town { get => town; set { Set(ref town, value); } } public string PrucahseRemark { get => prucahseRemark; set { Set(ref prucahseRemark, value); } } public PurchaseOrderMode PurchaseOrderMode { get => purchaseOrderMode; set { if (Set(ref purchaseOrderMode, value)) OnDelayTriggerExecute(Guid.NewGuid().ToString()); } } public IList ProductSkuWithSchemeList { get; set; } public ICommand FastCreateOrderCommand { get; set; } public ICommand PreviewOrderCommand { get; set; } public ICommand AddProductSkuCommand { get; set; } public BatchPurchaseCreateNewOrderViewModel(PurchaseProductAPIService purchaseProductAPIService) { this.purchaseProductAPIService = purchaseProductAPIService; ProductSkuWithSchemeList = new ObservableCollection(); FastCreateOrderCommand = new RelayCommand(FastCreateOrder); PreviewOrderCommand = new RelayCommand(PreviewOrder); AddProductSkuCommand = new RelayCommand(AddProductSku); } private void OnDelayTriggerExecute(string key) { if (string.IsNullOrEmpty(key)) { IsLoading = false; return; } if (string.IsNullOrEmpty(ContactName) || string.IsNullOrEmpty(Address) || string.IsNullOrEmpty(Mobile) || string.IsNullOrEmpty(Province) || string.IsNullOrEmpty(City) || string.IsNullOrEmpty(County)) { IsLoading = false; MessageBox.Show("缺少完整的收货信息", "提示"); return; } //IsLoading = true; //Task.Factory.StartNew(() => purchaseOrderService.PreviewPurchaseOrder(new Consignee() //{ // Address = Address, // City = City, // ContactName = ContactName, // County = County, // Mobile = Mobile, // Province = Province, // TelePhone = Mobile, // Town = Town //}, PurchaseSchemeProductSkuList, purchaseAccount.PurchasePlatformId, purchaseAccount, PurchaseOrderMode)) // .ContinueWith(t => // { // IsLoading = false; // var r = t.Result; // if (!r.Success) // { // ProductAmount = FreightAmount = TotalAmount = 0; // tradeMode = string.Empty; // App.Current.Dispatcher.Invoke(() => MessageBox.Show(r.Msg, "预览订单报价")); // return; // } // ProductAmount = r.Data.ProductAmount; // FreightAmount = r.Data.FreightAmount; // TotalAmount = r.Data.TotalAmount; // tradeMode = r.Data.OrderTradeType?.Code; // extensions = r.Data.Extensions; // }); } private void PreviewOrder() { OnDelayTriggerExecute(Guid.NewGuid().ToString()); } private void FastCreateOrder() { if (IsLoading) return; if (TotalAmount == 0) { MessageBox.Show("总金额为0不能提交订单", "提示"); return; } if (string.IsNullOrEmpty(Mobile) || string.IsNullOrEmpty(Address) || string.IsNullOrEmpty(City) || string.IsNullOrEmpty(Province) || string.IsNullOrEmpty(County) || string.IsNullOrEmpty(Town) || string.IsNullOrEmpty(ContactName)) { MessageBox.Show("收货人信息不全", "下单"); return; } //IsLoading = true; //Task.Factory.StartNew(() => purchaseOrderService.FastCreateOrder(new Consignee() //{ // Address = Address, // City = City, // ContactName = ContactName, // County = County, // Mobile = Mobile, // Province = Province, // TelePhone = Mobile, // Town = Town //}, PurchaseSchemeProductSkuList, // purchaseAccount.PurchasePlatformId, // purchaseAccount, // PurchaseOrderMode, // tradeMode, // PrucahseRemark, // order.Id, // globalContext.User.Shop.ShopId, // purchaseAccount.Id, // purchaseAccount.AccountName, // purchaseSchemeList[0].PurchaserName, // purchaser.Id, // globalContext.User.Shop.PlatformCommissionRatio ?? 0.05M, // extensions)).ContinueWith(t => // { // IsLoading = false; // var r = t.Result; // if (!r.Success) // { // App.Current.Dispatcher.Invoke(() => MessageBox.Show(r.Msg, "下单")); // return; // } // //刷新订单列表 // orderListViewModel.RefreshOrder(order.Id); // //关闭当前窗口 // GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(null, "OnlinePurchase_Close"); // }); } private void AddProductSku() { var addProductSkuWindow = new BatchPurchaseAddProductSku(); if (addProductSkuWindow.ShowDialog() == true) { var newProductSkuWithSchemeList = addProductSkuWindow.SelectedProductSkuWithSchemeList; } } } }