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

	_advOriginLog.advOriginLogDo.UseDB(db, opts...)
	_advOriginLog.advOriginLogDo.UseModel(&model.AdvOriginLog{})

	tableName := _advOriginLog.advOriginLogDo.TableName()
	_advOriginLog.ALL = field.NewAsterisk(tableName)
	_advOriginLog.ID = field.NewInt64(tableName, "id")
	_advOriginLog.Platform = field.NewInt64(tableName, "platform")
	_advOriginLog.Userid = field.NewInt64(tableName, "userid")
	_advOriginLog.ServerID = field.NewInt32(tableName, "server_id")
	_advOriginLog.Appid = field.NewString(tableName, "appid")
	_advOriginLog.NewUser = field.NewInt64(tableName, "new_user")
	_advOriginLog.Scene = field.NewString(tableName, "scene")
	_advOriginLog.Aid = field.NewString(tableName, "aid")
	_advOriginLog.Traceid = field.NewString(tableName, "traceid")
	_advOriginLog.IP = field.NewString(tableName, "ip")
	_advOriginLog.UserAgent = field.NewString(tableName, "user_agent")
	_advOriginLog.Extra = field.NewString(tableName, "extra")
	_advOriginLog.Date = field.NewString(tableName, "date")
	_advOriginLog.EventAt = field.NewInt32(tableName, "event_at")
	_advOriginLog.EventAtNs = field.NewInt64(tableName, "event_at_ns")

	_advOriginLog.fillFieldMap()

	return _advOriginLog
}

type advOriginLog struct {
	advOriginLogDo

	ALL       field.Asterisk
	ID        field.Int64
	Platform  field.Int64  // 来源平台
	Userid    field.Int64  // 用户ID
	ServerID  field.Int32  // 服务器ID
	Appid     field.String // 开放ID
	NewUser   field.Int64  // 是否为新用户
	Scene     field.String // 场景值
	Aid       field.String // 广告ID
	Traceid   field.String // 广告链路ID
	IP        field.String // 访问IP
	UserAgent field.String // 访问代理
	Extra     field.String
	Date      field.String // 触发日期 2005-01-02
	EventAt   field.Int32  // 触发时间戳
	EventAtNs field.Int64  // 发生时间ns

	fieldMap map[string]field.Expr
}

func (a advOriginLog) Table(newTableName string) *advOriginLog {
	a.advOriginLogDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a advOriginLog) As(alias string) *advOriginLog {
	a.advOriginLogDo.DO = *(a.advOriginLogDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *advOriginLog) updateTableName(table string) *advOriginLog {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.Platform = field.NewInt64(table, "platform")
	a.Userid = field.NewInt64(table, "userid")
	a.ServerID = field.NewInt32(table, "server_id")
	a.Appid = field.NewString(table, "appid")
	a.NewUser = field.NewInt64(table, "new_user")
	a.Scene = field.NewString(table, "scene")
	a.Aid = field.NewString(table, "aid")
	a.Traceid = field.NewString(table, "traceid")
	a.IP = field.NewString(table, "ip")
	a.UserAgent = field.NewString(table, "user_agent")
	a.Extra = field.NewString(table, "extra")
	a.Date = field.NewString(table, "date")
	a.EventAt = field.NewInt32(table, "event_at")
	a.EventAtNs = field.NewInt64(table, "event_at_ns")

	a.fillFieldMap()

	return a
}

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

func (a *advOriginLog) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 15)
	a.fieldMap["id"] = a.ID
	a.fieldMap["platform"] = a.Platform
	a.fieldMap["userid"] = a.Userid
	a.fieldMap["server_id"] = a.ServerID
	a.fieldMap["appid"] = a.Appid
	a.fieldMap["new_user"] = a.NewUser
	a.fieldMap["scene"] = a.Scene
	a.fieldMap["aid"] = a.Aid
	a.fieldMap["traceid"] = a.Traceid
	a.fieldMap["ip"] = a.IP
	a.fieldMap["user_agent"] = a.UserAgent
	a.fieldMap["extra"] = a.Extra
	a.fieldMap["date"] = a.Date
	a.fieldMap["event_at"] = a.EventAt
	a.fieldMap["event_at_ns"] = a.EventAtNs
}

func (a advOriginLog) clone(db *gorm.DB) advOriginLog {
	a.advOriginLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a advOriginLog) replaceDB(db *gorm.DB) advOriginLog {
	a.advOriginLogDo.ReplaceDB(db)
	return a
}

type advOriginLogDo struct{ gen.DO }

func (a advOriginLogDo) Debug() *advOriginLogDo {
	return a.withDO(a.DO.Debug())
}

func (a advOriginLogDo) WithContext(ctx context.Context) *advOriginLogDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a advOriginLogDo) ReadDB() *advOriginLogDo {
	return a.Clauses(dbresolver.Read)
}

func (a advOriginLogDo) WriteDB() *advOriginLogDo {
	return a.Clauses(dbresolver.Write)
}

func (a advOriginLogDo) Session(config *gorm.Session) *advOriginLogDo {
	return a.withDO(a.DO.Session(config))
}

func (a advOriginLogDo) Clauses(conds ...clause.Expression) *advOriginLogDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a advOriginLogDo) Not(conds ...gen.Condition) *advOriginLogDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a advOriginLogDo) Or(conds ...gen.Condition) *advOriginLogDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a advOriginLogDo) Select(conds ...field.Expr) *advOriginLogDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a advOriginLogDo) Where(conds ...gen.Condition) *advOriginLogDo {
	return a.withDO(a.DO.Where(conds...))
}

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

func (a advOriginLogDo) Order(conds ...field.Expr) *advOriginLogDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a advOriginLogDo) Distinct(cols ...field.Expr) *advOriginLogDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a advOriginLogDo) Omit(cols ...field.Expr) *advOriginLogDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a advOriginLogDo) Group(cols ...field.Expr) *advOriginLogDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a advOriginLogDo) Having(conds ...gen.Condition) *advOriginLogDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a advOriginLogDo) Limit(limit int) *advOriginLogDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a advOriginLogDo) Offset(offset int) *advOriginLogDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a advOriginLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *advOriginLogDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a advOriginLogDo) Unscoped() *advOriginLogDo {
	return a.withDO(a.DO.Unscoped())
}

func (a advOriginLogDo) Create(values ...*model.AdvOriginLog) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a advOriginLogDo) First() (*model.AdvOriginLog, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvOriginLog), nil
	}
}

func (a advOriginLogDo) Take() (*model.AdvOriginLog, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvOriginLog), nil
	}
}

func (a advOriginLogDo) Last() (*model.AdvOriginLog, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvOriginLog), nil
	}
}

func (a advOriginLogDo) Find() ([]*model.AdvOriginLog, error) {
	result, err := a.DO.Find()
	return result.([]*model.AdvOriginLog), err
}

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

func (a advOriginLogDo) Attrs(attrs ...field.AssignExpr) *advOriginLogDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a advOriginLogDo) Assign(attrs ...field.AssignExpr) *advOriginLogDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a advOriginLogDo) FirstOrInit() (*model.AdvOriginLog, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvOriginLog), nil
	}
}

func (a advOriginLogDo) FirstOrCreate() (*model.AdvOriginLog, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AdvOriginLog), nil
	}
}

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

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

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

func (a advOriginLogDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a advOriginLogDo) Delete(models ...*model.AdvOriginLog) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *advOriginLogDo) withDO(do gen.Dao) *advOriginLogDo {
	a.DO = *do.(*gen.DO)
	return a
}
