package model

import (
	"time"

	"gorm.io/gorm"
)

// ShopSeriesCommissionStats 梯度佣金统计缓存模型
// 缓存梯度返佣的统计数据(销量/销售额)，避免实时计算性能问题
// 通过 Redis + 异步任务更新，支持乐观锁防止并发冲突
type ShopSeriesCommissionStats struct {
	gorm.Model
	AllocationID     uint      `gorm:"column:allocation_id;index;not null;comment:关联的分配ID" json:"allocation_id"`
	PeriodType       string    `gorm:"column:period_type;type:varchar(20);not null;comment:周期类型 monthly-月度 quarterly-季度 yearly-年度" json:"period_type"`
	PeriodStart      time.Time `gorm:"column:period_start;type:timestamptz;not null;comment:周期开始时间" json:"period_start"`
	PeriodEnd        time.Time `gorm:"column:period_end;type:timestamptz;not null;comment:周期结束时间" json:"period_end"`
	TotalSalesCount  int64     `gorm:"column:total_sales_count;type:bigint;not null;default:0;comment:总销售数量" json:"total_sales_count"`
	TotalSalesAmount int64     `gorm:"column:total_sales_amount;type:bigint;not null;default:0;comment:总销售金额(分)" json:"total_sales_amount"`
	CurrentTierID    *uint     `gorm:"column:current_tier_id;type:bigint;comment:当前匹配的梯度ID" json:"current_tier_id"`
	LastUpdatedAt    time.Time `gorm:"column:last_updated_at;type:timestamptz;not null;comment:最后更新时间" json:"last_updated_at"`
	Version          int       `gorm:"column:version;type:int;not null;default:0;comment:版本号(乐观锁)" json:"version"`
	Status           string    `gorm:"column:status;type:varchar(20);not null;default:active;comment:状态 active-活跃 completed-已完成 cancelled-已取消" json:"status"`
}

// TableName 指定表名
func (ShopSeriesCommissionStats) TableName() string {
	return "tb_shop_series_commission_stats"
}

// 统计状态常量
const (
	// StatsStatusActive 活跃
	StatsStatusActive = "active"
	// StatsStatusCompleted 已完成
	StatsStatusCompleted = "completed"
	// StatsStatusCancelled 已取消
	StatsStatusCancelled = "cancelled"
)
