// 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 query

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/0xJacky/Nginx-UI/model"
)

func newChatGPTLog(db *gorm.DB, opts ...gen.DOOption) chatGPTLog {
	_chatGPTLog := chatGPTLog{}

	_chatGPTLog.chatGPTLogDo.UseDB(db, opts...)
	_chatGPTLog.chatGPTLogDo.UseModel(&model.ChatGPTLog{})

	tableName := _chatGPTLog.chatGPTLogDo.TableName()
	_chatGPTLog.ALL = field.NewAsterisk(tableName)
	_chatGPTLog.Name = field.NewString(tableName, "name")
	_chatGPTLog.Content = field.NewField(tableName, "content")

	_chatGPTLog.fillFieldMap()

	return _chatGPTLog
}

type chatGPTLog struct {
	chatGPTLogDo

	ALL     field.Asterisk
	Name    field.String
	Content field.Field

	fieldMap map[string]field.Expr
}

func (c chatGPTLog) Table(newTableName string) *chatGPTLog {
	c.chatGPTLogDo.UseTable(newTableName)
	return c.updateTableName(newTableName)
}

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

func (c *chatGPTLog) updateTableName(table string) *chatGPTLog {
	c.ALL = field.NewAsterisk(table)
	c.Name = field.NewString(table, "name")
	c.Content = field.NewField(table, "content")

	c.fillFieldMap()

	return c
}

func (c *chatGPTLog) 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 *chatGPTLog) fillFieldMap() {
	c.fieldMap = make(map[string]field.Expr, 2)
	c.fieldMap["name"] = c.Name
	c.fieldMap["content"] = c.Content
}

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

func (c chatGPTLog) replaceDB(db *gorm.DB) chatGPTLog {
	c.chatGPTLogDo.ReplaceDB(db)
	return c
}

type chatGPTLogDo struct{ gen.DO }

// FirstByID Where("id=@id")
func (c chatGPTLogDo) FirstByID(id int) (result *model.ChatGPTLog, err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("id=? ")

	var executeSQL *gorm.DB
	executeSQL = c.UnderlyingDB().Where(generateSQL.String(), params...).Take(&result) // ignore_security_alert
	err = executeSQL.Error

	return
}

// DeleteByID update @@table set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=@id
func (c chatGPTLogDo) DeleteByID(id int) (err error) {
	var params []interface{}

	var generateSQL strings.Builder
	params = append(params, id)
	generateSQL.WriteString("update chat_gpt_logs set deleted_at=strftime('%Y-%m-%d %H:%M:%S','now') where id=? ")

	var executeSQL *gorm.DB
	executeSQL = c.UnderlyingDB().Exec(generateSQL.String(), params...) // ignore_security_alert
	err = executeSQL.Error

	return
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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