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.
334 lines
13 KiB
334 lines
13 KiB
3 years ago
|
using BBWY.Client.APIServices;
|
||
|
using BBWY.Client.Models;
|
||
|
using BBWY.Common.Extensions;
|
||
|
using BBWY.Common.Models;
|
||
|
using GalaSoft.MvvmLight.Command;
|
||
|
using GalaSoft.MvvmLight.Messaging;
|
||
|
using Jd.Api.Request;
|
||
|
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 WareStockViewModel : BaseVM, IDenpendency
|
||
|
{
|
||
|
#region Property
|
||
|
private PurchaseOrderService purchaseOrderService;
|
||
|
private ProductService productService;
|
||
|
private GlobalContext globalContext;
|
||
|
|
||
|
private bool isLoading;
|
||
|
private int pageIndex = 1;
|
||
|
private int pageSize;
|
||
|
private int productCount;
|
||
|
private string searchProductItem;
|
||
|
private string searchSpu;
|
||
|
private string searchSku;
|
||
|
private string searchPurchaseOrder;
|
||
|
|
||
|
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
|
||
|
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 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); } }
|
||
|
public string SearchPurchaseOrder { get => searchPurchaseOrder; set { Set(ref searchPurchaseOrder, value); } }
|
||
|
|
||
|
public IList<Product> ProductList { get; set; }
|
||
|
#endregion
|
||
|
|
||
|
#region ICommand
|
||
|
public ICommand SearchCommand { get; set; }
|
||
|
public ICommand ProductPageIndexChangedCommand { get; set; }
|
||
|
public ICommand AddPurchaserOrderCommand { get; set; }
|
||
|
public ICommand EditPurchaseOrderCommand { get; set; }
|
||
|
public ICommand DeletePurchaseOrderCommand { get; set; }
|
||
|
public ICommand SavePurchaseOrderCommand { get; set; }
|
||
|
public ICommand SwitchStorageTypeCommand { get; set; }
|
||
|
#endregion
|
||
|
|
||
|
#region Method
|
||
|
public WareStockViewModel(GlobalContext globalContext, PurchaseOrderService purchaseOrderService, ProductService productService)
|
||
|
{
|
||
|
this.globalContext = globalContext;
|
||
|
this.purchaseOrderService = purchaseOrderService;
|
||
|
this.productService = productService;
|
||
|
ProductList = new ObservableCollection<Product>();
|
||
|
|
||
|
SearchCommand = new RelayCommand(() =>
|
||
|
{
|
||
|
PageIndex = 1;
|
||
|
Task.Factory.StartNew(() => LoadWare(1));
|
||
|
});
|
||
|
ProductPageIndexChangedCommand = new RelayCommand<Controls.PageArgs>((p) => Task.Factory.StartNew(() => LoadWare(p.PageIndex)));
|
||
|
SwitchStorageTypeCommand = new RelayCommand<StorageModel>(SwitchStorageType);
|
||
|
AddPurchaserOrderCommand = new RelayCommand<Product>(AddPurchaserOrder);
|
||
|
EditPurchaseOrderCommand = new RelayCommand<PurchaseOrder>(po => po.IsEdit = true);
|
||
|
DeletePurchaseOrderCommand = new RelayCommand<PurchaseOrder>(DeletePurchaseOrder);
|
||
|
SavePurchaseOrderCommand = new RelayCommand<PurchaseOrder>(SavePurchaseOrder);
|
||
|
Task.Factory.StartNew(() => LoadWare(1));
|
||
|
}
|
||
|
|
||
|
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, string.Empty, 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)
|
||
|
{
|
||
|
var ewh = new ManualResetEvent(false);
|
||
|
waitList.Add(ewh);
|
||
|
Task.Factory.StartNew(() => LoadSku(p, ewh));
|
||
|
}
|
||
|
WaitHandle.WaitAll(waitList.ToArray(), 8000);
|
||
|
#endregion
|
||
|
|
||
|
#region 加载采购单
|
||
|
LoadPurchaseOrder(productList, StorageType.京仓);
|
||
|
#endregion
|
||
|
|
||
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
||
|
{
|
||
|
foreach (var p in productList)
|
||
|
ProductList.Add(p);
|
||
|
//使滚动条保持顶部
|
||
|
Messenger.Default.Send(string.Empty, "WareStock_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;
|
||
|
}
|
||
|
foreach (var sku in skuResponse.Data)
|
||
|
{
|
||
|
sku.StorageList.Add(new StorageModel()
|
||
|
{
|
||
|
ProductId = product.Id,
|
||
|
SkuId = sku.Id,
|
||
|
StorageType = StorageType.京仓
|
||
|
});
|
||
|
sku.StorageList.Add(new StorageModel()
|
||
|
{
|
||
|
ProductId = product.Id,
|
||
|
SkuId = sku.Id,
|
||
|
StorageType = StorageType.云仓
|
||
|
});
|
||
|
sku.StorageList.Add(new StorageModel()
|
||
|
{
|
||
|
ProductId = product.Id,
|
||
|
SkuId = sku.Id,
|
||
|
StorageType = StorageType.本地自发
|
||
|
});
|
||
|
sku.SelectedStorageModel = sku.StorageList[0];
|
||
|
}
|
||
|
product.SkuList = skuResponse.Data;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
ewh.Set();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void LoadPurchaseOrder(IList<Product> productList, StorageType storageType)
|
||
|
{
|
||
|
var skuList = new List<ProductSku>();
|
||
|
foreach (var p in productList)
|
||
|
skuList.AddRange(p.SkuList);
|
||
|
LoadPurchaseOrder(skuList, storageType);
|
||
|
}
|
||
|
|
||
|
private void LoadPurchaseOrder(IList<ProductSku> skuList, StorageType storageType)
|
||
|
{
|
||
|
App.Current.Dispatcher.Invoke(() =>
|
||
|
{
|
||
|
foreach (var s in skuList)
|
||
|
s.PurchaseOrderList.Clear();
|
||
|
});
|
||
|
var response = purchaseOrderService.GetList(skuList.Select(s => s.Id).ToList(), storageType, globalContext.User.Shop.ShopId);
|
||
|
if (response.Success)
|
||
|
{
|
||
|
var purchaseOrderList = response.Data;
|
||
|
App.Current.Dispatcher.Invoke(() =>
|
||
|
{
|
||
|
foreach (var s in skuList)
|
||
|
{
|
||
|
var currentSkuPurchaseOrderList = purchaseOrderList.Where(po => po.SkuId == s.Id).Map<IList<PurchaseOrder>>();
|
||
|
foreach (var po in currentSkuPurchaseOrderList)
|
||
|
s.PurchaseOrderList.Add(po);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void SwitchStorageType(StorageModel storageModel)
|
||
|
{
|
||
|
var product = ProductList.FirstOrDefault(p => p.Id == storageModel.ProductId);
|
||
|
var sku = product.SkuList.FirstOrDefault(s => s.Id == storageModel.SkuId);
|
||
|
if (sku.SelectedStorageModel == storageModel)
|
||
|
return;
|
||
|
sku.SelectedStorageModel = storageModel;
|
||
|
IsLoading = true;
|
||
|
Task.Factory.StartNew(() =>
|
||
|
{
|
||
|
LoadPurchaseOrder(new List<ProductSku>() { sku }, storageModel.StorageType);
|
||
|
IsLoading = false;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void AddPurchaserOrder(Product product)
|
||
|
{
|
||
|
foreach (var sku in product.SkuList)
|
||
|
sku.PurchaseOrderList.Add(new PurchaseOrder
|
||
|
{
|
||
|
IsEdit = true,
|
||
|
SkuId = sku.Id,
|
||
|
ProductId = product.Id,
|
||
|
UserId = globalContext.User.Id,
|
||
|
StorageType = sku.SelectedStorageModel.StorageType,
|
||
|
PurchaseMethod = PurchaseMethod.线下采购,
|
||
|
CreateTime = DateTime.Now,
|
||
|
ShopId = globalContext.User.Shop.ShopId,
|
||
|
PurchasePlatform = Platform.阿里巴巴
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private void DeletePurchaseOrder(PurchaseOrder purchaseOrder)
|
||
|
{
|
||
|
var product = ProductList.FirstOrDefault(p => p.Id == purchaseOrder.ProductId);
|
||
|
var sku = product.SkuList.FirstOrDefault(s => s.Id == purchaseOrder.SkuId);
|
||
|
if (purchaseOrder.Id == 0)
|
||
|
{
|
||
|
sku.PurchaseOrderList.Remove(purchaseOrder);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (MessageBox.Show("确定要删除采购单吗?", "确认", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
|
||
|
return;
|
||
|
IsLoading = true;
|
||
|
Task.Factory.StartNew(() =>
|
||
|
{
|
||
|
var response = purchaseOrderService.DeletePurchaseOrder(purchaseOrder.Id);
|
||
|
IsLoading = false;
|
||
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
||
|
{
|
||
|
if (response.Success)
|
||
|
sku.PurchaseOrderList.Remove(purchaseOrder);
|
||
|
else
|
||
|
MessageBox.Show(response.Msg, "删除采购单");
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void SavePurchaseOrder(PurchaseOrder purchaseOrder)
|
||
|
{
|
||
|
if (purchaseOrder.PurchasePlatform == null ||
|
||
|
string.IsNullOrEmpty(purchaseOrder.PurchaseOrderId) ||
|
||
|
purchaseOrder.PurchaseQuantity == 0)
|
||
|
{
|
||
|
MessageBox.Show("缺少必要信息", "保存采购单");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (purchaseOrder.RemainingQuantity > purchaseOrder.PurchaseQuantity)
|
||
|
{
|
||
|
MessageBox.Show("剩余库存不成超过采购数量", "保存采购单");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
purchaseOrder.IsEdit = false;
|
||
|
var product = ProductList.FirstOrDefault(p => p.Id == purchaseOrder.ProductId);
|
||
|
var sku = product.SkuList.FirstOrDefault(s => s.Id == purchaseOrder.SkuId);
|
||
|
IsLoading = true;
|
||
|
Task.Factory.StartNew(() =>
|
||
|
{
|
||
|
var response = purchaseOrder.Id == 0 ? purchaseOrderService.AddPurchaseOrder(purchaseOrder) :
|
||
|
purchaseOrderService.EditPurchaseOrder(purchaseOrder);
|
||
|
IsLoading = false;
|
||
|
if (response.Success)
|
||
|
{
|
||
|
if (purchaseOrder.Id == 0)
|
||
|
LoadPurchaseOrder(new List<ProductSku>() { sku }, purchaseOrder.StorageType.Value);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "保存采购单"));
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
#endregion
|
||
|
}
|
||
|
}
|