// ==========================================================================
// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
// ==========================================================================

package internal

import (
	"context"

	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
)

// BookRatingsDao is the data access object for the table book_ratings.
type BookRatingsDao struct {
	table    string             // table is the underlying table name of the DAO.
	group    string             // group is the database configuration group name of the current DAO.
	columns  BookRatingsColumns // columns contains all the column names of Table for convenient usage.
	handlers []gdb.ModelHandler // handlers for customized model modification.
}

// BookRatingsColumns defines and stores column names for the table book_ratings.
type BookRatingsColumns struct {
	Id        string // 评分ID
	UserId    string // 用户ID
	BookId    string // 小说ID
	Score     string // 评分（0~10）
	Comment   string // 用户评论
	CreatedAt string // 创建时间
}

// bookRatingsColumns holds the columns for the table book_ratings.
var bookRatingsColumns = BookRatingsColumns{
	Id:        "id",
	UserId:    "user_id",
	BookId:    "book_id",
	Score:     "score",
	Comment:   "comment",
	CreatedAt: "created_at",
}

// NewBookRatingsDao creates and returns a new DAO object for table data access.
func NewBookRatingsDao(handlers ...gdb.ModelHandler) *BookRatingsDao {
	return &BookRatingsDao{
		group:    "default",
		table:    "book_ratings",
		columns:  bookRatingsColumns,
		handlers: handlers,
	}
}

// DB retrieves and returns the underlying raw database management object of the current DAO.
func (dao *BookRatingsDao) DB() gdb.DB {
	return g.DB(dao.group)
}

// Table returns the table name of the current DAO.
func (dao *BookRatingsDao) Table() string {
	return dao.table
}

// Columns returns all column names of the current DAO.
func (dao *BookRatingsDao) Columns() BookRatingsColumns {
	return dao.columns
}

// Group returns the database configuration group name of the current DAO.
func (dao *BookRatingsDao) Group() string {
	return dao.group
}

// Ctx creates and returns a Model for the current DAO. It automatically sets the context for the current operation.
func (dao *BookRatingsDao) Ctx(ctx context.Context) *gdb.Model {
	model := dao.DB().Model(dao.table)
	for _, handler := range dao.handlers {
		model = handler(model)
	}
	return model.Safe().Ctx(ctx)
}

// Transaction wraps the transaction logic using function f.
// It rolls back the transaction and returns the error if function f returns a non-nil error.
// It commits the transaction and returns nil if function f returns nil.
//
// Note: Do not commit or roll back the transaction in function f,
// as it is automatically handled by this function.
func (dao *BookRatingsDao) Transaction(ctx context.Context, f func(ctx context.Context, tx gdb.TX) error) (err error) {
	return dao.Ctx(ctx).Transaction(ctx, f)
}
