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

	_adminLog.adminLogDo.UseDB(db, opts...)
	_adminLog.adminLogDo.UseModel(&model.AdminLog{})

	tableName := _adminLog.adminLogDo.TableName()
	_adminLog.ALL = field.NewAsterisk(tableName)
	_adminLog.ID = field.NewInt64(tableName, "id")
	_adminLog.Environment = field.NewString(tableName, "environment")
	_adminLog.AdminID = field.NewInt64(tableName, "admin_id")
	_adminLog.AppID = field.NewString(tableName, "app_id")
	_adminLog.UserAgent = field.NewString(tableName, "user_agent")
	_adminLog.Method = field.NewString(tableName, "method")
	_adminLog.URL = field.NewString(tableName, "url")
	_adminLog.Host = field.NewString(tableName, "host")
	_adminLog.Path = field.NewString(tableName, "path")
	_adminLog.GetData = field.NewString(tableName, "get_data")
	_adminLog.PostData = field.NewString(tableName, "post_data")
	_adminLog.HeaderData = field.NewString(tableName, "header_data")
	_adminLog.AccessIP = field.NewString(tableName, "access_ip")
	_adminLog.StatusCode = field.NewInt32(tableName, "status_code")
	_adminLog.ErrorMessage = field.NewString(tableName, "error_message")
	_adminLog.Data = field.NewString(tableName, "data")
	_adminLog.Latency = field.NewInt32(tableName, "latency")
	_adminLog.Timestamp = field.NewTime(tableName, "timestamp")
	_adminLog.CreatedAt = field.NewTime(tableName, "created_at")
	_adminLog.Name = field.NewString(tableName, "name")
	_adminLog.Remark = field.NewString(tableName, "remark")

	_adminLog.fillFieldMap()

	return _adminLog
}

type adminLog struct {
	adminLogDo

	ALL          field.Asterisk
	ID           field.Int64
	Environment  field.String // 所在环境
	AdminID      field.Int64  // 管理员ID
	AppID        field.String // 所属应用
	UserAgent    field.String // 访问代理
	Method       field.String // 请求方式
	URL          field.String // 访问地址
	Host         field.String // 服务域
	Path         field.String // 访问路径
	GetData      field.String
	PostData     field.String
	HeaderData   field.String
	AccessIP     field.String // 访问IP
	StatusCode   field.Int32  // 响应状态码
	ErrorMessage field.String // 错误消息
	Data         field.String // 响应数据
	Latency      field.Int32  // 耗时
	Timestamp    field.Time   // 请求时间
	CreatedAt    field.Time   // 创建时间
	Name         field.String
	Remark       field.String

	fieldMap map[string]field.Expr
}

func (a adminLog) Table(newTableName string) *adminLog {
	a.adminLogDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

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

func (a *adminLog) updateTableName(table string) *adminLog {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt64(table, "id")
	a.Environment = field.NewString(table, "environment")
	a.AdminID = field.NewInt64(table, "admin_id")
	a.AppID = field.NewString(table, "app_id")
	a.UserAgent = field.NewString(table, "user_agent")
	a.Method = field.NewString(table, "method")
	a.URL = field.NewString(table, "url")
	a.Host = field.NewString(table, "host")
	a.Path = field.NewString(table, "path")
	a.GetData = field.NewString(table, "get_data")
	a.PostData = field.NewString(table, "post_data")
	a.HeaderData = field.NewString(table, "header_data")
	a.AccessIP = field.NewString(table, "access_ip")
	a.StatusCode = field.NewInt32(table, "status_code")
	a.ErrorMessage = field.NewString(table, "error_message")
	a.Data = field.NewString(table, "data")
	a.Latency = field.NewInt32(table, "latency")
	a.Timestamp = field.NewTime(table, "timestamp")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.Name = field.NewString(table, "name")
	a.Remark = field.NewString(table, "remark")

	a.fillFieldMap()

	return a
}

func (a *adminLog) 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 *adminLog) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 21)
	a.fieldMap["id"] = a.ID
	a.fieldMap["environment"] = a.Environment
	a.fieldMap["admin_id"] = a.AdminID
	a.fieldMap["app_id"] = a.AppID
	a.fieldMap["user_agent"] = a.UserAgent
	a.fieldMap["method"] = a.Method
	a.fieldMap["url"] = a.URL
	a.fieldMap["host"] = a.Host
	a.fieldMap["path"] = a.Path
	a.fieldMap["get_data"] = a.GetData
	a.fieldMap["post_data"] = a.PostData
	a.fieldMap["header_data"] = a.HeaderData
	a.fieldMap["access_ip"] = a.AccessIP
	a.fieldMap["status_code"] = a.StatusCode
	a.fieldMap["error_message"] = a.ErrorMessage
	a.fieldMap["data"] = a.Data
	a.fieldMap["latency"] = a.Latency
	a.fieldMap["timestamp"] = a.Timestamp
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["name"] = a.Name
	a.fieldMap["remark"] = a.Remark
}

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

func (a adminLog) replaceDB(db *gorm.DB) adminLog {
	a.adminLogDo.ReplaceDB(db)
	return a
}

type adminLogDo struct{ gen.DO }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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