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

	"wechat-api/database/dao/wechat/model"
)

func newUsageTotal(db *gorm.DB, opts ...gen.DOOption) usageTotal {
	_usageTotal := usageTotal{}

	_usageTotal.usageTotalDo.UseDB(db, opts...)
	_usageTotal.usageTotalDo.UseModel(&model.UsageTotal{})

	tableName := _usageTotal.usageTotalDo.TableName()
	_usageTotal.ALL = field.NewAsterisk(tableName)
	_usageTotal.ID = field.NewInt64(tableName, "id")
	_usageTotal.CreatedAt = field.NewTime(tableName, "created_at")
	_usageTotal.UpdatedAt = field.NewTime(tableName, "updated_at")
	_usageTotal.Status = field.NewInt64(tableName, "status")
	_usageTotal.DeletedAt = field.NewField(tableName, "deleted_at")
	_usageTotal.Type = field.NewInt64(tableName, "type")
	_usageTotal.BotID = field.NewString(tableName, "bot_id")
	_usageTotal.TotalTokens = field.NewInt64(tableName, "total_tokens")
	_usageTotal.StartIndex = field.NewInt64(tableName, "start_index")
	_usageTotal.EndIndex = field.NewInt64(tableName, "end_index")
	_usageTotal.OrganizationID = field.NewInt64(tableName, "organization_id")

	_usageTotal.fillFieldMap()

	return _usageTotal
}

type usageTotal struct {
	usageTotalDo

	ALL            field.Asterisk
	ID             field.Int64
	CreatedAt      field.Time   // Create Time | 创建日期
	UpdatedAt      field.Time   // Update Time | 修改日期
	Status         field.Int64  // 状态 1 正常 2 禁用
	DeletedAt      field.Field  // Delete Time | 删除日期
	Type           field.Int64  // 1 微信 2 名片
	BotID          field.String // 微信或名片id
	TotalTokens    field.Int64  // 使用token总数
	StartIndex     field.Int64  // 重制后的起始usage_detail 索引
	EndIndex       field.Int64  // usage_detail 索引
	OrganizationID field.Int64  // 机构 ID

	fieldMap map[string]field.Expr
}

func (u usageTotal) Table(newTableName string) *usageTotal {
	u.usageTotalDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

func (u usageTotal) As(alias string) *usageTotal {
	u.usageTotalDo.DO = *(u.usageTotalDo.As(alias).(*gen.DO))
	return u.updateTableName(alias)
}

func (u *usageTotal) updateTableName(table string) *usageTotal {
	u.ALL = field.NewAsterisk(table)
	u.ID = field.NewInt64(table, "id")
	u.CreatedAt = field.NewTime(table, "created_at")
	u.UpdatedAt = field.NewTime(table, "updated_at")
	u.Status = field.NewInt64(table, "status")
	u.DeletedAt = field.NewField(table, "deleted_at")
	u.Type = field.NewInt64(table, "type")
	u.BotID = field.NewString(table, "bot_id")
	u.TotalTokens = field.NewInt64(table, "total_tokens")
	u.StartIndex = field.NewInt64(table, "start_index")
	u.EndIndex = field.NewInt64(table, "end_index")
	u.OrganizationID = field.NewInt64(table, "organization_id")

	u.fillFieldMap()

	return u
}

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

func (u *usageTotal) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 11)
	u.fieldMap["id"] = u.ID
	u.fieldMap["created_at"] = u.CreatedAt
	u.fieldMap["updated_at"] = u.UpdatedAt
	u.fieldMap["status"] = u.Status
	u.fieldMap["deleted_at"] = u.DeletedAt
	u.fieldMap["type"] = u.Type
	u.fieldMap["bot_id"] = u.BotID
	u.fieldMap["total_tokens"] = u.TotalTokens
	u.fieldMap["start_index"] = u.StartIndex
	u.fieldMap["end_index"] = u.EndIndex
	u.fieldMap["organization_id"] = u.OrganizationID
}

func (u usageTotal) clone(db *gorm.DB) usageTotal {
	u.usageTotalDo.ReplaceConnPool(db.Statement.ConnPool)
	return u
}

func (u usageTotal) replaceDB(db *gorm.DB) usageTotal {
	u.usageTotalDo.ReplaceDB(db)
	return u
}

type usageTotalDo struct{ gen.DO }

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

func (u usageTotalDo) Debug() IUsageTotalDo {
	return u.withDO(u.DO.Debug())
}

func (u usageTotalDo) WithContext(ctx context.Context) IUsageTotalDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u usageTotalDo) ReadDB() IUsageTotalDo {
	return u.Clauses(dbresolver.Read)
}

func (u usageTotalDo) WriteDB() IUsageTotalDo {
	return u.Clauses(dbresolver.Write)
}

func (u usageTotalDo) Session(config *gorm.Session) IUsageTotalDo {
	return u.withDO(u.DO.Session(config))
}

func (u usageTotalDo) Clauses(conds ...clause.Expression) IUsageTotalDo {
	return u.withDO(u.DO.Clauses(conds...))
}

func (u usageTotalDo) Returning(value interface{}, columns ...string) IUsageTotalDo {
	return u.withDO(u.DO.Returning(value, columns...))
}

func (u usageTotalDo) Not(conds ...gen.Condition) IUsageTotalDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u usageTotalDo) Or(conds ...gen.Condition) IUsageTotalDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u usageTotalDo) Select(conds ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u usageTotalDo) Where(conds ...gen.Condition) IUsageTotalDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u usageTotalDo) Order(conds ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u usageTotalDo) Distinct(cols ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u usageTotalDo) Omit(cols ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.Omit(cols...))
}

func (u usageTotalDo) Join(table schema.Tabler, on ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.Join(table, on...))
}

func (u usageTotalDo) LeftJoin(table schema.Tabler, on ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.LeftJoin(table, on...))
}

func (u usageTotalDo) RightJoin(table schema.Tabler, on ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.RightJoin(table, on...))
}

func (u usageTotalDo) Group(cols ...field.Expr) IUsageTotalDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u usageTotalDo) Having(conds ...gen.Condition) IUsageTotalDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u usageTotalDo) Limit(limit int) IUsageTotalDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u usageTotalDo) Offset(offset int) IUsageTotalDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u usageTotalDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUsageTotalDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u usageTotalDo) Unscoped() IUsageTotalDo {
	return u.withDO(u.DO.Unscoped())
}

func (u usageTotalDo) Create(values ...*model.UsageTotal) error {
	if len(values) == 0 {
		return nil
	}
	return u.DO.Create(values)
}

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

func (u usageTotalDo) First() (*model.UsageTotal, error) {
	if result, err := u.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageTotal), nil
	}
}

func (u usageTotalDo) Take() (*model.UsageTotal, error) {
	if result, err := u.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageTotal), nil
	}
}

func (u usageTotalDo) Last() (*model.UsageTotal, error) {
	if result, err := u.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageTotal), nil
	}
}

func (u usageTotalDo) Find() ([]*model.UsageTotal, error) {
	result, err := u.DO.Find()
	return result.([]*model.UsageTotal), err
}

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

func (u usageTotalDo) Attrs(attrs ...field.AssignExpr) IUsageTotalDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u usageTotalDo) Assign(attrs ...field.AssignExpr) IUsageTotalDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

func (u usageTotalDo) FirstOrInit() (*model.UsageTotal, error) {
	if result, err := u.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageTotal), nil
	}
}

func (u usageTotalDo) FirstOrCreate() (*model.UsageTotal, error) {
	if result, err := u.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.UsageTotal), nil
	}
}

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

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

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

func (u usageTotalDo) Scan(result interface{}) (err error) {
	return u.DO.Scan(result)
}

func (u usageTotalDo) Delete(models ...*model.UsageTotal) (result gen.ResultInfo, err error) {
	return u.DO.Delete(models)
}

func (u *usageTotalDo) withDO(do gen.Dao) *usageTotalDo {
	u.DO = *do.(*gen.DO)
	return u
}
