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.
207 lines
7.3 KiB
207 lines
7.3 KiB
using BBWY.Client.APIServices;
|
|
using BBWY.Client.Models;
|
|
using GalaSoft.MvvmLight.Command;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using BBWY.Common.Extensions;
|
|
|
|
namespace BBWY.Client.ViewModels
|
|
{
|
|
public class BatchPurchaseAddProductSkuViewModel : BaseVM
|
|
{
|
|
private BatchPurchaseService batchPurchaseService;
|
|
private bool isLoading;
|
|
private string sku;
|
|
private string spu;
|
|
private Purchaser selectedPurchaser;
|
|
private Purchaser defaultPurchaser;
|
|
|
|
private KVModel selectedPurchasePlatform;
|
|
private KVModel defaultPurchasePlatform;
|
|
|
|
private bool allSelected;
|
|
|
|
public ICommand SearchCommand { get; set; }
|
|
public ICommand FilterCommand { get; set; }
|
|
public ICommand SaveCommand { get; set; }
|
|
|
|
public ICommand AddQuantityCommand { get; set; }
|
|
|
|
public ICommand SubtractQuantityCommand { get; set; }
|
|
|
|
public string Sku { get => sku; set { Set(ref sku, value); } }
|
|
public string Spu { get => spu; set { Set(ref spu, value); } }
|
|
|
|
public IList<Purchaser> PurchaserList { get; set; }
|
|
public IList<KVModel> PurchasePlatformList { get; set; }
|
|
public Purchaser SelectedPurchaser
|
|
{
|
|
get => selectedPurchaser; set { Set(ref selectedPurchaser, value); }
|
|
}
|
|
|
|
public KVModel SelectedPurchasePlatform
|
|
{
|
|
get => selectedPurchasePlatform; set { Set(ref selectedPurchasePlatform, value); }
|
|
}
|
|
|
|
public IList<ProductSkuWithScheme> SourceList { get; set; }
|
|
public IList<ProductSkuWithScheme> FilterList { get; set; }
|
|
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
|
|
|
|
public bool AllSelected
|
|
{
|
|
get => allSelected; set
|
|
{
|
|
if (Set(ref allSelected, value))
|
|
OnAllSelectedChanged();
|
|
}
|
|
}
|
|
|
|
public BatchPurchaseAddProductSkuViewModel(BatchPurchaseService batchPurchaseService)
|
|
{
|
|
this.batchPurchaseService = batchPurchaseService;
|
|
defaultPurchaser = new Purchaser() { Id = "-1", Name = "全部" };
|
|
defaultPurchasePlatform = new KVModel() { Key = "-1", Value = "全部" };
|
|
|
|
PurchaserList = new ObservableCollection<Purchaser>() { defaultPurchaser };
|
|
PurchasePlatformList = new ObservableCollection<KVModel>()
|
|
{
|
|
defaultPurchasePlatform,
|
|
new KVModel(){ Key=((int)Platform.拳探).ToString(),Value = Platform.拳探.ToString() },
|
|
new KVModel(){ Key=((int)Platform.阿里巴巴).ToString(),Value = Platform.阿里巴巴.ToString() }
|
|
};
|
|
|
|
SourceList = new List<ProductSkuWithScheme>();
|
|
FilterList = new ObservableCollection<ProductSkuWithScheme>();
|
|
SearchCommand = new RelayCommand(Search);
|
|
FilterCommand = new RelayCommand(Filter);
|
|
SaveCommand = new RelayCommand(Save);
|
|
AddQuantityCommand = new RelayCommand<ProductSkuWithScheme>(AddQuantity);
|
|
SubtractQuantityCommand = new RelayCommand<ProductSkuWithScheme>(SubtractQuantity);
|
|
SelectedPurchaser = PurchaserList[0];
|
|
SelectedPurchasePlatform = PurchasePlatformList[0];
|
|
}
|
|
|
|
private void Search()
|
|
{
|
|
IsLoading = true;
|
|
Task.Factory.StartNew(() => batchPurchaseService.GetProductSkuAndSchemeList(Sku, Spu)).ContinueWith(t =>
|
|
{
|
|
IsLoading = false;
|
|
var response = t.Result;
|
|
if (!response.Success)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "提示"));
|
|
return;
|
|
}
|
|
SourceList.Clear();
|
|
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
PurchaserList.Clear();
|
|
FilterList.Clear();
|
|
|
|
PurchaserList.Add(defaultPurchaser);
|
|
SelectedPurchaser = PurchaserList[0];
|
|
SelectedPurchasePlatform = PurchasePlatformList[0];
|
|
});
|
|
|
|
if (response.Data == null || response.Data.Count() == 0)
|
|
return;
|
|
var list = response.Data.Map<IList<ProductSkuWithScheme>>();
|
|
|
|
#region 提取采购商
|
|
var purchaserList = list.Where(item => item.PurchaseSchemeId != 0)
|
|
.Select(item => new Purchaser() { Id = item.PurchaserId, Name = item.PurchaserName, Platform = item.PurchasePlatform.Value })
|
|
.Distinct(new PurchaserComparer());
|
|
#endregion
|
|
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
foreach (var purchaser in purchaserList)
|
|
PurchaserList.Add(purchaser);
|
|
foreach (var item in list)
|
|
{
|
|
SourceList.Add(item);
|
|
}
|
|
Filter();
|
|
});
|
|
});
|
|
}
|
|
|
|
private void Filter()
|
|
{
|
|
FilterList.Clear();
|
|
if (SourceList.Count() == 0)
|
|
return;
|
|
var resultList = new List<ProductSkuWithScheme>();
|
|
resultList.AddRange(SourceList);
|
|
|
|
if (SelectedPurchaser.Id != "-1")
|
|
{
|
|
for (var i = 0; i < resultList.Count(); i++)
|
|
{
|
|
if (resultList[i].PurchaserId != SelectedPurchaser.Id)
|
|
{
|
|
resultList.Remove(resultList[i]);
|
|
i--;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (SelectedPurchasePlatform.Key != "-1")
|
|
{
|
|
for (var i = 0; i < resultList.Count(); i++)
|
|
{
|
|
if (resultList[i].PurchasePlatform != (Platform)int.Parse(SelectedPurchasePlatform.Key))
|
|
{
|
|
resultList.Remove(resultList[i]);
|
|
i--;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (resultList.Count() > 0)
|
|
{
|
|
foreach (var item in resultList)
|
|
FilterList.Add(item);
|
|
}
|
|
}
|
|
|
|
private void Save()
|
|
{
|
|
var selectedList = FilterList.Where(item => item.IsSelected).ToList();
|
|
if (selectedList == null || selectedList.Count() == 0)
|
|
{
|
|
MessageBox.Show("至少选择一个商品");
|
|
return;
|
|
}
|
|
GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(selectedList, "BatchPurchaseAddProductSkuSave");
|
|
}
|
|
|
|
private void OnAllSelectedChanged()
|
|
{
|
|
foreach (var item in FilterList)
|
|
{
|
|
if (item.PurchaseSchemeId == 0)
|
|
continue;
|
|
item.IsSelected = AllSelected;
|
|
}
|
|
}
|
|
|
|
private void AddQuantity(ProductSkuWithScheme item)
|
|
{
|
|
item.Quantity++;
|
|
}
|
|
|
|
private void SubtractQuantity(ProductSkuWithScheme item)
|
|
{
|
|
if (item.Quantity > 0)
|
|
item.Quantity--;
|
|
}
|
|
}
|
|
}
|
|
|