|
|
|
using BBWYB.Client.APIServices;
|
|
|
|
using BBWYB.Client.Models;
|
|
|
|
using BBWYB.Client.Views.Purchase;
|
|
|
|
using BBWYB.Common.Trigger;
|
|
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
using CommunityToolkit.Mvvm.Messaging;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Windows;
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
namespace BBWYB.Client.ViewModels
|
|
|
|
{
|
|
|
|
public class OnlinePurchaseViewModel : BaseVM
|
|
|
|
{
|
|
|
|
public ICommand FastCreateOrderCommand { get; set; }
|
|
|
|
public ICommand PreviewOrderCommand { get; set; }
|
|
|
|
|
|
|
|
public IList<PurchaseSchemeProductSku> PurchaseSchemeProductSkuList { get; set; }
|
|
|
|
|
|
|
|
public bool IsLoading { get => isLoading; set { SetProperty(ref isLoading, value); } }
|
|
|
|
|
|
|
|
public decimal ProductAmount { get => productAmount; set { SetProperty(ref productAmount, value); } }
|
|
|
|
public decimal FreightAmount { get => freightAmount; set { SetProperty(ref freightAmount, value); } }
|
|
|
|
public decimal TotalAmount { get => totalAmount; set { SetProperty(ref totalAmount, value); } }
|
|
|
|
public string ContactName { get => contactName; set { SetProperty(ref contactName, value); } }
|
|
|
|
public string Address { get => address; set { SetProperty(ref address, value); } }
|
|
|
|
public string Mobile { get => mobile; set { SetProperty(ref mobile, value); } }
|
|
|
|
public string Province { get => province; set { SetProperty(ref province, value); } }
|
|
|
|
public string City { get => city; set { SetProperty(ref city, value); } }
|
|
|
|
public string County { get => county; set { SetProperty(ref county, value); } }
|
|
|
|
public string Town { get => town; set { SetProperty(ref town, value); } }
|
|
|
|
public string PrucahseRemark { get => prucahseRemark; set { SetProperty(ref prucahseRemark, value); } }
|
|
|
|
|
|
|
|
public PurchaseOrderMode PurchaseOrderMode
|
|
|
|
{
|
|
|
|
get => purchaseOrderMode; set
|
|
|
|
{
|
|
|
|
if (SetProperty(ref purchaseOrderMode, value))
|
|
|
|
OnDelayTriggerExecute(Guid.NewGuid().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Purchaser purchaser;
|
|
|
|
private Order order;
|
|
|
|
private PurchaseAccount purchaseAccount;
|
|
|
|
|
|
|
|
private IList<PurchaseScheme> purchaseSchemeList;
|
|
|
|
private OrderViewModel orderVM;
|
|
|
|
private GlobalContext globalContext;
|
|
|
|
private bool isLoading;
|
|
|
|
private PurchaseService purchaseService;
|
|
|
|
private PurchaseOrderService purchaseOrderService;
|
|
|
|
private PurchaseProductAPIService purchaseProductAPIService;
|
|
|
|
private DelayTrigger delayTrigger;
|
|
|
|
|
|
|
|
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.代发;
|
|
|
|
/// <summary>
|
|
|
|
/// 扩展数据,暂用于拳探
|
|
|
|
/// </summary>
|
|
|
|
private string extensions;
|
|
|
|
|
|
|
|
public OnlinePurchaseViewModel(PurchaseService purchaseService,
|
|
|
|
PurchaseOrderService purchaseOrderService,
|
|
|
|
PurchaseProductAPIService purchaseProductAPIService,
|
|
|
|
GlobalContext globalContext,
|
|
|
|
OrderViewModel orderVM)
|
|
|
|
{
|
|
|
|
this.purchaseOrderService = purchaseOrderService;
|
|
|
|
this.purchaseProductAPIService = purchaseProductAPIService;
|
|
|
|
this.purchaseService = purchaseService;
|
|
|
|
this.delayTrigger = new DelayTrigger();
|
|
|
|
this.delayTrigger.OnExecute = OnDelayTriggerExecute;
|
|
|
|
PurchaseSchemeProductSkuList = new ObservableCollection<PurchaseSchemeProductSku>();
|
|
|
|
purchaseSchemeList = new List<PurchaseScheme>();
|
|
|
|
FastCreateOrderCommand = new RelayCommand(FastCreateOrder);
|
|
|
|
PreviewOrderCommand = new RelayCommand(PreviewOrder);
|
|
|
|
this.globalContext = globalContext;
|
|
|
|
this.orderVM = orderVM;
|
|
|
|
//PurchaseOrderMode = PurchaseOrderMode.代发;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData(Order order, Purchaser purchaser, PurchaseAccount purchaseAccount)
|
|
|
|
{
|
|
|
|
this.order = order;
|
|
|
|
this.purchaser = purchaser;
|
|
|
|
this.purchaseAccount = purchaseAccount;
|
|
|
|
|
|
|
|
this.ContactName = order.Consignee.ContactName;
|
|
|
|
this.Address = order.Consignee.Address;
|
|
|
|
this.Province = order.Consignee.Province;
|
|
|
|
this.City = order.Consignee.City;
|
|
|
|
this.County = order.Consignee.County;
|
|
|
|
this.Town = order.Consignee.Town;
|
|
|
|
this.Mobile = order.Consignee.Mobile;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Load()
|
|
|
|
{
|
|
|
|
IsLoading = true;
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() => purchaseService.GetPurchaseSchemeList(order.ItemList.Select(osku => osku.SkuId).ToList(), purchaser.Id, globalContext.User.Shop.ShopId))
|
|
|
|
.ContinueWith(r =>
|
|
|
|
{
|
|
|
|
var purchaseSchemeResponse = r.Result;
|
|
|
|
if (!purchaseSchemeResponse.Success)
|
|
|
|
{
|
|
|
|
IsLoading = false;
|
|
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(purchaseSchemeResponse.Msg, "获取采购方案"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var waitList = new List<WaitHandle>();
|
|
|
|
foreach (var purchaseSchemeApiModel in purchaseSchemeResponse.Data)
|
|
|
|
{
|
|
|
|
var purchaseScheme = PurchaseScheme.Convert(purchaseSchemeApiModel);
|
|
|
|
purchaseSchemeList.Add(purchaseScheme);
|
|
|
|
foreach (var purchaseSchemeProduct in purchaseScheme.PurchaseSchemeProductList)
|
|
|
|
{
|
|
|
|
var ewh = new ManualResetEvent(false);
|
|
|
|
waitList.Add(ewh);
|
|
|
|
var orderSku = order.ItemList.FirstOrDefault(osku => osku.SkuId == purchaseScheme.SkuId);
|
|
|
|
Task.Factory.StartNew(() => LoadPurchaseProduct(purchaseScheme.PurchasePlatform, purchaseSchemeProduct, orderSku, ewh));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WaitHandle.WaitAll(waitList.ToArray());
|
|
|
|
//IsLoading = false;
|
|
|
|
if (PurchaseSchemeProductSkuList.Count() > 0)
|
|
|
|
OnDelayTriggerExecute(Guid.NewGuid().ToString());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IsLoading = false;
|
|
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show("采购方案商品加载失败,请重新打开预览窗口", "提示"));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Unload()
|
|
|
|
{
|
|
|
|
order = null;
|
|
|
|
purchaser = null;
|
|
|
|
purchaseAccount = null;
|
|
|
|
|
|
|
|
purchaseSchemeList.Clear();
|
|
|
|
PurchaseSchemeProductSkuList.Clear();
|
|
|
|
extensions = string.Empty;
|
|
|
|
ProductAmount = FreightAmount = TotalAmount = 0;
|
|
|
|
ContactName = Address = Mobile = Province = City = County = Town = PrucahseRemark = string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadPurchaseProduct(Platform platform, PurchaseSchemeProduct purchaseSchemeProduct, OrderSku orderSku, ManualResetEvent ewh)
|
|
|
|
{
|
|
|
|
var data = purchaseProductAPIService.GetProductInfo(platform,
|
|
|
|
purchaseSchemeProduct.ProductId,
|
|
|
|
purchaseSchemeProduct.SkuId,
|
|
|
|
purchaseSchemeProduct.PurchaseProductId,
|
|
|
|
PurchaseOrderMode,
|
|
|
|
PurchaseProductAPIMode.Spider);
|
|
|
|
if (data != null && data.Value.purchaseSchemeProductSkus != null && data.Value.purchaseSchemeProductSkus.Count > 0)
|
|
|
|
{
|
|
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
|
|
{
|
|
|
|
foreach (var purchaseSchemeProductSku in data.Value.purchaseSchemeProductSkus)
|
|
|
|
{
|
|
|
|
if (purchaseSchemeProduct.SelectedSkuIdList.Any(s => s == purchaseSchemeProductSku.PurchaseSkuId))
|
|
|
|
{
|
|
|
|
PurchaseSchemeProductSkuList.Add(purchaseSchemeProductSku);
|
|
|
|
purchaseSchemeProductSku.ItemTotal = orderSku.ItemTotal;
|
|
|
|
purchaseSchemeProductSku.OnItemTotalChanged = OnItemTotalChanged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
ewh.Set();
|
|
|
|
ewh.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnItemTotalChanged(int itemTotal)
|
|
|
|
{
|
|
|
|
Console.WriteLine($"OnItemTotalChanged {DateTime.Now} {itemTotal}");
|
|
|
|
this.delayTrigger.SetKey(itemTotal.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
extensions = 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 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;
|
|
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(extensions))
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
PrucahseRemark,
|
|
|
|
order.Id,
|
|
|
|
globalContext.User.Shop.ShopId,
|
|
|
|
purchaseSchemeList[0].PurchaserName,
|
|
|
|
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);
|
|
|
|
orderVM.RefreshOrder(order.Id);
|
|
|
|
|
|
|
|
//关闭当前窗口
|
|
|
|
//GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<object>(null, "OnlinePurchase_Close");
|
|
|
|
WeakReferenceMessenger.Default.Send(new Message_OnlinePurchase_Close(null));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PreviewOrder()
|
|
|
|
{
|
|
|
|
OnDelayTriggerExecute(Guid.NewGuid().ToString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|