// 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"
	"database/sql"

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

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

	"gorm.io/plugin/dbresolver"

	"kra/internal/data/model"
)

func newSysExportTemplate(db *gorm.DB, opts ...gen.DOOption) sysExportTemplate {
	_sysExportTemplate := sysExportTemplate{}

	_sysExportTemplate.sysExportTemplateDo.UseDB(db, opts...)
	_sysExportTemplate.sysExportTemplateDo.UseModel(&model.SysExportTemplate{})

	tableName := _sysExportTemplate.sysExportTemplateDo.TableName()
	_sysExportTemplate.ALL = field.NewAsterisk(tableName)
	_sysExportTemplate.ID = field.NewInt64(tableName, "id")
	_sysExportTemplate.CreatedAt = field.NewTime(tableName, "created_at")
	_sysExportTemplate.UpdatedAt = field.NewTime(tableName, "updated_at")
	_sysExportTemplate.DeletedAt = field.NewField(tableName, "deleted_at")
	_sysExportTemplate.DbName = field.NewString(tableName, "db_name")
	_sysExportTemplate.Name = field.NewString(tableName, "name")
	_sysExportTemplate.TblName = field.NewString(tableName, "table_name")
	_sysExportTemplate.TemplateID = field.NewString(tableName, "template_id")
	_sysExportTemplate.TemplateInfo = field.NewString(tableName, "template_info")
	_sysExportTemplate.Limit = field.NewInt64(tableName, "limit")
	_sysExportTemplate.Order = field.NewString(tableName, "order")

	_sysExportTemplate.fillFieldMap()

	return _sysExportTemplate
}

type sysExportTemplate struct {
	sysExportTemplateDo sysExportTemplateDo

	ALL          field.Asterisk
	ID           field.Int64
	CreatedAt    field.Time
	UpdatedAt    field.Time
	DeletedAt    field.Field
	DbName       field.String // 数据库名称
	Name         field.String // 模板名称
	TblName      field.String // 表名称
	TemplateID   field.String // 模板标识
	TemplateInfo field.String
	Limit        field.Int64  // 导出限制
	Order        field.String // 排序

	fieldMap map[string]field.Expr
}

func (s sysExportTemplate) Table(newTableName string) *sysExportTemplate {
	s.sysExportTemplateDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s sysExportTemplate) As(alias string) *sysExportTemplate {
	s.sysExportTemplateDo.DO = *(s.sysExportTemplateDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *sysExportTemplate) updateTableName(table string) *sysExportTemplate {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.DeletedAt = field.NewField(table, "deleted_at")
	s.DbName = field.NewString(table, "db_name")
	s.Name = field.NewString(table, "name")
	s.TblName = field.NewString(table, "table_name")
	s.TemplateID = field.NewString(table, "template_id")
	s.TemplateInfo = field.NewString(table, "template_info")
	s.Limit = field.NewInt64(table, "limit")
	s.Order = field.NewString(table, "order")

	s.fillFieldMap()

	return s
}

func (s *sysExportTemplate) WithContext(ctx context.Context) ISysExportTemplateDo {
	return s.sysExportTemplateDo.WithContext(ctx)
}

func (s sysExportTemplate) TableName() string { return s.sysExportTemplateDo.TableName() }

func (s sysExportTemplate) Alias() string { return s.sysExportTemplateDo.Alias() }

func (s sysExportTemplate) Columns(cols ...field.Expr) gen.Columns {
	return s.sysExportTemplateDo.Columns(cols...)
}

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

func (s *sysExportTemplate) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 11)
	s.fieldMap["id"] = s.ID
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["deleted_at"] = s.DeletedAt
	s.fieldMap["db_name"] = s.DbName
	s.fieldMap["name"] = s.Name
	s.fieldMap["table_name"] = s.TblName
	s.fieldMap["template_id"] = s.TemplateID
	s.fieldMap["template_info"] = s.TemplateInfo
	s.fieldMap["limit"] = s.Limit
	s.fieldMap["order"] = s.Order
}

func (s sysExportTemplate) clone(db *gorm.DB) sysExportTemplate {
	s.sysExportTemplateDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s sysExportTemplate) replaceDB(db *gorm.DB) sysExportTemplate {
	s.sysExportTemplateDo.ReplaceDB(db)
	return s
}

type sysExportTemplateDo struct{ gen.DO }

type ISysExportTemplateDo interface {
	gen.SubQuery
	Debug() ISysExportTemplateDo
	WithContext(ctx context.Context) ISysExportTemplateDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() ISysExportTemplateDo
	WriteDB() ISysExportTemplateDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) ISysExportTemplateDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) ISysExportTemplateDo
	Not(conds ...gen.Condition) ISysExportTemplateDo
	Or(conds ...gen.Condition) ISysExportTemplateDo
	Select(conds ...field.Expr) ISysExportTemplateDo
	Where(conds ...gen.Condition) ISysExportTemplateDo
	Order(conds ...field.Expr) ISysExportTemplateDo
	Distinct(cols ...field.Expr) ISysExportTemplateDo
	Omit(cols ...field.Expr) ISysExportTemplateDo
	Join(table schema.Tabler, on ...field.Expr) ISysExportTemplateDo
	LeftJoin(table schema.Tabler, on ...field.Expr) ISysExportTemplateDo
	RightJoin(table schema.Tabler, on ...field.Expr) ISysExportTemplateDo
	Group(cols ...field.Expr) ISysExportTemplateDo
	Having(conds ...gen.Condition) ISysExportTemplateDo
	Limit(limit int) ISysExportTemplateDo
	Offset(offset int) ISysExportTemplateDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) ISysExportTemplateDo
	Unscoped() ISysExportTemplateDo
	Create(values ...*model.SysExportTemplate) error
	CreateInBatches(values []*model.SysExportTemplate, batchSize int) error
	Save(values ...*model.SysExportTemplate) error
	First() (*model.SysExportTemplate, error)
	Take() (*model.SysExportTemplate, error)
	Last() (*model.SysExportTemplate, error)
	Find() ([]*model.SysExportTemplate, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.SysExportTemplate, err error)
	FindInBatches(result *[]*model.SysExportTemplate, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.SysExportTemplate) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) ISysExportTemplateDo
	Assign(attrs ...field.AssignExpr) ISysExportTemplateDo
	Joins(fields ...field.RelationField) ISysExportTemplateDo
	Preload(fields ...field.RelationField) ISysExportTemplateDo
	FirstOrInit() (*model.SysExportTemplate, error)
	FirstOrCreate() (*model.SysExportTemplate, error)
	FindByPage(offset int, limit int) (result []*model.SysExportTemplate, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Rows() (*sql.Rows, error)
	Row() *sql.Row
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) ISysExportTemplateDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (s sysExportTemplateDo) Debug() ISysExportTemplateDo {
	return s.withDO(s.DO.Debug())
}

func (s sysExportTemplateDo) WithContext(ctx context.Context) ISysExportTemplateDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sysExportTemplateDo) ReadDB() ISysExportTemplateDo {
	return s.Clauses(dbresolver.Read)
}

func (s sysExportTemplateDo) WriteDB() ISysExportTemplateDo {
	return s.Clauses(dbresolver.Write)
}

func (s sysExportTemplateDo) Session(config *gorm.Session) ISysExportTemplateDo {
	return s.withDO(s.DO.Session(config))
}

func (s sysExportTemplateDo) Clauses(conds ...clause.Expression) ISysExportTemplateDo {
	return s.withDO(s.DO.Clauses(conds...))
}

func (s sysExportTemplateDo) Returning(value interface{}, columns ...string) ISysExportTemplateDo {
	return s.withDO(s.DO.Returning(value, columns...))
}

func (s sysExportTemplateDo) Not(conds ...gen.Condition) ISysExportTemplateDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sysExportTemplateDo) Or(conds ...gen.Condition) ISysExportTemplateDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sysExportTemplateDo) Select(conds ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sysExportTemplateDo) Where(conds ...gen.Condition) ISysExportTemplateDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sysExportTemplateDo) Order(conds ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sysExportTemplateDo) Distinct(cols ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sysExportTemplateDo) Omit(cols ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.Omit(cols...))
}

func (s sysExportTemplateDo) Join(table schema.Tabler, on ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.Join(table, on...))
}

func (s sysExportTemplateDo) LeftJoin(table schema.Tabler, on ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.LeftJoin(table, on...))
}

func (s sysExportTemplateDo) RightJoin(table schema.Tabler, on ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.RightJoin(table, on...))
}

func (s sysExportTemplateDo) Group(cols ...field.Expr) ISysExportTemplateDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sysExportTemplateDo) Having(conds ...gen.Condition) ISysExportTemplateDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sysExportTemplateDo) Limit(limit int) ISysExportTemplateDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sysExportTemplateDo) Offset(offset int) ISysExportTemplateDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sysExportTemplateDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISysExportTemplateDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sysExportTemplateDo) Unscoped() ISysExportTemplateDo {
	return s.withDO(s.DO.Unscoped())
}

func (s sysExportTemplateDo) Create(values ...*model.SysExportTemplate) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

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

func (s sysExportTemplateDo) First() (*model.SysExportTemplate, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.SysExportTemplate), nil
	}
}

func (s sysExportTemplateDo) Take() (*model.SysExportTemplate, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.SysExportTemplate), nil
	}
}

func (s sysExportTemplateDo) Last() (*model.SysExportTemplate, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.SysExportTemplate), nil
	}
}

func (s sysExportTemplateDo) Find() ([]*model.SysExportTemplate, error) {
	result, err := s.DO.Find()
	return result.([]*model.SysExportTemplate), err
}

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

func (s sysExportTemplateDo) Attrs(attrs ...field.AssignExpr) ISysExportTemplateDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sysExportTemplateDo) Assign(attrs ...field.AssignExpr) ISysExportTemplateDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

func (s sysExportTemplateDo) FirstOrInit() (*model.SysExportTemplate, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.SysExportTemplate), nil
	}
}

func (s sysExportTemplateDo) FirstOrCreate() (*model.SysExportTemplate, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.SysExportTemplate), nil
	}
}

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

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

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

func (s sysExportTemplateDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s sysExportTemplateDo) Delete(models ...*model.SysExportTemplate) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *sysExportTemplateDo) withDO(do gen.Dao) *sysExportTemplateDo {
	s.DO = *do.(*gen.DO)
	return s
}
