|
|
@ -4,9 +4,11 @@ using BBWYB.Client.Views.Ware; |
|
|
|
using BBWYB.Common.Models; |
|
|
|
using CommunityToolkit.Mvvm.Input; |
|
|
|
using CommunityToolkit.Mvvm.Messaging; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Collections.ObjectModel; |
|
|
|
using System.Linq; |
|
|
|
using System.Text.RegularExpressions; |
|
|
|
using System.Threading; |
|
|
@ -16,17 +18,17 @@ using System.Windows.Input; |
|
|
|
|
|
|
|
namespace BBWYB.Client.ViewModels |
|
|
|
{ |
|
|
|
public class BindingPurchaseProductViewModel : BaseVM, IDenpendency |
|
|
|
public class BindingPurchaseProductViewModel : BaseVM |
|
|
|
{ |
|
|
|
#region Properties
|
|
|
|
private GlobalContext globalContext; |
|
|
|
private PurchaseService purchaseService; |
|
|
|
private PurchaseProductAPIService purchaseProductAPIService; |
|
|
|
|
|
|
|
private string purchaserName; |
|
|
|
//private string purchaserName;
|
|
|
|
private bool isLoading; |
|
|
|
|
|
|
|
private Platform purchasePlatform; |
|
|
|
private long? schemeId; |
|
|
|
//private Platform purchasePlatform;
|
|
|
|
|
|
|
|
|
|
|
|
private IDictionary<Platform, string> urlPatternDictionary; |
|
|
@ -34,15 +36,18 @@ namespace BBWYB.Client.ViewModels |
|
|
|
|
|
|
|
public IList<ProductSku> ProductSkuList { get; set; } |
|
|
|
|
|
|
|
public string PurchaserId { get; set; } |
|
|
|
//public string PurchaserId { get; set; }
|
|
|
|
|
|
|
|
//public string PurchaserId2 { get; set; }
|
|
|
|
|
|
|
|
public string PurchaserId2 { get; set; } |
|
|
|
//public string PurchaserName { get => purchaserName; set { SetProperty(ref purchaserName, value); } }
|
|
|
|
//public string PurchaserLocation { get; set; }
|
|
|
|
|
|
|
|
public string PurchaserName { get => purchaserName; set { SetProperty(ref purchaserName, value); } } |
|
|
|
public string PurchaserLocation { get; set; } |
|
|
|
public bool IsLoading { get => isLoading; set { SetProperty(ref isLoading, value); } } |
|
|
|
|
|
|
|
public Platform PurchasePlatform { get => purchasePlatform; set { SetProperty(ref purchasePlatform, value); } } |
|
|
|
public long? SchemeId { get => schemeId; set { SetProperty(ref schemeId, value); } } |
|
|
|
|
|
|
|
//public Platform PurchasePlatform { get => purchasePlatform; set { SetProperty(ref purchasePlatform, value); } }
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
@ -86,68 +91,130 @@ namespace BBWYB.Client.ViewModels |
|
|
|
{ Platform.阿里巴巴,@"^(https://detail.1688.com/offer/(\d+).html)[^\s]*" }, |
|
|
|
//{ Platform.拳探, @"^(https://qt.qiyue666.com/goods_detail/(\d+))(\?\w*)?$" }
|
|
|
|
}; |
|
|
|
ProductSkuList = new ObservableCollection<ProductSku>(); |
|
|
|
} |
|
|
|
|
|
|
|
public void SetData(IList<ProductSku> productSkuList, string purchaserId, string purchaserId2, string purchaserName, Platform purchasePlatform) |
|
|
|
//public void SetData(IList<ProductSku> productSkuList, string purchaserId, string purchaserId2, string purchaserName, Platform purchasePlatform)
|
|
|
|
//{
|
|
|
|
// this.ProductSkuList = productSkuList;
|
|
|
|
// //this.Product = product;
|
|
|
|
// this.PurchaserId = purchaserId;
|
|
|
|
// this.PurchaserId2 = purchaserId2;
|
|
|
|
// this.PurchaserName = purchaserName;
|
|
|
|
// this.PurchasePlatform = purchasePlatform;
|
|
|
|
//}
|
|
|
|
|
|
|
|
public void SetData(IList<ProductSku> productSkuList, long? schemeId) |
|
|
|
{ |
|
|
|
this.ProductSkuList = productSkuList; |
|
|
|
foreach (var productSku in productSkuList) |
|
|
|
ProductSkuList.Add(productSku); |
|
|
|
this.SchemeId = schemeId; |
|
|
|
//this.Product = product;
|
|
|
|
this.PurchaserId = purchaserId; |
|
|
|
this.PurchaserId2 = purchaserId2; |
|
|
|
this.PurchaserName = purchaserName; |
|
|
|
this.PurchasePlatform = purchasePlatform; |
|
|
|
//this.PurchaserId = purchaserId;
|
|
|
|
//this.PurchaserId2 = purchaserId2;
|
|
|
|
//this.PurchaserName = purchaserName;
|
|
|
|
//this.PurchasePlatform = purchasePlatform;
|
|
|
|
} |
|
|
|
|
|
|
|
protected override void Load() |
|
|
|
{ |
|
|
|
if (!string.IsNullOrEmpty(PurchaserId)) |
|
|
|
if (this.SchemeId != null) |
|
|
|
{ |
|
|
|
IsLoading = true; |
|
|
|
Task.Factory.StartNew(() => purchaseService.GetPurchaseSchemeList(ProductSkuList.Select(s => s.Id).ToList(), PurchaserId, globalContext.User.Shop.ShopId, purchasePlatform: PurchasePlatform)).ContinueWith(r => |
|
|
|
{ |
|
|
|
var apiResponse = r.Result; |
|
|
|
if (!apiResponse.Success) |
|
|
|
{ |
|
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate |
|
|
|
{ |
|
|
|
MessageBox.Show(apiResponse.Msg, "查询采购方案"); |
|
|
|
}); |
|
|
|
IsLoading = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
var purchaseSchemeList = apiResponse.Data; |
|
|
|
|
|
|
|
var waitList = new List<EventWaitHandle>(); |
|
|
|
foreach (var sku in ProductSkuList) |
|
|
|
{ |
|
|
|
//当前SKU下当前采购商的采购方案
|
|
|
|
var apiScheme = purchaseSchemeList.FirstOrDefault(s => s.SkuId == sku.Id && s.PurchaserId == PurchaserId); |
|
|
|
|
|
|
|
if (apiScheme == null) |
|
|
|
continue; |
|
|
|
|
|
|
|
sku.SelectedPurchaseScheme = PurchaseScheme.Convert(apiScheme); |
|
|
|
var ewh = new ManualResetEvent(false); |
|
|
|
waitList.Add(ewh); |
|
|
|
|
|
|
|
Task.Factory.StartNew(() => |
|
|
|
{ |
|
|
|
foreach (var purchaseSchemeProduct in sku.SelectedPurchaseScheme.PurchaseSchemeProductList) |
|
|
|
{ |
|
|
|
purchaseSchemeProduct.IsEditing = false; |
|
|
|
LoadPurchaseProduct(sku.SelectedPurchaseScheme.PurchasePlatform, purchaseSchemeProduct, purchaseSchemeProduct.PurchaseProductId, null, out _); |
|
|
|
} |
|
|
|
ewh.Set(); |
|
|
|
ewh.Dispose(); |
|
|
|
}); |
|
|
|
} |
|
|
|
WaitHandle.WaitAll(waitList.ToArray()); |
|
|
|
IsLoading = false; |
|
|
|
}); |
|
|
|
var skuIdList = ProductSkuList.Select(s => s.Id).ToList(); |
|
|
|
var sku = ProductSkuList.FirstOrDefault(); |
|
|
|
Task.Factory.StartNew(() => purchaseService.GetPurchaseSchemeList(skuIdList, shopId: globalContext.User.Shop.ShopId, schemeId: SchemeId)).ContinueWith(r => |
|
|
|
{ |
|
|
|
var apiResponse = r.Result; |
|
|
|
if (!apiResponse.Success) |
|
|
|
{ |
|
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate |
|
|
|
{ |
|
|
|
MessageBox.Show(apiResponse.Msg, "查询采购方案"); |
|
|
|
}); |
|
|
|
IsLoading = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
var purchaseSchemeList = apiResponse.Data; |
|
|
|
var apiScheme = purchaseSchemeList.FirstOrDefault(); |
|
|
|
if (apiScheme == null) |
|
|
|
{ |
|
|
|
IsLoading = false; |
|
|
|
return; |
|
|
|
} |
|
|
|
sku.SelectedPurchaseScheme = PurchaseScheme.Convert(apiScheme); |
|
|
|
var ewh = new ManualResetEvent(false); |
|
|
|
|
|
|
|
var waitList = new List<EventWaitHandle>(); |
|
|
|
waitList.Add(ewh); |
|
|
|
|
|
|
|
Task.Factory.StartNew(() => |
|
|
|
{ |
|
|
|
foreach (var purchaseSchemeProduct in sku.SelectedPurchaseScheme.PurchaseSchemeProductList) |
|
|
|
{ |
|
|
|
purchaseSchemeProduct.IsEditing = false; |
|
|
|
LoadPurchaseProduct(purchaseSchemeProduct.PurchasePlatform, purchaseSchemeProduct, purchaseSchemeProduct.PurchaseProductId, null, out _); |
|
|
|
} |
|
|
|
ewh.Set(); |
|
|
|
ewh.Dispose(); |
|
|
|
}); |
|
|
|
WaitHandle.WaitAll(waitList.ToArray()); |
|
|
|
IsLoading = false; |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//protected override void Load()
|
|
|
|
//{
|
|
|
|
// if (!string.IsNullOrEmpty(PurchaserId))
|
|
|
|
// {
|
|
|
|
// IsLoading = true;
|
|
|
|
// Task.Factory.StartNew(() => purchaseService.GetPurchaseSchemeList(ProductSkuList.Select(s => s.Id).ToList(), PurchaserId, globalContext.User.Shop.ShopId, purchasePlatform: PurchasePlatform)).ContinueWith(r =>
|
|
|
|
// {
|
|
|
|
// var apiResponse = r.Result;
|
|
|
|
// if (!apiResponse.Success)
|
|
|
|
// {
|
|
|
|
// App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
|
|
// {
|
|
|
|
// MessageBox.Show(apiResponse.Msg, "查询采购方案");
|
|
|
|
// });
|
|
|
|
// IsLoading = false;
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// var purchaseSchemeList = apiResponse.Data;
|
|
|
|
|
|
|
|
// var waitList = new List<EventWaitHandle>();
|
|
|
|
// foreach (var sku in ProductSkuList)
|
|
|
|
// {
|
|
|
|
// //当前SKU下当前采购商的采购方案
|
|
|
|
// var apiScheme = purchaseSchemeList.FirstOrDefault(s => s.SkuId == sku.Id && s.PurchaserId == PurchaserId);
|
|
|
|
|
|
|
|
// if (apiScheme == null)
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
// sku.SelectedPurchaseScheme = PurchaseScheme.Convert(apiScheme);
|
|
|
|
// var ewh = new ManualResetEvent(false);
|
|
|
|
// waitList.Add(ewh);
|
|
|
|
|
|
|
|
// Task.Factory.StartNew(() =>
|
|
|
|
// {
|
|
|
|
// foreach (var purchaseSchemeProduct in sku.SelectedPurchaseScheme.PurchaseSchemeProductList)
|
|
|
|
// {
|
|
|
|
// purchaseSchemeProduct.IsEditing = false;
|
|
|
|
// LoadPurchaseProduct(sku.SelectedPurchaseScheme.PurchasePlatform, purchaseSchemeProduct, purchaseSchemeProduct.PurchaseProductId, null, out _);
|
|
|
|
// }
|
|
|
|
// ewh.Set();
|
|
|
|
// ewh.Dispose();
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// WaitHandle.WaitAll(waitList.ToArray());
|
|
|
|
// IsLoading = false;
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
public void LoadPurchaseProduct(Platform platform, PurchaseSchemeProduct purchaseSchemeProduct, string purchaseProductId, Func<string, string> checkPurchaserFunc, out string errorMsg) |
|
|
|
{ |
|
|
|
errorMsg = string.Empty; |
|
|
@ -180,10 +247,15 @@ namespace BBWYB.Client.ViewModels |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
PurchaserId = data.Value.purchaser.Id; |
|
|
|
PurchaserId2 = data.Value.purchaser.Id2; |
|
|
|
PurchaserName = data.Value.purchaser.Name; |
|
|
|
PurchaserLocation = data.Value.purchaser.Location; |
|
|
|
purchaseSchemeProduct.PurchaserId = data.Value.purchaser.Id; |
|
|
|
purchaseSchemeProduct.PurchaserId2 = data.Value.purchaser.Id2; |
|
|
|
purchaseSchemeProduct.PurchaserName = data.Value.purchaser.Name; |
|
|
|
purchaseSchemeProduct.PurchaserLocation = data.Value.purchaser.Location; |
|
|
|
|
|
|
|
//PurchaserId = data.Value.purchaser.Id;
|
|
|
|
//PurchaserId2 = data.Value.purchaser.Id2;
|
|
|
|
//PurchaserName = data.Value.purchaser.Name;
|
|
|
|
//PurchaserLocation = data.Value.purchaser.Location;
|
|
|
|
|
|
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate |
|
|
|
{ |
|
|
@ -214,8 +286,7 @@ namespace BBWYB.Client.ViewModels |
|
|
|
productSku.SelectedPurchaseScheme = new PurchaseScheme() |
|
|
|
{ |
|
|
|
ProductId = productSku.ProductId, |
|
|
|
SkuId = productSku.Id, |
|
|
|
PurchasePlatform = PurchasePlatform |
|
|
|
SkuId = productSku.Id |
|
|
|
}; |
|
|
|
} |
|
|
|
else if (productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Count >= 4) |
|
|
@ -229,7 +300,8 @@ namespace BBWYB.Client.ViewModels |
|
|
|
Id = DateTime.Now.ToFileTime(), |
|
|
|
IsEditing = true, |
|
|
|
ProductId = productSku.ProductId, |
|
|
|
SkuId = productSku.Id |
|
|
|
SkuId = productSku.Id, |
|
|
|
PurchasePlatform = Platform.阿里巴巴 //B端写死只能1688
|
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
@ -237,8 +309,8 @@ namespace BBWYB.Client.ViewModels |
|
|
|
{ |
|
|
|
var productSku = ProductSkuList.FirstOrDefault(sku => sku.Id == purchaseSchemeProduct.SkuId); |
|
|
|
productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Remove(purchaseSchemeProduct); |
|
|
|
if (!ProductSkuList.Any(s => s.SelectedPurchaseScheme != null && s.SelectedPurchaseScheme.PurchaseSchemeProductList.Count > 0)) |
|
|
|
PurchaserId = string.Empty; |
|
|
|
//if (!ProductSkuList.Any(s => s.SelectedPurchaseScheme != null && s.SelectedPurchaseScheme.PurchaseSchemeProductList.Count > 0))
|
|
|
|
// PurchaserId = string.Empty;
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -254,7 +326,7 @@ namespace BBWYB.Client.ViewModels |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
urlPatternDictionary.TryGetValue(PurchasePlatform, out string pattern); |
|
|
|
urlPatternDictionary.TryGetValue(purchaseSchemeProduct.PurchasePlatform, out string pattern); |
|
|
|
|
|
|
|
var match = Regex.Match(purchaseSchemeProduct.PurchaseUrl, pattern); |
|
|
|
if (!match.Success) |
|
|
@ -276,13 +348,13 @@ namespace BBWYB.Client.ViewModels |
|
|
|
|
|
|
|
Task.Factory.StartNew(() => |
|
|
|
{ |
|
|
|
LoadPurchaseProduct(sku.SelectedPurchaseScheme.PurchasePlatform, purchaseSchemeProduct, purchaseProductId, (p) => |
|
|
|
LoadPurchaseProduct(purchaseSchemeProduct.PurchasePlatform, purchaseSchemeProduct, purchaseProductId, (p) => |
|
|
|
{ |
|
|
|
if (sku.PurchaseSchemeList.Any(s => s.Id != sku.SelectedPurchaseScheme.Id && s.PurchaserId == p)) |
|
|
|
return $"sku{sku.Id}的采购方案中已存在相同的采购商"; //同一个sku中的采购方案不能有相同的采购商
|
|
|
|
//if (sku.PurchaseSchemeList.Any(s => s.Id != sku.SelectedPurchaseScheme.Id && s.PurchaserId == p))
|
|
|
|
// return $"sku{sku.Id}的采购方案中已存在相同的采购商"; //同一个sku中的采购方案不能有相同的采购商
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(PurchaserId) && p != PurchaserId) //同一批操作的sku必须为相同的采购商
|
|
|
|
return "采购商必须相同"; |
|
|
|
//if (!string.IsNullOrEmpty(PurchaserId) && p != PurchaserId) //同一批操作的sku必须为相同的采购商
|
|
|
|
// return "采购商必须相同";
|
|
|
|
|
|
|
|
return string.Empty; |
|
|
|
}, out string errorMsg); |
|
|
@ -319,10 +391,10 @@ namespace BBWYB.Client.ViewModels |
|
|
|
purchaseSchemeProduct.IsEditing = false; |
|
|
|
|
|
|
|
var productSku = ProductSkuList.FirstOrDefault(sku => sku.Id == purchaseSchemeProduct.SkuId); |
|
|
|
productSku.SelectedPurchaseScheme.PurchaserId = PurchaserId; |
|
|
|
productSku.SelectedPurchaseScheme.PurchaserId2 = PurchaserId2; |
|
|
|
productSku.SelectedPurchaseScheme.PurchaserName = PurchaserName; |
|
|
|
productSku.SelectedPurchaseScheme.PurchaserLocation = PurchaserLocation; |
|
|
|
//productSku.SelectedPurchaseScheme.PurchaserId = PurchaserId;
|
|
|
|
//productSku.SelectedPurchaseScheme.PurchaserId2 = PurchaserId2;
|
|
|
|
//productSku.SelectedPurchaseScheme.PurchaserName = PurchaserName;
|
|
|
|
//productSku.SelectedPurchaseScheme.PurchaserLocation = PurchaserLocation;
|
|
|
|
productSku.SelectedPurchaseScheme.DefaultCost = productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Sum(p => p.PurchaseSchemeProductSkuList.Count() == 0 ? 0 : p.PurchaseSchemeProductSkuList.Sum(s => s.Price)); |
|
|
|
|
|
|
|
} |
|
|
@ -385,12 +457,13 @@ namespace BBWYB.Client.ViewModels |
|
|
|
|
|
|
|
private void Closing(System.ComponentModel.CancelEventArgs e) |
|
|
|
{ |
|
|
|
PurchaserId = PurchaserName = string.Empty; |
|
|
|
this.SchemeId = null; |
|
|
|
//clear data
|
|
|
|
foreach (var sku in ProductSkuList) |
|
|
|
{ |
|
|
|
sku.SelectedPurchaseScheme = null; |
|
|
|
} |
|
|
|
this.ProductSkuList.Clear(); |
|
|
|
//Product = null;
|
|
|
|
e.Cancel = false; |
|
|
|
} |
|
|
|