topfans/backend/pkg/models/activity.go
2026-04-07 22:29:48 +08:00

174 lines
5.9 KiB
Go

package models
import (
"encoding/json"
"time"
)
// Activity 运营活动表
type Activity struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
ActivityType string `json:"activity_type" gorm:"size:50;not null"` // birthday/concert/bus
Title string `json:"title" gorm:"size:100;not null"`
Theme string `json:"theme" gorm:"size:100"` // 中文主题
Description string `json:"description" gorm:"type:text"`
StarID int64 `json:"star_id" gorm:"not null"`
StartTime int64 `json:"start_time" gorm:"not null"` // 毫秒时间戳
EndTime int64 `json:"end_time" gorm:"not null"` // 毫秒时间戳
TargetProgress int64 `json:"target_progress" gorm:"default:1000"`
CurrentProgress int64 `json:"current_progress" gorm:"default:0"`
Status string `json:"status" gorm:"size:20;default:pending"` // pending/active/completed/expired
StageConfigs json.RawMessage `json:"stage_configs" gorm:"type:jsonb"` // 阶段配置
CreatedAt int64 `json:"created_at" gorm:"not null"`
UpdatedAt int64 `json:"updated_at" gorm:"not null"`
Items []ActivityItem `json:"items,omitempty" gorm:"foreignKey:ActivityID"`
}
// TableName 表名
func (Activity) TableName() string {
return "activities"
}
// GetStage 获取当前阶段
func (a *Activity) GetStage() string {
if a.CurrentProgress >= a.TargetProgress {
return "completed"
}
progress := float64(a.CurrentProgress) / float64(a.TargetProgress) * 100
switch {
case progress <= 25:
return "early"
case progress <= 50:
return "mid"
case progress <= 75:
return "late"
default:
return "completed"
}
}
// GetCurrentStatus 获取当前活动状态
func (a *Activity) GetCurrentStatus() string {
now := time.Now().UnixMilli()
if now < a.StartTime {
return "pending"
}
if now > a.EndTime {
return "expired"
}
if a.CurrentProgress >= a.TargetProgress {
return "completed"
}
return "active"
}
// StageConfigJSON stage_configs 的 JSON 结构
type StageConfigJSON struct {
CoverImage string `json:"cover_image"`
BannerImage string `json:"banner_image"`
Stages []StageInfo `json:"stages"`
}
// StageInfo 阶段信息
type StageInfo struct {
Threshold int `json:"threshold"`
Background string `json:"background"`
Title string `json:"title"`
}
// GetStageImages 获取当前阶段的图片信息
func (a *Activity) GetStageImages() (coverImage, bannerImage, stageBackground, stageTitle string) {
if len(a.StageConfigs) == 0 {
return
}
var config StageConfigJSON
if err := json.Unmarshal(a.StageConfigs, &config); err != nil {
return
}
coverImage = config.CoverImage
bannerImage = config.BannerImage
// 根据当前进度找到对应的阶段
currentProgress := a.CurrentProgress
for _, stage := range config.Stages {
if currentProgress >= int64(stage.Threshold) {
stageBackground = stage.Background
stageTitle = stage.Title
}
}
// 如果没有匹配到阶段,使用最后一个阶段的背景
if stageBackground == "" && len(config.Stages) > 0 {
lastStage := config.Stages[len(config.Stages)-1]
stageBackground = lastStage.Background
stageTitle = lastStage.Title
}
return
}
// ActivityItem 活动道具表
type ActivityItem struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
ActivityID int64 `json:"activity_id" gorm:"not null"`
ItemType string `json:"item_type" gorm:"size:50;not null"` // firework/megaphone/love
ItemName string `json:"item_name" gorm:"size:50;not null"`
IconURL string `json:"icon_url" gorm:"size:500"`
CrystalCost int `json:"crystal_cost" gorm:"not null"`
ContributionPoints int `json:"contribution_points" gorm:"not null"`
SortOrder int `json:"sort_order" gorm:"default:0"`
IsActive bool `json:"is_active" gorm:"default:true"`
CreatedAt int64 `json:"created_at" gorm:"not null"`
UpdatedAt int64 `json:"updated_at" gorm:"not null"`
}
// TableName 表名
func (ActivityItem) TableName() string {
return "activity_items"
}
// ActivityContribution 用户活动贡献记录表
type ActivityContribution struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
ActivityID int64 `json:"activity_id" gorm:"not null"`
UserID int64 `json:"user_id" gorm:"not null"`
StarID int64 `json:"star_id" gorm:"not null"`
ItemID int64 `json:"item_id" gorm:"not null"`
ItemType string `json:"item_type" gorm:"size:50;not null"`
Quantity int `json:"quantity" gorm:"default:1"`
CrystalSpent int64 `json:"crystal_spent" gorm:"not null"`
ContributionPoints int64 `json:"contribution_points" gorm:"not null"`
CreatedAt int64 `json:"created_at" gorm:"not null"`
}
// TableName 表名
func (ActivityContribution) TableName() string {
return "activity_contributions"
}
// ActivityUserStats 用户活动贡献汇总表
type ActivityUserStats struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement"`
ActivityID int64 `json:"activity_id" gorm:"not null;uniqueIndex:uk_activity_user_star"`
UserID int64 `json:"user_id" gorm:"not null;uniqueIndex:uk_activity_user_star"`
StarID int64 `json:"star_id" gorm:"not null;uniqueIndex:uk_activity_user_star"`
TotalContribution int64 `json:"total_contribution" gorm:"default:0"`
TotalCrystalSpent int64 `json:"total_crystal_spent" gorm:"default:0"`
TotalItems int `json:"total_items" gorm:"default:0"`
LastContributeAt int64 `json:"last_contribute_at" gorm:"not null"`
CreatedAt int64 `json:"created_at" gorm:"not null"`
UpdatedAt int64 `json:"updated_at" gorm:"not null"`
}
// TableName 表名
func (ActivityUserStats) TableName() string {
return "activity_user_stats"
}