// 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 query

import (
	"context"

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

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

	"gorm.io/plugin/dbresolver"

	"github.com/bangumi/server/dal/dao"
)

func newTagList(db *gorm.DB, opts ...gen.DOOption) tagList {
	_tagList := tagList{}

	_tagList.tagListDo.UseDB(db, opts...)
	_tagList.tagListDo.UseModel(&dao.TagList{})

	tableName := _tagList.tagListDo.TableName()
	_tagList.ALL = field.NewAsterisk(tableName)
	_tagList.Tid = field.NewUint32(tableName, "tlt_tid")
	_tagList.UID = field.NewUint32(tableName, "tlt_uid")
	_tagList.Cat = field.NewUint8(tableName, "tlt_cat")
	_tagList.Type = field.NewUint8(tableName, "tlt_type")
	_tagList.Mid = field.NewUint32(tableName, "tlt_mid")
	_tagList.CreatedTime = field.NewUint32(tableName, "tlt_dateline")
	_tagList.Tag = tagListHasOneTag{
		db: db.Session(&gorm.Session{}),

		RelationField: field.NewRelation("Tag", "dao.TagIndex"),
	}

	_tagList.fillFieldMap()

	return _tagList
}

type tagList struct {
	tagListDo tagListDo

	ALL         field.Asterisk
	Tid         field.Uint32
	UID         field.Uint32
	Cat         field.Uint8
	Type        field.Uint8
	Mid         field.Uint32
	CreatedTime field.Uint32
	Tag         tagListHasOneTag

	fieldMap map[string]field.Expr
}

func (t tagList) Table(newTableName string) *tagList {
	t.tagListDo.UseTable(newTableName)
	return t.updateTableName(newTableName)
}

func (t tagList) As(alias string) *tagList {
	t.tagListDo.DO = *(t.tagListDo.As(alias).(*gen.DO))
	return t.updateTableName(alias)
}

func (t *tagList) updateTableName(table string) *tagList {
	t.ALL = field.NewAsterisk(table)
	t.Tid = field.NewUint32(table, "tlt_tid")
	t.UID = field.NewUint32(table, "tlt_uid")
	t.Cat = field.NewUint8(table, "tlt_cat")
	t.Type = field.NewUint8(table, "tlt_type")
	t.Mid = field.NewUint32(table, "tlt_mid")
	t.CreatedTime = field.NewUint32(table, "tlt_dateline")

	t.fillFieldMap()

	return t
}

func (t *tagList) WithContext(ctx context.Context) *tagListDo { return t.tagListDo.WithContext(ctx) }

func (t tagList) TableName() string { return t.tagListDo.TableName() }

func (t tagList) Alias() string { return t.tagListDo.Alias() }

func (t tagList) Columns(cols ...field.Expr) gen.Columns { return t.tagListDo.Columns(cols...) }

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

func (t *tagList) fillFieldMap() {
	t.fieldMap = make(map[string]field.Expr, 7)
	t.fieldMap["tlt_tid"] = t.Tid
	t.fieldMap["tlt_uid"] = t.UID
	t.fieldMap["tlt_cat"] = t.Cat
	t.fieldMap["tlt_type"] = t.Type
	t.fieldMap["tlt_mid"] = t.Mid
	t.fieldMap["tlt_dateline"] = t.CreatedTime

}

func (t tagList) clone(db *gorm.DB) tagList {
	t.tagListDo.ReplaceConnPool(db.Statement.ConnPool)
	t.Tag.db = db.Session(&gorm.Session{Initialized: true})
	t.Tag.db.Statement.ConnPool = db.Statement.ConnPool
	return t
}

func (t tagList) replaceDB(db *gorm.DB) tagList {
	t.tagListDo.ReplaceDB(db)
	t.Tag.db = db.Session(&gorm.Session{})
	return t
}

type tagListHasOneTag struct {
	db *gorm.DB

	field.RelationField
}

func (a tagListHasOneTag) Where(conds ...field.Expr) *tagListHasOneTag {
	if len(conds) == 0 {
		return &a
	}

	exprs := make([]clause.Expression, 0, len(conds))
	for _, cond := range conds {
		exprs = append(exprs, cond.BeCond().(clause.Expression))
	}
	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
	return &a
}

func (a tagListHasOneTag) WithContext(ctx context.Context) *tagListHasOneTag {
	a.db = a.db.WithContext(ctx)
	return &a
}

func (a tagListHasOneTag) Session(session *gorm.Session) *tagListHasOneTag {
	a.db = a.db.Session(session)
	return &a
}

func (a tagListHasOneTag) Model(m *dao.TagList) *tagListHasOneTagTx {
	return &tagListHasOneTagTx{a.db.Model(m).Association(a.Name())}
}

func (a tagListHasOneTag) Unscoped() *tagListHasOneTag {
	a.db = a.db.Unscoped()
	return &a
}

type tagListHasOneTagTx struct{ tx *gorm.Association }

func (a tagListHasOneTagTx) Find() (result *dao.TagIndex, err error) {
	return result, a.tx.Find(&result)
}

func (a tagListHasOneTagTx) Append(values ...*dao.TagIndex) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Append(targetValues...)
}

func (a tagListHasOneTagTx) Replace(values ...*dao.TagIndex) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Replace(targetValues...)
}

func (a tagListHasOneTagTx) Delete(values ...*dao.TagIndex) (err error) {
	targetValues := make([]interface{}, len(values))
	for i, v := range values {
		targetValues[i] = v
	}
	return a.tx.Delete(targetValues...)
}

func (a tagListHasOneTagTx) Clear() error {
	return a.tx.Clear()
}

func (a tagListHasOneTagTx) Count() int64 {
	return a.tx.Count()
}

func (a tagListHasOneTagTx) Unscoped() *tagListHasOneTagTx {
	a.tx = a.tx.Unscoped()
	return &a
}

type tagListDo struct{ gen.DO }

func (t tagListDo) Debug() *tagListDo {
	return t.withDO(t.DO.Debug())
}

func (t tagListDo) WithContext(ctx context.Context) *tagListDo {
	return t.withDO(t.DO.WithContext(ctx))
}

func (t tagListDo) ReadDB() *tagListDo {
	return t.Clauses(dbresolver.Read)
}

func (t tagListDo) WriteDB() *tagListDo {
	return t.Clauses(dbresolver.Write)
}

func (t tagListDo) Session(config *gorm.Session) *tagListDo {
	return t.withDO(t.DO.Session(config))
}

func (t tagListDo) Clauses(conds ...clause.Expression) *tagListDo {
	return t.withDO(t.DO.Clauses(conds...))
}

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

func (t tagListDo) Not(conds ...gen.Condition) *tagListDo {
	return t.withDO(t.DO.Not(conds...))
}

func (t tagListDo) Or(conds ...gen.Condition) *tagListDo {
	return t.withDO(t.DO.Or(conds...))
}

func (t tagListDo) Select(conds ...field.Expr) *tagListDo {
	return t.withDO(t.DO.Select(conds...))
}

func (t tagListDo) Where(conds ...gen.Condition) *tagListDo {
	return t.withDO(t.DO.Where(conds...))
}

func (t tagListDo) Order(conds ...field.Expr) *tagListDo {
	return t.withDO(t.DO.Order(conds...))
}

func (t tagListDo) Distinct(cols ...field.Expr) *tagListDo {
	return t.withDO(t.DO.Distinct(cols...))
}

func (t tagListDo) Omit(cols ...field.Expr) *tagListDo {
	return t.withDO(t.DO.Omit(cols...))
}

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

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

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

func (t tagListDo) Group(cols ...field.Expr) *tagListDo {
	return t.withDO(t.DO.Group(cols...))
}

func (t tagListDo) Having(conds ...gen.Condition) *tagListDo {
	return t.withDO(t.DO.Having(conds...))
}

func (t tagListDo) Limit(limit int) *tagListDo {
	return t.withDO(t.DO.Limit(limit))
}

func (t tagListDo) Offset(offset int) *tagListDo {
	return t.withDO(t.DO.Offset(offset))
}

func (t tagListDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *tagListDo {
	return t.withDO(t.DO.Scopes(funcs...))
}

func (t tagListDo) Unscoped() *tagListDo {
	return t.withDO(t.DO.Unscoped())
}

func (t tagListDo) Create(values ...*dao.TagList) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Create(values)
}

func (t tagListDo) CreateInBatches(values []*dao.TagList, batchSize int) error {
	return t.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 (t tagListDo) Save(values ...*dao.TagList) error {
	if len(values) == 0 {
		return nil
	}
	return t.DO.Save(values)
}

func (t tagListDo) First() (*dao.TagList, error) {
	if result, err := t.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*dao.TagList), nil
	}
}

func (t tagListDo) Take() (*dao.TagList, error) {
	if result, err := t.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*dao.TagList), nil
	}
}

func (t tagListDo) Last() (*dao.TagList, error) {
	if result, err := t.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*dao.TagList), nil
	}
}

func (t tagListDo) Find() ([]*dao.TagList, error) {
	result, err := t.DO.Find()
	return result.([]*dao.TagList), err
}

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

func (t tagListDo) Attrs(attrs ...field.AssignExpr) *tagListDo {
	return t.withDO(t.DO.Attrs(attrs...))
}

func (t tagListDo) Assign(attrs ...field.AssignExpr) *tagListDo {
	return t.withDO(t.DO.Assign(attrs...))
}

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

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

func (t tagListDo) FirstOrInit() (*dao.TagList, error) {
	if result, err := t.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*dao.TagList), nil
	}
}

func (t tagListDo) FirstOrCreate() (*dao.TagList, error) {
	if result, err := t.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*dao.TagList), nil
	}
}

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

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

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

func (t tagListDo) Scan(result interface{}) (err error) {
	return t.DO.Scan(result)
}

func (t tagListDo) Delete(models ...*dao.TagList) (result gen.ResultInfo, err error) {
	return t.DO.Delete(models)
}

func (t *tagListDo) withDO(do gen.Dao) *tagListDo {
	t.DO = *do.(*gen.DO)
	return t
}
