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.
348 lines
13 KiB
348 lines
13 KiB
using BBWY.Client.APIServices;
|
|
using BBWY.Client.Models;
|
|
using BBWY.Common.Models;
|
|
using GalaSoft.MvvmLight.Command;
|
|
using GalaSoft.MvvmLight.Messaging;
|
|
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 WareManagerViewModel : BaseVM, IDenpendency
|
|
{
|
|
#region Properties
|
|
private PurchaseService purchaseService;
|
|
private ProductService productService;
|
|
private BindingPurchaseProductViewModel bindingPurchaseProduct;
|
|
private GlobalContext globalContext;
|
|
private bool isLoading;
|
|
private int pageIndex = 1;
|
|
private int pageSize;
|
|
private int productCount;
|
|
private string searchProductName;
|
|
private string searchProductItem;
|
|
private string searchSpu;
|
|
private string searchSku;
|
|
|
|
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
|
|
|
|
public IList<Product> ProductList { get; set; }
|
|
public int PageIndex { get => pageIndex; set { Set(ref pageIndex, value); } }
|
|
public int PageSize { get => pageSize; set { Set(ref pageSize, value); } }
|
|
public int ProductCount { get => productCount; set { Set(ref productCount, value); } }
|
|
|
|
public string SearchProductName { get => searchProductName; set { Set(ref searchProductName, value); } }
|
|
public string SearchProductItem { get => searchProductItem; set { Set(ref searchProductItem, value); } }
|
|
public string SearchSpu { get => searchSpu; set { Set(ref searchSpu, value); } }
|
|
public string SearchSku { get => searchSku; set { Set(ref searchSku, value); } }
|
|
#endregion
|
|
|
|
#region Commands
|
|
public ICommand AddPurchaserCommand { get; set; }
|
|
public ICommand EditPurchaserCommand { get; set; }
|
|
public ICommand DeletePurchaserCommand { get; set; }
|
|
public ICommand SearchCommand { get; set; }
|
|
public ICommand ProductPageIndexChangedCommand { get; set; }
|
|
|
|
public ICommand SwitchPurchasePlatformCommand { get; set; }
|
|
#endregion
|
|
|
|
#region Methods
|
|
public WareManagerViewModel(GlobalContext globalContext, BindingPurchaseProductViewModel bindingPurchaseProduct, PurchaseService purchaseService, ProductService productService)
|
|
{
|
|
AddPurchaserCommand = new RelayCommand<Product>(AddPurchaser);
|
|
EditPurchaserCommand = new RelayCommand<Purchaser>(EditPurchaser);
|
|
DeletePurchaserCommand = new RelayCommand<Purchaser>(DeletePurchaser);
|
|
SwitchPurchasePlatformCommand = new RelayCommand<object>(SwitchPurchasePlatform);
|
|
SearchCommand = new RelayCommand(() =>
|
|
{
|
|
PageIndex = 1;
|
|
Task.Factory.StartNew(() => LoadWare(1));
|
|
});
|
|
ProductPageIndexChangedCommand = new RelayCommand<Controls.PageArgs>((p) => Task.Factory.StartNew(() => LoadWare(p.PageIndex)));
|
|
this.purchaseService = purchaseService;
|
|
this.productService = productService;
|
|
this.globalContext = globalContext;
|
|
this.bindingPurchaseProduct = bindingPurchaseProduct;
|
|
ProductList = new ObservableCollection<Product>();
|
|
Task.Factory.StartNew(() => LoadWare(1));
|
|
}
|
|
|
|
public override void Refresh()
|
|
{
|
|
this.ProductList.Clear();
|
|
this.ProductCount = 0;
|
|
PageIndex = 1;
|
|
}
|
|
|
|
protected override void Load()
|
|
{
|
|
Console.WriteLine($"{VMId} {DateTime.Now}");
|
|
}
|
|
|
|
private void LoadWare(int pageIndex)
|
|
{
|
|
if (!string.IsNullOrEmpty(SearchSpu) && !string.IsNullOrEmpty(SearchSku))
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show("SPU和SKU条件不能共存"));
|
|
return;
|
|
}
|
|
|
|
App.Current.Dispatcher.Invoke(() => ProductList.Clear());
|
|
|
|
IsLoading = true;
|
|
|
|
#region 加载JD商品列表
|
|
ApiResponse<ProductListResponse> productApiResponse = null;
|
|
if (!string.IsNullOrEmpty(SearchSku))
|
|
{
|
|
var skuResponse = productService.GetProductSkuList(string.Empty, SearchSku);
|
|
if (skuResponse.Success)
|
|
{
|
|
if (skuResponse.Data.Count == 0)
|
|
{
|
|
IsLoading = false;
|
|
return;
|
|
}
|
|
productApiResponse = productService.GetProductList(skuResponse.Data[0].ProductId, string.Empty, string.Empty, pageIndex);
|
|
}
|
|
else
|
|
{
|
|
IsLoading = false;
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(skuResponse.Msg, "加载sku"));
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
productApiResponse = productService.GetProductList(SearchSpu, SearchProductName, SearchProductItem, pageIndex);
|
|
}
|
|
|
|
if (!productApiResponse.Success)
|
|
{
|
|
IsLoading = false;
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(productApiResponse.Msg, "加载产品"));
|
|
return;
|
|
}
|
|
var productList = productApiResponse.Data.Items;
|
|
ProductCount = productApiResponse.Data.Count;
|
|
if (ProductCount == 0)
|
|
{
|
|
IsLoading = false;
|
|
return;
|
|
}
|
|
#endregion
|
|
|
|
#region 加载JDSKU列表
|
|
var waitList = new List<EventWaitHandle>();
|
|
foreach (var p in productList)
|
|
{
|
|
p.CreatePlatformList();
|
|
var ewh = new ManualResetEvent(false);
|
|
waitList.Add(ewh);
|
|
Task.Factory.StartNew(() => LoadSku(p, ewh));
|
|
}
|
|
WaitHandle.WaitAll(waitList.ToArray(), 8000);
|
|
#endregion
|
|
|
|
#region 加载采购方案
|
|
var skuList = new List<ProductSku>();
|
|
foreach (var p in productList)
|
|
skuList.AddRange(p.SkuList);
|
|
LoadPurchaseScheme(skuList);
|
|
#endregion
|
|
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
{
|
|
foreach (var p in productList)
|
|
ProductList.Add(p);
|
|
ExtractPurchaser();
|
|
//使滚动条保持顶部
|
|
Messenger.Default.Send(string.Empty, "WareManager_ProductListScrollToTop");
|
|
});
|
|
|
|
IsLoading = false;
|
|
}
|
|
|
|
private void LoadSku(Product product, EventWaitHandle ewh)
|
|
{
|
|
try
|
|
{
|
|
var skuResponse = productService.GetProductSkuList(product.Id, string.Empty);
|
|
if (!skuResponse.Success)
|
|
{
|
|
IsLoading = false;
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(skuResponse.Msg, "加载sku"));
|
|
return;
|
|
}
|
|
product.SkuList = skuResponse.Data;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
finally
|
|
{
|
|
ewh.Set();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加载采购方案
|
|
/// </summary>
|
|
/// <param name="productList"></param>
|
|
private void LoadPurchaseScheme(IList<ProductSku> skuList, Platform? purchasePlatform = Platform.阿里巴巴)
|
|
{
|
|
var response = purchaseService.GetPurchaseSchemeList(skuList.Select(s => s.Id).ToList(), string.Empty, globalContext.User.Shop.ShopId, purchasePlatform: purchasePlatform);
|
|
if (!response.Success)
|
|
{
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate { MessageBox.Show(response.Msg, "获取采购方案"); });
|
|
return;
|
|
}
|
|
|
|
var schemeList = response.Data;
|
|
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
foreach (var s in skuList)
|
|
{
|
|
s.PurchaseSchemeList.Clear();
|
|
var currentSchemeList = schemeList.Where(scheme => scheme.SkuId == s.Id);
|
|
if (currentSchemeList.Count() > 0)
|
|
foreach (var scheme in currentSchemeList)
|
|
s.PurchaseSchemeList.Add(PurchaseScheme.Convert(scheme));
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 提取SKU中的采购商到商品的采购商列表中
|
|
/// </summary>
|
|
private void ExtractPurchaser(string productId = "")
|
|
{
|
|
var productList = string.IsNullOrEmpty(productId) ? ProductList : ProductList.Where(p => p.Id == productId);
|
|
foreach (var product in productList)
|
|
{
|
|
product.PurchaserList.Clear();
|
|
foreach (var sku in product.SkuList)
|
|
{
|
|
if (sku.PurchaseSchemeList.Count() > 0)
|
|
{
|
|
foreach (var pscheme in sku.PurchaseSchemeList)
|
|
{
|
|
var purchaser = product.PurchaserList.FirstOrDefault(purchaser => purchaser.Id == pscheme.PurchaserId);
|
|
if (purchaser == null)
|
|
{
|
|
purchaser = new Purchaser() { Id = pscheme.PurchaserId, Name = pscheme.PurchaserName, ProductId = product.Id };
|
|
product.PurchaserList.Add(purchaser);
|
|
}
|
|
purchaser.SkuUseCount++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AddPurchaser(Product product)
|
|
{
|
|
if (product.PurchaserList.Count >= 5)
|
|
{
|
|
MessageBox.Show("一个SPU内最多允许5个采购商");
|
|
return;
|
|
}
|
|
|
|
OpenBindingView(product, string.Empty, string.Empty);
|
|
}
|
|
|
|
private void EditPurchaser(Purchaser purchaser)
|
|
{
|
|
var product = ProductList.FirstOrDefault(p => p.Id == purchaser.ProductId);
|
|
OpenBindingView(product, purchaser.Id, purchaser.Name);
|
|
}
|
|
|
|
private void DeletePurchaser(Purchaser purchaser)
|
|
{
|
|
if (MessageBox.Show("确认删除该采购商吗?", "提示", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
|
|
return;
|
|
IsLoading = true;
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
var response = purchaseService.DeletePurchaser(purchaser.ProductId, purchaser.Id);
|
|
IsLoading = false;
|
|
if (response.Success)
|
|
{
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
{
|
|
var product = ProductList.FirstOrDefault(p => p.Id == purchaser.ProductId);
|
|
if (product != null)
|
|
{
|
|
foreach (var sku in product.SkuList)
|
|
{
|
|
var deleteScheme = sku.PurchaseSchemeList.FirstOrDefault(s => s.PurchaserId == purchaser.Id);
|
|
if (deleteScheme != null)
|
|
sku.PurchaseSchemeList.Remove(deleteScheme);
|
|
}
|
|
product.PurchaserList.Remove(purchaser);
|
|
}
|
|
MessageBox.Show("采购商删除成功", "提示");
|
|
});
|
|
}
|
|
else
|
|
{
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
{
|
|
MessageBox.Show(response.Msg, "采购商删除");
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
private void OpenBindingView(Product product, string purchaserId, string purchaserName)
|
|
{
|
|
var skuList = product.SkuList;
|
|
bindingPurchaseProduct.SetData(skuList, purchaserId, purchaserName, product.SelectedPurchasePlatformModel);
|
|
var bindingView = new Views.Ware.BindingPurchaseProduct();
|
|
var r = bindingView.ShowDialog();
|
|
if (r == true)
|
|
{
|
|
Console.WriteLine("Refresh PurchaseScheme");
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
IsLoading = true;
|
|
LoadPurchaseScheme(skuList, product.SelectedPurchasePlatformModel);
|
|
IsLoading = false;
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
{
|
|
ExtractPurchaser(product.Id);
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
private void SwitchPurchasePlatform(object param)
|
|
{
|
|
var paramList = (object[])param;
|
|
var productId = paramList[0].ToString();
|
|
var platform = (Platform)paramList[1];
|
|
|
|
var product = ProductList.FirstOrDefault(p => p.Id == productId);
|
|
IsLoading = true;
|
|
Task.Factory.StartNew(() => LoadPurchaseScheme(product.SkuList, platform))
|
|
.ContinueWith(t =>
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => ExtractPurchaser(productId));
|
|
IsLoading = false;
|
|
});
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|