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

	_usageStatisticHour.usageStatisticHourDo.UseDB(db, opts...)
	_usageStatisticHour.usageStatisticHourDo.UseModel(&model.UsageStatisticHour{})

	tableName := _usageStatisticHour.usageStatisticHourDo.TableName()
	_usageStatisticHour.ALL = field.NewAsterisk(tableName)
	_usageStatisticHour.ID = field.NewInt64(tableName, "id")
	_usageStatisticHour.CreatedAt = field.NewTime(tableName, "created_at")
	_usageStatisticHour.UpdatedAt = field.NewTime(tableName, "updated_at")
	_usageStatisticHour.Status = field.NewInt64(tableName, "status")
	_usageStatisticHour.DeletedAt = field.NewField(tableName, "deleted_at")
	_usageStatisticHour.Addtime = field.NewInt64(tableName, "addtime")
	_usageStatisticHour.Type = field.NewInt64(tableName, "type")
	_usageStatisticHour.BotID = field.NewString(tableName, "bot_id")
	_usageStatisticHour.OrganizationID = field.NewInt64(tableName, "organization_id")
	_usageStatisticHour.AiResponse = field.NewInt64(tableName, "ai_response")
	_usageStatisticHour.SopRun = field.NewInt64(tableName, "sop_run")
	_usageStatisticHour.TotalFriend = field.NewInt64(tableName, "total_friend")
	_usageStatisticHour.TotalGroup = field.NewInt64(tableName, "total_group")
	_usageStatisticHour.AccountBalance = field.NewInt64(tableName, "account_balance")
	_usageStatisticHour.ConsumeToken = field.NewInt64(tableName, "consume_token")
	_usageStatisticHour.ActiveUser = field.NewInt64(tableName, "active_user")
	_usageStatisticHour.NewUser = field.NewInt64(tableName, "new_user")
	_usageStatisticHour.LabelDist = field.NewString(tableName, "label_dist")

	_usageStatisticHour.fillFieldMap()

	return _usageStatisticHour
}

type usageStatisticHour struct {
	usageStatisticHourDo

	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 | 删除日期
	Addtime        field.Int64  // 写入小时
	Type           field.Int64  // 1-微信 2-名片
	BotID          field.String // 微信或名片id
	OrganizationID field.Int64  // 机构 ID
	AiResponse     field.Int64  // AI回复次数
	SopRun         field.Int64  // SOP运行次数
	TotalFriend    field.Int64  // 好友总数
	TotalGroup     field.Int64  // 群总数
	AccountBalance field.Int64  // 账户余额（单位:分）
	ConsumeToken   field.Int64  // 消耗token数
	ActiveUser     field.Int64  // 活跃用户数
	NewUser        field.Int64  // 新增用户数
	LabelDist      field.String // 标签分布

	fieldMap map[string]field.Expr
}

func (u usageStatisticHour) Table(newTableName string) *usageStatisticHour {
	u.usageStatisticHourDo.UseTable(newTableName)
	return u.updateTableName(newTableName)
}

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

func (u *usageStatisticHour) updateTableName(table string) *usageStatisticHour {
	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.Addtime = field.NewInt64(table, "addtime")
	u.Type = field.NewInt64(table, "type")
	u.BotID = field.NewString(table, "bot_id")
	u.OrganizationID = field.NewInt64(table, "organization_id")
	u.AiResponse = field.NewInt64(table, "ai_response")
	u.SopRun = field.NewInt64(table, "sop_run")
	u.TotalFriend = field.NewInt64(table, "total_friend")
	u.TotalGroup = field.NewInt64(table, "total_group")
	u.AccountBalance = field.NewInt64(table, "account_balance")
	u.ConsumeToken = field.NewInt64(table, "consume_token")
	u.ActiveUser = field.NewInt64(table, "active_user")
	u.NewUser = field.NewInt64(table, "new_user")
	u.LabelDist = field.NewString(table, "label_dist")

	u.fillFieldMap()

	return u
}

func (u *usageStatisticHour) 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 *usageStatisticHour) fillFieldMap() {
	u.fieldMap = make(map[string]field.Expr, 18)
	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["addtime"] = u.Addtime
	u.fieldMap["type"] = u.Type
	u.fieldMap["bot_id"] = u.BotID
	u.fieldMap["organization_id"] = u.OrganizationID
	u.fieldMap["ai_response"] = u.AiResponse
	u.fieldMap["sop_run"] = u.SopRun
	u.fieldMap["total_friend"] = u.TotalFriend
	u.fieldMap["total_group"] = u.TotalGroup
	u.fieldMap["account_balance"] = u.AccountBalance
	u.fieldMap["consume_token"] = u.ConsumeToken
	u.fieldMap["active_user"] = u.ActiveUser
	u.fieldMap["new_user"] = u.NewUser
	u.fieldMap["label_dist"] = u.LabelDist
}

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

func (u usageStatisticHour) replaceDB(db *gorm.DB) usageStatisticHour {
	u.usageStatisticHourDo.ReplaceDB(db)
	return u
}

type usageStatisticHourDo struct{ gen.DO }

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

func (u usageStatisticHourDo) Debug() IUsageStatisticHourDo {
	return u.withDO(u.DO.Debug())
}

func (u usageStatisticHourDo) WithContext(ctx context.Context) IUsageStatisticHourDo {
	return u.withDO(u.DO.WithContext(ctx))
}

func (u usageStatisticHourDo) ReadDB() IUsageStatisticHourDo {
	return u.Clauses(dbresolver.Read)
}

func (u usageStatisticHourDo) WriteDB() IUsageStatisticHourDo {
	return u.Clauses(dbresolver.Write)
}

func (u usageStatisticHourDo) Session(config *gorm.Session) IUsageStatisticHourDo {
	return u.withDO(u.DO.Session(config))
}

func (u usageStatisticHourDo) Clauses(conds ...clause.Expression) IUsageStatisticHourDo {
	return u.withDO(u.DO.Clauses(conds...))
}

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

func (u usageStatisticHourDo) Not(conds ...gen.Condition) IUsageStatisticHourDo {
	return u.withDO(u.DO.Not(conds...))
}

func (u usageStatisticHourDo) Or(conds ...gen.Condition) IUsageStatisticHourDo {
	return u.withDO(u.DO.Or(conds...))
}

func (u usageStatisticHourDo) Select(conds ...field.Expr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Select(conds...))
}

func (u usageStatisticHourDo) Where(conds ...gen.Condition) IUsageStatisticHourDo {
	return u.withDO(u.DO.Where(conds...))
}

func (u usageStatisticHourDo) Order(conds ...field.Expr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Order(conds...))
}

func (u usageStatisticHourDo) Distinct(cols ...field.Expr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Distinct(cols...))
}

func (u usageStatisticHourDo) Omit(cols ...field.Expr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Omit(cols...))
}

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

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

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

func (u usageStatisticHourDo) Group(cols ...field.Expr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Group(cols...))
}

func (u usageStatisticHourDo) Having(conds ...gen.Condition) IUsageStatisticHourDo {
	return u.withDO(u.DO.Having(conds...))
}

func (u usageStatisticHourDo) Limit(limit int) IUsageStatisticHourDo {
	return u.withDO(u.DO.Limit(limit))
}

func (u usageStatisticHourDo) Offset(offset int) IUsageStatisticHourDo {
	return u.withDO(u.DO.Offset(offset))
}

func (u usageStatisticHourDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IUsageStatisticHourDo {
	return u.withDO(u.DO.Scopes(funcs...))
}

func (u usageStatisticHourDo) Unscoped() IUsageStatisticHourDo {
	return u.withDO(u.DO.Unscoped())
}

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

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

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

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

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

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

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

func (u usageStatisticHourDo) Attrs(attrs ...field.AssignExpr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Attrs(attrs...))
}

func (u usageStatisticHourDo) Assign(attrs ...field.AssignExpr) IUsageStatisticHourDo {
	return u.withDO(u.DO.Assign(attrs...))
}

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

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

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

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

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

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

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