// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/Wei-Shaw/sub2api/ent/promocode"
)

// PromoCode is the model entity for the PromoCode schema.
type PromoCode struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// 优惠码
	Code string `json:"code,omitempty"`
	// 赠送余额金额
	BonusAmount float64 `json:"bonus_amount,omitempty"`
	// 最大使用次数，0表示无限制
	MaxUses int `json:"max_uses,omitempty"`
	// 已使用次数
	UsedCount int `json:"used_count,omitempty"`
	// 状态: active, disabled
	Status string `json:"status,omitempty"`
	// 过期时间，null表示永不过期
	ExpiresAt *time.Time `json:"expires_at,omitempty"`
	// 备注
	Notes *string `json:"notes,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the PromoCodeQuery when eager-loading is set.
	Edges        PromoCodeEdges `json:"edges"`
	selectValues sql.SelectValues
}

// PromoCodeEdges holds the relations/edges for other nodes in the graph.
type PromoCodeEdges struct {
	// UsageRecords holds the value of the usage_records edge.
	UsageRecords []*PromoCodeUsage `json:"usage_records,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// UsageRecordsOrErr returns the UsageRecords value or an error if the edge
// was not loaded in eager-loading.
func (e PromoCodeEdges) UsageRecordsOrErr() ([]*PromoCodeUsage, error) {
	if e.loadedTypes[0] {
		return e.UsageRecords, nil
	}
	return nil, &NotLoadedError{edge: "usage_records"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*PromoCode) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case promocode.FieldBonusAmount:
			values[i] = new(sql.NullFloat64)
		case promocode.FieldID, promocode.FieldMaxUses, promocode.FieldUsedCount:
			values[i] = new(sql.NullInt64)
		case promocode.FieldCode, promocode.FieldStatus, promocode.FieldNotes:
			values[i] = new(sql.NullString)
		case promocode.FieldExpiresAt, promocode.FieldCreatedAt, promocode.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the PromoCode fields.
func (_m *PromoCode) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case promocode.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int64(value.Int64)
		case promocode.FieldCode:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field code", values[i])
			} else if value.Valid {
				_m.Code = value.String
			}
		case promocode.FieldBonusAmount:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field bonus_amount", values[i])
			} else if value.Valid {
				_m.BonusAmount = value.Float64
			}
		case promocode.FieldMaxUses:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field max_uses", values[i])
			} else if value.Valid {
				_m.MaxUses = int(value.Int64)
			}
		case promocode.FieldUsedCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field used_count", values[i])
			} else if value.Valid {
				_m.UsedCount = int(value.Int64)
			}
		case promocode.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = value.String
			}
		case promocode.FieldExpiresAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expires_at", values[i])
			} else if value.Valid {
				_m.ExpiresAt = new(time.Time)
				*_m.ExpiresAt = value.Time
			}
		case promocode.FieldNotes:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field notes", values[i])
			} else if value.Valid {
				_m.Notes = new(string)
				*_m.Notes = value.String
			}
		case promocode.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				_m.CreatedAt = value.Time
			}
		case promocode.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				_m.UpdatedAt = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the PromoCode.
// This includes values selected through modifiers, order, etc.
func (_m *PromoCode) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryUsageRecords queries the "usage_records" edge of the PromoCode entity.
func (_m *PromoCode) QueryUsageRecords() *PromoCodeUsageQuery {
	return NewPromoCodeClient(_m.config).QueryUsageRecords(_m)
}

// Update returns a builder for updating this PromoCode.
// Note that you need to call PromoCode.Unwrap() before calling this method if this PromoCode
// was returned from a transaction, and the transaction was committed or rolled back.
func (_m *PromoCode) Update() *PromoCodeUpdateOne {
	return NewPromoCodeClient(_m.config).UpdateOne(_m)
}

// Unwrap unwraps the PromoCode entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (_m *PromoCode) Unwrap() *PromoCode {
	_tx, ok := _m.config.driver.(*txDriver)
	if !ok {
		panic("ent: PromoCode is not a transactional entity")
	}
	_m.config.driver = _tx.drv
	return _m
}

// String implements the fmt.Stringer.
func (_m *PromoCode) String() string {
	var builder strings.Builder
	builder.WriteString("PromoCode(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("code=")
	builder.WriteString(_m.Code)
	builder.WriteString(", ")
	builder.WriteString("bonus_amount=")
	builder.WriteString(fmt.Sprintf("%v", _m.BonusAmount))
	builder.WriteString(", ")
	builder.WriteString("max_uses=")
	builder.WriteString(fmt.Sprintf("%v", _m.MaxUses))
	builder.WriteString(", ")
	builder.WriteString("used_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.UsedCount))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(_m.Status)
	builder.WriteString(", ")
	if v := _m.ExpiresAt; v != nil {
		builder.WriteString("expires_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.Notes; v != nil {
		builder.WriteString("notes=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// PromoCodes is a parsable slice of PromoCode.
type PromoCodes []*PromoCode
