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

	_adminNotice.adminNoticeDo.UseDB(db, opts...)
	_adminNotice.adminNoticeDo.UseModel(&model.AdminNotice{})

	tableName := _adminNotice.adminNoticeDo.TableName()
	_adminNotice.ALL = field.NewAsterisk(tableName)
	_adminNotice.ID = field.NewInt64(tableName, "id")
	_adminNotice.Environment = field.NewString(tableName, "environment")
	_adminNotice.NoticeType = field.NewInt32(tableName, "notice_type")
	_adminNotice.Content = field.NewString(tableName, "content")
	_adminNotice.ServerIds = field.NewString(tableName, "server_ids")
	_adminNotice.SendInterval = field.NewInt64(tableName, "send_interval")
	_adminNotice.StartAt = field.NewInt64(tableName, "start_at")
	_adminNotice.EndAt = field.NewInt64(tableName, "end_at")
	_adminNotice.LastSendAt = field.NewInt64(tableName, "last_send_at")
	_adminNotice.MsgID = field.NewString(tableName, "msg_id")
	_adminNotice.Status = field.NewInt32(tableName, "status")
	_adminNotice.UpdatedAt = field.NewInt64(tableName, "updated_at")
	_adminNotice.CreatedAt = field.NewInt64(tableName, "created_at")

	_adminNotice.fillFieldMap()

	return _adminNotice
}

type adminNotice struct {
	adminNoticeDo

	ALL          field.Asterisk
	ID           field.Int64
	Environment  field.String // 所在环境
	NoticeType   field.Int32  // 公告类型(1普通;2停服)
	Content      field.String // 公告内容
	ServerIds    field.String // 服务器
	SendInterval field.Int64  // 发送间隔(分钟)
	StartAt      field.Int64  // 开始时间
	EndAt        field.Int64  // 结束时间
	LastSendAt   field.Int64  // 最后发送时间
	MsgID        field.String // 广播消息ID
	Status       field.Int32  // 公告状态(1运行中;2已结束;3已终止;4未开始)
	UpdatedAt    field.Int64  // 更新时间
	CreatedAt    field.Int64  // 创建时间

	fieldMap map[string]field.Expr
}

func (a adminNotice) Table(newTableName string) *adminNotice {
	a.adminNoticeDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *adminNotice) updateTableName(table string) *adminNotice {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.Environment = field.NewString(table, "environment")
	a.NoticeType = field.NewInt32(table, "notice_type")
	a.Content = field.NewString(table, "content")
	a.ServerIds = field.NewString(table, "server_ids")
	a.SendInterval = field.NewInt64(table, "send_interval")
	a.StartAt = field.NewInt64(table, "start_at")
	a.EndAt = field.NewInt64(table, "end_at")
	a.LastSendAt = field.NewInt64(table, "last_send_at")
	a.MsgID = field.NewString(table, "msg_id")
	a.Status = field.NewInt32(table, "status")
	a.UpdatedAt = field.NewInt64(table, "updated_at")
	a.CreatedAt = field.NewInt64(table, "created_at")

	a.fillFieldMap()

	return a
}

func (a *adminNotice) 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 *adminNotice) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 13)
	a.fieldMap["id"] = a.ID
	a.fieldMap["environment"] = a.Environment
	a.fieldMap["notice_type"] = a.NoticeType
	a.fieldMap["content"] = a.Content
	a.fieldMap["server_ids"] = a.ServerIds
	a.fieldMap["send_interval"] = a.SendInterval
	a.fieldMap["start_at"] = a.StartAt
	a.fieldMap["end_at"] = a.EndAt
	a.fieldMap["last_send_at"] = a.LastSendAt
	a.fieldMap["msg_id"] = a.MsgID
	a.fieldMap["status"] = a.Status
	a.fieldMap["updated_at"] = a.UpdatedAt
	a.fieldMap["created_at"] = a.CreatedAt
}

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

func (a adminNotice) replaceDB(db *gorm.DB) adminNotice {
	a.adminNoticeDo.ReplaceDB(db)
	return a
}

type adminNoticeDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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