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

	_reviewEvaluation.reviewEvaluationDo.UseDB(db, opts...)
	_reviewEvaluation.reviewEvaluationDo.UseModel(&model.ReviewEvaluation{})

	tableName := _reviewEvaluation.reviewEvaluationDo.TableName()
	_reviewEvaluation.ALL = field.NewAsterisk(tableName)
	_reviewEvaluation.ID = field.NewInt64(tableName, "id")
	_reviewEvaluation.UID = field.NewString(tableName, "uid")
	_reviewEvaluation.Topic = field.NewString(tableName, "topic")
	_reviewEvaluation.ReviewID = field.NewInt64(tableName, "review_id")
	_reviewEvaluation.Question = field.NewString(tableName, "question")
	_reviewEvaluation.Reason = field.NewString(tableName, "reason")
	_reviewEvaluation.Solving = field.NewString(tableName, "solving")
	_reviewEvaluation.CreatedAt = field.NewTime(tableName, "created_at")
	_reviewEvaluation.UpdatedAt = field.NewTime(tableName, "updated_at")

	_reviewEvaluation.fillFieldMap()

	return _reviewEvaluation
}

type reviewEvaluation struct {
	reviewEvaluationDo

	ALL       field.Asterisk
	ID        field.Int64
	UID       field.String
	Topic     field.String
	ReviewID  field.Int64
	Question  field.String
	Reason    field.String
	Solving   field.String
	CreatedAt field.Time
	UpdatedAt field.Time

	fieldMap map[string]field.Expr
}

func (r reviewEvaluation) Table(newTableName string) *reviewEvaluation {
	r.reviewEvaluationDo.UseTable(newTableName)
	return r.updateTableName(newTableName)
}

func (r reviewEvaluation) As(alias string) *reviewEvaluation {
	r.reviewEvaluationDo.DO = *(r.reviewEvaluationDo.As(alias).(*gen.DO))
	return r.updateTableName(alias)
}

func (r *reviewEvaluation) updateTableName(table string) *reviewEvaluation {
	r.ALL = field.NewAsterisk(table)
	r.ID = field.NewInt64(table, "id")
	r.UID = field.NewString(table, "uid")
	r.Topic = field.NewString(table, "topic")
	r.ReviewID = field.NewInt64(table, "review_id")
	r.Question = field.NewString(table, "question")
	r.Reason = field.NewString(table, "reason")
	r.Solving = field.NewString(table, "solving")
	r.CreatedAt = field.NewTime(table, "created_at")
	r.UpdatedAt = field.NewTime(table, "updated_at")

	r.fillFieldMap()

	return r
}

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

func (r *reviewEvaluation) fillFieldMap() {
	r.fieldMap = make(map[string]field.Expr, 9)
	r.fieldMap["id"] = r.ID
	r.fieldMap["uid"] = r.UID
	r.fieldMap["topic"] = r.Topic
	r.fieldMap["review_id"] = r.ReviewID
	r.fieldMap["question"] = r.Question
	r.fieldMap["reason"] = r.Reason
	r.fieldMap["solving"] = r.Solving
	r.fieldMap["created_at"] = r.CreatedAt
	r.fieldMap["updated_at"] = r.UpdatedAt
}

func (r reviewEvaluation) clone(db *gorm.DB) reviewEvaluation {
	r.reviewEvaluationDo.ReplaceConnPool(db.Statement.ConnPool)
	return r
}

func (r reviewEvaluation) replaceDB(db *gorm.DB) reviewEvaluation {
	r.reviewEvaluationDo.ReplaceDB(db)
	return r
}

type reviewEvaluationDo struct{ gen.DO }

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

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

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

	return
}

func (r reviewEvaluationDo) Debug() *reviewEvaluationDo {
	return r.withDO(r.DO.Debug())
}

func (r reviewEvaluationDo) WithContext(ctx context.Context) *reviewEvaluationDo {
	return r.withDO(r.DO.WithContext(ctx))
}

func (r reviewEvaluationDo) ReadDB() *reviewEvaluationDo {
	return r.Clauses(dbresolver.Read)
}

func (r reviewEvaluationDo) WriteDB() *reviewEvaluationDo {
	return r.Clauses(dbresolver.Write)
}

func (r reviewEvaluationDo) Session(config *gorm.Session) *reviewEvaluationDo {
	return r.withDO(r.DO.Session(config))
}

func (r reviewEvaluationDo) Clauses(conds ...clause.Expression) *reviewEvaluationDo {
	return r.withDO(r.DO.Clauses(conds...))
}

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

func (r reviewEvaluationDo) Not(conds ...gen.Condition) *reviewEvaluationDo {
	return r.withDO(r.DO.Not(conds...))
}

func (r reviewEvaluationDo) Or(conds ...gen.Condition) *reviewEvaluationDo {
	return r.withDO(r.DO.Or(conds...))
}

func (r reviewEvaluationDo) Select(conds ...field.Expr) *reviewEvaluationDo {
	return r.withDO(r.DO.Select(conds...))
}

func (r reviewEvaluationDo) Where(conds ...gen.Condition) *reviewEvaluationDo {
	return r.withDO(r.DO.Where(conds...))
}

func (r reviewEvaluationDo) Order(conds ...field.Expr) *reviewEvaluationDo {
	return r.withDO(r.DO.Order(conds...))
}

func (r reviewEvaluationDo) Distinct(cols ...field.Expr) *reviewEvaluationDo {
	return r.withDO(r.DO.Distinct(cols...))
}

func (r reviewEvaluationDo) Omit(cols ...field.Expr) *reviewEvaluationDo {
	return r.withDO(r.DO.Omit(cols...))
}

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

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

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

func (r reviewEvaluationDo) Group(cols ...field.Expr) *reviewEvaluationDo {
	return r.withDO(r.DO.Group(cols...))
}

func (r reviewEvaluationDo) Having(conds ...gen.Condition) *reviewEvaluationDo {
	return r.withDO(r.DO.Having(conds...))
}

func (r reviewEvaluationDo) Limit(limit int) *reviewEvaluationDo {
	return r.withDO(r.DO.Limit(limit))
}

func (r reviewEvaluationDo) Offset(offset int) *reviewEvaluationDo {
	return r.withDO(r.DO.Offset(offset))
}

func (r reviewEvaluationDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *reviewEvaluationDo {
	return r.withDO(r.DO.Scopes(funcs...))
}

func (r reviewEvaluationDo) Unscoped() *reviewEvaluationDo {
	return r.withDO(r.DO.Unscoped())
}

func (r reviewEvaluationDo) Create(values ...*model.ReviewEvaluation) error {
	if len(values) == 0 {
		return nil
	}
	return r.DO.Create(values)
}

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

func (r reviewEvaluationDo) First() (*model.ReviewEvaluation, error) {
	if result, err := r.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ReviewEvaluation), nil
	}
}

func (r reviewEvaluationDo) Take() (*model.ReviewEvaluation, error) {
	if result, err := r.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ReviewEvaluation), nil
	}
}

func (r reviewEvaluationDo) Last() (*model.ReviewEvaluation, error) {
	if result, err := r.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ReviewEvaluation), nil
	}
}

func (r reviewEvaluationDo) Find() ([]*model.ReviewEvaluation, error) {
	result, err := r.DO.Find()
	return result.([]*model.ReviewEvaluation), err
}

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

func (r reviewEvaluationDo) Attrs(attrs ...field.AssignExpr) *reviewEvaluationDo {
	return r.withDO(r.DO.Attrs(attrs...))
}

func (r reviewEvaluationDo) Assign(attrs ...field.AssignExpr) *reviewEvaluationDo {
	return r.withDO(r.DO.Assign(attrs...))
}

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

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

func (r reviewEvaluationDo) FirstOrInit() (*model.ReviewEvaluation, error) {
	if result, err := r.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ReviewEvaluation), nil
	}
}

func (r reviewEvaluationDo) FirstOrCreate() (*model.ReviewEvaluation, error) {
	if result, err := r.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ReviewEvaluation), nil
	}
}

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

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

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

func (r reviewEvaluationDo) Scan(result interface{}) (err error) {
	return r.DO.Scan(result)
}

func (r reviewEvaluationDo) Delete(models ...*model.ReviewEvaluation) (result gen.ResultInfo, err error) {
	return r.DO.Delete(models)
}

func (r *reviewEvaluationDo) withDO(do gen.Dao) *reviewEvaluationDo {
	r.DO = *do.(*gen.DO)
	return r
}
