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.
314 lines
13 KiB
314 lines
13 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.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
|
|
namespace BBWY.Client.ViewModels
|
|
{
|
|
public class _1688PreviewPurchaseViewModel : BaseVM
|
|
{
|
|
public ICommand FastCreateOrderCommand { get; set; }
|
|
|
|
public IList<PurchaseSchemeProductSku> PurchaseSchemeProductSkuList { get; set; }
|
|
|
|
public PurchaseScheme PurchaseScheme { get; set; }
|
|
public PurchaseAccount PurchaseAccount { 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 OrderListViewModel orderListViewModel;
|
|
private GlobalContext globalContext;
|
|
private string orderId;
|
|
private int skuItemCount;
|
|
private bool isLoading;
|
|
private OneBoundAPIService oneBoundAPIService;
|
|
private PurchaseOrderService purchaseOrderService;
|
|
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;
|
|
|
|
public _1688PreviewPurchaseViewModel(OneBoundAPIService oneBoundAPIService, PurchaseOrderService purchaseOrderService, GlobalContext globalContext, OrderListViewModel orderListViewModel)
|
|
{
|
|
this.oneBoundAPIService = oneBoundAPIService;
|
|
this.purchaseOrderService = purchaseOrderService;
|
|
this.delayTrigger = new DelayTrigger();
|
|
this.delayTrigger.OnExecute = OnDelayTriggerExecute;
|
|
PurchaseSchemeProductSkuList = new ObservableCollection<PurchaseSchemeProductSku>();
|
|
FastCreateOrderCommand = new RelayCommand(FastCreateOrder);
|
|
this.globalContext = globalContext;
|
|
this.orderListViewModel = orderListViewModel;
|
|
//PurchaseOrderMode = PurchaseOrderMode.代发;
|
|
}
|
|
|
|
public void SetData(string orderId, int skuItemCount, PurchaseScheme purchaseScheme, PurchaseAccount purchaseAccount, Consignee consignee)
|
|
{
|
|
this.orderId = orderId;
|
|
this.skuItemCount = skuItemCount;
|
|
this.PurchaseScheme = purchaseScheme;
|
|
this.PurchaseAccount = purchaseAccount;
|
|
|
|
this.ContactName = consignee.ContactName;
|
|
this.Address = consignee.Address;
|
|
this.Province = consignee.Province;
|
|
this.City = consignee.City;
|
|
this.County = consignee.County;
|
|
this.Town = consignee.Town;
|
|
//this.Mobile = consignee.Mobile;
|
|
}
|
|
|
|
protected override void Load()
|
|
{
|
|
IsLoading = true;
|
|
var waitList = new List<WaitHandle>();
|
|
foreach (var purchaseSchemeProduct in PurchaseScheme.PurchaseSchemeProductList)
|
|
{
|
|
var ewh = new ManualResetEvent(false);
|
|
waitList.Add(ewh);
|
|
Task.Factory.StartNew(() => LoadPurchaseProduct(purchaseSchemeProduct, ewh));
|
|
}
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
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()
|
|
{
|
|
PurchaseSchemeProductSkuList.Clear();
|
|
PurchaseScheme = null;
|
|
orderId = string.Empty;
|
|
tradeMode = string.Empty;
|
|
skuItemCount = 0;
|
|
ProductAmount = FreightAmount = TotalAmount = 0;
|
|
ContactName = Address = Mobile = Province = City = County = Town = PrucahseRemark = string.Empty;
|
|
|
|
}
|
|
|
|
private void LoadPurchaseProduct(PurchaseSchemeProduct purchaseSchemeProduct, ManualResetEvent ewh)
|
|
{
|
|
var purchaseSchemeProductSkuList = LoadPurchaseProductCore(purchaseSchemeProduct.PurchaseProductId, out string errorMsg);
|
|
if (purchaseSchemeProductSkuList != null && purchaseSchemeProductSkuList.Count > 0)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
foreach (var purchaseSchemeProductSku in purchaseSchemeProductSkuList)
|
|
{
|
|
if (purchaseSchemeProduct.SelectedSkuIdList.Any(s => s == purchaseSchemeProductSku.PurchaseSkuId))
|
|
{
|
|
PurchaseSchemeProductSkuList.Add(purchaseSchemeProductSku);
|
|
purchaseSchemeProductSku.ItemTotal = skuItemCount;
|
|
purchaseSchemeProductSku.OnItemTotalChanged = OnItemTotalChanged;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
ewh.Set();
|
|
ewh.Dispose();
|
|
}
|
|
|
|
private IList<PurchaseSchemeProductSku> LoadPurchaseProductCore(string purchseProductId, out string errorMsg)
|
|
{
|
|
errorMsg = string.Empty;
|
|
//1688商品详情接口
|
|
var response = oneBoundAPIService.GetProductInfo("1688", purchseProductId);
|
|
if (!response.Success)
|
|
{
|
|
//记录日志
|
|
|
|
errorMsg = response.Msg;
|
|
Console.WriteLine(errorMsg);
|
|
return null;
|
|
}
|
|
var jobject = response.Data;
|
|
//purchaserId = jobject["item"]["seller_info"].Value<string>("user_num_id");
|
|
//purchaserName = jobject["item"]["seller_info"].Value<string>("title");
|
|
//purchaserLocation = jobject["item"].Value<string>("location");
|
|
//if (checkPurchaserFunc != null)
|
|
//{
|
|
// errorMsg = checkPurchaserFunc(purchaserId);
|
|
// if (!string.IsNullOrEmpty(errorMsg))
|
|
// return null;
|
|
//}
|
|
|
|
var skuJArray = (JArray)jobject["item"]["skus"]["sku"];
|
|
if (skuJArray.Count == 0)
|
|
{
|
|
errorMsg = $"商品{purchseProductId}缺少sku信息";
|
|
return null;
|
|
}
|
|
|
|
return skuJArray.Select(j => new PurchaseSchemeProductSku()
|
|
{
|
|
ProductId = PurchaseScheme.ProductId,
|
|
SkuId = PurchaseScheme.SkuId,
|
|
PurchaseProductId = purchseProductId,
|
|
Price = j.Value<decimal>("price"),
|
|
PurchaseSkuId = j.Value<string>("sku_id"),
|
|
PurchaseSkuSpecId = j.Value<string>("spec_id"),
|
|
Title = j.Value<string>("properties_name"),
|
|
Logo = GetSkuLogo(j, (JArray)jobject["item"]["prop_imgs"]["prop_img"])
|
|
}).ToList();
|
|
}
|
|
|
|
private string GetSkuLogo(JToken skuJToken, JArray prop_img)
|
|
{
|
|
if (!prop_img.HasValues)
|
|
return "pack://application:,,,/Resources/Images/defaultItem.png";
|
|
var properties = skuJToken.Value<string>("properties").Split(';', StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (var p in properties)
|
|
{
|
|
var imgJToken = prop_img.FirstOrDefault(g => g.Value<string>("properties") == p);
|
|
if (imgJToken != null)
|
|
return imgJToken.Value<string>("url");
|
|
}
|
|
return "pack://application:,,,/Resources/Images/defaultItem.png";
|
|
}
|
|
|
|
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))
|
|
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, Platform.阿里巴巴, 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;
|
|
});
|
|
}
|
|
|
|
private void FastCreateOrder()
|
|
{
|
|
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,
|
|
Platform.阿里巴巴,
|
|
PurchaseAccount,
|
|
PurchaseOrderMode,
|
|
tradeMode,
|
|
PrucahseRemark,
|
|
orderId,
|
|
globalContext.User.Shop.ShopId,
|
|
PurchaseAccount.AccountName,
|
|
PurchaseScheme.PurchaserName)).ContinueWith(t =>
|
|
{
|
|
IsLoading = false;
|
|
var r = t.Result;
|
|
if (!r.Success)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(r.Msg, "下单"));
|
|
return;
|
|
}
|
|
|
|
//刷新订单列表
|
|
orderListViewModel.RefreshOrder();
|
|
|
|
//关闭当前窗口
|
|
GalaSoft.MvvmLight.Messaging.Messenger.Default.Send<object>(null, "OnlinePurchase_Close");
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|