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

	_sysError.sysErrorDo.UseDB(db, opts...)
	_sysError.sysErrorDo.UseModel(&model.SysError{})

	tableName := _sysError.sysErrorDo.TableName()
	_sysError.ALL = field.NewAsterisk(tableName)
	_sysError.ID = field.NewInt64(tableName, "id")
	_sysError.CreatedAt = field.NewTime(tableName, "created_at")
	_sysError.UpdatedAt = field.NewTime(tableName, "updated_at")
	_sysError.DeletedAt = field.NewField(tableName, "deleted_at")
	_sysError.Form = field.NewString(tableName, "form")
	_sysError.Info = field.NewString(tableName, "info")
	_sysError.Level = field.NewString(tableName, "level")
	_sysError.Solution = field.NewString(tableName, "solution")
	_sysError.Status = field.NewString(tableName, "status")

	_sysError.fillFieldMap()

	return _sysError
}

type sysError struct {
	sysErrorDo sysErrorDo

	ALL       field.Asterisk
	ID        field.Int64
	CreatedAt field.Time
	UpdatedAt field.Time
	DeletedAt field.Field
	Form      field.String // 错误来源
	Info      field.String // 错误内容
	Level     field.String // 日志等级
	Solution  field.String // 解决方案
	Status    field.String // 处理状态

	fieldMap map[string]field.Expr
}

func (s sysError) Table(newTableName string) *sysError {
	s.sysErrorDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

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

func (s *sysError) updateTableName(table string) *sysError {
	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.Form = field.NewString(table, "form")
	s.Info = field.NewString(table, "info")
	s.Level = field.NewString(table, "level")
	s.Solution = field.NewString(table, "solution")
	s.Status = field.NewString(table, "status")

	s.fillFieldMap()

	return s
}

func (s *sysError) WithContext(ctx context.Context) ISysErrorDo { return s.sysErrorDo.WithContext(ctx) }

func (s sysError) TableName() string { return s.sysErrorDo.TableName() }

func (s sysError) Alias() string { return s.sysErrorDo.Alias() }

func (s sysError) Columns(cols ...field.Expr) gen.Columns { return s.sysErrorDo.Columns(cols...) }

func (s *sysError) 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 *sysError) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 9)
	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["form"] = s.Form
	s.fieldMap["info"] = s.Info
	s.fieldMap["level"] = s.Level
	s.fieldMap["solution"] = s.Solution
	s.fieldMap["status"] = s.Status
}

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

func (s sysError) replaceDB(db *gorm.DB) sysError {
	s.sysErrorDo.ReplaceDB(db)
	return s
}

type sysErrorDo struct{ gen.DO }

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

func (s sysErrorDo) Debug() ISysErrorDo {
	return s.withDO(s.DO.Debug())
}

func (s sysErrorDo) WithContext(ctx context.Context) ISysErrorDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s sysErrorDo) ReadDB() ISysErrorDo {
	return s.Clauses(dbresolver.Read)
}

func (s sysErrorDo) WriteDB() ISysErrorDo {
	return s.Clauses(dbresolver.Write)
}

func (s sysErrorDo) Session(config *gorm.Session) ISysErrorDo {
	return s.withDO(s.DO.Session(config))
}

func (s sysErrorDo) Clauses(conds ...clause.Expression) ISysErrorDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s sysErrorDo) Not(conds ...gen.Condition) ISysErrorDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s sysErrorDo) Or(conds ...gen.Condition) ISysErrorDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s sysErrorDo) Select(conds ...field.Expr) ISysErrorDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s sysErrorDo) Where(conds ...gen.Condition) ISysErrorDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s sysErrorDo) Order(conds ...field.Expr) ISysErrorDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s sysErrorDo) Distinct(cols ...field.Expr) ISysErrorDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s sysErrorDo) Omit(cols ...field.Expr) ISysErrorDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s sysErrorDo) Group(cols ...field.Expr) ISysErrorDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s sysErrorDo) Having(conds ...gen.Condition) ISysErrorDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s sysErrorDo) Limit(limit int) ISysErrorDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s sysErrorDo) Offset(offset int) ISysErrorDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s sysErrorDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ISysErrorDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s sysErrorDo) Unscoped() ISysErrorDo {
	return s.withDO(s.DO.Unscoped())
}

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

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

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

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

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

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

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

func (s sysErrorDo) Attrs(attrs ...field.AssignExpr) ISysErrorDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s sysErrorDo) Assign(attrs ...field.AssignExpr) ISysErrorDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

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

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

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

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

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