// 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 newSysParam(db *gorm.DB, opts ...gen.DOOption) sysParam {
	_sysParam := sysParam{}

	_sysParam.sysParamDo.UseDB(db, opts...)
	_sysParam.sysParamDo.UseModel(&model.SysParam{})

	tableName := _sysParam.sysParamDo.TableName()
	_sysParam.ALL = field.NewAsterisk(tableName)
	_sysParam.ID = field.NewInt64(tableName, "id")
	_sysParam.CreatedAt = field.NewTime(tableName, "created_at")
	_sysParam.UpdatedAt = field.NewTime(tableName, "updated_at")
	_sysParam.DeletedAt = field.NewField(tableName, "deleted_at")
	_sysParam.Name = field.NewString(tableName, "name")
	_sysParam.Key = field.NewString(tableName, "key")
	_sysParam.Value = field.NewString(tableName, "value")
	_sysParam.Desc = field.NewString(tableName, "desc")

	_sysParam.fillFieldMap()

	return _sysParam
}

type sysParam struct {
	sysParamDo sysParamDo

	ALL       field.Asterisk
	ID        field.Int64
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Name      field.String // 参数名称
	Key       field.String // 参数键
	Value     field.String // 参数值
	Desc      field.String // 参数说明

	fieldMap map[string]field.Expr
}

func (s sysParam) Table(newTableName string) *sysParam {
	s.sysParamDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *sysParam) updateTableName(table string) *sysParam {
	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.Name = field.NewString(table, "name")
	s.Key = field.NewString(table, "key")
	s.Value = field.NewString(table, "value")
	s.Desc = field.NewString(table, "desc")

	s.fillFieldMap()

	return s
}

func (s *sysParam) WithContext(ctx context.Context) ISysParamDo { return s.sysParamDo.WithContext(ctx) }

func (s sysParam) TableName() string { return s.sysParamDo.TableName() }

func (s sysParam) Alias() string { return s.sysParamDo.Alias() }

func (s sysParam) Columns(cols ...field.Expr) gen.Columns { return s.sysParamDo.Columns(cols...) }

func (s *sysParam) 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 *sysParam) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 8)
	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["name"] = s.Name
	s.fieldMap["key"] = s.Key
	s.fieldMap["value"] = s.Value
	s.fieldMap["desc"] = s.Desc
}

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

func (s sysParam) replaceDB(db *gorm.DB) sysParam {
	s.sysParamDo.ReplaceDB(db)
	return s
}

type sysParamDo struct{ gen.DO }

type ISysParamDo interface {
	gen.SubQuery
	Debug() ISysParamDo
	WithContext(ctx context.Context) ISysParamDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() ISysParamDo
	WriteDB() ISysParamDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) ISysParamDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) ISysParamDo
	Not(conds ...gen.Condition) ISysParamDo
	Or(conds ...gen.Condition) ISysParamDo
	Select(conds ...field.Expr) ISysParamDo
	Where(conds ...gen.Condition) ISysParamDo
	Order(conds ...field.Expr) ISysParamDo
	Distinct(cols ...field.Expr) ISysParamDo
	Omit(cols ...field.Expr) ISysParamDo
	Join(table schema.Tabler, on ...field.Expr) ISysParamDo
	LeftJoin(table schema.Tabler, on ...field.Expr) ISysParamDo
	RightJoin(table schema.Tabler, on ...field.Expr) ISysParamDo
	Group(cols ...field.Expr) ISysParamDo
	Having(conds ...gen.Condition) ISysParamDo
	Limit(limit int) ISysParamDo
	Offset(offset int) ISysParamDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) ISysParamDo
	Unscoped() ISysParamDo
	Create(values ...*model.SysParam) error
	CreateInBatches(values []*model.SysParam, batchSize int) error
	Save(values ...*model.SysParam) error
	First() (*model.SysParam, error)
	Take() (*model.SysParam, error)
	Last() (*model.SysParam, error)
	Find() ([]*model.SysParam, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.SysParam, err error)
	FindInBatches(result *[]*model.SysParam, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.SysParam) (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) ISysParamDo
	Assign(attrs ...field.AssignExpr) ISysParamDo
	Joins(fields ...field.RelationField) ISysParamDo
	Preload(fields ...field.RelationField) ISysParamDo
	FirstOrInit() (*model.SysParam, error)
	FirstOrCreate() (*model.SysParam, error)
	FindByPage(offset int, limit int) (result []*model.SysParam, 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) ISysParamDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (s sysParamDo) Debug() ISysParamDo {
	return s.withDO(s.DO.Debug())
}

func (s sysParamDo) WithContext(ctx context.Context) ISysParamDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sysParamDo) ReadDB() ISysParamDo {
	return s.Clauses(dbresolver.Read)
}

func (s sysParamDo) WriteDB() ISysParamDo {
	return s.Clauses(dbresolver.Write)
}

func (s sysParamDo) Session(config *gorm.Session) ISysParamDo {
	return s.withDO(s.DO.Session(config))
}

func (s sysParamDo) Clauses(conds ...clause.Expression) ISysParamDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s sysParamDo) Not(conds ...gen.Condition) ISysParamDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sysParamDo) Or(conds ...gen.Condition) ISysParamDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sysParamDo) Select(conds ...field.Expr) ISysParamDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sysParamDo) Where(conds ...gen.Condition) ISysParamDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sysParamDo) Order(conds ...field.Expr) ISysParamDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sysParamDo) Distinct(cols ...field.Expr) ISysParamDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sysParamDo) Omit(cols ...field.Expr) ISysParamDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s sysParamDo) Group(cols ...field.Expr) ISysParamDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sysParamDo) Having(conds ...gen.Condition) ISysParamDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sysParamDo) Limit(limit int) ISysParamDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sysParamDo) Offset(offset int) ISysParamDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sysParamDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISysParamDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sysParamDo) Unscoped() ISysParamDo {
	return s.withDO(s.DO.Unscoped())
}

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

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

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

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

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

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

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

func (s sysParamDo) Attrs(attrs ...field.AssignExpr) ISysParamDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sysParamDo) Assign(attrs ...field.AssignExpr) ISysParamDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

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

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

func (s sysParamDo) FindByPage(offset int, limit int) (result []*model.SysParam, 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 sysParamDo) 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 sysParamDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

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

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