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

	_workflowScript.workflowScriptDo.UseDB(db, opts...)
	_workflowScript.workflowScriptDo.UseModel(&model.WorkflowScript{})

	tableName := _workflowScript.workflowScriptDo.TableName()
	_workflowScript.ALL = field.NewAsterisk(tableName)
	_workflowScript.ID = field.NewInt64(tableName, "id")
	_workflowScript.WorkflowID = field.NewInt64(tableName, "workflow_id")
	_workflowScript.Lang = field.NewField(tableName, "lang")
	_workflowScript.Code = field.NewString(tableName, "code")
	_workflowScript.Version = field.NewInt32(tableName, "version")
	_workflowScript.CreatedAt = field.NewTime(tableName, "created_at")
	_workflowScript.UpdatedAt = field.NewTime(tableName, "updated_at")

	_workflowScript.fillFieldMap()

	return _workflowScript
}

type workflowScript struct {
	workflowScriptDo

	ALL        field.Asterisk
	ID         field.Int64
	WorkflowID field.Int64
	Lang       field.Field
	Code       field.String
	Version    field.Int32
	CreatedAt  field.Time
	UpdatedAt  field.Time

	fieldMap map[string]field.Expr
}

func (w workflowScript) Table(newTableName string) *workflowScript {
	w.workflowScriptDo.UseTable(newTableName)
	return w.updateTableName(newTableName)
}

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

func (w *workflowScript) updateTableName(table string) *workflowScript {
	w.ALL = field.NewAsterisk(table)
	w.ID = field.NewInt64(table, "id")
	w.WorkflowID = field.NewInt64(table, "workflow_id")
	w.Lang = field.NewField(table, "lang")
	w.Code = field.NewString(table, "code")
	w.Version = field.NewInt32(table, "version")
	w.CreatedAt = field.NewTime(table, "created_at")
	w.UpdatedAt = field.NewTime(table, "updated_at")

	w.fillFieldMap()

	return w
}

func (w *workflowScript) 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 *workflowScript) fillFieldMap() {
	w.fieldMap = make(map[string]field.Expr, 7)
	w.fieldMap["id"] = w.ID
	w.fieldMap["workflow_id"] = w.WorkflowID
	w.fieldMap["lang"] = w.Lang
	w.fieldMap["code"] = w.Code
	w.fieldMap["version"] = w.Version
	w.fieldMap["created_at"] = w.CreatedAt
	w.fieldMap["updated_at"] = w.UpdatedAt
}

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

func (w workflowScript) replaceDB(db *gorm.DB) workflowScript {
	w.workflowScriptDo.ReplaceDB(db)
	return w
}

type workflowScriptDo struct{ gen.DO }

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

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("SELECT * FROM workflow_script 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 workflowScriptDo) TakeByUidAndTopic(uid string, topic string) (result model.WorkflowScript, 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 workflowScriptDo) FindByUidAndTopic(uid string, topic string) (result []*model.WorkflowScript, 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 workflowScriptDo) Debug() *workflowScriptDo {
	return w.withDO(w.DO.Debug())
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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