Files
29WxOrder/29WxOrderWpf/Ctrls/Statistics/ShowOrderDialogView.xaml.cs
T
2026-10-07 16:16:55 +08:00

129 lines
3.4 KiB
C#

using _29WxOrderWpf.BaseWindows;
using _29WxOrderWpf.Commands;
using _29WxOrderWpf.Ctrls.Statistics.Models;
using _29WxOrderWpf.ToolCtrls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFCommon.Models;
namespace _29WxOrderWpf.Ctrls.Statistics;
/// <summary>
/// ShowOrderDialogView.xaml 的交互逻辑
/// </summary>
public partial class ShowOrderDialogView : UserControl
{
public ShowOrderDialogView(List<ShopRevenueStatisticsRow> list)
{
InitializeComponent();
this.DataContext = new ShowOrderDialogViewModel(list);
}
/// <summary>
/// 复制订单号到剪贴板
/// </summary>
private void CopyOrderId_Click(object sender, RoutedEventArgs e)
{
if (sender is FrameworkElement element
&& element.DataContext is ShopRevenueStatisticsRow row
&& !string.IsNullOrEmpty(row.OrderId))
{
Window window = Window.GetWindow(this);
try
{
Clipboard.SetDataObject(row.OrderId);
ToastHelper.Show(window, "复制成功");
}
catch (Exception ex)
{
ToastHelper.Show(window, "复制失败 " + ex.Message, type: ToastType.Error);
}
}
}
}
class ShowOrderDialogViewModel : BindableBaseEx
{
public ObservableCollection<ShopRevenueStatisticsRow> Data { get; set; }
public ICollectionView DataView { get; private set; }
public ShowOrderDialogViewModel(List<ShopRevenueStatisticsRow> list)
{
Data = new ObservableCollection<ShopRevenueStatisticsRow>(list ?? new List<ShopRevenueStatisticsRow>());
DataView = CollectionViewSource.GetDefaultView(Data);
DataView.Filter = FilterRow;
}
private bool FilterRow(object obj)
{
if (string.IsNullOrWhiteSpace(OrderId))
return true;
if (obj is ShopRevenueStatisticsRow row)
{
return row.OrderId.Contains(OrderId);
}
return false;
}
public string OrderId
{
get => GetValue<string>();
set => SetValue(value, () => DataView.Refresh());
}
public ICommand SelectAllCommand => new DelegateCommand(SelectAll);
private void SelectAll()
{
var sb = new StringBuilder();
// 表头
sb.AppendLine("店名\t订单\t金额\t退款金额\t外采金额");
// 数据行
foreach (var item in Data)
{
string shopName = CsvEscape(item.ShopName);
string orderId = item.OrderId;
string orderPrice = (item.OrderPrice / 100m).ToString("0.00");
string refundAmount = (item.RefundAmount / 100m).ToString("0.00");
string outAmount = (item.OutAmount / 100m).ToString("0.00");
sb.AppendLine($"{shopName}\t{orderId}\t{orderPrice}\t{refundAmount}\t{outAmount}");
}
Application.Current.Dispatcher.Invoke(() => Clipboard.SetText(sb.ToString()));
var window = Application.Current.Windows
.OfType<Window>()
.FirstOrDefault(w => w.IsActive) ?? Application.Current.MainWindow;
ToastHelper.Show(window, "复制成功");
}
private string CsvEscape(string value)
{
if (string.IsNullOrEmpty(value))
return "";
// 含逗号、引号、换行时,用双引号包起来,内部引号翻倍
if (value.Contains(',') || value.Contains('"') || value.Contains('\n') || value.Contains('\r'))
{
return "\"" + value.Replace("\"", "\"\"") + "\"";
}
return value;
}
}