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

	_ordersSettle.ordersSettleDo.UseDB(db, opts...)
	_ordersSettle.ordersSettleDo.UseModel(&model.OrdersSettle{})

	tableName := _ordersSettle.ordersSettleDo.TableName()
	_ordersSettle.ALL = field.NewAsterisk(tableName)
	_ordersSettle.ID = field.NewInt64(tableName, "id")
	_ordersSettle.Type = field.NewInt32(tableName, "type")
	_ordersSettle.UserID = field.NewInt64(tableName, "user_id")
	_ordersSettle.Openid = field.NewString(tableName, "openid")
	_ordersSettle.Balance = field.NewInt32(tableName, "balance")
	_ordersSettle.Remark = field.NewString(tableName, "remark")
	_ordersSettle.OrderSn = field.NewString(tableName, "order_sn")
	_ordersSettle.CreatedBy = field.NewInt64(tableName, "created_by")
	_ordersSettle.Status = field.NewInt32(tableName, "status")
	_ordersSettle.Response = field.NewString(tableName, "response")
	_ordersSettle.Error = field.NewString(tableName, "error")
	_ordersSettle.CreatedAt = field.NewTime(tableName, "created_at")

	_ordersSettle.fillFieldMap()

	return _ordersSettle
}

type ordersSettle struct {
	ordersSettleDo

	ALL       field.Asterisk
	ID        field.Int64
	Type      field.Int32  // 平账类型(1：米大师)
	UserID    field.Int64  // 玩家ID
	Openid    field.String // openid
	Balance   field.Int32  // 平账金额
	Remark    field.String // 平账理由
	OrderSn   field.String // 订单号
	CreatedBy field.Int64  // 操作人
	Status    field.Int32  // 订单状态
	Response  field.String // 响应结果
	Error     field.String // 报错信息
	CreatedAt field.Time   // 创建时间

	fieldMap map[string]field.Expr
}

func (o ordersSettle) Table(newTableName string) *ordersSettle {
	o.ordersSettleDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

func (o ordersSettle) As(alias string) *ordersSettle {
	o.ordersSettleDo.DO = *(o.ordersSettleDo.As(alias).(*gen.DO))
	return o.updateTableName(alias)
}

func (o *ordersSettle) updateTableName(table string) *ordersSettle {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.Type = field.NewInt32(table, "type")
	o.UserID = field.NewInt64(table, "user_id")
	o.Openid = field.NewString(table, "openid")
	o.Balance = field.NewInt32(table, "balance")
	o.Remark = field.NewString(table, "remark")
	o.OrderSn = field.NewString(table, "order_sn")
	o.CreatedBy = field.NewInt64(table, "created_by")
	o.Status = field.NewInt32(table, "status")
	o.Response = field.NewString(table, "response")
	o.Error = field.NewString(table, "error")
	o.CreatedAt = field.NewTime(table, "created_at")

	o.fillFieldMap()

	return o
}

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

func (o *ordersSettle) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 12)
	o.fieldMap["id"] = o.ID
	o.fieldMap["type"] = o.Type
	o.fieldMap["user_id"] = o.UserID
	o.fieldMap["openid"] = o.Openid
	o.fieldMap["balance"] = o.Balance
	o.fieldMap["remark"] = o.Remark
	o.fieldMap["order_sn"] = o.OrderSn
	o.fieldMap["created_by"] = o.CreatedBy
	o.fieldMap["status"] = o.Status
	o.fieldMap["response"] = o.Response
	o.fieldMap["error"] = o.Error
	o.fieldMap["created_at"] = o.CreatedAt
}

func (o ordersSettle) clone(db *gorm.DB) ordersSettle {
	o.ordersSettleDo.ReplaceConnPool(db.Statement.ConnPool)
	return o
}

func (o ordersSettle) replaceDB(db *gorm.DB) ordersSettle {
	o.ordersSettleDo.ReplaceDB(db)
	return o
}

type ordersSettleDo struct{ gen.DO }

func (o ordersSettleDo) Debug() *ordersSettleDo {
	return o.withDO(o.DO.Debug())
}

func (o ordersSettleDo) WithContext(ctx context.Context) *ordersSettleDo {
	return o.withDO(o.DO.WithContext(ctx))
}

func (o ordersSettleDo) ReadDB() *ordersSettleDo {
	return o.Clauses(dbresolver.Read)
}

func (o ordersSettleDo) WriteDB() *ordersSettleDo {
	return o.Clauses(dbresolver.Write)
}

func (o ordersSettleDo) Session(config *gorm.Session) *ordersSettleDo {
	return o.withDO(o.DO.Session(config))
}

func (o ordersSettleDo) Clauses(conds ...clause.Expression) *ordersSettleDo {
	return o.withDO(o.DO.Clauses(conds...))
}

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

func (o ordersSettleDo) Not(conds ...gen.Condition) *ordersSettleDo {
	return o.withDO(o.DO.Not(conds...))
}

func (o ordersSettleDo) Or(conds ...gen.Condition) *ordersSettleDo {
	return o.withDO(o.DO.Or(conds...))
}

func (o ordersSettleDo) Select(conds ...field.Expr) *ordersSettleDo {
	return o.withDO(o.DO.Select(conds...))
}

func (o ordersSettleDo) Where(conds ...gen.Condition) *ordersSettleDo {
	return o.withDO(o.DO.Where(conds...))
}

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

func (o ordersSettleDo) Order(conds ...field.Expr) *ordersSettleDo {
	return o.withDO(o.DO.Order(conds...))
}

func (o ordersSettleDo) Distinct(cols ...field.Expr) *ordersSettleDo {
	return o.withDO(o.DO.Distinct(cols...))
}

func (o ordersSettleDo) Omit(cols ...field.Expr) *ordersSettleDo {
	return o.withDO(o.DO.Omit(cols...))
}

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

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

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

func (o ordersSettleDo) Group(cols ...field.Expr) *ordersSettleDo {
	return o.withDO(o.DO.Group(cols...))
}

func (o ordersSettleDo) Having(conds ...gen.Condition) *ordersSettleDo {
	return o.withDO(o.DO.Having(conds...))
}

func (o ordersSettleDo) Limit(limit int) *ordersSettleDo {
	return o.withDO(o.DO.Limit(limit))
}

func (o ordersSettleDo) Offset(offset int) *ordersSettleDo {
	return o.withDO(o.DO.Offset(offset))
}

func (o ordersSettleDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *ordersSettleDo {
	return o.withDO(o.DO.Scopes(funcs...))
}

func (o ordersSettleDo) Unscoped() *ordersSettleDo {
	return o.withDO(o.DO.Unscoped())
}

func (o ordersSettleDo) Create(values ...*model.OrdersSettle) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Create(values)
}

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

func (o ordersSettleDo) First() (*model.OrdersSettle, error) {
	if result, err := o.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrdersSettle), nil
	}
}

func (o ordersSettleDo) Take() (*model.OrdersSettle, error) {
	if result, err := o.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrdersSettle), nil
	}
}

func (o ordersSettleDo) Last() (*model.OrdersSettle, error) {
	if result, err := o.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrdersSettle), nil
	}
}

func (o ordersSettleDo) Find() ([]*model.OrdersSettle, error) {
	result, err := o.DO.Find()
	return result.([]*model.OrdersSettle), err
}

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

func (o ordersSettleDo) Attrs(attrs ...field.AssignExpr) *ordersSettleDo {
	return o.withDO(o.DO.Attrs(attrs...))
}

func (o ordersSettleDo) Assign(attrs ...field.AssignExpr) *ordersSettleDo {
	return o.withDO(o.DO.Assign(attrs...))
}

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

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

func (o ordersSettleDo) FirstOrInit() (*model.OrdersSettle, error) {
	if result, err := o.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrdersSettle), nil
	}
}

func (o ordersSettleDo) FirstOrCreate() (*model.OrdersSettle, error) {
	if result, err := o.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.OrdersSettle), nil
	}
}

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

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

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

func (o ordersSettleDo) Scan(result interface{}) (err error) {
	return o.DO.Scan(result)
}

func (o ordersSettleDo) Delete(models ...*model.OrdersSettle) (result gen.ResultInfo, err error) {
	return o.DO.Delete(models)
}

func (o *ordersSettleDo) withDO(do gen.Dao) *ordersSettleDo {
	o.DO = *do.(*gen.DO)
	return o
}
