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

	_creditUsage.creditUsageDo.UseDB(db, opts...)
	_creditUsage.creditUsageDo.UseModel(&model.CreditUsage{})

	tableName := _creditUsage.creditUsageDo.TableName()
	_creditUsage.ALL = field.NewAsterisk(tableName)
	_creditUsage.ID = field.NewInt64(tableName, "id")
	_creditUsage.CreatedAt = field.NewTime(tableName, "created_at")
	_creditUsage.UpdatedAt = field.NewTime(tableName, "updated_at")
	_creditUsage.Status = field.NewInt64(tableName, "status")
	_creditUsage.DeletedAt = field.NewField(tableName, "deleted_at")
	_creditUsage.UserID = field.NewString(tableName, "user_id")
	_creditUsage.Number = field.NewField(tableName, "number")
	_creditUsage.Ntype = field.NewInt64(tableName, "ntype")
	_creditUsage.MTable = field.NewString(tableName, "table")
	_creditUsage.Nid = field.NewInt64(tableName, "nid")
	_creditUsage.Reason = field.NewString(tableName, "reason")
	_creditUsage.Operator = field.NewString(tableName, "operator")
	_creditUsage.OrganizationID = field.NewInt64(tableName, "organization_id")

	_creditUsage.fillFieldMap()

	return _creditUsage
}

// creditUsage 积分变化明细表
type creditUsage struct {
	creditUsageDo

	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 | 删除日期
	UserID         field.String // 用户ID
	Number         field.Field  // 积分改变量
	Ntype          field.Int64  // 积分变化类型：1-消耗 2-增加
	MTable         field.String // 积分变化表名
	Nid            field.Int64  // 积分变化关联信息ID
	Reason         field.String // 积分变动原因
	Operator       field.String // 积分调整人
	OrganizationID field.Int64  // 机构 ID

	fieldMap map[string]field.Expr
}

func (c creditUsage) Table(newTableName string) *creditUsage {
	c.creditUsageDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c creditUsage) As(alias string) *creditUsage {
	c.creditUsageDo.DO = *(c.creditUsageDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *creditUsage) updateTableName(table string) *creditUsage {
	c.ALL = field.NewAsterisk(table)
	c.ID = field.NewInt64(table, "id")
	c.CreatedAt = field.NewTime(table, "created_at")
	c.UpdatedAt = field.NewTime(table, "updated_at")
	c.Status = field.NewInt64(table, "status")
	c.DeletedAt = field.NewField(table, "deleted_at")
	c.UserID = field.NewString(table, "user_id")
	c.Number = field.NewField(table, "number")
	c.Ntype = field.NewInt64(table, "ntype")
	c.MTable = field.NewString(table, "table")
	c.Nid = field.NewInt64(table, "nid")
	c.Reason = field.NewString(table, "reason")
	c.Operator = field.NewString(table, "operator")
	c.OrganizationID = field.NewInt64(table, "organization_id")

	c.fillFieldMap()

	return c
}

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

func (c *creditUsage) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 13)
	c.fieldMap["id"] = c.ID
	c.fieldMap["created_at"] = c.CreatedAt
	c.fieldMap["updated_at"] = c.UpdatedAt
	c.fieldMap["status"] = c.Status
	c.fieldMap["deleted_at"] = c.DeletedAt
	c.fieldMap["user_id"] = c.UserID
	c.fieldMap["number"] = c.Number
	c.fieldMap["ntype"] = c.Ntype
	c.fieldMap["table"] = c.MTable
	c.fieldMap["nid"] = c.Nid
	c.fieldMap["reason"] = c.Reason
	c.fieldMap["operator"] = c.Operator
	c.fieldMap["organization_id"] = c.OrganizationID
}

func (c creditUsage) clone(db *gorm.DB) creditUsage {
	c.creditUsageDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c creditUsage) replaceDB(db *gorm.DB) creditUsage {
	c.creditUsageDo.ReplaceDB(db)
	return c
}

type creditUsageDo struct{ gen.DO }

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

func (c creditUsageDo) Debug() ICreditUsageDo {
	return c.withDO(c.DO.Debug())
}

func (c creditUsageDo) WithContext(ctx context.Context) ICreditUsageDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c creditUsageDo) ReadDB() ICreditUsageDo {
	return c.Clauses(dbresolver.Read)
}

func (c creditUsageDo) WriteDB() ICreditUsageDo {
	return c.Clauses(dbresolver.Write)
}

func (c creditUsageDo) Session(config *gorm.Session) ICreditUsageDo {
	return c.withDO(c.DO.Session(config))
}

func (c creditUsageDo) Clauses(conds ...clause.Expression) ICreditUsageDo {
	return c.withDO(c.DO.Clauses(conds...))
}

func (c creditUsageDo) Returning(value interface{}, columns ...string) ICreditUsageDo {
	return c.withDO(c.DO.Returning(value, columns...))
}

func (c creditUsageDo) Not(conds ...gen.Condition) ICreditUsageDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c creditUsageDo) Or(conds ...gen.Condition) ICreditUsageDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c creditUsageDo) Select(conds ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c creditUsageDo) Where(conds ...gen.Condition) ICreditUsageDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c creditUsageDo) Order(conds ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c creditUsageDo) Distinct(cols ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c creditUsageDo) Omit(cols ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.Omit(cols...))
}

func (c creditUsageDo) Join(table schema.Tabler, on ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.Join(table, on...))
}

func (c creditUsageDo) LeftJoin(table schema.Tabler, on ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.LeftJoin(table, on...))
}

func (c creditUsageDo) RightJoin(table schema.Tabler, on ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.RightJoin(table, on...))
}

func (c creditUsageDo) Group(cols ...field.Expr) ICreditUsageDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c creditUsageDo) Having(conds ...gen.Condition) ICreditUsageDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c creditUsageDo) Limit(limit int) ICreditUsageDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c creditUsageDo) Offset(offset int) ICreditUsageDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c creditUsageDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ICreditUsageDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c creditUsageDo) Unscoped() ICreditUsageDo {
	return c.withDO(c.DO.Unscoped())
}

func (c creditUsageDo) Create(values ...*model.CreditUsage) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c creditUsageDo) First() (*model.CreditUsage, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.CreditUsage), nil
	}
}

func (c creditUsageDo) Take() (*model.CreditUsage, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.CreditUsage), nil
	}
}

func (c creditUsageDo) Last() (*model.CreditUsage, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.CreditUsage), nil
	}
}

func (c creditUsageDo) Find() ([]*model.CreditUsage, error) {
	result, err := c.DO.Find()
	return result.([]*model.CreditUsage), err
}

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

func (c creditUsageDo) Attrs(attrs ...field.AssignExpr) ICreditUsageDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c creditUsageDo) Assign(attrs ...field.AssignExpr) ICreditUsageDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c creditUsageDo) FirstOrInit() (*model.CreditUsage, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.CreditUsage), nil
	}
}

func (c creditUsageDo) FirstOrCreate() (*model.CreditUsage, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.CreditUsage), nil
	}
}

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

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

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

func (c creditUsageDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c creditUsageDo) Delete(models ...*model.CreditUsage) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *creditUsageDo) withDO(do gen.Dao) *creditUsageDo {
	c.DO = *do.(*gen.DO)
	return c
}
