|
|
|
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 ICommand EditQuantityRatioCommand { get; set; }
|
|
|
|
|
|
|
|
public ICommand AddQuantityCommand { get; set; }
|
|
|
|
|
|
|
|
public ICommand SubtractQuantityCommand { get; set; }
|
|
|
|
|
|
|
|
//public IList<PurchaseSchemeProductSku> PurchaseSchemeProductSkuList { get; set; }
|
|
|
|
|
|
|
|
public IList<ProductSkuWithScheme> ProductSkuWithSchemeList { 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.代发;
|
|
|
|
|
|
|
|
private IList<PurchaseScheme> schemeList;
|
|
|
|
/// <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;
|
|
|
|
ProductSkuWithSchemeList = new ObservableCollection<ProductSkuWithScheme>();
|
|
|
|
purchaseSchemeList = new List<PurchaseScheme>();
|
|
|
|
FastCreateOrderCommand = new RelayCommand(FastCreateOrder);
|
|
|
|
PreviewOrderCommand = new RelayCommand(PreviewOrder);
|
|
|
|
EditQuantityRatioCommand = new RelayCommand<object>(EditQuantityRatio);
|
|
|
|
AddQuantityCommand = new RelayCommand<PurchaseSchemeProductSku>(AddQuantity);
|
|
|
|
SubtractQuantityCommand = new RelayCommand<PurchaseSchemeProductSku>(SubtractQuantity);
|
|
|
|
this.globalContext = globalContext;
|
|
|
|
this.orderVM = orderVM;
|
|
|
|
//PurchaseOrderMode = PurchaseOrderMode.代发;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData(Order order, IList<PurchaseScheme> schemeList)
|
|
|
|
{
|
|
|
|
this.order = order;
|
|
|
|
//this.purchaser = purchaser;
|
|
|
|
//this.purchaseAccount = purchaseAccount;
|
|
|
|
this.schemeList = schemeList;
|
|
|
|
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;
|
|
|
|
var waitList = new List<EventWaitHandle>();
|
|
|
|
foreach (var scheme in schemeList)
|
|
|
|
{
|
|
|
|
var orderSku = order.ItemList.FirstOrDefault(osku => osku.SkuId == scheme.SkuId);
|
|
|
|
var productSkuWithScheme = new ProductSkuWithScheme()
|
|
|
|
{
|
|
|
|
SkuId = scheme.SkuId,
|
|
|
|
Id = scheme.SkuId,
|
|
|
|
Logo = orderSku.Logo,
|
|
|
|
Price = orderSku.Price.Value,
|
|
|
|
ProductId = scheme.ProductId,
|
|
|
|
PurchasePlatform = scheme.PurchasePlatform,
|
|
|
|
PurchaserId = scheme.PurchaserId,
|
|
|
|
PurchaserName = scheme.PurchaserName,
|
|
|
|
PurchaseSchemeId = scheme.Id,
|
|
|
|
Quantity = orderSku.ItemTotal,
|
|
|
|
Title = orderSku.Title
|
|
|
|
};
|
|
|
|
ProductSkuWithSchemeList.Add(productSkuWithScheme);
|
|
|
|
|
|
|
|
var ewh = new ManualResetEvent(false);
|
|
|
|
waitList.Add(ewh);
|
|
|
|
Task.Factory.StartNew(() => LoadPurchaseScheme(productSkuWithScheme, scheme, ewh));
|
|
|
|
}
|
|
|
|
|
|
|
|
Task.Factory.StartNew(() =>
|
|
|
|
{
|
|
|
|
WaitHandle.WaitAll(waitList.ToArray());
|
|
|
|
PreviewOrder(); //预览订单
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadPurchaseScheme(ProductSkuWithScheme productSkuWithScheme, PurchaseScheme purchaseScheme, EventWaitHandle ewh)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
#region 完善采购方案基本信息
|
|
|
|
foreach (var purchaseSchemeProduct in purchaseScheme.PurchaseSchemeProductList)
|
|
|
|
{
|
|
|
|
var data = purchaseProductAPIService.GetProductInfo(productSkuWithScheme.PurchasePlatform.Value,
|
|
|
|
productSkuWithScheme.ProductId,
|
|
|
|
productSkuWithScheme.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))
|
|
|
|
{
|
|
|
|
productSkuWithScheme.PurchaseSchemeProductSkuList.Add(purchaseSchemeProductSku);
|
|
|
|
|
|
|
|
purchaseSchemeProductSku.ItemTotal = productSkuWithScheme.Quantity;
|
|
|
|
purchaseSchemeProductSku.OnItemTotalChanged = OnItemTotalChanged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void EditQuantityRatio(object param)
|
|
|
|
{
|
|
|
|
var paramList = (object[])param;
|
|
|
|
var skuQuantity = Convert.ToInt32(paramList[0]);
|
|
|
|
var purchaseSchemeProductSku = paramList[1] as PurchaseSchemeProductSku;
|
|
|
|
|
|
|
|
var editWindow = new EditQuantityRatioWindow(purchaseSchemeProductSku.QuantityRatio);
|
|
|
|
if (editWindow.ShowDialog() == true)
|
|
|
|
{
|
|
|
|
var quantityRatio = editWindow.QuantityRatio;
|
|
|
|
purchaseSchemeProductSku.QuantityRatio = quantityRatio;
|
|
|
|
purchaseSchemeProductSku.ItemTotal = quantityRatio * skuQuantity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void AddQuantity(PurchaseSchemeProductSku purchaseSchemeProductSku)
|
|
|
|
{
|
|
|
|
purchaseSchemeProductSku.ItemTotal++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SubtractQuantity(PurchaseSchemeProductSku purchaseSchemeProductSku)
|
|
|
|
{
|
|
|
|
if (purchaseSchemeProductSku.ItemTotal > 1)
|
|
|
|
purchaseSchemeProductSku.ItemTotal--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|