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

	_workflowTrigger.workflowTriggerDo.UseDB(db, opts...)
	_workflowTrigger.workflowTriggerDo.UseModel(&model.WorkflowTrigger{})

	tableName := _workflowTrigger.workflowTriggerDo.TableName()
	_workflowTrigger.ALL = field.NewAsterisk(tableName)
	_workflowTrigger.ID = field.NewInt64(tableName, "id")
	_workflowTrigger.WorkflowID = field.NewInt64(tableName, "workflow_id")
	_workflowTrigger.Type = field.NewField(tableName, "type")
	_workflowTrigger.Rule = field.NewField(tableName, "rule")
	_workflowTrigger.Count_ = field.NewInt32(tableName, "count")
	_workflowTrigger.State = field.NewField(tableName, "state")
	_workflowTrigger.CreatedAt = field.NewTime(tableName, "created_at")
	_workflowTrigger.UpdatedAt = field.NewTime(tableName, "updated_at")

	_workflowTrigger.fillFieldMap()

	return _workflowTrigger
}

type workflowTrigger struct {
	workflowTriggerDo

	ALL        field.Asterisk
	ID         field.Int64
	WorkflowID field.Int64
	Type       field.Field
	Rule       field.Field
	Count_     field.Int32
	State      field.Field
	CreatedAt  field.Time
	UpdatedAt  field.Time

	fieldMap map[string]field.Expr
}

func (w workflowTrigger) Table(newTableName string) *workflowTrigger {
	w.workflowTriggerDo.UseTable(newTableName)
	return w.updateTableName(newTableName)
}

func (w workflowTrigger) As(alias string) *workflowTrigger {
	w.workflowTriggerDo.DO = *(w.workflowTriggerDo.As(alias).(*gen.DO))
	return w.updateTableName(alias)
}

func (w *workflowTrigger) updateTableName(table string) *workflowTrigger {
	w.ALL = field.NewAsterisk(table)
	w.ID = field.NewInt64(table, "id")
	w.WorkflowID = field.NewInt64(table, "workflow_id")
	w.Type = field.NewField(table, "type")
	w.Rule = field.NewField(table, "rule")
	w.Count_ = field.NewInt32(table, "count")
	w.State = field.NewField(table, "state")
	w.CreatedAt = field.NewTime(table, "created_at")
	w.UpdatedAt = field.NewTime(table, "updated_at")

	w.fillFieldMap()

	return w
}

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

func (w *workflowTrigger) fillFieldMap() {
	w.fieldMap = make(map[string]field.Expr, 8)
	w.fieldMap["id"] = w.ID
	w.fieldMap["workflow_id"] = w.WorkflowID
	w.fieldMap["type"] = w.Type
	w.fieldMap["rule"] = w.Rule
	w.fieldMap["count"] = w.Count_
	w.fieldMap["state"] = w.State
	w.fieldMap["created_at"] = w.CreatedAt
	w.fieldMap["updated_at"] = w.UpdatedAt
}

func (w workflowTrigger) clone(db *gorm.DB) workflowTrigger {
	w.workflowTriggerDo.ReplaceConnPool(db.Statement.ConnPool)
	return w
}

func (w workflowTrigger) replaceDB(db *gorm.DB) workflowTrigger {
	w.workflowTriggerDo.ReplaceDB(db)
	return w
}

type workflowTriggerDo struct{ gen.DO }

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

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

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

	return
}

func (w workflowTriggerDo) Debug() *workflowTriggerDo {
	return w.withDO(w.DO.Debug())
}

func (w workflowTriggerDo) WithContext(ctx context.Context) *workflowTriggerDo {
	return w.withDO(w.DO.WithContext(ctx))
}

func (w workflowTriggerDo) ReadDB() *workflowTriggerDo {
	return w.Clauses(dbresolver.Read)
}

func (w workflowTriggerDo) WriteDB() *workflowTriggerDo {
	return w.Clauses(dbresolver.Write)
}

func (w workflowTriggerDo) Session(config *gorm.Session) *workflowTriggerDo {
	return w.withDO(w.DO.Session(config))
}

func (w workflowTriggerDo) Clauses(conds ...clause.Expression) *workflowTriggerDo {
	return w.withDO(w.DO.Clauses(conds...))
}

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

func (w workflowTriggerDo) Not(conds ...gen.Condition) *workflowTriggerDo {
	return w.withDO(w.DO.Not(conds...))
}

func (w workflowTriggerDo) Or(conds ...gen.Condition) *workflowTriggerDo {
	return w.withDO(w.DO.Or(conds...))
}

func (w workflowTriggerDo) Select(conds ...field.Expr) *workflowTriggerDo {
	return w.withDO(w.DO.Select(conds...))
}

func (w workflowTriggerDo) Where(conds ...gen.Condition) *workflowTriggerDo {
	return w.withDO(w.DO.Where(conds...))
}

func (w workflowTriggerDo) Order(conds ...field.Expr) *workflowTriggerDo {
	return w.withDO(w.DO.Order(conds...))
}

func (w workflowTriggerDo) Distinct(cols ...field.Expr) *workflowTriggerDo {
	return w.withDO(w.DO.Distinct(cols...))
}

func (w workflowTriggerDo) Omit(cols ...field.Expr) *workflowTriggerDo {
	return w.withDO(w.DO.Omit(cols...))
}

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

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

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

func (w workflowTriggerDo) Group(cols ...field.Expr) *workflowTriggerDo {
	return w.withDO(w.DO.Group(cols...))
}

func (w workflowTriggerDo) Having(conds ...gen.Condition) *workflowTriggerDo {
	return w.withDO(w.DO.Having(conds...))
}

func (w workflowTriggerDo) Limit(limit int) *workflowTriggerDo {
	return w.withDO(w.DO.Limit(limit))
}

func (w workflowTriggerDo) Offset(offset int) *workflowTriggerDo {
	return w.withDO(w.DO.Offset(offset))
}

func (w workflowTriggerDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *workflowTriggerDo {
	return w.withDO(w.DO.Scopes(funcs...))
}

func (w workflowTriggerDo) Unscoped() *workflowTriggerDo {
	return w.withDO(w.DO.Unscoped())
}

func (w workflowTriggerDo) Create(values ...*model.WorkflowTrigger) error {
	if len(values) == 0 {
		return nil
	}
	return w.DO.Create(values)
}

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

func (w workflowTriggerDo) First() (*model.WorkflowTrigger, error) {
	if result, err := w.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.WorkflowTrigger), nil
	}
}

func (w workflowTriggerDo) Take() (*model.WorkflowTrigger, error) {
	if result, err := w.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.WorkflowTrigger), nil
	}
}

func (w workflowTriggerDo) Last() (*model.WorkflowTrigger, error) {
	if result, err := w.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.WorkflowTrigger), nil
	}
}

func (w workflowTriggerDo) Find() ([]*model.WorkflowTrigger, error) {
	result, err := w.DO.Find()
	return result.([]*model.WorkflowTrigger), err
}

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

func (w workflowTriggerDo) Attrs(attrs ...field.AssignExpr) *workflowTriggerDo {
	return w.withDO(w.DO.Attrs(attrs...))
}

func (w workflowTriggerDo) Assign(attrs ...field.AssignExpr) *workflowTriggerDo {
	return w.withDO(w.DO.Assign(attrs...))
}

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

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

func (w workflowTriggerDo) FirstOrInit() (*model.WorkflowTrigger, error) {
	if result, err := w.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.WorkflowTrigger), nil
	}
}

func (w workflowTriggerDo) FirstOrCreate() (*model.WorkflowTrigger, error) {
	if result, err := w.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.WorkflowTrigger), nil
	}
}

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

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

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

func (w workflowTriggerDo) Scan(result interface{}) (err error) {
	return w.DO.Scan(result)
}

func (w workflowTriggerDo) Delete(models ...*model.WorkflowTrigger) (result gen.ResultInfo, err error) {
	return w.DO.Delete(models)
}

func (w *workflowTriggerDo) withDO(do gen.Dao) *workflowTriggerDo {
	w.DO = *do.(*gen.DO)
	return w
}
