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

	_messageRecord.messageRecordDo.UseDB(db, opts...)
	_messageRecord.messageRecordDo.UseModel(&model.MessageRecord{})

	tableName := _messageRecord.messageRecordDo.TableName()
	_messageRecord.ALL = field.NewAsterisk(tableName)
	_messageRecord.ID = field.NewInt64(tableName, "id")
	_messageRecord.CreatedAt = field.NewTime(tableName, "created_at")
	_messageRecord.UpdatedAt = field.NewTime(tableName, "updated_at")
	_messageRecord.Status = field.NewInt64(tableName, "status")
	_messageRecord.DeletedAt = field.NewField(tableName, "deleted_at")
	_messageRecord.BotWxid = field.NewString(tableName, "bot_wxid")
	_messageRecord.ContactID = field.NewInt64(tableName, "contact_id")
	_messageRecord.ContactType = field.NewInt64(tableName, "contact_type")
	_messageRecord.ContactWxid = field.NewString(tableName, "contact_wxid")
	_messageRecord.ContentType = field.NewInt64(tableName, "content_type")
	_messageRecord.Content = field.NewString(tableName, "content")
	_messageRecord.Meta = field.NewString(tableName, "meta")
	_messageRecord.ErrorDetail = field.NewString(tableName, "error_detail")
	_messageRecord.SendTime = field.NewTime(tableName, "send_time")
	_messageRecord.SourceType = field.NewInt64(tableName, "source_type")
	_messageRecord.SourceID = field.NewInt64(tableName, "source_id")
	_messageRecord.SubSourceID = field.NewInt64(tableName, "sub_source_id")
	_messageRecord.OrganizationID = field.NewInt64(tableName, "organization_id")

	_messageRecord.fillFieldMap()

	return _messageRecord
}

type messageRecord struct {
	messageRecordDo

	ALL            field.Asterisk
	ID             field.Int64
	CreatedAt      field.Time   // Create Time | 创建日期
	UpdatedAt      field.Time   // Update Time | 修改日期
	Status         field.Int64  // 发送状态：1 未发送  2  发送中  3 发送成功 4 发送失败 ….(待定)
	DeletedAt      field.Field  // Delete Time | 删除日期
	BotWxid        field.String // 机器人微信 id
	ContactID      field.Int64  // 联系人 id
	ContactType    field.Int64  // 类型：1好友，2群组，3企业微信联系人
	ContactWxid    field.String // 接收方微信 id
	ContentType    field.Int64  // 内容类型 1 文本 2 文件
	Content        field.String // 发送内容
	Meta           field.String // 元数据（如文件名）
	ErrorDetail    field.String // 异常原因
	SendTime       field.Time   // 发送时间
	SourceType     field.Int64  // 源类型 1 点发 2 群发 3 SOP 阶段 4 SOP 节点
	SourceID       field.Int64  // 源 ID
	SubSourceID    field.Int64  // 次源 ID
	OrganizationID field.Int64  // 机构 ID

	fieldMap map[string]field.Expr
}

func (m messageRecord) Table(newTableName string) *messageRecord {
	m.messageRecordDo.UseTable(newTableName)
	return m.updateTableName(newTableName)
}

func (m messageRecord) As(alias string) *messageRecord {
	m.messageRecordDo.DO = *(m.messageRecordDo.As(alias).(*gen.DO))
	return m.updateTableName(alias)
}

func (m *messageRecord) updateTableName(table string) *messageRecord {
	m.ALL = field.NewAsterisk(table)
	m.ID = field.NewInt64(table, "id")
	m.CreatedAt = field.NewTime(table, "created_at")
	m.UpdatedAt = field.NewTime(table, "updated_at")
	m.Status = field.NewInt64(table, "status")
	m.DeletedAt = field.NewField(table, "deleted_at")
	m.BotWxid = field.NewString(table, "bot_wxid")
	m.ContactID = field.NewInt64(table, "contact_id")
	m.ContactType = field.NewInt64(table, "contact_type")
	m.ContactWxid = field.NewString(table, "contact_wxid")
	m.ContentType = field.NewInt64(table, "content_type")
	m.Content = field.NewString(table, "content")
	m.Meta = field.NewString(table, "meta")
	m.ErrorDetail = field.NewString(table, "error_detail")
	m.SendTime = field.NewTime(table, "send_time")
	m.SourceType = field.NewInt64(table, "source_type")
	m.SourceID = field.NewInt64(table, "source_id")
	m.SubSourceID = field.NewInt64(table, "sub_source_id")
	m.OrganizationID = field.NewInt64(table, "organization_id")

	m.fillFieldMap()

	return m
}

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

func (m *messageRecord) fillFieldMap() {
	m.fieldMap = make(map[string]field.Expr, 18)
	m.fieldMap["id"] = m.ID
	m.fieldMap["created_at"] = m.CreatedAt
	m.fieldMap["updated_at"] = m.UpdatedAt
	m.fieldMap["status"] = m.Status
	m.fieldMap["deleted_at"] = m.DeletedAt
	m.fieldMap["bot_wxid"] = m.BotWxid
	m.fieldMap["contact_id"] = m.ContactID
	m.fieldMap["contact_type"] = m.ContactType
	m.fieldMap["contact_wxid"] = m.ContactWxid
	m.fieldMap["content_type"] = m.ContentType
	m.fieldMap["content"] = m.Content
	m.fieldMap["meta"] = m.Meta
	m.fieldMap["error_detail"] = m.ErrorDetail
	m.fieldMap["send_time"] = m.SendTime
	m.fieldMap["source_type"] = m.SourceType
	m.fieldMap["source_id"] = m.SourceID
	m.fieldMap["sub_source_id"] = m.SubSourceID
	m.fieldMap["organization_id"] = m.OrganizationID
}

func (m messageRecord) clone(db *gorm.DB) messageRecord {
	m.messageRecordDo.ReplaceConnPool(db.Statement.ConnPool)
	return m
}

func (m messageRecord) replaceDB(db *gorm.DB) messageRecord {
	m.messageRecordDo.ReplaceDB(db)
	return m
}

type messageRecordDo struct{ gen.DO }

type IMessageRecordDo interface {
	gen.SubQuery
	Debug() IMessageRecordDo
	WithContext(ctx context.Context) IMessageRecordDo
	WithResult(fc func(tx gen.Dao)) gen.ResultInfo
	ReplaceDB(db *gorm.DB)
	ReadDB() IMessageRecordDo
	WriteDB() IMessageRecordDo
	As(alias string) gen.Dao
	Session(config *gorm.Session) IMessageRecordDo
	Columns(cols ...field.Expr) gen.Columns
	Clauses(conds ...clause.Expression) IMessageRecordDo
	Not(conds ...gen.Condition) IMessageRecordDo
	Or(conds ...gen.Condition) IMessageRecordDo
	Select(conds ...field.Expr) IMessageRecordDo
	Where(conds ...gen.Condition) IMessageRecordDo
	Order(conds ...field.Expr) IMessageRecordDo
	Distinct(cols ...field.Expr) IMessageRecordDo
	Omit(cols ...field.Expr) IMessageRecordDo
	Join(table schema.Tabler, on ...field.Expr) IMessageRecordDo
	LeftJoin(table schema.Tabler, on ...field.Expr) IMessageRecordDo
	RightJoin(table schema.Tabler, on ...field.Expr) IMessageRecordDo
	Group(cols ...field.Expr) IMessageRecordDo
	Having(conds ...gen.Condition) IMessageRecordDo
	Limit(limit int) IMessageRecordDo
	Offset(offset int) IMessageRecordDo
	Count() (count int64, err error)
	Scopes(funcs ...func(gen.Dao) gen.Dao) IMessageRecordDo
	Unscoped() IMessageRecordDo
	Create(values ...*model.MessageRecord) error
	CreateInBatches(values []*model.MessageRecord, batchSize int) error
	Save(values ...*model.MessageRecord) error
	First() (*model.MessageRecord, error)
	Take() (*model.MessageRecord, error)
	Last() (*model.MessageRecord, error)
	Find() ([]*model.MessageRecord, error)
	FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.MessageRecord, err error)
	FindInBatches(result *[]*model.MessageRecord, batchSize int, fc func(tx gen.Dao, batch int) error) error
	Pluck(column field.Expr, dest interface{}) error
	Delete(...*model.MessageRecord) (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) IMessageRecordDo
	Assign(attrs ...field.AssignExpr) IMessageRecordDo
	Joins(fields ...field.RelationField) IMessageRecordDo
	Preload(fields ...field.RelationField) IMessageRecordDo
	FirstOrInit() (*model.MessageRecord, error)
	FirstOrCreate() (*model.MessageRecord, error)
	FindByPage(offset int, limit int) (result []*model.MessageRecord, 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) IMessageRecordDo
	UnderlyingDB() *gorm.DB
	schema.Tabler
}

func (m messageRecordDo) Debug() IMessageRecordDo {
	return m.withDO(m.DO.Debug())
}

func (m messageRecordDo) WithContext(ctx context.Context) IMessageRecordDo {
	return m.withDO(m.DO.WithContext(ctx))
}

func (m messageRecordDo) ReadDB() IMessageRecordDo {
	return m.Clauses(dbresolver.Read)
}

func (m messageRecordDo) WriteDB() IMessageRecordDo {
	return m.Clauses(dbresolver.Write)
}

func (m messageRecordDo) Session(config *gorm.Session) IMessageRecordDo {
	return m.withDO(m.DO.Session(config))
}

func (m messageRecordDo) Clauses(conds ...clause.Expression) IMessageRecordDo {
	return m.withDO(m.DO.Clauses(conds...))
}

func (m messageRecordDo) Returning(value interface{}, columns ...string) IMessageRecordDo {
	return m.withDO(m.DO.Returning(value, columns...))
}

func (m messageRecordDo) Not(conds ...gen.Condition) IMessageRecordDo {
	return m.withDO(m.DO.Not(conds...))
}

func (m messageRecordDo) Or(conds ...gen.Condition) IMessageRecordDo {
	return m.withDO(m.DO.Or(conds...))
}

func (m messageRecordDo) Select(conds ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.Select(conds...))
}

func (m messageRecordDo) Where(conds ...gen.Condition) IMessageRecordDo {
	return m.withDO(m.DO.Where(conds...))
}

func (m messageRecordDo) Order(conds ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.Order(conds...))
}

func (m messageRecordDo) Distinct(cols ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.Distinct(cols...))
}

func (m messageRecordDo) Omit(cols ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.Omit(cols...))
}

func (m messageRecordDo) Join(table schema.Tabler, on ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.Join(table, on...))
}

func (m messageRecordDo) LeftJoin(table schema.Tabler, on ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.LeftJoin(table, on...))
}

func (m messageRecordDo) RightJoin(table schema.Tabler, on ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.RightJoin(table, on...))
}

func (m messageRecordDo) Group(cols ...field.Expr) IMessageRecordDo {
	return m.withDO(m.DO.Group(cols...))
}

func (m messageRecordDo) Having(conds ...gen.Condition) IMessageRecordDo {
	return m.withDO(m.DO.Having(conds...))
}

func (m messageRecordDo) Limit(limit int) IMessageRecordDo {
	return m.withDO(m.DO.Limit(limit))
}

func (m messageRecordDo) Offset(offset int) IMessageRecordDo {
	return m.withDO(m.DO.Offset(offset))
}

func (m messageRecordDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMessageRecordDo {
	return m.withDO(m.DO.Scopes(funcs...))
}

func (m messageRecordDo) Unscoped() IMessageRecordDo {
	return m.withDO(m.DO.Unscoped())
}

func (m messageRecordDo) Create(values ...*model.MessageRecord) error {
	if len(values) == 0 {
		return nil
	}
	return m.DO.Create(values)
}

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

func (m messageRecordDo) First() (*model.MessageRecord, error) {
	if result, err := m.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.MessageRecord), nil
	}
}

func (m messageRecordDo) Take() (*model.MessageRecord, error) {
	if result, err := m.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.MessageRecord), nil
	}
}

func (m messageRecordDo) Last() (*model.MessageRecord, error) {
	if result, err := m.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.MessageRecord), nil
	}
}

func (m messageRecordDo) Find() ([]*model.MessageRecord, error) {
	result, err := m.DO.Find()
	return result.([]*model.MessageRecord), err
}

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

func (m messageRecordDo) Attrs(attrs ...field.AssignExpr) IMessageRecordDo {
	return m.withDO(m.DO.Attrs(attrs...))
}

func (m messageRecordDo) Assign(attrs ...field.AssignExpr) IMessageRecordDo {
	return m.withDO(m.DO.Assign(attrs...))
}

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

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

func (m messageRecordDo) FirstOrInit() (*model.MessageRecord, error) {
	if result, err := m.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.MessageRecord), nil
	}
}

func (m messageRecordDo) FirstOrCreate() (*model.MessageRecord, error) {
	if result, err := m.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.MessageRecord), nil
	}
}

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

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

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

func (m messageRecordDo) Scan(result interface{}) (err error) {
	return m.DO.Scan(result)
}

func (m messageRecordDo) Delete(models ...*model.MessageRecord) (result gen.ResultInfo, err error) {
	return m.DO.Delete(models)
}

func (m *messageRecordDo) withDO(do gen.Dao) *messageRecordDo {
	m.DO = *do.(*gen.DO)
	return m
}
