// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.
// Code generated by gorm.io/gen. DO NOT EDIT.

package dao

import (
	"context"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"bindbox-game/internal/repository/mysql/model"
)

func newLogRequest(db *gorm.DB, opts ...gen.DOOption) logRequest {
	_logRequest := logRequest{}

	_logRequest.logRequestDo.UseDB(db, opts...)
	_logRequest.logRequestDo.UseModel(&model.LogRequest{})

	tableName := _logRequest.logRequestDo.TableName()
	_logRequest.ALL = field.NewAsterisk(tableName)
	_logRequest.ID = field.NewInt32(tableName, "id")
	_logRequest.Tid = field.NewString(tableName, "tid")
	_logRequest.Username = field.NewString(tableName, "username")
	_logRequest.Host = field.NewString(tableName, "host")
	_logRequest.Path = field.NewString(tableName, "path")
	_logRequest.Method = field.NewString(tableName, "method")
	_logRequest.HTTPCode = field.NewInt32(tableName, "http_code")
	_logRequest.BusinessCode = field.NewInt32(tableName, "business_code")
	_logRequest.CostMilliseconds = field.NewFloat64(tableName, "cost_milliseconds")
	_logRequest.IsSuccess = field.NewInt32(tableName, "is_success")
	_logRequest.Content = field.NewString(tableName, "content")
	_logRequest.CreatedAt = field.NewTime(tableName, "created_at")

	_logRequest.fillFieldMap()

	return _logRequest
}

// logRequest 请求日志表
type logRequest struct {
	logRequestDo

	ALL              field.Asterisk
	ID               field.Int32   // 主键
	Tid              field.String  // 请求链路 ID
	Username         field.String  // 登录名
	Host             field.String  // 请求 HOST
	Path             field.String  // 请求 Path
	Method           field.String  // 请求 Method
	HTTPCode         field.Int32   // HTTP 状态码
	BusinessCode     field.Int32   // 业务码
	CostMilliseconds field.Float64 // 耗时(毫秒)
	IsSuccess        field.Int32   // 是否成功(1=是 -1=否)
	Content          field.String  // 内容
	CreatedAt        field.Time    // 创建时间

	fieldMap map[string]field.Expr
}

func (l logRequest) Table(newTableName string) *logRequest {
	l.logRequestDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

func (l logRequest) As(alias string) *logRequest {
	l.logRequestDo.DO = *(l.logRequestDo.As(alias).(*gen.DO))
	return l.updateTableName(alias)
}

func (l *logRequest) updateTableName(table string) *logRequest {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewInt32(table, "id")
	l.Tid = field.NewString(table, "tid")
	l.Username = field.NewString(table, "username")
	l.Host = field.NewString(table, "host")
	l.Path = field.NewString(table, "path")
	l.Method = field.NewString(table, "method")
	l.HTTPCode = field.NewInt32(table, "http_code")
	l.BusinessCode = field.NewInt32(table, "business_code")
	l.CostMilliseconds = field.NewFloat64(table, "cost_milliseconds")
	l.IsSuccess = field.NewInt32(table, "is_success")
	l.Content = field.NewString(table, "content")
	l.CreatedAt = field.NewTime(table, "created_at")

	l.fillFieldMap()

	return l
}

func (l *logRequest) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
	_f, ok := l.fieldMap[fieldName]
	if !ok || _f == nil {
		return nil, false
	}
	_oe, ok := _f.(field.OrderExpr)
	return _oe, ok
}

func (l *logRequest) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 12)
	l.fieldMap["id"] = l.ID
	l.fieldMap["tid"] = l.Tid
	l.fieldMap["username"] = l.Username
	l.fieldMap["host"] = l.Host
	l.fieldMap["path"] = l.Path
	l.fieldMap["method"] = l.Method
	l.fieldMap["http_code"] = l.HTTPCode
	l.fieldMap["business_code"] = l.BusinessCode
	l.fieldMap["cost_milliseconds"] = l.CostMilliseconds
	l.fieldMap["is_success"] = l.IsSuccess
	l.fieldMap["content"] = l.Content
	l.fieldMap["created_at"] = l.CreatedAt
}

func (l logRequest) clone(db *gorm.DB) logRequest {
	l.logRequestDo.ReplaceConnPool(db.Statement.ConnPool)
	return l
}

func (l logRequest) replaceDB(db *gorm.DB) logRequest {
	l.logRequestDo.ReplaceDB(db)
	return l
}

type logRequestDo struct{ gen.DO }

func (l logRequestDo) Debug() *logRequestDo {
	return l.withDO(l.DO.Debug())
}

func (l logRequestDo) WithContext(ctx context.Context) *logRequestDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l logRequestDo) ReadDB() *logRequestDo {
	return l.Clauses(dbresolver.Read)
}

func (l logRequestDo) WriteDB() *logRequestDo {
	return l.Clauses(dbresolver.Write)
}

func (l logRequestDo) Session(config *gorm.Session) *logRequestDo {
	return l.withDO(l.DO.Session(config))
}

func (l logRequestDo) Clauses(conds ...clause.Expression) *logRequestDo {
	return l.withDO(l.DO.Clauses(conds...))
}

func (l logRequestDo) Returning(value interface{}, columns ...string) *logRequestDo {
	return l.withDO(l.DO.Returning(value, columns...))
}

func (l logRequestDo) Not(conds ...gen.Condition) *logRequestDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l logRequestDo) Or(conds ...gen.Condition) *logRequestDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l logRequestDo) Select(conds ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l logRequestDo) Where(conds ...gen.Condition) *logRequestDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l logRequestDo) Order(conds ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l logRequestDo) Distinct(cols ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l logRequestDo) Omit(cols ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.Omit(cols...))
}

func (l logRequestDo) Join(table schema.Tabler, on ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.Join(table, on...))
}

func (l logRequestDo) LeftJoin(table schema.Tabler, on ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.LeftJoin(table, on...))
}

func (l logRequestDo) RightJoin(table schema.Tabler, on ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.RightJoin(table, on...))
}

func (l logRequestDo) Group(cols ...field.Expr) *logRequestDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l logRequestDo) Having(conds ...gen.Condition) *logRequestDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l logRequestDo) Limit(limit int) *logRequestDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l logRequestDo) Offset(offset int) *logRequestDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l logRequestDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *logRequestDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l logRequestDo) Unscoped() *logRequestDo {
	return l.withDO(l.DO.Unscoped())
}

func (l logRequestDo) Create(values ...*model.LogRequest) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

func (l logRequestDo) CreateInBatches(values []*model.LogRequest, batchSize int) error {
	return l.DO.CreateInBatches(values, batchSize)
}

// Save : !!! underlying implementation is different with GORM
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
func (l logRequestDo) Save(values ...*model.LogRequest) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Save(values)
}

func (l logRequestDo) First() (*model.LogRequest, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.LogRequest), nil
	}
}

func (l logRequestDo) Take() (*model.LogRequest, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.LogRequest), nil
	}
}

func (l logRequestDo) Last() (*model.LogRequest, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.LogRequest), nil
	}
}

func (l logRequestDo) Find() ([]*model.LogRequest, error) {
	result, err := l.DO.Find()
	return result.([]*model.LogRequest), err
}

func (l logRequestDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.LogRequest, err error) {
	buf := make([]*model.LogRequest, 0, batchSize)
	err = l.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
		defer func() { results = append(results, buf...) }()
		return fc(tx, batch)
	})
	return results, err
}

func (l logRequestDo) FindInBatches(result *[]*model.LogRequest, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return l.DO.FindInBatches(result, batchSize, fc)
}

func (l logRequestDo) Attrs(attrs ...field.AssignExpr) *logRequestDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l logRequestDo) Assign(attrs ...field.AssignExpr) *logRequestDo {
	return l.withDO(l.DO.Assign(attrs...))
}

func (l logRequestDo) Joins(fields ...field.RelationField) *logRequestDo {
	for _, _f := range fields {
		l = *l.withDO(l.DO.Joins(_f))
	}
	return &l
}

func (l logRequestDo) Preload(fields ...field.RelationField) *logRequestDo {
	for _, _f := range fields {
		l = *l.withDO(l.DO.Preload(_f))
	}
	return &l
}

func (l logRequestDo) FirstOrInit() (*model.LogRequest, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.LogRequest), nil
	}
}

func (l logRequestDo) FirstOrCreate() (*model.LogRequest, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.LogRequest), nil
	}
}

func (l logRequestDo) FindByPage(offset int, limit int) (result []*model.LogRequest, count int64, err error) {
	result, err = l.Offset(offset).Limit(limit).Find()
	if err != nil {
		return
	}

	if size := len(result); 0 < limit && 0 < size && size < limit {
		count = int64(size + offset)
		return
	}

	count, err = l.Offset(-1).Limit(-1).Count()
	return
}

func (l logRequestDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
	count, err = l.Count()
	if err != nil {
		return
	}

	err = l.Offset(offset).Limit(limit).Scan(result)
	return
}

func (l logRequestDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l logRequestDo) Delete(models ...*model.LogRequest) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

func (l *logRequestDo) withDO(do gen.Dao) *logRequestDo {
	l.DO = *do.(*gen.DO)
	return l
}
