// 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"
	"strings"

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

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

	"gorm.io/plugin/dbresolver"

	"github.com/flowline-io/flowbot/internal/store/model"
)

func newOAuth(db *gorm.DB, opts ...gen.DOOption) oAuth {
	_oAuth := oAuth{}

	_oAuth.oAuthDo.UseDB(db, opts...)
	_oAuth.oAuthDo.UseModel(&model.OAuth{})

	tableName := _oAuth.oAuthDo.TableName()
	_oAuth.ALL = field.NewAsterisk(tableName)
	_oAuth.ID = field.NewInt64(tableName, "id")
	_oAuth.UID = field.NewString(tableName, "uid")
	_oAuth.Topic = field.NewString(tableName, "topic")
	_oAuth.Name = field.NewString(tableName, "name")
	_oAuth.Type = field.NewString(tableName, "type")
	_oAuth.Token = field.NewString(tableName, "token")
	_oAuth.Extra = field.NewField(tableName, "extra")
	_oAuth.CreatedAt = field.NewTime(tableName, "created_at")
	_oAuth.UpdatedAt = field.NewTime(tableName, "updated_at")

	_oAuth.fillFieldMap()

	return _oAuth
}

type oAuth struct {
	oAuthDo

	ALL       field.Asterisk
	ID        field.Int64
	UID       field.String
	Topic     field.String
	Name      field.String
	Type      field.String
	Token     field.String
	Extra     field.Field
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (o oAuth) Table(newTableName string) *oAuth {
	o.oAuthDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

func (o oAuth) As(alias string) *oAuth {
	o.oAuthDo.DO = *(o.oAuthDo.As(alias).(*gen.DO))
	return o.updateTableName(alias)
}

func (o *oAuth) updateTableName(table string) *oAuth {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.UID = field.NewString(table, "uid")
	o.Topic = field.NewString(table, "topic")
	o.Name = field.NewString(table, "name")
	o.Type = field.NewString(table, "type")
	o.Token = field.NewString(table, "token")
	o.Extra = field.NewField(table, "extra")
	o.CreatedAt = field.NewTime(table, "created_at")
	o.UpdatedAt = field.NewTime(table, "updated_at")

	o.fillFieldMap()

	return o
}

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

func (o *oAuth) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 9)
	o.fieldMap["id"] = o.ID
	o.fieldMap["uid"] = o.UID
	o.fieldMap["topic"] = o.Topic
	o.fieldMap["name"] = o.Name
	o.fieldMap["type"] = o.Type
	o.fieldMap["token"] = o.Token
	o.fieldMap["extra"] = o.Extra
	o.fieldMap["created_at"] = o.CreatedAt
	o.fieldMap["updated_at"] = o.UpdatedAt
}

func (o oAuth) clone(db *gorm.DB) oAuth {
	o.oAuthDo.ReplaceConnPool(db.Statement.ConnPool)
	return o
}

func (o oAuth) replaceDB(db *gorm.DB) oAuth {
	o.oAuthDo.ReplaceDB(db)
	return o
}

type oAuthDo struct{ gen.DO }

// GetByID
//
// SELECT * FROM @@table WHERE id=@id
func (o oAuthDo) GetByID(id int) (result model.OAuth, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("SELECT * FROM oauth WHERE id=? ")

	var executeSQL *gorm.DB
	executeSQL = o.UnderlyingDB().Raw(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// TakeByUidAndTopic query data by uid and topic and return one
//
// where("uid=@uid AND topic=@topic")
func (o oAuthDo) TakeByUidAndTopic(uid string, topic string) (result model.OAuth, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, uid)
	params = append(params, topic)
	generateSQL.WriteString("uid=? AND topic=? ")

	var executeSQL *gorm.DB
	executeSQL = o.UnderlyingDB().Where(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// FindByUidAndTopic query data by uid and topic and return array
//
// where("uid=@uid AND topic=@topic")
func (o oAuthDo) FindByUidAndTopic(uid string, topic string) (result []*model.OAuth, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, uid)
	params = append(params, topic)
	generateSQL.WriteString("uid=? AND topic=? ")

	var executeSQL *gorm.DB
	executeSQL = o.UnderlyingDB().Where(generateSQL.String(), params...).Find(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (o oAuthDo) Debug() *oAuthDo {
	return o.withDO(o.DO.Debug())
}

func (o oAuthDo) WithContext(ctx context.Context) *oAuthDo {
	return o.withDO(o.DO.WithContext(ctx))
}

func (o oAuthDo) ReadDB() *oAuthDo {
	return o.Clauses(dbresolver.Read)
}

func (o oAuthDo) WriteDB() *oAuthDo {
	return o.Clauses(dbresolver.Write)
}

func (o oAuthDo) Session(config *gorm.Session) *oAuthDo {
	return o.withDO(o.DO.Session(config))
}

func (o oAuthDo) Clauses(conds ...clause.Expression) *oAuthDo {
	return o.withDO(o.DO.Clauses(conds...))
}

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

func (o oAuthDo) Not(conds ...gen.Condition) *oAuthDo {
	return o.withDO(o.DO.Not(conds...))
}

func (o oAuthDo) Or(conds ...gen.Condition) *oAuthDo {
	return o.withDO(o.DO.Or(conds...))
}

func (o oAuthDo) Select(conds ...field.Expr) *oAuthDo {
	return o.withDO(o.DO.Select(conds...))
}

func (o oAuthDo) Where(conds ...gen.Condition) *oAuthDo {
	return o.withDO(o.DO.Where(conds...))
}

func (o oAuthDo) Order(conds ...field.Expr) *oAuthDo {
	return o.withDO(o.DO.Order(conds...))
}

func (o oAuthDo) Distinct(cols ...field.Expr) *oAuthDo {
	return o.withDO(o.DO.Distinct(cols...))
}

func (o oAuthDo) Omit(cols ...field.Expr) *oAuthDo {
	return o.withDO(o.DO.Omit(cols...))
}

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

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

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

func (o oAuthDo) Group(cols ...field.Expr) *oAuthDo {
	return o.withDO(o.DO.Group(cols...))
}

func (o oAuthDo) Having(conds ...gen.Condition) *oAuthDo {
	return o.withDO(o.DO.Having(conds...))
}

func (o oAuthDo) Limit(limit int) *oAuthDo {
	return o.withDO(o.DO.Limit(limit))
}

func (o oAuthDo) Offset(offset int) *oAuthDo {
	return o.withDO(o.DO.Offset(offset))
}

func (o oAuthDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *oAuthDo {
	return o.withDO(o.DO.Scopes(funcs...))
}

func (o oAuthDo) Unscoped() *oAuthDo {
	return o.withDO(o.DO.Unscoped())
}

func (o oAuthDo) Create(values ...*model.OAuth) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Create(values)
}

func (o oAuthDo) CreateInBatches(values []*model.OAuth, batchSize int) error {
	return o.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 (o oAuthDo) Save(values ...*model.OAuth) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Save(values)
}

func (o oAuthDo) First() (*model.OAuth, error) {
	if result, err := o.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.OAuth), nil
	}
}

func (o oAuthDo) Take() (*model.OAuth, error) {
	if result, err := o.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.OAuth), nil
	}
}

func (o oAuthDo) Last() (*model.OAuth, error) {
	if result, err := o.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.OAuth), nil
	}
}

func (o oAuthDo) Find() ([]*model.OAuth, error) {
	result, err := o.DO.Find()
	return result.([]*model.OAuth), err
}

func (o oAuthDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.OAuth, err error) {
	buf := make([]*model.OAuth, 0, batchSize)
	err = o.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 (o oAuthDo) FindInBatches(result *[]*model.OAuth, batchSize int, fc func(tx gen.Dao, batch int) error) error {
	return o.DO.FindInBatches(result, batchSize, fc)
}

func (o oAuthDo) Attrs(attrs ...field.AssignExpr) *oAuthDo {
	return o.withDO(o.DO.Attrs(attrs...))
}

func (o oAuthDo) Assign(attrs ...field.AssignExpr) *oAuthDo {
	return o.withDO(o.DO.Assign(attrs...))
}

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

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

func (o oAuthDo) FirstOrInit() (*model.OAuth, error) {
	if result, err := o.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.OAuth), nil
	}
}

func (o oAuthDo) FirstOrCreate() (*model.OAuth, error) {
	if result, err := o.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.OAuth), nil
	}
}

func (o oAuthDo) FindByPage(offset int, limit int) (result []*model.OAuth, count int64, err error) {
	result, err = o.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 = o.Offset(-1).Limit(-1).Count()
	return
}

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

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

func (o oAuthDo) Scan(result interface{}) (err error) {
	return o.DO.Scan(result)
}

func (o oAuthDo) Delete(models ...*model.OAuth) (result gen.ResultInfo, err error) {
	return o.DO.Delete(models)
}

func (o *oAuthDo) withDO(do gen.Dao) *oAuthDo {
	o.DO = *do.(*gen.DO)
	return o
}
