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
{
///
/// 店铺营收统计 - 页面 ViewModel(MVVM)。
/// 通过 IShopRevenueStatisticsService 调用数据接口;
/// 接入真实接口时传入自定义实现:new ShopRevenueStatisticsViewModel(你的服务实现)。
///
public class ShopRevenueStatisticsViewModel : PropertyNotifyObject
{
private readonly IShopRevenueStatisticsService _service;
private ShopRevenueSummary _summary;
private ObservableCollection _trendData;
private ObservableCollection _shopProportionData;
private ObservableCollection _shopStatistics;
private ShopOption _selectedShop;
private string _selectedPlatform;
public ShopRevenueStatisticsViewModel()
: this(new ShopRevenueStatisticsService())
{
}
public ShopRevenueStatisticsViewModel(IShopRevenueStatisticsService service)
{
_service = service ?? new ShopRevenueStatisticsService();
TimeRangeOptions = new ObservableCollection
{
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();
QueryCommand = new DelegateCommand(async () => await LoadAsync());
ResetCommand = new DelegateCommand(Reset);
InitializeAsync();
}
#region 查询条件
/// 时间范围选项
public ObservableCollection TimeRangeOptions { get; }
/// 选中的时间范围
public TimeRangeOption SelectedTimeRange
{
get { return this.GetValue(x => x.SelectedTimeRange); }
set
{
this.SetValue(x => x.SelectedTimeRange, value);
ApplyTimeRange();
OnPropertyChanged(nameof(IsCustomDateRange));
}
}
/// 是否为自定义时间范围
public bool IsCustomDateRange
{
get { return SelectedTimeRange != null && SelectedTimeRange.Value == "custom"; }
}
/// 开始日期
public DateTime StartDate
{
get { return this.GetValue(x => x.StartDate); }
set { this.SetValue(x => x.StartDate, value); }
}
/// 结束日期
public DateTime EndDate
{
get { return this.GetValue(x => x.EndDate); }
set { this.SetValue(x => x.EndDate, value); }
}
/// 店铺选项(含“全部店铺”)
public ObservableCollection ShopOptions { get; }
/// 选中的店铺
public ShopOption SelectedShop
{
get { return _selectedShop; }
set
{
_selectedShop = value;
OnPropertyChanged(nameof(SelectedShop));
}
}
#endregion
#region 统计结果
/// 汇总卡片数据
public ShopRevenueSummary Summary
{
get { return _summary; }
private set
{
_summary = value;
OnPropertyChanged(nameof(Summary));
}
}
/// 销售趋势数据
public ObservableCollection TrendData
{
get { return _trendData; }
private set
{
_trendData = value;
OnPropertyChanged(nameof(TrendData));
}
}
/// 店铺销售占比数据
public ObservableCollection ShopProportionData
{
get { return _shopProportionData; }
private set
{
_shopProportionData = value;
OnPropertyChanged(nameof(ShopProportionData));
}
}
/// 店铺统计表格数据
public ObservableCollection ShopStatistics
{
get { return _shopStatistics; }
private set
{
_shopStatistics = value;
OnPropertyChanged(nameof(ShopStatistics));
}
}
/// 是否正在加载
public bool IsLoading
{
get { return this.GetValue(x => x.IsLoading); }
set { this.SetValue(x => x.IsLoading, value); }
}
#endregion
#region 命令
/// 查询命令
public ICommand QueryCommand { get; }
/// 重置命令
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 业务逻辑
/// 根据时间范围选项计算起止日期
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;
}
}
/// 执行查询,刷新页面所有统计数据
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(trend);
var proportion = await _service.GetShopSalesProportionAsync(query);
ShopProportionData = new ObservableCollection(proportion);
var table = await _service.GetShopStatisticsAsync(query);
ShopStatistics = new ObservableCollection(table);
}
catch (Exception)
{
// 查询异常:保留上一次数据,可在此接入日志/错误提示
}
finally
{
IsLoading = false;
}
}
/// 重置查询条件并重新加载
private async void Reset()
{
SelectedShop = ShopOptions.FirstOrDefault();
SelectedTimeRange = TimeRangeOptions.FirstOrDefault(t => t.Value == "month");
ApplyTimeRange();
await LoadAsync();
}
#endregion
}
}