package model

import (
	"fmt"
	"gorm.io/gorm"
	"time"
)

// DynamicChapterLogsUserDetail 动态战斗日志表名（时间分表）
type DynamicChapterLogsUserDetail interface {
	TimeInfo() string
}

// TimeInfo 根据时间进行分表，格式年+月
func (clog *ChapterLogsUserDetail) TimeInfo() string {
	if clog.EventAt == 0 {
		return "chapter_logs_user_details"
	}
	return fmt.Sprintf("chapter_logs_user_details_%v", time.Unix(int64(clog.EventAt), 0).Format("200601"))
}

// ChapterLogsUserDetailTable gormDB的scopes， 根据clog来获取对应的表名
func ChapterLogsUserDetailTable(clog ChapterLogsUserDetail) func(tx *gorm.DB) *gorm.DB {
	return func(tx *gorm.DB) *gorm.DB {
		return tx.Table(clog.TimeInfo())
	}
}

func ChapterLogsUserDetailTableSetDate(n string) func(tx *gorm.DB) *gorm.DB {
	return func(tx *gorm.DB) *gorm.DB {
		if n == "" {
			return tx.Table("chapter_logs_user_details")
		}
		return tx.Table(fmt.Sprintf("chapter_logs_user_details_%v", n))
	}
}
