|
|
|
using BBWY.Client.APIServices;
|
|
|
|
using BBWY.Client.Models;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace BBWY.Client.ViewModels
|
|
|
|
{
|
|
|
|
public class _1688PreviewPurchaseViewModel : BaseVM
|
|
|
|
{
|
|
|
|
public IList<PurchaseSchemeProductSku> PurchaseSchemeProductSkuList { get; set; }
|
|
|
|
|
|
|
|
public PurchaseScheme PurchaseScheme { get; set; }
|
|
|
|
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
|
|
|
|
|
|
|
|
private string orderId;
|
|
|
|
private int skuItemCount;
|
|
|
|
private bool isLoading;
|
|
|
|
private OneBoundAPIService oneBoundAPIService;
|
|
|
|
|
|
|
|
public _1688PreviewPurchaseViewModel(OneBoundAPIService oneBoundAPIService)
|
|
|
|
{
|
|
|
|
this.oneBoundAPIService = oneBoundAPIService;
|
|
|
|
PurchaseSchemeProductSkuList = new ObservableCollection<PurchaseSchemeProductSku>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetData(string orderId, int skuItemCount, PurchaseScheme purchaseScheme)
|
|
|
|
{
|
|
|
|
this.orderId = orderId;
|
|
|
|
this.skuItemCount = skuItemCount;
|
|
|
|
this.PurchaseScheme = purchaseScheme;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void Unload()
|
|
|
|
{
|
|
|
|
PurchaseSchemeProductSkuList.Clear();
|
|
|
|
PurchaseScheme = null;
|
|
|
|
orderId = string.Empty;
|
|
|
|
skuItemCount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void LoadPurchaseProduct(PurchaseSchemeProduct purchaseSchemeProduct, ManualResetEvent ewh)
|
|
|
|
{
|
|
|
|
var purchaseSchemeProductSkuList = LoadPurchaseProductCore(purchaseSchemeProduct.PurchaseProductId, out string errorMsg);
|
|
|
|
if (purchaseSchemeProductSkuList != null && purchaseSchemeProductSkuList.Count > 0)
|
|
|
|
{
|
|
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
|
|
{
|
|
|
|
foreach (var purchaseSchemeProductSku in purchaseSchemeProductSkuList)
|
|
|
|
{
|
|
|
|
if (purchaseSchemeProduct.SelectedSkuIdList.Any(s => s == purchaseSchemeProductSku.PurchaseSkuId))
|
|
|
|
{
|
|
|
|
PurchaseSchemeProductSkuList.Add(purchaseSchemeProductSku);
|
|
|
|
purchaseSchemeProductSku.ItemTotal = skuItemCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|