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.

360 lines
15 KiB

2 years ago
using BBWYB.Client.APIServices;
using BBWYB.Client.Models;
2 years ago
using BBWYB.Client.Views.Purchase;
2 years ago
using BBWYB.Common.Trigger;
using CommunityToolkit.Mvvm.Input;
2 years ago
using CommunityToolkit.Mvvm.Messaging;
2 years ago
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; }
2 years ago
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; }
2 years ago
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;
2 years ago
private OrderViewModel orderVM;
2 years ago
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.;
2 years ago
private IList<PurchaseScheme> schemeList;
2 years ago
/// <summary>
/// 扩展数据,暂用于拳探
/// </summary>
private string extensions;
public OnlinePurchaseViewModel(PurchaseService purchaseService,
2 years ago
PurchaseOrderService purchaseOrderService,
PurchaseProductAPIService purchaseProductAPIService,
GlobalContext globalContext,
OrderViewModel orderVM)
2 years ago
{
this.purchaseOrderService = purchaseOrderService;
this.purchaseProductAPIService = purchaseProductAPIService;
this.purchaseService = purchaseService;
this.delayTrigger = new DelayTrigger();
this.delayTrigger.OnExecute = OnDelayTriggerExecute;
2 years ago
ProductSkuWithSchemeList = new ObservableCollection<ProductSkuWithScheme>();
2 years ago
purchaseSchemeList = new List<PurchaseScheme>();
FastCreateOrderCommand = new RelayCommand(FastCreateOrder);
PreviewOrderCommand = new RelayCommand(PreviewOrder);
2 years ago
EditQuantityRatioCommand = new RelayCommand<object>(EditQuantityRatio);
AddQuantityCommand = new RelayCommand<PurchaseSchemeProductSku>(AddQuantity);
SubtractQuantityCommand = new RelayCommand<PurchaseSchemeProductSku>(SubtractQuantity);
2 years ago
this.globalContext = globalContext;
2 years ago
this.orderVM = orderVM;
2 years ago
//PurchaseOrderMode = PurchaseOrderMode.代发;
}
2 years ago
public void SetData(Order order, IList<PurchaseScheme> schemeList)
2 years ago
{
this.order = order;
2 years ago
//this.purchaser = purchaser;
//this.purchaseAccount = purchaseAccount;
2 years ago
this.schemeList = schemeList;
2 years ago
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;
2 years ago
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);
2 years ago
2 years ago
var ewh = new ManualResetEvent(false);
waitList.Add(ewh);
Task.Factory.StartNew(() => LoadPurchaseScheme(productSkuWithScheme, scheme, ewh));
}
2 years ago
2 years ago
Task.Factory.StartNew(() =>
{
WaitHandle.WaitAll(waitList.ToArray());
PreviewOrder(); //预览订单
});
2 years ago
}
2 years ago
private void LoadPurchaseScheme(ProductSkuWithScheme productSkuWithScheme, PurchaseScheme purchaseScheme, EventWaitHandle ewh)
2 years ago
{
2 years ago
try
2 years ago
{
2 years ago
#region 完善采购方案基本信息
foreach (var purchaseSchemeProduct in purchaseScheme.PurchaseSchemeProductList)
2 years ago
{
2 years ago
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)
2 years ago
{
2 years ago
App.Current.Dispatcher.Invoke(() =>
2 years ago
{
2 years ago
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;
}
}
});
2 years ago
}
2 years ago
}
#endregion
2 years ago
}
2 years ago
catch
{
2 years ago
2 years ago
}
finally
{
ewh.Set();
ewh.Dispose();
}
2 years ago
}
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;
}
2 years ago
//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;
// });
2 years ago
}
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;
}
2 years ago
if (string.IsNullOrEmpty(extensions))
{
MessageBox.Show("缺少报价扩展数据", "下单");
return;
}
2 years ago
2 years ago
//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));
// });
2 years ago
}
private void PreviewOrder()
{
OnDelayTriggerExecute(Guid.NewGuid().ToString());
}
2 years ago
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--;
}
2 years ago
}
}