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

	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"gorm.io/gorm/schema"

	"gorm.io/gen"
	"gorm.io/gen/field"

	"gorm.io/plugin/dbresolver"

	"wechat-api/database/dao/wechat/model"
)

func newFormRecord(db *gorm.DB, opts ...gen.DOOption) formRecord {
	_formRecord := formRecord{}

	_formRecord.formRecordDo.UseDB(db, opts...)
	_formRecord.formRecordDo.UseModel(&model.FormRecord{})

	tableName := _formRecord.formRecordDo.TableName()
	_formRecord.ALL = field.NewAsterisk(tableName)
	_formRecord.ID = field.NewString(tableName, "id")
	_formRecord.ExternalID = field.NewString(tableName, "external_id")
	_formRecord.IndustryType = field.NewInt64(tableName, "industry_type")
	_formRecord.ChatHistory = field.NewString(tableName, "chat_history")
	_formRecord.FormData = field.NewString(tableName, "form_data")
	_formRecord.FormValue = field.NewString(tableName, "form_value")
	_formRecord.OrgID = field.NewInt64(tableName, "org_id")
	_formRecord.Status = field.NewInt64(tableName, "status")
	_formRecord.RequestData = field.NewString(tableName, "request_data")
	_formRecord.ResponseData = field.NewString(tableName, "response_data")
	_formRecord.CreatedAt = field.NewTime(tableName, "created_at")
	_formRecord.UpdatedAt = field.NewTime(tableName, "updated_at")

	_formRecord.fillFieldMap()

	return _formRecord
}

type formRecord struct {
	formRecordDo

	ALL          field.Asterisk
	ID           field.String // 主键id
	ExternalID   field.String // 外部id
	IndustryType field.Int64  // 评分规则代码 0 通用 1 教育
	ChatHistory  field.String // 通话记录
	FormData     field.String // 表单数据
	FormValue    field.String // 表单结果
	OrgID        field.Int64  // 机构 ID
	Status       field.Int64  // 状态 1 入库 2 已判断 3 已回调
	RequestData  field.String
	ResponseData field.String
	CreatedAt    field.Time // Create Time | 创建日期
	UpdatedAt    field.Time // Update Time | 修改日期

	fieldMap map[string]field.Expr
}

func (f formRecord) Table(newTableName string) *formRecord {
	f.formRecordDo.UseTable(newTableName)
	return f.updateTableName(newTableName)
}

func (f formRecord) As(alias string) *formRecord {
	f.formRecordDo.DO = *(f.formRecordDo.As(alias).(*gen.DO))
	return f.updateTableName(alias)
}

func (f *formRecord) updateTableName(table string) *formRecord {
	f.ALL = field.NewAsterisk(table)
	f.ID = field.NewString(table, "id")
	f.ExternalID = field.NewString(table, "external_id")
	f.IndustryType = field.NewInt64(table, "industry_type")
	f.ChatHistory = field.NewString(table, "chat_history")
	f.FormData = field.NewString(table, "form_data")
	f.FormValue = field.NewString(table, "form_value")
	f.OrgID = field.NewInt64(table, "org_id")
	f.Status = field.NewInt64(table, "status")
	f.RequestData = field.NewString(table, "request_data")
	f.ResponseData = field.NewString(table, "response_data")
	f.CreatedAt = field.NewTime(table, "created_at")
	f.UpdatedAt = field.NewTime(table, "updated_at")

	f.fillFieldMap()

	return f
}

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

func (f *formRecord) fillFieldMap() {
	f.fieldMap = make(map[string]field.Expr, 12)
	f.fieldMap["id"] = f.ID
	f.fieldMap["external_id"] = f.ExternalID
	f.fieldMap["industry_type"] = f.IndustryType
	f.fieldMap["chat_history"] = f.ChatHistory
	f.fieldMap["form_data"] = f.FormData
	f.fieldMap["form_value"] = f.FormValue
	f.fieldMap["org_id"] = f.OrgID
	f.fieldMap["status"] = f.Status
	f.fieldMap["request_data"] = f.RequestData
	f.fieldMap["response_data"] = f.ResponseData
	f.fieldMap["created_at"] = f.CreatedAt
	f.fieldMap["updated_at"] = f.UpdatedAt
}

func (f formRecord) clone(db *gorm.DB) formRecord {
	f.formRecordDo.ReplaceConnPool(db.Statement.ConnPool)
	return f
}

func (f formRecord) replaceDB(db *gorm.DB) formRecord {
	f.formRecordDo.ReplaceDB(db)
	return f
}

type formRecordDo struct{ gen.DO }

type IFormRecordDo interface {
	gen.SubQuery
	Debug() IFormRecordDo
	WithContext(ctx context.Context) IFormRecordDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IFormRecordDo
	WriteDB() IFormRecordDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IFormRecordDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IFormRecordDo
	Not(conds ...gen.Condition) IFormRecordDo
	Or(conds ...gen.Condition) IFormRecordDo
	Select(conds ...field.Expr) IFormRecordDo
	Where(conds ...gen.Condition) IFormRecordDo
	Order(conds ...field.Expr) IFormRecordDo
	Distinct(cols ...field.Expr) IFormRecordDo
	Omit(cols ...field.Expr) IFormRecordDo
	Join(table schema.Tabler, on ...field.Expr) IFormRecordDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IFormRecordDo
	RightJoin(table schema.Tabler, on ...field.Expr) IFormRecordDo
	Group(cols ...field.Expr) IFormRecordDo
	Having(conds ...gen.Condition) IFormRecordDo
	Limit(limit int) IFormRecordDo
	Offset(offset int) IFormRecordDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IFormRecordDo
	Unscoped() IFormRecordDo
	Create(values ...*model.FormRecord) error
	CreateInBatches(values []*model.FormRecord, batchSize int) error
	Save(values ...*model.FormRecord) error
	First() (*model.FormRecord, error)
	Take() (*model.FormRecord, error)
	Last() (*model.FormRecord, error)
	Find() ([]*model.FormRecord, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.FormRecord, err error)
	FindInBatches(result *[]*model.FormRecord, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.FormRecord) (info gen.ResultInfo, err error)
	Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	Updates(value interface{}) (info gen.ResultInfo, err error)
	UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
	UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
	UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
	UpdateFrom(q gen.SubQuery) gen.Dao
	Attrs(attrs ...field.AssignExpr) IFormRecordDo
	Assign(attrs ...field.AssignExpr) IFormRecordDo
	Joins(fields ...field.RelationField) IFormRecordDo
	Preload(fields ...field.RelationField) IFormRecordDo
	FirstOrInit() (*model.FormRecord, error)
	FirstOrCreate() (*model.FormRecord, error)
	FindByPage(offset int, limit int) (result []*model.FormRecord, count int64, err error)
	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
	Scan(result interface{}) (err error)
	Returning(value interface{}, columns ...string) IFormRecordDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (f formRecordDo) Debug() IFormRecordDo {
	return f.withDO(f.DO.Debug())
}

func (f formRecordDo) WithContext(ctx context.Context) IFormRecordDo {
	return f.withDO(f.DO.WithContext(ctx))
}

func (f formRecordDo) ReadDB() IFormRecordDo {
	return f.Clauses(dbresolver.Read)
}

func (f formRecordDo) WriteDB() IFormRecordDo {
	return f.Clauses(dbresolver.Write)
}

func (f formRecordDo) Session(config *gorm.Session) IFormRecordDo {
	return f.withDO(f.DO.Session(config))
}

func (f formRecordDo) Clauses(conds ...clause.Expression) IFormRecordDo {
	return f.withDO(f.DO.Clauses(conds...))
}

func (f formRecordDo) Returning(value interface{}, columns ...string) IFormRecordDo {
	return f.withDO(f.DO.Returning(value, columns...))
}

func (f formRecordDo) Not(conds ...gen.Condition) IFormRecordDo {
	return f.withDO(f.DO.Not(conds...))
}

func (f formRecordDo) Or(conds ...gen.Condition) IFormRecordDo {
	return f.withDO(f.DO.Or(conds...))
}

func (f formRecordDo) Select(conds ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.Select(conds...))
}

func (f formRecordDo) Where(conds ...gen.Condition) IFormRecordDo {
	return f.withDO(f.DO.Where(conds...))
}

func (f formRecordDo) Order(conds ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.Order(conds...))
}

func (f formRecordDo) Distinct(cols ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.Distinct(cols...))
}

func (f formRecordDo) Omit(cols ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.Omit(cols...))
}

func (f formRecordDo) Join(table schema.Tabler, on ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.Join(table, on...))
}

func (f formRecordDo) LeftJoin(table schema.Tabler, on ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.LeftJoin(table, on...))
}

func (f formRecordDo) RightJoin(table schema.Tabler, on ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.RightJoin(table, on...))
}

func (f formRecordDo) Group(cols ...field.Expr) IFormRecordDo {
	return f.withDO(f.DO.Group(cols...))
}

func (f formRecordDo) Having(conds ...gen.Condition) IFormRecordDo {
	return f.withDO(f.DO.Having(conds...))
}

func (f formRecordDo) Limit(limit int) IFormRecordDo {
	return f.withDO(f.DO.Limit(limit))
}

func (f formRecordDo) Offset(offset int) IFormRecordDo {
	return f.withDO(f.DO.Offset(offset))
}

func (f formRecordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IFormRecordDo {
	return f.withDO(f.DO.Scopes(funcs...))
}

func (f formRecordDo) Unscoped() IFormRecordDo {
	return f.withDO(f.DO.Unscoped())
}

func (f formRecordDo) Create(values ...*model.FormRecord) error {
	if len(values) == 0 {
		return nil
	}
	return f.DO.Create(values)
}

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

func (f formRecordDo) First() (*model.FormRecord, error) {
	if result, err := f.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.FormRecord), nil
	}
}

func (f formRecordDo) Take() (*model.FormRecord, error) {
	if result, err := f.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.FormRecord), nil
	}
}

func (f formRecordDo) Last() (*model.FormRecord, error) {
	if result, err := f.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.FormRecord), nil
	}
}

func (f formRecordDo) Find() ([]*model.FormRecord, error) {
	result, err := f.DO.Find()
	return result.([]*model.FormRecord), err
}

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

func (f formRecordDo) Attrs(attrs ...field.AssignExpr) IFormRecordDo {
	return f.withDO(f.DO.Attrs(attrs...))
}

func (f formRecordDo) Assign(attrs ...field.AssignExpr) IFormRecordDo {
	return f.withDO(f.DO.Assign(attrs...))
}

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

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

func (f formRecordDo) FirstOrInit() (*model.FormRecord, error) {
	if result, err := f.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.FormRecord), nil
	}
}

func (f formRecordDo) FirstOrCreate() (*model.FormRecord, error) {
	if result, err := f.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.FormRecord), nil
	}
}

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

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

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

func (f formRecordDo) Scan(result interface{}) (err error) {
	return f.DO.Scan(result)
}

func (f formRecordDo) Delete(models ...*model.FormRecord) (result gen.ResultInfo, err error) {
	return f.DO.Delete(models)
}

func (f *formRecordDo) withDO(do gen.Dao) *formRecordDo {
	f.DO = *do.(*gen.DO)
	return f
}
