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

	_instruct.instructDo.UseDB(db, opts...)
	_instruct.instructDo.UseModel(&model.Instruct{})

	tableName := _instruct.instructDo.TableName()
	_instruct.ALL = field.NewAsterisk(tableName)
	_instruct.ID = field.NewInt64(tableName, "id")
	_instruct.No = field.NewString(tableName, "no")
	_instruct.UID = field.NewString(tableName, "uid")
	_instruct.Object = field.NewField(tableName, "object")
	_instruct.Bot = field.NewString(tableName, "bot")
	_instruct.Flag = field.NewString(tableName, "flag")
	_instruct.Content = field.NewField(tableName, "content")
	_instruct.Priority = field.NewField(tableName, "priority")
	_instruct.State = field.NewField(tableName, "state")
	_instruct.ExpireAt = field.NewTime(tableName, "expire_at")
	_instruct.CreatedAt = field.NewTime(tableName, "created_at")
	_instruct.UpdatedAt = field.NewTime(tableName, "updated_at")

	_instruct.fillFieldMap()

	return _instruct
}

type instruct struct {
	instructDo

	ALL       field.Asterisk
	ID        field.Int64
	No        field.String
	UID       field.String
	Object    field.Field
	Bot       field.String
	Flag      field.String
	Content   field.Field
	Priority  field.Field
	State     field.Field
	ExpireAt  field.Time
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (i instruct) Table(newTableName string) *instruct {
	i.instructDo.UseTable(newTableName)
	return i.updateTableName(newTableName)
}

func (i instruct) As(alias string) *instruct {
	i.instructDo.DO = *(i.instructDo.As(alias).(*gen.DO))
	return i.updateTableName(alias)
}

func (i *instruct) updateTableName(table string) *instruct {
	i.ALL = field.NewAsterisk(table)
	i.ID = field.NewInt64(table, "id")
	i.No = field.NewString(table, "no")
	i.UID = field.NewString(table, "uid")
	i.Object = field.NewField(table, "object")
	i.Bot = field.NewString(table, "bot")
	i.Flag = field.NewString(table, "flag")
	i.Content = field.NewField(table, "content")
	i.Priority = field.NewField(table, "priority")
	i.State = field.NewField(table, "state")
	i.ExpireAt = field.NewTime(table, "expire_at")
	i.CreatedAt = field.NewTime(table, "created_at")
	i.UpdatedAt = field.NewTime(table, "updated_at")

	i.fillFieldMap()

	return i
}

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

func (i *instruct) fillFieldMap() {
	i.fieldMap = make(map[string]field.Expr, 12)
	i.fieldMap["id"] = i.ID
	i.fieldMap["no"] = i.No
	i.fieldMap["uid"] = i.UID
	i.fieldMap["object"] = i.Object
	i.fieldMap["bot"] = i.Bot
	i.fieldMap["flag"] = i.Flag
	i.fieldMap["content"] = i.Content
	i.fieldMap["priority"] = i.Priority
	i.fieldMap["state"] = i.State
	i.fieldMap["expire_at"] = i.ExpireAt
	i.fieldMap["created_at"] = i.CreatedAt
	i.fieldMap["updated_at"] = i.UpdatedAt
}

func (i instruct) clone(db *gorm.DB) instruct {
	i.instructDo.ReplaceConnPool(db.Statement.ConnPool)
	return i
}

func (i instruct) replaceDB(db *gorm.DB) instruct {
	i.instructDo.ReplaceDB(db)
	return i
}

type instructDo struct{ gen.DO }

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

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

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

	return
}

func (i instructDo) Debug() *instructDo {
	return i.withDO(i.DO.Debug())
}

func (i instructDo) WithContext(ctx context.Context) *instructDo {
	return i.withDO(i.DO.WithContext(ctx))
}

func (i instructDo) ReadDB() *instructDo {
	return i.Clauses(dbresolver.Read)
}

func (i instructDo) WriteDB() *instructDo {
	return i.Clauses(dbresolver.Write)
}

func (i instructDo) Session(config *gorm.Session) *instructDo {
	return i.withDO(i.DO.Session(config))
}

func (i instructDo) Clauses(conds ...clause.Expression) *instructDo {
	return i.withDO(i.DO.Clauses(conds...))
}

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

func (i instructDo) Not(conds ...gen.Condition) *instructDo {
	return i.withDO(i.DO.Not(conds...))
}

func (i instructDo) Or(conds ...gen.Condition) *instructDo {
	return i.withDO(i.DO.Or(conds...))
}

func (i instructDo) Select(conds ...field.Expr) *instructDo {
	return i.withDO(i.DO.Select(conds...))
}

func (i instructDo) Where(conds ...gen.Condition) *instructDo {
	return i.withDO(i.DO.Where(conds...))
}

func (i instructDo) Order(conds ...field.Expr) *instructDo {
	return i.withDO(i.DO.Order(conds...))
}

func (i instructDo) Distinct(cols ...field.Expr) *instructDo {
	return i.withDO(i.DO.Distinct(cols...))
}

func (i instructDo) Omit(cols ...field.Expr) *instructDo {
	return i.withDO(i.DO.Omit(cols...))
}

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

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

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

func (i instructDo) Group(cols ...field.Expr) *instructDo {
	return i.withDO(i.DO.Group(cols...))
}

func (i instructDo) Having(conds ...gen.Condition) *instructDo {
	return i.withDO(i.DO.Having(conds...))
}

func (i instructDo) Limit(limit int) *instructDo {
	return i.withDO(i.DO.Limit(limit))
}

func (i instructDo) Offset(offset int) *instructDo {
	return i.withDO(i.DO.Offset(offset))
}

func (i instructDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *instructDo {
	return i.withDO(i.DO.Scopes(funcs...))
}

func (i instructDo) Unscoped() *instructDo {
	return i.withDO(i.DO.Unscoped())
}

func (i instructDo) Create(values ...*model.Instruct) error {
	if len(values) == 0 {
		return nil
	}
	return i.DO.Create(values)
}

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

func (i instructDo) First() (*model.Instruct, error) {
	if result, err := i.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.Instruct), nil
	}
}

func (i instructDo) Take() (*model.Instruct, error) {
	if result, err := i.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.Instruct), nil
	}
}

func (i instructDo) Last() (*model.Instruct, error) {
	if result, err := i.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.Instruct), nil
	}
}

func (i instructDo) Find() ([]*model.Instruct, error) {
	result, err := i.DO.Find()
	return result.([]*model.Instruct), err
}

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

func (i instructDo) Attrs(attrs ...field.AssignExpr) *instructDo {
	return i.withDO(i.DO.Attrs(attrs...))
}

func (i instructDo) Assign(attrs ...field.AssignExpr) *instructDo {
	return i.withDO(i.DO.Assign(attrs...))
}

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

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

func (i instructDo) FirstOrInit() (*model.Instruct, error) {
	if result, err := i.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.Instruct), nil
	}
}

func (i instructDo) FirstOrCreate() (*model.Instruct, error) {
	if result, err := i.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.Instruct), nil
	}
}

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

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

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

func (i instructDo) Scan(result interface{}) (err error) {
	return i.DO.Scan(result)
}

func (i instructDo) Delete(models ...*model.Instruct) (result gen.ResultInfo, err error) {
	return i.DO.Delete(models)
}

func (i *instructDo) withDO(do gen.Dao) *instructDo {
	i.DO = *do.(*gen.DO)
	return i
}
