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.
399 lines
20 KiB
399 lines
20 KiB
using BBWY.Client.APIServices;
|
|
using BBWY.Client.Models;
|
|
using BBWY.Client.Models.QiKu;
|
|
using BBWY.Client.Views.BatchPurchase;
|
|
using BBWY.Common.Trigger;
|
|
using GalaSoft.MvvmLight.Command;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
|
|
namespace BBWY.Client.ViewModels
|
|
{
|
|
public class BatchPurchaseCreateNewOrderViewModel : BaseVM
|
|
{
|
|
private GlobalContext globalContext;
|
|
private PurchaseProductAPIService purchaseProductAPIService;
|
|
private PurchaseService purchaseService;
|
|
private BatchPurchaseService batchPurchaseService;
|
|
private DelayTrigger delayTrigger;
|
|
|
|
private bool isLoading;
|
|
private decimal productAmount;
|
|
private decimal freightAmount;
|
|
private decimal totalAmount;
|
|
private string contactName;
|
|
private string address;
|
|
private string mobile;
|
|
private string province;
|
|
private string city;
|
|
private string county;
|
|
private string town;
|
|
private string purchaseRemark;
|
|
private PurchaseOrderMode purchaseOrderMode = PurchaseOrderMode.批发;
|
|
|
|
//private string tradeMode;
|
|
private string extensions;
|
|
|
|
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
|
|
|
|
public decimal ProductAmount { get => productAmount; set { Set(ref productAmount, value); } }
|
|
public decimal FreightAmount { get => freightAmount; set { Set(ref freightAmount, value); } }
|
|
public decimal TotalAmount { get => totalAmount; set { Set(ref totalAmount, value); } }
|
|
public string ContactName { get => contactName; set { Set(ref contactName, value); } }
|
|
public string Address { get => address; set { Set(ref address, value); } }
|
|
public string Mobile { get => mobile; set { Set(ref mobile, value); } }
|
|
public string Province { get => province; set { Set(ref province, value); } }
|
|
public string City { get => city; set { Set(ref city, value); } }
|
|
public string County { get => county; set { Set(ref county, value); } }
|
|
public string Town { get => town; set { Set(ref town, value); } }
|
|
public string PurchaseRemark { get => purchaseRemark; set { Set(ref purchaseRemark, value); } }
|
|
|
|
public PurchaseOrderMode PurchaseOrderMode
|
|
{
|
|
get => purchaseOrderMode; set
|
|
{
|
|
if (Set(ref purchaseOrderMode, value))
|
|
OnDelayTriggerExecute(Guid.NewGuid().ToString());
|
|
}
|
|
}
|
|
public IList<ProductSkuWithScheme> ProductSkuWithSchemeList { get; set; }
|
|
|
|
|
|
public ICommand FastCreateOrderCommand { get; set; }
|
|
public ICommand PreviewOrderCommand { get; set; }
|
|
public ICommand AddProductSkuCommand { get; set; }
|
|
public ICommand DeleteProductSkuWithSchemeCommand { get; set; }
|
|
|
|
public ICommand EditQuantityRatioCommand { get; set; }
|
|
|
|
public ICommand AddQuantityCommand { get; set; }
|
|
|
|
public ICommand SubtractQuantityCommand { get; set; }
|
|
|
|
public BatchPurchaseCreateNewOrderViewModel(PurchaseProductAPIService purchaseProductAPIService,
|
|
PurchaseService purchaseService,
|
|
GlobalContext globalContext,
|
|
BatchPurchaseService batchPurchaseService)
|
|
{
|
|
this.globalContext = globalContext;
|
|
this.purchaseProductAPIService = purchaseProductAPIService;
|
|
this.purchaseService = purchaseService;
|
|
this.batchPurchaseService = batchPurchaseService;
|
|
ProductSkuWithSchemeList = new ObservableCollection<ProductSkuWithScheme>();
|
|
|
|
FastCreateOrderCommand = new RelayCommand(FastCreateOrder);
|
|
PreviewOrderCommand = new RelayCommand(PreviewOrder);
|
|
AddProductSkuCommand = new RelayCommand(AddProductSku);
|
|
DeleteProductSkuWithSchemeCommand = new RelayCommand<ProductSkuWithScheme>(DeleteProductSkuWithScheme);
|
|
EditQuantityRatioCommand = new RelayCommand<object>(EditQuantityRatio);
|
|
AddQuantityCommand = new RelayCommand<PurchaseSchemeProductSku>(AddQuantity);
|
|
SubtractQuantityCommand = new RelayCommand<PurchaseSchemeProductSku>(SubtractQuantity);
|
|
this.delayTrigger = new DelayTrigger();
|
|
this.delayTrigger.OnExecute = OnDelayTriggerExecute;
|
|
}
|
|
|
|
protected override void Load()
|
|
{
|
|
this.Province = "福建省";
|
|
this.City = "泉州市";
|
|
this.County = "鲤城区";
|
|
this.Town = "金龙街道";
|
|
this.Address = "南环路1129号创鑫产业园3楼齐越";
|
|
this.ContactName = globalContext.User.Shop.PurchaseAccountList.FirstOrDefault(pa => pa.PurchasePlatformId == Platform.拳探)?.AccountName;
|
|
}
|
|
|
|
private void OnDelayTriggerExecute(string key)
|
|
{
|
|
if (string.IsNullOrEmpty(key))
|
|
{
|
|
IsLoading = false;
|
|
return;
|
|
}
|
|
|
|
if (ProductSkuWithSchemeList.Count() == 0)
|
|
{
|
|
ProductAmount = 0;
|
|
FreightAmount = 0;
|
|
TotalAmount = 0;
|
|
IsLoading = false;
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(ContactName) ||
|
|
string.IsNullOrEmpty(Address) ||
|
|
string.IsNullOrEmpty(Mobile) ||
|
|
string.IsNullOrEmpty(Province) ||
|
|
string.IsNullOrEmpty(City) ||
|
|
string.IsNullOrEmpty(County))
|
|
{
|
|
IsLoading = false;
|
|
MessageBox.Show("缺少完整的收货信息", "提示");
|
|
return;
|
|
}
|
|
|
|
IsLoading = true;
|
|
Task.Factory.StartNew(() => batchPurchaseService.PreviewOrder(ProductSkuWithSchemeList, new Consignee()
|
|
{
|
|
Address = Address,
|
|
City = City,
|
|
ContactName = ContactName,
|
|
County = County,
|
|
Mobile = Mobile,
|
|
Province = Province,
|
|
TelePhone = Mobile,
|
|
Town = Town
|
|
}, this.PurchaseOrderMode, globalContext.User.Shop.PurchaseAccountList))
|
|
.ContinueWith(t =>
|
|
{
|
|
IsLoading = false;
|
|
var r = t.Result;
|
|
if (!r.Success)
|
|
{
|
|
ProductAmount = FreightAmount = TotalAmount = 0;
|
|
//tradeMode = string.Empty;
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(r.Msg, "预览订单报价"));
|
|
return;
|
|
}
|
|
ProductAmount = r.Data.ProductAmount;
|
|
FreightAmount = r.Data.FreightAmount;
|
|
TotalAmount = r.Data.TotalAmount;
|
|
//tradeMode = r.Data.OrderTradeType?.Code;
|
|
extensions = r.Data.Extensions;
|
|
});
|
|
}
|
|
|
|
private void PreviewOrder()
|
|
{
|
|
OnDelayTriggerExecute(Guid.NewGuid().ToString());
|
|
}
|
|
|
|
private void FastCreateOrder()
|
|
{
|
|
if (IsLoading)
|
|
return;
|
|
if (TotalAmount == 0)
|
|
{
|
|
MessageBox.Show("总金额为0不能提交订单", "提示");
|
|
return;
|
|
}
|
|
if (string.IsNullOrEmpty(Mobile) ||
|
|
string.IsNullOrEmpty(Address) ||
|
|
string.IsNullOrEmpty(City) ||
|
|
string.IsNullOrEmpty(Province) ||
|
|
string.IsNullOrEmpty(County) ||
|
|
string.IsNullOrEmpty(Town) ||
|
|
string.IsNullOrEmpty(ContactName))
|
|
{
|
|
MessageBox.Show("收货人信息不全", "下单");
|
|
return;
|
|
}
|
|
|
|
IList<PackSkuConfig> packSkuConfigList = null;
|
|
var isContainsQT = ProductSkuWithSchemeList.Any(ps => ps.PurchasePlatform == Platform.拳探);
|
|
if (isContainsQT)
|
|
{
|
|
var packWindow = new PackSkuConfigWindow(ProductSkuWithSchemeList.Where(ps => ps.PurchasePlatform == Platform.拳探).Select(ps => new PackSkuConfig()
|
|
{
|
|
Logo = ps.Logo,
|
|
SkuId = ps.SkuId,
|
|
Title = ps.Title,
|
|
PurchaseCount = ps.Quantity
|
|
}).ToList());
|
|
|
|
if (packWindow.ShowDialog() != true)
|
|
return;
|
|
packSkuConfigList = packWindow.GetPackSkuConfigList();
|
|
}
|
|
IsLoading = true;
|
|
|
|
Task.Factory.StartNew(() => batchPurchaseService.CreateOrder(ProductSkuWithSchemeList,
|
|
new Consignee()
|
|
{
|
|
Address = Address,
|
|
City = City,
|
|
ContactName = ContactName,
|
|
County = County,
|
|
Mobile = Mobile,
|
|
Province = Province,
|
|
TelePhone = Mobile,
|
|
Town = Town
|
|
},
|
|
this.PurchaseOrderMode,
|
|
globalContext.User.Shop.PurchaseAccountList,
|
|
this.extensions,
|
|
this.PurchaseRemark,
|
|
packSkuConfigList)).ContinueWith(t =>
|
|
{
|
|
IsLoading = false;
|
|
var response = t.Result;
|
|
if (!response.Success)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "创建采购单"));
|
|
return;
|
|
}
|
|
|
|
if (response.Data.SuccessSkuIdList != null && response.Data.SuccessSkuIdList.Count() > 0)
|
|
{
|
|
//删除下单成功的订单
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
for (var i = 0; i < ProductSkuWithSchemeList.Count(); i++)
|
|
{
|
|
if (response.Data.SuccessSkuIdList.Contains(ProductSkuWithSchemeList[i].SkuId))
|
|
{
|
|
ProductSkuWithSchemeList.Remove(ProductSkuWithSchemeList[i]);
|
|
i--;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
if (response.Data.FailSkuList != null && response.Data.FailSkuList.Count() > 0)
|
|
{
|
|
var errorBuilder = new StringBuilder();
|
|
foreach (var error in response.Data.FailSkuList)
|
|
{
|
|
errorBuilder.AppendLine($"SkuId {error.SkuId}");
|
|
errorBuilder.AppendLine($"错误信息 {error.ErrorMsg}");
|
|
errorBuilder.AppendLine();
|
|
}
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(errorBuilder.ToString(), "创建采购单"));
|
|
}
|
|
else
|
|
{
|
|
//关闭窗口
|
|
App.Current.Dispatcher.Invoke(() => GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(true, "BatchCreateNewPurchaseOrderClose"));
|
|
|
|
}
|
|
});
|
|
}
|
|
|
|
private void AddProductSku()
|
|
{
|
|
var addProductSkuWindow = new BatchPurchaseAddProductSku();
|
|
if (addProductSkuWindow.ShowDialog() != true)
|
|
return;
|
|
|
|
var newProductSkuWithSchemeList = addProductSkuWindow.SelectedProductSkuWithSchemeList;
|
|
var waitList = new List<EventWaitHandle>();
|
|
foreach (var newProductSkuWithScheme in newProductSkuWithSchemeList)
|
|
{
|
|
if (ProductSkuWithSchemeList.Any(item => item.SkuId == newProductSkuWithScheme.SkuId))
|
|
continue;
|
|
ProductSkuWithSchemeList.Add(newProductSkuWithScheme);
|
|
var ewh = new ManualResetEvent(false);
|
|
waitList.Add(ewh);
|
|
Task.Factory.StartNew(() => LoadPurchaseScheme(newProductSkuWithScheme, ewh));
|
|
}
|
|
if (waitList.Count() == 0)
|
|
return;
|
|
IsLoading = true;
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
WaitHandle.WaitAll(waitList.ToArray());
|
|
PreviewOrder(); //预览订单
|
|
});
|
|
}
|
|
|
|
private void LoadPurchaseScheme(ProductSkuWithScheme productSkuWithScheme, EventWaitHandle ewh)
|
|
{
|
|
try
|
|
{
|
|
#region 加载采购方案
|
|
var purchaseSchemeResponse = purchaseService.GetPurchaseSchemeList(shopId: globalContext.User.Shop.ShopId, schemeId: productSkuWithScheme.PurchaseSchemeId);
|
|
if (!purchaseSchemeResponse.Success)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show($"方案Id{productSkuWithScheme.PurchaseSchemeId}\r\n{purchaseSchemeResponse.Msg}", "加载采购方案失败"));
|
|
return;
|
|
}
|
|
|
|
var purchaseScheme = purchaseSchemeResponse.Data[0];
|
|
#endregion
|
|
|
|
#region 完善采购方案基本信息
|
|
foreach (var purchaseSchemeProduct in purchaseScheme.PurchaseSchemeProductList)
|
|
{
|
|
var data = purchaseProductAPIService.GetProductInfo(productSkuWithScheme.PurchasePlatform.Value,
|
|
productSkuWithScheme.ProductId,
|
|
productSkuWithScheme.SkuId,
|
|
purchaseSchemeProduct.PurchaseProductId,
|
|
PurchaseOrderMode.批发,
|
|
PurchaseProductAPIMode.Spider);
|
|
|
|
if (data != null && data.Value.purchaseSchemeProductSkus != null && data.Value.purchaseSchemeProductSkus.Count > 0)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
foreach (var purchaseSchemeProductSku in data.Value.purchaseSchemeProductSkus)
|
|
{
|
|
if (purchaseSchemeProduct.PurchaseSchemeProductSkuList.Any(s => s.PurchaseSkuId == purchaseSchemeProductSku.PurchaseSkuId))
|
|
{
|
|
productSkuWithScheme.PurchaseSchemeProductSkuList.Add(purchaseSchemeProductSku);
|
|
|
|
purchaseSchemeProductSku.ItemTotal = productSkuWithScheme.Quantity;
|
|
purchaseSchemeProductSku.OnItemTotalChanged = OnItemTotalChanged;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
finally
|
|
{
|
|
ewh.Set();
|
|
}
|
|
}
|
|
|
|
private void OnItemTotalChanged(int itemTotal)
|
|
{
|
|
Console.WriteLine($"OnItemTotalChanged {DateTime.Now} {itemTotal}");
|
|
this.delayTrigger.SetKey(Guid.NewGuid().ToString());
|
|
}
|
|
|
|
private void DeleteProductSkuWithScheme(ProductSkuWithScheme productSkuWithScheme)
|
|
{
|
|
ProductSkuWithSchemeList.Remove(productSkuWithScheme);
|
|
this.delayTrigger.SetKey(Guid.NewGuid().ToString());
|
|
}
|
|
|
|
private void EditQuantityRatio(object param)
|
|
{
|
|
var paramList = (object[])param;
|
|
var skuQuantity = Convert.ToInt32(paramList[0]);
|
|
var purchaseSchemeProductSku = paramList[1] as PurchaseSchemeProductSku;
|
|
|
|
var editWindow = new EditQuantityRatioWindow(purchaseSchemeProductSku.QuantityRatio);
|
|
if (editWindow.ShowDialog() == true)
|
|
{
|
|
var quantityRatio = editWindow.QuantityRatio;
|
|
purchaseSchemeProductSku.QuantityRatio = quantityRatio;
|
|
purchaseSchemeProductSku.ItemTotal = quantityRatio * skuQuantity;
|
|
}
|
|
}
|
|
|
|
private void AddQuantity(PurchaseSchemeProductSku purchaseSchemeProductSku)
|
|
{
|
|
purchaseSchemeProductSku.ItemTotal++;
|
|
}
|
|
|
|
private void SubtractQuantity(PurchaseSchemeProductSku purchaseSchemeProductSku)
|
|
{
|
|
if (purchaseSchemeProductSku.ItemTotal > 1)
|
|
purchaseSchemeProductSku.ItemTotal--;
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|