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

	"gadmin/internal/gorm/model"
)

func newAdminEmail(db *gorm.DB, opts ...gen.DOOption) adminEmail {
	_adminEmail := adminEmail{}

	_adminEmail.adminEmailDo.UseDB(db, opts...)
	_adminEmail.adminEmailDo.UseModel(&model.AdminEmail{})

	tableName := _adminEmail.adminEmailDo.TableName()
	_adminEmail.ALL = field.NewAsterisk(tableName)
	_adminEmail.ID = field.NewInt64(tableName, "id")
	_adminEmail.OperatorID = field.NewInt64(tableName, "operator_id")
	_adminEmail.Title = field.NewString(tableName, "title")
	_adminEmail.Content = field.NewString(tableName, "content")
	_adminEmail.Ext = field.NewString(tableName, "ext")
	_adminEmail.SendType = field.NewInt32(tableName, "send_type")
	_adminEmail.SendAt = field.NewInt64(tableName, "send_at")
	_adminEmail.Expired = field.NewInt32(tableName, "expired")
	_adminEmail.ReceiptType = field.NewInt32(tableName, "receipt_type")
	_adminEmail.ServerIds = field.NewString(tableName, "server_ids")
	_adminEmail.PlayerIds = field.NewString(tableName, "player_ids")
	_adminEmail.Status = field.NewInt32(tableName, "status")
	_adminEmail.UpdatedAt = field.NewInt64(tableName, "updated_at")
	_adminEmail.CreatedAt = field.NewInt64(tableName, "created_at")
	_adminEmail.Remark = field.NewString(tableName, "remark")

	_adminEmail.fillFieldMap()

	return _adminEmail
}

type adminEmail struct {
	adminEmailDo

	ALL         field.Asterisk
	ID          field.Int64
	OperatorID  field.Int64  // 操作人
	Title       field.String // 标题
	Content     field.String
	Ext         field.String // 奖励
	SendType    field.Int32  // 发送方式
	SendAt      field.Int64  // 发送时间
	Expired     field.Int32  // 有效期
	ReceiptType field.Int32  // 收件类型
	ServerIds   field.String // 服务器
	PlayerIds   field.String // 玩家ID
	Status      field.Int32  // 发送状态
	UpdatedAt   field.Int64  // 更新时间
	CreatedAt   field.Int64  // 创建时间
	Remark      field.String // 备注

	fieldMap map[string]field.Expr
}

func (a adminEmail) Table(newTableName string) *adminEmail {
	a.adminEmailDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a adminEmail) As(alias string) *adminEmail {
	a.adminEmailDo.DO = *(a.adminEmailDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *adminEmail) updateTableName(table string) *adminEmail {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.OperatorID = field.NewInt64(table, "operator_id")
	a.Title = field.NewString(table, "title")
	a.Content = field.NewString(table, "content")
	a.Ext = field.NewString(table, "ext")
	a.SendType = field.NewInt32(table, "send_type")
	a.SendAt = field.NewInt64(table, "send_at")
	a.Expired = field.NewInt32(table, "expired")
	a.ReceiptType = field.NewInt32(table, "receipt_type")
	a.ServerIds = field.NewString(table, "server_ids")
	a.PlayerIds = field.NewString(table, "player_ids")
	a.Status = field.NewInt32(table, "status")
	a.UpdatedAt = field.NewInt64(table, "updated_at")
	a.CreatedAt = field.NewInt64(table, "created_at")
	a.Remark = field.NewString(table, "remark")

	a.fillFieldMap()

	return a
}

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

func (a *adminEmail) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 15)
	a.fieldMap["id"] = a.ID
	a.fieldMap["operator_id"] = a.OperatorID
	a.fieldMap["title"] = a.Title
	a.fieldMap["content"] = a.Content
	a.fieldMap["ext"] = a.Ext
	a.fieldMap["send_type"] = a.SendType
	a.fieldMap["send_at"] = a.SendAt
	a.fieldMap["expired"] = a.Expired
	a.fieldMap["receipt_type"] = a.ReceiptType
	a.fieldMap["server_ids"] = a.ServerIds
	a.fieldMap["player_ids"] = a.PlayerIds
	a.fieldMap["status"] = a.Status
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["remark"] = a.Remark
}

func (a adminEmail) clone(db *gorm.DB) adminEmail {
	a.adminEmailDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a adminEmail) replaceDB(db *gorm.DB) adminEmail {
	a.adminEmailDo.ReplaceDB(db)
	return a
}

type adminEmailDo struct{ gen.DO }

func (a adminEmailDo) Debug() *adminEmailDo {
	return a.withDO(a.DO.Debug())
}

func (a adminEmailDo) WithContext(ctx context.Context) *adminEmailDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a adminEmailDo) ReadDB() *adminEmailDo {
	return a.Clauses(dbresolver.Read)
}

func (a adminEmailDo) WriteDB() *adminEmailDo {
	return a.Clauses(dbresolver.Write)
}

func (a adminEmailDo) Session(config *gorm.Session) *adminEmailDo {
	return a.withDO(a.DO.Session(config))
}

func (a adminEmailDo) Clauses(conds ...clause.Expression) *adminEmailDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a adminEmailDo) Not(conds ...gen.Condition) *adminEmailDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a adminEmailDo) Or(conds ...gen.Condition) *adminEmailDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a adminEmailDo) Select(conds ...field.Expr) *adminEmailDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a adminEmailDo) Where(conds ...gen.Condition) *adminEmailDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a adminEmailDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) *adminEmailDo {
	return a.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
}

func (a adminEmailDo) Order(conds ...field.Expr) *adminEmailDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a adminEmailDo) Distinct(cols ...field.Expr) *adminEmailDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a adminEmailDo) Omit(cols ...field.Expr) *adminEmailDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a adminEmailDo) Group(cols ...field.Expr) *adminEmailDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a adminEmailDo) Having(conds ...gen.Condition) *adminEmailDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a adminEmailDo) Limit(limit int) *adminEmailDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a adminEmailDo) Offset(offset int) *adminEmailDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a adminEmailDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *adminEmailDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a adminEmailDo) Unscoped() *adminEmailDo {
	return a.withDO(a.DO.Unscoped())
}

func (a adminEmailDo) Create(values ...*model.AdminEmail) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a adminEmailDo) First() (*model.AdminEmail, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdminEmail), nil
	}
}

func (a adminEmailDo) Take() (*model.AdminEmail, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdminEmail), nil
	}
}

func (a adminEmailDo) Last() (*model.AdminEmail, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdminEmail), nil
	}
}

func (a adminEmailDo) Find() ([]*model.AdminEmail, error) {
	result, err := a.DO.Find()
	return result.([]*model.AdminEmail), err
}

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

func (a adminEmailDo) Attrs(attrs ...field.AssignExpr) *adminEmailDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a adminEmailDo) Assign(attrs ...field.AssignExpr) *adminEmailDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a adminEmailDo) FirstOrInit() (*model.AdminEmail, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdminEmail), nil
	}
}

func (a adminEmailDo) FirstOrCreate() (*model.AdminEmail, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdminEmail), nil
	}
}

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

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

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

func (a adminEmailDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a adminEmailDo) Delete(models ...*model.AdminEmail) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *adminEmailDo) withDO(do gen.Dao) *adminEmailDo {
	a.DO = *do.(*gen.DO)
	return a
}
