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

	_ordersSync.ordersSyncDo.UseDB(db, opts...)
	_ordersSync.ordersSyncDo.UseModel(&model.OrdersSync{})

	tableName := _ordersSync.ordersSyncDo.TableName()
	_ordersSync.ALL = field.NewAsterisk(tableName)
	_ordersSync.ID = field.NewInt64(tableName, "id")
	_ordersSync.Platform = field.NewInt32(tableName, "platform")
	_ordersSync.ServerID = field.NewInt32(tableName, "server_id")
	_ordersSync.LastID = field.NewInt64(tableName, "last_id")
	_ordersSync.LastCreateTime = field.NewTime(tableName, "last_create_time")
	_ordersSync.UpdatedAt = field.NewTime(tableName, "updated_at")
	_ordersSync.OrderWay = field.NewInt32(tableName, "order_way")

	_ordersSync.fillFieldMap()

	return _ordersSync
}

type ordersSync struct {
	ordersSyncDo

	ALL            field.Asterisk
	ID             field.Int64
	Platform       field.Int32 // 订单平台(1：安卓，2：苹果)
	ServerID       field.Int32 // 服务器ID
	LastID         field.Int64 // 最后同步ID
	LastCreateTime field.Time  // 最后同步订单的创建时间
	UpdatedAt      field.Time  // 更新时间
	OrderWay       field.Int32 // 1 游戏内订单 2游戏外订单

	fieldMap map[string]field.Expr
}

func (o ordersSync) Table(newTableName string) *ordersSync {
	o.ordersSyncDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

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

func (o *ordersSync) updateTableName(table string) *ordersSync {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.Platform = field.NewInt32(table, "platform")
	o.ServerID = field.NewInt32(table, "server_id")
	o.LastID = field.NewInt64(table, "last_id")
	o.LastCreateTime = field.NewTime(table, "last_create_time")
	o.UpdatedAt = field.NewTime(table, "updated_at")
	o.OrderWay = field.NewInt32(table, "order_way")

	o.fillFieldMap()

	return o
}

func (o *ordersSync) 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 *ordersSync) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 7)
	o.fieldMap["id"] = o.ID
	o.fieldMap["platform"] = o.Platform
	o.fieldMap["server_id"] = o.ServerID
	o.fieldMap["last_id"] = o.LastID
	o.fieldMap["last_create_time"] = o.LastCreateTime
	o.fieldMap["updated_at"] = o.UpdatedAt
	o.fieldMap["order_way"] = o.OrderWay
}

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

func (o ordersSync) replaceDB(db *gorm.DB) ordersSync {
	o.ordersSyncDo.ReplaceDB(db)
	return o
}

type ordersSyncDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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