package model

import (
	"fmt"
	"time"

	"gorm.io/gorm"
)

func TableOfYearMonth(baseTableName string, today time.Time) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		tableName := fmt.Sprintf("%s_%s", baseTableName, today.Format("200601"))
		if db.Migrator().HasTable(tableName) {
			return db.Table(tableName)
		}
		return db.Table(baseTableName)
	}
}
