统计页面增加 tab页面 天,周,月 统计
This commit is contained in:
1 parent
355a0f6be7
commit
872dc70094
10 files changed
+1222
-19
No files matched your search
@@ -39,6 +39,9 @@ namespace _29WxOrderWpf.Ctrls.Statistics.Services
|
||||
/// <summary>获取店铺统计表格</summary>
|
||||
Task<List<ShopStatisticsItem>> GetShopStatisticsAsync(ShopRevenueStatisticsQuery query);
|
||||
|
||||
/// <summary>获取天/周/月周期统计表格(每天/每周/每月各一行,按所选店铺或全部店铺统计)</summary>
|
||||
Task<List<ShopPeriodStatisticsItem>> GetPeriodStatisticsAsync(ShopRevenueStatisticsQuery query, PeriodStatisticType periodType);
|
||||
|
||||
/// <summary>获取店铺下拉列表(用于查询条件)</summary>
|
||||
Task<List<ShopOption>> GetShopOptionsAsync();
|
||||
}
|
||||
@@ -197,6 +200,113 @@ namespace _29WxOrderWpf.Ctrls.Statistics.Services
|
||||
.OrderByDescending(i => i.SalesAmount)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>天/周/月周期统计表格(每天/每周/每月各一行,无数据的周期也补一行,金额单位:元)</summary>
|
||||
public static List<ShopPeriodStatisticsItem> GetPeriodStatistics(IEnumerable<ShopRevenueStatisticsRow> rows, DateTime startDate, DateTime endDate, PeriodStatisticType periodType)
|
||||
{
|
||||
var list = (rows ?? new List<ShopRevenueStatisticsRow>()).ToList();
|
||||
|
||||
// 有数据的周期
|
||||
var map = list
|
||||
.GroupBy(r => GetPeriodStart(r.CreateTime, periodType))
|
||||
.ToDictionary(
|
||||
g => g.Key,
|
||||
g =>
|
||||
{
|
||||
var valid = g.Where(IsValidOrder).ToList();
|
||||
var refunded = g.Where(IsRefunded).ToList();
|
||||
decimal salesAmount = valid.Sum(r => (decimal)r.OrderPrice) / 100m;
|
||||
int orderCount = valid.Select(r => r.OrderId).Distinct().Count();
|
||||
return new ShopPeriodStatisticsItem
|
||||
{
|
||||
PeriodStart = g.Key,
|
||||
Period = GetPeriodText(g.Key, periodType),
|
||||
OrderCount = orderCount,
|
||||
SalesAmount = salesAmount,
|
||||
Profit = valid.Sum(r => (decimal)r.Profit) / 100m,
|
||||
RefundAmount = refunded.Sum(r => (decimal)r.OrderPrice) / 100m,
|
||||
CustomerCount = orderCount,
|
||||
AvgOrderAmount = orderCount == 0 ? 0m : salesAmount / orderCount
|
||||
};
|
||||
});
|
||||
|
||||
// 按周期补齐整段时间:无数据的周期也生成一行(全 0)
|
||||
var result = new List<ShopPeriodStatisticsItem>();
|
||||
foreach (var periodStart in EnumeratePeriods(startDate, endDate, periodType))
|
||||
{
|
||||
if (map.TryGetValue(periodStart, out var item))
|
||||
result.Add(item);
|
||||
else
|
||||
result.Add(new ShopPeriodStatisticsItem
|
||||
{
|
||||
PeriodStart = periodStart,
|
||||
Period = GetPeriodText(periodStart, periodType),
|
||||
OrderCount = 0,
|
||||
SalesAmount = 0m,
|
||||
Profit = 0m,
|
||||
RefundAmount = 0m,
|
||||
CustomerCount = 0,
|
||||
AvgOrderAmount = 0m
|
||||
});
|
||||
}
|
||||
|
||||
// 倒序排列:最新的周期在最上面
|
||||
result.Reverse();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>枚举 startDate~endDate 覆盖的所有周期开始日期(天/周/月)</summary>
|
||||
private static IEnumerable<DateTime> EnumeratePeriods(DateTime startDate, DateTime endDate, PeriodStatisticType periodType)
|
||||
{
|
||||
var start = GetPeriodStart(startDate, periodType);
|
||||
var end = GetPeriodStart(endDate, periodType);
|
||||
switch (periodType)
|
||||
{
|
||||
case PeriodStatisticType.Week:
|
||||
for (var d = start; d <= end; d = d.AddDays(7))
|
||||
yield return d;
|
||||
break;
|
||||
case PeriodStatisticType.Month:
|
||||
for (var d = start; d <= end; d = d.AddMonths(1))
|
||||
yield return d;
|
||||
break;
|
||||
default:
|
||||
for (var d = start; d <= end; d = d.AddDays(1))
|
||||
yield return d;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>周期开始日期:天=当天;周=周一起始;月=当月1号</summary>
|
||||
private static DateTime GetPeriodStart(DateTime time, PeriodStatisticType periodType)
|
||||
{
|
||||
switch (periodType)
|
||||
{
|
||||
case PeriodStatisticType.Week:
|
||||
var d = time.Date;
|
||||
int diff = d.DayOfWeek == DayOfWeek.Sunday ? 6 : (int)d.DayOfWeek - 1;
|
||||
return d.AddDays(-diff);
|
||||
case PeriodStatisticType.Month:
|
||||
return new DateTime(time.Year, time.Month, 1);
|
||||
default:
|
||||
return time.Date;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>周期显示文本:天=yyyy-MM-dd;周=yyyy-MM-dd ~ yyyy-MM-dd;月=yyyy-MM</summary>
|
||||
private static string GetPeriodText(DateTime periodStart, PeriodStatisticType periodType)
|
||||
{
|
||||
switch (periodType)
|
||||
{
|
||||
case PeriodStatisticType.Week:
|
||||
return periodStart.ToString("yyyy-MM-dd") + " ~ " + periodStart.AddDays(6).ToString("yyyy-MM-dd");
|
||||
case PeriodStatisticType.Month:
|
||||
return periodStart.ToString("yyyy-MM");
|
||||
default:
|
||||
return periodStart.ToString("yyyy-MM-dd");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -317,15 +427,24 @@ namespace _29WxOrderWpf.Ctrls.Statistics.Services
|
||||
sb.Append("&end_time=").Append(BaseClass.ConvertDateTimeLong(endDate.Date.AddDays(1).AddTicks(-1)));
|
||||
var html = await GetHtml.PostUtf8Async(url, sb.ToString());
|
||||
|
||||
var ret = JsonConvert.DeserializeObject<Result<List<ShopRevenueStatisticsRow>>>(html, new JsonSerializerSettings
|
||||
try
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
});
|
||||
if (ret.Code != 200)
|
||||
{
|
||||
throw new Exception(ret.Msg);
|
||||
var ret = JsonConvert.DeserializeObject<Result<List<ShopRevenueStatisticsRow>>>(html, new JsonSerializerSettings
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
||||
});
|
||||
if (ret.Code != 200)
|
||||
{
|
||||
throw new Exception(ret.Msg);
|
||||
}
|
||||
return ret.Data ?? new List<ShopRevenueStatisticsRow>();
|
||||
|
||||
}
|
||||
return ret.Data ?? new List<ShopRevenueStatisticsRow>();
|
||||
catch
|
||||
{
|
||||
throw new Exception("查询失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -427,6 +546,13 @@ namespace _29WxOrderWpf.Ctrls.Statistics.Services
|
||||
return RevenueStatisticsAggregator.GetShopStatistics(rows);
|
||||
}
|
||||
|
||||
/// <summary>获取天/周/月周期统计表格</summary>
|
||||
public async Task<List<ShopPeriodStatisticsItem>> GetPeriodStatisticsAsync(ShopRevenueStatisticsQuery query, PeriodStatisticType periodType)
|
||||
{
|
||||
var rows = await QueryRowsAsync(query);
|
||||
return RevenueStatisticsAggregator.GetPeriodStatistics(rows, query.StartDate, query.EndDate, periodType);
|
||||
}
|
||||
|
||||
public Task<List<ShopOption>> GetShopOptionsAsync()
|
||||
{
|
||||
var list = GlobalConfig.ShopLoginList.Select(a =>
|
||||
|
||||
Reference in new issue
Block a user