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

import (
	"context"
	"database/sql"

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

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

	"gorm.io/plugin/dbresolver"

	"fafa-crawler/src/models"
)

func newLibraryProductImage(db *gorm.DB, opts ...gen.DOOption) libraryProductImage {
	_libraryProductImage := libraryProductImage{}

	_libraryProductImage.libraryProductImageDo.UseDB(db, opts...)
	_libraryProductImage.libraryProductImageDo.UseModel(&models.LibraryProductImage{})

	tableName := _libraryProductImage.libraryProductImageDo.TableName()
	_libraryProductImage.ALL = field.NewAsterisk(tableName)
	_libraryProductImage.ID = field.NewUint64(tableName, "id")
	_libraryProductImage.ProductID = field.NewUint64(tableName, "product_id")
	_libraryProductImage.ImageURL = field.NewString(tableName, "image_url")
	_libraryProductImage.IsMain = field.NewInt32(tableName, "is_main")
	_libraryProductImage.Seq = field.NewInt32(tableName, "seq")
	_libraryProductImage.Status = field.NewInt32(tableName, "status")
	_libraryProductImage.CreatedAt = field.NewTime(tableName, "created_at")
	_libraryProductImage.UpdatedAt = field.NewTime(tableName, "updated_at")

	_libraryProductImage.fillFieldMap()

	return _libraryProductImage
}

// libraryProductImage 商品库商品图片
type libraryProductImage struct {
	libraryProductImageDo

	ALL       field.Asterisk
	ID        field.Uint64 // 图片ID
	ProductID field.Uint64 // 商品ID
	ImageURL  field.String // 图片地址
	IsMain    field.Int32  // 是否主图 1-主图 0-副图
	Seq       field.Int32  // 排序值，越小越前面
	Status    field.Int32  // 状态 1-有效 2-无效
	CreatedAt field.Time   // 创建时间
	UpdatedAt field.Time   // 更新时间

	fieldMap map[string]field.Expr
}

func (l libraryProductImage) Table(newTableName string) *libraryProductImage {
	l.libraryProductImageDo.UseTable(newTableName)
	return l.updateTableName(newTableName)
}

func (l libraryProductImage) As(alias string) *libraryProductImage {
	l.libraryProductImageDo.DO = *(l.libraryProductImageDo.As(alias).(*gen.DO))
	return l.updateTableName(alias)
}

func (l *libraryProductImage) updateTableName(table string) *libraryProductImage {
	l.ALL = field.NewAsterisk(table)
	l.ID = field.NewUint64(table, "id")
	l.ProductID = field.NewUint64(table, "product_id")
	l.ImageURL = field.NewString(table, "image_url")
	l.IsMain = field.NewInt32(table, "is_main")
	l.Seq = field.NewInt32(table, "seq")
	l.Status = field.NewInt32(table, "status")
	l.CreatedAt = field.NewTime(table, "created_at")
	l.UpdatedAt = field.NewTime(table, "updated_at")

	l.fillFieldMap()

	return l
}

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

func (l *libraryProductImage) fillFieldMap() {
	l.fieldMap = make(map[string]field.Expr, 8)
	l.fieldMap["id"] = l.ID
	l.fieldMap["product_id"] = l.ProductID
	l.fieldMap["image_url"] = l.ImageURL
	l.fieldMap["is_main"] = l.IsMain
	l.fieldMap["seq"] = l.Seq
	l.fieldMap["status"] = l.Status
	l.fieldMap["created_at"] = l.CreatedAt
	l.fieldMap["updated_at"] = l.UpdatedAt
}

func (l libraryProductImage) clone(db *gorm.DB) libraryProductImage {
	l.libraryProductImageDo.ReplaceConnPool(db.Statement.ConnPool)
	return l
}

func (l libraryProductImage) replaceDB(db *gorm.DB) libraryProductImage {
	l.libraryProductImageDo.ReplaceDB(db)
	return l
}

type libraryProductImageDo struct{ gen.DO }

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

func (l libraryProductImageDo) Debug() ILibraryProductImageDo {
	return l.withDO(l.DO.Debug())
}

func (l libraryProductImageDo) WithContext(ctx context.Context) ILibraryProductImageDo {
	return l.withDO(l.DO.WithContext(ctx))
}

func (l libraryProductImageDo) ReadDB() ILibraryProductImageDo {
	return l.Clauses(dbresolver.Read)
}

func (l libraryProductImageDo) WriteDB() ILibraryProductImageDo {
	return l.Clauses(dbresolver.Write)
}

func (l libraryProductImageDo) Session(config *gorm.Session) ILibraryProductImageDo {
	return l.withDO(l.DO.Session(config))
}

func (l libraryProductImageDo) Clauses(conds ...clause.Expression) ILibraryProductImageDo {
	return l.withDO(l.DO.Clauses(conds...))
}

func (l libraryProductImageDo) Returning(value interface{}, columns ...string) ILibraryProductImageDo {
	return l.withDO(l.DO.Returning(value, columns...))
}

func (l libraryProductImageDo) Not(conds ...gen.Condition) ILibraryProductImageDo {
	return l.withDO(l.DO.Not(conds...))
}

func (l libraryProductImageDo) Or(conds ...gen.Condition) ILibraryProductImageDo {
	return l.withDO(l.DO.Or(conds...))
}

func (l libraryProductImageDo) Select(conds ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.Select(conds...))
}

func (l libraryProductImageDo) Where(conds ...gen.Condition) ILibraryProductImageDo {
	return l.withDO(l.DO.Where(conds...))
}

func (l libraryProductImageDo) Order(conds ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.Order(conds...))
}

func (l libraryProductImageDo) Distinct(cols ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.Distinct(cols...))
}

func (l libraryProductImageDo) Omit(cols ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.Omit(cols...))
}

func (l libraryProductImageDo) Join(table schema.Tabler, on ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.Join(table, on...))
}

func (l libraryProductImageDo) LeftJoin(table schema.Tabler, on ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.LeftJoin(table, on...))
}

func (l libraryProductImageDo) RightJoin(table schema.Tabler, on ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.RightJoin(table, on...))
}

func (l libraryProductImageDo) Group(cols ...field.Expr) ILibraryProductImageDo {
	return l.withDO(l.DO.Group(cols...))
}

func (l libraryProductImageDo) Having(conds ...gen.Condition) ILibraryProductImageDo {
	return l.withDO(l.DO.Having(conds...))
}

func (l libraryProductImageDo) Limit(limit int) ILibraryProductImageDo {
	return l.withDO(l.DO.Limit(limit))
}

func (l libraryProductImageDo) Offset(offset int) ILibraryProductImageDo {
	return l.withDO(l.DO.Offset(offset))
}

func (l libraryProductImageDo) Scopes(funcs ...func(gen.Dao) gen.Dao) ILibraryProductImageDo {
	return l.withDO(l.DO.Scopes(funcs...))
}

func (l libraryProductImageDo) Unscoped() ILibraryProductImageDo {
	return l.withDO(l.DO.Unscoped())
}

func (l libraryProductImageDo) Create(values ...*models.LibraryProductImage) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Create(values)
}

func (l libraryProductImageDo) CreateInBatches(values []*models.LibraryProductImage, batchSize int) error {
	return l.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 (l libraryProductImageDo) Save(values ...*models.LibraryProductImage) error {
	if len(values) == 0 {
		return nil
	}
	return l.DO.Save(values)
}

func (l libraryProductImageDo) First() (*models.LibraryProductImage, error) {
	if result, err := l.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibraryProductImage), nil
	}
}

func (l libraryProductImageDo) Take() (*models.LibraryProductImage, error) {
	if result, err := l.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibraryProductImage), nil
	}
}

func (l libraryProductImageDo) Last() (*models.LibraryProductImage, error) {
	if result, err := l.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibraryProductImage), nil
	}
}

func (l libraryProductImageDo) Find() ([]*models.LibraryProductImage, error) {
	result, err := l.DO.Find()
	return result.([]*models.LibraryProductImage), err
}

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

func (l libraryProductImageDo) Attrs(attrs ...field.AssignExpr) ILibraryProductImageDo {
	return l.withDO(l.DO.Attrs(attrs...))
}

func (l libraryProductImageDo) Assign(attrs ...field.AssignExpr) ILibraryProductImageDo {
	return l.withDO(l.DO.Assign(attrs...))
}

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

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

func (l libraryProductImageDo) FirstOrInit() (*models.LibraryProductImage, error) {
	if result, err := l.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibraryProductImage), nil
	}
}

func (l libraryProductImageDo) FirstOrCreate() (*models.LibraryProductImage, error) {
	if result, err := l.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*models.LibraryProductImage), nil
	}
}

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

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

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

func (l libraryProductImageDo) Scan(result interface{}) (err error) {
	return l.DO.Scan(result)
}

func (l libraryProductImageDo) Delete(models ...*models.LibraryProductImage) (result gen.ResultInfo, err error) {
	return l.DO.Delete(models)
}

func (l *libraryProductImageDo) withDO(do gen.Dao) *libraryProductImageDo {
	l.DO = *do.(*gen.DO)
	return l
}
