tables_common_scope.go 374 B

123456789101112131415161718
  1. package model
  2. import (
  3. "fmt"
  4. "time"
  5. "gorm.io/gorm"
  6. )
  7. func TableOfYearMonth(baseTableName string, today time.Time) func(db *gorm.DB) *gorm.DB {
  8. return func(db *gorm.DB) *gorm.DB {
  9. tableName := fmt.Sprintf("%s_%s", baseTableName, today.Format("200601"))
  10. if db.Migrator().HasTable(tableName) {
  11. return db.Table(tableName)
  12. }
  13. return db.Table(baseTableName)
  14. }
  15. }