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.
375 lines
16 KiB
375 lines
16 KiB
using BBWY.Client.APIServices;
|
|
using BBWY.Client.Models;
|
|
using BBWY.Common.Trigger;
|
|
using GalaSoft.MvvmLight.Command;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BBWY.Client.ViewModels
|
|
{
|
|
public class _1688PreviewPurchaseViewModel : BaseVM
|
|
{
|
|
public ICommand FastCreateOrderCommand { get; set; }
|
|
public ICommand PreviewOrderCommand { get; set; }
|
|
|
|
public IList<PurchaseSchemeProductSku> PurchaseSchemeProductSkuList { get; set; }
|
|
|
|
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());
|
|
}
|
|
}
|
|
|
|
private Purchaser purchaser;
|
|
private Order order;
|
|
private PurchaseAccount purchaseAccount;
|
|
|
|
private IList<PurchaseScheme> purchaseSchemeList;
|
|
private OrderListViewModel orderListViewModel;
|
|
private GlobalContext globalContext;
|
|
private bool isLoading;
|
|
private OneBoundAPIService oneBoundAPIService;
|
|
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 string tradeMode;
|
|
/// <summary>
|
|
/// 扩展数据,暂用于拳探
|
|
/// </summary>
|
|
private string extensions;
|
|
private bool isSkuMatchError;
|
|
|
|
public _1688PreviewPurchaseViewModel(OneBoundAPIService oneBoundAPIService,
|
|
PurchaseService purchaseService,
|
|
PurchaseOrderService purchaseOrderService,
|
|
PurchaseProductAPIService purchaseProductAPIService,
|
|
GlobalContext globalContext,
|
|
OrderListViewModel orderListViewModel)
|
|
{
|
|
this.oneBoundAPIService = oneBoundAPIService;
|
|
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.orderListViewModel = orderListViewModel;
|
|
//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;
|
|
isSkuMatchError = false;
|
|
Task.Factory.StartNew(() => purchaseService.GetPurchaseSchemeList(order.ItemList.Select(osku => osku.Id).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.Id == 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;
|
|
if (isSkuMatchError)
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show("采购方案与当前商品页的数据匹配失败,请删除旧采购方案后重新绑定采购方案", "提示"));
|
|
else
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show("采购方案商品加载失败,请重新打开预览窗口", "提示"));
|
|
}
|
|
});
|
|
}
|
|
|
|
protected override void Unload()
|
|
{
|
|
order = null;
|
|
purchaser = null;
|
|
purchaseAccount = null;
|
|
|
|
purchaseSchemeList.Clear();
|
|
PurchaseSchemeProductSkuList.Clear();
|
|
tradeMode = string.Empty;
|
|
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 (orderSku != null && purchaseSchemeProductSku.SkuId == orderSku.Id)
|
|
{
|
|
purchaseSchemeProductSku.SkuPurchaseSchemeId = purchaseSchemeProduct.SkuPurchaseSchemeId;
|
|
if (purchaseSchemeProduct.SelectedSkuIdList.Any(s => s == purchaseSchemeProductSku.PurchaseSkuId))
|
|
{
|
|
PurchaseSchemeProductSkuList.Add(purchaseSchemeProductSku);
|
|
purchaseSchemeProductSku.ItemTotal = orderSku.ItemTotal;
|
|
purchaseSchemeProductSku.OnItemTotalChanged = OnItemTotalChanged;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
if (purchaseSchemeProduct.SelectedSkuIdList.Any(s => !data.Value.purchaseSchemeProductSkus.Any(s1 => s1.PurchaseSkuId == s)))
|
|
{
|
|
isSkuMatchError = true;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
#region 由于未知原因,会存在不属于当前订单的配件sku,预览时清理一次
|
|
for (var i = 0; i < PurchaseSchemeProductSkuList.Count(); i++)
|
|
{
|
|
var pss = PurchaseSchemeProductSkuList[i];
|
|
if (!order.ItemList.Any(osku => osku.Id == pss.SkuId))
|
|
{
|
|
//这个采购配件不属于这笔订单
|
|
PurchaseSchemeProductSkuList.RemoveAt(i);
|
|
i--;
|
|
}
|
|
}
|
|
if (PurchaseSchemeProductSkuList.Count() == 0)
|
|
{
|
|
MessageBox.Show("采购配件数量为0");
|
|
return;
|
|
}
|
|
#endregion
|
|
|
|
var cargoParamList = PurchaseSchemeProductSkuList.Select(sku => new CargoParam
|
|
{
|
|
ProductId = sku.PurchaseProductId,
|
|
SkuId = sku.PurchaseSkuId,
|
|
SpecId = sku.PurchaseSkuSpecId,
|
|
Quantity = sku.ItemTotal,
|
|
BelongSkuId = sku.SkuId
|
|
}).ToList();
|
|
|
|
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
|
|
}, cargoParamList, 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 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;
|
|
}
|
|
var cargoParamList = PurchaseSchemeProductSkuList.Select(sku => new CargoParam
|
|
{
|
|
ProductId = sku.PurchaseProductId,
|
|
SkuId = sku.PurchaseSkuId,
|
|
SpecId = sku.PurchaseSkuSpecId,
|
|
Quantity = sku.ItemTotal,
|
|
BelongSkuId = sku.SkuId,
|
|
SchemeId = sku.SkuPurchaseSchemeId
|
|
}).ToList();
|
|
|
|
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
|
|
}, cargoParamList,
|
|
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<object>(null, "OnlinePurchase_Close");
|
|
});
|
|
}
|
|
|
|
private void PreviewOrder()
|
|
{
|
|
OnDelayTriggerExecute(Guid.NewGuid().ToString());
|
|
}
|
|
}
|
|
}
|
|
|