// 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 dao

import (
	"context"
	"strings"

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"github.com/flowline-io/flowbot/internal/store/model"
)

func newParameter(db *gorm.DB, opts ...gen.DOOption) parameter {
	_parameter := parameter{}

	_parameter.parameterDo.UseDB(db, opts...)
	_parameter.parameterDo.UseModel(&model.Parameter{})

	tableName := _parameter.parameterDo.TableName()
	_parameter.ALL = field.NewAsterisk(tableName)
	_parameter.ID = field.NewInt64(tableName, "id")
	_parameter.Flag = field.NewString(tableName, "flag")
	_parameter.Params = field.NewField(tableName, "params")
	_parameter.CreatedAt = field.NewTime(tableName, "created_at")
	_parameter.UpdatedAt = field.NewTime(tableName, "updated_at")
	_parameter.ExpiredAt = field.NewTime(tableName, "expired_at")

	_parameter.fillFieldMap()

	return _parameter
}

type parameter struct {
	parameterDo

	ALL       field.Asterisk
	ID        field.Int64
	Flag      field.String
	Params    field.Field
	CreatedAt field.Time
	UpdatedAt field.Time
	ExpiredAt field.Time

	fieldMap map[string]field.Expr
}

func (p parameter) Table(newTableName string) *parameter {
	p.parameterDo.UseTable(newTableName)
	return p.updateTableName(newTableName)
}

func (p parameter) As(alias string) *parameter {
	p.parameterDo.DO = *(p.parameterDo.As(alias).(*gen.DO))
	return p.updateTableName(alias)
}

func (p *parameter) updateTableName(table string) *parameter {
	p.ALL = field.NewAsterisk(table)
	p.ID = field.NewInt64(table, "id")
	p.Flag = field.NewString(table, "flag")
	p.Params = field.NewField(table, "params")
	p.CreatedAt = field.NewTime(table, "created_at")
	p.UpdatedAt = field.NewTime(table, "updated_at")
	p.ExpiredAt = field.NewTime(table, "expired_at")

	p.fillFieldMap()

	return p
}

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

func (p *parameter) fillFieldMap() {
	p.fieldMap = make(map[string]field.Expr, 6)
	p.fieldMap["id"] = p.ID
	p.fieldMap["flag"] = p.Flag
	p.fieldMap["params"] = p.Params
	p.fieldMap["created_at"] = p.CreatedAt
	p.fieldMap["updated_at"] = p.UpdatedAt
	p.fieldMap["expired_at"] = p.ExpiredAt
}

func (p parameter) clone(db *gorm.DB) parameter {
	p.parameterDo.ReplaceConnPool(db.Statement.ConnPool)
	return p
}

func (p parameter) replaceDB(db *gorm.DB) parameter {
	p.parameterDo.ReplaceDB(db)
	return p
}

type parameterDo struct{ gen.DO }

// GetByID
//
// SELECT * FROM @@table WHERE id=@id
func (p parameterDo) GetByID(id int) (result model.Parameter, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("SELECT * FROM parameter WHERE id=? ")

	var executeSQL *gorm.DB
	executeSQL = p.UnderlyingDB().Raw(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// TakeByUidAndTopic query data by uid and topic and return one
//
// where("uid=@uid AND topic=@topic")
func (p parameterDo) TakeByUidAndTopic(uid string, topic string) (result model.Parameter, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, uid)
	params = append(params, topic)
	generateSQL.WriteString("uid=? AND topic=? ")

	var executeSQL *gorm.DB
	executeSQL = p.UnderlyingDB().Where(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// FindByUidAndTopic query data by uid and topic and return array
//
// where("uid=@uid AND topic=@topic")
func (p parameterDo) FindByUidAndTopic(uid string, topic string) (result []*model.Parameter, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, uid)
	params = append(params, topic)
	generateSQL.WriteString("uid=? AND topic=? ")

	var executeSQL *gorm.DB
	executeSQL = p.UnderlyingDB().Where(generateSQL.String(), params...).Find(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (p parameterDo) Debug() *parameterDo {
	return p.withDO(p.DO.Debug())
}

func (p parameterDo) WithContext(ctx context.Context) *parameterDo {
	return p.withDO(p.DO.WithContext(ctx))
}

func (p parameterDo) ReadDB() *parameterDo {
	return p.Clauses(dbresolver.Read)
}

func (p parameterDo) WriteDB() *parameterDo {
	return p.Clauses(dbresolver.Write)
}

func (p parameterDo) Session(config *gorm.Session) *parameterDo {
	return p.withDO(p.DO.Session(config))
}

func (p parameterDo) Clauses(conds ...clause.Expression) *parameterDo {
	return p.withDO(p.DO.Clauses(conds...))
}

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

func (p parameterDo) Not(conds ...gen.Condition) *parameterDo {
	return p.withDO(p.DO.Not(conds...))
}

func (p parameterDo) Or(conds ...gen.Condition) *parameterDo {
	return p.withDO(p.DO.Or(conds...))
}

func (p parameterDo) Select(conds ...field.Expr) *parameterDo {
	return p.withDO(p.DO.Select(conds...))
}

func (p parameterDo) Where(conds ...gen.Condition) *parameterDo {
	return p.withDO(p.DO.Where(conds...))
}

func (p parameterDo) Order(conds ...field.Expr) *parameterDo {
	return p.withDO(p.DO.Order(conds...))
}

func (p parameterDo) Distinct(cols ...field.Expr) *parameterDo {
	return p.withDO(p.DO.Distinct(cols...))
}

func (p parameterDo) Omit(cols ...field.Expr) *parameterDo {
	return p.withDO(p.DO.Omit(cols...))
}

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

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

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

func (p parameterDo) Group(cols ...field.Expr) *parameterDo {
	return p.withDO(p.DO.Group(cols...))
}

func (p parameterDo) Having(conds ...gen.Condition) *parameterDo {
	return p.withDO(p.DO.Having(conds...))
}

func (p parameterDo) Limit(limit int) *parameterDo {
	return p.withDO(p.DO.Limit(limit))
}

func (p parameterDo) Offset(offset int) *parameterDo {
	return p.withDO(p.DO.Offset(offset))
}

func (p parameterDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *parameterDo {
	return p.withDO(p.DO.Scopes(funcs...))
}

func (p parameterDo) Unscoped() *parameterDo {
	return p.withDO(p.DO.Unscoped())
}

func (p parameterDo) Create(values ...*model.Parameter) error {
	if len(values) == 0 {
		return nil
	}
	return p.DO.Create(values)
}

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

func (p parameterDo) First() (*model.Parameter, error) {
	if result, err := p.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Parameter), nil
	}
}

func (p parameterDo) Take() (*model.Parameter, error) {
	if result, err := p.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Parameter), nil
	}
}

func (p parameterDo) Last() (*model.Parameter, error) {
	if result, err := p.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Parameter), nil
	}
}

func (p parameterDo) Find() ([]*model.Parameter, error) {
	result, err := p.DO.Find()
	return result.([]*model.Parameter), err
}

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

func (p parameterDo) Attrs(attrs ...field.AssignExpr) *parameterDo {
	return p.withDO(p.DO.Attrs(attrs...))
}

func (p parameterDo) Assign(attrs ...field.AssignExpr) *parameterDo {
	return p.withDO(p.DO.Assign(attrs...))
}

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

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

func (p parameterDo) FirstOrInit() (*model.Parameter, error) {
	if result, err := p.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Parameter), nil
	}
}

func (p parameterDo) FirstOrCreate() (*model.Parameter, error) {
	if result, err := p.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Parameter), nil
	}
}

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

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

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

func (p parameterDo) Scan(result interface{}) (err error) {
	return p.DO.Scan(result)
}

func (p parameterDo) Delete(models ...*model.Parameter) (result gen.ResultInfo, err error) {
	return p.DO.Delete(models)
}

func (p *parameterDo) withDO(do gen.Dao) *parameterDo {
	p.DO = *do.(*gen.DO)
	return p
}
