新增加 利润统计Window。

隐藏利润统计按钮
增加 Common 文件夹 内部是WPF公用类库
This commit is contained in:
“chfei” committed 2026-08-05 18:55:03 +08:00
1 parent 4a7af051b2
commit 7c6f91f207
25 files changed
+6462 -3554

No files matched your search

@@ -0,0 +1,288 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using AllBase.Model.MvvmBaseClasses;
using _29WxOrderWpf.Ctrls.Statistics.Models;
using _29WxOrderWpf.Ctrls.Statistics.Services;
using CefSharp;
using _29WxOrderWpf.Commands;
namespace _29WxOrderWpf.Ctrls.Statistics.ViewModels
{
/// <summary>
/// 店铺营收统计 - 页面 ViewModel(MVVM)。
/// 通过 IShopRevenueStatisticsService 调用数据接口;
/// 接入真实接口时传入自定义实现:new ShopRevenueStatisticsViewModel(你的服务实现)。
/// </summary>
public class ShopRevenueStatisticsViewModel : PropertyNotifyObject
{
private readonly IShopRevenueStatisticsService _service;
private ShopRevenueSummary _summary;
private ObservableCollection<RevenueTrendPoint> _trendData;
private ObservableCollection<ShopSalesProportionItem> _shopProportionData;
private ObservableCollection<ShopStatisticsItem> _shopStatistics;
private ShopOption _selectedShop;
private string _selectedPlatform;
public ShopRevenueStatisticsViewModel()
: this(new ShopRevenueStatisticsService())
{
}
public ShopRevenueStatisticsViewModel(IShopRevenueStatisticsService service)
{
_service = service ?? new ShopRevenueStatisticsService();
TimeRangeOptions = new ObservableCollection<TimeRangeOption>
{
new TimeRangeOption { Text = "昨天", Value = "yesterday" },
new TimeRangeOption { Text = "本周", Value = "week" },
new TimeRangeOption { Text = "本月", Value = "month" },
new TimeRangeOption { Text = "最近10天", Value = "last10" },
new TimeRangeOption { Text = "自定义", Value = "custom" }
};
ShopOptions = new ObservableCollection<ShopOption>();
QueryCommand = new DelegateCommand(async () => await LoadAsync());
ResetCommand = new DelegateCommand(Reset);
InitializeAsync();
}
#region 查询条件
/// <summary>时间范围选项</summary>
public ObservableCollection<TimeRangeOption> TimeRangeOptions { get; }
/// <summary>选中的时间范围</summary>
public TimeRangeOption SelectedTimeRange
{
get { return this.GetValue(x => x.SelectedTimeRange); }
set
{
this.SetValue(x => x.SelectedTimeRange, value);
ApplyTimeRange();
OnPropertyChanged(nameof(IsCustomDateRange));
}
}
/// <summary>是否为自定义时间范围</summary>
public bool IsCustomDateRange
{
get { return SelectedTimeRange != null && SelectedTimeRange.Value == "custom"; }
}
/// <summary>开始日期</summary>
public DateTime StartDate
{
get { return this.GetValue(x => x.StartDate); }
set { this.SetValue(x => x.StartDate, value); }
}
/// <summary>结束日期</summary>
public DateTime EndDate
{
get { return this.GetValue(x => x.EndDate); }
set { this.SetValue(x => x.EndDate, value); }
}
/// <summary>店铺选项(含“全部店铺”)</summary>
public ObservableCollection<ShopOption> ShopOptions { get; }
/// <summary>选中的店铺</summary>
public ShopOption SelectedShop
{
get { return _selectedShop; }
set
{
_selectedShop = value;
OnPropertyChanged(nameof(SelectedShop));
}
}
#endregion
#region 统计结果
/// <summary>汇总卡片数据</summary>
public ShopRevenueSummary Summary
{
get { return _summary; }
private set
{
_summary = value;
OnPropertyChanged(nameof(Summary));
}
}
/// <summary>销售趋势数据</summary>
public ObservableCollection<RevenueTrendPoint> TrendData
{
get { return _trendData; }
private set
{
_trendData = value;
OnPropertyChanged(nameof(TrendData));
}
}
/// <summary>店铺销售占比数据</summary>
public ObservableCollection<ShopSalesProportionItem> ShopProportionData
{
get { return _shopProportionData; }
private set
{
_shopProportionData = value;
OnPropertyChanged(nameof(ShopProportionData));
}
}
/// <summary>店铺统计表格数据</summary>
public ObservableCollection<ShopStatisticsItem> ShopStatistics
{
get { return _shopStatistics; }
private set
{
_shopStatistics = value;
OnPropertyChanged(nameof(ShopStatistics));
}
}
/// <summary>是否正在加载</summary>
public bool IsLoading
{
get { return this.GetValue(x => x.IsLoading); }
set { this.SetValue(x => x.IsLoading, value); }
}
#endregion
#region 命令
/// <summary>查询命令</summary>
public ICommand QueryCommand { get; }
/// <summary>重置命令</summary>
public ICommand ResetCommand { get; }
#endregion
#region 初始化
private async void InitializeAsync()
{
try
{
// 店铺选项
ShopOptions.Add(new ShopOption { AppKey = string.Empty, ShopName = "全部店铺" });
var shops = await _service.GetShopOptionsAsync();
foreach (var shop in shops)
ShopOptions.Add(shop);
SelectedShop = ShopOptions.FirstOrDefault();
// 默认时间范围:本月
SelectedTimeRange = TimeRangeOptions.FirstOrDefault(t => t.Value == "month");
ApplyTimeRange();
await LoadAsync();
}
catch (Exception)
{
// 初始化失败(如接口不可用),页面保持可查询状态;可在此接入日志/提示
}
}
#endregion
#region 业务逻辑
/// <summary>根据时间范围选项计算起止日期</summary>
private void ApplyTimeRange()
{
var now = DateTime.Now;
var today = now.Date;
var selected = SelectedTimeRange == null ? string.Empty : SelectedTimeRange.Value;
switch (selected)
{
case "yesterday":
StartDate = today.AddDays(-1);
EndDate = today.AddDays(-1).AddHours(23).AddMinutes(59).AddSeconds(59);
break;
case "week":
int diff = today.DayOfWeek == DayOfWeek.Sunday ? 6 : (int)today.DayOfWeek - 1;
StartDate = today.AddDays(-diff);
EndDate = now;
break;
case "month":
StartDate = new DateTime(today.Year, today.Month, 1);
EndDate = now;
break;
case "last10":
StartDate = today.AddDays(-9);
EndDate = now;
break;
default:
// 自定义:保持用户选择的日期不变
break;
}
}
/// <summary>执行查询,刷新页面所有统计数据</summary>
private async Task LoadAsync()
{
if (StartDate > EndDate)
{
var temp = StartDate;
StartDate = EndDate;
EndDate = temp;
}
IsLoading = true;
try
{
var query = new ShopRevenueStatisticsQuery
{
StartDate = StartDate,
EndDate = EndDate,
ShopKey = SelectedShop == null ? string.Empty : SelectedShop.AppKey,
};
Summary = await _service.GetSummaryAsync(query);
var trend = await _service.GetSalesTrendAsync(query);
TrendData = new ObservableCollection<RevenueTrendPoint>(trend);
var proportion = await _service.GetShopSalesProportionAsync(query);
ShopProportionData = new ObservableCollection<ShopSalesProportionItem>(proportion);
var table = await _service.GetShopStatisticsAsync(query);
ShopStatistics = new ObservableCollection<ShopStatisticsItem>(table);
}
catch (Exception)
{
// 查询异常:保留上一次数据,可在此接入日志/错误提示
}
finally
{
IsLoading = false;
}
}
/// <summary>重置查询条件并重新加载</summary>
private async void Reset()
{
SelectedShop = ShopOptions.FirstOrDefault();
SelectedTimeRange = TimeRangeOptions.FirstOrDefault(t => t.Value == "month");
ApplyTimeRange();
await LoadAsync();
}
#endregion
}
}