using Newtonsoft.Json;
using System;
using System.Collections.Generic;
namespace _29WxOrderWpf.Ctrls.Statistics.Models
{
///
/// 店铺营收统计 - 查询参数
///
public class ShopRevenueStatisticsQuery
{
/// 开始时间
public DateTime StartDate { get; set; }
/// 结束时间
public DateTime EndDate { get; set; }
/// 选中的店铺 app_key 列表;空/Null 表示全部店铺
public List ShopKeys { get; set; }
}
///
/// 店铺营收统计 - 接口返回的原始明细行。
/// 与后端 WXOrderAmountInfo 保持一致(camelCase JSON):
/// shopName, createTime, orderId, status, productPrice, orderPrice, purchasePrice, freight,
/// predictCommissionFee, commissionAmount, discountedPrice, profit, purchaseStatus,
/// outAmount, refundStatus, refundStatusString, refundAmount, externalProcurement
/// 金额字段单位均为分(显示时需除以 100)。
///
public class ShopRevenueStatisticsRow
{
///
/// 店铺名称
///
[JsonProperty("shop_name")]
public string ShopName { get; set; }
///
/// 下单时间(秒级时间戳)
///
[JsonProperty("create_time")]
[JsonConverter(typeof(UnixDateTimeConverter))]
public DateTime CreateTime { get; set; }
///
/// 订单号
///
[JsonProperty("order_id")]
public string OrderId { get; set; }
///
/// 订单状态
///
[JsonProperty("status")]
public int Status { get; set; }
///
/// 商品总价(分)
///
[JsonProperty("product_price")]
public int ProductPrice { get; set; }
///
/// 订单金额(分)
///
[JsonProperty("order_price")]
public int OrderPrice { get; set; }
///
/// 采购价(分)
///
[JsonProperty("purchase_price")]
public int PurchasePrice { get; set; }
///
/// 运费(分)
///
[JsonProperty("freight")]
public int Freight { get; set; }
///
/// 平台服务费(预计技术服务费,分)
///
[JsonProperty("predict_commission_fee")]
public int PredictCommissionFee { get; set; }
///
/// 达人佣金(分)
///
[JsonProperty("commission_amount")]
public int CommissionAmount { get; set; }
///
/// 优惠金额(分)
///
[JsonProperty("discounted_price")]
public int DiscountedPrice { get; set; }
///
/// 利润(分)
///
[JsonProperty("profit")]
public int Profit { get; set; }
///
/// 采购状态
///
[JsonProperty("purchase_status")]
public int PurchaseStatus { get; set; }
///
/// 外采金额(分),out_amount为空或0时从中转仓订单表补全
///
[JsonProperty("out_amount")]
public int OutAmount { get; set; }
///
/// 售后状态(订单明细 refund_status)
///
[JsonProperty("refund_status")]
public int RefundStatus { get; set; }
///
/// 售后状态(退款单 status)
///
[JsonProperty("refund_status_string")]
public string RefundStatusString { get; set; }
///
/// 售后退款金额(分)
///
[JsonProperty("refund_amount")]
public int RefundAmount { get; set; }
///
/// 是否外采:0=未从中转仓订单表查到,1=查到
///
[JsonProperty("external_procurement")]
public int ExternalProcurement { get; set; }
///
/// 买手退款金额
///
[JsonProperty("buyer_refund_amount")]
public int BuyerRefundAmount { get; set; }
///
/// 买手退款状态
///
[JsonProperty("buyer_refund_status")]
public string BuyerRefundStatus { get; set; }
}
///
/// 顶部汇总卡片数据(金额单位:元)
///
public class ShopRevenueSummary
{
/// 销售额(最终有效销售额)
public decimal SalesAmount { get; set; }
/// 有效订单数(已扣除退款订单)
public int ValidOrderCount { get; set; }
/// 利润(实际利润)
public decimal Profit { get; set; }
/// 退款金额(统计期间退款)
public decimal RefundAmount { get; set; }
/// 退款订单数
public int RefundOrderCount { get; set; }
/// 平台服务费总额(元)
public decimal PlatformFeeTotal { get; set; }
/// 达人佣金(元)
public decimal Commission { get; set; }
/// 客单价(平均每单金额)
public decimal AvgOrderAmount { get; set; }
/// 退款率(退款订单占比,百分数,如 2.31 表示 2.31%)
public decimal RefundRate { get; set; }
/// 平均利润率(利润 / 采购金额,百分数,如 12.34 表示 12.34%)
public decimal AverageProfitRate { get; set; }
/// 买手号付款金额(采购金额,元)
public decimal BuyerPaymentAmount { get; set; }
/// 买手号退款金额(采购退款金额,元)
public decimal BuyerRefundAmount { get; set; }
/// 买手号退款成功金额(采购退款金额,元)
public decimal BuyerRefundSuccessedAmount { get; set; }
/// 买手号剩余退款金额(尚未成功退款的金额,元)
public decimal BuyerRefundRemainingAmount => BuyerRefundAmount - BuyerRefundSuccessedAmount;
}
///
/// 销售趋势数据点(金额单位:元)
///
public class RevenueTrendPoint
{
/// 日期
public DateTime Date { get; set; }
/// 日期文本(MM-dd)
public string DateText { get { return Date.ToString("MM-dd"); } }
/// 当日销售额(元)
public decimal SalesAmount { get; set; }
/// 当日有效订单数
public int OrderCount { get; set; }
}
///
/// 店铺销售占比数据(金额单位:元)
///
public class ShopSalesProportionItem
{
/// 店铺名称
public string ShopName { get; set; }
/// 销售额(元)
public decimal SalesAmount { get; set; }
/// 占比(0~100)
public double Percentage { get; set; }
}
///
/// 店铺统计表格行(金额单位:元)
///
public class ShopStatisticsItem
{
/// 店铺名称
public string ShopName { get; set; }
/// 订单数
public int OrderCount { get; set; }
/// 销售额(元)
public decimal SalesAmount { get; set; }
/// 采购金额(元)
public decimal BuyerPaymentAmount { get; set; }
/// 利润(元)
public decimal Profit { get; set; }
/// 退款金额(元)
public decimal RefundAmount { get; set; }
/// 平台服务费总额(元)
public decimal PlatformFeeTotal { get; set; }
/// 达人佣金(元)
public decimal Commission { get; set; }
/// 客单价(元)
public decimal AvgOrderAmount { get; set; }
/// 平均利润率(利润 / 采购金额,百分数,如 12.34 表示 12.34%)
public decimal AverageProfitRate { get; set; }
}
///
/// 统计周期类型:天 / 周 / 月
///
public enum PeriodStatisticType
{
/// 按天统计
Day = 0,
/// 按周统计(周一起始)
Week = 1,
/// 按月统计
Month = 2
}
///
/// 天/周/月周期统计表格行(金额单位:元)。
/// 按所选店铺(或全部店铺)统计:天统计每天一行;周统计每周一行;月统计每月一行。
///
public class ShopPeriodStatisticsItem
{
/// 周期开始日期(用于排序)
public DateTime PeriodStart { get; set; }
/// 周期显示文本(天:yyyy-MM-dd;周:yyyy-MM-dd ~ yyyy-MM-dd;月:yyyy-MM)
public string Period { get; set; }
/// 订单数
public int OrderCount { get; set; }
/// 销售额(元)
public decimal SalesAmount { get; set; }
/// 买手号付款金额(采购金额,元)
public decimal BuyerPaymentAmount { get; set; }
/// 利润(元)
public decimal Profit { get; set; }
/// 退款金额(元)
public decimal RefundAmount { get; set; }
/// 平台服务费总额(元)
public decimal PlatformFeeTotal { get; set; }
/// 达人佣金(元)
public decimal Commission { get; set; }
/// 平均利润率(利润 / 采购金额,百分数,如 12.34 表示 12.34%)
public decimal AverageProfitRate { get; set; }
/// 客户数
public int CustomerCount { get; set; }
/// 客单价(元)
public decimal AvgOrderAmount { get; set; }
}
///
/// 店铺下拉选项
///
public class ShopOption
{
/// 店铺 app_key(A.app_key)
public string AppKey { get; set; }
public string ShopId { get; set; }
/// 店铺名称
public string ShopName { get; set; }
}
///
/// 时间范围下拉选项
///
public class TimeRangeOption
{
/// 显示文本(如:今天、昨天、本周、本月、最近10天、自定义)
public string Text { get; set; }
/// 值(today / yesterday / week / month / last10 / custom)
public string Value { get; set; }
}
class UnixDateTimeConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(DateTime);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// 读取数值(支持 long 和 int 类型)
if (reader.TokenType == JsonToken.Integer)
{
long milliseconds = (long)reader.Value;
// Unix 纪元:1970-01-01 UTC
//DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime epoch = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
if (milliseconds.ToString().Length == 13)
{
return epoch.AddMilliseconds(milliseconds);
}
return epoch.AddSeconds(milliseconds);
}
// 如果传入的是字符串(如 "1700000000000"),也支持
else if (reader.TokenType == JsonToken.String)
{
if (long.TryParse((string)reader.Value, out long milliseconds))
{
//DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime epoch = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
if (milliseconds.ToString().Length == 13)
{
return epoch.AddMilliseconds(milliseconds);
}
return epoch.AddSeconds(milliseconds);
}
}
throw new JsonSerializationException($"无法将 {reader.Value} 转换为 DateTime");
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
// 序列化时转回时间戳
//DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime epoch = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
long milliseconds = (long)((DateTime)value - epoch).TotalMilliseconds;
writer.WriteValue(milliseconds);
}
}
}