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.
488 lines
22 KiB
488 lines
22 KiB
using BBWY.Client.Models;
|
|
using GalaSoft.MvvmLight.Command;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using BBWY.Client.APIServices;
|
|
using System.Collections.Generic;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Linq;
|
|
using System;
|
|
using System.Windows;
|
|
using BBWY.Common.Models;
|
|
using System.Text.RegularExpressions;
|
|
using GalaSoft.MvvmLight.Messaging;
|
|
using System.Threading;
|
|
|
|
namespace BBWY.Client.ViewModels
|
|
{
|
|
public class BindingPurchaseProductViewModel : BaseVM, IDenpendency
|
|
{
|
|
#region Properties
|
|
private GlobalContext globalContext;
|
|
private PurchaseService purchaseService;
|
|
private PurchaseProductAPIService purchaseProductAPIService;
|
|
|
|
private string purchaserName;
|
|
private bool isLoading;
|
|
|
|
private Platform purchasePlatform;
|
|
|
|
|
|
private IDictionary<Platform, string> urlPatternDictionary;
|
|
|
|
|
|
public IList<ProductSku> ProductSkuList { get; set; }
|
|
|
|
public string PurchaserId { get; set; }
|
|
public string PurchaserName { get => purchaserName; set { Set(ref purchaserName, value); } }
|
|
public string PurchaserLocation { get; set; }
|
|
public bool IsLoading { get => isLoading; set { Set(ref isLoading, value); } }
|
|
|
|
public Platform PurchasePlatform { get => purchasePlatform; set { Set(ref purchasePlatform, value); } }
|
|
|
|
#endregion
|
|
|
|
#region ICommands
|
|
public ICommand ClosingCommand { get; set; }
|
|
|
|
public ICommand AddPurchaseProductCommand { get; set; }
|
|
|
|
public ICommand RemovePurchaseSchemeProductCommand { get; set; }
|
|
|
|
public ICommand GetPurchaseProductInfoCommand { get; set; }
|
|
|
|
public ICommand ConfirmPurchaseProductCommand { get; set; }
|
|
|
|
public ICommand EditPurchaseProductCommand { get; set; }
|
|
|
|
public ICommand SavePurchaseSchemeCommand { get; set; }
|
|
|
|
public ICommand SearchPurchaseSkuNameCommand { get; set; }
|
|
|
|
public ICommand ClearPurchaseSkuNameCommand { get; set; }
|
|
#endregion
|
|
|
|
#region Methods
|
|
public BindingPurchaseProductViewModel(PurchaseProductAPIService purchaseProductAPIService, PurchaseService purchaseService, GlobalContext globalContext)
|
|
{
|
|
this.globalContext = globalContext;
|
|
this.purchaseService = purchaseService;
|
|
this.purchaseProductAPIService = purchaseProductAPIService;
|
|
ClosingCommand = new RelayCommand<System.ComponentModel.CancelEventArgs>(Closing);
|
|
AddPurchaseProductCommand = new RelayCommand<ProductSku>(AddPurchaseProduct);
|
|
RemovePurchaseSchemeProductCommand = new RelayCommand<PurchaseSchemeProduct>(RemovePurchaseSchemeProduct);
|
|
GetPurchaseProductInfoCommand = new RelayCommand<PurchaseSchemeProduct>(GetPurchaseProductInfo);
|
|
ConfirmPurchaseProductCommand = new RelayCommand<PurchaseSchemeProduct>(ConfirmPurchaseProduct);
|
|
EditPurchaseProductCommand = new RelayCommand<PurchaseSchemeProduct>(EditPurchaseProduct);
|
|
SavePurchaseSchemeCommand = new RelayCommand(SavePurchaseScheme);
|
|
SearchPurchaseSkuNameCommand = new RelayCommand<PurchaseSchemeProduct>(SearchPurchaseSkuName);
|
|
ClearPurchaseSkuNameCommand = new RelayCommand<PurchaseSchemeProduct>(ClearPurchaseSkuName);
|
|
urlPatternDictionary = new Dictionary<Platform, string>()
|
|
{
|
|
{ Platform.阿里巴巴,@"^(https://detail.1688.com/offer/(\d+).html)[^\s]*" },
|
|
{ Platform.拳探, @"^(https://qt.qiyue666.com/goods_detail/(\d+))(\?\w*)?$" }
|
|
};
|
|
}
|
|
|
|
public void SetData(IList<ProductSku> productSkuList, string purchaserId, string purchaserName, Platform purchasePlatform)
|
|
{
|
|
this.ProductSkuList = productSkuList;
|
|
//this.Product = product;
|
|
this.PurchaserId = purchaserId;
|
|
this.PurchaserName = purchaserName;
|
|
this.PurchasePlatform = purchasePlatform;
|
|
}
|
|
|
|
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;
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
purchaseSchemeProduct.SearchSkuList.Clear();
|
|
purchaseSchemeProduct.SkuList.Clear();
|
|
purchaseSchemeProduct.PurchaseSchemeProductSkuList.Clear();
|
|
});
|
|
//skuList = LoadPurchaseProductCore(purchaseSchemeProduct.PurchaseProductId, out _, out _, out _, out _, null);
|
|
var data = purchaseProductAPIService.GetProductInfo(platform,
|
|
purchaseSchemeProduct.ProductId,
|
|
purchaseSchemeProduct.SkuId,
|
|
purchaseProductId,
|
|
PurchaseOrderMode.批发,
|
|
PurchaseProductAPIMode.Spider);
|
|
|
|
if (data != null)
|
|
{
|
|
if (checkPurchaserFunc != null)
|
|
{
|
|
errorMsg = checkPurchaserFunc(data.Value.purchaser.Id);
|
|
if (!string.IsNullOrEmpty(errorMsg))
|
|
return;
|
|
}
|
|
|
|
if (data.Value.purchaseSchemeProductSkus.Count == 0)
|
|
{
|
|
errorMsg = $"商品{purchaseSchemeProduct.PurchaseProductId}缺少sku信息";
|
|
return;
|
|
}
|
|
|
|
PurchaserId = data.Value.purchaser.Id;
|
|
PurchaserName = data.Value.purchaser.Name;
|
|
PurchaserLocation = data.Value.purchaser.Location;
|
|
|
|
App.Current.Dispatcher.BeginInvoke((Action)delegate
|
|
{
|
|
foreach (var sku in data.Value.purchaseSchemeProductSkus)
|
|
{
|
|
purchaseSchemeProduct.SearchSkuList.Add(sku);
|
|
purchaseSchemeProduct.SkuList.Add(sku);
|
|
if (purchaseSchemeProduct.SelectedSkuIdList.Any(s => s == sku.PurchaseSkuId))
|
|
{
|
|
sku.IsSelected = true;
|
|
purchaseSchemeProduct.PurchaseSchemeProductSkuList.Add(sku);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
//private IList<PurchaseSchemeProductSku> LoadPurchaseProductCore(string purchseProductId,
|
|
// out string errorMsg,
|
|
// out string purchaserId,
|
|
// out string purchaserName,
|
|
// out string purchaserLocation,
|
|
// Func<string, string> checkPurchaserFunc)
|
|
//{
|
|
// errorMsg = string.Empty;
|
|
// purchaserId = string.Empty;
|
|
// purchaserName = string.Empty;
|
|
// purchaserLocation = string.Empty;
|
|
// //1688商品详情接口
|
|
// var response = oneBoundAPIService.GetProductInfo("1688", purchseProductId);
|
|
// if (!response.Success)
|
|
// {
|
|
// //记录日志
|
|
|
|
// errorMsg = response.Msg;
|
|
// return null;
|
|
// }
|
|
// var jobject = response.Data;
|
|
// purchaserId = jobject["item"]["seller_info"].Value<string>("user_num_id");
|
|
// purchaserName = jobject["item"]["seller_info"].Value<string>("title");
|
|
// if (string.IsNullOrEmpty(purchaserName))
|
|
// purchaserName = jobject["item"]["seller_info"].Value<string>("shop_name");
|
|
// purchaserLocation = jobject["item"].Value<string>("location");
|
|
// if (checkPurchaserFunc != null)
|
|
// {
|
|
// errorMsg = checkPurchaserFunc(purchaserId);
|
|
// if (!string.IsNullOrEmpty(errorMsg))
|
|
// return null;
|
|
// }
|
|
// //else if (PurchaserId != shopId)
|
|
// //{
|
|
// // errorMsg = "同一条采购方案内的商品所属店铺必须相同";
|
|
// // return null;
|
|
// //}
|
|
|
|
// var skuJArray = (JArray)jobject["item"]["skus"]["sku"];
|
|
// if (skuJArray.Count == 0)
|
|
// {
|
|
// errorMsg = $"商品{purchseProductId}缺少sku信息";
|
|
// return null;
|
|
// }
|
|
|
|
// return skuJArray.Select(j => new PurchaseSchemeProductSku()
|
|
// {
|
|
// //ProductId = Product.Id,
|
|
// //SkuId = purchaseSchemeProduct.SkuId,
|
|
// PurchaseProductId = purchseProductId,
|
|
// Price = j.Value<decimal>("price"),
|
|
// PurchaseSkuId = j.Value<string>("sku_id"),
|
|
// PurchaseSkuSpecId = j.Value<string>("spec_id"),
|
|
// Title = j.Value<string>("properties_name"),
|
|
// Logo = GetSkuLogo(j, (JArray)jobject["item"]["prop_imgs"]["prop_img"])
|
|
// }).ToList();
|
|
//}
|
|
|
|
private void AddPurchaseProduct(ProductSku productSku)
|
|
{
|
|
if (productSku.PurchaseSchemeList.Count > 4)
|
|
{
|
|
MessageBox.Show("该sku的采购方案已达上限(5)");
|
|
return;
|
|
}
|
|
|
|
if (productSku.SelectedPurchaseScheme == null)
|
|
{
|
|
productSku.SelectedPurchaseScheme = new PurchaseScheme()
|
|
{
|
|
ProductId = productSku.ProductId,
|
|
SkuId = productSku.Id,
|
|
PurchasePlatform = PurchasePlatform
|
|
};
|
|
}
|
|
else if (productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Count >= 4)
|
|
{
|
|
MessageBox.Show("该采购方案的商品数量已达上限(5)");
|
|
return;
|
|
}
|
|
|
|
productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Add(new PurchaseSchemeProduct()
|
|
{
|
|
Id = DateTime.Now.ToFileTime(),
|
|
IsEditing = true,
|
|
ProductId = productSku.ProductId,
|
|
SkuId = productSku.Id
|
|
});
|
|
}
|
|
|
|
private void RemovePurchaseSchemeProduct(PurchaseSchemeProduct purchaseSchemeProduct)
|
|
{
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询
|
|
/// </summary>
|
|
/// <param name="purchaseSchemeProduct"></param>
|
|
private void GetPurchaseProductInfo(PurchaseSchemeProduct purchaseSchemeProduct)
|
|
{
|
|
//验证路径是否合法
|
|
if (string.IsNullOrEmpty(purchaseSchemeProduct.PurchaseUrl))
|
|
{
|
|
MessageBox.Show("商品url不能为空");
|
|
return;
|
|
}
|
|
|
|
urlPatternDictionary.TryGetValue(PurchasePlatform, out string pattern);
|
|
|
|
var match = Regex.Match(purchaseSchemeProduct.PurchaseUrl, pattern);
|
|
if (!match.Success)
|
|
{
|
|
MessageBox.Show("未能识别的url");
|
|
return;
|
|
}
|
|
var sku = ProductSkuList.FirstOrDefault(s => s.Id == purchaseSchemeProduct.SkuId);
|
|
var purchaseUrl = match.Groups[1].Value;
|
|
if (sku.SelectedPurchaseScheme.PurchaseSchemeProductList.Any(p => p.Id != purchaseSchemeProduct.Id && p.PurchaseUrl == purchaseUrl))
|
|
{
|
|
MessageBox.Show("商品链接重复");
|
|
return;
|
|
}
|
|
|
|
var purchaseProductId = match.Groups[2].Value;
|
|
|
|
IsLoading = true;
|
|
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
LoadPurchaseProduct(sku.SelectedPurchaseScheme.PurchasePlatform, purchaseSchemeProduct, purchaseProductId, (p) =>
|
|
{
|
|
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 "采购商必须相同";
|
|
|
|
return string.Empty;
|
|
}, out string errorMsg);
|
|
IsLoading = false;
|
|
|
|
if (!string.IsNullOrEmpty(errorMsg))
|
|
{
|
|
App.Current.Dispatcher.Invoke(() => MessageBox.Show(errorMsg, "绑定采购商"));
|
|
return;
|
|
}
|
|
|
|
purchaseSchemeProduct.PurchaseUrl = purchaseUrl;
|
|
purchaseSchemeProduct.PurchaseProductId = purchaseProductId;
|
|
});
|
|
}
|
|
|
|
private void ConfirmPurchaseProduct(PurchaseSchemeProduct purchaseSchemeProduct)
|
|
{
|
|
if (!purchaseSchemeProduct.SkuList.Any(s => s.IsSelected))
|
|
{
|
|
MessageBox.Show("至少选中一个采购Sku");
|
|
return;
|
|
}
|
|
purchaseSchemeProduct.PurchaseSchemeProductSkuList.Clear();
|
|
purchaseSchemeProduct.SelectedSkuIdList.Clear();
|
|
foreach (var sku in purchaseSchemeProduct.SkuList)
|
|
{
|
|
if (sku.IsSelected)
|
|
{
|
|
purchaseSchemeProduct.PurchaseSchemeProductSkuList.Add(sku);
|
|
purchaseSchemeProduct.SelectedSkuIdList.Add(sku.PurchaseSkuId);
|
|
}
|
|
}
|
|
purchaseSchemeProduct.IsEditing = false;
|
|
|
|
var productSku = ProductSkuList.FirstOrDefault(sku => sku.Id == purchaseSchemeProduct.SkuId);
|
|
productSku.SelectedPurchaseScheme.PurchaserId = PurchaserId;
|
|
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));
|
|
|
|
}
|
|
|
|
private void EditPurchaseProduct(PurchaseSchemeProduct purchaseSchemeProduct)
|
|
{
|
|
purchaseSchemeProduct.IsEditing = true;
|
|
}
|
|
|
|
private void SavePurchaseScheme()
|
|
{
|
|
if (!ProductSkuList.Any(s => s.SelectedPurchaseScheme != null && s.SelectedPurchaseScheme.PurchaseSchemeProductList.Count != 0))
|
|
{
|
|
MessageBox.Show("没有需要保存的数据,如需删除该采购商所有数据请返回采购商列表进行删除", "提示");
|
|
return;
|
|
}
|
|
|
|
var hasNoReady = ProductSkuList.Any(s => s.SelectedPurchaseScheme != null && s.SelectedPurchaseScheme.PurchaseSchemeProductList.Any(p => p.IsEditing));
|
|
if (hasNoReady)
|
|
{
|
|
MessageBox.Show("存在未保存的数据", "提示");
|
|
return;
|
|
}
|
|
|
|
var addPurchaseSchemeList = ProductSkuList.Where(s => s.SelectedPurchaseScheme != null &&
|
|
s.SelectedPurchaseScheme.PurchaseSchemeProductList.Count > 0 &&
|
|
s.SelectedPurchaseScheme.Id == 0)
|
|
.Select(s => s.SelectedPurchaseScheme).ToList();
|
|
var editPurchaseSchemeList = ProductSkuList.Where(s => s.SelectedPurchaseScheme != null &&
|
|
s.SelectedPurchaseScheme.PurchaseSchemeProductList.Count > 0 &&
|
|
s.SelectedPurchaseScheme.Id != 0)
|
|
.Select(s => s.SelectedPurchaseScheme).ToList();
|
|
SuppPurchaseSkuData(addPurchaseSchemeList);
|
|
SuppPurchaseSkuData(editPurchaseSchemeList);
|
|
|
|
IsLoading = true;
|
|
Task.Factory.StartNew(() =>
|
|
{
|
|
var response = purchaseService.EditPurchaseScheme(addPurchaseSchemeList, editPurchaseSchemeList);
|
|
IsLoading = false;
|
|
if (response.Success)
|
|
{
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
MessageBox.Show("绑定成功", "提示");
|
|
});
|
|
//关闭窗口
|
|
Messenger.Default.Send(true, "BindingPurchaseProduct_Close");
|
|
}
|
|
else
|
|
{
|
|
App.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
MessageBox.Show(response.Msg, "绑定失败");
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
private void Closing(System.ComponentModel.CancelEventArgs e)
|
|
{
|
|
PurchaserId = PurchaserName = string.Empty;
|
|
//clear data
|
|
foreach (var sku in ProductSkuList)
|
|
{
|
|
sku.SelectedPurchaseScheme = null;
|
|
}
|
|
//Product = null;
|
|
e.Cancel = false;
|
|
}
|
|
|
|
private void SuppPurchaseSkuData(IList<PurchaseScheme> purchaseSchemeList)
|
|
{
|
|
foreach (var scheme in purchaseSchemeList)
|
|
{
|
|
scheme.ShopId = globalContext.User.Shop.ShopId;
|
|
foreach (var purchaseProduct in scheme.PurchaseSchemeProductList)
|
|
{
|
|
foreach (var purchaseSku in purchaseProduct.PurchaseSchemeProductSkuList)
|
|
{
|
|
purchaseSku.ProductId = scheme.ProductId;
|
|
purchaseSku.SkuId = scheme.SkuId;
|
|
purchaseSku.SkuPurchaseSchemeId = scheme.Id;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SearchPurchaseSkuName(PurchaseSchemeProduct purchaseSchemeProduct)
|
|
{
|
|
purchaseSchemeProduct.SearchSkuList.Clear();
|
|
if (string.IsNullOrEmpty(purchaseSchemeProduct.SearchPurchaseSkuName))
|
|
{
|
|
foreach (var sku in purchaseSchemeProduct.SkuList)
|
|
purchaseSchemeProduct.SearchSkuList.Add(sku);
|
|
}
|
|
else
|
|
{
|
|
var searchList = purchaseSchemeProduct.SkuList.Where(s => s.Title.Contains(purchaseSchemeProduct.SearchPurchaseSkuName));
|
|
foreach (var sku in searchList)
|
|
purchaseSchemeProduct.SearchSkuList.Add(sku);
|
|
}
|
|
}
|
|
|
|
private void ClearPurchaseSkuName(PurchaseSchemeProduct purchaseSchemeProduct)
|
|
{
|
|
purchaseSchemeProduct.SearchPurchaseSkuName = string.Empty;
|
|
SearchPurchaseSkuName(purchaseSchemeProduct);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|