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

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

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

	"gorm.io/plugin/dbresolver"

	"mini-chat/internal/repository/mysql/model"
)

func newAppMessageLog(db *gorm.DB, opts ...gen.DOOption) appMessageLog {
	_appMessageLog := appMessageLog{}

	_appMessageLog.appMessageLogDo.UseDB(db, opts...)
	_appMessageLog.appMessageLogDo.UseModel(&model.AppMessageLog{})

	tableName := _appMessageLog.appMessageLogDo.TableName()
	_appMessageLog.ALL = field.NewAsterisk(tableName)
	_appMessageLog.ID = field.NewInt32(tableName, "id")
	_appMessageLog.AppID = field.NewString(tableName, "app_id")
	_appMessageLog.SenderID = field.NewString(tableName, "sender_id")
	_appMessageLog.SenderName = field.NewString(tableName, "sender_name")
	_appMessageLog.SendTime = field.NewTime(tableName, "send_time")
	_appMessageLog.ReceiverID = field.NewString(tableName, "receiver_id")
	_appMessageLog.MsgType = field.NewInt32(tableName, "msg_type")
	_appMessageLog.Content = field.NewString(tableName, "content")
	_appMessageLog.CreatedAt = field.NewTime(tableName, "created_at")
	_appMessageLog.IsRead = field.NewInt32(tableName, "is_read")

	_appMessageLog.fillFieldMap()

	return _appMessageLog
}

// appMessageLog 消息日志表
type appMessageLog struct {
	appMessageLogDo

	ALL        field.Asterisk
	ID         field.Int32  // 主键ID
	AppID      field.String // 小程序ID
	SenderID   field.String // 发送人ID
	SenderName field.String // 发送人昵称
	SendTime   field.Time   // 发送时间
	ReceiverID field.String // 接收人ID
	MsgType    field.Int32  // 消息类型(1:文本 2:图片)
	Content    field.String // 消息内容
	CreatedAt  field.Time   // 创建时间
	IsRead     field.Int32  // 是否已读(0:未读 1:已读)

	fieldMap map[string]field.Expr
}

func (a appMessageLog) Table(newTableName string) *appMessageLog {
	a.appMessageLogDo.UseTable(newTableName)
	return a.updateTableName(newTableName)
}

func (a appMessageLog) As(alias string) *appMessageLog {
	a.appMessageLogDo.DO = *(a.appMessageLogDo.As(alias).(*gen.DO))
	return a.updateTableName(alias)
}

func (a *appMessageLog) updateTableName(table string) *appMessageLog {
	a.ALL = field.NewAsterisk(table)
	a.ID = field.NewInt32(table, "id")
	a.AppID = field.NewString(table, "app_id")
	a.SenderID = field.NewString(table, "sender_id")
	a.SenderName = field.NewString(table, "sender_name")
	a.SendTime = field.NewTime(table, "send_time")
	a.ReceiverID = field.NewString(table, "receiver_id")
	a.MsgType = field.NewInt32(table, "msg_type")
	a.Content = field.NewString(table, "content")
	a.CreatedAt = field.NewTime(table, "created_at")
	a.IsRead = field.NewInt32(table, "is_read")

	a.fillFieldMap()

	return a
}

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

func (a *appMessageLog) fillFieldMap() {
	a.fieldMap = make(map[string]field.Expr, 10)
	a.fieldMap["id"] = a.ID
	a.fieldMap["app_id"] = a.AppID
	a.fieldMap["sender_id"] = a.SenderID
	a.fieldMap["sender_name"] = a.SenderName
	a.fieldMap["send_time"] = a.SendTime
	a.fieldMap["receiver_id"] = a.ReceiverID
	a.fieldMap["msg_type"] = a.MsgType
	a.fieldMap["content"] = a.Content
	a.fieldMap["created_at"] = a.CreatedAt
	a.fieldMap["is_read"] = a.IsRead
}

func (a appMessageLog) clone(db *gorm.DB) appMessageLog {
	a.appMessageLogDo.ReplaceConnPool(db.Statement.ConnPool)
	return a
}

func (a appMessageLog) replaceDB(db *gorm.DB) appMessageLog {
	a.appMessageLogDo.ReplaceDB(db)
	return a
}

type appMessageLogDo struct{ gen.DO }

func (a appMessageLogDo) Debug() *appMessageLogDo {
	return a.withDO(a.DO.Debug())
}

func (a appMessageLogDo) WithContext(ctx context.Context) *appMessageLogDo {
	return a.withDO(a.DO.WithContext(ctx))
}

func (a appMessageLogDo) ReadDB() *appMessageLogDo {
	return a.Clauses(dbresolver.Read)
}

func (a appMessageLogDo) WriteDB() *appMessageLogDo {
	return a.Clauses(dbresolver.Write)
}

func (a appMessageLogDo) Session(config *gorm.Session) *appMessageLogDo {
	return a.withDO(a.DO.Session(config))
}

func (a appMessageLogDo) Clauses(conds ...clause.Expression) *appMessageLogDo {
	return a.withDO(a.DO.Clauses(conds...))
}

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

func (a appMessageLogDo) Not(conds ...gen.Condition) *appMessageLogDo {
	return a.withDO(a.DO.Not(conds...))
}

func (a appMessageLogDo) Or(conds ...gen.Condition) *appMessageLogDo {
	return a.withDO(a.DO.Or(conds...))
}

func (a appMessageLogDo) Select(conds ...field.Expr) *appMessageLogDo {
	return a.withDO(a.DO.Select(conds...))
}

func (a appMessageLogDo) Where(conds ...gen.Condition) *appMessageLogDo {
	return a.withDO(a.DO.Where(conds...))
}

func (a appMessageLogDo) Order(conds ...field.Expr) *appMessageLogDo {
	return a.withDO(a.DO.Order(conds...))
}

func (a appMessageLogDo) Distinct(cols ...field.Expr) *appMessageLogDo {
	return a.withDO(a.DO.Distinct(cols...))
}

func (a appMessageLogDo) Omit(cols ...field.Expr) *appMessageLogDo {
	return a.withDO(a.DO.Omit(cols...))
}

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

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

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

func (a appMessageLogDo) Group(cols ...field.Expr) *appMessageLogDo {
	return a.withDO(a.DO.Group(cols...))
}

func (a appMessageLogDo) Having(conds ...gen.Condition) *appMessageLogDo {
	return a.withDO(a.DO.Having(conds...))
}

func (a appMessageLogDo) Limit(limit int) *appMessageLogDo {
	return a.withDO(a.DO.Limit(limit))
}

func (a appMessageLogDo) Offset(offset int) *appMessageLogDo {
	return a.withDO(a.DO.Offset(offset))
}

func (a appMessageLogDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *appMessageLogDo {
	return a.withDO(a.DO.Scopes(funcs...))
}

func (a appMessageLogDo) Unscoped() *appMessageLogDo {
	return a.withDO(a.DO.Unscoped())
}

func (a appMessageLogDo) Create(values ...*model.AppMessageLog) error {
	if len(values) == 0 {
		return nil
	}
	return a.DO.Create(values)
}

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

func (a appMessageLogDo) First() (*model.AppMessageLog, error) {
	if result, err := a.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppMessageLog), nil
	}
}

func (a appMessageLogDo) Take() (*model.AppMessageLog, error) {
	if result, err := a.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppMessageLog), nil
	}
}

func (a appMessageLogDo) Last() (*model.AppMessageLog, error) {
	if result, err := a.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppMessageLog), nil
	}
}

func (a appMessageLogDo) Find() ([]*model.AppMessageLog, error) {
	result, err := a.DO.Find()
	return result.([]*model.AppMessageLog), err
}

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

func (a appMessageLogDo) Attrs(attrs ...field.AssignExpr) *appMessageLogDo {
	return a.withDO(a.DO.Attrs(attrs...))
}

func (a appMessageLogDo) Assign(attrs ...field.AssignExpr) *appMessageLogDo {
	return a.withDO(a.DO.Assign(attrs...))
}

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

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

func (a appMessageLogDo) FirstOrInit() (*model.AppMessageLog, error) {
	if result, err := a.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppMessageLog), nil
	}
}

func (a appMessageLogDo) FirstOrCreate() (*model.AppMessageLog, error) {
	if result, err := a.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.AppMessageLog), nil
	}
}

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

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

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

func (a appMessageLogDo) Scan(result interface{}) (err error) {
	return a.DO.Scan(result)
}

func (a appMessageLogDo) Delete(models ...*model.AppMessageLog) (result gen.ResultInfo, err error) {
	return a.DO.Delete(models)
}

func (a *appMessageLogDo) withDO(do gen.Dao) *appMessageLogDo {
	a.DO = *do.(*gen.DO)
	return a
}
