步步为盈
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.

442 lines
19 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 OneBoundAPIService oneBoundAPIService;
private PurchaseService purchaseService;
private string purchaserName;
private bool isLoading;
//public Product Product { get; set; }
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); } }
#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; }
#endregion
#region Methods
public BindingPurchaseProductViewModel(OneBoundAPIService oneBoundAPIService, PurchaseService purchaseService, GlobalContext globalContext)
{
this.globalContext = globalContext;
this.oneBoundAPIService = oneBoundAPIService;
this.purchaseService = purchaseService;
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);
}
public void SetData(IList<ProductSku> productSkuList, string purchaserId, string purchaserName)
{
this.ProductSkuList = productSkuList;
//this.Product = product;
this.PurchaserId = purchaserId;
this.PurchaserName = purchaserName;
}
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)).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(purchaseSchemeProduct);
}
ewh.Set();
ewh.Dispose();
});
}
WaitHandle.WaitAll(waitList.ToArray());
IsLoading = false;
});
}
}
public void LoadPurchaseProduct(PurchaseSchemeProduct purchaseSchemeProduct, IList<PurchaseSchemeProductSku> skuList = null)
{
App.Current.Dispatcher.Invoke(() =>
{
purchaseSchemeProduct.SkuList.Clear();
purchaseSchemeProduct.PurchaseSchemeProductSkuList.Clear();
});
if (skuList == null)
skuList = LoadPurchaseProductCore(purchaseSchemeProduct.PurchaseProductId, out _, out _, out _, out _, null);
if (skuList == null)
return;
App.Current.Dispatcher.BeginInvoke((Action)delegate
{
foreach (var sku in skuList)
{
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");
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 string GetSkuLogo(JToken skuJToken, JArray prop_img)
{
if (!prop_img.HasValues)
return "pack://application:,,,/Resources/Images/defaultItem.png";
var properties = skuJToken.Value<string>("properties").Split(';', StringSplitOptions.RemoveEmptyEntries);
foreach (var p in properties)
{
var imgJToken = prop_img.FirstOrDefault(g => g.Value<string>("properties") == p);
if (imgJToken != null)
return imgJToken.Value<string>("url");
}
return "pack://application:,,,/Resources/Images/defaultItem.png";
}
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
};
}
else if (productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Count >= 4)
{
MessageBox.Show("该采购方案的商品数量已达上限(4)");
return;
}
productSku.SelectedPurchaseScheme.PurchaseSchemeProductList.Add(new PurchaseSchemeProduct()
{
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;
}
var match = Regex.Match(purchaseSchemeProduct.PurchaseUrl, @"^(https://detail.1688.com/offer/(\d+).html)[^\s]*");
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.PurchaseUrl == purchaseUrl))
{
MessageBox.Show("商品链接重复");
return;
}
var purchaseProductId = match.Groups[2].Value;
IsLoading = true;
Task.Factory.StartNew(() =>
{
var purchaseSchemeProductSkuList = LoadPurchaseProductCore(purchaseProductId,
out string errorMsg,
out string purchaserId,
out string purchaserName,
out string purchaserLocation,
(p) =>
{
if (sku.PurchaseSchemeList.Any(s => s.PurchaserId == p))
return $"sku{sku.Id}的采购方案中已存在相同的采购商";
if (!string.IsNullOrEmpty(PurchaserId) && p != PurchaserId)
return "采购商必须相同";
return string.Empty;
});
IsLoading = false;
if (!string.IsNullOrEmpty(errorMsg))
{
App.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show(errorMsg, "绑定采购商");
});
return;
}
PurchaserId = purchaserId;
PurchaserName = purchaserName;
PurchaserLocation = purchaserLocation;
purchaseSchemeProduct.PurchaseUrl = purchaseUrl;
purchaseSchemeProduct.PurchaseProductId = purchaseProductId;
LoadPurchaseProduct(purchaseSchemeProduct, purchaseSchemeProductSkuList);
});
}
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;
}
}
}
}
#endregion
}
}