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

	"bindbox-game/internal/repository/mysql/model"
)

func newShippingRecords(db *gorm.DB, opts ...gen.DOOption) shippingRecords {
	_shippingRecords := shippingRecords{}

	_shippingRecords.shippingRecordsDo.UseDB(db, opts...)
	_shippingRecords.shippingRecordsDo.UseModel(&model.ShippingRecords{})

	tableName := _shippingRecords.shippingRecordsDo.TableName()
	_shippingRecords.ALL = field.NewAsterisk(tableName)
	_shippingRecords.ID = field.NewInt64(tableName, "id")
	_shippingRecords.CreatedAt = field.NewTime(tableName, "created_at")
	_shippingRecords.UpdatedAt = field.NewTime(tableName, "updated_at")
	_shippingRecords.UserID = field.NewInt64(tableName, "user_id")
	_shippingRecords.OrderID = field.NewInt64(tableName, "order_id")
	_shippingRecords.OrderItemID = field.NewInt64(tableName, "order_item_id")
	_shippingRecords.InventoryID = field.NewInt64(tableName, "inventory_id")
	_shippingRecords.ProductID = field.NewInt64(tableName, "product_id")
	_shippingRecords.Quantity = field.NewInt64(tableName, "quantity")
	_shippingRecords.Price = field.NewInt64(tableName, "price")
	_shippingRecords.AddressID = field.NewInt64(tableName, "address_id")
	_shippingRecords.Status = field.NewInt32(tableName, "status")
	_shippingRecords.ExpressCode = field.NewString(tableName, "express_code")
	_shippingRecords.ExpressNo = field.NewString(tableName, "express_no")
	_shippingRecords.BatchNo = field.NewString(tableName, "batch_no")
	_shippingRecords.ShippedAt = field.NewTime(tableName, "shipped_at")
	_shippingRecords.ReceivedAt = field.NewTime(tableName, "received_at")
	_shippingRecords.Remark = field.NewString(tableName, "remark")

	_shippingRecords.fillFieldMap()

	return _shippingRecords
}

// shippingRecords 发货记录（合并：单表）
type shippingRecords struct {
	shippingRecordsDo

	ALL         field.Asterisk
	ID          field.Int64  // 主键ID
	CreatedAt   field.Time   // 创建时间
	UpdatedAt   field.Time   // 更新时间
	UserID      field.Int64  // 收件用户ID（user_members.id）
	OrderID     field.Int64  // 关联订单ID（orders.id，抽奖产物可为空）
	OrderItemID field.Int64  // 订单行ID（商城直发时使用）
	InventoryID field.Int64  // 资产ID（抽奖产物发货时使用）
	ProductID   field.Int64  // 商品ID（统计/冗余）
	Quantity    field.Int64  // 发货数量
	Price       field.Int64  // 行价格（分，可选）
	AddressID   field.Int64  // 收货地址ID（user_addresses.id）
	Status      field.Int32  // 发货状态：1待发货 2已发货 3已签收 4异常
	ExpressCode field.String // 快递公司编码
	ExpressNo   field.String // 运单号
	BatchNo     field.String // 批次号（批量发货时用于聚合）
	ShippedAt   field.Time   // 发货时间
	ReceivedAt  field.Time   // 签收时间
	Remark      field.String // 备注

	fieldMap map[string]field.Expr
}

func (s shippingRecords) Table(newTableName string) *shippingRecords {
	s.shippingRecordsDo.UseTable(newTableName)
	return s.updateTableName(newTableName)
}

func (s shippingRecords) As(alias string) *shippingRecords {
	s.shippingRecordsDo.DO = *(s.shippingRecordsDo.As(alias).(*gen.DO))
	return s.updateTableName(alias)
}

func (s *shippingRecords) updateTableName(table string) *shippingRecords {
	s.ALL = field.NewAsterisk(table)
	s.ID = field.NewInt64(table, "id")
	s.CreatedAt = field.NewTime(table, "created_at")
	s.UpdatedAt = field.NewTime(table, "updated_at")
	s.UserID = field.NewInt64(table, "user_id")
	s.OrderID = field.NewInt64(table, "order_id")
	s.OrderItemID = field.NewInt64(table, "order_item_id")
	s.InventoryID = field.NewInt64(table, "inventory_id")
	s.ProductID = field.NewInt64(table, "product_id")
	s.Quantity = field.NewInt64(table, "quantity")
	s.Price = field.NewInt64(table, "price")
	s.AddressID = field.NewInt64(table, "address_id")
	s.Status = field.NewInt32(table, "status")
	s.ExpressCode = field.NewString(table, "express_code")
	s.ExpressNo = field.NewString(table, "express_no")
	s.BatchNo = field.NewString(table, "batch_no")
	s.ShippedAt = field.NewTime(table, "shipped_at")
	s.ReceivedAt = field.NewTime(table, "received_at")
	s.Remark = field.NewString(table, "remark")

	s.fillFieldMap()

	return s
}

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

func (s *shippingRecords) fillFieldMap() {
	s.fieldMap = make(map[string]field.Expr, 18)
	s.fieldMap["id"] = s.ID
	s.fieldMap["created_at"] = s.CreatedAt
	s.fieldMap["updated_at"] = s.UpdatedAt
	s.fieldMap["user_id"] = s.UserID
	s.fieldMap["order_id"] = s.OrderID
	s.fieldMap["order_item_id"] = s.OrderItemID
	s.fieldMap["inventory_id"] = s.InventoryID
	s.fieldMap["product_id"] = s.ProductID
	s.fieldMap["quantity"] = s.Quantity
	s.fieldMap["price"] = s.Price
	s.fieldMap["address_id"] = s.AddressID
	s.fieldMap["status"] = s.Status
	s.fieldMap["express_code"] = s.ExpressCode
	s.fieldMap["express_no"] = s.ExpressNo
	s.fieldMap["batch_no"] = s.BatchNo
	s.fieldMap["shipped_at"] = s.ShippedAt
	s.fieldMap["received_at"] = s.ReceivedAt
	s.fieldMap["remark"] = s.Remark
}

func (s shippingRecords) clone(db *gorm.DB) shippingRecords {
	s.shippingRecordsDo.ReplaceConnPool(db.Statement.ConnPool)
	return s
}

func (s shippingRecords) replaceDB(db *gorm.DB) shippingRecords {
	s.shippingRecordsDo.ReplaceDB(db)
	return s
}

type shippingRecordsDo struct{ gen.DO }

func (s shippingRecordsDo) Debug() *shippingRecordsDo {
	return s.withDO(s.DO.Debug())
}

func (s shippingRecordsDo) WithContext(ctx context.Context) *shippingRecordsDo {
	return s.withDO(s.DO.WithContext(ctx))
}

func (s shippingRecordsDo) ReadDB() *shippingRecordsDo {
	return s.Clauses(dbresolver.Read)
}

func (s shippingRecordsDo) WriteDB() *shippingRecordsDo {
	return s.Clauses(dbresolver.Write)
}

func (s shippingRecordsDo) Session(config *gorm.Session) *shippingRecordsDo {
	return s.withDO(s.DO.Session(config))
}

func (s shippingRecordsDo) Clauses(conds ...clause.Expression) *shippingRecordsDo {
	return s.withDO(s.DO.Clauses(conds...))
}

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

func (s shippingRecordsDo) Not(conds ...gen.Condition) *shippingRecordsDo {
	return s.withDO(s.DO.Not(conds...))
}

func (s shippingRecordsDo) Or(conds ...gen.Condition) *shippingRecordsDo {
	return s.withDO(s.DO.Or(conds...))
}

func (s shippingRecordsDo) Select(conds ...field.Expr) *shippingRecordsDo {
	return s.withDO(s.DO.Select(conds...))
}

func (s shippingRecordsDo) Where(conds ...gen.Condition) *shippingRecordsDo {
	return s.withDO(s.DO.Where(conds...))
}

func (s shippingRecordsDo) Order(conds ...field.Expr) *shippingRecordsDo {
	return s.withDO(s.DO.Order(conds...))
}

func (s shippingRecordsDo) Distinct(cols ...field.Expr) *shippingRecordsDo {
	return s.withDO(s.DO.Distinct(cols...))
}

func (s shippingRecordsDo) Omit(cols ...field.Expr) *shippingRecordsDo {
	return s.withDO(s.DO.Omit(cols...))
}

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

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

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

func (s shippingRecordsDo) Group(cols ...field.Expr) *shippingRecordsDo {
	return s.withDO(s.DO.Group(cols...))
}

func (s shippingRecordsDo) Having(conds ...gen.Condition) *shippingRecordsDo {
	return s.withDO(s.DO.Having(conds...))
}

func (s shippingRecordsDo) Limit(limit int) *shippingRecordsDo {
	return s.withDO(s.DO.Limit(limit))
}

func (s shippingRecordsDo) Offset(offset int) *shippingRecordsDo {
	return s.withDO(s.DO.Offset(offset))
}

func (s shippingRecordsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *shippingRecordsDo {
	return s.withDO(s.DO.Scopes(funcs...))
}

func (s shippingRecordsDo) Unscoped() *shippingRecordsDo {
	return s.withDO(s.DO.Unscoped())
}

func (s shippingRecordsDo) Create(values ...*model.ShippingRecords) error {
	if len(values) == 0 {
		return nil
	}
	return s.DO.Create(values)
}

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

func (s shippingRecordsDo) First() (*model.ShippingRecords, error) {
	if result, err := s.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.ShippingRecords), nil
	}
}

func (s shippingRecordsDo) Take() (*model.ShippingRecords, error) {
	if result, err := s.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.ShippingRecords), nil
	}
}

func (s shippingRecordsDo) Last() (*model.ShippingRecords, error) {
	if result, err := s.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.ShippingRecords), nil
	}
}

func (s shippingRecordsDo) Find() ([]*model.ShippingRecords, error) {
	result, err := s.DO.Find()
	return result.([]*model.ShippingRecords), err
}

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

func (s shippingRecordsDo) Attrs(attrs ...field.AssignExpr) *shippingRecordsDo {
	return s.withDO(s.DO.Attrs(attrs...))
}

func (s shippingRecordsDo) Assign(attrs ...field.AssignExpr) *shippingRecordsDo {
	return s.withDO(s.DO.Assign(attrs...))
}

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

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

func (s shippingRecordsDo) FirstOrInit() (*model.ShippingRecords, error) {
	if result, err := s.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.ShippingRecords), nil
	}
}

func (s shippingRecordsDo) FirstOrCreate() (*model.ShippingRecords, error) {
	if result, err := s.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.ShippingRecords), nil
	}
}

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

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

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

func (s shippingRecordsDo) Scan(result interface{}) (err error) {
	return s.DO.Scan(result)
}

func (s shippingRecordsDo) Delete(models ...*model.ShippingRecords) (result gen.ResultInfo, err error) {
	return s.DO.Delete(models)
}

func (s *shippingRecordsDo) withDO(do gen.Dao) *shippingRecordsDo {
	s.DO = *do.(*gen.DO)
	return s
}
