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.

123 lines
4.3 KiB

2 years ago
using BBWYB.Client.APIServices;
using BBWYB.Client.Models;
using BBWYB.Client.Views.Order;
2 years ago
using CommunityToolkit.Mvvm.Input;
2 years ago
using CommunityToolkit.Mvvm.Messaging;
using System;
2 years ago
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace BBWYB.Client.ViewModels
{
public class EditPriceViewModel : BaseVM
{
2 years ago
private OrderService orderService;
2 years ago
public IList<OrderSkuEditPrice> OrderSkuList { get; set; }
private Order order;
private decimal freightAmount;
private bool isResponseFreightChanged;
public ICommand DistributFreightCommand { get; set; }
2 years ago
public ICommand SaveCommand { get; set; }
2 years ago
public decimal FreightAmount { get => freightAmount; set { SetProperty(ref freightAmount, value); } }
2 years ago
public EditPriceViewModel(OrderService orderService)
2 years ago
{
OrderSkuList = new ObservableCollection<OrderSkuEditPrice>();
DistributFreightCommand = new RelayCommand(DistributFreight);
2 years ago
SaveCommand = new RelayCommand(Save);
this.orderService = orderService;
2 years ago
}
2 years ago
public void SetData(Order order)
2 years ago
{
this.order = order;
foreach (var orderSku in order.ItemList)
{
OrderSkuList.Add(new OrderSkuEditPrice()
{
Id = orderSku.Id,
Logo = orderSku.Logo,
Title = orderSku.Title,
SkuId = orderSku.SkuId,
Price = orderSku.Price,
NewPrice = orderSku.Price ?? 0,
ItemTotal = orderSku.ItemTotal,
OnFreightChanged = OnFreightChanged
});
}
}
private void DistributFreight()
{
if (FreightAmount <= 0)
{
MessageBox.Show("运费不正确", "提示");
return;
}
isResponseFreightChanged = false;
var totalItemCount = OrderSkuList.Sum(s => s.ItemTotal);
foreach (var orderSku in OrderSkuList)
{
var quantityRatio = 1M * orderSku.ItemTotal / totalItemCount;
var currentFreightAmount = FreightAmount * quantityRatio;
orderSku.FreightAmount = currentFreightAmount;
orderSku.IsResponseFreightChanged = true;
}
isResponseFreightChanged = true;
}
private void OnFreightChanged()
{
if (!isResponseFreightChanged)
return;
2 years ago
isResponseFreightChanged = false;
var waitDistributFreight = FreightAmount - OrderSkuList.Where(osku => !osku.IsResponseFreightChanged).Sum(osku => osku.FreightAmount);
var waitDistributSkuList = OrderSkuList.Where(osku => osku.IsResponseFreightChanged);
var totalItemCount = waitDistributSkuList.Sum(osku => osku.ItemTotal);
foreach (var orderSku in waitDistributSkuList)
{
var quantityRatio = 1M * orderSku.ItemTotal / totalItemCount;
var currentFreightAmount = waitDistributFreight * quantityRatio;
orderSku.FreightAmount = currentFreightAmount;
orderSku.IsResponseFreightChanged = true;
}
isResponseFreightChanged = true;
}
private void Save()
{
if (FreightAmount <= 0)
{
MessageBox.Show("运费不正确", "提示");
return;
}
if (OrderSkuList.Any(osku => osku.FreightAmount <= 0 || osku.NewPrice <= 0))
{
MessageBox.Show("运费或单价不正确", "提示");
return;
}
if (Math.Abs(FreightAmount - OrderSkuList.Sum(osku => osku.FreightAmount)) > 1)
{
MessageBox.Show("运费超出误差范围", "提示");
return;
}
var response = orderService.EditPrice(this.order.Id, OrderSkuList);
if (!response.Success)
{
MessageBox.Show(response.Msg, "提示");
return;
}
WeakReferenceMessenger.Default.Send(new Message_EditPrice_Close(null));
2 years ago
}
}
}