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

	_counterRecord.counterRecordDo.UseDB(db, opts...)
	_counterRecord.counterRecordDo.UseModel(&model.CounterRecord{})

	tableName := _counterRecord.counterRecordDo.TableName()
	_counterRecord.ALL = field.NewAsterisk(tableName)
	_counterRecord.CounterID = field.NewInt64(tableName, "counter_id")
	_counterRecord.Digit = field.NewInt32(tableName, "digit")
	_counterRecord.CreatedAt = field.NewTime(tableName, "created_at")

	_counterRecord.fillFieldMap()

	return _counterRecord
}

type counterRecord struct {
	counterRecordDo

	ALL       field.Asterisk
	CounterID field.Int64
	Digit     field.Int32
	CreatedAt field.Time

	fieldMap map[string]field.Expr
}

func (c counterRecord) Table(newTableName string) *counterRecord {
	c.counterRecordDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

func (c counterRecord) As(alias string) *counterRecord {
	c.counterRecordDo.DO = *(c.counterRecordDo.As(alias).(*gen.DO))
	return c.updateTableName(alias)
}

func (c *counterRecord) updateTableName(table string) *counterRecord {
	c.ALL = field.NewAsterisk(table)
	c.CounterID = field.NewInt64(table, "counter_id")
	c.Digit = field.NewInt32(table, "digit")
	c.CreatedAt = field.NewTime(table, "created_at")

	c.fillFieldMap()

	return c
}

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

func (c *counterRecord) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 3)
	c.fieldMap["counter_id"] = c.CounterID
	c.fieldMap["digit"] = c.Digit
	c.fieldMap["created_at"] = c.CreatedAt
}

func (c counterRecord) clone(db *gorm.DB) counterRecord {
	c.counterRecordDo.ReplaceConnPool(db.Statement.ConnPool)
	return c
}

func (c counterRecord) replaceDB(db *gorm.DB) counterRecord {
	c.counterRecordDo.ReplaceDB(db)
	return c
}

type counterRecordDo struct{ gen.DO }

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

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

	var executeSQL *gorm.DB
	executeSQL = c.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 (c counterRecordDo) TakeByUidAndTopic(uid string, topic string) (result model.CounterRecord, 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 = c.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 (c counterRecordDo) FindByUidAndTopic(uid string, topic string) (result []*model.CounterRecord, 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 = c.UnderlyingDB().Where(generateSQL.String(), params...).Find(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

func (c counterRecordDo) Debug() *counterRecordDo {
	return c.withDO(c.DO.Debug())
}

func (c counterRecordDo) WithContext(ctx context.Context) *counterRecordDo {
	return c.withDO(c.DO.WithContext(ctx))
}

func (c counterRecordDo) ReadDB() *counterRecordDo {
	return c.Clauses(dbresolver.Read)
}

func (c counterRecordDo) WriteDB() *counterRecordDo {
	return c.Clauses(dbresolver.Write)
}

func (c counterRecordDo) Session(config *gorm.Session) *counterRecordDo {
	return c.withDO(c.DO.Session(config))
}

func (c counterRecordDo) Clauses(conds ...clause.Expression) *counterRecordDo {
	return c.withDO(c.DO.Clauses(conds...))
}

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

func (c counterRecordDo) Not(conds ...gen.Condition) *counterRecordDo {
	return c.withDO(c.DO.Not(conds...))
}

func (c counterRecordDo) Or(conds ...gen.Condition) *counterRecordDo {
	return c.withDO(c.DO.Or(conds...))
}

func (c counterRecordDo) Select(conds ...field.Expr) *counterRecordDo {
	return c.withDO(c.DO.Select(conds...))
}

func (c counterRecordDo) Where(conds ...gen.Condition) *counterRecordDo {
	return c.withDO(c.DO.Where(conds...))
}

func (c counterRecordDo) Order(conds ...field.Expr) *counterRecordDo {
	return c.withDO(c.DO.Order(conds...))
}

func (c counterRecordDo) Distinct(cols ...field.Expr) *counterRecordDo {
	return c.withDO(c.DO.Distinct(cols...))
}

func (c counterRecordDo) Omit(cols ...field.Expr) *counterRecordDo {
	return c.withDO(c.DO.Omit(cols...))
}

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

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

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

func (c counterRecordDo) Group(cols ...field.Expr) *counterRecordDo {
	return c.withDO(c.DO.Group(cols...))
}

func (c counterRecordDo) Having(conds ...gen.Condition) *counterRecordDo {
	return c.withDO(c.DO.Having(conds...))
}

func (c counterRecordDo) Limit(limit int) *counterRecordDo {
	return c.withDO(c.DO.Limit(limit))
}

func (c counterRecordDo) Offset(offset int) *counterRecordDo {
	return c.withDO(c.DO.Offset(offset))
}

func (c counterRecordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *counterRecordDo {
	return c.withDO(c.DO.Scopes(funcs...))
}

func (c counterRecordDo) Unscoped() *counterRecordDo {
	return c.withDO(c.DO.Unscoped())
}

func (c counterRecordDo) Create(values ...*model.CounterRecord) error {
	if len(values) == 0 {
		return nil
	}
	return c.DO.Create(values)
}

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

func (c counterRecordDo) First() (*model.CounterRecord, error) {
	if result, err := c.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.CounterRecord), nil
	}
}

func (c counterRecordDo) Take() (*model.CounterRecord, error) {
	if result, err := c.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.CounterRecord), nil
	}
}

func (c counterRecordDo) Last() (*model.CounterRecord, error) {
	if result, err := c.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.CounterRecord), nil
	}
}

func (c counterRecordDo) Find() ([]*model.CounterRecord, error) {
	result, err := c.DO.Find()
	return result.([]*model.CounterRecord), err
}

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

func (c counterRecordDo) Attrs(attrs ...field.AssignExpr) *counterRecordDo {
	return c.withDO(c.DO.Attrs(attrs...))
}

func (c counterRecordDo) Assign(attrs ...field.AssignExpr) *counterRecordDo {
	return c.withDO(c.DO.Assign(attrs...))
}

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

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

func (c counterRecordDo) FirstOrInit() (*model.CounterRecord, error) {
	if result, err := c.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.CounterRecord), nil
	}
}

func (c counterRecordDo) FirstOrCreate() (*model.CounterRecord, error) {
	if result, err := c.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.CounterRecord), nil
	}
}

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

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

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

func (c counterRecordDo) Scan(result interface{}) (err error) {
	return c.DO.Scan(result)
}

func (c counterRecordDo) Delete(models ...*model.CounterRecord) (result gen.ResultInfo, err error) {
	return c.DO.Delete(models)
}

func (c *counterRecordDo) withDO(do gen.Dao) *counterRecordDo {
	c.DO = *do.(*gen.DO)
	return c
}
