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

	_opsShippingStats.opsShippingStatsDo.UseDB(db, opts...)
	_opsShippingStats.opsShippingStatsDo.UseModel(&model.OpsShippingStats{})

	tableName := _opsShippingStats.opsShippingStatsDo.TableName()
	_opsShippingStats.ALL = field.NewAsterisk(tableName)
	_opsShippingStats.ID = field.NewInt64(tableName, "id")
	_opsShippingStats.ShippedAt = field.NewTime(tableName, "shipped_at")
	_opsShippingStats.ProductID = field.NewInt64(tableName, "product_id")
	_opsShippingStats.ProductName = field.NewString(tableName, "product_name")
	_opsShippingStats.ProductPriceCents = field.NewInt64(tableName, "product_price_cents")
	_opsShippingStats.ShippedQty = field.NewInt64(tableName, "shipped_qty")
	_opsShippingStats.UserID = field.NewInt64(tableName, "user_id")
	_opsShippingStats.UserName = field.NewString(tableName, "user_name")
	_opsShippingStats.UserAddressText = field.NewString(tableName, "user_address_text")
	_opsShippingStats.ExpressCode = field.NewString(tableName, "express_code")
	_opsShippingStats.ExpressNo = field.NewString(tableName, "express_no")
	_opsShippingStats.OrderID = field.NewInt64(tableName, "order_id")
	_opsShippingStats.OrderNo = field.NewString(tableName, "order_no")
	_opsShippingStats.OrderQty = field.NewInt64(tableName, "order_qty")
	_opsShippingStats.OrderAmountCents = field.NewInt64(tableName, "order_amount_cents")
	_opsShippingStats.ProfitLossCents = field.NewInt64(tableName, "profit_loss_cents")
	_opsShippingStats.OrderSourceType = field.NewInt32(tableName, "order_source_type")
	_opsShippingStats.OrderSourceText = field.NewString(tableName, "order_source_text")
	_opsShippingStats.Payer = field.NewString(tableName, "payer")
	_opsShippingStats.CreatedAt = field.NewTime(tableName, "created_at")

	_opsShippingStats.fillFieldMap()

	return _opsShippingStats
}

// opsShippingStats 运营发货统计（离线明细表）
type opsShippingStats struct {
	opsShippingStatsDo

	ALL               field.Asterisk
	ID                field.Int64  // 主键ID
	ShippedAt         field.Time   // 发货时间（来源：shipping_records.shipped_at）
	ProductID         field.Int64  // 商品ID（优先取 shipping_records.product_id，否则取 order_items.product_id）
	ProductName       field.String // 产品名称（优先取 order_items.title 快照，否则取 products.name）
	ProductPriceCents field.Int64  // 产品价格（单位分；优先取 shipping_records.price，其次 order_items.price，最后 products.price）
	ShippedQty        field.Int64  // 发货数量（来源：shipping_records.quantity）
	UserID            field.Int64  // 收件用户ID（来源：user_addresses.user_id）
	UserName          field.String // 收件人姓名（来源：user_addresses.name）
	UserAddressText   field.String // 收件人地址（拼接 province/city/district/address）
	ExpressCode       field.String // 快递公司编码（来源：shipping_records.express_code）
	ExpressNo         field.String // 物流单号（来源：shipping_records.express_no）
	OrderID           field.Int64  // 订单ID（来源：shipping_records.order_id）
	OrderNo           field.String // 订单号（来源：orders.order_no）
	OrderQty          field.Int64  // 下单数量（订单维度总件数，聚合 order_items.quantity）
	OrderAmountCents  field.Int64  // 下单金额（单位分；订单实付金额 orders.actual_amount）
	ProfitLossCents   field.Int64  // 盈亏金额（单位分；行价格×发货数量 − 订单实付金额；可按运营口径调整）
	OrderSourceType   field.Int32  // 下单来源类型码（1商城直购 2抽奖票据 3其他；来源：orders.source_type）
	OrderSourceText   field.String // 下单来源中文（由 order_source_type 映射：商城直购/抽奖票据/其他/未知）
	Payer             field.String // 垫付人（当前业务未存；建议新增 shipping_records.payer 或在 remark 规范写入并解析）
	CreatedAt         field.Time   // 统计入库时间

	fieldMap map[string]field.Expr
}

func (o opsShippingStats) Table(newTableName string) *opsShippingStats {
	o.opsShippingStatsDo.UseTable(newTableName)
	return o.updateTableName(newTableName)
}

func (o opsShippingStats) As(alias string) *opsShippingStats {
	o.opsShippingStatsDo.DO = *(o.opsShippingStatsDo.As(alias).(*gen.DO))
	return o.updateTableName(alias)
}

func (o *opsShippingStats) updateTableName(table string) *opsShippingStats {
	o.ALL = field.NewAsterisk(table)
	o.ID = field.NewInt64(table, "id")
	o.ShippedAt = field.NewTime(table, "shipped_at")
	o.ProductID = field.NewInt64(table, "product_id")
	o.ProductName = field.NewString(table, "product_name")
	o.ProductPriceCents = field.NewInt64(table, "product_price_cents")
	o.ShippedQty = field.NewInt64(table, "shipped_qty")
	o.UserID = field.NewInt64(table, "user_id")
	o.UserName = field.NewString(table, "user_name")
	o.UserAddressText = field.NewString(table, "user_address_text")
	o.ExpressCode = field.NewString(table, "express_code")
	o.ExpressNo = field.NewString(table, "express_no")
	o.OrderID = field.NewInt64(table, "order_id")
	o.OrderNo = field.NewString(table, "order_no")
	o.OrderQty = field.NewInt64(table, "order_qty")
	o.OrderAmountCents = field.NewInt64(table, "order_amount_cents")
	o.ProfitLossCents = field.NewInt64(table, "profit_loss_cents")
	o.OrderSourceType = field.NewInt32(table, "order_source_type")
	o.OrderSourceText = field.NewString(table, "order_source_text")
	o.Payer = field.NewString(table, "payer")
	o.CreatedAt = field.NewTime(table, "created_at")

	o.fillFieldMap()

	return o
}

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

func (o *opsShippingStats) fillFieldMap() {
	o.fieldMap = make(map[string]field.Expr, 20)
	o.fieldMap["id"] = o.ID
	o.fieldMap["shipped_at"] = o.ShippedAt
	o.fieldMap["product_id"] = o.ProductID
	o.fieldMap["product_name"] = o.ProductName
	o.fieldMap["product_price_cents"] = o.ProductPriceCents
	o.fieldMap["shipped_qty"] = o.ShippedQty
	o.fieldMap["user_id"] = o.UserID
	o.fieldMap["user_name"] = o.UserName
	o.fieldMap["user_address_text"] = o.UserAddressText
	o.fieldMap["express_code"] = o.ExpressCode
	o.fieldMap["express_no"] = o.ExpressNo
	o.fieldMap["order_id"] = o.OrderID
	o.fieldMap["order_no"] = o.OrderNo
	o.fieldMap["order_qty"] = o.OrderQty
	o.fieldMap["order_amount_cents"] = o.OrderAmountCents
	o.fieldMap["profit_loss_cents"] = o.ProfitLossCents
	o.fieldMap["order_source_type"] = o.OrderSourceType
	o.fieldMap["order_source_text"] = o.OrderSourceText
	o.fieldMap["payer"] = o.Payer
	o.fieldMap["created_at"] = o.CreatedAt
}

func (o opsShippingStats) clone(db *gorm.DB) opsShippingStats {
	o.opsShippingStatsDo.ReplaceConnPool(db.Statement.ConnPool)
	return o
}

func (o opsShippingStats) replaceDB(db *gorm.DB) opsShippingStats {
	o.opsShippingStatsDo.ReplaceDB(db)
	return o
}

type opsShippingStatsDo struct{ gen.DO }

func (o opsShippingStatsDo) Debug() *opsShippingStatsDo {
	return o.withDO(o.DO.Debug())
}

func (o opsShippingStatsDo) WithContext(ctx context.Context) *opsShippingStatsDo {
	return o.withDO(o.DO.WithContext(ctx))
}

func (o opsShippingStatsDo) ReadDB() *opsShippingStatsDo {
	return o.Clauses(dbresolver.Read)
}

func (o opsShippingStatsDo) WriteDB() *opsShippingStatsDo {
	return o.Clauses(dbresolver.Write)
}

func (o opsShippingStatsDo) Session(config *gorm.Session) *opsShippingStatsDo {
	return o.withDO(o.DO.Session(config))
}

func (o opsShippingStatsDo) Clauses(conds ...clause.Expression) *opsShippingStatsDo {
	return o.withDO(o.DO.Clauses(conds...))
}

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

func (o opsShippingStatsDo) Not(conds ...gen.Condition) *opsShippingStatsDo {
	return o.withDO(o.DO.Not(conds...))
}

func (o opsShippingStatsDo) Or(conds ...gen.Condition) *opsShippingStatsDo {
	return o.withDO(o.DO.Or(conds...))
}

func (o opsShippingStatsDo) Select(conds ...field.Expr) *opsShippingStatsDo {
	return o.withDO(o.DO.Select(conds...))
}

func (o opsShippingStatsDo) Where(conds ...gen.Condition) *opsShippingStatsDo {
	return o.withDO(o.DO.Where(conds...))
}

func (o opsShippingStatsDo) Order(conds ...field.Expr) *opsShippingStatsDo {
	return o.withDO(o.DO.Order(conds...))
}

func (o opsShippingStatsDo) Distinct(cols ...field.Expr) *opsShippingStatsDo {
	return o.withDO(o.DO.Distinct(cols...))
}

func (o opsShippingStatsDo) Omit(cols ...field.Expr) *opsShippingStatsDo {
	return o.withDO(o.DO.Omit(cols...))
}

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

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

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

func (o opsShippingStatsDo) Group(cols ...field.Expr) *opsShippingStatsDo {
	return o.withDO(o.DO.Group(cols...))
}

func (o opsShippingStatsDo) Having(conds ...gen.Condition) *opsShippingStatsDo {
	return o.withDO(o.DO.Having(conds...))
}

func (o opsShippingStatsDo) Limit(limit int) *opsShippingStatsDo {
	return o.withDO(o.DO.Limit(limit))
}

func (o opsShippingStatsDo) Offset(offset int) *opsShippingStatsDo {
	return o.withDO(o.DO.Offset(offset))
}

func (o opsShippingStatsDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *opsShippingStatsDo {
	return o.withDO(o.DO.Scopes(funcs...))
}

func (o opsShippingStatsDo) Unscoped() *opsShippingStatsDo {
	return o.withDO(o.DO.Unscoped())
}

func (o opsShippingStatsDo) Create(values ...*model.OpsShippingStats) error {
	if len(values) == 0 {
		return nil
	}
	return o.DO.Create(values)
}

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

func (o opsShippingStatsDo) First() (*model.OpsShippingStats, error) {
	if result, err := o.DO.First(); err != nil {
		return nil, err
	} else {
		return result.(*model.OpsShippingStats), nil
	}
}

func (o opsShippingStatsDo) Take() (*model.OpsShippingStats, error) {
	if result, err := o.DO.Take(); err != nil {
		return nil, err
	} else {
		return result.(*model.OpsShippingStats), nil
	}
}

func (o opsShippingStatsDo) Last() (*model.OpsShippingStats, error) {
	if result, err := o.DO.Last(); err != nil {
		return nil, err
	} else {
		return result.(*model.OpsShippingStats), nil
	}
}

func (o opsShippingStatsDo) Find() ([]*model.OpsShippingStats, error) {
	result, err := o.DO.Find()
	return result.([]*model.OpsShippingStats), err
}

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

func (o opsShippingStatsDo) Attrs(attrs ...field.AssignExpr) *opsShippingStatsDo {
	return o.withDO(o.DO.Attrs(attrs...))
}

func (o opsShippingStatsDo) Assign(attrs ...field.AssignExpr) *opsShippingStatsDo {
	return o.withDO(o.DO.Assign(attrs...))
}

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

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

func (o opsShippingStatsDo) FirstOrInit() (*model.OpsShippingStats, error) {
	if result, err := o.DO.FirstOrInit(); err != nil {
		return nil, err
	} else {
		return result.(*model.OpsShippingStats), nil
	}
}

func (o opsShippingStatsDo) FirstOrCreate() (*model.OpsShippingStats, error) {
	if result, err := o.DO.FirstOrCreate(); err != nil {
		return nil, err
	} else {
		return result.(*model.OpsShippingStats), nil
	}
}

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

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

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

func (o opsShippingStatsDo) Scan(result interface{}) (err error) {
	return o.DO.Scan(result)
}

func (o opsShippingStatsDo) Delete(models ...*model.OpsShippingStats) (result gen.ResultInfo, err error) {
	return o.DO.Delete(models)
}

func (o *opsShippingStatsDo) withDO(do gen.Dao) *opsShippingStatsDo {
	o.DO = *do.(*gen.DO)
	return o
}
